| 1 |
/* |
| 2 |
* proto rpc server |
| 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 "TcpServer.h" |
| 11 |
|
| 12 |
using namespace hv; |
| 13 |
|
| 14 |
#include "protorpc.h" |
| 15 |
#include "router.h" |
| 16 |
#include "handler/handler.h" |
| 17 |
#include "handler/calc.h" |
| 18 |
#include "handler/login.h" |
| 19 |
|
| 20 |
// valgrind --leak-check=full --show-leak-kinds=all |
| 21 |
class ProtobufRAII { |
| 22 |
public: |
| 23 |
ProtobufRAII() { |
| 24 |
} |
| 25 |
~ProtobufRAII() { |
| 26 |
google::protobuf::ShutdownProtobufLibrary(); |
| 27 |
} |
| 28 |
}; |
| 29 |
static ProtobufRAII s_protobuf; |
| 30 |
|
| 31 |
protorpc_router router[] = { |
| 32 |
{"add", calc_add}, |
| 33 |
{"sub", calc_sub}, |
| 34 |
{"mul", calc_mul}, |
| 35 |
{"div", calc_div}, |
| 36 |
{"login", login}, |
| 37 |
}; |
| 38 |
#define PROTORPC_ROUTER_NUM (sizeof(router)/sizeof(router[0])) |
| 39 |
|
| 40 |
class ProtoRpcServer : public TcpServer { |
| 41 |
public: |
| 42 |
ProtoRpcServer() : TcpServer() |
| 43 |
{ |
| 44 |
onConnection = [](const SocketChannelPtr& channel) { |
| 45 |
std::string peeraddr = channel->peeraddr(); |
| 46 |
if (channel->isConnected()) { |
| 47 |
printf("%s connected! connfd=%d\n", peeraddr.c_str(), channel->fd()); |
| 48 |
} else { |
| 49 |
printf("%s disconnected! connfd=%d\n", peeraddr.c_str(), channel->fd()); |
| 50 |
} |
| 51 |
}; |
| 52 |
onMessage = handleMessage; |
| 53 |
// init protorpc_unpack_setting |
| 54 |
unpack_setting_t protorpc_unpack_setting; |
| 55 |
memset(&protorpc_unpack_setting, 0, sizeof(unpack_setting_t)); |
| 56 |
protorpc_unpack_setting.mode = UNPACK_BY_LENGTH_FIELD; |
| 57 |
protorpc_unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH; |
| 58 |
protorpc_unpack_setting.body_offset = PROTORPC_HEAD_LENGTH; |
| 59 |
protorpc_unpack_setting.length_field_offset = PROTORPC_HEAD_LENGTH_FIELD_OFFSET; |
| 60 |
protorpc_unpack_setting.length_field_bytes = PROTORPC_HEAD_LENGTH_FIELD_BYTES; |
| 61 |
protorpc_unpack_setting.length_field_coding = ENCODE_BY_BIG_ENDIAN; |
| 62 |
setUnpack(&protorpc_unpack_setting); |
| 63 |
} |
| 64 |
|
| 65 |
int listen(int port) { return createsocket(port); } |
| 66 |
|
| 67 |
private: |
| 68 |
static void handleMessage(const SocketChannelPtr& channel, Buffer* buf) { |
| 69 |
// unpack -> Request::ParseFromArray -> router -> Response::SerializeToArray -> pack -> Channel::write |
| 70 |
// protorpc_unpack |
| 71 |
protorpc_message msg; |
| 72 |
memset(&msg, 0, sizeof(msg)); |
| 73 |
int packlen = protorpc_unpack(&msg, buf->data(), buf->size()); |
| 74 |
if (packlen < 0) { |
| 75 |
printf("protorpc_unpack failed!\n"); |
| 76 |
return; |
| 77 |
} |
| 78 |
assert(packlen == buf->size()); |
| 79 |
if (protorpc_head_check(&msg.head) != 0) { |
| 80 |
printf("protorpc_head_check failed!\n"); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
// Request::ParseFromArray |
| 85 |
protorpc::Request req; |
| 86 |
protorpc::Response res; |
| 87 |
if (req.ParseFromArray(msg.body, msg.head.length)) { |
| 88 |
printf("> %s\n", req.DebugString().c_str()); |
| 89 |
res.set_id(req.id()); |
| 90 |
// router |
| 91 |
const char* method = req.method().c_str(); |
| 92 |
bool found = false; |
| 93 |
for (int i = 0; i < PROTORPC_ROUTER_NUM; ++i) { |
| 94 |
if (strcmp(method, router[i].method) == 0) { |
| 95 |
found = true; |
| 96 |
router[i].handler(req, &res); |
| 97 |
break; |
| 98 |
} |
| 99 |
} |
| 100 |
if (!found) { |
| 101 |
not_found(req, &res); |
| 102 |
} |
| 103 |
} else { |
| 104 |
bad_request(req, &res); |
| 105 |
} |
| 106 |
|
| 107 |
// Response::SerializeToArray + protorpc_pack |
| 108 |
protorpc_message_init(&msg); |
| 109 |
msg.head.length = res.ByteSize(); |
| 110 |
packlen = protorpc_package_length(&msg.head); |
| 111 |
unsigned char* writebuf = NULL; |
| 112 |
HV_STACK_ALLOC(writebuf, packlen); |
| 113 |
packlen = protorpc_pack(&msg, writebuf, packlen); |
| 114 |
if (packlen > 0) { |
| 115 |
printf("< %s\n", res.DebugString().c_str()); |
| 116 |
res.SerializeToArray(writebuf + PROTORPC_HEAD_LENGTH, msg.head.length); |
| 117 |
channel->write(writebuf, packlen); |
| 118 |
} |
| 119 |
HV_STACK_FREE(writebuf); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
int main(int argc, char** argv) { |
| 124 |
if (argc < 2) { |
| 125 |
printf("Usage: %s port\n", argv[0]); |
| 126 |
return -10; |
| 127 |
} |
| 128 |
int port = atoi(argv[1]); |
| 129 |
|
| 130 |
ProtoRpcServer srv; |
| 131 |
int listenfd = srv.listen(port); |
| 132 |
if (listenfd < 0) { |
| 133 |
return -20; |
| 134 |
} |
| 135 |
printf("protorpc_server listen on port %d, listenfd=%d ...\n", port, listenfd); |
| 136 |
srv.setThreadNum(4); |
| 137 |
srv.start(); |
| 138 |
|
| 139 |
while (1) hv_sleep(1); |
| 140 |
return 0; |
| 141 |
} |