Loading src/connection.cpp −2 Changes for src/connection.cpp: 0 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -104,12 +104,10 @@ size_t netplus::condata<char>::search(const char* word){ } netplus::con::con() : csock(nullptr){ state=0; lasteventime = time(nullptr); } netplus::con::con(eventapi *event) : csock(nullptr) { state = 0; lasteventime = time(nullptr); csock = nullptr; } Loading src/connection.h −3 Changes for src/connection.h: 0 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -152,9 +152,6 @@ namespace netplus { /*clientsocket*/ std::unique_ptr<socket> csock; /*event stauts*/ int state; /*connection Data*/ condata<char> RecvData; condata<char> SendData; Loading src/event/epoll.cpp +50 −30 Changes for src/event/epoll.cpp: 50 added lines, 30 removed lines. Original line number Diff line number Diff line Loading @@ -116,6 +116,10 @@ namespace netplus { }; virtual ~poll() { for (int i = 0; i <_ServerSocket->getMaxconnections(); ++i){ if(_Events[i].data.ptr) delete reinterpret_cast<con*>(_Events[i].data.ptr); } }; /*basic functions*/ Loading @@ -128,6 +132,10 @@ namespace netplus { }; int getEventFlags(int pos) { return _Events[pos].events; } void setpollEvents(int pos,int events){ NetException except; struct epoll_event setevent = { 0 }; Loading @@ -146,10 +154,7 @@ namespace netplus { int pollState(int pos){ if(!_Events[pos].data.ptr) return EventHandlerStatus::EVCON; con *pcon = reinterpret_cast<con*>(_Events[pos].data.ptr); return pcon->state; return _Events[pos].events; } int waitEventHandler(int timeout) { Loading Loading @@ -189,10 +194,9 @@ namespace netplus { std::cout << "Connected: " << ip << std::endl; ccon->lasteventime = time(nullptr); ccon->state=EVIN; struct epoll_event setevent { 0 }; setevent.events = EPOLLIN | EPOLLONESHOT; setevent.events = EPOLLIN | EPOLLONESHOT | EPOLLET; con *oldptr=ccon.release(); setevent.data.ptr = oldptr; Loading Loading @@ -220,13 +224,11 @@ namespace netplus { if(rcvsize!=0){ rcon->RecvData.append(buf.data.buf,rcvsize); rcon->state=EVIN; } evconnection->RequestEvent(*rcon,tid,args); if(!rcon->SendData.empty()){ rcon->state=EVOUT; setpollEvents(pos,EPOLLOUT | EPOLLONESHOT); return; } Loading @@ -235,7 +237,6 @@ namespace netplus { }catch(NetException &e){ if(e.getErrorType()== NetException::Note){ rcon->state=EVIN; setpollEvents(pos,EPOLLIN | EPOLLONESHOT); return; } Loading @@ -250,13 +251,11 @@ namespace netplus { evconnection->ResponseEvent(*wcon,tid,args); if(wcon->SendData.empty()){ wcon->state=EVIN; setpollEvents(pos,EPOLLIN | EPOLLONESHOT); return; } size_t ssize = BLOCKSIZE < wcon->SendData.size() ? BLOCKSIZE : wcon->SendData.size(); wcon->state=EVOUT; buffer buf(wcon->SendData.data(),ssize); size_t sended = wcon->csock->sendData(buf); Loading @@ -267,7 +266,6 @@ namespace netplus { setpollEvents(pos,EPOLLOUT | EPOLLONESHOT); }catch(NetException &e){ if(e.getErrorType()== NetException::Note){ wcon->state=EVOUT; setpollEvents(pos,EPOLLOUT | EPOLLONESHOT); return; }else{ Loading @@ -278,6 +276,12 @@ namespace netplus { void CloseEventHandler(int pos,const int tid,ULONG_PTR args) { std::lock_guard<std::mutex> lock(_pollMutex); if (!_Events[pos].data.ptr) { return; } con *ccon = reinterpret_cast<con*>(_Events[pos].data.ptr); try{ Loading @@ -300,7 +304,7 @@ namespace netplus { }; private: std::mutex _pollMutex; int _pollFD; std::unique_ptr<struct epoll_event[]> _Events; socket* _ServerSocket; Loading Loading @@ -339,33 +343,51 @@ namespace netplus { int wait=0; // In EventWorker::EventWorker: while(RUNNING){ try { wait=pollptr.waitEventHandler(eargs->timeout); for(int i =0; i<wait; ++i){ try{ switch (pollptr.pollState(i)) { case pollapi::EventHandlerStatus::EVCON: int events = pollptr.pollState(i); // Check for listener socket (data.ptr is nullptr) if (events == pollapi::EventHandlerStatus::EVCON) { pollptr.ConnectEventHandler(i,tid,args); continue; case pollapi::EventHandlerStatus::EVIN: } events = pollptr.getEventFlags(i); // Check for error/hangs (EPOLLHUP/EPOLLERR) if (events & (EPOLLHUP | EPOLLERR)) { // Treat as close event if any of these flags are set pollptr.CloseEventHandler(i,tid,args); continue; } // Prioritize read events (new data from client) if (events & EPOLLIN) { pollptr.ReadEventHandler(i,tid,args); continue; case pollapi::EventHandlerStatus::EVOUT: } // Handle write events (ready to send data) if (events & EPOLLOUT) { pollptr.WriteEventHandler(i,tid,args); continue; default: } // FALLBACK/DEFAULT HANDLER NetException e; e[NetException::Error] << "EventWorker: Request type not known!"; e[NetException::Error] << "EventWorker: Unhandled epoll event type!"; throw e; } }catch(NetException& e){ switch(e.getErrorType()){ case NetException::Note: continue; default: std::cerr << e.what() << std::endl; pollptr.CloseEventHandler(i,tid,args); } if(e.getErrorType()==NetException::Critical) throw e; } } }catch (NetException& e) { Loading Loading @@ -399,9 +421,9 @@ namespace netplus { } _Timeout=timeout; _ServerSocket->bind(); _ServerSocket->setFlag(O_NONBLOCK,1); _ServerSocket->setNonBlock(); _ServerSocket->listen(); threads=-1; threads=sysconf(_SC_NPROCESSORS_ONLN); } event::event(const event &src) : _ServerSocket(src._ServerSocket) { Loading @@ -419,8 +441,6 @@ namespace netplus { } void event::runEventloop(ULONG_PTR args) { if(threads<0) threads=sysconf(_SC_NPROCESSORS_ONLN); NetException exception; signal(SIGINT, signalHandler); Loading Loading @@ -458,7 +478,7 @@ MAINWORKERLOOP: for (int i = 0; i < threads; i++) { try { threadpool.push_back( std::thread([&eargs,args,i]{ threadpool.emplace_back( std::thread([&eargs,args,i]{ EventWorker(i,args,&eargs); })); } catch (NetException& e) { Loading Loading
src/connection.cpp −2 Changes for src/connection.cpp: 0 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -104,12 +104,10 @@ size_t netplus::condata<char>::search(const char* word){ } netplus::con::con() : csock(nullptr){ state=0; lasteventime = time(nullptr); } netplus::con::con(eventapi *event) : csock(nullptr) { state = 0; lasteventime = time(nullptr); csock = nullptr; } Loading
src/connection.h −3 Changes for src/connection.h: 0 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -152,9 +152,6 @@ namespace netplus { /*clientsocket*/ std::unique_ptr<socket> csock; /*event stauts*/ int state; /*connection Data*/ condata<char> RecvData; condata<char> SendData; Loading
src/event/epoll.cpp +50 −30 Changes for src/event/epoll.cpp: 50 added lines, 30 removed lines. Original line number Diff line number Diff line Loading @@ -116,6 +116,10 @@ namespace netplus { }; virtual ~poll() { for (int i = 0; i <_ServerSocket->getMaxconnections(); ++i){ if(_Events[i].data.ptr) delete reinterpret_cast<con*>(_Events[i].data.ptr); } }; /*basic functions*/ Loading @@ -128,6 +132,10 @@ namespace netplus { }; int getEventFlags(int pos) { return _Events[pos].events; } void setpollEvents(int pos,int events){ NetException except; struct epoll_event setevent = { 0 }; Loading @@ -146,10 +154,7 @@ namespace netplus { int pollState(int pos){ if(!_Events[pos].data.ptr) return EventHandlerStatus::EVCON; con *pcon = reinterpret_cast<con*>(_Events[pos].data.ptr); return pcon->state; return _Events[pos].events; } int waitEventHandler(int timeout) { Loading Loading @@ -189,10 +194,9 @@ namespace netplus { std::cout << "Connected: " << ip << std::endl; ccon->lasteventime = time(nullptr); ccon->state=EVIN; struct epoll_event setevent { 0 }; setevent.events = EPOLLIN | EPOLLONESHOT; setevent.events = EPOLLIN | EPOLLONESHOT | EPOLLET; con *oldptr=ccon.release(); setevent.data.ptr = oldptr; Loading Loading @@ -220,13 +224,11 @@ namespace netplus { if(rcvsize!=0){ rcon->RecvData.append(buf.data.buf,rcvsize); rcon->state=EVIN; } evconnection->RequestEvent(*rcon,tid,args); if(!rcon->SendData.empty()){ rcon->state=EVOUT; setpollEvents(pos,EPOLLOUT | EPOLLONESHOT); return; } Loading @@ -235,7 +237,6 @@ namespace netplus { }catch(NetException &e){ if(e.getErrorType()== NetException::Note){ rcon->state=EVIN; setpollEvents(pos,EPOLLIN | EPOLLONESHOT); return; } Loading @@ -250,13 +251,11 @@ namespace netplus { evconnection->ResponseEvent(*wcon,tid,args); if(wcon->SendData.empty()){ wcon->state=EVIN; setpollEvents(pos,EPOLLIN | EPOLLONESHOT); return; } size_t ssize = BLOCKSIZE < wcon->SendData.size() ? BLOCKSIZE : wcon->SendData.size(); wcon->state=EVOUT; buffer buf(wcon->SendData.data(),ssize); size_t sended = wcon->csock->sendData(buf); Loading @@ -267,7 +266,6 @@ namespace netplus { setpollEvents(pos,EPOLLOUT | EPOLLONESHOT); }catch(NetException &e){ if(e.getErrorType()== NetException::Note){ wcon->state=EVOUT; setpollEvents(pos,EPOLLOUT | EPOLLONESHOT); return; }else{ Loading @@ -278,6 +276,12 @@ namespace netplus { void CloseEventHandler(int pos,const int tid,ULONG_PTR args) { std::lock_guard<std::mutex> lock(_pollMutex); if (!_Events[pos].data.ptr) { return; } con *ccon = reinterpret_cast<con*>(_Events[pos].data.ptr); try{ Loading @@ -300,7 +304,7 @@ namespace netplus { }; private: std::mutex _pollMutex; int _pollFD; std::unique_ptr<struct epoll_event[]> _Events; socket* _ServerSocket; Loading Loading @@ -339,33 +343,51 @@ namespace netplus { int wait=0; // In EventWorker::EventWorker: while(RUNNING){ try { wait=pollptr.waitEventHandler(eargs->timeout); for(int i =0; i<wait; ++i){ try{ switch (pollptr.pollState(i)) { case pollapi::EventHandlerStatus::EVCON: int events = pollptr.pollState(i); // Check for listener socket (data.ptr is nullptr) if (events == pollapi::EventHandlerStatus::EVCON) { pollptr.ConnectEventHandler(i,tid,args); continue; case pollapi::EventHandlerStatus::EVIN: } events = pollptr.getEventFlags(i); // Check for error/hangs (EPOLLHUP/EPOLLERR) if (events & (EPOLLHUP | EPOLLERR)) { // Treat as close event if any of these flags are set pollptr.CloseEventHandler(i,tid,args); continue; } // Prioritize read events (new data from client) if (events & EPOLLIN) { pollptr.ReadEventHandler(i,tid,args); continue; case pollapi::EventHandlerStatus::EVOUT: } // Handle write events (ready to send data) if (events & EPOLLOUT) { pollptr.WriteEventHandler(i,tid,args); continue; default: } // FALLBACK/DEFAULT HANDLER NetException e; e[NetException::Error] << "EventWorker: Request type not known!"; e[NetException::Error] << "EventWorker: Unhandled epoll event type!"; throw e; } }catch(NetException& e){ switch(e.getErrorType()){ case NetException::Note: continue; default: std::cerr << e.what() << std::endl; pollptr.CloseEventHandler(i,tid,args); } if(e.getErrorType()==NetException::Critical) throw e; } } }catch (NetException& e) { Loading Loading @@ -399,9 +421,9 @@ namespace netplus { } _Timeout=timeout; _ServerSocket->bind(); _ServerSocket->setFlag(O_NONBLOCK,1); _ServerSocket->setNonBlock(); _ServerSocket->listen(); threads=-1; threads=sysconf(_SC_NPROCESSORS_ONLN); } event::event(const event &src) : _ServerSocket(src._ServerSocket) { Loading @@ -419,8 +441,6 @@ namespace netplus { } void event::runEventloop(ULONG_PTR args) { if(threads<0) threads=sysconf(_SC_NPROCESSORS_ONLN); NetException exception; signal(SIGINT, signalHandler); Loading Loading @@ -458,7 +478,7 @@ MAINWORKERLOOP: for (int i = 0; i < threads; i++) { try { threadpool.push_back( std::thread([&eargs,args,i]{ threadpool.emplace_back( std::thread([&eargs,args,i]{ EventWorker(i,args,&eargs); })); } catch (NetException& e) { Loading