Loading src/event/iocp.cpp +87 −21 Changes for src/event/iocp.cpp: 87 added lines, 21 removed lines. Original line number Diff line number Diff line Loading @@ -218,6 +218,37 @@ namespace netplus { }; class EventWorker { private: static void start_read(client* ctx) { if (!ctx) return; con& c = *ctx->CurCon; if (!c.csock) return; // For IOCP, recvData() should post WSARecv and return quickly (or block if _Wait true). // Use your existing RecvBuf as the target. c.csock->recvData(ctx->RecvBuf, 0); } static void start_write(client* ctx) { if (!ctx) return; con& c = *ctx->CurCon; if (!c.csock) return; if (c.SendData.empty()) { ctx->last_plain_consumed = 0; return; } const size_t toSend = std::min<size_t>(BLOCKSIZE, c.SendData.size()); buffer out(c.SendData.data(), toSend); // IMPORTANT: returns PLAINTEXT CONSUMED (for TCP==socket bytes, for SSL==plaintext bytes) ctx->last_plain_consumed = c.csock->sendData(out, 0); // If it consumed 0, we still posted work (TLS may be flushing a record), // or TCP would-block. Next completion/wakeup will retry. } public: EventWorker(int tid, ULONG_PTR args, EventWorkerArgs* eargs) { Loading Loading @@ -259,35 +290,70 @@ namespace netplus { switch (pClientContext->State) { case IO_READ_STATE: { // Append the received data to the buffer curCon->RecvData.append(curCon->csock->_wsaBuf.buf, dwBytesTransfered); eargs->event->RequestEvent(*curCon, tid, args); // IOCP completed a read: bytes are in csock->_wsaBuf.buf (your tcp/ssl layer owns that buffer) if (!curCon->SendData.empty()) { // Switch to write state and initiate an async send pClientContext->State = IO_WRITE_STATE; buffer buf(curCon->SendData.data(), curCon->SendData.size()); curCon->csock->sendData(buf); // For TCP: these bytes are plaintext // For SSL: these bytes are *ciphertext* and must be fed into SSL record decoder. // The clean way: have ssl expose a "feedEncrypted()" method and then pull plaintext via ssl::recvData(). // If you don't have that yet, do NOT treat _wsaBuf as plaintext for SSL. con& c = *curCon; if (c.csock->_Type == SSL) { // PREFERRED FIX: // static_cast<ssl*>(c.csock.get())->feedEncrypted((uint8_t*)c.csock->_wsaBuf.buf, dwBytesTransfered); // // then pull plaintext: // for(;;){ buffer plain(BLOCKSIZE); size_t n = c.csock->recvData(plain,0); ... } // // If you can't do that today, you *must* not append ciphertext to RecvData. // Return to posting another recv. start_read(pClientContext); break; } else { // Stay in read state and initiate another async read curCon->csock->recvData(pClientContext->RecvBuf); // TCP case: c.RecvData.append(c.csock->_wsaBuf.buf, dwBytesTransfered); eargs->event->RequestEvent(c, tid, args); if (!c.SendData.empty()) { pClientContext->State = IO_WRITE_STATE; start_write(pClientContext); } else { start_read(pClientContext); } } break; case IO_WRITE_STATE: { // Erase the sent data from the buffer curCon->SendData.erase(curCon->SendData.begin(),curCon->SendData.begin()+dwBytesTransfered); eargs->event->ResponseEvent(*curCon, tid, args); con& c = *curCon; const size_t consumed = pClientContext->last_plain_consumed; // consumed==0 means: would-block or TLS flush pending record; retry send if (consumed == 0) { start_write(pClientContext); break; } if (!curCon->SendData.empty()) { // More data to send, initiate another async send buffer buf(curCon->SendData.data(), curCon->SendData.size()); curCon->csock->sendData(buf); if (consumed > c.SendData.size()) { NetException e; e[NetException::Error] << "IOCP Write: consumed > SendData.size()"; throw e; } else { // All data sent, switch to read state and initiate a new read // Erase PLAINTEXT, not dwBytesTransfered c.SendData.erase(c.SendData.begin(), c.SendData.begin() + consumed); // Only after fully flushing current queue, allow protocol to enqueue more. if (c.SendData.empty()) { eargs->event->ResponseEvent(c, tid, args); } if (!c.SendData.empty()) { start_write(pClientContext); } else { pClientContext->State = IO_READ_STATE; curCon->csock->recvData(pClientContext->RecvBuf); start_read(pClientContext); } } break; default: { Loading Loading
src/event/iocp.cpp +87 −21 Changes for src/event/iocp.cpp: 87 added lines, 21 removed lines. Original line number Diff line number Diff line Loading @@ -218,6 +218,37 @@ namespace netplus { }; class EventWorker { private: static void start_read(client* ctx) { if (!ctx) return; con& c = *ctx->CurCon; if (!c.csock) return; // For IOCP, recvData() should post WSARecv and return quickly (or block if _Wait true). // Use your existing RecvBuf as the target. c.csock->recvData(ctx->RecvBuf, 0); } static void start_write(client* ctx) { if (!ctx) return; con& c = *ctx->CurCon; if (!c.csock) return; if (c.SendData.empty()) { ctx->last_plain_consumed = 0; return; } const size_t toSend = std::min<size_t>(BLOCKSIZE, c.SendData.size()); buffer out(c.SendData.data(), toSend); // IMPORTANT: returns PLAINTEXT CONSUMED (for TCP==socket bytes, for SSL==plaintext bytes) ctx->last_plain_consumed = c.csock->sendData(out, 0); // If it consumed 0, we still posted work (TLS may be flushing a record), // or TCP would-block. Next completion/wakeup will retry. } public: EventWorker(int tid, ULONG_PTR args, EventWorkerArgs* eargs) { Loading Loading @@ -259,35 +290,70 @@ namespace netplus { switch (pClientContext->State) { case IO_READ_STATE: { // Append the received data to the buffer curCon->RecvData.append(curCon->csock->_wsaBuf.buf, dwBytesTransfered); eargs->event->RequestEvent(*curCon, tid, args); // IOCP completed a read: bytes are in csock->_wsaBuf.buf (your tcp/ssl layer owns that buffer) if (!curCon->SendData.empty()) { // Switch to write state and initiate an async send pClientContext->State = IO_WRITE_STATE; buffer buf(curCon->SendData.data(), curCon->SendData.size()); curCon->csock->sendData(buf); // For TCP: these bytes are plaintext // For SSL: these bytes are *ciphertext* and must be fed into SSL record decoder. // The clean way: have ssl expose a "feedEncrypted()" method and then pull plaintext via ssl::recvData(). // If you don't have that yet, do NOT treat _wsaBuf as plaintext for SSL. con& c = *curCon; if (c.csock->_Type == SSL) { // PREFERRED FIX: // static_cast<ssl*>(c.csock.get())->feedEncrypted((uint8_t*)c.csock->_wsaBuf.buf, dwBytesTransfered); // // then pull plaintext: // for(;;){ buffer plain(BLOCKSIZE); size_t n = c.csock->recvData(plain,0); ... } // // If you can't do that today, you *must* not append ciphertext to RecvData. // Return to posting another recv. start_read(pClientContext); break; } else { // Stay in read state and initiate another async read curCon->csock->recvData(pClientContext->RecvBuf); // TCP case: c.RecvData.append(c.csock->_wsaBuf.buf, dwBytesTransfered); eargs->event->RequestEvent(c, tid, args); if (!c.SendData.empty()) { pClientContext->State = IO_WRITE_STATE; start_write(pClientContext); } else { start_read(pClientContext); } } break; case IO_WRITE_STATE: { // Erase the sent data from the buffer curCon->SendData.erase(curCon->SendData.begin(),curCon->SendData.begin()+dwBytesTransfered); eargs->event->ResponseEvent(*curCon, tid, args); con& c = *curCon; const size_t consumed = pClientContext->last_plain_consumed; // consumed==0 means: would-block or TLS flush pending record; retry send if (consumed == 0) { start_write(pClientContext); break; } if (!curCon->SendData.empty()) { // More data to send, initiate another async send buffer buf(curCon->SendData.data(), curCon->SendData.size()); curCon->csock->sendData(buf); if (consumed > c.SendData.size()) { NetException e; e[NetException::Error] << "IOCP Write: consumed > SendData.size()"; throw e; } else { // All data sent, switch to read state and initiate a new read // Erase PLAINTEXT, not dwBytesTransfered c.SendData.erase(c.SendData.begin(), c.SendData.begin() + consumed); // Only after fully flushing current queue, allow protocol to enqueue more. if (c.SendData.empty()) { eargs->event->ResponseEvent(c, tid, args); } if (!c.SendData.empty()) { start_write(pClientContext); } else { pClientContext->State = IO_READ_STATE; curCon->csock->recvData(pClientContext->RecvBuf); start_read(pClientContext); } } break; default: { Loading