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

test

parent 95391bc2
Loading
Loading
Loading
Loading
+1 −1
Changes for src/connection.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -111,7 +111,7 @@ netplus::con::con(){
    state=0;
}

netplus::con::con(eventapi *eapi) {
netplus::con::con(std::shared_ptr<eventapi> eapi) {
    state = 0;
    _eventapi=eapi;
}
+4 −4
Changes for src/connection.h: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -137,11 +137,11 @@ namespace netplus {
        class con {
        public:
            
            con(eventapi *event);
            con(std::shared_ptr<eventapi> event);
            virtual ~con();

            /*clientsocket*/
            socket *csock;
            std::shared_ptr <socket> csock;

            /*event stauts*/
            int state;
@@ -154,8 +154,8 @@ namespace netplus {
            con();
            int lasteventime;
        private:
            eventapi         *_eventapi;
            pollapi          *_pollapi;
            std::shared_ptr<eventapi> _eventapi;
            std::shared_ptr<pollapi>  _pollapi;
            friend class poll;
        };
};
+35 −44
Changes for src/event/iocp.cpp: 35 added lines, 44 removed lines.
Original line number Diff line number Diff line
@@ -65,30 +65,27 @@ namespace netplus {

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

		~client() {
			if (CurCon) {
				api->deleteConnetion(CurCon);
			}
		}

		int                State;
		eventapi* api;
		con* CurCon;
		std::shared_ptr<eventapi> api;
		std::shared_ptr<con>	  CurCon;
		std::mutex cltmtx;
	};

	class poll {
	public:

		poll(HANDLE iocp, eventapi* api, socket* ssock)
		poll(HANDLE iocp, std::shared_ptr<eventapi> api, std::shared_ptr <socket> ssock)
			: g_iocp(iocp), g_eventapi(api), g_serversocket(ssock) {
		}

		void AddToClientList(client* pClientContext) {
		void AddToClientList(std::shared_ptr<client> pClientContext) {
			std::lock<std::mutex>(g_ClientContextMutex, pClientContext->cltmtx);
			std::lock_guard<std::mutex> lock(g_ClientContextMutex);
			std::lock_guard<std::mutex> clock(pClientContext->cltmtx);
@@ -96,9 +93,9 @@ namespace netplus {
			g_ClientContext.push_back(pClientContext);
		}

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

			if (!giocp2 || g_iocp != giocp2) {
				DWORD err = GetLastError();
@@ -108,7 +105,7 @@ namespace netplus {
			}
		}

		void RemoveFromClientList(client* pClientContext) {
		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);
@@ -118,18 +115,15 @@ namespace netplus {
			if (it != g_ClientContext.end()) {
				g_ClientContext.erase(it);
				// Clean up the client context
				delete pClientContext;
				pClientContext = nullptr;
			}
			

		}

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

			if (g_serversocket->_Type == TCP)
				pClientContext->CurCon->csock = new tcp();
				pClientContext->CurCon->csock = std::make_shared <tcp>();

			try {
				g_serversocket->accept(lpfnAcceptEx, pClientContext->CurCon->csock);
@@ -142,7 +136,6 @@ namespace netplus {
				AssociateWithIOCP(pClientContext);
			}
			catch (NetException& e) {
				delete pClientContext->CurCon->csock;
				RemoveFromClientList(pClientContext);
				throw e;
			}
@@ -161,39 +154,35 @@ namespace netplus {

	private:
		HANDLE                               g_iocp;
		socket* g_serversocket;
		eventapi* g_eventapi;
		std::vector<client*> g_ClientContext;
		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;
	};

	void eventapi::RequestEvent(con* curcon, const int tid, ULONG_PTR args) {
	void eventapi::RequestEvent(std::shared_ptr <con> curcon, const int tid, ULONG_PTR args) {
		//dummy
	};

	void eventapi::ResponseEvent(con* curcon, const int tid, ULONG_PTR args) {
	void eventapi::ResponseEvent(std::shared_ptr <con> curcon, const int tid, ULONG_PTR args) {
		//dummy
	};

	void eventapi::ConnectEvent(con* curcon, const int tid, ULONG_PTR args) {
	void eventapi::ConnectEvent(std::shared_ptr <con> curcon, const int tid, ULONG_PTR args) {
		//dummy
	};

	void eventapi::DisconnectEvent(con* curcon, const int tid, ULONG_PTR args) {
	void eventapi::DisconnectEvent(std::shared_ptr <con> curcon, const int tid, ULONG_PTR args) {
		//dummy
	};

	void eventapi::CreateConnetion(con** curcon) {
		*curcon = new con(this);
	void eventapi::CreateConnetion(std::shared_ptr <con> curcon) {
		curcon = std::make_shared<con>(std::shared_ptr<eventapi>(this));
	};

	void eventapi::deleteConnetion(con* curcon) {
		delete curcon;
	};

	event::event(socket* serversocket, int timeout) {
	event::event(std::shared_ptr<socket> serversocket, int timeout) {
		if (!serversocket) {
			NetException exp;
			exp[NetException::Critical] << "server socket empty!";
@@ -240,13 +229,13 @@ namespace netplus {
		HANDLE      eviocp;
		poll* evpoll;
		eventapi* event;
		socket* ssocket;
		std::shared_ptr<socket> ssocket;
		void* args;
	};

	class EventWorker {
	public:
		EventWorker(int tid, ULONG_PTR args, EventWorkerArgs* eargs) {
		EventWorker(int tid, ULONG_PTR args, std::shared_ptr<EventWorkerArgs> eargs) {
			for (;;) {
				ULONG_PTR   lpContext = 0;
				OVERLAPPED* pOverlapped = nullptr;
@@ -263,7 +252,7 @@ namespace netplus {
					continue;
				}

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

				if (!bReturn && dwBytesTransfered == 0) {
					eargs->evpoll->RemoveFromClientList(pClientContext);
@@ -327,7 +316,9 @@ namespace netplus {

	void event::runEventloop(ULONG_PTR args) {
		NetException exception;
		EventWorkerArgs eargs;
		std::shared_ptr<EventWorkerArgs> eargs;

		eargs = std::make_shared<EventWorkerArgs>();

		HANDLE iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 0);

@@ -344,20 +335,20 @@ namespace netplus {

		memset(&olOverlap, 0, sizeof(olOverlap));

		poll evpoll(iocp, this, _ServerSocket);
		poll evpoll(iocp,std::shared_ptr<event>(this), _ServerSocket);

		eargs.ssocket = _ServerSocket;
		eargs.event = this;
		eargs.evpoll = &evpoll;
		eargs.timeout = _Timeout;
		eargs.eviocp = iocp;
		eargs->ssocket = _ServerSocket;
		eargs->event = this;
		eargs->evpoll = &evpoll;
		eargs->timeout = _Timeout;
		eargs->eviocp = iocp;

		std::vector <std::thread> threadpool;

		for (int i = 0; i < threads; i++) {
			try {
				threadpool.push_back(std::thread([&eargs, args, i] {
					EventWorker(i, args, &eargs);
				threadpool.push_back(std::thread([eargs, args, i] {
					EventWorker(i, args, eargs);
					}));
			}
			catch (NetException& e) {
+7 −8
Changes for src/eventapi.h: 7 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -41,19 +41,18 @@ namespace netplus {
        class eventapi {
        public:
             /*HTTP API Events*/
            virtual void RequestEvent(con *curcon,const int tid,ULONG_PTR args);
            virtual void ResponseEvent(con *curcon,const int tid, ULONG_PTR args);
            virtual void ConnectEvent(con *curcon,const int tid, ULONG_PTR args);
            virtual void DisconnectEvent(con *curcon,const int tid, ULONG_PTR args);
            virtual void RequestEvent(std::shared_ptr <con> curcon,const int tid,ULONG_PTR args);
            virtual void ResponseEvent(std::shared_ptr <con> curcon,const int tid, ULONG_PTR args);
            virtual void ConnectEvent(std::shared_ptr <con> curcon,const int tid, ULONG_PTR args);
            virtual void DisconnectEvent(std::shared_ptr <con> curcon,const int tid, ULONG_PTR args);

            /*memory allocation*/
            virtual void CreateConnetion(con **curon)=0;
            virtual void deleteConnetion(con *curon)=0;
            virtual void CreateConnetion(std::shared_ptr<con> curon)=0;
        };

        class event : public eventapi{
        public:
            event(socket *serversocket,int timeout = -1);
            event(std::shared_ptr<socket> serversocket,int timeout = -1);
            void runEventloop(ULONG_PTR args=0);

            virtual ~event();
@@ -62,7 +61,7 @@ namespace netplus {

            int                      threads;
        private:
            socket                  *_ServerSocket;
            std::shared_ptr<socket>  _ServerSocket;
            int                      _Timeout;
            int                      _pollFD;
            friend class EventWorker;
+6 −6
Changes for src/socket.h: 6 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -54,9 +54,9 @@ namespace netplus {

            void                     setTimeout(int sec);
            
            virtual void             accept(socket *csock)=0;
            virtual void             accept(std::shared_ptr<socket> csock)=0;
#ifdef Windows 
            virtual void             accept(LPFN_ACCEPTEX lpfnAcceptEx, socket* ssock)=0;
            virtual void             accept(LPFN_ACCEPTEX lpfnAcceptEx, std::shared_ptr<socket> ssock)=0;
#endif
            virtual void             bind()=0;
            virtual void             listen()=0;
@@ -100,9 +100,9 @@ namespace netplus {
                int sockopts);
            ~tcp();
#ifdef Windows 
            void          accept(LPFN_ACCEPTEX lpfnAcceptEx,socket* ssock);
            void          accept(LPFN_ACCEPTEX lpfnAcceptEx, std::shared_ptr<socket> ssock);
#endif
            void          accept(socket *ssock);
            void          accept(std::shared_ptr<socket> ssock);
            void          bind();
            void          listen();
            void          setNonBlock();
@@ -132,9 +132,9 @@ namespace netplus {
                int sockopts);
            ~udp();

            void          accept(socket * ssock);
            void          accept(std::shared_ptr<socket> ssock);
#ifdef Windows 
            void          accept(LPFN_ACCEPTEX lpfnAcceptEx, socket* ssock);
            void          accept(LPFN_ACCEPTEX lpfnAcceptEx, std::shared_ptr<socket> ssock);
#endif
            void          bind();
            void          listen();
Loading