From fee6ee79725ae1751d47f321244014b470bb6de8 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Thu, 5 Jul 2018 01:56:22 +0800 Subject: [PATCH] Add epoll support --- gsock.cpp | 30 ++++++++++++++++++++++++++++++ gsock.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/gsock.cpp b/gsock.cpp index d061e8e..0fa70e6 100644 --- a/gsock.cpp +++ b/gsock.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #define closesocket close using BYTE = unsigned char; #define WSAGetLastError() errno @@ -1066,6 +1067,35 @@ bool selector::is_error(const vsock& v) return FD_ISSET(v._vp->sfd, &_pp->errorset); } +#ifdef WIN32 // Windows: IOCP. Coming soon... + +#else // Linux: epoll +epoll::epoll() +{ + _fd=epoll_create(1024); // this parameter is useless. +} +epoll::~epoll() +{ + close(_fd); +} +int epoll::add(const vsock& v,epoll_event* event) +{ + return epoll_ctl(_fd,EPOLL_CTL_ADD,v._vp->sfd,event); +} +int epoll::mod(const vsock& v,epoll_event* event) +{ + return epoll_ctl(_fd,EPOLL_CTL_MOD,v._vp->sfd,event); +} +int epoll::del(const vsock& v,epoll_event* event) +{ + return epoll_ctl(_fd,EPOLL_CTL_DEL,v._vp->sfd,event); +} +int epoll::wait(epoll_event* events,int maxsize,int timeout) +{ + return epoll_wait(_fd,events,maxsize,timeout); +} +#endif + int DNSResolve(const std::string& HostName, std::vector& _out_IPStrVec) { std::vector vec; diff --git a/gsock.h b/gsock.h index 922b9b1..a512f6f 100644 --- a/gsock.h +++ b/gsock.h @@ -39,6 +39,11 @@ protected: _impl* _vp; friend class selector; +#ifdef WIN32 + +#else + friend class epoll; +#endif }; class sock : public vsock @@ -220,6 +225,29 @@ private: _impl* _pp; }; +#ifdef WIN32 // Windows: IOCP. Coming soon... + +#else // Linux: epoll +class epoll +{ +public: + epoll(); + int add(const vsock& v,epoll_event* event); + int mod(const vsock& v,epoll_event* event); + int del(const vsock& v,epoll_event* event); + + // >0: event counts. + // =0: Time up. + // <0: Error. + // Set timeout to -1 for infinity waiting. + int wait(epoll_event* events,int maxsize,int timeout); + + ~epoll(); +private: + int _fd; +}; +#endif // End of Platform specific + /// Net Tools // Return: