-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathepollengineer.cpp
More file actions
132 lines (119 loc) · 2.89 KB
/
epollengineer.cpp
File metadata and controls
132 lines (119 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "epollengineer.h"
#include <fcntl.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/un.h>
std::atomic_bool Epoll::sigHandle(false);
std::atomic_bool Epoll::epollRunning(true);
Epoll::Epoll()
{
epollFileDescriptor = epoll_create1(0);
epollTimeout = 3000;
countServers = 0;
}
void Epoll::addServer()
{
countServers++;
if (countServers == 1)
{
serverThread = std::thread(&Epoll::execute, this);
}
}
void Epoll::removeServer()
{
countServers--;
if (countServers == 0)
{
serverThread.join();
}
}
void Epoll::addFileDescriptor(int fd, std::function<void(epoll_event)> callFunc)
{
event.data.fd = fd;
event.events = DEFAULT_EVENTS;
if (epoll_ctl(epollFileDescriptor, EPOLL_CTL_ADD, fd, &event) != -1)
{
fdFunc.insert(make_pair(fd, callFunc));
}
else
{
throw SuperTcpManager::SuperEpollException();
}
}
void Epoll::closeSocket(int sfd)
{
SuperTcpManager::printMyDebug("close", sfd);
if (::close(sfd) == -1)
{
throw SuperTcpManager::SuperSocketCloseException();
}
}
void Epoll::removeFileDescriptor(int fd)
{
closeSocket(fd);
fdFunc.erase(fd);
}
void Epoll::setFdOpt(int fd, unsigned int opt)
{
event.data.fd = fd;
event.events = opt;
if (epoll_ctl(epollFileDescriptor, EPOLL_CTL_MOD, fd, &event) == -1)
{
throw SuperTcpManager::SuperInvalidArgumentException();
}
}
void Epoll::writeMsg(int fd, const std::string& msg)
{
::write(fd, msg.c_str(), msg.size());
}
void Epoll::execute()
{
SuperTcpManager::printMyDebug("start epoll");
std::vector<epoll_event> events(MAXEVENTS);
while (countServers > 0)
{
int nfds = epoll_wait (epollFileDescriptor, events.data(), (int)events.size(), epollTimeout);
if (sigHandle)
{
for (auto it : fdFunc)
{
closeSocket(it.first);
}
fdFunc.clear();
countServers = 0;
epollRunning = false;
break;
}
if (nfds == 0)
{
continue;
}
if (nfds == -1)
{
SuperTcpManager::printMyDebug("epoll error");
throw SuperTcpManager::SuperTcpManagerException();
// break;
}
SuperTcpManager::printMyDebug("get epoll events count =", nfds);
for (int i = 0; i < nfds; i++)
{
SuperTcpManager::printMyDebug("event on ", events[i].data.fd);
fdFunc[events[i].data.fd](events[i]);
}
}
SuperTcpManager::printMyDebug("end epoll");
}
Epoll::~Epoll()
{
SuperTcpManager::printMyDebug("Destructor epoll start");
if (serverThread.joinable())
{
serverThread.join();
}
epollRunning = false;
SuperTcpManager::printMyDebug("Destructor epoll end");
}