Skip to content

Commit

Permalink
fix: Fix a bug for rpc logic, will cause promise error.
Browse files Browse the repository at this point in the history
  • Loading branch information
liufang-robot committed Dec 21, 2022
1 parent e54ab8e commit ecbb4b9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sdk/src/jsonrpc_connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ int JSONRpcConnector::CallRpc(const std::string & method, const std::string & re
std::string jsonrpc_resp;
jsonrpc_req = ToJSONRpcReqString(call_jsonrpc_id, method, req_data_str);
// std::cout<<"jsonrpc_req id:"<<call_jsonrpc_id<<" "<<method<<"\n";
auto future = endpoint_.createFuture(id_, call_jsonrpc_id);
if(!endpoint_.send(id_, jsonrpc_req))
{
throw std::runtime_error("Send Jsonrpc request failed!");
}
auto future = endpoint_.createFuture(id_, call_jsonrpc_id);
}
auto resq_data = future.get();
if(resp_data_str)
{
Expand Down
9 changes: 7 additions & 2 deletions sdk/src/websocket.hh
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace lebai

void onMessage(websocketpp::connection_hdl, WSClient::message_ptr msg)
{
std::lock_guard<std::mutex> guard(promises_map_mutex_);
std::string message_str = msg->get_payload();
int callback_jsonrpc_id;
int error_code;
Expand All @@ -90,7 +91,7 @@ namespace lebai
return;
}
else
{
{
if(promises_.find(callback_jsonrpc_id) != promises_.end())
{
if(ret == JSONRpcRespParseResult::kResult)
Expand All @@ -103,7 +104,9 @@ namespace lebai
promises_[callback_jsonrpc_id]->set_value(std::make_tuple(error_code, resp_data_str));
promises_.erase(callback_jsonrpc_id);
}

}
else{
// Should not happen
}
}
}
Expand All @@ -120,6 +123,7 @@ namespace lebai

std::future<std::tuple<int, std::string>> createPromise(int rpc_id)
{
std::lock_guard<std::mutex> guard(promises_map_mutex_);
promises_[rpc_id] = std::make_unique<std::promise<std::tuple<int, std::string>>>();
return promises_[rpc_id]->get_future();;
}
Expand All @@ -137,6 +141,7 @@ namespace lebai
std::string uri_;
std::string server_;
std::string error_reason_;
std::mutex promises_map_mutex_;
};

// std::ostream& operator<<(std::ostream& out, ConnectionMetadata const& data) {
Expand Down

0 comments on commit ecbb4b9

Please sign in to comment.