Commit d2ff9094 authored by jan.koester's avatar jan.koester
Browse files

test

parent 9c70e09b
Loading
Loading
Loading
Loading
+26 −65
Changes for src/event/iocp.cpp: 26 added lines, 65 removed lines.
Original line number Diff line number Diff line
@@ -82,7 +82,6 @@ namespace netplus {
		~client() {
		}

		size_t last_plain_consumed = 0;
		int                       State;
		eventapi                 *api;
		std::unique_ptr<con>      CurCon;
@@ -219,54 +218,6 @@ namespace netplus {
	};

	class EventWorker {
	private:
		void start_read(client* ctx) {
			ctx->CurCon->csock->recvData(ctx->RecvBuf, 0);
		};

		void start_write (client* ctx ,int tid, ULONG_PTR args) {
			con& c = *ctx->CurCon;

			while (!c.SendData.empty()) {
				buffer out(c.SendData.data(),
				std::min<size_t>(BLOCKSIZE, c.SendData.size()));
				size_t consumed = 0;
				try {
					consumed = c.csock->sendData(out, 0); // PLAINTEXT consumed
				} catch (NetException& e) {
					if (e.getErrorType() == NetException::Note) {
						ctx->State = IO_WRITE_STATE;
						return;
					}
					throw e;
				}

				if (consumed == 0) {
					ctx->State = IO_WRITE_STATE;
					return;
				}

				if (consumed > c.SendData.size()) {
					NetException e;
					e[NetException::Error] << "IOCP: sendData over-consumed";
					throw e;
				}

				c.SendData.erase(c.SendData.begin(),
				c.SendData.begin() + consumed);
			}

			// Let protocol append more ONCE
			ctx->api->ResponseEvent(c, tid, args);

			if (!c.SendData.empty()) {
				ctx->State = IO_WRITE_STATE;
				start_write(ctx,tid,args);
			} else {
				ctx->State = IO_READ_STATE;
				start_read(ctx);
			}
		};
	public:
		EventWorker(int tid, ULONG_PTR args, EventWorkerArgs* eargs) {

@@ -302,32 +253,42 @@ namespace netplus {
					delete pClientContext;
					continue;
				}

				try {
					std::unique_lock<std::mutex> guard(pClientContext->cltmtx);

					switch (pClientContext->State) {
					case IO_READ_STATE: {
							con& c = *curCon;

							// 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).

							c.RecvData.append(c.csock->_wsaBuf.buf, c.csock->_wsaBuf.len);
							eargs->event->RequestEvent(c, tid, args);
						// Append the received data to the buffer
						curCon->RecvData.append(curCon->csock->_wsaBuf.buf, dwBytesTransfered);
						eargs->event->RequestEvent(*curCon, tid, args);

							if (!c.SendData.empty()) {
						if (!curCon->SendData.empty()) {
							// Switch to write state and initiate an async send
							pClientContext->State = IO_WRITE_STATE;
								start_write(pClientContext,tid,args);
							} else {
								pClientContext->State = IO_READ_STATE;
								start_read(pClientContext);
							buffer buf(curCon->SendData.data(), curCon->SendData.size());
							curCon->csock->sendData(buf);
						}
						else {
							// Stay in read state and initiate another async read
							curCon->csock->recvData(pClientContext->RecvBuf);
						}
					}break;
					case IO_WRITE_STATE: {
							start_write(pClientContext,tid,args);
						// Erase the sent data from the buffer
						curCon->SendData.erase(curCon->SendData.begin(),curCon->SendData.begin()+dwBytesTransfered);
						eargs->event->ResponseEvent(*curCon, tid, args);

						if (!curCon->SendData.empty()) {
							// More data to send, initiate another async send
							buffer buf(curCon->SendData.data(), curCon->SendData.size());
							curCon->csock->sendData(buf);
						}
						else {
							// All data sent, switch to read state and initiate a new read
							pClientContext->State = IO_READ_STATE;
							curCon->csock->recvData(pClientContext->RecvBuf);
						}
					}break;
					default: {
						NetException exp;