-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathserver1.cpp
123 lines (109 loc) · 2.95 KB
/
server1.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <mutex>
#include <assert.h>
#include <vector>
#include <memory>
#include <queue>
#include <stack>
#include <thread>
#include "io_pool.h"
namespace {
class session : public std::enable_shared_from_this<session>, noncopyable
{
public:
session(asio::io_service& ios, size_t block_size)
: io_service_(ios)
, socket_(ios)
, block_size_(block_size)
, buffer_(new char[block_size]) {
}
~session() {
delete[] buffer_;
}
asio::ip::tcp::socket& socket() {
return socket_;
}
void start() {
asio::ip::tcp::no_delay no_delay(true);
socket_.set_option(no_delay);
read();
}
void write() {
asio::async_write(socket_, asio::buffer(buffer_, block_size_),
[this, self = shared_from_this()](
const asio::error_code& err, size_t cb) {
if (!err) {
assert(cb == block_size_);
read();
}
});
}
void read() {
asio::async_read(socket_, asio::buffer(buffer_, block_size_),
[this, self = shared_from_this()](
const asio::error_code& err, size_t cb) {
if (!err) {
assert(cb == block_size_);
write();
}
});
}
private:
asio::io_service& io_service_;
asio::ip::tcp::socket socket_;
size_t const block_size_;
char* buffer_;
};
class server : noncopyable
{
public:
server(int thread_count, const asio::ip::tcp::endpoint& endpoint,
size_t block_size)
: thread_count_(thread_count)
, block_size_(block_size)
, service_pool_(thread_count)
, acceptor_(service_pool_.get_io_service())
{
acceptor_.open(endpoint.protocol());
acceptor_.set_option(asio::ip::tcp::acceptor::reuse_address(1));
acceptor_.bind(endpoint);
acceptor_.listen();
}
void start() {
accept();
}
void wait() {
service_pool_.run();
}
private:
void accept() {
std::shared_ptr<session> new_session(new session(
service_pool_.get_io_service(), block_size_));
auto& socket = new_session->socket();
acceptor_.async_accept(socket,
[this, new_session = std::move(new_session)](
const asio::error_code& err) {
if (!err) {
new_session->start();
accept();
}
});
}
private:
int const thread_count_;
size_t const block_size_;
io_service_pool service_pool_;
asio::ip::tcp::acceptor acceptor_;
};
} // namespace
void server_test1(int thread_count, char const* host, char const* port,
size_t block_size) {
auto endpoint = asio::ip::tcp::endpoint(
asio::ip::address::from_string(host), atoi(port));
server s(thread_count, endpoint, block_size);
s.start();
s.wait();
}