| 1 |
/* |
| 2 |
* proto rpc client |
| 3 |
* |
| 4 |
* @build make protorpc |
| 5 |
* @server bin/protorpc_server 1234 |
| 6 |
* @client bin/protorpc_client 127.0.0.1 1234 add 1 2 |
| 7 |
* |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "TcpClient.h" |
| 11 |
|
| 12 |
#include <mutex> |
| 13 |
#include <condition_variable> |
| 14 |
|
| 15 |
using namespace hv; |
| 16 |
|
| 17 |
#include "protorpc.h" |
| 18 |
#include "generated/base.pb.h" |
| 19 |
#include "generated/calc.pb.h" |
| 20 |
#include "generated/login.pb.h" |
| 21 |
|
| 22 |
// valgrind --leak-check=full --show-leak-kinds=all |
| 23 |
class ProtobufRAII { |
| 24 |
public: |
| 25 |
ProtobufRAII() { |
| 26 |
} |
| 27 |
~ProtobufRAII() { |
| 28 |
google::protobuf::ShutdownProtobufLibrary(); |
| 29 |
} |
| 30 |
}; |
| 31 |
static ProtobufRAII s_protobuf; |
| 32 |
|
| 33 |
namespace protorpc { |
| 34 |
typedef std::shared_ptr<protorpc::Request> RequestPtr; |
| 35 |
typedef std::shared_ptr<protorpc::Response> ResponsePtr; |
| 36 |
|
| 37 |
enum ProtoRpcResult { |
| 38 |
kRpcSuccess = 0, |
| 39 |
kRpcTimeout = -1, |
| 40 |
kRpcError = -2, |
| 41 |
kRpcNoResult = -3, |
| 42 |
kRpcParseError = -4, |
| 43 |
}; |
| 44 |
|
| 45 |
class ProtoRpcContext { |
| 46 |
public: |
| 47 |
protorpc::RequestPtr req; |
| 48 |
protorpc::ResponsePtr res; |
| 49 |
private: |
| 50 |
std::mutex _mutex; |
| 51 |
std::condition_variable _cond; |
| 52 |
|
| 53 |
public: |
| 54 |
void wait(int timeout_ms) { |
| 55 |
std::unique_lock<std::mutex> locker(_mutex); |
| 56 |
_cond.wait_for(locker, std::chrono::milliseconds(timeout_ms)); |
| 57 |
} |
| 58 |
|
| 59 |
void notify() { |
| 60 |
_cond.notify_one(); |
| 61 |
} |
| 62 |
}; |
| 63 |
typedef std::shared_ptr<ProtoRpcContext> ContextPtr; |
| 64 |
|
| 65 |
class ProtoRpcClient : public TcpClient { |
| 66 |
public: |
| 67 |
ProtoRpcClient() : TcpClient() |
| 68 |
{ |
| 69 |
connect_state = kInitialized; |
| 70 |
|
| 71 |
setConnectTimeout(5000); |
| 72 |
|
| 73 |
reconn_setting_t reconn; |
| 74 |
reconn_setting_init(&reconn); |
| 75 |
reconn.min_delay = 1000; |
| 76 |
reconn.max_delay = 10000; |
| 77 |
reconn.delay_policy = 2; |
| 78 |
setReconnect(&reconn); |
| 79 |
|
| 80 |
// init protorpc_unpack_setting |
| 81 |
unpack_setting_t protorpc_unpack_setting; |
| 82 |
memset(&protorpc_unpack_setting, 0, sizeof(unpack_setting_t)); |
| 83 |
protorpc_unpack_setting.mode = UNPACK_BY_LENGTH_FIELD; |
| 84 |
protorpc_unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH; |
| 85 |
protorpc_unpack_setting.body_offset = PROTORPC_HEAD_LENGTH; |
| 86 |
protorpc_unpack_setting.length_field_offset = PROTORPC_HEAD_LENGTH_FIELD_OFFSET; |
| 87 |
protorpc_unpack_setting.length_field_bytes = PROTORPC_HEAD_LENGTH_FIELD_BYTES; |
| 88 |
protorpc_unpack_setting.length_field_coding = ENCODE_BY_BIG_ENDIAN; |
| 89 |
setUnpack(&protorpc_unpack_setting); |
| 90 |
|
| 91 |
onConnection = [this](const SocketChannelPtr& channel) { |
| 92 |
std::string peeraddr = channel->peeraddr(); |
| 93 |
if (channel->isConnected()) { |
| 94 |
connect_state = kConnected; |
| 95 |
printf("connected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd()); |
| 96 |
} else { |
| 97 |
connect_state = kDisconnectd; |
| 98 |
printf("disconnected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd()); |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
onMessage = [this](const SocketChannelPtr& channel, Buffer* buf) { |
| 103 |
// protorpc_unpack |
| 104 |
protorpc_message msg; |
| 105 |
memset(&msg, 0, sizeof(msg)); |
| 106 |
int packlen = protorpc_unpack(&msg, buf->data(), buf->size()); |
| 107 |
if (packlen < 0) { |
| 108 |
printf("protorpc_unpack failed!\n"); |
| 109 |
return; |
| 110 |
} |
| 111 |
assert(packlen == buf->size()); |
| 112 |
if (protorpc_head_check(&msg.head) != 0) { |
| 113 |
printf("protorpc_head_check failed!\n"); |
| 114 |
return; |
| 115 |
} |
| 116 |
// Response::ParseFromArray |
| 117 |
auto res = std::make_shared<protorpc::Response>(); |
| 118 |
if (!res->ParseFromArray(msg.body, msg.head.length)) { |
| 119 |
return; |
| 120 |
} |
| 121 |
// id => res |
| 122 |
calls_mutex.lock(); |
| 123 |
auto iter = calls.find(res->id()); |
| 124 |
if (iter == calls.end()) { |
| 125 |
calls_mutex.unlock(); |
| 126 |
return; |
| 127 |
} |
| 128 |
auto ctx = iter->second; |
| 129 |
calls_mutex.unlock(); |
| 130 |
ctx->res = res; |
| 131 |
ctx->notify(); |
| 132 |
}; |
| 133 |
} |
| 134 |
|
| 135 |
int connect(int port, const char* host = "127.0.0.1") { |
| 136 |
createsocket(port, host); |
| 137 |
connect_state = kConnecting; |
| 138 |
start(); |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
protorpc::ResponsePtr call(protorpc::RequestPtr& req, int timeout_ms = 10000) { |
| 143 |
if (connect_state != kConnected) { |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
static std::atomic<uint64_t> s_id = ATOMIC_VAR_INIT(0); |
| 147 |
req->set_id(++s_id); |
| 148 |
req->id(); |
| 149 |
auto ctx = std::make_shared<protorpc::ProtoRpcContext>(); |
| 150 |
ctx->req = req; |
| 151 |
calls_mutex.lock(); |
| 152 |
calls[req->id()] = ctx; |
| 153 |
calls_mutex.unlock(); |
| 154 |
// Request::SerializeToArray + protorpc_pack |
| 155 |
protorpc_message msg; |
| 156 |
protorpc_message_init(&msg); |
| 157 |
msg.head.length = req->ByteSize(); |
| 158 |
int packlen = protorpc_package_length(&msg.head); |
| 159 |
unsigned char* writebuf = NULL; |
| 160 |
HV_STACK_ALLOC(writebuf, packlen); |
| 161 |
packlen = protorpc_pack(&msg, writebuf, packlen); |
| 162 |
if (packlen > 0) { |
| 163 |
printf("%s\n", req->DebugString().c_str()); |
| 164 |
req->SerializeToArray(writebuf + PROTORPC_HEAD_LENGTH, msg.head.length); |
| 165 |
channel->write(writebuf, packlen); |
| 166 |
} |
| 167 |
HV_STACK_FREE(writebuf); |
| 168 |
// wait until response come or timeout |
| 169 |
ctx->wait(timeout_ms); |
| 170 |
auto res = ctx->res; |
| 171 |
calls_mutex.lock(); |
| 172 |
calls.erase(req->id()); |
| 173 |
calls_mutex.unlock(); |
| 174 |
if (res == NULL) { |
| 175 |
printf("RPC timeout!\n"); |
| 176 |
} else if (res->has_error()) { |
| 177 |
printf("RPC error:\n%s\n", res->error().DebugString().c_str()); |
| 178 |
} |
| 179 |
return res; |
| 180 |
} |
| 181 |
|
| 182 |
int calc(const char* method, int num1, int num2, int& out) { |
| 183 |
auto req = std::make_shared<protorpc::Request>(); |
| 184 |
// method |
| 185 |
req->set_method(method); |
| 186 |
// params |
| 187 |
protorpc::CalcParam param1, param2; |
| 188 |
param1.set_num(num1); |
| 189 |
param2.set_num(num2); |
| 190 |
req->add_params()->assign(param1.SerializeAsString()); |
| 191 |
req->add_params()->assign(param2.SerializeAsString()); |
| 192 |
|
| 193 |
auto res = call(req); |
| 194 |
|
| 195 |
if (res == NULL) return kRpcTimeout; |
| 196 |
if (res->has_error()) return kRpcError; |
| 197 |
if (res->result().empty()) return kRpcNoResult; |
| 198 |
protorpc::CalcResult result; |
| 199 |
if (!result.ParseFromString(res->result())) return kRpcParseError; |
| 200 |
out = result.num(); |
| 201 |
return kRpcSuccess; |
| 202 |
} |
| 203 |
|
| 204 |
int login(const protorpc::LoginParam& param, protorpc::LoginResult* result) { |
| 205 |
auto req = std::make_shared<protorpc::Request>(); |
| 206 |
// method |
| 207 |
req->set_method("login"); |
| 208 |
// params |
| 209 |
req->add_params()->assign(param.SerializeAsString()); |
| 210 |
|
| 211 |
auto res = call(req); |
| 212 |
|
| 213 |
if (res == NULL) return kRpcTimeout; |
| 214 |
if (res->has_error()) return kRpcError; |
| 215 |
if (res->result().empty()) return kRpcNoResult; |
| 216 |
if (!result->ParseFromString(res->result())) return kRpcParseError; |
| 217 |
return kRpcSuccess; |
| 218 |
} |
| 219 |
|
| 220 |
enum { |
| 221 |
kInitialized, |
| 222 |
kConnecting, |
| 223 |
kConnected, |
| 224 |
kDisconnectd, |
| 225 |
} connect_state; |
| 226 |
std::map<uint64_t, protorpc::ContextPtr> calls; |
| 227 |
std::mutex calls_mutex; |
| 228 |
}; |
| 229 |
} |
| 230 |
|
| 231 |
int main(int argc, char** argv) { |
| 232 |
if (argc < 6) { |
| 233 |
printf("Usage: %s host port method param1 param2\n", argv[0]); |
| 234 |
printf("method = [add, sub, mul, div]\n"); |
| 235 |
printf("Examples:\n"); |
| 236 |
printf(" %s 127.0.0.1 1234 add 1 2\n", argv[0]); |
| 237 |
printf(" %s 127.0.0.1 1234 div 1 0\n", argv[0]); |
| 238 |
return -10; |
| 239 |
} |
| 240 |
const char* host = argv[1]; |
| 241 |
int port = atoi(argv[2]); |
| 242 |
const char* method = argv[3]; |
| 243 |
const char* param1 = argv[4]; |
| 244 |
const char* param2 = argv[5]; |
| 245 |
|
| 246 |
protorpc::ProtoRpcClient cli; |
| 247 |
cli.connect(port, host); |
| 248 |
while (cli.connect_state == protorpc::ProtoRpcClient::kConnecting) hv_msleep(1); |
| 249 |
if (cli.connect_state == protorpc::ProtoRpcClient::kDisconnectd) { |
| 250 |
return -20; |
| 251 |
} |
| 252 |
|
| 253 |
// test login |
| 254 |
{ |
| 255 |
protorpc::LoginParam param; |
| 256 |
param.set_username("admin"); |
| 257 |
param.set_password("123456"); |
| 258 |
protorpc::LoginResult result; |
| 259 |
if (cli.login(param, &result) == protorpc::kRpcSuccess) { |
| 260 |
printf("login success!\n"); |
| 261 |
printf("%s\n", result.DebugString().c_str()); |
| 262 |
} else { |
| 263 |
printf("login failed!\n"); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
// test calc |
| 268 |
{ |
| 269 |
int num1 = atoi(param1); |
| 270 |
int num2 = atoi(param2); |
| 271 |
int result = 0; |
| 272 |
if (cli.calc(method, num1, num2, result) == protorpc::kRpcSuccess) { |
| 273 |
printf("calc success!\n"); |
| 274 |
printf("%d %s %d = %d\n", num1, method, num2, result); |
| 275 |
} else { |
| 276 |
printf("calc failed!\n"); |
| 277 |
} |
| 278 |
} |
| 279 |
return 0; |
| 280 |
} |