diff --git a/include/TNetwork.h b/include/TNetwork.h index 3b0a754..743f188 100644 --- a/include/TNetwork.h +++ b/include/TNetwork.h @@ -39,6 +39,7 @@ private: void HandleDownload(SOCKET TCPSock); void OnConnect(const std::weak_ptr& c); void TCPClient(const std::weak_ptr& c); + void Looper(const std::weak_ptr& c); int OpenID(); void OnDisconnect(const std::weak_ptr& ClientPtr, bool kicked); void Parse(TClient& c, const std::string& Packet); diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index a0ed7ee..a9660f2 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -484,18 +484,8 @@ void TNetwork::ClientKick(TClient& c, const std::string& R) { c.SetStatus(-2); CloseSocketProper(c.GetTCPSock()); } - -void TNetwork::TCPClient(const std::weak_ptr& c) { - // TODO: the c.expired() might cause issues here, remove if you end up here with your debugger - if (c.expired() || c.lock()->GetTCPSock() == -1) { - mServer.RemoveClient(c); - return; - } - OnConnect(c); - RegisterThread("(" + std::to_string(c.lock()->GetID()) + ") \"" + c.lock()->GetName() + "\""); - while (true) { - if (c.expired()) - break; +void TNetwork::Looper(const std::weak_ptr& c){ + while(!c.expired()) { auto Client = c.lock(); if (Client->GetStatus() < 0) { debug("client status < 0, breaking client loop"); @@ -527,7 +517,31 @@ void TNetwork::TCPClient(const std::weak_ptr& c) { break; } } + }else{ + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } + } +} +void TNetwork::TCPClient(const std::weak_ptr& c) { + // TODO: the c.expired() might cause issues here, remove if you end up here with your debugger + if (c.expired() || c.lock()->GetTCPSock() == -1) { + mServer.RemoveClient(c); + return; + } + OnConnect(c); + RegisterThread("(" + std::to_string(c.lock()->GetID()) + ") \"" + c.lock()->GetName() + "\""); + + std::thread QueueSync(&TNetwork::Looper, this ,c); + + while (true) { + if (c.expired()) + break; + auto Client = c.lock(); + if (Client->GetStatus() < 0) { + debug("client status < 0, breaking client loop"); + break; + } + auto res = TCPRcv(*Client); if (res == "") { debug("TCPRcv error, break client loop"); @@ -535,6 +549,8 @@ void TNetwork::TCPClient(const std::weak_ptr& c) { } TServer::GlobalParser(c, res, mPPSMonitor, *this); } + if(QueueSync.joinable())QueueSync.join(); + if (!c.expired()) { auto Client = c.lock(); OnDisconnect(c, Client->GetStatus() == -2);