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

it runs again and faster with windows :-D

parent f6573213
Loading
Loading
Loading
Loading
+1
Changes for src/connection.cpp: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -111,6 +111,7 @@ netplus::con::con() : csock(nullptr){
netplus::con::con(eventapi *event) : csock(nullptr) {
    state = 0;
	lasteventime = time(nullptr);
    csock = nullptr;
}

netplus::con::~con(){
+53 −55
Changes for src/event/iocp.cpp: 53 added lines, 55 removed lines.
Original line number Diff line number Diff line
@@ -114,6 +114,10 @@ namespace netplus {
			}

			g_eventapi->ConnectEvent(*pClientContext->CurCon, tid, args);

			buffer buf(BLOCKSIZE);
			size_t ret=pClientContext->CurCon->csock->recvData(buf, 0);
			pClientContext->CurCon->RecvData.append(buf.data.buf, ret);
		}

	private:
@@ -219,69 +223,65 @@ namespace netplus {
					&pOverlapped,
					WSA_INFINITE);

				if (!bReturn) {		
					DWORD dwError = GetLastError();
					if (dwError == WAIT_TIMEOUT) {
						if (eargs->timeout > 0) {
							std::this_thread::sleep_for(std::chrono::milliseconds(eargs->timeout));
						}
						continue;
					} else if (dwError == ERROR_OPERATION_ABORTED) {
						continue;
					} else {
						//NetException exp;
						//exp[NetException::Critical] << "GetQueuedCompletionStatus failed with error: " << dwError;
						//throw exp;
						continue;
					}
				}

				if (lpContext == 0) {
					continue;
				}

				client* pClientContext = reinterpret_cast<client*>(lpContext);

				try {
				con* curCon = pClientContext->CurCon;
					switch (pClientContext->State) {
						case IO_WRITE_STATE: {
							std::unique_lock<std::mutex> clock(pClientContext->cltmtx);

							eargs->event->ResponseEvent(*curCon, tid, args);

							ULONG ssize = curCon->SendData.size() < BLOCKSIZE ? (ULONG)curCon->SendData.size() : BLOCKSIZE;
							size_t ret = 0;

							buffer buf(curCon->SendData.data(),ssize);
								
							ret = curCon->csock->sendData(buf, 0);							
				// Check for I/O completion errors or graceful disconnects
				if (!bReturn || (bReturn && dwBytesTransfered == 0)) {
					DWORD dwError = GetLastError();
					if (dwError != WAIT_TIMEOUT && dwError != ERROR_OPERATION_ABORTED) {
						NetException exp;
						exp[NetException::Error] << "I/O operation failed or client disconnected. Error code: " << dwError;
						std::cerr << exp.what() << std::endl;
					}

							// Erase the sent data from the beginning of the buffer
							curCon->SendData.resize(ret);
							if (curCon->SendData.empty()) {
								pClientContext->State = IO_READ_STATE;
							} else {
					eargs->event->DisconnectEvent(*curCon, tid, args);
					delete pClientContext;
					continue;
				}
						}break;
						case IO_READ_STATE: {
							std::unique_lock<std::mutex> clock(pClientContext->cltmtx);

							buffer buf(BLOCKSIZE);

							size_t recv = 0;

							recv = curCon->csock->recvData(buf, 0);

							curCon->RecvData.append(buf.data.buf, recv);

				try {
					// Lock before accessing shared data
					std::unique_lock<std::mutex> clock(pClientContext->cltmtx);

					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);

						if (!curCon->SendData.empty()) {
							// Switch to write state and initiate an async send
							pClientContext->State = IO_WRITE_STATE;
								continue;
							buffer buf(curCon->SendData.data(), curCon->SendData.size());
							curCon->csock->sendData(buf);
						}
						else {
							// Stay in read state and initiate another async read
							buffer buf(BLOCKSIZE);
							curCon->csock->recvData(buf);
						}
					}break;
					case IO_WRITE_STATE: {
						// Erase the sent data from the buffer
						curCon->SendData.resize(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;
							buffer buf(BLOCKSIZE);
							curCon->csock->recvData(buf);
						}
					}break;
					default: {
@@ -290,16 +290,14 @@ namespace netplus {
						throw exp;
					}break;
					}
				} catch (NetException& e) {
				}
				catch (NetException& e) {
					std::cerr << e.what() << std::endl;
					eargs->event->DisconnectEvent(*pClientContext->CurCon, tid, args);
					WSACloseEvent(pClientContext->CurCon->csock->_Overlapped.hEvent);
					delete pClientContext->CurCon->csock;
					eargs->event->DisconnectEvent(*curCon, tid, args);
					delete pClientContext;
				} catch (...) {
					eargs->event->DisconnectEvent(*pClientContext->CurCon, tid, args);
					WSACloseEvent(pClientContext->CurCon->csock->_Overlapped.hEvent);
					delete pClientContext->CurCon->csock;
				}
				catch (...) {
					eargs->event->DisconnectEvent(*curCon, tid, args);
					delete pClientContext;
				}
			} // for
+1
Changes for src/socket.h: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -113,6 +113,7 @@ namespace netplus {
            static std::atomic<int>  _InitCount;
#ifdef Windows 
            WSAOVERLAPPED            _Overlapped;
            WSABUF                   _wsaBuf;
#endif
        protected:
            void copyAddrInfo(ULONG_PTR *dest,ULONG_PTR src,size_t srclen);
+10 −10
Changes for src/windows/tcp.cpp: 10 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -194,18 +194,18 @@ void netplus::tcp::bind(){


size_t netplus::tcp::sendData(buffer &data,int flags){
    WSABUF buf{0};

    if (data.ptr)
        buf.buf = (char*)data.data.ptr;
        _wsaBuf.buf = (char*)data.data.ptr;
    else
        buf.buf = data.data.buf;
        _wsaBuf.buf = data.data.buf;

    _wsaBuf.len = static_cast<ULONG>(data.size);

    buf.len = static_cast<ULONG>(data.size);
    memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED));

    DWORD dwsend = 0;

    int rval=::WSASend(_Socket,&buf,1,&dwsend,flags,&_Overlapped,nullptr);
    int rval=::WSASend(_Socket,&_wsaBuf,1,&dwsend,flags,&_Overlapped,nullptr);

    NetException exception;

@@ -234,14 +234,14 @@ size_t netplus::tcp::recvData(buffer &data,int flags){
        throw exception;
    }

    WSABUF buf;
    memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED));

	buf.buf = data.data.buf;
    buf.len = static_cast<ULONG>(data.size);
	_wsaBuf.buf = data.data.buf;
    _wsaBuf.len = static_cast<ULONG>(data.size);

    DWORD dwBytes = 0;

    int rval=::WSARecv(_Socket, &buf, 1,&dwBytes,(LPDWORD) &flags, &_Overlapped, nullptr);
    int rval=::WSARecv(_Socket, &_wsaBuf, 1,&dwBytes,(LPDWORD) &flags, &_Overlapped, nullptr);

    if (rval == SOCKET_ERROR) {
        switch (WSAGetLastError()) {
+10 −10
Changes for src/windows/udp.cpp: 10 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -189,18 +189,18 @@ void netplus::udp::bind() {
}

size_t netplus::udp::sendData(buffer &data, int flags) {
    WSABUF buf{ 0 };

    if (data.ptr)
        buf.buf = (char*)data.data.ptr;
        _wsaBuf.buf = (char*)data.data.ptr;
    else
        buf.buf = data.data.buf;
        _wsaBuf.buf = data.data.buf;

    _wsaBuf.len = static_cast<ULONG>(data.size);

    buf.len = static_cast<ULONG>(data.size);
    memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED));

    DWORD dwsend = 0;

    int rval = ::WSASend(_Socket, &buf, 1, &dwsend, flags, &_Overlapped, nullptr);
    int rval = ::WSASend(_Socket, &_wsaBuf, 1, &dwsend, flags, &_Overlapped, nullptr);

    NetException exception;

@@ -229,14 +229,14 @@ size_t netplus::udp::recvData(buffer &data, int flags) {
        throw exception;
    }

    WSABUF buf;
    memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED));

    buf.buf = data.data.buf;
    buf.len = static_cast<ULONG>(data.size);
    _wsaBuf.buf = data.data.buf;
    _wsaBuf.len = static_cast<ULONG>(data.size);

    DWORD dwBytes = 0;

    int rval = ::WSARecv(_Socket, &buf, 1, &dwBytes, (LPDWORD)&flags, &_Overlapped, nullptr);
    int rval = ::WSARecv(_Socket, &_wsaBuf, 1, &dwBytes, (LPDWORD)&flags, &_Overlapped, nullptr);

    if (rval == SOCKET_ERROR) {
        switch (WSAGetLastError()) {