-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoll.c
235 lines (208 loc) · 6.35 KB
/
epoll.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "epoll.h"
#define MAXBUFFSIZE 2048
void newConnectionTasks(int socketFD) {
/* if you want to tell the newly connected socket anything
this is the spot */
}
void startServer(int port,int (*fnPtr)(int, char*, int)) {
struct epoll_event *events;
int socketFD;
int epollFD;
socketFD = validateSocket(port);
bindandListenSocket(socketFD);
epollFD = createEPoll();
setEPollSocket(epollFD, socketFD, &events);
eventLoop(socketFD, epollFD, events, fnPtr);
close(socketFD);
}
int readDataFromSocket(int socketFD, int (*fnPtr)(int, char*, int)) {
ssize_t length = 0;
char *buf;
buf = malloc(sizeof(char) * MAXBUFFSIZE);
if (buf == NULL) {
perror("MALLOC");
abort();
}
while (1) {
ssize_t count;
count = read (socketFD, &buf[length], MAXBUFFSIZE - length);
length += count;
if (length == MAXBUFFSIZE) {
printf("hope this never is reached");
//some error condition here
}
switch (count) {
case -1:
if (errno != EAGAIN) {
perror ("read");
return 1;
}
if (length != 0) {
return fnPtr(socketFD, buf, length);
}
//not sure what to do here so falling throught
case 0:
return 1;
default:
continue;
}
}
return 0;
}
void makeNonBlockingSocket (int socketDescriptor) {
int flags, result;
flags = fcntl (socketDescriptor, F_GETFL, 0);
if (flags == -1) {
perror ("fcntl");
abort ();
}
flags |= O_NONBLOCK;
result = fcntl (socketDescriptor, F_SETFL, flags);
if (result == -1) {
perror ("fcntl");
abort ();
}
return;
}
int getAddressResult(int port, struct addrinfo **result) {
struct addrinfo hints;
int returnValue;
char sPort[6];
memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
sprintf(sPort, "%d",port);
returnValue = getaddrinfo (NULL, sPort, &hints, result);
if (returnValue != 0) {
fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (returnValue));
return -1;
}
return 0;
}
int createAndBind(int port) {
struct addrinfo *result, *rp;
int socketFD;
if (getAddressResult(port, &result) == -1) { return -1; }
for (rp = result; rp != NULL; rp = rp->ai_next) {
socketFD = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (socketFD == -1) {
continue;
}
if (bind (socketFD, rp->ai_addr, rp->ai_addrlen) == 0) {
/* We managed to bind successfully! */
break;
}
close (socketFD);
}
if (rp == NULL) {
fprintf (stderr, "Could not bind\n");
return -1;
}
freeaddrinfo (result);
return socketFD;
}
void processIncomingNewSocket(int socketFD, int epollFD) {
while (1) {
struct epoll_event event;
struct sockaddr in_addr;
socklen_t in_len;
int infd, epollSuccess, connectResult;
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
in_len = sizeof(in_addr);
infd = accept (socketFD, &in_addr, &in_len);
if (infd == -1) {
if ((errno == EAGAIN) ||
(errno == EWOULDBLOCK)) {
/* We have processed all incoming
connections. */
break;
} else {
perror ("accept");
break;
}
}
connectResult = getnameinfo (&in_addr, in_len,
hbuf, sizeof hbuf,
sbuf, sizeof sbuf,
NI_NUMERICHOST | NI_NUMERICSERV);
if (connectResult == 0) {
printf("Accepted connection on descriptor %d "
"(host=%s, port=%s)\n", infd, hbuf, sbuf);
newConnectionTasks(infd);
}
makeNonBlockingSocket (infd);
event.data.fd = infd;
event.events = EPOLLIN | EPOLLET;
epollSuccess = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
if (epollSuccess == -1) {
perror ("epoll_ctl");
abort ();
}
}
}
void eventLoop(int socketFD, int epollFD, struct epoll_event *events, int (*fnPtr)(int, char*, int)) {
while (TRUE) {
int n, i;
n = epoll_wait (epollFD, events, MAXEVENTS, -1);
for (i = 0; i < n; i++) {
if ((events[i].events & EPOLLERR) ||
(events[i].events & EPOLLHUP) ||
(!(events[i].events & EPOLLIN))) {
/* An error has occured on this fd, or the socketFD is not
ready for reading (why were we notified then?) */
fprintf (stderr, "epoll error\n");
close (events[i].data.fd);
continue;
} else if (socketFD == events[i].data.fd) {
processIncomingNewSocket(socketFD, epollFD);
continue;
} else {
//Read all data from socket and do something with it
if (readDataFromSocket(events[i].data.fd, fnPtr)) {
printf ("Closed connection on descriptor %d\n",
events[i].data.fd);
close (events[i].data.fd);
}
}
}
}
free (events);
}
int validateSocket(int port) {
int socketFD = createAndBind (port);
if (socketFD == -1) {
abort ();
}
return socketFD;
}
void bindandListenSocket(int socketFD) {
int result;
makeNonBlockingSocket (socketFD);
result = listen (socketFD, SOMAXCONN);
if (result == -1) {
perror ("listen");
abort ();
}
}
int createEPoll() {
int epollFD = epoll_create1 (0);
if (epollFD == -1) {
perror ("epoll_create");
abort ();
}
return epollFD;
}
void setEPollSocket(int epollFD, int socketFD, struct epoll_event **pevents) {
struct epoll_event event;
int result;
event.data.fd = socketFD;
event.events = EPOLLIN | EPOLLET;
result = epoll_ctl (epollFD, EPOLL_CTL_ADD, socketFD, &event);
if (result == -1) {
perror ("epoll_ctl");
abort ();
}
/* Buffer where events are returned */
*pevents = calloc (MAXEVENTS, sizeof event);
}