Loading src/connection.cpp +1 −1 Changes for src/connection.cpp: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -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; } Loading src/connection.h +4 −4 Changes for src/connection.h: 4 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; }; }; src/event/iocp.cpp +35 −44 Changes for src/event/iocp.cpp: 35 added lines, 44 removed lines. Original line number Diff line number Diff line Loading @@ -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); Loading @@ -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(); Loading @@ -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); Loading @@ -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); Loading @@ -142,7 +136,6 @@ namespace netplus { AssociateWithIOCP(pClientContext); } catch (NetException& e) { delete pClientContext->CurCon->csock; RemoveFromClientList(pClientContext); throw e; } Loading @@ -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!"; Loading Loading @@ -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; Loading @@ -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); Loading Loading @@ -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); Loading @@ -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) { Loading src/eventapi.h +7 −8 Changes for src/eventapi.h: 7 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -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(); Loading @@ -62,7 +61,7 @@ namespace netplus { int threads; private: socket *_ServerSocket; std::shared_ptr<socket> _ServerSocket; int _Timeout; int _pollFD; friend class EventWorker; Loading src/socket.h +6 −6 Changes for src/socket.h: 6 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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(); Loading Loading @@ -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 Loading
src/connection.cpp +1 −1 Changes for src/connection.cpp: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -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; } Loading
src/connection.h +4 −4 Changes for src/connection.h: 4 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; }; };
src/event/iocp.cpp +35 −44 Changes for src/event/iocp.cpp: 35 added lines, 44 removed lines. Original line number Diff line number Diff line Loading @@ -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); Loading @@ -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(); Loading @@ -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); Loading @@ -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); Loading @@ -142,7 +136,6 @@ namespace netplus { AssociateWithIOCP(pClientContext); } catch (NetException& e) { delete pClientContext->CurCon->csock; RemoveFromClientList(pClientContext); throw e; } Loading @@ -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!"; Loading Loading @@ -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; Loading @@ -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); Loading Loading @@ -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); Loading @@ -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) { Loading
src/eventapi.h +7 −8 Changes for src/eventapi.h: 7 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -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(); Loading @@ -62,7 +61,7 @@ namespace netplus { int threads; private: socket *_ServerSocket; std::shared_ptr<socket> _ServerSocket; int _Timeout; int _pollFD; friend class EventWorker; Loading
src/socket.h +6 −6 Changes for src/socket.h: 6 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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(); Loading Loading @@ -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