IO多路复用之epoll(二)

前一篇介绍了epoll的LT模式LT模式注意epollout事件在数据全部写成功后需要取消关注,或者更改为EPOLLIN。

而这次epoll的ET模式,要注意的是在读和写的过程中要在循环中写完或者读完所有数据确保不要丢掉一些数据

因为epoll ET模式只在两种边缘更改的时候触发,对于读事件只在内核缓冲区由空变为

非空通知一次用户,对于写事件,内核缓冲区只在由满变为非满的情况通知用户一次。

下面是代码

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
int main()
{

int eventsize = 20;
struct epoll_event * epoll_eventsList = (struct epoll_event *)malloc(sizeof(struct epoll_event)

*eventsize);

//打开一个空的描述符
int idlefd = open("/dev/null",O_RDONLY|O_CLOEXEC);
cout << "idlefd" <<idlefd <<endl;
//生成listen描述符

int listenfd = socket(PF_INET, SOCK_CLOEXEC | SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
if(listenfd < 0)
{
ERR_EXIT("socketfd");
}

//初始化地址信息
struct sockaddr_in servaddr;
memset(&servaddr,0 ,sizeof(struct sockaddr_in));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6667);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);


int on = 1;
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
ERR_EXIT("setsockopt");

if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)))
ERR_EXIT("bindError");

if (listen(listenfd, SOMAXCONN) < 0)
ERR_EXIT("listen");

//记录客户端连接对应的socket
std::vector<int> clients;
//创建epollfd, 用于管理epoll事件表
int epollfd;
epollfd = epoll_create1(EPOLL_CLOEXEC);

struct epoll_event event;
event.data.fd = listenfd;
event.events = EPOLLIN|EPOLLET;
//将listenfd加入到epollfd管理的表里
epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &event);

//用于接收新连接的客户端地址
struct sockaddr_in peeraddr;
socklen_t peerlen;
int connfd;
//为了简单起见写了个很大的数组,根据文件描述符存储内容
//其实很多项目代码采epoll.data.ptr回调具体的读写

vector<string> recievebuf;

for(int i = 0 ; i < 22222; i++)
{
recievebuf.push_back("");
}

