Skip to content

Commit c16c181

Browse files
committed
mod: thread safety client holds a call-local msg_id
1 parent 124336f commit c16c181

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/client.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
class RPCClient {
99
RpcDecoder<>* decoder = nullptr;
10-
uint32_t _waiting_msg_id;
1110

1211
public:
1312
RpcError lastError;
@@ -29,14 +28,16 @@ class RPCClient {
2928
template<typename RType, typename... Args>
3029
bool call(const MsgPack::str_t method, RType& result, Args&&... args) {
3130

32-
if(!send_rpc(method, std::forward<Args>(args)...)) {
31+
uint32_t msg_id_wait;
32+
33+
if(!send_rpc(method, msg_id_wait, std::forward<Args>(args)...)) {
3334
lastError.code = GENERIC_ERR;
3435
lastError.traceback = "Failed to send RPC call";
3536
return false;
3637
}
3738

3839
// blocking call
39-
while (!get_response(result)){
40+
while (!get_response(msg_id_wait, result)){
4041
//delay(1);
4142
}
4243

@@ -45,21 +46,21 @@ class RPCClient {
4546
}
4647

4748
template<typename... Args>
48-
bool send_rpc(const MsgPack::str_t method, Args&&... args) {
49+
bool send_rpc(const MsgPack::str_t method, uint32_t& wait_id, Args&&... args) {
4950
uint32_t msg_id;
5051
if (decoder->send_call(CALL_MSG, method, msg_id, std::forward<Args>(args)...)) {
51-
_waiting_msg_id = msg_id;
52+
wait_id = msg_id;
5253
return true;
5354
}
5455
return false;
5556
}
5657

5758
template<typename RType>
58-
bool get_response(RType& result) {
59+
bool get_response(const uint32_t wait_id, RType& result) {
5960
RpcError tmp_error;
6061
decoder->decode();
6162

62-
if (decoder->get_response(_waiting_msg_id, result, tmp_error)) {
63+
if (decoder->get_response(wait_id, result, tmp_error)) {
6364
lastError.code = tmp_error.code;
6465
lastError.traceback = tmp_error.traceback;
6566
return true;

0 commit comments

Comments
 (0)