Skip to content

Commit 82dfcc8

Browse files
committed
mod: decluttering decoder.h examples: rpc_lite_server binding lambdas and static class methods
1 parent 40cbc63 commit 82dfcc8

File tree

3 files changed

+113
-88
lines changed

3 files changed

+113
-88
lines changed

examples/rpc_lite_server/rpc_lite_server.ino

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <Arduino_RPClite.h>
22

3-
SerialTransport transport(&Serial1);
3+
SerialTransport transport(&Serial2);
44
RPCServer server(transport);
55

66
int add(int a, int b){
@@ -15,15 +15,27 @@ MsgPack::str_t loopback(MsgPack::str_t message){
1515
return message;
1616
}
1717

18+
class multiplier {
19+
public:
20+
21+
multiplier(){}
22+
static int mult(int a, int b){
23+
return a*b;
24+
}
25+
};
26+
27+
1828
void setup() {
19-
Serial1.begin(115200);
29+
Serial2.begin(115200);
2030
transport.begin();
2131
pinMode(LED_BUILTIN, OUTPUT);
2232
Serial.begin(9600);
2333
while(!Serial);
2434

2535
server.bind("add", add);
2636
server.bind("greet", greet);
37+
server.bind("another_greeting", [] {return MsgPack::str_t ("This is a lambda greeting");});
38+
server.bind("object_multi", &multiplier::mult);
2739

2840
}
2941

src/decoder.h

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "MsgPack.h"
55
#include "transport.h"
66
#include "dispatcher.h"
7+
#include "rpclite_utils.h"
78

89

910
#define NO_MSG -1
@@ -293,91 +294,6 @@ class RpcDecoder {
293294
return 0;
294295
}
295296

296-
bool unpackObject(MsgPack::Unpacker& unpacker){
297-
298-
if (unpacker.isNil()){
299-
static MsgPack::object::nil_t nil;
300-
return unpacker.deserialize(nil);
301-
}
302-
if (unpacker.isBool()){
303-
static bool b;
304-
return unpacker.deserialize(b);
305-
}
306-
if (unpacker.isUInt() || unpacker.isInt()){
307-
static int integer;
308-
return unpacker.deserialize(integer);
309-
}
310-
if (unpacker.isFloat32()){
311-
static float num32;
312-
return unpacker.deserialize(num32);
313-
}
314-
if (unpacker.isFloat64()){
315-
static double num64;
316-
return unpacker.deserialize(num64);
317-
}
318-
if (unpacker.isStr()){
319-
static MsgPack::str_t string;
320-
return unpacker.deserialize(string);
321-
}
322-
if (unpacker.isBin()){
323-
static MsgPack::bin_t<uint8_t> bytes;
324-
return unpacker.deserialize(bytes);
325-
}
326-
if (unpacker.isArray()){
327-
static size_t arr_sz;
328-
return unpackArray(unpacker, arr_sz);
329-
}
330-
if (unpacker.isMap()){
331-
static size_t map_sz;
332-
return unpackMap(unpacker, map_sz);
333-
}
334-
if (unpacker.isFixExt() || unpacker.isExt()){
335-
static MsgPack::object::ext e;
336-
return unpacker.deserialize(e);
337-
}
338-
if (unpacker.isTimestamp()){
339-
static MsgPack::object::timespec t;
340-
return unpacker.deserialize(t);
341-
}
342-
343-
return false;
344-
}
345-
346-
bool unpackArray(MsgPack::Unpacker& unpacker, size_t& size) {
347-
348-
static MsgPack::arr_size_t sz;
349-
unpacker.deserialize(sz);
350-
351-
size = 0;
352-
for (size_t i=0; i<sz.size(); i++){
353-
if (unpackObject(unpacker)){
354-
size++;
355-
} else {
356-
return false;
357-
}
358-
}
359-
360-
return true;
361-
362-
}
363-
364-
bool unpackMap(MsgPack::Unpacker& unpacker, size_t& size) {
365-
static MsgPack::map_size_t sz;
366-
unpacker.deserialize(sz);
367-
368-
size = 0;
369-
for (size_t i=0; i<sz.size(); i++){
370-
if (unpackObject(unpacker) && unpackObject(unpacker)){ // must unpack key&value
371-
size++;
372-
} else {
373-
return false;
374-
}
375-
}
376-
377-
return true;
378-
379-
}
380-
381297
};
382298