while(1)
{
int nready = epoll_wait(epollfd, epoll_eventsList, eventsize, -1);
if (nready == -1)
{
if (errno == EINTR)
continue;

ERR_EXIT("epoll_wait");
}
if (nready == 0)
continue;

//大小不够重新开辟

if ((size_t)nready == eventsize)
{
if(eventsize * 2 >= 22222)
{
ERR_EXIT("too many fds");
}

struct epoll_event * epoll_eventsList2 = (struct epoll_event *)malloc(sizeof(struct epoll_event) *
eventsize *2);
if(epoll_eventsList2)
{
memcpy(epoll_eventsList2,epoll_eventsList,sizeof(struct epoll_event) * eventsize);
eventsize = eventsize * 2;
free(epoll_eventsList);
epoll_eventsList = epoll_eventsList2;

}


}


for (int i = 0; i < nready; ++i)
{
//判断wait返回的events数组状态是否正常
if ((epoll_eventsList[i].events & EPOLLERR) ||
(epoll_eventsList[i].events & EPOLLHUP))
{
fprintf (stderr, "epoll error\n");
close (epoll_eventsList[i].data.fd);
continue;

}

if (epoll_eventsList[i].data.fd == listenfd)
{
peerlen = sizeof(peeraddr);
//ET模式accept放在while循环里

do
{
connfd = ::accept4(listenfd, (struct sockaddr*)&peeraddr,
&peerlen, SOCK_NONBLOCK | SOCK_CLOEXEC);

if(connfd <= 0)
break;

std::cout<<"ip="<<inet_ntoa(peeraddr.sin_addr)<<
" port="<<ntohs(peeraddr.sin_port)<<std::endl;
clients.push_back(connfd);

//将connd加入epoll表里,关注读事件

event.data.fd = connfd;
event.events = EPOLLIN |EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_ADD, connfd, &event);
cout << "loop" <<endl;
cout << "loop" << connfd << endl;
}while(1);
//accept失败,判断是否接收全所有的fd
cout << connfd << endl;
if (connfd == -1){
if (errno != EAGAIN && errno != ECONNABORTED
&& errno != EPROTO && errno != EINTR)
{
cout << "error" <<endl;
ERR_EXIT("accept");
}

}

//所有请求都处理完成
cout << "continue"<<endl;
continue;

}//endif
else if(epoll_eventsList[i].events & EPOLLIN)
{
connfd = epoll_eventsList[i].data.fd;
if(connfd > 22222)
{
close(connfd);
event = epoll_eventsList[i];
epoll_ctl(epollfd, EPOLL_CTL_DEL, connfd, &event);
clients.erase(std::remove(clients.begin(), clients.end(), connfd), clients.end());
continue;

}

char buf[1024] = {0};
if(connfd < 0)
continue;
int ret = 0;
int total = 0;
std::string strtemp;
while(1)
{
cout << "begin read" <<endl;
ret = read(connfd, buf, 1024);
if(ret <= 0)
{
break;
}

strtemp += string(buf);
total += ret;
memset(buf, 0, 1024);

if(ret < 1024)
{
break;
}

}//endwhile(1)

cout << "end read" <<endl;
recievebuf[connfd] = strtemp.c_str();
cout << "buff data :" << recievebuf[connfd]<<endl;
if(ret == -1)
{
if((errno == EAGAIN) ||
(errno == EWOULDBLOCK))
{
//由于内核缓冲区空了,下次有数据到来是会触发epollin
continue;
}


ERR_EXIT("read");

}//endif ret == -1

//连接断开
if(ret == 0)
{
std::cout<<"client close"<<std::endl;
close(connfd);
event = epoll_eventsList[i];
epoll_ctl(epollfd, EPOLL_CTL_DEL, connfd, &event);
clients.erase(std::remove(clients.begin(), clients.end(), connfd), clients.end());
continue;

}

cout << "turn to write" << endl;
//更改为写模式

event.data.fd = connfd;
event.events = EPOLLOUT | EPOLLET;

epoll_ctl(epollfd, EPOLL_CTL_MOD, connfd, &event);
cout << "epoll mod change success" << endl;

}//end elif

else //写事件
{

if(epoll_eventsList[i].events & EPOLLOUT)
{
cout << "begin write" <<endl;
connfd = epoll_eventsList[i].data.fd;
int count = 0;
int totalsend = 0;
char buf[1024];
strcpy(buf, recievebuf[connfd].c_str());

cout << "write buff" <<buf<<endl;
while(1)
{
int totalcount = strlen(buf);
int pos = 0;
count = write(epoll_eventsList[i].data.fd, buf + pos, totalcount);
cout << "write count:" << count;
if(count < 0)
{
break;
}

if(count < totalcount)
{
totalcount = totalcount - count;
pos += count;

}
else
{
break;

}

}//end while


if(count == -1)
{
if((errno == EAGAIN) ||
(errno == EWOULDBLOCK))
{
//由于内核缓冲区满了
//于内核缓冲区满了
continue;
}

ERR_EXIT("write");
}

if(count == 0)
{
std::cout<<"client close"<<std::endl;
close(connfd);
event = epoll_eventsList[i];
epoll_ctl(epollfd, EPOLL_CTL_DEL, connfd, &event);
clients.erase(std::remove(clients.begin(), clients.end(), connfd),
clients.end());
continue;

}

event.data.fd = connfd;
event.events = EPOLLIN|EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_MOD, connfd, &event);

}

}//end eles 写事件


}

}
}

源代码下载地址:http://download.csdn.net/detail/secondtonone1/9486222
我的微信公众号:
https://cdn.llfc.club/gzh.jpg