This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
test-otpc.cpp
91 lines (81 loc) · 2.36 KB
/
test-otpc.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
/**
* @file test-ocmt.cpp
* @author Alex Ott <alexott@gmail.com>
*
* @brief Implementation of 'stupid' www server with implementation of thread per
* connection strategy. Working threads use sync mode for input/output, but server thread
* work in async mode.
*
*
*/
#include "test-otpc-conn.hpp"
#include <set>
/**
* Server class
*
*/
class server : private boost::noncopyable {
public:
server(ba::io_service& io_service, int port=10001);
private:
void handle_accept(const boost::system::error_code& e);
ba::io_service& io_service_; /**< reference to io_service */
ba::ip::tcp::acceptor acceptor_; /**< object, that accepts new connections */
connection::pointer new_connection_; /**< pointer to connection, that will proceed next */
};
/**
* Initialize all needed data
*
* @param io_service reference to io_service
* @param port port to listen on, by default - 10001
*/
server::server(ba::io_service& io_service,int port)
: io_service_(io_service),
acceptor_(io_service_, ba::ip::tcp::endpoint(ba::ip::tcp::v4(), port)),
new_connection_(connection::create(io_service_)) {
// start acceptor in async mode
acceptor_.async_accept(new_connection_->socket(),
boost::bind(&server::handle_accept, this,
ba::placeholders::error));
}
/**
* Run when new connection is accepted
*
* @param e reference to error object
*/
void server::handle_accept(const boost::system::error_code& e) {
if (!e) {
// run connection in new thread
boost::thread t(boost::bind(&connection::run, new_connection_));
// create next connection, that will accepted
new_connection_=connection::create(io_service_);
// start new accept operation
acceptor_.async_accept(new_connection_->socket(),
boost::bind(&server::handle_accept, this,
ba::placeholders::error));
}
}
/**
* Main routine
*
* @param argc number of arguments
* @param argv pointers to arguments
*
* @return error code
*/
int main(int argc, char** argv) {
try {
int port=10001;
// read port number from command line, if provided
if(argc > 1)
port=boost::lexical_cast<int>(argv[1]);
ba::io_service io_service;
// construct new server object
server s(io_service,port);
// run io_service object, that perform all dispatch operations
io_service.run();
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}