ADD: adden a function to delete clients

This commit is contained in:
Henry Winkel
2023-08-10 10:45:08 +02:00
parent 424c4dca15
commit 6d0f7edc67
2 changed files with 20 additions and 4 deletions

View File

@@ -46,6 +46,7 @@ namespace DirectCommunication
std::vector<std::uint32_t> connectedClients_;
bool hasClient(std::uint32_t clientId);
void deleteClient(std::uint32_t clientId);
std::function<void(std::string)> MessageHandle_ = nullptr;

View File

@@ -48,7 +48,14 @@ namespace DirectCommunication
for (auto it = connectedClients_.begin(); it != connectedClients_.end(); ++it) {
zmq::message_t zmqMsg(msg.begin(),msg.end());
zmqMsg.set_routing_id(*it);
try {
socket_.send(zmqMsg,zmq::send_flags::dontwait);
} catch (const std::exception& e) {
LOG_S(ERROR)<<e.what();
LOG_S(ERROR)<< "Routing id: " << *it;
deleteClient(*it);
}
}
}
}
@@ -72,7 +79,6 @@ namespace DirectCommunication
void DirectCommunicationServer::workerFunc_()
{
LOG_S(INFO)<<"worker started";
while (stopWorker_ == false)
{
zmq::message_t msg;
@@ -87,13 +93,13 @@ namespace DirectCommunication
}
if (msg.to_string() == "CLOSE")
{
auto it = std::find(connectedClients_.begin(), connectedClients_.end(), msg.routing_id());
connectedClients_.erase(it);
LOG_S(INFO)<<"CLOSE received from: "<< msg.routing_id();
deleteClient(msg.routing_id());
LOG_S(INFO)<<"remaining connected clients: " <<connectedClients_.size();
}else
{
if (MessageHandle_ != nullptr)
{
LOG_S(INFO)<<"using handle";
MessageHandle_(msg.to_string());
}
else
@@ -119,6 +125,15 @@ namespace DirectCommunication
}
}
void DirectCommunicationServer::deleteClient(std::uint32_t clientId)
{
auto it = std::find(connectedClients_.begin(), connectedClients_.end(), clientId);
if (it != connectedClients_.end() ) {
connectedClients_.erase(it);
}
}
}