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

test

parents f81fa426 f44e7e73
Loading
Loading
Loading
Loading
+26 −11
Changes for src/event/iocp.cpp: 26 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -56,9 +56,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

namespace netplus {

	enum IOState {
		IO_READ_STATE = 0,
		IO_WRITE_STATE = 1,
		IO_CLOSE_STATE = 2
	};

	class client {
	public:
		client(eventapi* eapi) : api(eapi), State(nullptr), CurCon(nullptr) {
		client(eventapi* eapi) : api(eapi), State(IO_READ_STATE), CurCon(nullptr) {
			api->CreateConnetion(&CurCon);
		}

@@ -68,7 +74,7 @@ namespace netplus {
			}
		}

		HANDLE             State;
		int                State;
		eventapi*          api;
		con*               CurCon;
		mutable std::mutex cltmtx;
@@ -249,32 +255,41 @@ namespace netplus {
				}

				try {
					std::lock_guard<std::mutex> lock(pClientContext->cltmtx);
					auto& curCon = pClientContext->CurCon;
					if (!curCon->SendData.empty()) {

					switch (pClientContext->State) {
						case IO_WRITE_STATE: {
							std::lock_guard<std::mutex> lock(pClientContext->cltmtx);
							eargs->event->ResponseEvent(curCon, tid, args);

							ULONG ssize = curCon->SendData.size() < BLOCKSIZE ? (ULONG)curCon->SendData.size() : BLOCKSIZE;
							DWORD ret = 0;
							ret = curCon->csock->sendData(curCon->SendData.data(), ssize, 0);
							curCon->SendData.resize(ret);
					} else {
							if (curCon->SendData.empty())
								pClientContext->State = IO_READ_STATE;
						}break;
						case IO_READ_STATE: {
							std::lock_guard<std::mutex> lock(pClientContext->cltmtx);
							std::shared_ptr<char[]> buf(new char[BLOCKSIZE], std::default_delete<char[]>());
							size_t recv = curCon->csock->recvData(buf.get(), BLOCKSIZE, 0);
							curCon->RecvData.append(buf.get(), recv);

							eargs->event->RequestEvent(curCon, tid, args);
							if (!curCon->SendData.empty())
								pClientContext->State = IO_WRITE_STATE;
						}break;
						case IO_CLOSE_STATE: {
							WSACloseEvent(pClientContext->CurCon->csock->_Overlapped.hEvent);
							eargs->evpoll->RemoveFromClientList(pClientContext);
						}break;
					}
				} catch (NetException& e) {
					std::cerr << e.what() << std::endl;
					if(pClientContext && e.getErrorType()!=NetException::Note){
						WSACloseEvent(pClientContext->CurCon->csock->_Overlapped.hEvent);
						eargs->evpoll->RemoveFromClientList(pClientContext);
						pClientContext = nullptr;
					if (e.getErrorType() != NetException::Note) {
						pClientContext->State = IO_CLOSE_STATE;
						continue;
					}
				} catch (...) {
					std::cerr << "Eventworker: unkown Error !" << std::endl;
				}
				WSAResetEvent(pClientContext->CurCon->csock->_Overlapped.hEvent);
			} // for