forked from anqin/trident
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_listener.h
More file actions
236 lines (196 loc) · 6.44 KB
/
rpc_listener.h
File metadata and controls
236 lines (196 loc) · 6.44 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) 2014 The Trident Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
//
#ifndef _TRIDENT_RPC_LISTENER_H_
#define _TRIDENT_RPC_LISTENER_H_
#include <trident/common_internal.h>
#include <trident/rpc_endpoint.h>
#include <trident/rpc_error_code.h>
#include <trident/locks.h>
namespace trident {
using boost::asio::ip::tcp;
class RpcListener : public trident::enable_shared_from_this<RpcListener>
{
// Callback function when created or accepted a new connection.
typedef boost::function<void(const RpcServerStreamPtr& /* stream */)> Callback;
typedef boost::function<void(RpcErrorCode /* error_code */,
const std::string& /* error_text */)> FailCallback;
public:
static const int LISTEN_MAX_CONNECTIONS = 4096;
public:
RpcListener(IOService& io_service, const RpcEndpoint& endpoint)
: _io_service(io_service)
, _endpoint(endpoint)
, _endpoint_str(RpcEndpointToString(endpoint))
, _acceptor(io_service)
, _is_closed(false)
{
TRIDENT_INC_RESOURCE_COUNTER(RpcListener);
}
virtual ~RpcListener()
{
TRIDENT_FUNCTION_TRACE;
close();
TRIDENT_DEC_RESOURCE_COUNTER(RpcListener);
}
void close()
{
ScopedLocker<MutexLock> _(_close_lock);
if (_is_closed) return;
_is_closed = true;
boost::system::error_code ec;
_acceptor.cancel(ec);
_acceptor.close(ec);
#if 0
LOG(INFO) << "close(): listener closed: " << _endpoint_str;
#else
SLOG(INFO, "close(): listener closed: %s", _endpoint_str.c_str());
#endif
}
bool is_closed()
{
ScopedLocker<MutexLock> _(_close_lock);
return _is_closed;
}
// Set the callback funtion when created a new connection.
template <typename T>
void set_create_callback(const T& create_callback)
{
_create_callback = create_callback;
}
// Set the callback funtion when accepted a new connection.
template <typename T>
void set_accept_callback(const T& accept_callback)
{
_accept_callback = accept_callback;
}
// Set the callback funtion when failed to accept connection.
template <typename T>
void set_accept_fail_callback(const T& accept_fail_callback)
{
_accept_fail_callback = accept_fail_callback;
}
// Start listen. Return false if failed.
bool start_listen()
{
boost::system::error_code ec;
_acceptor.open(_endpoint.protocol(), ec);
if (ec)
{
#if 0
LOG(ERROR) << "start_listen(): open acceptor failed: "
<< _endpoint_str << ": " << ec.message();
#else
SLOG(ERROR, "start_listen(): open acceptor failed: %s: %s",
_endpoint_str.c_str(), ec.message().c_str());
#endif
return false;
}
_acceptor.set_option(tcp::acceptor::reuse_address(true), ec);
if (ec)
{
#if 0
LOG(ERROR) << "start_listen(): set acceptor option failed: "
<< _endpoint_str << ": " << ec.message();
#else
SLOG(ERROR, "start_listen(): set acceptor option failed: %s: %s",
_endpoint_str.c_str(), ec.message().c_str());
#endif
return false;
}
_acceptor.bind(_endpoint, ec);
if (ec)
{
#if 0
LOG(ERROR) << "start_listen(): bind acceptor failed: "
<< _endpoint_str << ": " << ec.message();
#else
SLOG(ERROR, "start_listen(): bind acceptor failed: %s: %s",
_endpoint_str.c_str(), ec.message().c_str());
#endif
return false;
}
//_acceptor.listen(boost::asio::socket_base::max_connections, ec);
_acceptor.listen(LISTEN_MAX_CONNECTIONS, ec);
if (ec)
{
#if 0
LOG(ERROR) << "start_listen(): listen acceptor failed: "
<< _endpoint_str << ": " << ec.message();
#else
SLOG(ERROR, "start_listen(): listen acceptor failed: %s: %s",
_endpoint_str.c_str(), ec.message().c_str());
#endif
return false;
}
#if 0
LOG(INFO) << "start_listen(): listen succeed: " << _endpoint_str;
#else
SLOG(INFO, "start_listen(): listen succeed: %s", _endpoint_str.c_str());
#endif
async_accept();
return true;
}
private:
void async_accept()
{
RpcServerStreamPtr stream(new RpcServerStream(_io_service));
if (_create_callback)
_create_callback(stream);
_acceptor.async_accept(stream->socket(), boost::bind(
&RpcListener::on_accept, shared_from_this(), stream, _1));
}
void on_accept(const RpcServerStreamPtr& stream,
const boost::system::error_code& ec)
{
if (_is_closed)
return;
if (ec)
{
#if 0
LOG(ERROR) << "on_accept(): accept error at "
<< _endpoint_str << ": " << ec.message();
#else
SLOG(ERROR, "on_accept(): accept error at %s: %s",
_endpoint_str.c_str(), ec.message().c_str());
#endif
close();
if (_accept_fail_callback)
{
RpcErrorCode error_code = ec == boost::asio::error::no_descriptors ?
RPC_ERROR_TOO_MANY_OPEN_FILES : RPC_ERROR_UNKNOWN;
_accept_fail_callback(error_code, ec.message());
}
}
else
{
if (_accept_callback)
_accept_callback(stream);
stream->set_socket_connected();
#if 0
LOG(INFO) << "on_accept(): accept connection at "
<< _endpoint_str << ": " << RpcEndpointToString(stream->remote_endpoint());
#else
SLOG(INFO, "on_accept(): accept connection at %s: %s",
_endpoint_str.c_str(), RpcEndpointToString(stream->remote_endpoint()).c_str());
#endif
async_accept();
}
}
private:
IOService& _io_service;
RpcEndpoint _endpoint;
std::string _endpoint_str;
Callback _create_callback;
Callback _accept_callback;
FailCallback _accept_fail_callback;
tcp::acceptor _acceptor;
volatile bool _is_closed;
MutexLock _close_lock;
TRIDENT_DISALLOW_EVIL_CONSTRUCTORS(RpcListener);
}; // class RpcListener
} // namespace trident
#endif // _TRIDENT_RPC_LISTENER_H_
/* vim: set ts=4 sw=4 sts=4 tw=100 */