-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
95 lines (81 loc) · 2.15 KB
/
main.c
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
#include "log.h"
#include "epoll.h"
#include "node.h"
#include "client.h"
#include "tcpServer.h"
#include "udpServer.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#define EPOLL_EVENTS 1
void usage() {
printf("Usage:\n");
printf(" ./mnh_server <port>\n");
}
int main(int argc, char const *argv[])
{
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
if(argc != 2) {
usage();
return 1;
}
int port = atoi(argv[1]);
int epoll_fd = epoll_create(100);
if(epoll_fd < 0) {
log_e("epoll_create error %d: %s", errno, strerror(errno));
return 1;
}
int tcpServer_fd = tcpServer_create(port);
if(tcpServer_fd < 0) {
log_e("tcpServer_create error %d: %s", errno, strerror(errno));
return 1;
}
if(epoll_add(epoll_fd, tcpServer_fd, &tcpServer_fd) < 0) {
log_e("add tcp server to epoll error %d: %s", errno, strerror(errno));
return 1;
}
int udpServer_fd = udpServer_create(port);
if(udpServer_fd < 0) {
log_e("udpServer_create error %d: %s", errno, strerror(errno));
return 1;
}
if(epoll_add(epoll_fd, udpServer_fd, &udpServer_fd) < 0) {
log_e("add udp server to epoll error %d: %s", errno, strerror(errno));
return 1;
}
log_i("running");
while(true) {
struct epoll_event evs[EPOLL_EVENTS];
int ready = epoll_wait(epoll_fd, evs, EPOLL_EVENTS, (1000 * client_getMinAfter()));
if(ready < 0) {
if(errno = EINTR) {
log_e("epoll interrupted");
continue;
} else {
log_e("epoll error %d: %s", errno, strerror(errno));
break;
}
}
for(int i = 0; i < ready; i++) {
if(evs[i].data.ptr == &tcpServer_fd)
tcpServer_handle(epoll_fd, tcpServer_fd);
else if(evs[i].data.ptr == &udpServer_fd)
udpServer_handle(epoll_fd, udpServer_fd);
else {
NODE *node = evs[i].data.ptr;
if(evs[i].events & EPOLLERR) {
log_e("ERROR %d(%s)", node->sock, node->id);
client_close(epoll_fd, node);
} else if(evs[i].events & EPOLLRDHUP) {
log_i("close %d(%s)", node->sock, node->id);
client_close(epoll_fd, node);
} else if(evs[i].events & EPOLLIN)
client_handleTCP(epoll_fd, node);
}
}
client_cleanTimeouts(epoll_fd);
}
return 0;
}