forked from anqin/trident
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_client_stream.h
254 lines (222 loc) · 8.31 KB
/
rpc_client_stream.h
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
// 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_CLIENT_STREAM_H_
#define _TRIDENT_RPC_CLIENT_STREAM_H_
#include <trident/rpc_message_stream.h>
#include <trident/rpc_controller_impl.h>
#include <trident/rpc_meta.pb.h>
namespace trident {
class RpcClientStream : public RpcMessageStream<RpcControllerImplPtr>
{
public:
RpcClientStream(IOService& io_service, const RpcEndpoint& endpoint)
: RpcMessageStream<RpcControllerImplPtr>(ROLE_TYPE_CLIENT, io_service, endpoint)
{}
virtual ~RpcClientStream()
{
TRIDENT_FUNCTION_TRACE;
close("stream destructed");
for (ControllerMap::iterator it = _controller_map.begin();
it != _controller_map.end(); ++it)
{
it->second->Done(RPC_ERROR_CONNECTION_CLOSED, _error_message);
}
}
void call_method(const RpcControllerImplPtr& cntl)
{
TRIDENT_FUNCTION_TRACE;
if (is_closed())
{
#if defined( LOG )
LOG(ERROR) << "call_method(): " << RpcEndpointToString(_remote_endpoint)
<< ": stream already closed: " << _error_message;
#else
SLOG(ERROR, "call_method(): %s: stream already closed: %s",
RpcEndpointToString(_remote_endpoint).c_str(), _error_message);
#endif
cntl->Done(RPC_ERROR_CONNECTION_CLOSED, _error_message);
return;
}
// add to map
uint64 sequence_id = cntl->SequenceId();
{
ScopedLocker<FastLock> _(_controller_map_lock);
_controller_map[sequence_id] = cntl;
}
/// !!!Attention: the call may be already done at this point.
async_send_message(cntl->RequestBuffer(), cntl);
}
private:
virtual void on_closed()
{
TRIDENT_FUNCTION_TRACE;
ControllerMap tmp_map;
{
ScopedLocker<FastLock> _(_controller_map_lock);
tmp_map.swap(_controller_map);
}
for (ControllerMap::iterator it = tmp_map.begin();
it != tmp_map.end(); ++it)
{
it->second->Done(RPC_ERROR_CONNECTION_CLOSED, _error_message);
}
}
virtual bool on_sending(
const ReadBufferPtr& /* request_message */,
const RpcControllerImplPtr& cntl)
{
TRIDENT_FUNCTION_TRACE;
// if already done (may be done by timeout manager),
// should cancel sending.
return !cntl->IsDone() && !cntl->IsStartCancel();
}
virtual void on_sent(
const ReadBufferPtr& request_message,
const RpcControllerImplPtr& cntl)
{
TRIDENT_FUNCTION_TRACE;
// the request has been sent.
cntl->NotifyRequestSent(_local_endpoint, request_message->TotalCount());
}
virtual void on_send_failed(
RpcErrorCode error_code,
const ReadBufferPtr& /* request_message */,
const RpcControllerImplPtr& cntl)
{
TRIDENT_FUNCTION_TRACE;
// TODO more efficient sync map
// remove the call handle from map
uint64 sequence_id = cntl->SequenceId();
{
ScopedLocker<FastLock> _(_controller_map_lock);
_controller_map.erase(sequence_id);
}
cntl->Done(error_code, _error_message);
}
virtual void on_received(
const ReadBufferPtr& message,
int meta_size,
int64 data_size)
{
TRIDENT_FUNCTION_TRACE;
RpcMeta meta;
if (!meta.ParseFromBoundedZeroCopyStream(message.get(), meta_size))
{
#if defined( LOG )
LOG(ERROR) << "on_received(): " << RpcEndpointToString(_remote_endpoint)
<< ": parse rpc meta failed, ignore";
#else
SLOG(ERROR, "on_received(): %s: parse rpc meta failed, ignore",
RpcEndpointToString(_remote_endpoint).c_str());
#endif
return;
}
if (meta.type() != RpcMeta::RESPONSE)
{
// TODO handle un-expected message type
//
// just ignore it
#if defined( LOG )
LOG(ERROR) << "on_received(): " << RpcEndpointToString(_remote_endpoint)
<< " {" << meta.sequence_id() << "}"
<< ": un-expected message type " << meta.type() << ", ignore";
#else
SLOG(ERROR, "on_received(): %s {%lu}: un-expected message type %d, ignore",
RpcEndpointToString(_remote_endpoint).c_str(),
meta.sequence_id(), meta.type());
#endif
return;
}
// find corresponding call handle and erase from map
// TODO more efficient sync map
RpcControllerImplPtr cntl;
{
ScopedLocker<FastLock> _(_controller_map_lock);
ControllerMap::iterator it = _controller_map.find(meta.sequence_id());
if (it != _controller_map.end())
{
cntl = it->second;
_controller_map.erase(it);
}
}
if (!cntl)
{
// TODO handle un-expected sequence id
//
// just ignore it
#if defined( LOG )
LOG(ERROR) << "on_received(): " << RpcEndpointToString(_remote_endpoint)
<< " {" << meta.sequence_id() << "}"
<< ": sequence id not found, ignore";
#else
SLOG(ERROR, "on_received(): %s {%lu}: sequence id not found, ignore",
RpcEndpointToString(_remote_endpoint).c_str(), meta.sequence_id());
#endif
return;
}
if (!meta.has_failed())
{
#if defined( LOG )
LOG(ERROR) << "on_received(): " << RpcEndpointToString(_remote_endpoint)
<< " {" << meta.sequence_id() << "}"
<< ": bad rpc meta: \"failed\" field not set";
#else
SLOG(ERROR, "on_received(): %s {%lu}: bad rpc meta: \"failed\" field not set",
RpcEndpointToString(_remote_endpoint).c_str(), meta.sequence_id());
#endif
cntl->Done(RPC_ERROR_PARSE_RESPONSE_MESSAGE, "rpc meta: \"failed\" field not set");
}
else if (meta.failed())
{
if (!meta.has_error_code())
{
#if defined( LOG )
LOG(ERROR) << "on_received(): " << RpcEndpointToString(_remote_endpoint)
<< " {" << meta.sequence_id() << "}"
<< ": bad rpc meta: \"error_code\" field not set";
#else
SLOG(ERROR, "on_received(): %s {%lu}: bad rpc meta: \"error_code\" field not set",
RpcEndpointToString(_remote_endpoint).c_str(), meta.sequence_id());
#endif
cntl->Done(RPC_ERROR_PARSE_RESPONSE_MESSAGE, "rpc meta: \"error_code\" field not set");
}
else if (!meta.has_reason())
{
#if defined( LOG )
LOG(ERROR) << "on_received(): " << RpcEndpointToString(_remote_endpoint)
<< " {" << meta.sequence_id() << "}"
<< ": bad rpc meta: \"reason\" field not set";
#else
SLOG(ERROR, "on_received(): %s {%lu}: bad rpc meta: \"reason\" field not set",
RpcEndpointToString(_remote_endpoint).c_str(), meta.sequence_id());
#endif
cntl->Done(RPC_ERROR_PARSE_RESPONSE_MESSAGE, "rpc meta: \"reason\" field not set");
}
else
{
cntl->Done(meta.error_code(), meta.reason());
}
}
else // !meta.failed()
{
SCHECK_EQ(data_size, message->TotalCount() - message->ByteCount());
cntl->SetResponseBuffer(message);
cntl->SetResponseCompressType(meta.has_compress_type() ?
meta.compress_type() : CompressTypeNone);
cntl->Done(RPC_SUCCESS, "succeed");
}
}
private:
// sequence_id ==> RpcControllerImplPtr
// TODO more efficient sync map
typedef std::map<uint64, RpcControllerImplPtr> ControllerMap;
ControllerMap _controller_map;
FastLock _controller_map_lock;
TRIDENT_DISALLOW_EVIL_CONSTRUCTORS(RpcClientStream);
}; // class RpcClientStream
} // namespace trident
#endif // _TRIDENT_RPC_CLIENT_STREAM_H_
/* vim: set ts=4 sw=4 sts=4 tw=100 */