forked from anqin/trident
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_request.cc
More file actions
213 lines (194 loc) · 6.87 KB
/
rpc_request.cc
File metadata and controls
213 lines (194 loc) · 6.87 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
// 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.
// Author: Zuoyan Qin (qinzuoyan@gmail.com)
#include <trident/rpc_request.h>
#include <trident/closure.h>
#include <trident/rpc_server_stream.h>
namespace trident {
void RpcRequest::CallMethod(
MethodBoard* method_board,
RpcController* controller,
google::protobuf::Message* request,
google::protobuf::Message* response)
{
method_board->ReportProcessBegin();
google::protobuf::Closure* done = NewClosure(
shared_from_this(), &RpcRequest::OnCallMethodDone,
method_board, controller, request, response);
method_board->GetServiceBoard()->Service()->CallMethod(
method_board->Descriptor(), controller, request, response, done);
}
void RpcRequest::OnCallMethodDone(
MethodBoard* method_board,
RpcController* controller,
google::protobuf::Message* request,
google::protobuf::Message* response)
{
const RpcControllerImplPtr& cntl = controller->impl();
cntl->SetFinishProcessTime(ptime_now());
int64 process_time_us =
(cntl->FinishProcessTime() - cntl->StartProcessTime()).total_microseconds();
method_board->ReportProcessEnd(!cntl->Failed(), process_time_us);
if (cntl->Failed())
{
#if 0
LOG(ERROR) << "OnCallMethodDone(): " << RpcEndpointToString(_remote_endpoint)
<< " {" << cntl->SequenceId() << "}"
<< ": call method \"" << cntl->MethodId() << "\" failed: "
<< RpcErrorCodeToString(cntl->ErrorCode()) << ": " << cntl->Reason();
#else
SLOG(ERROR, "OnCallMethodDone(): %s {%lu}: call method \"%s\" failed: %s: %s",
RpcEndpointToString(_remote_endpoint).c_str(), cntl->SequenceId(),
cntl->MethodId().c_str(),
RpcErrorCodeToString(cntl->ErrorCode()), cntl->Reason().c_str());
#endif
SendFailedResponse(cntl->RpcServerStream(), cntl->ErrorCode(), cntl->Reason());
}
else
{
#if 0
#else
SLOG(DEBUG, "OnCallMethodDone(): %s {%lu}: call method \"%s\" succeed",
RpcEndpointToString(_remote_endpoint).c_str(), cntl->SequenceId(),
cntl->MethodId().c_str());
#endif
SendSucceedResponse(cntl, response);
}
delete request;
delete response;
delete controller;
}
void RpcRequest::SendSucceedResponse(
const RpcControllerImplPtr& cntl,
const google::protobuf::Message* response)
{
std::string err;
ReadBufferPtr read_buffer = AssembleSucceedResponse(cntl, response, err);
if (!read_buffer)
{
#if 0
LOG(ERROR) << "SendSucceedResponse(): " << RpcEndpointToString(_remote_endpoint)
<< " {" << SequenceId() << "}"
<< ": assemble response buffer failed: " << err;
#else
SLOG(ERROR, "SendSucceedResponse(): %s {%lu}: assemble response buffer failed: %s",
RpcEndpointToString(_remote_endpoint).c_str(), SequenceId(), err.c_str());
#endif
return;
}
SendSucceedResponse(cntl->RpcServerStream(), read_buffer);
}
void RpcRequest::SendSucceedResponse(
const RpcServerStreamWPtr& stream,
const ReadBufferPtr& buffer)
{
RpcServerStreamPtr real_stream = stream.lock();
if (!real_stream)
{
#if 0
LOG(ERROR) << "SendSucceedResponse(): " << RpcEndpointToString(_remote_endpoint)
<< " {" << SequenceId() << "}"
<< ": stream already closed, ignore";
#else
SLOG(ERROR, "SendSucceedResponse(): %s {%lu}: stream already closed, ignore",
RpcEndpointToString(_remote_endpoint).c_str(), SequenceId());
#endif
return;
}
real_stream->send_response(buffer,
boost::bind(&RpcRequest::OnSendResponseDone, shared_from_this(), _1));
}
void RpcRequest::SendFailedResponse(
const RpcServerStreamWPtr& stream,
int32 error_code,
const std::string& reason)
{
uint64 sequence_id = SequenceId();
RpcServerStreamPtr real_stream = stream.lock();
if (!real_stream)
{
#if 0
LOG(ERROR) << "SendFailedResponse(): " << RpcEndpointToString(_remote_endpoint)
<< " {" << sequence_id << "}"
<< ": stream already closed, ignore";
#else
SLOG(ERROR, "SendFailedResponse(): %s {%lu}: stream already closed, ignore",
RpcEndpointToString(_remote_endpoint).c_str(), sequence_id);
#endif
return;
}
std::string err;
ReadBufferPtr read_buffer = AssembleFailedResponse(error_code, reason, err);
if (!read_buffer)
{
#if 0
LOG(ERROR) << "SendFailedResponse(): " << RpcEndpointToString(_remote_endpoint)
<< " {" << sequence_id << "}"
<< ": assemble response buffer failed: " << err;
#else
SLOG(ERROR, "SendFailedResponse(): %s {%lu}: assemble response buffer failed: %s",
RpcEndpointToString(_remote_endpoint).c_str(), sequence_id, err.c_str());
#endif
return;
}
real_stream->send_response(read_buffer,
boost::bind(&RpcRequest::OnSendResponseDone, shared_from_this(), _1));
}
void RpcRequest::OnSendResponseDone(
RpcErrorCode error_code)
{
if (error_code == RPC_SUCCESS)
{
#if 0
#else
SLOG(DEBUG, "OnSendResponseDone(): %s {%lu}: send succeed",
RpcEndpointToString(_remote_endpoint).c_str(), SequenceId());
#endif
}
else
{
#if 0
LOG(ERROR) << "OnSendResponseDone(): " << RpcEndpointToString(_remote_endpoint)
<< " {" << SequenceId() << "}"
<< ": send failed: " << RpcErrorCodeToString(error_code);
#else
SLOG(ERROR, "OnSendResponseDone(): %s {%lu}: send failed: %s",
RpcEndpointToString(_remote_endpoint).c_str(), SequenceId(),
RpcErrorCodeToString(error_code));
#endif
}
}
bool RpcRequest::ParseMethodFullName(
const std::string& method_full_name,
std::string* service_name,
std::string* method_name)
{
std::string::size_type pos = method_full_name.rfind('.');
if (pos == std::string::npos)
{
return false;
}
*service_name = method_full_name.substr(0, pos);
*method_name = method_full_name.substr(pos + 1);
return true;
}
MethodBoard* RpcRequest::FindMethodBoard(
const ServicePoolPtr& service_pool,
const std::string& service_name,
const std::string& method_name)
{
ServiceBoard* service_board = service_pool->FindService(service_name);
if (service_board == NULL)
{
return NULL;
}
const google::protobuf::MethodDescriptor* method_desc =
service_board->Descriptor()->FindMethodByName(method_name);
if (method_desc == NULL)
{
return NULL;
}
return service_board->Method(method_desc->index());
}
} // namespace trident