stephb9959
2022-11-18 07:24:06 -08:00
parent 66f4742ca5
commit 155d6ba319
3 changed files with 42 additions and 37 deletions

2
build
View File

@@ -1 +1 @@
30 31

View File

@@ -55,10 +55,27 @@ namespace OpenWifi {
{ {
} }
void UI_WebSocketClientServer::EndConnection([[maybe_unused]] std::lock_guard<std::recursive_mutex> &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_) { if(Client->second->SocketRegistered_) {
Client->second->SocketRegistered_ = false; Client->second->SocketRegistered_ = false;
(*Client->second->WS_).shutdown();
Reactor_.removeEventHandler(*Client->second->WS_, Reactor_.removeEventHandler(*Client->second->WS_,
Poco::NObserver<UI_WebSocketClientServer, Poco::NObserver<UI_WebSocketClientServer,
Poco::Net::ReadableNotification>(*this,&UI_WebSocketClientServer::OnSocketReadable)); Poco::Net::ReadableNotification>(*this,&UI_WebSocketClientServer::OnSocketReadable));
@@ -69,28 +86,17 @@ namespace OpenWifi {
Poco::NObserver<UI_WebSocketClientServer, Poco::NObserver<UI_WebSocketClientServer,
Poco::Net::ErrorNotification>(*this,&UI_WebSocketClientServer::OnSocketError)); Poco::Net::ErrorNotification>(*this,&UI_WebSocketClientServer::OnSocketError));
} }
Clients_.erase(Client); ToBeRemoved_.push_back(Client);
UsersConnected_ = Clients_.size();
} }
void UI_WebSocketClientServer::run() {
Running_ = true ;
Utils::SetThreadName("ws:uiclnt-svr");
while(Running_) {
Poco::Thread::trySleep(2000);
if(!Running_)
break;
}
};
int UI_WebSocketClientServer::Start() { int UI_WebSocketClientServer::Start() {
poco_information(Logger(),"Starting..."); poco_information(Logger(),"Starting...");
GoogleApiKey_ = MicroServiceConfigGetString("google.apikey",""); GoogleApiKey_ = MicroServiceConfigGetString("google.apikey","");
GeoCodeEnabled_ = !GoogleApiKey_.empty(); GeoCodeEnabled_ = !GoogleApiKey_.empty();
ReactorThread_.start(Reactor_); ReactorThread_.start(Reactor_);
ReactorThread_.setName("ws:ui-reactor"); ReactorThread_.setName("ws:ui-reactor");
// Thr_.start(*this); CleanerThread_.start(*this);
CleanerThread_.setName("ws:ui-cleaner");
return 0; return 0;
}; };
@@ -101,8 +107,8 @@ namespace OpenWifi {
Reactor_.stop(); Reactor_.stop();
ReactorThread_.join(); ReactorThread_.join();
Running_ = false; Running_ = false;
// Thr_.wakeUp(); CleanerThread_.wakeUp();
// Thr_.join(); CleanerThread_.join();
poco_information(Logger(),"Stopped..."); poco_information(Logger(),"Stopped...");
} }
}; };
@@ -173,10 +179,10 @@ namespace OpenWifi {
void UI_WebSocketClientServer::OnSocketError([[maybe_unused]] const Poco::AutoPtr<Poco::Net::ErrorNotification> &pNf) { void UI_WebSocketClientServer::OnSocketError([[maybe_unused]] const Poco::AutoPtr<Poco::Net::ErrorNotification> &pNf) {
std::lock_guard G(LocalMutex_); 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_)) if(Client==end(Clients_))
return; return;
EndConnection(G,Client); EndConnection(Client);
} }
void UI_WebSocketClientServer::OnSocketReadable([[maybe_unused]] const Poco::AutoPtr<Poco::Net::ReadableNotification> &pNf) { void UI_WebSocketClientServer::OnSocketReadable([[maybe_unused]] const Poco::AutoPtr<Poco::Net::ReadableNotification> &pNf) {
@@ -187,7 +193,7 @@ namespace OpenWifi {
try { try {
Client = FindWSClient(G,pNf->socket().impl()->sockfd()); Client = Clients_.find(pNf->socket().impl()->sockfd());
if( Client == end(Clients_)) if( Client == end(Clients_))
return; return;
@@ -199,7 +205,7 @@ namespace OpenWifi {
if (n == 0) { if (n == 0) {
poco_debug(Logger(),fmt::format("CLOSE({}): {} UI Client is closing WS connection.", Client->second->Id_, Client->second->UserName_)); 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) { switch (Op) {
@@ -212,7 +218,7 @@ namespace OpenWifi {
} break; } break;
case Poco::Net::WebSocket::FRAME_OP_CLOSE: { 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_)); 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; } break;
case Poco::Net::WebSocket::FRAME_OP_TEXT: { case Poco::Net::WebSocket::FRAME_OP_TEXT: {
constexpr const char *DropMessagesCommand = "drop-notifications"; constexpr const char *DropMessagesCommand = "drop-notifications";
@@ -245,7 +251,7 @@ namespace OpenWifi {
std::ostringstream OS; std::ostringstream OS;
WelcomeMessage.stringify(OS); WelcomeMessage.stringify(OS);
Client->second->WS_->sendFrame(OS.str().c_str(), (int) OS.str().size()); Client->second->WS_->sendFrame(OS.str().c_str(), (int) OS.str().size());
return EndConnection(G, Client); return EndConnection(Client);
} }
} else { } else {
Poco::JSON::Parser P; Poco::JSON::Parser P;
@@ -274,7 +280,7 @@ namespace OpenWifi {
} }
if(CloseConnection) { if(CloseConnection) {
return EndConnection(G, Client); return EndConnection(Client);
} }
} }
} break; } break;
@@ -282,18 +288,16 @@ namespace OpenWifi {
} }
} }
} catch (...) { } catch (...) {
return EndConnection(G, Client); return EndConnection(Client);
} }
} }
void UI_WebSocketClientServer::OnSocketShutdown([[maybe_unused]] const Poco::AutoPtr<Poco::Net::ShutdownNotification> &pNf) { void UI_WebSocketClientServer::OnSocketShutdown([[maybe_unused]] const Poco::AutoPtr<Poco::Net::ShutdownNotification> &pNf) {
ClientList::iterator Client;
std::lock_guard G(LocalMutex_);
try { try {
Client = FindWSClient(G, pNf->socket().impl()->sockfd()); auto Client = Clients_.find(pNf->socket().impl()->sockfd());
if (Client == end(Clients_)) if (Client == end(Clients_))
return; return;
EndConnection(G, Client); EndConnection(Client);
} catch (...) { } catch (...) {
} }

View File

@@ -100,21 +100,22 @@ namespace OpenWifi {
bool IsFiltered(std::uint64_t id, const UI_WebSocketClientInfo &Client); bool IsFiltered(std::uint64_t id, const UI_WebSocketClientInfo &Client);
private: private:
mutable std::atomic_bool Running_ = false; volatile bool Running_ = false;
std::atomic_uint64_t UsersConnected_=0; std::atomic_uint64_t UsersConnected_=0;
Poco::Thread Thr_;
Poco::Net::SocketReactor Reactor_; Poco::Net::SocketReactor Reactor_;
Poco::Thread ReactorThread_; Poco::Thread ReactorThread_;
Poco::Thread CleanerThread_;
std::recursive_mutex LocalMutex_; std::recursive_mutex LocalMutex_;
bool GeoCodeEnabled_ = false; bool GeoCodeEnabled_ = false;
std::string GoogleApiKey_; std::string GoogleApiKey_;
ClientList Clients_; ClientList Clients_;
UI_WebSocketClientProcessor *Processor_ = nullptr; UI_WebSocketClientProcessor *Processor_ = nullptr;
NotificationTypeIdVec NotificationTypes_; NotificationTypeIdVec NotificationTypes_;
Poco::JSON::Object NotificationTypesJSON_; Poco::JSON::Object NotificationTypesJSON_;
std::vector<ClientList::iterator> ToBeRemoved_;
UI_WebSocketClientServer() noexcept; UI_WebSocketClientServer() noexcept;
void EndConnection(std::lock_guard<std::recursive_mutex> &G, ClientList::iterator & Client); void EndConnection(ClientList::iterator Client);
void OnSocketReadable(const Poco::AutoPtr<Poco::Net::ReadableNotification> &pNf); void OnSocketReadable(const Poco::AutoPtr<Poco::Net::ReadableNotification> &pNf);
void OnSocketShutdown(const Poco::AutoPtr<Poco::Net::ShutdownNotification> &pNf); void OnSocketShutdown(const Poco::AutoPtr<Poco::Net::ShutdownNotification> &pNf);