void
gameSocket::onRpcMessage(_protobuf_CodeResult& res){
std::lock_guard<std::mutex> lock(g_ctxs_mutex);
auto
it = g_ctxs.find(res.rpcindex());
if
(it != g_ctxs.end()){
it->second->res = std::make_shared<_protobuf_CodeResult>(res);
it->second->notify();
return
;
}
}
gameSocket::ResponsePtr gameSocket::callRpc(
const
RequestPtr& req,
int
timeoutMs){
static
std::atomic<uint64_t> s_id = ATOMIC_VAR_INIT(1);
req->set_rpcindex(++s_id);
auto
ctx = std::make_shared<gameSocket::ProtoRpcContext>();
ctx->req = req;
{
std::lock_guard<std::mutex> lock(g_ctxs_mutex);
g_ctxs[req->rpcindex()] = ctx;
}
std::string messageReq = req->SerializeAsString();
safePrintfLine(
"callRpc debugString:{}"
,req->DebugString());
gameSocket::wsClient.send(messageReq);
if
(!ctx->wait(timeoutMs)) {
}
ResponsePtr res = ctx->res;
std::lock_guard<std::mutex> lock(g_ctxs_mutex);
eg::uint64 rpcindex = req->rpcindex();
gameSocket::g_ctxs.erase(rpcindex);
return
res;
}
vector<std::string> gameSocket::call_GetDir(std::string_view dir,std::string_view extension,
bool
deep){
try
{
_protobuf_CodeRequest req;
req.set_cmd(TYPE_protobuf_CodeType::CodeType_rpc);
req.set_methed(
"GetDir"
);
_protobuf_RPC_GetFilesByDir params;
params.set_dir(dir.data());
params.set_extensionname(extension.data());
params.set_deep(deep);
req.add_params()->assign(params.SerializeAsString());
RequestPtr reqPtr = std::make_shared<_protobuf_CodeRequest>(req);
auto
s = callRpc(reqPtr,10*1000);
if
(!s){
return
{};
}
std::vector<std::string> result;
for
(
int
i = 0; i < s->result_size(); i++){
result.emplace_back(s->result(i));
}
return
result;
}
catch
(std::exception& e){
MySdk::ErrorOutExit(
"call_GetDir Error:{}"
,e.what());
}
return
{};
}
std::string gameSocket::call_GetHash(std::string_view file,std::string_view hashType){
try
{
if
(hashType!=
"md5"
&&hashType!=
"tiger"
&&hashType!=
"sha256"
){
MySdk::ErrorOutExit(
"call_GetHash 获取服务器哈希值 方法错误:{}"
,hashType);
}
_protobuf_CodeRequest req;
req.set_cmd(TYPE_protobuf_CodeType::CodeType_rpc);
req.set_methed(
"GetHash"
);
req.add_params()->assign(file);
req.add_params()->assign(hashType);
RequestPtr reqPtr = std::make_shared<_protobuf_CodeRequest>(req);
auto
s = callRpc(reqPtr,10 * 1000);
if
(!s){
return
{};
}
if
(s->result_size()==0) {
return
{};
}
return
s->result(0);
}
catch
(std::exception& e){
MySdk::ErrorOutExit(
"GetHash Error:{}"
,e.what());
}
return
{};
}