383299
#endif

src/rpclite_utils.h

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,115 @@
33
#define RPCLITE_UTILS_H
44

55
#include <tuple>
6-
#include <utility>
6+
#include <utility>
77

88
namespace RpcUtils {
99
namespace detail {
1010

11+
12+
///////////////////////////////////////
13+
/// --- deserialization helpers --- ///
14+
///////////////////////////////////////
15+
16+
bool unpackObject(MsgPack::Unpacker& unpacker);
17+
18+
bool unpackArray(MsgPack::Unpacker& unpacker, size_t& size) {
19+
20+
static MsgPack::arr_size_t sz;
21+
unpacker.deserialize(sz);
22+
23+
size = 0;
24+
for (size_t i=0; i<sz.size(); i++){
25+
if (unpackObject(unpacker)){
26+
size++;
27+
} else {
28+
return false;
29+
}
30+
}
31+
32+
return true;
33+
34+
}
35+
36+
bool unpackMap(MsgPack::Unpacker& unpacker, size_t& size) {
37+
static MsgPack::map_size_t sz;
38+
unpacker.deserialize(sz);
39+
40+
size = 0;
41+
for (size_t i=0; i<sz.size(); i++){
42+
if (unpackObject(unpacker) && unpackObject(unpacker)){ // must unpack key&value
43+
size++;
44+
} else {
45+
return false;
46+
}
47+
}
48+
49+
return true;
50+
51+
}
52+
53+
bool unpackObject(MsgPack::Unpacker& unpacker){
54+
55+
if (unpacker.isNil()){
56+
static MsgPack::object::nil_t nil;
57+
return unpacker.deserialize(nil);
58+
}
59+
if (unpacker.isBool()){
60+
static bool b;
61+
return unpacker.deserialize(b);
62+
}
63+
if (unpacker.isUInt() || unpacker.isInt()){
64+
static int integer;
65+
return unpacker.deserialize(integer);
66+
}
67+
if (unpacker.isFloat32()){
68+
static float num32;
69+
return unpacker.deserialize(num32);
70+
}
71+
if (unpacker.isFloat64()){
72+
static double num64;
73+
return unpacker.deserialize(num64);
74+
}
75+
if (unpacker.isStr()){
76+
static MsgPack::str_t string;
77+
return unpacker.deserialize(string);
78+
}
79+
if (unpacker.isBin()){
80+
static MsgPack::bin_t<uint8_t> bytes;
81+
return unpacker.deserialize(bytes);
82+
}
83+
if (unpacker.isArray()){
84+
static size_t arr_sz;
85+
return unpackArray(unpacker, arr_sz);
86+
}
87+
if (unpacker.isMap()){
88+
static size_t map_sz;
89+
return unpackMap(unpacker, map_sz);
90+
}
91+
if (unpacker.isFixExt() || unpacker.isExt()){
92+
static MsgPack::object::ext e;
93+
return unpacker.deserialize(e);
94+
}
95+
if (unpacker.isTimestamp()){
96+
static MsgPack::object::timespec t;
97+
return unpacker.deserialize(t);
98+
}
99+
100+
return false;
101+
}
102+
11103
template<typename T>
12104
bool deserialize_single(MsgPack::Unpacker& unpacker, T& value) {
13105
if (!unpacker.unpackable(value)) return false;
14106
unpacker.deserialize(value);
15107
return true;
16108
}
17109

110+
111+
/////////////////////////////
112+
/// --- tuple helpers --- ///
113+
/////////////////////////////
114+
18115
template<std::size_t I = 0, typename... Ts>
19116
typename std::enable_if<I == sizeof...(Ts), bool>::type
20117
deserialize_tuple(MsgPack::Unpacker&, std::tuple<Ts...>&) {

0 commit comments

Comments
 (0)