修正升级了jsoncpp库之后tp_core无法编译的问题。

pull/236/head
Apex Liu 2019-11-17 04:43:33 +08:00
parent 483aa810ee
commit f99af6a2df
2 changed files with 929 additions and 873 deletions

View File

@ -1,4 +1,4 @@
#include "ts_http_rpc.h"
#include "ts_http_rpc.h"
#include "ts_ver.h"
#include "ts_env.h"
#include "ts_session.h"
@ -99,7 +99,7 @@ bool TsHttpRpc::init(void)
mg_set_protocol_http_websocket(nc);
// 导致内存泄露的地方每次请求约消耗1KB内存
// 导致内存泄露的地方每次请求约消耗1KB内存
// DO NOT USE MULTITHREADING OF MG.
// cpq (one of the authors of MG) commented on 3 Feb: Multithreading support has been removed.
// https://github.com/cesanta/mongoose/commit/707b9ed2d6f177b3ad8787cb16a1bff90ddad992
@ -153,7 +153,7 @@ void TsHttpRpc::_mg_event_handler(struct mg_connection *nc, int ev, void *ev_dat
_this->_create_json_ret(ret_buf, TPE_PARAM, "not a `rpc` request.");
}
mg_printf(nc, "HTTP/1.0 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n%s", (int)ret_buf.size() - 1, &ret_buf[0]);
mg_printf(nc, "HTTP/1.0 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n%s", (int)ret_buf.length(), &ret_buf[0]);
nc->flags |= MG_F_SEND_AND_CLOSE;
}
break;
@ -188,7 +188,7 @@ ex_rv TsHttpRpc::_parse_request(struct http_message* req, ex_astr& func_cmd, Jso
}
if (need_decode) {
// 将参数进行 url-decode 解码
// 将参数进行 url-decode 解码
int len = json_str.length() * 2;
ex_chars sztmp;
sztmp.resize(len);
@ -202,9 +202,14 @@ ex_rv TsHttpRpc::_parse_request(struct http_message* req, ex_astr& func_cmd, Jso
if (0 == json_str.length())
return TPE_PARAM;
Json::Reader jreader;
//Json::Reader jreader;
Json::CharReaderBuilder jcrb;
std::unique_ptr<Json::CharReader> const jreader(jcrb.newCharReader());
const char *str_json_begin = json_str.c_str();
ex_astr err;
if (!jreader.parse(json_str.c_str(), json_param))
//if (!jreader.parse(json_str.c_str(), json_param))
if (!jreader->parse(str_json_begin, str_json_begin + json_str.length(), &json_param, &err))
return TPE_JSON_FORMAT;
if (json_param.isArray())
@ -221,37 +226,49 @@ ex_rv TsHttpRpc::_parse_request(struct http_message* req, ex_astr& func_cmd, Jso
void TsHttpRpc::_create_json_ret(ex_astr& buf, int errcode, const Json::Value& jr_data)
{
// 返回: {"code":errcode, "data":{jr_data}}
// 返回: {"code":errcode, "data":{jr_data}}
Json::FastWriter jr_writer;
//Json::FastWriter jr_writer;
Json::Value jr_root;
jr_root["code"] = errcode;
jr_root["data"] = jr_data;
buf = jr_writer.write(jr_root);
//buf = jr_writer.write(jr_root);
Json::StreamWriterBuilder jwb;
std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ex_aoss os;
jwriter->write(jr_root, &os);
buf = os.str();
}
void TsHttpRpc::_create_json_ret(ex_astr& buf, int errcode)
{
// 返回: {"code":errcode}
// 返回: {"code":errcode}
Json::FastWriter jr_writer;
//Json::FastWriter jr_writer;
Json::Value jr_root;
jr_root["code"] = errcode;
buf = jr_writer.write(jr_root);
//buf = jr_writer.write(jr_root);
Json::StreamWriterBuilder jwb;
std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ex_aoss os;
jwriter->write(jr_root, &os);
buf = os.str();
}
void TsHttpRpc::_create_json_ret(ex_astr& buf, int errcode, const char* message)
{
// 返回: {"code":errcode, "message":message}
// 返回: {"code":errcode, "message":message}
Json::FastWriter jr_writer;
//Json::FastWriter jr_writer;
Json::Value jr_root;
jr_root["code"] = errcode;
jr_root["message"] = message;
buf = jr_writer.write(jr_root);
//buf = jr_writer.write(jr_root);
Json::StreamWriterBuilder jwb;
std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ex_aoss os;
jwriter->write(jr_root, &os);
buf = os.str();
}
void TsHttpRpc::_process_request(const ex_astr& func_cmd, const Json::Value& json_param, ex_astr& buf)
@ -280,10 +297,10 @@ void TsHttpRpc::_process_request(const ex_astr& func_cmd, const Json::Value& jso
}
}
extern bool g_exit_flag; // 要求整个TS退出的标志用于停止各个工作线程
extern bool g_exit_flag; // 要求整个TS退出的标志用于停止各个工作线程
void TsHttpRpc::_rpc_func_exit(const Json::Value& json_param, ex_astr& buf)
{
// 设置一个全局退出标志
// 设置一个全局退出标志
g_exit_flag = true;
_create_json_ret(buf, TPE_OK);
}
@ -370,7 +387,7 @@ void TsHttpRpc::_rpc_func_request_session(const Json::Value& json_param, ex_astr
// info->ref_count = 0;
// info->ticket_start = ex_get_tick_count();
//
// 生成一个session-id内部会避免重复
// 生成一个session-id内部会避免重复
ex_astr sid;
if (!g_session_mgr.request_session(sid, info)) {
_create_json_ret(buf, TPE_FAILED);
@ -421,14 +438,14 @@ void TsHttpRpc::_rpc_func_kill_sessions(const Json::Value& json_param, ex_astr&
void TsHttpRpc::_rpc_func_enc(const Json::Value& json_param, ex_astr& buf)
{
// https://github.com/tp4a/teleport/wiki/TELEPORT-CORE-JSON-RPC#enc
// 加密一个字符串 [ p=plain-text, c=cipher-text ]
// 入参: {"p":"need be encrypt"}
// 示例: {"p":"this-is-a-password"}
// p: 被加密的字符串
// 返回:
// data域中的"c"的内容是加密后密文的base64编码结果
// 示例: {"code":0, "data":{"c":"Mxs340a9r3fs+3sdf=="}}
// 错误返回: {"code":1234}
// 加密一个字符串 [ p=plain-text, c=cipher-text ]
// 入参: {"p":"need be encrypt"}
// 示例: {"p":"this-is-a-password"}
// p: 被加密的字符串
// 返回:
// data域中的"c"的内容是加密后密文的base64编码结果
// 示例: {"code":0, "data":{"c":"Mxs340a9r3fs+3sdf=="}}
// 错误返回: {"code":1234}
if (json_param.isArray())
{
@ -468,7 +485,7 @@ void TsHttpRpc::_rpc_func_set_config(const Json::Value& json_param, ex_astr& buf
// https://github.com/tp4a/teleport/wiki/TELEPORT-CORE-JSON-RPC#set_config
/*
{
"noop-timeout": 15 #
"noop-timeout": 15 #
}
*/
@ -497,14 +514,14 @@ void TsHttpRpc::_rpc_func_set_config(const Json::Value& json_param, ex_astr& buf
void TsHttpRpc::_rpc_func_enc(const Json::Value& json_param, ex_astr& buf)
{
// https://github.com/tp4a/teleport/wiki/TELEPORT-CORE-JSON-RPC#enc
// 加密多个个字符串 [ p=plain-text, c=cipher-text ]
// 入参: {"p":["need be encrypt", "plain to cipher"]}
// 示例: {"p":["password-for-A"]}
// p: 被加密的字符串
// 返回:
// data域中的"c"的内容是加密后密文的base64编码结果
// 示例: {"code":0, "data":{"c":["Mxs340a9r3fs+3sdf=="]}}
// 错误返回: {"code":1234}
// 加密多个个字符串 [ p=plain-text, c=cipher-text ]
// 入参: {"p":["need be encrypt", "plain to cipher"]}
// 示例: {"p":["password-for-A"]}
// p: 被加密的字符串
// 返回:
// data域中的"c"的内容是加密后密文的base64编码结果
// 示例: {"code":0, "data":{"c":["Mxs340a9r3fs+3sdf=="]}}
// 错误返回: {"code":1234}
if (json_param.isArray())
{

View File

@ -1,4 +1,4 @@
#include "ts_web_rpc.h"
#include "ts_web_rpc.h"
#include "ts_env.h"
#include "ts_crypto.h"
#include "ts_http_client.h"
@ -10,13 +10,18 @@
bool ts_web_rpc_register_core()
{
Json::FastWriter json_writer;
//Json::FastWriter json_writer;
Json::Value jreq;
jreq["method"] = "register_core";
jreq["param"]["rpc"] = g_env.core_server_rpc;
ex_astr json_param;
json_param = json_writer.write(jreq);
//json_param = json_writer.write(jreq);
Json::StreamWriterBuilder jwb;
std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ex_aoss os;
jwriter->write(jreq, &os);
json_param = os.str();
ex_astr param;
ts_url_encode(json_param.c_str(), param);
@ -31,13 +36,18 @@ bool ts_web_rpc_register_core()
int ts_web_rpc_get_conn_info(int conn_id, TS_CONNECT_INFO& info)
{
Json::FastWriter json_writer;
//Json::FastWriter json_writer;
Json::Value jreq;
jreq["method"] = "get_conn_info";
jreq["param"]["conn_id"] = conn_id;
ex_astr json_param;
json_param = json_writer.write(jreq);
//json_param = json_writer.write(jreq);
Json::StreamWriterBuilder jwb;
std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ex_aoss os;
jwriter->write(jreq, &os);
json_param = os.str();
ex_astr param;
ts_url_encode(json_param.c_str(), param);
@ -57,10 +67,17 @@ int ts_web_rpc_get_conn_info(int conn_id, TS_CONNECT_INFO& info)
return TPE_NETWORK;
}
Json::Reader jreader;
//Json::Reader jreader;
Json::Value jret;
if (!jreader.parse(body.c_str(), jret))
//if (!jreader.parse(body.c_str(), jret))
Json::CharReaderBuilder jcrb;
std::unique_ptr<Json::CharReader> const jreader(jcrb.newCharReader());
const char *str_json_begin = body.c_str();
ex_astr err;
//if (!jreader.parse(func_args.c_str(), jsRoot)) {
if (!jreader->parse(str_json_begin, str_json_begin + body.length(), &jret, &err))
return TPE_PARAM;
if (!jret.isObject())
return TPE_PARAM;
@ -135,13 +152,13 @@ int ts_web_rpc_get_conn_info(int conn_id, TS_CONNECT_INFO& info)
int user_id;
int host_id;
int acc_id;
ex_astr user_username;// 申请本次连接的用户名
ex_astr host_ip;// 真正的远程主机IP如果是直接连接模式则与remote_host_ip相同
ex_astr conn_ip;// 要连接的远程主机的IP如果是端口映射模式则为路由主机的IP
int conn_port;// 要连接的远程主机的端口(如果是端口映射模式,则为路由主机的端口)
ex_astr user_username;// 申请本次连接的用户名
ex_astr host_ip;// 真正的远程主机IP如果是直接连接模式则与remote_host_ip相同
ex_astr conn_ip;// 要连接的远程主机的IP如果是端口映射模式则为路由主机的IP
int conn_port;// 要连接的远程主机的端口(如果是端口映射模式,则为路由主机的端口)
ex_astr client_ip;
ex_astr acc_username; // 远程主机的账号
ex_astr acc_secret;// 远程主机账号的密码(或者私钥)
ex_astr acc_username; // 远程主机的账号
ex_astr acc_secret;// 远程主机账号的密码(或者私钥)
ex_astr username_prompt;
ex_astr password_prompt;
int protocol_type = 0;
@ -171,8 +188,8 @@ int ts_web_rpc_get_conn_info(int conn_id, TS_CONNECT_INFO& info)
_enc = _jret["_enc"].asBool();
// 进一步判断参数是否合法
// 注意account_id可以为-1表示这是一次测试连接。
// 进一步判断参数是否合法
// 注意account_id可以为-1表示这是一次测试连接。
if (user_id <= 0 || host_id <= 0
|| user_username.length() == 0
|| host_ip.length() == 0 || conn_ip.length() == 0 || client_ip.length() == 0
@ -216,7 +233,7 @@ int ts_web_rpc_get_conn_info(int conn_id, TS_CONNECT_INFO& info)
bool ts_web_rpc_session_begin(TS_CONNECT_INFO& info, int& record_id)
{
Json::FastWriter json_writer;
//Json::FastWriter json_writer;
Json::Value jreq;
jreq["method"] = "session_begin";
@ -236,7 +253,12 @@ bool ts_web_rpc_session_begin(TS_CONNECT_INFO& info, int& record_id)
jreq["param"]["protocol_sub_type"] = info.protocol_sub_type;
ex_astr json_param;
json_param = json_writer.write(jreq);
//json_param = json_writer.write(jreq);
Json::StreamWriterBuilder jwb;
std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ex_aoss os;
jwriter->write(jreq, &os);
json_param = os.str();
ex_astr param;
ts_url_encode(json_param.c_str(), param);
@ -254,10 +276,17 @@ bool ts_web_rpc_session_begin(TS_CONNECT_INFO& info, int& record_id)
return false;
}
Json::Reader jreader;
//Json::Reader jreader;
Json::Value jret;
if (!jreader.parse(body.c_str(), jret))
//if (!jreader.parse(body.c_str(), jret))
Json::CharReaderBuilder jcrb;
std::unique_ptr<Json::CharReader> const jreader(jcrb.newCharReader());
const char *str_json_begin = body.c_str();
ex_astr err;
//if (!jreader.parse(func_args.c_str(), jsRoot)) {
if (!jreader->parse(str_json_begin, str_json_begin + body.length(), &jret, &err))
return false;
if (!jret.isObject())
return false;
@ -272,7 +301,7 @@ bool ts_web_rpc_session_begin(TS_CONNECT_INFO& info, int& record_id)
}
bool ts_web_rpc_session_update(int record_id, int protocol_sub_type, int state) {
Json::FastWriter json_writer;
//Json::FastWriter json_writer;
Json::Value jreq;
jreq["method"] = "session_update";
jreq["param"]["rid"] = record_id;
@ -280,7 +309,12 @@ bool ts_web_rpc_session_update(int record_id, int protocol_sub_type, int state)
jreq["param"]["code"] = state;
ex_astr json_param;
json_param = json_writer.write(jreq);
//json_param = json_writer.write(jreq);
Json::StreamWriterBuilder jwb;
std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ex_aoss os;
jwriter->write(jreq, &os);
json_param = os.str();
ex_astr param;
ts_url_encode(json_param.c_str(), param);
@ -294,19 +328,24 @@ bool ts_web_rpc_session_update(int record_id, int protocol_sub_type, int state)
}
//session 结束
//session 结束
bool ts_web_rpc_session_end(const char* sid, int record_id, int ret_code)
{
// TODO: 对指定的sid相关的会话的引用计数减一但减到0时销毁
// TODO: 对指定的sid相关的会话的引用计数减一但减到0时销毁
Json::FastWriter json_writer;
//Json::FastWriter json_writer;
Json::Value jreq;
jreq["method"] = "session_end";
jreq["param"]["rid"] = record_id;
jreq["param"]["code"] = ret_code;
ex_astr json_param;
json_param = json_writer.write(jreq);
//json_param = json_writer.write(jreq);
Json::StreamWriterBuilder jwb;
std::unique_ptr<Json::StreamWriter> jwriter(jwb.newStreamWriter());
ex_aoss os;
jwriter->write(jreq, &os);
json_param = os.str();
ex_astr param;
ts_url_encode(json_param.c_str(), param);