erase all removed clients instead of the first

This commit is contained in:
eb
2021-11-26 15:28:43 -07:00
parent bf58575b8a
commit f24c5bdbfe

View File

@@ -10,17 +10,21 @@
#include <vector>
template <typename NodeT, typename ClientT>
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<ClientT>(connection, node_));
}
void AddClient(std::unique_ptr<ClientT> client) { clients_.push_back(std::move(client)); }
std::vector<std::unique_ptr<ClientT>> clients_;
NodeT* node_;
UdpManager* udpManager_;
NodeT *node_;
UdpManager *udpManager_;
};