Add epoll support

This commit is contained in:
Kirigaya Kazuto 2018-07-05 01:56:22 +08:00
parent 37e9ecf160
commit fee6ee7972
2 changed files with 58 additions and 0 deletions

View File

@ -38,6 +38,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/epoll.h>
#define closesocket close #define closesocket close
using BYTE = unsigned char; using BYTE = unsigned char;
#define WSAGetLastError() errno #define WSAGetLastError() errno
@ -1066,6 +1067,35 @@ bool selector::is_error(const vsock& v)
return FD_ISSET(v._vp->sfd, &_pp->errorset); 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<std::string>& _out_IPStrVec) int DNSResolve(const std::string& HostName, std::vector<std::string>& _out_IPStrVec)
{ {
std::vector<std::string> vec; std::vector<std::string> vec;

28
gsock.h
View File

@ -39,6 +39,11 @@ protected:
_impl* _vp; _impl* _vp;
friend class selector; friend class selector;
#ifdef WIN32
#else
friend class epoll;
#endif
}; };
class sock : public vsock class sock : public vsock
@ -220,6 +225,29 @@ private:
_impl* _pp; _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 /// Net Tools
// Return: // Return: