forked from anqin/trident
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_request.h
More file actions
144 lines (114 loc) · 3.8 KB
/
rpc_request.h
File metadata and controls
144 lines (114 loc) · 3.8 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
// 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)
#ifndef _TRIDENT_RPC_REQUEST_H_
#define _TRIDENT_RPC_REQUEST_H_
#include <trident/buffer.h>
#include <trident/common_internal.h>
#include <trident/ptime.h>
#include <trident/rpc_endpoint.h>
#include <trident/rpc_controller_impl.h>
#include <trident/service_pool.h>
#include <trident/smart_ptr/enable_shared_from_this.hpp>
#include <trident/smart_ptr/shared_ptr.hpp>
namespace trident {
class RpcRequest;
typedef trident::shared_ptr<RpcRequest> RpcRequestPtr;
class RpcRequest : public trident::enable_shared_from_this<RpcRequest>
{
public:
enum RpcRequestType
{
BINARY,
HTTP
};
public:
RpcRequest() {}
virtual ~RpcRequest() {}
// The request type: BINARY or HTTP.
virtual RpcRequestType RequestType() = 0;
// The method full name to request.
virtual std::string Method() = 0;
// The sequence id if supported.
virtual uint64 SequenceId() = 0;
// Process the request.
virtual void ProcessRequest(
const RpcServerStreamWPtr& server_stream,
const ServicePoolPtr& service_pool) = 0;
// Assemble succeed response to buffer.
virtual ReadBufferPtr AssembleSucceedResponse(
const RpcControllerImplPtr& cntl,
const google::protobuf::Message* response,
std::string& err) = 0;
// Assemble failed response to buffer.
virtual ReadBufferPtr AssembleFailedResponse(
int32 error_code,
const std::string& reason,
std::string& err) = 0;
public:
void CallMethod(
MethodBoard* method_board,
RpcController* controller,
google::protobuf::Message* request,
google::protobuf::Message* response);
void OnCallMethodDone(
MethodBoard* method_board,
RpcController* controller,
google::protobuf::Message* request,
google::protobuf::Message* response);
void SendSucceedResponse(
const RpcControllerImplPtr& cntl,
const google::protobuf::Message* response);
void SendSucceedResponse(
const RpcServerStreamWPtr& stream,
const ReadBufferPtr& buffer);
void SendFailedResponse(
const RpcServerStreamWPtr& stream,
int32 error_code,
const std::string& reason);
void OnSendResponseDone(
RpcErrorCode error_code);
// Parse service & method name from method full name.
// @return false if parse failed.
static bool ParseMethodFullName(
const std::string& method_full_name,
std::string* service_name,
std::string* method_name);
// Find method board from service pool.
// @return NULL if not found.
static MethodBoard* FindMethodBoard(
const ServicePoolPtr& service_pool,
const std::string& service_name,
const std::string& method_name);
void SetLocalEndpoint(const RpcEndpoint& local_endpoint)
{
_local_endpoint = local_endpoint;
}
const RpcEndpoint& LocalEndpoint()
{
return _local_endpoint;
}
void SetRemoteEndpoint(const RpcEndpoint& remote_endpoint)
{
_remote_endpoint = remote_endpoint;
}
const RpcEndpoint& RemoteEndpoint()
{
return _remote_endpoint;
}
void SetReceivedTime(const PTime& received_time)
{
_received_time = received_time;
}
const PTime& ReceivedTime()
{
return _received_time;
}
protected:
RpcEndpoint _local_endpoint;
RpcEndpoint _remote_endpoint;
PTime _received_time;
}; // class RpcRequest
} // namespace trident
#endif // _TRIDENT_RPC_REQUEST_H_