Loading src/event/iocp.cpp +41 −27 Changes for src/event/iocp.cpp: 41 added lines, 27 removed lines. Original line number Diff line number Diff line Loading @@ -116,22 +116,17 @@ namespace netplus { g_eventapi->ConnectEvent(*pClientContext->CurCon, tid, args); buffer buf(0); for (;;) { INITIALRECV: try { size_t recv = pClientContext->CurCon->csock->recvData(buf, 0); break; } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { continue; } else if (e.getErrorType() == NetException::Error) { }catch (NetException& e) { if (e.getErrorType() != NetException::Note) { std::cerr << e.what() << std::endl; g_eventapi->DisconnectEvent(*pClientContext->CurCon, tid, args); delete pClientContext->CurCon->csock; delete pClientContext; return; throw e; } }; } } Loading Loading @@ -251,6 +246,7 @@ namespace netplus { NetException exp; exp[NetException::Critical] << "GetQueuedCompletionStatus failed with error: " << dwError; throw exp; } } Loading @@ -261,39 +257,69 @@ namespace netplus { client *pClientContext = reinterpret_cast<client*>(lpContext); try { std::cout << "test: " << pClientContext->State << std::endl; std::lock_guard<std::mutex> clock(pClientContext->cltmtx); con *curCon = pClientContext->CurCon; switch (pClientContext->State) { case IO_WRITE_STATE: { try { eargs->event->ResponseEvent(*curCon, tid, args); } catch (...) { curCon->SendData.clear(); } ULONG ssize = curCon->SendData.size() < BLOCKSIZE ? (ULONG)curCon->SendData.size() : BLOCKSIZE; size_t ret = 0; buffer buf(curCon->SendData.data(),ssize); try{ ret = curCon->csock->sendData(buf, 0); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { continue; // Retry sending data } else { throw e; // Rethrow other exceptions } } // Erase the sent data from the beginning of the buffer curCon->SendData.erase(curCon->SendData.begin(), curCon->SendData.begin() + ret); curCon->SendData.resize(ret); if (curCon->SendData.empty()) { pClientContext->State = IO_READ_STATE; } else { PostQueuedCompletionStatus(eargs->eviocp, 0, lpContext, pOverlapped); continue; } }break; case IO_READ_STATE: { buffer buf(BLOCKSIZE); size_t recv = curCon->csock->recvData(buf, 0); std::copy(buf.data.buf,buf.data.buf+recv,std::back_inserter<std::vector<char,netplus::condataAlloc<char>>>(curCon->RecvData)); size_t recv = 0; try { recv = curCon->csock->recvData(buf, 0); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { std::cout << "Note: " << e.what() << std::endl; continue; // Retry receiving data } else { throw e; // Rethrow other exceptions } } curCon->RecvData.append(buf.data.buf, recv); try{ eargs->event->RequestEvent(*curCon, tid, args); } catch (...){ curCon->RecvData.clear(); } if (!curCon->SendData.empty()) { pClientContext->State = IO_WRITE_STATE; PostQueuedCompletionStatus(eargs->eviocp, 0, lpContext, pOverlapped); continue; } }break; Loading @@ -304,21 +330,9 @@ namespace netplus { }break; } } catch (NetException& e) { if (e.getErrorType() != NetException::Note) { std::cerr << e.what() << std::endl; pClientContext->cltmtx.lock(); eargs->event->DisconnectEvent(*pClientContext->CurCon, tid, args); // Close the socket WSACloseEvent(pClientContext->CurCon->csock->_Overlapped.hEvent); delete pClientContext->CurCon->csock; delete pClientContext; } }catch (...) { std::cerr << "Eventworker unknown error !" << std::endl; pClientContext->cltmtx.lock(); eargs->event->DisconnectEvent(*pClientContext->CurCon, tid, args); // Close the socket WSACloseEvent(pClientContext->CurCon->csock->_Overlapped.hEvent); delete pClientContext->CurCon->csock; delete pClientContext; } Loading Loading
src/event/iocp.cpp +41 −27 Changes for src/event/iocp.cpp: 41 added lines, 27 removed lines. Original line number Diff line number Diff line Loading @@ -116,22 +116,17 @@ namespace netplus { g_eventapi->ConnectEvent(*pClientContext->CurCon, tid, args); buffer buf(0); for (;;) { INITIALRECV: try { size_t recv = pClientContext->CurCon->csock->recvData(buf, 0); break; } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { continue; } else if (e.getErrorType() == NetException::Error) { }catch (NetException& e) { if (e.getErrorType() != NetException::Note) { std::cerr << e.what() << std::endl; g_eventapi->DisconnectEvent(*pClientContext->CurCon, tid, args); delete pClientContext->CurCon->csock; delete pClientContext; return; throw e; } }; } } Loading Loading @@ -251,6 +246,7 @@ namespace netplus { NetException exp; exp[NetException::Critical] << "GetQueuedCompletionStatus failed with error: " << dwError; throw exp; } } Loading @@ -261,39 +257,69 @@ namespace netplus { client *pClientContext = reinterpret_cast<client*>(lpContext); try { std::cout << "test: " << pClientContext->State << std::endl; std::lock_guard<std::mutex> clock(pClientContext->cltmtx); con *curCon = pClientContext->CurCon; switch (pClientContext->State) { case IO_WRITE_STATE: { try { eargs->event->ResponseEvent(*curCon, tid, args); } catch (...) { curCon->SendData.clear(); } ULONG ssize = curCon->SendData.size() < BLOCKSIZE ? (ULONG)curCon->SendData.size() : BLOCKSIZE; size_t ret = 0; buffer buf(curCon->SendData.data(),ssize); try{ ret = curCon->csock->sendData(buf, 0); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { continue; // Retry sending data } else { throw e; // Rethrow other exceptions } } // Erase the sent data from the beginning of the buffer curCon->SendData.erase(curCon->SendData.begin(), curCon->SendData.begin() + ret); curCon->SendData.resize(ret); if (curCon->SendData.empty()) { pClientContext->State = IO_READ_STATE; } else { PostQueuedCompletionStatus(eargs->eviocp, 0, lpContext, pOverlapped); continue; } }break; case IO_READ_STATE: { buffer buf(BLOCKSIZE); size_t recv = curCon->csock->recvData(buf, 0); std::copy(buf.data.buf,buf.data.buf+recv,std::back_inserter<std::vector<char,netplus::condataAlloc<char>>>(curCon->RecvData)); size_t recv = 0; try { recv = curCon->csock->recvData(buf, 0); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { std::cout << "Note: " << e.what() << std::endl; continue; // Retry receiving data } else { throw e; // Rethrow other exceptions } } curCon->RecvData.append(buf.data.buf, recv); try{ eargs->event->RequestEvent(*curCon, tid, args); } catch (...){ curCon->RecvData.clear(); } if (!curCon->SendData.empty()) { pClientContext->State = IO_WRITE_STATE; PostQueuedCompletionStatus(eargs->eviocp, 0, lpContext, pOverlapped); continue; } }break; Loading @@ -304,21 +330,9 @@ namespace netplus { }break; } } catch (NetException& e) { if (e.getErrorType() != NetException::Note) { std::cerr << e.what() << std::endl; pClientContext->cltmtx.lock(); eargs->event->DisconnectEvent(*pClientContext->CurCon, tid, args); // Close the socket WSACloseEvent(pClientContext->CurCon->csock->_Overlapped.hEvent); delete pClientContext->CurCon->csock; delete pClientContext; } }catch (...) { std::cerr << "Eventworker unknown error !" << std::endl; pClientContext->cltmtx.lock(); eargs->event->DisconnectEvent(*pClientContext->CurCon, tid, args); // Close the socket WSACloseEvent(pClientContext->CurCon->csock->_Overlapped.hEvent); delete pClientContext->CurCon->csock; delete pClientContext; } Loading