Commit 21c9b369 authored by jan.koester's avatar jan.koester
Browse files

test

parent 70d9b4c6
Loading
Loading
Loading
Loading
+13 −44
Changes for src/event/iocp.cpp: 13 added lines, 44 removed lines.
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <iostream>
#include <algorithm>
#include <chrono>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <cstring>
@@ -84,17 +85,9 @@ namespace netplus {
			: g_iocp(iocp), g_eventapi(api), g_serversocket(ssock) {
		}

		void AddToClientList(std::shared_ptr<client> pClientContext) {
			std::lock<std::mutex>(g_ClientContextMutex, pClientContext->cltmtx);
			std::lock_guard<std::mutex> lock(g_ClientContextMutex,std::adopt_lock);
			std::lock_guard<std::mutex> clock(pClientContext->cltmtx,std::adopt_lock);

			g_ClientContext.push_back(pClientContext);
		}

		void AssociateWithIOCP(std::shared_ptr <client> pClientContext) {
		void AssociateWithIOCP(client *pClientContext) {
			//Associate the socket with IOCP
			HANDLE giocp2 = CreateIoCompletionPort((HANDLE)(pClientContext)->CurCon->csock->fd(), g_iocp, (ULONG_PTR)pClientContext.get(), 0);
			HANDLE giocp2 = CreateIoCompletionPort((HANDLE)(pClientContext)->CurCon->csock->fd(), g_iocp,reinterpret_cast<ULONG_PTR>(pClientContext), 0);

			if (!giocp2 || g_iocp != giocp2) {
				DWORD err = GetLastError();
@@ -104,22 +97,8 @@ namespace netplus {
			}
		}

		void RemoveFromClientList(std::shared_ptr <client> pClientContext) {
			std::lock(g_ClientContextMutex, pClientContext->cltmtx);
			std::lock_guard<std::mutex> lock(g_ClientContextMutex, std::adopt_lock);
			std::lock_guard<std::mutex> lock2(pClientContext->cltmtx, std::adopt_lock);

			//Remove the client context from the list
			auto it = std::find(g_ClientContext.begin(), g_ClientContext.end(), pClientContext);
			if (it != g_ClientContext.end()) {
				g_ClientContext.erase(it);
				// Clean up the client context
				pClientContext = nullptr;
			}
		}

		void AcceptConnection(LPFN_ACCEPTEX lpfnAcceptEx, int tid, ULONG_PTR args) {
			std::shared_ptr<client> pClientContext(new client(g_eventapi), std::default_delete<client>());
			client *pClientContext =new client(g_eventapi);

			if (g_serversocket->_Type == TCP)
				pClientContext->CurCon->csock = std::make_shared <tcp>();
@@ -129,15 +108,13 @@ namespace netplus {

				pClientContext->CurCon->csock->setNonBlock();

				//Store this object
				AddToClientList(pClientContext);

				AssociateWithIOCP(pClientContext);
			}
			catch (NetException& e) {
				RemoveFromClientList(pClientContext);
				delete pClientContext;
				throw e;
			}

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

			try {
@@ -147,16 +124,12 @@ namespace netplus {
				if (e.getErrorType() != NetException::Note)
					throw e;
			}


		}

	private:
		HANDLE                               g_iocp;
		std::shared_ptr <socket>             g_serversocket;
		std::shared_ptr <eventapi>			 g_eventapi;
		std::vector<std::shared_ptr<client>> g_ClientContext;
		std::mutex							 g_ClientContextMutex; // NEU: mutex als Member
		friend class						 client;
		friend class                         EventWorker;
	};
@@ -182,7 +155,6 @@ namespace netplus {
	};

	event::event(std::shared_ptr<socket>& serversocket, int timeout){
		_ServerSocket=serversocket;
		if (!serversocket) {
			NetException exp;
			exp[NetException::Critical] << "server socket empty!";
@@ -190,6 +162,7 @@ namespace netplus {
		}
		_pollFD = -1;
		_Timeout = timeout;
		_ServerSocket = serversocket;
		_ServerSocket->setNonBlock();
		_ServerSocket->bind();
		_ServerSocket->listen();
@@ -250,19 +223,18 @@ namespace netplus {
					continue;
				}

				std::shared_ptr<client> pClientContext = std::shared_ptr<client>(reinterpret_cast<client*>(lpContext));
				client *pClientContext = reinterpret_cast<client*>(lpContext);

				if (!bReturn && dwBytesTransfered == 0) {
					eargs->evpoll->RemoveFromClientList(pClientContext);
					continue;
				}

				try {
					auto& curCon = pClientContext->CurCon;
					std::lock_guard clock(pClientContext->cltmtx);

					auto& curCon = pClientContext->CurCon;
					switch (pClientContext->State) {
						case IO_WRITE_STATE: {
							std::lock_guard<std::mutex> clock(pClientContext->cltmtx);
							eargs->event->ResponseEvent(curCon, tid, args);
	
							ULONG ssize = curCon->SendData.size() < BLOCKSIZE ? (ULONG)curCon->SendData.size() : BLOCKSIZE;
@@ -273,7 +245,6 @@ namespace netplus {
								pClientContext->State = IO_READ_STATE;
						}break;
						case IO_READ_STATE: {
							std::lock_guard<std::mutex> clock(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);
@@ -282,21 +253,19 @@ namespace netplus {
							if (!curCon->SendData.empty())
								pClientContext->State = IO_WRITE_STATE;
						}break;
						case IO_CLOSE_STATE: {
							eargs->evpoll->RemoveFromClientList(pClientContext);
						}continue;
					}
				}
				catch (NetException& e) {
					std::cerr << e.what() << std::endl;
					if (e.getErrorType() != NetException::Note) {
						pClientContext->State = IO_CLOSE_STATE;
						continue;
					}
					delete pClientContext;
					continue;
				}
				catch (...) {
					std::cerr << "unknown error close coonection !" << std::endl;
					pClientContext->State = IO_CLOSE_STATE;
					delete pClientContext;
					continue;
				}