-
Notifications
You must be signed in to change notification settings - Fork 3
/
acceptor.cc
54 lines (41 loc) · 889 Bytes
/
acceptor.cc
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
#include "acceptor.h"
#include <lace/try.h>
#include <climits>
#include <sys/socket.h>
namespace {
static const int flags = SOCK_NONBLOCK | SOCK_CLOEXEC;
}
simple_acceptor::simple_acceptor(const socket_address & addr)
: simple_task(TRY(socket, addr.protocol(), SOCK_STREAM|flags, 0))
{
file_descriptor::reuseaddr(t.fd());
TRY(bind, t.fd(), &addr.cast(), addr.length());
TRY(listen, t.fd(), INT_MAX);
}
void
simple_acceptor::operator()() {
while (!dead()) {
any_address addr;
socklen_t len = addr.length();
int fd = accept4(t.fd(), &addr.cast(), &len, flags);
if (0 <= fd) {
on_accept(fd, addr);
continue;
}
switch (errno) {
case EAGAIN:
core::wait_for_read(t);
continue;
case EINTR:
continue;
default:
on_error();
continue;
}
}
}
void
simple_acceptor::on_error() {
kill();
}
//