#ifndef __linux__ enum EPOLL_EVENTS { EPOLLIN = 0x001, #define EPOLLIN EPOLLIN EPOLLPRI = 0x002, #define EPOLLPRI EPOLLPRI EPOLLOUT = 0x004, #define EPOLLOUT EPOLLOUT EPOLLRDNORM = 0x040, #define EPOLLRDNORM EPOLLRDNORM EPOLLRDBAND = 0x080, #define EPOLLRDBAND EPOLLRDBAND EPOLLWRNORM = 0x100, #define EPOLLWRNORM EPOLLWRNORM EPOLLWRBAND = 0x200, #define EPOLLWRBAND EPOLLWRBAND EPOLLMSG = 0x400, #define EPOLLMSG EPOLLMSG EPOLLERR = 0x008, #define EPOLLERR EPOLLERR EPOLLHUP = 0x010, #define EPOLLHUP EPOLLHUP EPOLLRDHUP = 0x2000, #define EPOLLRDHUP EPOLLRDHUP EPOLLONESHOT = (1 << 30), #define EPOLLONESHOT EPOLLONESHOT EPOLLET = (1 << 31) #define EPOLLET EPOLLET }; /* Valid opcodes ( "op" parameter ) to issue to epoll_ctl(). */ #define EPOLL_CTL_ADD 1 /* Add a file descriptor to the interface. */ #define EPOLL_CTL_DEL 2 /* Remove a file descriptor from the interface. */ #define EPOLL_CTL_MOD 3 /* Change file descriptor epoll_event structure. */ typedef union epoll_data { void *ptr; int fd; unsigned int u32; unsigned __int64 u64; } epoll_data_t; struct epoll_event { unsigned __int64 events; /* Epoll events */ epoll_data_t data; /* User data variable */ }; int epoll_create(int size); int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout); #endif
#include "epoll.h" #include <map> #ifndef WIN32 #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #else #include <io.h> #include <Winsock2.h> #pragma comment(lib, "ws2_32.lib") #endif std::map<int, epoll_event> g_mapSockets;
int epoll_create(int size) { return 1; }
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { switch(op) { case EPOLL_CTL_ADD: case EPOLL_CTL_MOD: g_mapSockets[fd] = *event; return 0; case EPOLL_CTL_DEL: if (g_mapSockets.find(fd) == g_mapSockets.end()) return -1; g_mapSockets.erase(fd); return 0; } return 0; }
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) { if ((!events) || (!maxevents)) return -1; // select fd_set readfds, writefds, exceptfds; FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); // int nFDS = 0; for (auto it=g_mapSockets.begin(); it != g_mapSockets.end(); ++it) { if (it->first == -1) continue; if (it->first > nFDS) nFDS = it->first; FD_SET(it->first, &readfds); FD_SET(it->first, &writefds); FD_SET(it->first, &exceptfds); } // struct timeval tv; tv.tv_sec = timeout/1000; tv.tv_usec = timeout - tv.tv_sec*1000; // nFDS++; select(nFDS, &readfds, &writefds, &exceptfds, &tv); // , epoll int nRetEvents = 0; for (auto it=g_mapSockets.begin(); (it != g_mapSockets.end() && nRetEvents < maxevents); ++it) { if (it->first == -1) continue; if (!FD_ISSET(it->first, &readfds) && !FD_ISSET(it->first, &writefds) && !FD_ISSET(it->first, &exceptfds)) continue; memcpy(&events[nRetEvents].data, &it->second.data, sizeof(epoll_data)); if (FD_ISSET(it->first, &readfds)) events[nRetEvents].events |= EPOLLIN; if (FD_ISSET(it->first, &writefds)) events[nRetEvents].events |= EPOLLOUT; if (FD_ISSET(it->first, &exceptfds)) events[nRetEvents].events |= EPOLLERR; nRetEvents++; } return nRetEvents; }
#ifdef __linux__ #include <sys/epoll.h> #else #include "epoll.h" #endif
private: // struct epoll_event m_ListenEvent; // vector<struct epoll_event> m_events; int m_epoll;
m_epoll = epoll_create (1); if (m_epoll == -1) { printf("error: epoll_create\n"); return; } m_ListenEvent.data.fd = listen_sd; m_ListenEvent.events = EPOLLIN | EPOLLET; epoll_ctl (m_epoll, EPOLL_CTL_ADD, listen_sd, &m_ListenEvent); while(true) { m_events.resize(m_mapClients.size()+1); int n = epoll_wait (m_epoll, &m_events[0], m_events.size(), 5000); if (n == -1) continue; Callback(n); }
void Callback(const int nCount) { for (int i = 0; i < nCount; i++) { SOCKET hSocketIn = m_events[i].data.fd; if (m_ListenEvent.data.fd == (int)hSocketIn) { if (!m_events[i].events == EPOLLIN) continue; struct sockaddr_in sa_cli; size_t client_len = sizeof(sa_cli); #ifdef WIN32 const SOCKET sd = accept (hSocketIn, (struct sockaddr*) &sa_cli, (int *)&client_len); #else const SOCKET sd = accept (hSocketIn, (struct sockaddr*) &sa_cli, (socklen_t *)&client_len); #endif if (sd != INVALID_SOCKET) { // m_mapClients[sd] = shared_ptr<CClient>(new CClient(sd)); auto it = m_mapClients.find(sd); if (it == m_mapClients.end()) continue; // epoll struct epoll_event ev = it->second->GetEvent(); epoll_ctl (m_epoll, EPOLL_CTL_ADD, it->first, &ev); } continue; } auto it = m_mapClients.find(hSocketIn); // if (it == m_mapClients.end()) continue; if (!it->second->Continue()) // - { // false, epoll epoll_ctl (m_epoll, EPOLL_CTL_DEL, it->first, NULL); m_mapClients.erase(it); } } }
private: // struct epoll_event m_ClientEvent; public: const struct epoll_event GetEvent() const {return m_ClientEvent;}
Source: https://habr.com/ru/post/212101/
All Articles