forked from caozhiyi/CppNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPCServer.cpp
140 lines (120 loc) · 4.08 KB
/
RPCServer.cpp
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
#include "RPCServer.h"
#include "InfoRouter.h"
#include "FuncThread.h"
#include "ParsePackage.h"
#include "Log.h"
#include "CppNet.h"
#include "CommonStruct.h"
CRPCServer::CRPCServer() : _info_router(new CInfoRouter), _parse_package(new CParsePackage), _pool(1024, 5), _need_mutex(false){
}
CRPCServer::~CRPCServer() {
}
void CRPCServer::Init(int thread) {
for (int i = 0; i < thread; i++) {
auto thread = std::shared_ptr<CFuncThread>(new CFuncThread(_info_router));
_info_router->AddThread(thread);
}
}
void CRPCServer::Destroy() {
_info_router->StopAllThread();
}
void CRPCServer::Start(short port, std::string ip) {
cppnet::Init(2);
cppnet::SetAcceptCallback(std::bind(&CRPCServer::_DoAccept, this, std::placeholders::_1, std::placeholders::_2));
cppnet::SetWriteCallback(std::bind(&CRPCServer::_DoWrite, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
cppnet::SetReadCallback(std::bind(&CRPCServer::_DoRead, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
cppnet::ListenAndAccept(ip, port);
for (;;) {
auto info = _info_router->GetRet();
if (info) {
_PackageAndSend(info->_socket, info, NO_ERROR);
}
}
cppnet::Join();
}
bool CRPCServer::RegisterFunc(std::string name, std::string func_str, const CommonFunc& func) {
std::unique_lock<std::mutex> lock(_mutex);
if (_func_map.count(name)) {
return false;
}
_func_map[name] = func_str;
_info_router->RegisterFunc(name, func);
return true;
}
bool CRPCServer::RemoveFunc(std::string name) {
std::unique_lock<std::mutex> lock(_mutex);
if (_func_map.count(name)) {
_func_map.erase(name);
_info_router->RemoveFunc(name);
return true;
}
return false;
}
void CRPCServer::_DoRead(const cppnet::Handle& handle, base::CBuffer* data,
uint32_t len, uint32_t err) {
if (err != cppnet::CEC_SUCCESS) {
base::LOG_ERROR("read data failed! err : %d", err);
return;
}
int need_len = 0;
for (;;) {
char recv_buf[4096] = { 0 };
int read_len = data->ReadUntil(recv_buf, 4096, "\r\n\r\n", strlen("\r\n\r\n"), need_len);
//get a comlete message
if (read_len > 0) {
FuncCallInfo* info = _pool.PoolNew<FuncCallInfo>();
if (_need_mutex) {
std::unique_lock<std::mutex> lock(_mutex);
if (_parse_package->ParseFuncCall(recv_buf + 2, read_len - 2, info->_func_name, _func_map, info->_func_param_ret)) {
info->_socket = handle;
_info_router->PushTask(info);
} else {
base::LOG_ERROR("parse function call request failed!");
}
} else {
if (_parse_package->ParseFuncCall(recv_buf + 2, read_len - 2, info->_func_name, _func_map, info->_func_param_ret)) {
info->_socket = handle;
_info_router->PushTask(info);
} else {
base::LOG_ERROR("parse function call request failed!");
}
}
} else {
break;
}
}
}
void CRPCServer::_DoWrite(const cppnet::Handle&, uint32_t, uint32_t err) {
if (err != cppnet::CEC_SUCCESS) {
base::LOG_ERROR("send response to client failed!");
}
}
void CRPCServer::_DoAccept(const cppnet::Handle& handle, uint32_t) {
char buf[8192] = { 0 };
int len = 8192;
std::unique_lock<std::mutex> lock(_mutex);
if (!_parse_package->PackageFuncList(buf, len, _func_map)) {
base::LOG_ERROR("package functnion info failed!");
abort();
}
base::LOG_DEBUG("send to %d, buf : %s", handle, buf);
cppnet::Write(handle, buf, len);
}
void CRPCServer::_PackageAndSend(const cppnet::Handle& handle, FuncCallInfo* info, int code) {
if (!info) {
base::LOG_ERROR("function info is null!");
return;
}
bool send = true;
int get_len = 65535;
int need_len = 0;
char send_buf[65535] = { 0 };
need_len = get_len;
if (!_parse_package->PackageFuncRet(send_buf, need_len, code, info->_func_name, _func_map, info->_func_param_ret)) {
base::LOG_ERROR("package function response failed!");
send = false;
}
if (send) {
cppnet::Write(handle, send_buf, need_len);
}
}