Update PR

* Update PR
This commit is contained in:
abraunegg 2025-12-28 06:47:12 +11:00
commit 3146d82ad2
2 changed files with 41 additions and 16 deletions

View file

@ -27,7 +27,7 @@ import log;
// ========== Logging Shim ==========
private void logCurlWebsocketOutput(string s) {
if (debugLogging) {
collectException(addLogEntry("WEBSOCKET: " ~ s, ["debug"]));
addLogEntry("WEBSOCKET: " ~ s, ["debug"]);
}
}
@ -93,11 +93,12 @@ public:
~this() {
if (curl !is null) {
logCurlWebsocketOutput("Cleaning up an instance of a CurlWebSocket object accessing libcurl for HTTP operations");
curl_easy_cleanup(curl);
curl = null;
logCurlWebsocketOutput("Cleaned up an instance of a CurlWebSocket object accessing libcurl for HTTP operations");
}
websocketConnected = false;
logCurlWebsocketOutput("Cleaned-up an instance of a CurlWebSocket object accessing libcurl for HTTP operations");
}
bool isConnected() {
@ -238,8 +239,12 @@ public:
int close(ushort code = 1000, string reason = "") {
logCurlWebsocketOutput("Running curlWebsocket close()");
if (!websocketConnected) return 0;
logCurlWebsocketOutput("Running curlWebsocket close() - websocketConnected = true");
if (!websocketConnected) {
logCurlWebsocketOutput("Websocket alread closed - websocketConnected = false");
return 0;
} else {
logCurlWebsocketOutput("Running curlWebsocket close() - websocketConnected = true");
}
// Build close payload: 2 bytes status code (network order) + optional reason
ubyte[] pay;
@ -251,11 +256,21 @@ public:
auto frame = encodeFrame(0x8, pay); // opcode 0x8 = Close
auto rc = sendAll(frame);
// Even if sending fails, cleanup below so we dont leak.
collectException(logCurlWebsocketOutput("Sending RFC6455 Close (code=" ~ to!string(code) ~ ")"));
logCurlWebsocketOutput("Sending RFC6455 Close (code=" ~ to!string(code) ~ ")");
// Flag we are no longer connected with the websocket
websocketConnected = false;
return rc;
}
// Cleanup curl handler
void cleanupCurlHandle() {
if (curl !is null) {
logCurlWebsocketOutput("Cleaning up an instance of a CurlWebSocket object accessing libcurl for HTTP operations");
curl_easy_cleanup(curl);
curl = null;
logCurlWebsocketOutput("Cleaned up an instance of a CurlWebSocket object accessing libcurl for HTTP operations");
}
}
int sendText(string payload) {
if (!websocketConnected) return -1;

View file

@ -21,7 +21,7 @@ import curlWebsockets;
// ========== Logging Shim ==========
private void logSocketIOOutput(string s) {
if (debugLogging) {
collectException(addLogEntry("SOCKETIO: " ~ s, ["debug"]));
addLogEntry("SOCKETIO: " ~ s, ["debug"]);
}
}
@ -50,16 +50,26 @@ public:
this.parentTid = parentTid;
this.appConfig = appConfig;
}
~this() {
logSocketIOOutput("Signalling to stop a OneDriveSocketIo instance");
stop(); // sets pleaseStop + waits for workerExited
~this(){
logSocketIOOutput("Destroying OneDriveSocketIo Instance");
collectException(ws.close(1000, "client stop"));
logSocketIOOutput("Closed libcurl RFC6455 WebSocket client cleanly");
object.destroy(ws); // call destructor
ws = null;
logSocketIOOutput("Destroyed libcurl RFC6455 WebSocket client cleanly");
if (atomicLoad(workerExited)) {
if (ws !is null) {
logSocketIOOutput("Attempting to destroy libcurl RFC6455 WebSocket client cleanly");
// Worker has exited; safe to close/cleanup/destroy
collectException(ws.close(1000, "client stop"));
object.destroy(ws);
ws = null;
logSocketIOOutput("Destroyed libcurl RFC6455 WebSocket client cleanly");
}
} else {
// Worker still running; DO NOT touch ws/curl from this thread.
logSocketIOOutput("Worker still running; skipping ws destruction to avoid race.");
}
}
void start() {
if (started) return;
// Get current WebSocket Notification URL
@ -93,7 +103,6 @@ public:
waited += stepMs;
}
// Mark not started only after we know we've requested stop
started = false;
@ -234,6 +243,7 @@ private:
if (self.pleaseStop) {
logSocketIOOutput("Stop requested; shutting down run() loop");
collectException(self.ws.close(1000, "stop-requested"));
collectException(self.ws.cleanupCurlHandle());
return;
}