Loading src/event/iocp.cpp +61 −40 Changes for src/event/iocp.cpp: 61 added lines, 40 removed lines. Original line number Diff line number Diff line Loading @@ -255,59 +255,80 @@ namespace netplus { continue; } try { std::unique_lock<std::mutex> guard(pClientContext->cltmtx); switch (pClientContext->State) { case IO_READ_STATE: { curCon->RecvData.append(curCon->csock->_wsaBuf.buf, dwBytesTransfered); eargs->event->RequestEvent(*curCon, tid, args); auto start_read = [&](client* ctx) { // IMPORTANT: this must schedule a new overlapped recv (your tcp::recvData does that) ctx->CurCon->csock->recvData(ctx->RecvBuf, 0); }; if (!curCon->SendData.empty()) { pClientContext->State = IO_WRITE_STATE; auto start_write = [&](client* ctx) { con& c = *ctx->CurCon; // Kick one send attempt (consumes plaintext OR flushes pending record) buffer buf(curCon->SendData.data(), curCon->SendData.size()); pClientContext->last_plain_consumed = curCon->csock->sendData(buf, 0); // Try to flush as much as possible right now. while (!c.SendData.empty()) { buffer out(c.SendData.data(), c.SendData.size() > BLOCKSIZE ? BLOCKSIZE : c.SendData.size()); // IMPORTANT: do NOT erase here. Only erase in WRITE completion path. // Also do NOT call sendData twice. } else { curCon->csock->recvData(pClientContext->RecvBuf, 0); size_t consumed = 0; try { consumed = c.csock->sendData(out, 0); // returns PLAINTEXT consumed } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // would-block / pending -> wait for IOCP send completion ctx->State = IO_WRITE_STATE; return; } } break; case IO_WRITE_STATE: { // 1) First, erase plaintext that was consumed when we issued the last send attempt const size_t consumed = pClientContext->last_plain_consumed; if (consumed > 0) { if (consumed > curCon->SendData.size()) { throw e; } if (consumed == 0) { // SSL may be flushing internal pending TLS record; wait for completion ctx->State = IO_WRITE_STATE; return; } // erase plaintext that is now owned by ssl internal buffers/record builder if (consumed > c.SendData.size()) { NetException e; e[NetException::Error] << "IOCP: consumed > SendData.size() (logic error)"; e[NetException::Error] << "IOCP: sendData consumed > SendData.size()"; throw e; } curCon->SendData.erase(curCon->SendData.begin(), curCon->SendData.begin() + consumed); c.SendData.erase(c.SendData.begin(), c.SendData.begin() + consumed); } // 2) Let app layer refill ONLY when buffer is empty (prevents duplication) if (curCon->SendData.empty()) { eargs->event->ResponseEvent(*curCon, tid, args); // queue empty: let protocol layer append more (ONCE) ctx->api->ResponseEvent(c, /*tid*/0, /*args*/0); // If ResponseEvent produced more data -> keep writing, else go back to read if (!c.SendData.empty()) { ctx->State = IO_WRITE_STATE; start_write(ctx); } else { ctx->State = IO_READ_STATE; start_read(ctx); } }; try { std::unique_lock<std::mutex> guard(pClientContext->cltmtx); switch (pClientContext->State) { case IO_READ_STATE: { con& c = *curCon; // 3) If there is still data to send, issue one more send attempt if (!curCon->SendData.empty()) { buffer buf(curCon->SendData.data(), curCon->SendData.size()); pClientContext->last_plain_consumed = curCon->csock->sendData(buf, 0); // IMPORTANT: dwBytesTransfered is raw socket bytes (encrypted or plaintext) // Append exactly those bytes into the socket recv buffer your stack expects. // Your code currently does: curCon->RecvData.append(curCon->csock->_wsaBuf.buf,...) // That is OK for TCP, BUT for SSL you must ensure ssl::recvData is used // (see note below). // If last_plain_consumed == 0, that's fine: it means TLS is flushing its internal record. // We rely on the next IOCP send completion to come in, and we'll try again then. break; } c.RecvData.append(c.csock->_wsaBuf.buf, dwBytesTransfered); eargs->event->RequestEvent(c, tid, args); // 4) No more to send -> go back to read if (!c.SendData.empty()) { pClientContext->State = IO_WRITE_STATE; start_write(pClientContext); } else { pClientContext->State = IO_READ_STATE; pClientContext->last_plain_consumed = 0; curCon->csock->recvData(pClientContext->RecvBuf, 0); start_read(pClientContext); } } break; case IO_WRITE_STATE: { start_write(pClientContext); } break; default: { NetException exp; Loading Loading
src/event/iocp.cpp +61 −40 Changes for src/event/iocp.cpp: 61 added lines, 40 removed lines. Original line number Diff line number Diff line Loading @@ -255,59 +255,80 @@ namespace netplus { continue; } try { std::unique_lock<std::mutex> guard(pClientContext->cltmtx); switch (pClientContext->State) { case IO_READ_STATE: { curCon->RecvData.append(curCon->csock->_wsaBuf.buf, dwBytesTransfered); eargs->event->RequestEvent(*curCon, tid, args); auto start_read = [&](client* ctx) { // IMPORTANT: this must schedule a new overlapped recv (your tcp::recvData does that) ctx->CurCon->csock->recvData(ctx->RecvBuf, 0); }; if (!curCon->SendData.empty()) { pClientContext->State = IO_WRITE_STATE; auto start_write = [&](client* ctx) { con& c = *ctx->CurCon; // Kick one send attempt (consumes plaintext OR flushes pending record) buffer buf(curCon->SendData.data(), curCon->SendData.size()); pClientContext->last_plain_consumed = curCon->csock->sendData(buf, 0); // Try to flush as much as possible right now. while (!c.SendData.empty()) { buffer out(c.SendData.data(), c.SendData.size() > BLOCKSIZE ? BLOCKSIZE : c.SendData.size()); // IMPORTANT: do NOT erase here. Only erase in WRITE completion path. // Also do NOT call sendData twice. } else { curCon->csock->recvData(pClientContext->RecvBuf, 0); size_t consumed = 0; try { consumed = c.csock->sendData(out, 0); // returns PLAINTEXT consumed } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // would-block / pending -> wait for IOCP send completion ctx->State = IO_WRITE_STATE; return; } } break; case IO_WRITE_STATE: { // 1) First, erase plaintext that was consumed when we issued the last send attempt const size_t consumed = pClientContext->last_plain_consumed; if (consumed > 0) { if (consumed > curCon->SendData.size()) { throw e; } if (consumed == 0) { // SSL may be flushing internal pending TLS record; wait for completion ctx->State = IO_WRITE_STATE; return; } // erase plaintext that is now owned by ssl internal buffers/record builder if (consumed > c.SendData.size()) { NetException e; e[NetException::Error] << "IOCP: consumed > SendData.size() (logic error)"; e[NetException::Error] << "IOCP: sendData consumed > SendData.size()"; throw e; } curCon->SendData.erase(curCon->SendData.begin(), curCon->SendData.begin() + consumed); c.SendData.erase(c.SendData.begin(), c.SendData.begin() + consumed); } // 2) Let app layer refill ONLY when buffer is empty (prevents duplication) if (curCon->SendData.empty()) { eargs->event->ResponseEvent(*curCon, tid, args); // queue empty: let protocol layer append more (ONCE) ctx->api->ResponseEvent(c, /*tid*/0, /*args*/0); // If ResponseEvent produced more data -> keep writing, else go back to read if (!c.SendData.empty()) { ctx->State = IO_WRITE_STATE; start_write(ctx); } else { ctx->State = IO_READ_STATE; start_read(ctx); } }; try { std::unique_lock<std::mutex> guard(pClientContext->cltmtx); switch (pClientContext->State) { case IO_READ_STATE: { con& c = *curCon; // 3) If there is still data to send, issue one more send attempt if (!curCon->SendData.empty()) { buffer buf(curCon->SendData.data(), curCon->SendData.size()); pClientContext->last_plain_consumed = curCon->csock->sendData(buf, 0); // IMPORTANT: dwBytesTransfered is raw socket bytes (encrypted or plaintext) // Append exactly those bytes into the socket recv buffer your stack expects. // Your code currently does: curCon->RecvData.append(curCon->csock->_wsaBuf.buf,...) // That is OK for TCP, BUT for SSL you must ensure ssl::recvData is used // (see note below). // If last_plain_consumed == 0, that's fine: it means TLS is flushing its internal record. // We rely on the next IOCP send completion to come in, and we'll try again then. break; } c.RecvData.append(c.csock->_wsaBuf.buf, dwBytesTransfered); eargs->event->RequestEvent(c, tid, args); // 4) No more to send -> go back to read if (!c.SendData.empty()) { pClientContext->State = IO_WRITE_STATE; start_write(pClientContext); } else { pClientContext->State = IO_READ_STATE; pClientContext->last_plain_consumed = 0; curCon->csock->recvData(pClientContext->RecvBuf, 0); start_read(pClientContext); } } break; case IO_WRITE_STATE: { start_write(pClientContext); } break; default: { NetException exp; Loading