Loading src/event/iocp.cpp +13 −7 Changes for src/event/iocp.cpp: 13 added lines, 7 removed lines. Original line number Diff line number Diff line Loading @@ -30,6 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <chrono> #include <memory> #include <mutex> #include <shared_mutex> #include <cstring> #include <signal.h> Loading Loading @@ -77,7 +78,7 @@ namespace netplus { int State; eventapi* api; con* CurCon; mutable std::mutex cltmtx; std::mutex cltmtx; }; class poll { Loading Loading @@ -105,16 +106,17 @@ namespace netplus { } void RemoveFromClientList(client* pClientContext) { std::lock<std::mutex>(g_ClientContextMutex,pClientContext->cltmtx); std::lock(g_ClientContextMutex, pClientContext->cltmtx); std::lock_guard<std::mutex> lock(g_ClientContextMutex, std::adopt_lock); std::lock_guard<std::mutex> clock(pClientContext->cltmtx, std::adopt_lock); //Remove the client context from the list auto it = std::find(g_ClientContext.begin(), g_ClientContext.end(), pClientContext); if (it != g_ClientContext.end()) { g_ClientContext.erase(it); pClientContext->cltmtx.unlock(); delete pClientContext; pClientContext = nullptr; } } void AcceptConnection(LPFN_ACCEPTEX lpfnAcceptEx, int tid, ULONG_PTR args) { Loading Loading @@ -264,7 +266,7 @@ namespace netplus { switch (pClientContext->State) { case IO_WRITE_STATE: { std::lock_guard<std::mutex> lock(pClientContext->cltmtx); pClientContext->cltmtx.lock(); eargs->event->ResponseEvent(curCon, tid, args); Loading @@ -275,9 +277,10 @@ namespace netplus { if (curCon->SendData.empty()) pClientContext->State = IO_READ_STATE; WSAResetEvent(pClientContext->CurCon->csock->_Overlapped.hEvent); pClientContext->cltmtx.unlock(); }break; case IO_READ_STATE: { std::lock_guard<std::mutex> lock(pClientContext->cltmtx); pClientContext->cltmtx.lock(); std::shared_ptr<char[]> buf(new char[BLOCKSIZE], std::default_delete<char[]>()); size_t recv = curCon->csock->recvData(buf.get(), BLOCKSIZE, 0); Loading @@ -286,6 +289,7 @@ namespace netplus { eargs->event->RequestEvent(curCon, tid, args); if (!curCon->SendData.empty()) pClientContext->State = IO_WRITE_STATE; pClientContext->cltmtx.unlock(); }break; case IO_CLOSE_STATE: { WSACloseEvent(pClientContext->CurCon->csock->_Overlapped.hEvent); Loading @@ -297,12 +301,14 @@ namespace netplus { std::cerr << e.what() << std::endl; if (e.getErrorType() != NetException::Note) { pClientContext->State = IO_CLOSE_STATE; continue; } pClientContext->cltmtx.unlock(); continue; } catch (...) { std::cerr << "unknown error close coonection !" << std::endl; pClientContext->State = IO_CLOSE_STATE; pClientContext->cltmtx.unlock(); continue; } Loading Loading
src/event/iocp.cpp +13 −7 Changes for src/event/iocp.cpp: 13 added lines, 7 removed lines. Original line number Diff line number Diff line Loading @@ -30,6 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <chrono> #include <memory> #include <mutex> #include <shared_mutex> #include <cstring> #include <signal.h> Loading Loading @@ -77,7 +78,7 @@ namespace netplus { int State; eventapi* api; con* CurCon; mutable std::mutex cltmtx; std::mutex cltmtx; }; class poll { Loading Loading @@ -105,16 +106,17 @@ namespace netplus { } void RemoveFromClientList(client* pClientContext) { std::lock<std::mutex>(g_ClientContextMutex,pClientContext->cltmtx); std::lock(g_ClientContextMutex, pClientContext->cltmtx); std::lock_guard<std::mutex> lock(g_ClientContextMutex, std::adopt_lock); std::lock_guard<std::mutex> clock(pClientContext->cltmtx, std::adopt_lock); //Remove the client context from the list auto it = std::find(g_ClientContext.begin(), g_ClientContext.end(), pClientContext); if (it != g_ClientContext.end()) { g_ClientContext.erase(it); pClientContext->cltmtx.unlock(); delete pClientContext; pClientContext = nullptr; } } void AcceptConnection(LPFN_ACCEPTEX lpfnAcceptEx, int tid, ULONG_PTR args) { Loading Loading @@ -264,7 +266,7 @@ namespace netplus { switch (pClientContext->State) { case IO_WRITE_STATE: { std::lock_guard<std::mutex> lock(pClientContext->cltmtx); pClientContext->cltmtx.lock(); eargs->event->ResponseEvent(curCon, tid, args); Loading @@ -275,9 +277,10 @@ namespace netplus { if (curCon->SendData.empty()) pClientContext->State = IO_READ_STATE; WSAResetEvent(pClientContext->CurCon->csock->_Overlapped.hEvent); pClientContext->cltmtx.unlock(); }break; case IO_READ_STATE: { std::lock_guard<std::mutex> lock(pClientContext->cltmtx); pClientContext->cltmtx.lock(); std::shared_ptr<char[]> buf(new char[BLOCKSIZE], std::default_delete<char[]>()); size_t recv = curCon->csock->recvData(buf.get(), BLOCKSIZE, 0); Loading @@ -286,6 +289,7 @@ namespace netplus { eargs->event->RequestEvent(curCon, tid, args); if (!curCon->SendData.empty()) pClientContext->State = IO_WRITE_STATE; pClientContext->cltmtx.unlock(); }break; case IO_CLOSE_STATE: { WSACloseEvent(pClientContext->CurCon->csock->_Overlapped.hEvent); Loading @@ -297,12 +301,14 @@ namespace netplus { std::cerr << e.what() << std::endl; if (e.getErrorType() != NetException::Note) { pClientContext->State = IO_CLOSE_STATE; continue; } pClientContext->cltmtx.unlock(); continue; } catch (...) { std::cerr << "unknown error close coonection !" << std::endl; pClientContext->State = IO_CLOSE_STATE; pClientContext->cltmtx.unlock(); continue; } Loading