forked from qicosmos/rest_rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
3,393 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
Oops, something went wrong.