mirror of
https://github.com/hoytech/strfry.git
synced 2025-06-17 16:58:50 +00:00
WSConnection improvements: better destruction, logging
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
#include "golpe.h"
|
#include "golpe.h"
|
||||||
|
|
||||||
|
|
||||||
class WSConnection {
|
class WSConnection : NonCopyable {
|
||||||
std::string url;
|
std::string url;
|
||||||
|
|
||||||
uWS::Hub hub;
|
uWS::Hub hub;
|
||||||
@ -26,19 +26,17 @@ class WSConnection {
|
|||||||
std::function<void()> onDisconnect;
|
std::function<void()> onDisconnect;
|
||||||
std::function<void()> onError;
|
std::function<void()> onError;
|
||||||
bool reconnect = true;
|
bool reconnect = true;
|
||||||
|
std::atomic<bool> shutdown = false;
|
||||||
uint64_t reconnectDelayMilliseconds = 5'000;
|
uint64_t reconnectDelayMilliseconds = 5'000;
|
||||||
std::string remoteAddr;
|
std::string remoteAddr;
|
||||||
|
|
||||||
~WSConnection() {
|
~WSConnection() {
|
||||||
close();
|
if (hubGroup || hubTrigger || currWs) LW << "WSConnection destroyed before close";
|
||||||
}
|
}
|
||||||
|
|
||||||
void close() {
|
void close() {
|
||||||
if (hubGroup) hubGroup->close();
|
shutdown = true;
|
||||||
hubGroup = nullptr;
|
trigger();
|
||||||
|
|
||||||
if (hubTrigger) hubTrigger->close();
|
|
||||||
hubTrigger = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Should only be called from the websocket thread (ie within an onConnect or onMessage callback)
|
// Should only be called from the websocket thread (ie within an onConnect or onMessage callback)
|
||||||
@ -58,22 +56,24 @@ class WSConnection {
|
|||||||
void run() {
|
void run() {
|
||||||
hubGroup = hub.createGroup<uWS::CLIENT>(uWS::PERMESSAGE_DEFLATE | uWS::SLIDING_DEFLATE_WINDOW);
|
hubGroup = hub.createGroup<uWS::CLIENT>(uWS::PERMESSAGE_DEFLATE | uWS::SLIDING_DEFLATE_WINDOW);
|
||||||
|
|
||||||
|
|
||||||
auto doConnect = [&](uint64_t delay = 0){
|
auto doConnect = [&](uint64_t delay = 0){
|
||||||
if (delay) std::this_thread::sleep_for(std::chrono::milliseconds(delay));
|
if (delay) std::this_thread::sleep_for(std::chrono::milliseconds(delay));
|
||||||
|
if (shutdown) return;
|
||||||
LI << "Attempting to connect to " << url;
|
LI << "Attempting to connect to " << url;
|
||||||
hub.connect(url, nullptr, {}, 5000, hubGroup);
|
hub.connect(url, nullptr, {}, 5000, hubGroup);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
hubGroup->onConnection([&](uWS::WebSocket<uWS::CLIENT> *ws, uWS::HttpRequest req) {
|
hubGroup->onConnection([&](uWS::WebSocket<uWS::CLIENT> *ws, uWS::HttpRequest req) {
|
||||||
|
if (shutdown) return;
|
||||||
|
|
||||||
if (currWs) {
|
if (currWs) {
|
||||||
currWs->terminate();
|
currWs->terminate();
|
||||||
currWs = nullptr;
|
currWs = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteAddr = ws->getAddress().address;
|
remoteAddr = ws->getAddress().address;
|
||||||
LI << "Connected to " << remoteAddr;
|
LI << "Connected to " << url << " (" << remoteAddr << ")";
|
||||||
|
|
||||||
{
|
{
|
||||||
int optval = 1;
|
int optval = 1;
|
||||||
@ -93,7 +93,9 @@ class WSConnection {
|
|||||||
});
|
});
|
||||||
|
|
||||||
hubGroup->onDisconnection([&](uWS::WebSocket<uWS::CLIENT> *ws, int code, char *message, size_t length) {
|
hubGroup->onDisconnection([&](uWS::WebSocket<uWS::CLIENT> *ws, int code, char *message, size_t length) {
|
||||||
LI << "Disconnected: " << code << "/" << (message ? std::string_view(message, length) : "-");
|
LI << "Disconnected from " << url << " : " << code << "/" << (message ? std::string_view(message, length) : "-");
|
||||||
|
|
||||||
|
if (shutdown) return;
|
||||||
|
|
||||||
if (ws == currWs) {
|
if (ws == currWs) {
|
||||||
currWs = nullptr;
|
currWs = nullptr;
|
||||||
@ -123,6 +125,11 @@ class WSConnection {
|
|||||||
});
|
});
|
||||||
|
|
||||||
std::function<void()> asyncCb = [&]{
|
std::function<void()> asyncCb = [&]{
|
||||||
|
if (shutdown) {
|
||||||
|
terminate();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!onTrigger) return;
|
if (!onTrigger) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -145,4 +152,24 @@ class WSConnection {
|
|||||||
|
|
||||||
hub.run();
|
hub.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void terminate() {
|
||||||
|
if (hubGroup) {
|
||||||
|
hubGroup->close();
|
||||||
|
hubGroup = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hubTrigger) {
|
||||||
|
hubTrigger->close();
|
||||||
|
hubTrigger = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currWs) {
|
||||||
|
currWs->terminate();
|
||||||
|
currWs = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user