diff --git a/build b/build index 8580e7b..b74e882 100644 --- a/build +++ b/build @@ -1 +1 @@ -30 \ No newline at end of file +31 \ No newline at end of file diff --git a/src/framework/UI_WebSocketClientServer.cpp b/src/framework/UI_WebSocketClientServer.cpp index a9346e5..a712d87 100644 --- a/src/framework/UI_WebSocketClientServer.cpp +++ b/src/framework/UI_WebSocketClientServer.cpp @@ -55,10 +55,27 @@ namespace OpenWifi { { } - void UI_WebSocketClientServer::EndConnection([[maybe_unused]] std::lock_guard &G, ClientList::iterator &Client) { + void UI_WebSocketClientServer::run() { + Running_ = true; + while(Running_) { + Poco::Thread::trySleep(2000); + + if(!Running_) + break; + + std::lock_guard G(LocalMutex_); + for(const auto i:ToBeRemoved_) { + std::cout << "Erasing old WS UI connection..." << std::endl; + Clients_.erase(i); + } + UsersConnected_ = Clients_.size(); + } + + } + + void UI_WebSocketClientServer::EndConnection(ClientList::iterator Client) { if(Client->second->SocketRegistered_) { Client->second->SocketRegistered_ = false; - (*Client->second->WS_).shutdown(); Reactor_.removeEventHandler(*Client->second->WS_, Poco::NObserver(*this,&UI_WebSocketClientServer::OnSocketReadable)); @@ -69,28 +86,17 @@ namespace OpenWifi { Poco::NObserver(*this,&UI_WebSocketClientServer::OnSocketError)); } - Clients_.erase(Client); - UsersConnected_ = Clients_.size(); + ToBeRemoved_.push_back(Client); } - void UI_WebSocketClientServer::run() { - Running_ = true ; - Utils::SetThreadName("ws:uiclnt-svr"); - while(Running_) { - Poco::Thread::trySleep(2000); - - if(!Running_) - break; - } - }; - int UI_WebSocketClientServer::Start() { poco_information(Logger(),"Starting..."); GoogleApiKey_ = MicroServiceConfigGetString("google.apikey",""); GeoCodeEnabled_ = !GoogleApiKey_.empty(); ReactorThread_.start(Reactor_); ReactorThread_.setName("ws:ui-reactor"); - // Thr_.start(*this); + CleanerThread_.start(*this); + CleanerThread_.setName("ws:ui-cleaner"); return 0; }; @@ -101,8 +107,8 @@ namespace OpenWifi { Reactor_.stop(); ReactorThread_.join(); Running_ = false; - // Thr_.wakeUp(); - // Thr_.join(); + CleanerThread_.wakeUp(); + CleanerThread_.join(); poco_information(Logger(),"Stopped..."); } }; @@ -173,10 +179,10 @@ namespace OpenWifi { void UI_WebSocketClientServer::OnSocketError([[maybe_unused]] const Poco::AutoPtr &pNf) { std::lock_guard G(LocalMutex_); - auto Client = FindWSClient(G,pNf->socket().impl()->sockfd()); + auto Client = Clients_.find(pNf->socket().impl()->sockfd()); if(Client==end(Clients_)) return; - EndConnection(G,Client); + EndConnection(Client); } void UI_WebSocketClientServer::OnSocketReadable([[maybe_unused]] const Poco::AutoPtr &pNf) { @@ -187,7 +193,7 @@ namespace OpenWifi { try { - Client = FindWSClient(G,pNf->socket().impl()->sockfd()); + Client = Clients_.find(pNf->socket().impl()->sockfd()); if( Client == end(Clients_)) return; @@ -199,7 +205,7 @@ namespace OpenWifi { if (n == 0) { poco_debug(Logger(),fmt::format("CLOSE({}): {} UI Client is closing WS connection.", Client->second->Id_, Client->second->UserName_)); - return EndConnection(G, Client); + return EndConnection(Client); } switch (Op) { @@ -212,7 +218,7 @@ namespace OpenWifi { } break; case Poco::Net::WebSocket::FRAME_OP_CLOSE: { poco_debug(Logger(),fmt::format("CLOSE({}): {} UI Client is closing WS connection.", Client->second->Id_, Client->second->UserName_)); - return EndConnection(G, Client); + return EndConnection(Client); } break; case Poco::Net::WebSocket::FRAME_OP_TEXT: { constexpr const char *DropMessagesCommand = "drop-notifications"; @@ -245,7 +251,7 @@ namespace OpenWifi { std::ostringstream OS; WelcomeMessage.stringify(OS); Client->second->WS_->sendFrame(OS.str().c_str(), (int) OS.str().size()); - return EndConnection(G, Client); + return EndConnection(Client); } } else { Poco::JSON::Parser P; @@ -274,7 +280,7 @@ namespace OpenWifi { } if(CloseConnection) { - return EndConnection(G, Client); + return EndConnection(Client); } } } break; @@ -282,18 +288,16 @@ namespace OpenWifi { } } } catch (...) { - return EndConnection(G, Client); + return EndConnection(Client); } } void UI_WebSocketClientServer::OnSocketShutdown([[maybe_unused]] const Poco::AutoPtr &pNf) { - ClientList::iterator Client; - std::lock_guard G(LocalMutex_); try { - Client = FindWSClient(G, pNf->socket().impl()->sockfd()); + auto Client = Clients_.find(pNf->socket().impl()->sockfd()); if (Client == end(Clients_)) return; - EndConnection(G, Client); + EndConnection(Client); } catch (...) { } diff --git a/src/framework/UI_WebSocketClientServer.h b/src/framework/UI_WebSocketClientServer.h index 95e4a33..dee6a8d 100644 --- a/src/framework/UI_WebSocketClientServer.h +++ b/src/framework/UI_WebSocketClientServer.h @@ -100,21 +100,22 @@ namespace OpenWifi { bool IsFiltered(std::uint64_t id, const UI_WebSocketClientInfo &Client); private: - mutable std::atomic_bool Running_ = false; + volatile bool Running_ = false; std::atomic_uint64_t UsersConnected_=0; - Poco::Thread Thr_; Poco::Net::SocketReactor Reactor_; Poco::Thread ReactorThread_; + Poco::Thread CleanerThread_; std::recursive_mutex LocalMutex_; - bool GeoCodeEnabled_ = false; - std::string GoogleApiKey_; - ClientList Clients_; + bool GeoCodeEnabled_ = false; + std::string GoogleApiKey_; + ClientList Clients_; UI_WebSocketClientProcessor *Processor_ = nullptr; NotificationTypeIdVec NotificationTypes_; Poco::JSON::Object NotificationTypesJSON_; + std::vector ToBeRemoved_; UI_WebSocketClientServer() noexcept; - void EndConnection(std::lock_guard &G, ClientList::iterator & Client); + void EndConnection(ClientList::iterator Client); void OnSocketReadable(const Poco::AutoPtr &pNf); void OnSocketShutdown(const Poco::AutoPtr &pNf);