Skip to content

Commit b37dd4b

Browse files
committed
feat: asynced both RPC operations (send_rpc/get_response) in RpcResult
1 parent 5dc55dd commit b37dd4b

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

src/bridge.h

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
This Source Code Form is subject to the terms of the Mozilla Public
77
License, v. 2.0. If a copy of the MPL was not distributed with this
88
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9-
9+
1010
*/
1111

1212
#pragma once
@@ -29,16 +29,33 @@
2929

3030
void updateEntryPoint(void *, void *, void *);
3131

32+
template<typename... Args>
3233
class RpcResult {
3334

35+
private:
36+
std::tuple<Args...> callback_params;
37+
MsgPack::str_t method;
38+
3439
public:
3540
RpcError error;
3641

37-
RpcResult(uint32_t id, RPCClient* c, struct k_mutex* rm, struct k_mutex* wm) : msg_id_wait(id), client(c), read_mutex(rm), write_mutex(wm) {}
42+
RpcResult(const MsgPack::str_t& m, RPCClient* c, struct k_mutex* rm, struct k_mutex* wm, Args&&... args): method(m), client(c), read_mutex(rm), write_mutex(wm), callback_params(std::forward_as_tuple(std::forward<Args>(args)...)) {}
3843

3944
template<typename RType> bool result(RType& result) {
4045
if (_executed) return error.code == NO_ERR;
4146

47+
while (true) {
48+
if (k_mutex_lock(write_mutex, K_MSEC(10)) == 0) {
49+
std::apply([this](const auto&... elems) {
50+
client->send_rpc(method, msg_id_wait, elems...);
51+
}, callback_params);
52+
k_mutex_unlock(write_mutex);
53+
break;
54+
} else {
55+
k_yield();
56+
}
57+
}
58+
4259
while(true) {
4360
if (k_mutex_lock(read_mutex, K_MSEC(10)) == 0 ) {
4461
if (client->get_response(msg_id_wait, result, error)) {
@@ -74,7 +91,7 @@ class RpcResult {
7491
}
7592

7693
private:
77-
uint32_t msg_id_wait;
94+
uint32_t msg_id_wait{};
7895
RPCClient* client;
7996
bool _executed = false;
8097
struct k_mutex* read_mutex;
@@ -150,7 +167,7 @@ class BridgeClass {
150167
}
151168

152169
return server->bind(name, func, "__safe__");
153-
170+
154171
}
155172

156173
void update() {
@@ -171,7 +188,7 @@ class BridgeClass {
171188

172189
// Lock write mutex
173190
while (true) {
174-
191+
175192
if (k_mutex_lock(&write_mutex, K_MSEC(10)) == 0){
176193
server->send_response(req);
177194
k_mutex_unlock(&write_mutex);
@@ -185,21 +202,8 @@ class BridgeClass {
185202
}
186203

187204
template<typename... Args>
188-
RpcResult call(const MsgPack::str_t& method, Args&&... args) {
189-
190-
uint32_t msg_id_wait;
191-
192-
// Lock write mutex
193-
while (true) {
194-
if (k_mutex_lock(&write_mutex, K_MSEC(10)) == 0) {
195-
client->send_rpc(method, msg_id_wait, std::forward<Args>(args)...);
196-
k_mutex_unlock(&write_mutex);
197-
break;
198-
} else {
199-
k_yield();
200-
}
201-
}
202-
return RpcResult{msg_id_wait, client, &read_mutex, &write_mutex};
205+
RpcResult<Args...> call(const MsgPack::str_t& method, Args&&... args) {
206+
return RpcResult<Args...>(method, client, &read_mutex, &write_mutex, std::forward<Args>(args)...);
203207
}
204208

205209

@@ -248,7 +252,7 @@ class BridgeClass {
248252

249253
// Lock write mutex
250254
while (true) {
251-
255+
252256
if (k_mutex_lock(&write_mutex, K_MSEC(10)) == 0){
253257
server->send_response(req);
254258
k_mutex_unlock(&write_mutex);
@@ -258,7 +262,7 @@ class BridgeClass {
258262
}
259263

260264
}
261-
265+
262266
}
263267

264268
friend class BridgeClassUpdater;

0 commit comments

Comments
 (0)