Skip to content

Commit

Permalink
move files to rest_rpc dir
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Aug 26, 2020
1 parent f86897a commit 3953ce0
Show file tree
Hide file tree
Showing 12 changed files with 3,393 additions and 0 deletions.
52 changes: 52 additions & 0 deletions include/rest_rpc/client_util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once
#include <tuple>
#if __cplusplus > 201402L
#include <string_view>
using string_view = std::string_view;
#else
#ifdef ASIO_STANDALONE
#include "string_view.hpp"
using namespace nonstd;
#else
#include <boost/utility/string_view.hpp>
using string_view = boost::string_view;
#endif
#endif

#include "codec.h"

namespace rest_rpc {
inline bool has_error(string_view result) {
if (result.empty()) {
return true;
}

rpc_service::msgpack_codec codec;
auto tp = codec.unpack<std::tuple<int>>(result.data(), result.size());

return std::get<0>(tp) != 0;
}

template<typename T>
inline T get_result(string_view result) {
rpc_service::msgpack_codec codec;
auto tp = codec.unpack<std::tuple<int, T>>(result.data(), result.size());
return std::get<1>(tp);
}


inline std::string get_error_msg(string_view result) {
rpc_service::msgpack_codec codec;
auto tp = codec.unpack<std::tuple<int, std::string>>(result.data(), result.size());
return std::get<1>(tp);
}

template<typename T>
inline T as(string_view result) {
if (has_error(result)) {
throw std::logic_error(get_error_msg(result));
}

return get_result<T>(result);
}
}
49 changes: 49 additions & 0 deletions include/rest_rpc/codec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef REST_RPC_CODEC_H_
#define REST_RPC_CODEC_H_

#include <msgpack.hpp>

namespace rest_rpc {
namespace rpc_service {

using buffer_type = msgpack::sbuffer;
struct msgpack_codec {
const static size_t init_size = 2 * 1024;

template<typename... Args>
static buffer_type pack_args(Args&&... args) {
buffer_type buffer(init_size);
msgpack::pack(buffer, std::forward_as_tuple(std::forward<Args>(args)...));
return buffer;
}

template<typename Arg, typename... Args,
typename = typename std::enable_if<std::is_enum<Arg>::value>::type>
static std::string pack_args_str(Arg arg, Args&&... args) {
buffer_type buffer(init_size);
msgpack::pack(buffer, std::forward_as_tuple((int)arg, std::forward<Args>(args)...));
return std::string(buffer.data(), buffer.size());
}

template<typename T>
buffer_type pack(T&& t) const {
buffer_type buffer;
msgpack::pack(buffer, std::forward<T>(t));
return buffer;
}

template<typename T>
T unpack(char const* data, size_t length) {
try {
msgpack::unpack(&msg_, data, length);
return msg_.get().as<T>();
} catch (...) { throw std::invalid_argument("unpack failed: Args not match!"); }
}

private:
msgpack::unpacked msg_;
};
} // namespace rpc_service
} // namespace rest_rpc

#endif // REST_RPC_CODEC_H_
Loading

0 comments on commit 3953ce0

Please sign in to comment.