Loading src/event/kqueue.cpp +45 −51 Changes for src/event/kqueue.cpp: 45 added lines, 51 removed lines. Original line number Diff line number Diff line Loading @@ -55,7 +55,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace netplus { std::atomic<bool> RUNNING(true); std::mutex CLOSE_HANDLER_MUTEX; std::mutex POLL_HANDLER_MUTEX; void signalHandler(int signum) { switch(signum){ Loading Loading @@ -149,9 +149,6 @@ namespace netplus { EV_SET(&setevent,curcon->csock->fd(),filter,events,0,0,curcon); if (kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr) < 0) { if (events & EV_DELETE) { return; } except[NetException::Error] << "_setPollEvents: can change socket!"; throw except; } Loading @@ -168,17 +165,15 @@ namespace netplus { } int waitEventHandler(int timeout) { NetException exception; struct timespec ts; ts.tv_nsec = timeout; int evn = kevent(_pollFD,nullptr,0,_Events,_ServerSocket->getMaxconnections(),&ts); if (evn < 0 ) { NetException exception; char str[255]; strerror_r_netplus(errno,str,255); exception[NetException::Error] << "waitEventHandler: kqueue wait failure: " << str; exception[NetException::Error] << "waitEventHandler: epoll wait failure: " << str; throw exception; } return evn; Loading @@ -186,21 +181,23 @@ namespace netplus { void ConnectEventHandler(int pos,const int tid,ULONG_PTR args) { NetException exception; std::unique_ptr<con> ccon; _evtapi->CreateConnection(ccon); evconnection->CreateConnection(ccon); if(_ServerSocket->_Type==sockettype::TCP){ ccon->csock=std::make_unique<tcp>(); ccon->csock=std::make_unique<tcp>(-1); }else if(_ServerSocket->_Type==sockettype::UDP){ ccon->csock=std::make_unique<udp>(); ccon->csock=std::make_unique<udp>(-1); }else if(_ServerSocket->_Type==sockettype::SSL){ ccon->csock=std::make_unique<ssl>(); // clt->connection->csock=std::make_unique<ssl>( -1); } _ServerSocket->accept(ccon->csock); ccon->csock->setFlag(O_NONBLOCK,1); std::string ip; ccon->csock->getAddress(ip); std::cout << "Connected: " << ip << std::endl; Loading @@ -208,50 +205,41 @@ namespace netplus { ccon->lasteventime = time(nullptr); struct kevent setevent { 0 }; con *ccon_ptr=ccon.release(); EV_SET(&setevent, ccon_ptr->csock->fd(), EVFILT_READ, EV_ADD | EV_ONESHOT,0,0,ccon_ptr); setevent.data.ptr = ccon.get(); int estate = kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr); if ( estate < 0 ) { char errstr[255]; strerror_r_netplus(errno,errstr,255); exception[NetException::Error] << "ConnectEventHandler: can't add socket to kqueue: " << errstr; exception[NetException::Error] << "ConnectEventHandler: can't add socket to epoll: " << errstr; throw exception; } _evtapi->ConnectEvent(*ccon_ptr,tid,args); }; void ReadEventHandler(int pos,const int tid,ULONG_PTR args) { con *rcon = (con*)_Events[pos].udata; con *rcon = reinterpret_cast<con*>(_Events[pos].data.ptr); buffer buf(BLOCKSIZE); if (!rcon || !rcon->csock) { return; } std::lock_guard<std::mutex> lock(rcon->event_mutex); std::lock_guard<std::mutex> lock_guard(rcon->event_mutex); if (!rcon->csock) { return; } try{ buffer buf(BLOCKSIZE); size_t rcvsize = rcon->csock->recvData(buf,0); rcon->lasteventime = time(nullptr); size_t rcvsize = rcon->csock->recvData(buf, BLOCKSIZE); if (rcvsize == 0) { CloseEventHandler(pos, tid, args); return; // Connection closed, exit handler } rcon->lasteventime = time(nullptr); if(rcvsize>0){ if(rcvsize!=0){ rcon->RecvData.append(buf.data.buf,rcvsize); } _evtapi->RequestEvent(*rcon,tid,args); evconnection->RequestEvent(*rcon,tid,args); if(!rcon->SendData.empty()){ setpollEvents(rcon,EVFILT_WRITE,EV_ADD | EV_ONESHOT); Loading @@ -267,22 +255,21 @@ namespace netplus { } throw e; } } }; void WriteEventHandler(int pos,const int tid,ULONG_PTR args) { con *wcon = (con*)_Events[pos].udata; con *wcon = reinterpret_cast<con*>(_Events[pos].data.ptr); if(!wcon || !wcon->csock){ return; } std::lock_guard<std::mutex> lock(wcon->event_mutex); std::lock_guard<std::mutex> lock_guard(wcon->event_mutex); if (!wcon->csock) { return; } try{ _evtapi->ResponseEvent(*wcon,tid,args); evconnection->ResponseEvent(*wcon,tid,args); if(wcon->SendData.empty()){ setpollEvents(wcon,EVFILT_READ,EV_ADD | EV_ONESHOT); Loading @@ -291,16 +278,12 @@ namespace netplus { size_t ssize = BLOCKSIZE < wcon->SendData.size() ? BLOCKSIZE : wcon->SendData.size(); size_t sended; buffer buf(wcon->SendData.data(),ssize); sended = wcon->csock->sendData(buf,0); size_t sended = wcon->csock->sendData(buf); if (sended > ssize) { NetException e; e[NetException::Note] << "WriteEventHandler: Connection aborted during send."; e[NetException::Note] << "WriteEventHandler: Fatal socket error detected. Closing connection."; throw e; } Loading @@ -308,10 +291,6 @@ namespace netplus { wcon->lasteventime = time(nullptr); if(wcon->SendData.empty()){ setpollEvents(wcon,EVFILT_READ,EV_ADD | EV_ONESHOT); return; } setpollEvents(wcon,EVFILT_WRITE,EV_ADD | EV_ONESHOT); }catch(NetException &e){ if(e.getErrorType()== NetException::Note){ Loading @@ -323,19 +302,34 @@ namespace netplus { } }; void CloseEventHandler(int pos, const int tid, ULONG_PTR args) { std::lock_guard<std::mutex> global_lock(CLOSE_HANDLER_MUTEX); std::lock_guard<std::mutex> global_lock(POLL_HANDLER_MUTEX); con *ccon = (con*)_Events[pos].udata; if (!ccon) { if (!_Events[pos].data.ptr) { return; } con *ccon = reinterpret_cast<con*>(_Events[pos].data.ptr); { std::lock_guard<std::mutex> lock(ccon->event_mutex); _Events[pos].udata = nullptr; // Clear the pointer if (ccon->csock) { try { if (kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr) < 0) { NetException except; char errstr[255]; strerror_r_netplus(errno,errstr,255); except[NetException::Error] << "CloseEventHandler: can't close socket to epoll: " << errstr; throw except; } evconnection->DisconnectEvent(*ccon, tid, args); } catch (NetException &e) { throw e; } } _Events[pos].data.ptr = nullptr; } delete ccon; Loading Loading
src/event/kqueue.cpp +45 −51 Changes for src/event/kqueue.cpp: 45 added lines, 51 removed lines. Original line number Diff line number Diff line Loading @@ -55,7 +55,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace netplus { std::atomic<bool> RUNNING(true); std::mutex CLOSE_HANDLER_MUTEX; std::mutex POLL_HANDLER_MUTEX; void signalHandler(int signum) { switch(signum){ Loading Loading @@ -149,9 +149,6 @@ namespace netplus { EV_SET(&setevent,curcon->csock->fd(),filter,events,0,0,curcon); if (kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr) < 0) { if (events & EV_DELETE) { return; } except[NetException::Error] << "_setPollEvents: can change socket!"; throw except; } Loading @@ -168,17 +165,15 @@ namespace netplus { } int waitEventHandler(int timeout) { NetException exception; struct timespec ts; ts.tv_nsec = timeout; int evn = kevent(_pollFD,nullptr,0,_Events,_ServerSocket->getMaxconnections(),&ts); if (evn < 0 ) { NetException exception; char str[255]; strerror_r_netplus(errno,str,255); exception[NetException::Error] << "waitEventHandler: kqueue wait failure: " << str; exception[NetException::Error] << "waitEventHandler: epoll wait failure: " << str; throw exception; } return evn; Loading @@ -186,21 +181,23 @@ namespace netplus { void ConnectEventHandler(int pos,const int tid,ULONG_PTR args) { NetException exception; std::unique_ptr<con> ccon; _evtapi->CreateConnection(ccon); evconnection->CreateConnection(ccon); if(_ServerSocket->_Type==sockettype::TCP){ ccon->csock=std::make_unique<tcp>(); ccon->csock=std::make_unique<tcp>(-1); }else if(_ServerSocket->_Type==sockettype::UDP){ ccon->csock=std::make_unique<udp>(); ccon->csock=std::make_unique<udp>(-1); }else if(_ServerSocket->_Type==sockettype::SSL){ ccon->csock=std::make_unique<ssl>(); // clt->connection->csock=std::make_unique<ssl>( -1); } _ServerSocket->accept(ccon->csock); ccon->csock->setFlag(O_NONBLOCK,1); std::string ip; ccon->csock->getAddress(ip); std::cout << "Connected: " << ip << std::endl; Loading @@ -208,50 +205,41 @@ namespace netplus { ccon->lasteventime = time(nullptr); struct kevent setevent { 0 }; con *ccon_ptr=ccon.release(); EV_SET(&setevent, ccon_ptr->csock->fd(), EVFILT_READ, EV_ADD | EV_ONESHOT,0,0,ccon_ptr); setevent.data.ptr = ccon.get(); int estate = kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr); if ( estate < 0 ) { char errstr[255]; strerror_r_netplus(errno,errstr,255); exception[NetException::Error] << "ConnectEventHandler: can't add socket to kqueue: " << errstr; exception[NetException::Error] << "ConnectEventHandler: can't add socket to epoll: " << errstr; throw exception; } _evtapi->ConnectEvent(*ccon_ptr,tid,args); }; void ReadEventHandler(int pos,const int tid,ULONG_PTR args) { con *rcon = (con*)_Events[pos].udata; con *rcon = reinterpret_cast<con*>(_Events[pos].data.ptr); buffer buf(BLOCKSIZE); if (!rcon || !rcon->csock) { return; } std::lock_guard<std::mutex> lock(rcon->event_mutex); std::lock_guard<std::mutex> lock_guard(rcon->event_mutex); if (!rcon->csock) { return; } try{ buffer buf(BLOCKSIZE); size_t rcvsize = rcon->csock->recvData(buf,0); rcon->lasteventime = time(nullptr); size_t rcvsize = rcon->csock->recvData(buf, BLOCKSIZE); if (rcvsize == 0) { CloseEventHandler(pos, tid, args); return; // Connection closed, exit handler } rcon->lasteventime = time(nullptr); if(rcvsize>0){ if(rcvsize!=0){ rcon->RecvData.append(buf.data.buf,rcvsize); } _evtapi->RequestEvent(*rcon,tid,args); evconnection->RequestEvent(*rcon,tid,args); if(!rcon->SendData.empty()){ setpollEvents(rcon,EVFILT_WRITE,EV_ADD | EV_ONESHOT); Loading @@ -267,22 +255,21 @@ namespace netplus { } throw e; } } }; void WriteEventHandler(int pos,const int tid,ULONG_PTR args) { con *wcon = (con*)_Events[pos].udata; con *wcon = reinterpret_cast<con*>(_Events[pos].data.ptr); if(!wcon || !wcon->csock){ return; } std::lock_guard<std::mutex> lock(wcon->event_mutex); std::lock_guard<std::mutex> lock_guard(wcon->event_mutex); if (!wcon->csock) { return; } try{ _evtapi->ResponseEvent(*wcon,tid,args); evconnection->ResponseEvent(*wcon,tid,args); if(wcon->SendData.empty()){ setpollEvents(wcon,EVFILT_READ,EV_ADD | EV_ONESHOT); Loading @@ -291,16 +278,12 @@ namespace netplus { size_t ssize = BLOCKSIZE < wcon->SendData.size() ? BLOCKSIZE : wcon->SendData.size(); size_t sended; buffer buf(wcon->SendData.data(),ssize); sended = wcon->csock->sendData(buf,0); size_t sended = wcon->csock->sendData(buf); if (sended > ssize) { NetException e; e[NetException::Note] << "WriteEventHandler: Connection aborted during send."; e[NetException::Note] << "WriteEventHandler: Fatal socket error detected. Closing connection."; throw e; } Loading @@ -308,10 +291,6 @@ namespace netplus { wcon->lasteventime = time(nullptr); if(wcon->SendData.empty()){ setpollEvents(wcon,EVFILT_READ,EV_ADD | EV_ONESHOT); return; } setpollEvents(wcon,EVFILT_WRITE,EV_ADD | EV_ONESHOT); }catch(NetException &e){ if(e.getErrorType()== NetException::Note){ Loading @@ -323,19 +302,34 @@ namespace netplus { } }; void CloseEventHandler(int pos, const int tid, ULONG_PTR args) { std::lock_guard<std::mutex> global_lock(CLOSE_HANDLER_MUTEX); std::lock_guard<std::mutex> global_lock(POLL_HANDLER_MUTEX); con *ccon = (con*)_Events[pos].udata; if (!ccon) { if (!_Events[pos].data.ptr) { return; } con *ccon = reinterpret_cast<con*>(_Events[pos].data.ptr); { std::lock_guard<std::mutex> lock(ccon->event_mutex); _Events[pos].udata = nullptr; // Clear the pointer if (ccon->csock) { try { if (kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr) < 0) { NetException except; char errstr[255]; strerror_r_netplus(errno,errstr,255); except[NetException::Error] << "CloseEventHandler: can't close socket to epoll: " << errstr; throw except; } evconnection->DisconnectEvent(*ccon, tid, args); } catch (NetException &e) { throw e; } } _Events[pos].data.ptr = nullptr; } delete ccon; Loading