Skip to content

Commit

Permalink
Fixed: WebSockets are now correctly reloading and mutex is being reused
Browse files Browse the repository at this point in the history
  • Loading branch information
MrcSnm committed Dec 17, 2024
1 parent 22f7db3 commit cf78200
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/hbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: dlang-community/setup-dlang@4c99aa991ce7d19dd3064de0a4f2f6b2f152e2d7
- uses: actions/checkout@v4
- uses: dlang-community/setup-dlang@v2
- name: 'Install libcurl dev'
run: |
sudo apt-get update
Expand All @@ -45,10 +45,10 @@ jobs:
build-macos:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- uses: dlang-community/setup-dlang@4c99aa991ce7d19dd3064de0a4f2f6b2f152e2d7
- uses: actions/checkout@v4
- uses: dlang-community/setup-dlang@v2
with:
compiler: ldc2
compiler: ldc
- name: 'Build'
run: |
# Build the project, with its main file included, without unittests
Expand All @@ -65,8 +65,8 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v3
- uses: dlang-community/setup-dlang@4c99aa991ce7d19dd3064de0a4f2f6b2f152e2d7
- uses: actions/checkout@v4
- uses: dlang-community/setup-dlang@v2
- name: 'Build'
run: |
# Build the project, with its main file included, without unittests
Expand Down
36 changes: 18 additions & 18 deletions tools/hbuild/source/commons.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ struct Terminal
void color(Color main, Color secondary){if(arsdTerminal) arsdTerminal.color(main, secondary);}
int cursorY()
{
if(arsdTerminal) return arsdTerminal.cursorY;
if(arsdTerminal)
{
arsdTerminal.updateCursorPosition();
return arsdTerminal.cursorY;
}
return 0;
}
string getline(string message)
Expand Down Expand Up @@ -59,7 +63,17 @@ struct Terminal

void hideCursor(){ if(arsdTerminal) arsdTerminal.hideCursor();}
void showCursor(){ if(arsdTerminal) arsdTerminal.showCursor();}
void clearToEndOfLine(){ if(arsdTerminal) arsdTerminal.clearToEndOfLine();}
void clearToEndOfLine()
{
if(arsdTerminal) arsdTerminal.clearToEndOfLine();
flush();
}
void clearLine()
{
moveTo(0, cursorY);
clearToEndOfLine();
flush();
}

void writeln(T...)(T args)
{
Expand Down Expand Up @@ -228,20 +242,6 @@ size_t selectChoiceBase(ref Terminal terminal, ref RealTimeConsoleInput input, C
enum SelectionHint = "Select an option by using W/S or Arrow Up/Down and choose it by pressing Enter.";

static bool isFirst = true;
static void changeChoice(ref Terminal t, Choice[] choices, string title, Choice current, Choice next, int nextCursorOffset)
{
int currCursor = t.cursorY;
t.moveTo(0, currCursor);
t.clearToEndOfLine();
t.write(current.name);

t.moveTo(0, currCursor+nextCursorOffset);
t.clearToEndOfLine();
with(TerminalColors(Color.green, Color.DEFAULT, t))
t.write(">> ", next.name);
t.flush;
}

static void changeChoiceClear(ref Terminal t, Choice[] choices, string title, Choice current, Choice next, int nextCursorOffset, bool bClear)
{
t.color(Color.DEFAULT, Color.DEFAULT);
Expand All @@ -262,7 +262,6 @@ size_t selectChoiceBase(ref Terminal terminal, ref RealTimeConsoleInput input, C
terminal.clear();
int startLine = terminal.cursorY;
terminal.flush();

terminal.moveTo(0, startLine + cast(int)selectedChoice);
terminal.hideCursor();

Expand Down Expand Up @@ -293,9 +292,10 @@ size_t selectChoiceBase(ref Terminal terminal, ref RealTimeConsoleInput input, C
default: goto CheckInput;
}
}
import std.algorithm.searching;
terminal.moveTo(0, cast(int)startLine);
//Title + SelectionHint
foreach(i; 0..choices.length+3)
foreach(i; 0..choices.length+ ( count(selectionTitle, "\n")+2))
terminal.moveTo(0, cast(int)(startLine+i)), terminal.clearToEndOfLine();
terminal.moveTo(0, cast(int)startLine+1); //Jump title
terminal.writelnSuccess(">> ", choices[selectedChoice].name);
Expand Down
24 changes: 15 additions & 9 deletions tools/hbuild/source/websocket_connection.d
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
module websocket_connection;
import core.sync.mutex;
import handy_httpd;

class WebSocketReloadServer: WebSocketMessageHandler
{
// ctx.server.stop(); // Calling stop will gracefully shutdown the server.
Connection* thisConnection;
this()
{
wsMutex = new shared Mutex;
}

private size_t wsID;

override void onConnectionEstablished(WebSocketConnection conn)
{
synchronized
synchronized(wsMutex)
{
connections~= Connection(id);
connections~= thisConnection = new Connection(id);
wsID = id++;
}
}
Expand All @@ -21,14 +27,13 @@ class WebSocketReloadServer: WebSocketMessageHandler
import std.stdio;
import std.algorithm.searching;

Connection conn = Connection(wsID);
string text = msg.payload; //Irrelevant here

synchronized
synchronized(wsMutex)
{
foreach(socketMsg; conn.messages)
foreach(socketMsg; thisConnection.messages)
msg.conn.sendTextMessage(socketMsg);
conn.messages.length = 0;
thisConnection.messages.length = 0;
}

// infoF!"Got TEXT: %s"(msg.payload);
Expand All @@ -40,7 +45,7 @@ class WebSocketReloadServer: WebSocketMessageHandler
import std.stdio;
synchronized
{
ptrdiff_t index = countUntil!((Connection c) => c.id == wsID)(connections);
ptrdiff_t index = countUntil!((Connection* c) => c.id == wsID)(connections);
if(index != -1)
{
connections = connections[0..index]~ connections[index+1..$];
Expand All @@ -53,7 +58,7 @@ class WebSocketReloadServer: WebSocketMessageHandler
}
void pushWebsocketMessage(string message)
{
synchronized
synchronized(wsMutex)
{
foreach(ref conn; connections)
conn.messages~= message;
Expand All @@ -67,4 +72,5 @@ struct Connection
string[] messages;
}
private __gshared size_t id;
private __gshared Connection[] connections;
private __gshared Connection*[] connections;
private shared Mutex wsMutex;

0 comments on commit cf78200

Please sign in to comment.