WebCFace 2.5.2
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
pack.h
Go to the documentation of this file.
1#pragma once
2#ifdef MSGPACK_DEFINE_MAP
3#error "webcface/common/internal/pack.h must be included first"
4#endif
5
6#include <msgpack.hpp>
7#include <string>
8#include <cstdint>
9#include <spdlog/logger.h>
10#include <utf8.h>
12#include "./base.h"
13
14MSGPACK_ADD_ENUM(webcface::ValType)
15
17namespace message {
18struct Ping;
19struct PingStatusReq;
20
27template <typename T>
28struct Entry : public MessageBase<T::kind + MessageKind::entry> {
29 unsigned int member_id = 0;
31 MSGPACK_DEFINE_MAP(MSGPACK_NVP("m", member_id), MSGPACK_NVP("f", field))
32};
39template <typename T>
40struct Req : public MessageBase<T::kind + MessageKind::req> {
43 unsigned int req_id = 0;
44 MSGPACK_DEFINE_MAP(MSGPACK_NVP("i", req_id), MSGPACK_NVP("M", member),
45 MSGPACK_NVP("f", field))
46};
47struct Image;
48template <>
49struct Req<Image>;
50
55std::vector<std::pair<int, std::shared_ptr<void>>>
56unpack(const std::string &message,
57 const std::shared_ptr<spdlog::logger> &logger);
58
63template <typename T>
64std::string packSingle(const T &obj) {
65 msgpack::type::tuple<int, T> src(static_cast<int>(T::kind), obj);
66 std::stringstream buffer;
67 msgpack::pack(buffer, src);
68 return buffer.str();
69}
70
75template <typename T>
76void pack(std::stringstream &buffer, int &len, const T &obj) {
77 msgpack::pack(buffer, static_cast<int>(T::kind));
78 msgpack::pack(buffer, obj);
79 len += 2;
80}
81
82inline std::string packDone(std::stringstream &buffer, int len) {
83 std::stringstream buffer2;
84 msgpack::packer packer(buffer2);
85 packer.pack_array(len);
86 buffer2 << buffer.rdbuf();
87 return buffer2.str();
88}
89
90} // namespace message
92
93namespace msgpack {
94MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
95 namespace adaptor {
96
97 template <>
98 struct convert<webcface::SharedString> {
99 msgpack::object const &operator()(msgpack::object const &o,
100 webcface::SharedString &v) const {
101 v = webcface::SharedString::fromU8String(utf8::replace_invalid(
102 std::string(o.via.bin.ptr, o.via.bin.size)));
103 return o;
104 }
105 };
106 template <>
107 struct pack<webcface::SharedString> {
108 template <typename Stream>
109 msgpack::packer<Stream> &operator()(msgpack::packer<Stream> &o,
110 const webcface::SharedString &v) {
111 o.pack(v.u8String());
112 return o;
113 }
114 };
115
116 template <>
117 struct convert<webcface::ValAdaptor> {
118 msgpack::object const &operator()(msgpack::object const &o,
119 webcface::ValAdaptor &v) const {
120 switch (o.type) {
121 case msgpack::type::FLOAT32:
122 case msgpack::type::FLOAT64:
123 v = o.via.f64;
124 break;
125 case msgpack::type::POSITIVE_INTEGER:
126 v = o.via.u64;
127 break;
128 case msgpack::type::NEGATIVE_INTEGER:
129 v = o.via.i64;
130 break;
131 case msgpack::type::BOOLEAN:
132 v = o.via.boolean;
133 break;
134 case msgpack::type::BIN:
135 case msgpack::type::STR:
136 v = webcface::SharedString::fromU8String(utf8::replace_invalid(
137 std::string(o.via.bin.ptr, o.via.bin.size)));
138 break;
139 default:
140 throw msgpack::type_error();
141 }
142 return o;
143 }
144 };
145 template <>
146 struct pack<webcface::ValAdaptor> {
147 template <typename Stream>
148 msgpack::packer<Stream> &operator()(msgpack::packer<Stream> &o,
149 const webcface::ValAdaptor &v) {
150 switch (v.valType()) {
152 o.pack(static_cast<bool>(v));
153 break;
155 o.pack(static_cast<std::int64_t>(v));
156 break;
158 o.pack(static_cast<double>(v));
159 break;
161 default:
162 o.pack(v.asU8StringRef());
163 break;
164 }
165 return o;
166 }
167 };
168
169 template <typename T>
170 struct EmptyConvert {
171 msgpack::object const &operator()(msgpack::object const &o, T &) const {
172 return o;
173 }
174 };
175 template <typename T>
176 struct EmptyPack {
177 template <typename Stream>
178 msgpack::packer<Stream> &operator()(msgpack::packer<Stream> &o,
179 const T &) {
180 o.pack_map(0);
181 return o;
182 }
183 };
184 template <>
185 struct convert<webcface::message::Ping>
186 : public EmptyConvert<webcface::message::Ping> {};
187 template <>
188 struct convert<webcface::message::PingStatusReq>
189 : public EmptyConvert<webcface::message::PingStatusReq> {};
190 template <>
191 struct pack<webcface::message::Ping>
192 : public EmptyPack<webcface::message::Ping> {};
193 template <>
194 struct pack<webcface::message::PingStatusReq>
195 : public EmptyPack<webcface::message::PingStatusReq> {};
196
197 } // namespace adaptor
198}
199} // namespace msgpack
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:69
const std::string & u8String() const
Definition encoding.cc:104
static SharedString fromU8String(std::string_view u8s)
Definition encoding.cc:63
数値、文字列などの値を相互変換するクラス
Definition val_adaptor.h:87
const std::string & asU8StringRef() const
Definition val_adaptor.cc:94
ValType valType() const
Definition val_adaptor.h:166
#define MSGPACK_DEFINE_MAP(...)
Definition canvas2d.h:10
webcface::ValAdaptor ValAdaptor
Definition val_adaptor.h:350
webcface::SharedString SharedString
Definition encoding.h:127
std::string packDone(std::stringstream &buffer, int len)
Definition pack.h:82
void pack(std::stringstream &buffer, int &len, const T &obj)
メッセージをシリアル化しbufferに追加
Definition pack.h:76
std::vector< std::pair< int, std::shared_ptr< void > > > unpack(const std::string &message, const std::shared_ptr< spdlog::logger > &logger)
msgpackのメッセージをパースし返す
Definition message.cc:31
std::string packSingle(const T &obj)
メッセージ1つを要素数2の配列としてシリアル化
Definition pack.h:64
Definition arg.h:14
ValType
引数や戻り値の型を表すenum
Definition val_adaptor.h:21
server->client 新しいvalueなどの報告
Definition pack.h:28
SharedString field
Definition pack.h:30
Definition image.h:59
型からkindを取得するためだけのベースクラス
Definition base.h:53
client->server 以降Recvを送るようリクエスト
Definition pack.h:40
SharedString field
Definition pack.h:42
SharedString member
Definition pack.h:41
#define WEBCFACE_NS_END
Definition webcface-config.h:104
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:103