Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/rpc_lite_server/rpc_lite_server.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ MsgPack::str_t loopback(MsgPack::str_t message){
return message;
}

class multiplier {
public:

multiplier(){}
static int mult(int a, int b){
return a*b;
}
};


void setup() {
Serial1.begin(115200);
transport.begin();
Expand All @@ -24,6 +34,8 @@ void setup() {

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

}

Expand Down
86 changes: 1 addition & 85 deletions src/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "MsgPack.h"
#include "transport.h"
#include "dispatcher.h"
#include "rpclite_utils.h"


#define NO_MSG -1
Expand Down Expand Up @@ -293,91 +294,6 @@ class RpcDecoder {
return 0;
}

bool unpackObject(MsgPack::Unpacker& unpacker){

if (unpacker.isNil()){
static MsgPack::object::nil_t nil;
return unpacker.deserialize(nil);
}
if (unpacker.isBool()){
static bool b;
return unpacker.deserialize(b);
}
if (unpacker.isUInt() || unpacker.isInt()){
static int integer;
return unpacker.deserialize(integer);
}
if (unpacker.isFloat32()){
static float num32;
return unpacker.deserialize(num32);
}
if (unpacker.isFloat64()){
static double num64;
return unpacker.deserialize(num64);
}
if (unpacker.isStr()){
static MsgPack::str_t string;
return unpacker.deserialize(string);
}
if (unpacker.isBin()){
static MsgPack::bin_t<uint8_t> bytes;
return unpacker.deserialize(bytes);
}
if (unpacker.isArray()){
static size_t arr_sz;
return unpackArray(unpacker, arr_sz);
}
if (unpacker.isMap()){
static size_t map_sz;
return unpackMap(unpacker, map_sz);
}
if (unpacker.isFixExt() || unpacker.isExt()){
static MsgPack::object::ext e;
return unpacker.deserialize(e);
}
if (unpacker.isTimestamp()){
static MsgPack::object::timespec t;
return unpacker.deserialize(t);
}

return false;
}

bool unpackArray(MsgPack::Unpacker& unpacker, size_t& size) {

static MsgPack::arr_size_t sz;
unpacker.deserialize(sz);

size = 0;
for (size_t i=0; i<sz.size(); i++){
if (unpackObject(unpacker)){
size++;
} else {
return false;
}
}

return true;

}

bool unpackMap(MsgPack::Unpacker& unpacker, size_t& size) {
static MsgPack::map_size_t sz;
unpacker.deserialize(sz);

size = 0;
for (size_t i=0; i<sz.size(); i++){
if (unpackObject(unpacker) && unpackObject(unpacker)){ // must unpack key&value
size++;
} else {
return false;
}
}

return true;

}

};

#endif
99 changes: 98 additions & 1 deletion src/rpclite_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,115 @@
#define RPCLITE_UTILS_H

#include <tuple>
#include <utility>
#include <utility>

namespace RpcUtils {
namespace detail {


///////////////////////////////////////
/// --- deserialization helpers --- ///
///////////////////////////////////////

bool unpackObject(MsgPack::Unpacker& unpacker);

bool unpackArray(MsgPack::Unpacker& unpacker, size_t& size) {

static MsgPack::arr_size_t sz;
unpacker.deserialize(sz);

size = 0;
for (size_t i=0; i<sz.size(); i++){
if (unpackObject(unpacker)){
size++;
} else {
return false;
}
}

return true;

}

bool unpackMap(MsgPack::Unpacker& unpacker, size_t& size) {
static MsgPack::map_size_t sz;
unpacker.deserialize(sz);

size = 0;
for (size_t i=0; i<sz.size(); i++){
if (unpackObject(unpacker) && unpackObject(unpacker)){ // must unpack key&value
size++;
} else {
return false;
}
}

return true;

}

bool unpackObject(MsgPack::Unpacker& unpacker){

if (unpacker.isNil()){
static MsgPack::object::nil_t nil;
return unpacker.deserialize(nil);
}
if (unpacker.isBool()){
static bool b;
return unpacker.deserialize(b);
}
if (unpacker.isUInt() || unpacker.isInt()){
static int integer;
return unpacker.deserialize(integer);
}
if (unpacker.isFloat32()){
static float num32;
return unpacker.deserialize(num32);
}
if (unpacker.isFloat64()){
static double num64;
return unpacker.deserialize(num64);
}
if (unpacker.isStr()){
static MsgPack::str_t string;
return unpacker.deserialize(string);
}
if (unpacker.isBin()){
static MsgPack::bin_t<uint8_t> bytes;
return unpacker.deserialize(bytes);
}
if (unpacker.isArray()){
static size_t arr_sz;
return unpackArray(unpacker, arr_sz);
}
if (unpacker.isMap()){
static size_t map_sz;
return unpackMap(unpacker, map_sz);
}
if (unpacker.isFixExt() || unpacker.isExt()){
static MsgPack::object::ext e;
return unpacker.deserialize(e);
}
if (unpacker.isTimestamp()){
static MsgPack::object::timespec t;
return unpacker.deserialize(t);
}

return false;
}

template<typename T>
bool deserialize_single(MsgPack::Unpacker& unpacker, T& value) {
if (!unpacker.unpackable(value)) return false;
unpacker.deserialize(value);
return true;
}


/////////////////////////////
/// --- tuple helpers --- ///
/////////////////////////////

template<std::size_t I = 0, typename... Ts>
typename std::enable_if<I == sizeof...(Ts), bool>::type
deserialize_tuple(MsgPack::Unpacker&, std::tuple<Ts...>&) {
Expand Down