From f24c5bdbfee7320a99ce9203349980c5041bfe82 Mon Sep 17 00:00:00 2001 From: eb Date: Fri, 26 Nov 2021 15:28:43 -0700 Subject: [PATCH] erase all removed clients instead of the first --- src/stationapi/Node.hpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/stationapi/Node.hpp b/src/stationapi/Node.hpp index 6ce98a3..1ab4703 100644 --- a/src/stationapi/Node.hpp +++ b/src/stationapi/Node.hpp @@ -10,17 +10,21 @@ #include template -class Node : public UdpManagerHandler { +class Node : public UdpManagerHandler +{ public: - explicit Node(NodeT* node, const std::string& listenAddress, uint16_t listenPort, bool bindToIp = false) - : node_{node} { + explicit Node(NodeT *node, const std::string &listenAddress, uint16_t listenPort, bool bindToIp = false) + : node_{node} + { UdpManager::Params params; params.handler = this; params.port = listenPort; - if (bindToIp) { - if (listenAddress.length() > sizeof(params.bindIpAddress)) { + if (bindToIp) + { + if (listenAddress.length() > sizeof(params.bindIpAddress)) + { throw std::runtime_error{"Invalid bind ip specified: " + listenAddress}; } @@ -32,16 +36,15 @@ public: virtual ~Node() { udpManager_->Release(); } - void Tick() { + void Tick() + { udpManager_->GiveTime(); - auto remove_iter - = std::remove_if(std::begin(clients_), std::end(clients_), [](auto& client) { - return client->GetConnection()->GetStatus() == UdpConnection::cStatusDisconnected; - }); + auto remove_iter = std::remove_if(std::begin(clients_), std::end(clients_), [](auto &client) + { return client->GetConnection()->GetStatus() == UdpConnection::cStatusDisconnected; }); if (remove_iter != std::end(clients_)) - clients_.erase(remove_iter); + clients_.erase(remove_iter, clients_.end()); OnTick(); } @@ -49,13 +52,14 @@ public: private: virtual void OnTick() = 0; - void OnConnectRequest(UdpConnection* connection) override { + void OnConnectRequest(UdpConnection *connection) override + { AddClient(std::make_unique(connection, node_)); } void AddClient(std::unique_ptr client) { clients_.push_back(std::move(client)); } std::vector> clients_; - NodeT* node_; - UdpManager* udpManager_; + NodeT *node_; + UdpManager *udpManager_; };