Skip to content

Commit aad3d04

Browse files
committed
mod: stripped smart pointer dependency in dispatcher. nano esp32 compatible server
1 parent 943c15d commit aad3d04

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/dispatcher.h

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,37 @@
55
#include "wrapper.h"
66
#include "error.h"
77

8+
struct DispatchEntry {
9+
MsgPack::str_t name;
10+
IFunctionWrapper* fn;
11+
};
12+
13+
template<size_t N>
814
class RpcFunctionDispatcher {
9-
public:
10-
template<typename F>
11-
void bind(const MsgPack::str_t& name, F&& func) {
12-
_functions[name] = std::make_shared<decltype(wrap(std::forward<F>(func)))>(wrap(std::forward<F>(func)));
13-
}
14-
15-
bool call(const MsgPack::str_t& name, MsgPack::Unpacker& unpacker, MsgPack::Packer& packer) {
16-
auto it = _functions.find(name);
17-
if (it != _functions.end()) {
18-
return (*(it->second))(unpacker, packer);
15+
public:
16+
template<typename F>
17+
void bind(MsgPack::str_t name, F&& f) {
18+
//assert(_count < N);
19+
static auto wrapper = wrap(std::forward<F>(f));
20+
_entries[_count++] = {name, &wrapper};
21+
}
22+
23+
bool call(MsgPack::str_t name, MsgPack::Unpacker& unpacker, MsgPack::Packer& packer) {
24+
for (size_t i = 0; i < _count; ++i) {
25+
if (_entries[i].name == name) {
26+
return (*_entries[i].fn)(unpacker, packer);
1927
}
28+
}
2029

21-
RpcError not_found(FUNCTION_NOT_FOUND_ERR, name);
22-
MsgPack::object::nil_t nil;
23-
packer.serialize(not_found, nil);
30+
// handle not found
31+
MsgPack::object::nil_t nil;
32+
packer.serialize(RpcError(FUNCTION_NOT_FOUND_ERR, name), nil);
33+
return false;
34+
}
2435

25-
return false;
26-
}
27-
28-
private:
29-
std::map<MsgPack::str_t, std::shared_ptr<IFunctionWrapper>> _functions;
30-
};
36+
private:
37+
DispatchEntry _entries[N];
38+
size_t _count = 0;
39+
};
3140

3241
#endif

src/server.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "wrapper.h"
1111
#include "dispatcher.h"
1212

13+
#define MAX_CALLBACKS 100
14+
1315
class RPCServer {
1416
ITransport& transport;
1517

@@ -58,7 +60,7 @@ class RPCServer {
5860

5961
private:
6062

61-
RpcFunctionDispatcher dispatcher;
63+
RpcFunctionDispatcher<MAX_CALLBACKS> dispatcher;
6264
MsgPack::Unpacker unpacker;
6365
MsgPack::Packer packer;
6466

src/wrapper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
#define RPCLITE_WRAPPER_H
33

44
#include "error.h"
5+
6+
#ifdef HANDLE_RPC_ERRORS
57
#include <stdexcept>
8+
#endif
69

710
//TODO maybe use arx::function_traits
811

0 commit comments

Comments
 (0)