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_) {
Client->second->SocketRegistered_ = false;
(*Client->second->WS_).shutdown();
Reactor_.removeEventHandler(*Client->second->WS_,
Poco::NObserver<UI_WebSocketClientServer,
Poco::Net::ReadableNotification>(*this,&UI_WebSocketClientServer::OnSocketReadable));
@@ -69,28 +86,17 @@ namespace OpenWifi {
Poco::NObserver<UI_WebSocketClientServer,
Poco::Net::ErrorNotification>(*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<Poco::Net::ErrorNotification> &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<Poco::Net::ReadableNotification> &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<Poco::Net::ShutdownNotification> &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 (...) {
}

View File

@@ -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<ClientList::iterator> ToBeRemoved_;
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 OnSocketShutdown(const Poco::AutoPtr<Poco::Net::ShutdownNotification> &pNf);