-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho_poll_server.cpp
84 lines (70 loc) · 2.17 KB
/
echo_poll_server.cpp
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
#include <sys/poll.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
using namespace std;
#include "tcp_listener.h"
const int MAX_EVENTS = 128;
int main() {
TcpListener listener;
listener.listen(8888, true);
vector<struct pollfd> events(MAX_EVENTS);
events[0].fd = listener.listen_fd();
events[0].events = POLLRDNORM;
// init empty event with fd{-1}
int i;
for (i = 1; i < MAX_EVENTS; i++) {
events[i].fd = -1;
}
int n_ready;
while (true) {
if ((n_ready = poll(events.data(), MAX_EVENTS, -1)) < 0) {
cerr << "poll error: " << strerror(errno) << endl;
exit(1);
}
// check listen_fd first
if (events[0].revents & POLLRDNORM) {
int conn_fd = listener.accept().conn_fd();
for (i = 1; i < MAX_EVENTS; i++) {
if (events[i].fd < 0) {
events[i].fd = conn_fd;
events[i].events = POLLRDNORM;
break;
}
}
if (i == MAX_EVENTS) {
cerr << "can not hold so many clients\n";
}
if (--n_ready <= 0) {
continue;
}
}
for (i = 1; i < MAX_EVENTS; i++) {
int conn_fd;
if ((conn_fd = events[i].fd) < 0) {
continue;
}
if (events[i].revents & POLLHUP) {
cerr << "poll error" << endl;
close(events[i].fd);
events[i].fd = -1;
} else if (events[i].revents & (POLLRDNORM | POLLERR)) {
TcpConnection conn{conn_fd};
string msg;
if (!(msg = conn.blocking_receive_line()).empty()) {
cout << "server received: " << msg;
conn.blocking_send(msg);
} else {
conn.close();
cout << "server closed conn_fd: " << conn_fd << endl;
events[i].fd = -1;
}
if (--n_ready <= 0) {
break;
}
}
}
}
}