/
p3.c
217 lines (169 loc) · 4.17 KB
/
p3.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
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
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <pthread.h>
#include "common.h"
pid_t tid() {
return (pid_t)syscall(__NR_gettid) - getpid();
}
struct fdinfo {
enum {
LSOCK,
CLIENT
} type;
int fd;
struct sockaddr_in sin;
};
static void print_event(int fd, struct epoll_event ev) {
char flags_str[200];
flags_str[0] = 0;
flags_str[1] = 0; /* dirty */
if (ev.events & EPOLLIN ) strcat(flags_str, "|EPOLLIN");
if (ev.events & EPOLLOUT) strcat(flags_str, "|EPOLLOUT");
if (ev.events & EPOLLERR) strcat(flags_str, "|EPOLLERR");
if (ev.events & EPOLLHUP) strcat(flags_str, "|EPOLLHUP");
if (ev.events & EPOLLRDHUP) strcat(flags_str, "|EPOLLRDHUP");
if (ev.events & EPOLLPRI) strcat(flags_str, "|EPOLLPRI");
if (ev.events & EPOLLET) strcat(flags_str, "|EPOLLET");
if (ev.events & EPOLLONESHOT) strcat(flags_str, "|EPOLLONESHOT");
fprintf(stderr, "Event for fd %i. Flags=(%s).\n",
fd, flags_str+1);
}
int start_listener(int port);
void service(int epfd);
void *service_wrap(void *arg) {
int epfd = *(int*)arg;
service(epfd);
return 0;
}
int main () {
int epfd = epoll_create(1);
int lsock = start_listener(9000);
struct epoll_event ev;
struct fdinfo *fdinfo;
int ret;
int nthr = sysconf(_SC_NPROCESSORS_ONLN);
long i;
pthread_t *ths;
ths = malloc(nthr * sizeof ths[1]);
fdinfo = malloc(sizeof (struct fdinfo));
fdinfo->type = LSOCK;
fdinfo->fd = lsock;
ev.data.ptr = fdinfo;
ev.events = EPOLLIN;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, lsock, &ev);
if (ret < 0)
die("epoll_ctl.1");
for (i = 0; i<nthr; i++)
pthread_create(&ths[i], NULL, service_wrap, &epfd);
for (i = 0; i<nthr; i++)
pthread_join(ths[i], NULL);
close(epfd);
return 0;
}
int start_listener(int port) {
struct sockaddr_in listen_addr;
int lsock;
int ret;
lsock = socket(AF_INET, SOCK_STREAM, 0);
if (lsock < 0)
die("socket");
memset(&listen_addr, 0, sizeof listen_addr);
listen_addr.sin_family = AF_INET;
listen_addr.sin_port = htons(port);
listen_addr.sin_addr.s_addr = INADDR_ANY;
ret = bind(lsock, (sad*)&listen_addr, sizeof listen_addr);
if (ret < 0)
die("bind");
ret = listen(lsock, 10);
if (ret < 0)
die("listen");
return lsock;
}
void accept_one(int epfd, int lsock) {
int cli;
struct fdinfo *fdinfo;
struct epoll_event ev;
socklen_t crap = sizeof fdinfo->sin;
int ret;
fdinfo = malloc(sizeof *fdinfo);
cli = accept4(lsock, (sad*)&fdinfo->sin, &crap, SOCK_NONBLOCK);
if (cli < 0)
die("accept");
fdinfo->fd = cli;
fdinfo->type = CLIENT;
ev.data.ptr = fdinfo;
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLET;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, cli, &ev);
if (ret < 0)
die("epoll_ctl.2");
}
void handle_cli(struct fdinfo *fdinfo) {
int fd = fdinfo->fd;
int t;
char buf1[100], buf2[100];
inet_ntop(AF_INET, &fdinfo->sin.sin_addr, buf1, sizeof fdinfo->sin);
/*
* Leemos y hacemos echo de todo lo que haya,
* hasta que la lectura falle (EAGAIN)
*/
do {
t = read(fd, buf2, 100);
buf2[t] = 0;
printf("%i: %s mandó %s\n", tid(), buf1, buf2);
write(fd, buf2, t);
} while (t > 0);
}
void kill_cli(int epfd, struct fdinfo *fdinfo) {
char buf[100];
int fd = fdinfo->fd;
free(fdinfo);
inet_ntop(AF_INET, &fdinfo->sin.sin_addr, buf, sizeof fdinfo->sin);
printf("%i: Chau %s!\n", tid(), buf);
epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
close(fd);
}
void handle_event(int epfd, struct epoll_event ev) {
struct fdinfo *fdinfo = ev.data.ptr;
switch (fdinfo->type) {
case LSOCK:
accept_one(epfd, fdinfo->fd);
break;
case CLIENT:
print_event(fdinfo->fd, ev);
if (ev.events & EPOLLHUP) {
kill_cli(epfd, fdinfo);
return;
}
if (ev.events & EPOLLIN) {
handle_cli(fdinfo);
}
if (ev.events & EPOLLRDHUP) {
handle_cli(fdinfo);
kill_cli(epfd, fdinfo);
return;
}
break;
}
}
void service(int epfd) {
int i, n;
struct epoll_event evs[10];
for (;;) {
n = epoll_wait(epfd, evs, 10, 5000);
if (n < 0)
die("epoll_wait");
if (n == 0) {
printf("%i: Que aburrimiento che....\n", tid());
continue;
}
for (i=0; i<n; i++)
handle_event(epfd, evs[i]);
}
}