-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrpc_byte_stream.h
More file actions
315 lines (266 loc) · 8.31 KB
/
Copy pathrpc_byte_stream.h
File metadata and controls
315 lines (266 loc) · 8.31 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// 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_BYTE_STREAM_H_
#define _TRIDENT_RPC_BYTE_STREAM_H_
#include <cstdio> // for snprintf()
#include <cstring> // for memset()
#include <trident/common_internal.h>
#include <trident/rpc_endpoint.h>
namespace trident {
using boost::asio::ip::tcp;
class RpcByteStream : public trident::enable_shared_from_this<RpcByteStream>
{
public:
RpcByteStream(IOService& io_service, const RpcEndpoint& endpoint)
: _io_service(io_service)
, _remote_endpoint(endpoint)
, _ticks(0)
, _last_rw_ticks(0)
, _socket(io_service)
, _status(STATUS_INIT)
{
TRIDENT_INC_RESOURCE_COUNTER(RpcByteStream);
memset(_error_message, 0, sizeof(_error_message));
}
virtual ~RpcByteStream()
{
TRIDENT_FUNCTION_TRACE;
close("stream destructed");
boost::system::error_code ec;
_socket.close(ec);
TRIDENT_DEC_RESOURCE_COUNTER(RpcByteStream);
}
// Close the channel.
void close(const std::string& reason)
{
// should run for only once
if (atomic_swap(&_status, (int)STATUS_CLOSED) != STATUS_CLOSED)
{
snprintf(_error_message, sizeof(_error_message), "%s", reason.c_str());
boost::system::error_code ec;
_socket.shutdown(tcp::socket::shutdown_both, ec);
on_closed();
if (_remote_endpoint != RpcEndpoint())
{
#if 0
LOG(INFO) << "close(): connection closed: "
<< RpcEndpointToString(_remote_endpoint)
<< ": " << _error_message;
#else
SLOG(INFO, "close(): connection closed: %s: %s",
RpcEndpointToString(_remote_endpoint).c_str(), _error_message);
#endif
}
}
}
// Connect the channel. Used by client.
void async_connect()
{
TRIDENT_FUNCTION_TRACE;
_last_rw_ticks = _ticks;
_status = STATUS_CONNECTING;
_socket.async_connect(_remote_endpoint,
boost::bind(&RpcByteStream::on_connect, shared_from_this(), _1));
}
// Set socket connected. Used by server.
//
// Precondition:
// * the "socket" is opened.
void set_socket_connected()
{
TRIDENT_FUNCTION_TRACE;
_last_rw_ticks = _ticks;
boost::system::error_code ec;
_local_endpoint = _socket.local_endpoint(ec);
if (ec)
{
#if 0
LOG(ERROR) << "set_socket_connected(): get local endpoint failed: "
<< ec.message();
#else
SLOG(ERROR, "set_socket_connected(): get local endpoint failed: %s",
ec.message().c_str());
#endif
close("init stream failed: " + ec.message());
return;
}
_remote_endpoint = _socket.remote_endpoint(ec);
if (ec)
{
#if 0
LOG(ERROR) << "set_socket_connected(): get remote endpoint failed: "
<< ec.message();
#else
SLOG(ERROR, "set_socket_connected(): get remote endpoint failed: %s",
ec.message().c_str());
#endif
close("init stream failed: " + ec.message());
return;
}
if (!on_connected())
{
#if 0
LOG(ERROR) << "set_socket_connected(): call on_connected() failed";
#else
SLOG(ERROR, "set_socket_connected(): call on_connected() failed");
#endif
close("init stream failed: call on_connected() failed");
return;
}
_status = STATUS_CONNECTED;
trigger_receive();
trigger_send();
}
// Get the socket.
tcp::socket& socket()
{
return _socket;
}
// Get the local endpoint.
const RpcEndpoint& local_endpoint() const
{
return _local_endpoint;
}
// Get the remote endpoint.
const RpcEndpoint& remote_endpoint() const
{
return _remote_endpoint;
}
// Check if the channel is connecting.
bool is_connecting() const
{
return _status == STATUS_CONNECTING;
}
// Check if the channel is connected.
bool is_connected() const
{
return _status == STATUS_CONNECTED;
}
// Check if the channel is closed.
bool is_closed() const
{
return _status == STATUS_CLOSED;
}
// Reset current time ticks.
void reset_ticks(int64 ticks)
{
_ticks = ticks;
}
// Get the last time ticks for read or write.
int64 last_rw_ticks() const
{
return _last_rw_ticks;
}
// Trigger receiving operator.
// @return true if suceessfully triggered
virtual bool trigger_receive() = 0;
// Trigger sending operator.
// @return true if suceessfully triggered
virtual bool trigger_send() = 0;
protected:
// Async read some data from the stream.
void async_read_some(char* data, size_t size)
{
TRIDENT_FUNCTION_TRACE;
_socket.async_read_some(boost::asio::buffer(data, size),
boost::bind(&RpcByteStream::on_read_some,
shared_from_this(), _1, _2));
}
// Async write some data to the stream.
void async_write_some(const char* data, size_t size)
{
TRIDENT_FUNCTION_TRACE;
_socket.async_write_some(boost::asio::buffer(data, size),
boost::bind(&RpcByteStream::on_write_some,
shared_from_this(), _1, _2));
}
// Hook function when connected.
// @return false if some error occured.
virtual bool on_connected() = 0;
// Hook function when closed.
virtual void on_closed() = 0;
// Callback of "async_read_some()".
virtual void on_read_some(
const boost::system::error_code& error,
std::size_t bytes_transferred) = 0;
// Callback of "async_write_some()".
virtual void on_write_some(
const boost::system::error_code& error,
std::size_t bytes_transferred) = 0;
private:
// Callback of "async_connect()".
void on_connect(const boost::system::error_code& error)
{
TRIDENT_FUNCTION_TRACE;
if (error)
{
// TODO retry connect?
#if 0
LOG(ERROR) << "on_connect(): connect error: "
<< RpcEndpointToString(_remote_endpoint) << ": " << error.message();
#else
SLOG(ERROR, "on_connect(): connect error: %s: %s",
RpcEndpointToString(_remote_endpoint).c_str(),
error.message().c_str());
#endif
close("init stream failed: " + error.message());
return;
}
boost::system::error_code ec;
_local_endpoint = _socket.local_endpoint(ec);
if (ec)
{
#if 0
LOG(ERROR) << "on_connect(): get local endpoint failed: "
<< ec.message();
#else
SLOG(ERROR, "on_connect(): get local endpoint failed: %s",
ec.message().c_str());
#endif
close("init stream failed: " + ec.message());
return;
}
if (!on_connected())
{
#if 0
LOG(ERROR) << "on_connect(): call on_connected() failed";
#else
SLOG(ERROR, "on_connect(): call on_connected() failed");
#endif
close("init stream failed: call on_connected() failed");
return;
}
#if 0
LOG(INFO) << "on_connect(): connection established: "
<< RpcEndpointToString(_remote_endpoint);
#else
SLOG(INFO, "on_connect(): connection established: %s",
RpcEndpointToString(_remote_endpoint).c_str());
#endif
_status = STATUS_CONNECTED;
trigger_receive();
trigger_send();
}
protected:
IOService& _io_service;
RpcEndpoint _local_endpoint;
RpcEndpoint _remote_endpoint;
char _error_message[128];
volatile int64 _ticks;
volatile int64 _last_rw_ticks;
private:
tcp::socket _socket;
enum {
STATUS_INIT = 0,
STATUS_CONNECTING = 1,
STATUS_CONNECTED = 2,
STATUS_CLOSED = 3,
};
volatile int _status;
}; // class RpcByteStream
} // namespace trident
#endif // _TRIDENT_RPC_BYTE_STREAM_H_
/* vim: set ts=4 sw=4 sts=4 tw=100 */