-
Notifications
You must be signed in to change notification settings - Fork 3
/
acceptor.cpp
104 lines (78 loc) · 2.05 KB
/
acceptor.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "acceptor.hpp"
#include <memory>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "sock_stream.hpp"
#include "reactor.hpp"
#include "exception.hpp"
#include "config.hpp"
#include "debug.hpp"
#include "session.hpp"
Acceptor::~Acceptor()
{
::close(socket_);
}
Acceptor::Acceptor(ReactorPtr acceptor_reactor, ReactorPtr session_reactor) :
acceptor_reactor_(acceptor_reactor),
session_reactor_(session_reactor),
timeout_(3)
{
}
void Acceptor::open()
{
if((socket_ = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
throw exception("Can't create socket");
}
int yes;
if(setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
{
throw exception("Can't set socket options");
}
sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = INADDR_ANY;
serveraddr.sin_port = htons(PORT);
memset(&(serveraddr.sin_zero), '\0', 8);
if(bind(socket_, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == -1)
{
throw exception("Can't bind to the socket");
}
if(listen(socket_, SOMAXCONN) == -1)
{
throw exception("Can't listen on the socket");
}
struct timeval tv;
tv.tv_sec = timeout_;
tv.tv_usec = 0;
setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
// acceptor_reactor_->register_handler(this->get_ptr());
acceptor_reactor_->register_handler(shared_from_this());
}
void Acceptor::accept(SockStream& stream)
{
int socket;
struct sockaddr_in clientaddr;
unsigned int addrlen = sizeof(clientaddr);
if((socket = ::accept(socket_, (struct sockaddr *)&clientaddr, &addrlen)) == -1)
{
throw exception("Can't accept connection");
}
stream.set_handle(socket);
}
void Acceptor::close()
{
}
State Acceptor::handle_input()
{
// DEBUG("acceptor handle input");
SessionPtr session(new Session());
accept(session->stream());
session_reactor_->register_handler(session);
return State::RESUME;
// DEBUG("accepted");
}