Loading src/connection.h +2 −0 Changes for src/connection.h: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -79,6 +79,8 @@ namespace netplus { /*set sending state*/ void sending(bool state); /*connection locking*/ std::mutex conlock; protected: con(); /*Incomming Data*/ Loading src/eventapi.h +14 −31 Changes for src/eventapi.h: 14 added lines, 31 removed lines. Original line number Diff line number Diff line Loading @@ -25,8 +25,6 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <map> #include "socket.h" #include "connection.h" Loading @@ -44,15 +42,12 @@ namespace netplus { virtual void initEventHandler()=0; virtual const char *getpolltype()=0; /*get pollState by thread id*/ virtual int pollState(int thid,con *ccon)=0; /*EventHandler*/ virtual unsigned int waitEventHandler()=0; virtual void ConnectEventHandler(con** ccon)=0; virtual void ReadEventHandler(con* rcon)=0; virtual void WriteEventHandler(con* wcon)=0; virtual void CloseEventHandler(con** dcon)=0; virtual int ConnectEventHandler(int pos)=0; virtual void ReadEventHandler(int pos)=0; virtual void WriteEventHandler(int pos)=0; virtual void CloseEventHandler(int pos)=0; /*HTTP API Events*/ virtual void RequestEvent(con *curcon)=0; Loading @@ -64,41 +59,30 @@ namespace netplus { * DANGEROUS to burnout your cpu *only use this if know what you do!*/ virtual void sendReady(con *curcon,bool ready)=0; public: size_t getThreadsAmount(){ return thdsamount; } protected: /*store number of threads*/ size_t thdsamount; /*thread -> connection*/ std::map<int,con*> thcon; }; class poll : public eventapi{ public: poll(socket* serversocket); virtual ~poll(); void initEventHandler(); const char *getpolltype(); int pollState(int thid,con *ccon); void initEventHandler(); unsigned int waitEventHandler(); void ConnectEventHandler(con** ccon); void ReadEventHandler(con* rcon); void WriteEventHandler(con *wcon); void CloseEventHandler(con **dcon); int ConnectEventHandler(int pos); void ReadEventHandler(int pos); void WriteEventHandler(int pos); void CloseEventHandler(int pos); void sendReady(con *curcon,bool ready); private: void _lockCon(int pos); void _unlockCon(int pos); bool _trylockCon(int pos); void _lockCon(con *curcon); void _unlockCon(con *curcon); bool _trylockCon(con *curcon); void _setpollEvents(con *curcon,int events); int _pollFD; struct poll_event *_Events; socket *_ServerSocket; std::mutex _StateLock; }; class event : public poll { Loading @@ -116,6 +100,5 @@ namespace netplus { virtual ~event(); static bool _Run; static bool _Restart; std::mutex _StateLock; }; }; src/linux/event.cpp +106 −100 Changes for src/linux/event.cpp: 106 added lines, 100 removed lines. Original line number Diff line number Diff line Loading @@ -26,7 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <iostream> #include <map> #include <vector> #include <thread> #include <unistd.h> #include <signal.h> Loading Loading @@ -67,17 +67,6 @@ namespace netplus { return "EPOLL"; } int poll::pollState(int thid,con *ccon){ ccon=thcon[thid]; if(!ccon){ return poll::EVWAIT; } if(ccon->getSendLength()!=0) return poll::EVOUT; return poll::EVIN; } /*event handler function*/ void poll::initEventHandler() { NetException exception; Loading Loading @@ -120,40 +109,62 @@ namespace netplus { return ret; }; void poll::ConnectEventHandler(con** ccon) { int poll::ConnectEventHandler(int pos) { NetException exception; con* ccon = (con*)_Events[pos].data.ptr; try { if (!ccon) { *ccon = new con(this); (*ccon)->csock = _ServerSocket->accept(); (*ccon)->csock->setnonblocking(); ccon = new con(this); ccon->conlock.lock(); ccon->csock = _ServerSocket->accept(); ccon->csock->setnonblocking(); struct poll_event setevent { 0 }; setevent.events = EPOLLIN; setevent.data.ptr = *ccon; setevent.data.ptr = ccon; if (epoll_ctl(_pollFD, EPOLL_CTL_ADD, (*ccon)->csock->getSocket(), (struct epoll_event*)&setevent) < 0) { if (epoll_ctl(_pollFD, EPOLL_CTL_ADD, ccon->csock->getSocket(), (struct epoll_event*)&setevent) < 0) { exception[NetException::Error] << "ConnectEventHandler: can't add socket to epoll"; throw exception; } ConnectEvent(*ccon); std::cout << "I'am connecting" << std::endl; ConnectEvent(ccon); ccon->conlock.unlock(); return EventHandlerStatus::EVIN; }else{ _trylockCon(ccon); if (ccon->getSendData()) { _unlockCon(ccon); return EventHandlerStatus::EVOUT; } else { _unlockCon(ccon); return EventHandlerStatus::EVIN; } } } catch (NetException& e) { delete (*ccon)->csock; delete *ccon; ccon=nullptr; delete ccon->csock; delete ccon; throw e; } }; void poll::ReadEventHandler(con* rcon) { void poll::ReadEventHandler(int pos) { con* rcon = (con*)_Events[pos].data.ptr; if (!rcon) { NetException exp; exp[NetException::Error] << "ReadEvent: No valied Connection at pos: " << pos; throw exp; } try { if(!_trylockCon(rcon)) return; char buf[BLOCKSIZE]; ssize_t rcvsize = _ServerSocket->recvData(rcon->csock, buf, BLOCKSIZE); if (rcvsize < 0) { NetException exp; exp[NetException::Error] << "ReadEvent: recvData failed close connection!"; exp[NetException::Error] << "ReadEvent: recvData failed at pos: " << pos; throw exp; } Loading @@ -162,14 +173,24 @@ namespace netplus { rcon->addRecvQueue(buf, rcvsize); RequestEvent(rcon); _unlockCon(rcon); } catch (NetException& e) { _unlockCon(rcon); throw e; } }; void poll::WriteEventHandler(con* wcon) { void poll::WriteEventHandler(int pos) { con* wcon = (con*)_Events[pos].data.ptr; if (!wcon) { NetException exp; exp[NetException::Error] << "WriteEvent: No valied Connection at pos: " << pos; throw exp; } try { if(!_trylockCon(wcon)) return; ssize_t sended = _ServerSocket->sendData(wcon->csock, (void*)wcon->getSendData()->getData(), Loading @@ -177,7 +198,7 @@ namespace netplus { if (sended < 0) { NetException exp; exp[NetException::Error] << "WriteEvent: sendData failed failed close connection!"; exp[NetException::Error] << "WriteEvent: sendData failed at pos: " << pos; throw exp; } Loading @@ -186,26 +207,37 @@ namespace netplus { wcon->resizeSendQueue(sended); ResponseEvent(wcon); _unlockCon(wcon); } catch (NetException& e) { _unlockCon(wcon); throw e; } }; void poll::CloseEventHandler(con** dcon) { void poll::CloseEventHandler(int pos) { NetException except; con* delcon = (con*)_Events[pos].data.ptr; if (!delcon) { except[NetException::Error] << "CloseEvent connection empty cannot remove!"; throw except; } _lockCon(delcon); int ect = epoll_ctl(_pollFD, EPOLL_CTL_DEL,(*dcon)->csock->getSocket(), 0); int ect = epoll_ctl(_pollFD, EPOLL_CTL_DEL, delcon->csock->getSocket(), 0); if (ect < 0) { except[NetException::Error] << "CloseEvent can't delete Connection from epoll"; _unlockCon(delcon);; throw except; } DisconnectEvent(*dcon); delete (*dcon)->csock; delete *dcon; dcon=nullptr; DisconnectEvent(delcon); delete delcon->csock; delete delcon; }; /*Connection Ready to send Data*/ Loading @@ -231,41 +263,63 @@ namespace netplus { } }; void poll::_lockCon(con *curcon){ const std::lock_guard<std::mutex> lock(_StateLock); return curcon->conlock.lock(); } void poll::_unlockCon(con *curcon){ const std::lock_guard<std::mutex> lock(_StateLock); return curcon->conlock.unlock(); } bool poll::_trylockCon(con *curcon){ const std::lock_guard<std::mutex> lock(_StateLock); return curcon->conlock.try_lock(); } bool event::_Run = true; bool event::_Restart = false; struct workerArgs{ eventapi *api; int thid; }; class EventWorker{ public: EventWorker(void* args) { eventapi* eventptr = ((workerArgs*)args)->api; int id= ((workerArgs*)args)->thid; eventapi* eventptr = ((eventapi*)args); while (event::_Run) { try { con *ccon=nullptr; switch (eventptr->pollState(id,ccon)) { unsigned int wfd = eventptr->waitEventHandler(); for (int i = 0; i < wfd; ++i) { try { switch (eventptr->ConnectEventHandler(i)) { case poll::EVIN: eventptr->ReadEventHandler(ccon); eventptr->ReadEventHandler(i); break; case poll::EVOUT: eventptr->WriteEventHandler(ccon); eventptr->WriteEventHandler(i); break; default: usleep(1000); eventptr->CloseEventHandler(i); break; } } catch (NetException& e) { std::cout << e.what() << std::endl; eventptr->CloseEventHandler(i); if (e.getErrorType() == NetException::Critical) { throw e; } } } } catch (NetException& e) { switch (e.getErrorType()) { case NetException::Critical: std::cerr << e.what() << std::endl; break; } } } } }; eventapi::~eventapi() { Loading Loading @@ -300,19 +354,16 @@ namespace netplus { } void event::runEventloop() { thdsamount = sysconf(_SC_NPROCESSORS_ONLN); unsigned long thrs = sysconf(_SC_NPROCESSORS_ONLN); signal(SIGPIPE, SIG_IGN); initEventHandler(); MAINWORKERLOOP: std::map<int,std::thread> thpool; for (size_t i = 0; i < thdsamount; i++) { std::vector<std::thread> thpool; for (unsigned long i = 0; i < thrs; i++) { try { struct workerArgs args; args.api=this; args.thid=i; thpool[i]=(std::thread([args](){ new EventWorker((void*)&args); thpool.push_back(std::thread([this](){ new EventWorker((void*)this); })); } catch (NetException& e) { Loading @@ -320,54 +371,8 @@ namespace netplus { } } for (size_t i = 0; i < thdsamount; i++) thcon[i]=nullptr; while (event::_Run) { try { int wfd = waitEventHandler(); for(int i = 0; i < wfd; ++i){ con *ccon=nullptr; try{ ConnectEventHandler(&ccon); bool free=false; for(size_t ii = 0; ii < thdsamount; ii++){ if(!thcon[ii]){ thcon[ii]=ccon; free=true; } } }catch (NetException& e) { switch (e.getErrorType()) { case NetException::Critical:{ throw e; } } std::cout << e.what() << std::endl; if(ccon){ for(size_t ii = 0; ii < thdsamount; ii++){ if(thcon[ii]==ccon){ CloseEventHandler(&ccon); thcon[ii]=nullptr; } } } } } } catch (NetException& e) { switch (e.getErrorType()) { case NetException::Critical:{ throw e; } } } } for(std::map<int,std::thread>::iterator thd = thpool.begin(); thd!=thpool.end(); ++thd){ thd->second.join(); for(std::vector<std::thread>::iterator thd = thpool.begin(); thd!=thpool.end(); ++thd){ thd->join(); } if (event::_Restart) { Loading @@ -376,3 +381,4 @@ namespace netplus { } } }; Loading
src/connection.h +2 −0 Changes for src/connection.h: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -79,6 +79,8 @@ namespace netplus { /*set sending state*/ void sending(bool state); /*connection locking*/ std::mutex conlock; protected: con(); /*Incomming Data*/ Loading
src/eventapi.h +14 −31 Changes for src/eventapi.h: 14 added lines, 31 removed lines. Original line number Diff line number Diff line Loading @@ -25,8 +25,6 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <map> #include "socket.h" #include "connection.h" Loading @@ -44,15 +42,12 @@ namespace netplus { virtual void initEventHandler()=0; virtual const char *getpolltype()=0; /*get pollState by thread id*/ virtual int pollState(int thid,con *ccon)=0; /*EventHandler*/ virtual unsigned int waitEventHandler()=0; virtual void ConnectEventHandler(con** ccon)=0; virtual void ReadEventHandler(con* rcon)=0; virtual void WriteEventHandler(con* wcon)=0; virtual void CloseEventHandler(con** dcon)=0; virtual int ConnectEventHandler(int pos)=0; virtual void ReadEventHandler(int pos)=0; virtual void WriteEventHandler(int pos)=0; virtual void CloseEventHandler(int pos)=0; /*HTTP API Events*/ virtual void RequestEvent(con *curcon)=0; Loading @@ -64,41 +59,30 @@ namespace netplus { * DANGEROUS to burnout your cpu *only use this if know what you do!*/ virtual void sendReady(con *curcon,bool ready)=0; public: size_t getThreadsAmount(){ return thdsamount; } protected: /*store number of threads*/ size_t thdsamount; /*thread -> connection*/ std::map<int,con*> thcon; }; class poll : public eventapi{ public: poll(socket* serversocket); virtual ~poll(); void initEventHandler(); const char *getpolltype(); int pollState(int thid,con *ccon); void initEventHandler(); unsigned int waitEventHandler(); void ConnectEventHandler(con** ccon); void ReadEventHandler(con* rcon); void WriteEventHandler(con *wcon); void CloseEventHandler(con **dcon); int ConnectEventHandler(int pos); void ReadEventHandler(int pos); void WriteEventHandler(int pos); void CloseEventHandler(int pos); void sendReady(con *curcon,bool ready); private: void _lockCon(int pos); void _unlockCon(int pos); bool _trylockCon(int pos); void _lockCon(con *curcon); void _unlockCon(con *curcon); bool _trylockCon(con *curcon); void _setpollEvents(con *curcon,int events); int _pollFD; struct poll_event *_Events; socket *_ServerSocket; std::mutex _StateLock; }; class event : public poll { Loading @@ -116,6 +100,5 @@ namespace netplus { virtual ~event(); static bool _Run; static bool _Restart; std::mutex _StateLock; }; };
src/linux/event.cpp +106 −100 Changes for src/linux/event.cpp: 106 added lines, 100 removed lines. Original line number Diff line number Diff line Loading @@ -26,7 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <iostream> #include <map> #include <vector> #include <thread> #include <unistd.h> #include <signal.h> Loading Loading @@ -67,17 +67,6 @@ namespace netplus { return "EPOLL"; } int poll::pollState(int thid,con *ccon){ ccon=thcon[thid]; if(!ccon){ return poll::EVWAIT; } if(ccon->getSendLength()!=0) return poll::EVOUT; return poll::EVIN; } /*event handler function*/ void poll::initEventHandler() { NetException exception; Loading Loading @@ -120,40 +109,62 @@ namespace netplus { return ret; }; void poll::ConnectEventHandler(con** ccon) { int poll::ConnectEventHandler(int pos) { NetException exception; con* ccon = (con*)_Events[pos].data.ptr; try { if (!ccon) { *ccon = new con(this); (*ccon)->csock = _ServerSocket->accept(); (*ccon)->csock->setnonblocking(); ccon = new con(this); ccon->conlock.lock(); ccon->csock = _ServerSocket->accept(); ccon->csock->setnonblocking(); struct poll_event setevent { 0 }; setevent.events = EPOLLIN; setevent.data.ptr = *ccon; setevent.data.ptr = ccon; if (epoll_ctl(_pollFD, EPOLL_CTL_ADD, (*ccon)->csock->getSocket(), (struct epoll_event*)&setevent) < 0) { if (epoll_ctl(_pollFD, EPOLL_CTL_ADD, ccon->csock->getSocket(), (struct epoll_event*)&setevent) < 0) { exception[NetException::Error] << "ConnectEventHandler: can't add socket to epoll"; throw exception; } ConnectEvent(*ccon); std::cout << "I'am connecting" << std::endl; ConnectEvent(ccon); ccon->conlock.unlock(); return EventHandlerStatus::EVIN; }else{ _trylockCon(ccon); if (ccon->getSendData()) { _unlockCon(ccon); return EventHandlerStatus::EVOUT; } else { _unlockCon(ccon); return EventHandlerStatus::EVIN; } } } catch (NetException& e) { delete (*ccon)->csock; delete *ccon; ccon=nullptr; delete ccon->csock; delete ccon; throw e; } }; void poll::ReadEventHandler(con* rcon) { void poll::ReadEventHandler(int pos) { con* rcon = (con*)_Events[pos].data.ptr; if (!rcon) { NetException exp; exp[NetException::Error] << "ReadEvent: No valied Connection at pos: " << pos; throw exp; } try { if(!_trylockCon(rcon)) return; char buf[BLOCKSIZE]; ssize_t rcvsize = _ServerSocket->recvData(rcon->csock, buf, BLOCKSIZE); if (rcvsize < 0) { NetException exp; exp[NetException::Error] << "ReadEvent: recvData failed close connection!"; exp[NetException::Error] << "ReadEvent: recvData failed at pos: " << pos; throw exp; } Loading @@ -162,14 +173,24 @@ namespace netplus { rcon->addRecvQueue(buf, rcvsize); RequestEvent(rcon); _unlockCon(rcon); } catch (NetException& e) { _unlockCon(rcon); throw e; } }; void poll::WriteEventHandler(con* wcon) { void poll::WriteEventHandler(int pos) { con* wcon = (con*)_Events[pos].data.ptr; if (!wcon) { NetException exp; exp[NetException::Error] << "WriteEvent: No valied Connection at pos: " << pos; throw exp; } try { if(!_trylockCon(wcon)) return; ssize_t sended = _ServerSocket->sendData(wcon->csock, (void*)wcon->getSendData()->getData(), Loading @@ -177,7 +198,7 @@ namespace netplus { if (sended < 0) { NetException exp; exp[NetException::Error] << "WriteEvent: sendData failed failed close connection!"; exp[NetException::Error] << "WriteEvent: sendData failed at pos: " << pos; throw exp; } Loading @@ -186,26 +207,37 @@ namespace netplus { wcon->resizeSendQueue(sended); ResponseEvent(wcon); _unlockCon(wcon); } catch (NetException& e) { _unlockCon(wcon); throw e; } }; void poll::CloseEventHandler(con** dcon) { void poll::CloseEventHandler(int pos) { NetException except; con* delcon = (con*)_Events[pos].data.ptr; if (!delcon) { except[NetException::Error] << "CloseEvent connection empty cannot remove!"; throw except; } _lockCon(delcon); int ect = epoll_ctl(_pollFD, EPOLL_CTL_DEL,(*dcon)->csock->getSocket(), 0); int ect = epoll_ctl(_pollFD, EPOLL_CTL_DEL, delcon->csock->getSocket(), 0); if (ect < 0) { except[NetException::Error] << "CloseEvent can't delete Connection from epoll"; _unlockCon(delcon);; throw except; } DisconnectEvent(*dcon); delete (*dcon)->csock; delete *dcon; dcon=nullptr; DisconnectEvent(delcon); delete delcon->csock; delete delcon; }; /*Connection Ready to send Data*/ Loading @@ -231,41 +263,63 @@ namespace netplus { } }; void poll::_lockCon(con *curcon){ const std::lock_guard<std::mutex> lock(_StateLock); return curcon->conlock.lock(); } void poll::_unlockCon(con *curcon){ const std::lock_guard<std::mutex> lock(_StateLock); return curcon->conlock.unlock(); } bool poll::_trylockCon(con *curcon){ const std::lock_guard<std::mutex> lock(_StateLock); return curcon->conlock.try_lock(); } bool event::_Run = true; bool event::_Restart = false; struct workerArgs{ eventapi *api; int thid; }; class EventWorker{ public: EventWorker(void* args) { eventapi* eventptr = ((workerArgs*)args)->api; int id= ((workerArgs*)args)->thid; eventapi* eventptr = ((eventapi*)args); while (event::_Run) { try { con *ccon=nullptr; switch (eventptr->pollState(id,ccon)) { unsigned int wfd = eventptr->waitEventHandler(); for (int i = 0; i < wfd; ++i) { try { switch (eventptr->ConnectEventHandler(i)) { case poll::EVIN: eventptr->ReadEventHandler(ccon); eventptr->ReadEventHandler(i); break; case poll::EVOUT: eventptr->WriteEventHandler(ccon); eventptr->WriteEventHandler(i); break; default: usleep(1000); eventptr->CloseEventHandler(i); break; } } catch (NetException& e) { std::cout << e.what() << std::endl; eventptr->CloseEventHandler(i); if (e.getErrorType() == NetException::Critical) { throw e; } } } } catch (NetException& e) { switch (e.getErrorType()) { case NetException::Critical: std::cerr << e.what() << std::endl; break; } } } } }; eventapi::~eventapi() { Loading Loading @@ -300,19 +354,16 @@ namespace netplus { } void event::runEventloop() { thdsamount = sysconf(_SC_NPROCESSORS_ONLN); unsigned long thrs = sysconf(_SC_NPROCESSORS_ONLN); signal(SIGPIPE, SIG_IGN); initEventHandler(); MAINWORKERLOOP: std::map<int,std::thread> thpool; for (size_t i = 0; i < thdsamount; i++) { std::vector<std::thread> thpool; for (unsigned long i = 0; i < thrs; i++) { try { struct workerArgs args; args.api=this; args.thid=i; thpool[i]=(std::thread([args](){ new EventWorker((void*)&args); thpool.push_back(std::thread([this](){ new EventWorker((void*)this); })); } catch (NetException& e) { Loading @@ -320,54 +371,8 @@ namespace netplus { } } for (size_t i = 0; i < thdsamount; i++) thcon[i]=nullptr; while (event::_Run) { try { int wfd = waitEventHandler(); for(int i = 0; i < wfd; ++i){ con *ccon=nullptr; try{ ConnectEventHandler(&ccon); bool free=false; for(size_t ii = 0; ii < thdsamount; ii++){ if(!thcon[ii]){ thcon[ii]=ccon; free=true; } } }catch (NetException& e) { switch (e.getErrorType()) { case NetException::Critical:{ throw e; } } std::cout << e.what() << std::endl; if(ccon){ for(size_t ii = 0; ii < thdsamount; ii++){ if(thcon[ii]==ccon){ CloseEventHandler(&ccon); thcon[ii]=nullptr; } } } } } } catch (NetException& e) { switch (e.getErrorType()) { case NetException::Critical:{ throw e; } } } } for(std::map<int,std::thread>::iterator thd = thpool.begin(); thd!=thpool.end(); ++thd){ thd->second.join(); for(std::vector<std::thread>::iterator thd = thpool.begin(); thd!=thpool.end(); ++thd){ thd->join(); } if (event::_Restart) { Loading @@ -376,3 +381,4 @@ namespace netplus { } } };