WSConnection clean shutdown, fix hubTrigger ownership (it's deleted by the event loop)

This commit is contained in:
Doug Hoyte
2023-08-16 13:37:03 -04:00
parent aec8dbc33a
commit 42fe1f16ca
3 changed files with 18 additions and 6 deletions

View File

@ -10,8 +10,8 @@ class WSConnection {
std::string url;
uWS::Hub hub;
uWS::Group<uWS::CLIENT> *hubGroup;
std::unique_ptr<uS::Async> hubTrigger;
uWS::Group<uWS::CLIENT> *hubGroup = nullptr;
uS::Async *hubTrigger = nullptr;
uWS::WebSocket<uWS::CLIENT> *currWs = nullptr;
@ -29,6 +29,18 @@ class WSConnection {
uint64_t reconnectDelayMilliseconds = 5'000;
std::string remoteAddr;
~WSConnection() {
close();
}
void close() {
if (hubGroup) hubGroup->close();
hubGroup = nullptr;
if (hubTrigger) hubTrigger->close();
hubTrigger = nullptr;
}
// Should only be called from the websocket thread (ie within an onConnect or onMessage callback)
void send(std::string_view msg, uWS::OpCode op = uWS::OpCode::TEXT, size_t *compressedSize = nullptr) {
if (currWs) {
@ -120,7 +132,7 @@ class WSConnection {
}
};
hubTrigger = std::make_unique<uS::Async>(hub.getLoop());
hubTrigger = new uS::Async(hub.getLoop());
hubTrigger->setData(&asyncCb);
hubTrigger->start([](uS::Async *a){

View File

@ -149,7 +149,7 @@ struct MsgNegentropy : NonCopyable {
struct RelayServer {
std::unique_ptr<uS::Async> hubTrigger;
uS::Async *hubTrigger = nullptr;
// Thread Pools

View File

@ -39,7 +39,7 @@ void RelayServer::runWebsocket(ThreadPool<MsgWebsocket>::Thread &thr) {
};
uWS::Hub hub;
uWS::Group<uWS::SERVER> *hubGroup;
uWS::Group<uWS::SERVER> *hubGroup = nullptr;
flat_hash_map<uint64_t, Connection*> connIdToConnection;
uint64_t nextConnectionId = 1;
bool gracefulShutdown = false;
@ -219,7 +219,7 @@ void RelayServer::runWebsocket(ThreadPool<MsgWebsocket>::Thread &thr) {
}
};
hubTrigger = std::make_unique<uS::Async>(hub.getLoop());
hubTrigger = new uS::Async(hub.getLoop());
hubTrigger->setData(&asyncCb);
hubTrigger->start([](uS::Async *a){