This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
server.hpp
85 lines (65 loc) · 2.18 KB
/
server.hpp
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
#ifndef __DARNER_SERVER_HPP__
#define __DARNER_SERVER_HPP__
#include <boost/scoped_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include "darner/util/log.h"
#include "darner/util/stats.hpp"
#include "darner/util/queue_map.hpp"
#include "darner/net/request.h"
#include "darner/net/handler.h"
namespace darner {
/*
* server loads up the queues, handles accepts and spawns off clients for each new accept
*/
class server
{
public:
server(const std::string& data_path,
unsigned short listen_port)
: listen_port_(listen_port),
acceptor_(ios_),
queues_(ios_, data_path)
{
// open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), listen_port_);
acceptor_.open(endpoint.protocol());
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen();
// get our first conn ready
handler_ = handler::ptr_type(new handler(ios_, parser_, queues_, stats_));
// pump the first async accept into the loop
acceptor_.async_accept(handler_->socket(),
boost::bind(&server::handle_accept, this, boost::asio::placeholders::error));
// start up the event loop for the service
runner_ = boost::thread(boost::bind(&boost::asio::io_service::run, &ios_));
}
void stop()
{
ios_.stop();
// let the thread exit
runner_.join();
}
private:
void handle_accept(const boost::system::error_code& e)
{
if (e)
return log::ERROR("server::handle_accept: %1%", e.message());
handler_->start();
handler_ = handler::ptr_type(new handler(ios_, parser_, queues_, stats_));
acceptor_.async_accept(handler_->socket(),
boost::bind(&server::handle_accept, this, boost::asio::placeholders::error));
}
unsigned short listen_port_;
boost::asio::io_service ios_;
boost::asio::ip::tcp::acceptor acceptor_;
queue_map queues_;
request_parser parser_;
stats stats_;
handler::ptr_type handler_;
boost::thread runner_;
};
} // darner
#endif // __DARNER_SERVER_HPP__