-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmprpcmessage.hpp
409 lines (338 loc) · 10.9 KB
/
mprpcmessage.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// solid/frame/mprpc/mprpcservice.hpp
//
// Copyright (c) 2007, 2008, 2013 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#pragma once
#include <memory>
#include <optional>
#include <type_traits>
#include "solid/system/common.hpp"
#include "solid/system/exception.hpp"
#include "solid/utility/cacheable.hpp"
#include "solid/utility/function.hpp"
#include "solid/utility/typetraits.hpp"
#include "solid/frame/mprpc/mprpcid.hpp"
#include "solid/frame/mprpc/mprpcmessageflags.hpp"
#include "solid/reflection/v1/reflection.hpp"
#if !defined(SOLID_MPRPC_USE_SHARED_PTR_MESSAGE)
#include "solid/utility/intrusiveptr.hpp"
#endif
namespace solid {
namespace frame {
namespace mprpc {
class Service;
class Connection;
class ConnectionContext;
struct MessageRelayHeader {
using GroupIdT = uint32_t;
using ReplicaIdT = uint16_t;
GroupIdT group_id_ = 0;
ReplicaIdT replica_id_ = 0;
MessageRelayHeader() = default;
MessageRelayHeader(const GroupIdT _group_id, const ReplicaIdT _replica_id = 0)
: group_id_(_group_id)
, replica_id_(_replica_id)
{
}
void clear()
{
group_id_ = InvalidIndex();
replica_id_ = 0;
}
SOLID_REFLECT_V1(_rs, _rthis, _rctx)
{
if constexpr (std::decay_t<decltype(_rs)>::is_const_reflector) {
_rs.add(_rctx.pmessage_relay_header_->group_id_, _rctx, 1, "group_id");
_rs.add(_rctx.pmessage_relay_header_->replica_id_, _rctx, 2, "replica_id");
} else {
_rs.add(_rthis.group_id_, _rctx, 1, "group_id");
_rs.add(_rthis.replica_id_, _rctx, 2, "replica_id");
}
}
};
using OptionalMessageRelayHeaderT = std::optional<MessageRelayHeader>;
std::ostream& operator<<(std::ostream& _ros, const MessageRelayHeader& _header);
std::ostream& operator<<(std::ostream& _ros, const OptionalMessageRelayHeaderT& _header);
struct MessageHeader {
using FlagsT = MessageFlagsValueT;
FlagsT flags_{0};
RequestId sender_request_id_;
RequestId recipient_request_id_;
MessageRelayHeader relay_;
static MessageFlagsT fetch_state_flags(const MessageFlagsT& _flags)
{
static const MessageFlagsT state_flags{MessageFlagsE::OnPeer, MessageFlagsE::BackOnSender, MessageFlagsE::Relayed};
return _flags & state_flags;
}
MessageHeader() = default;
MessageHeader(
const MessageHeader& _rmsgh)
: flags_(fetch_state_flags(_rmsgh.flags_).toUnderlyingType())
, sender_request_id_(_rmsgh.sender_request_id_)
, recipient_request_id_(_rmsgh.recipient_request_id_)
{
}
MessageHeader& operator=(MessageHeader&& _umh) noexcept
{
flags_ = _umh.flags_;
sender_request_id_ = _umh.sender_request_id_;
recipient_request_id_ = _umh.recipient_request_id_;
relay_ = std::move(_umh.relay_);
return *this;
}
MessageHeader& operator=(const MessageHeader& _rmh)
{
flags_ = fetch_state_flags(_rmh.flags_).toUnderlyingType();
sender_request_id_ = _rmh.sender_request_id_;
recipient_request_id_ = _rmh.recipient_request_id_;
relay_ = _rmh.relay_;
return *this;
}
void clear()
{
flags_ = 0;
sender_request_id_.clear();
recipient_request_id_.clear();
relay_.clear();
}
SOLID_REFLECT_V1(_rr, _rthis, _rctx)
{
if constexpr (std::decay_t<decltype(_rr)>::is_const_reflector) {
const MessageFlagsValueT tmp = _rctx.message_flags_.toUnderlyingType();
_rr.add(tmp, _rctx, 1, "flags");
_rr.add(_rctx.request_id_.index, _rctx, 2, "sender_request_index");
_rr.add(_rctx.request_id_.unique, _rctx, 3, "sender_request_unique");
_rr.add(_rthis.sender_request_id_.index, _rctx, 4, "recipient_request_index");
_rr.add(_rthis.sender_request_id_.unique, _rctx, 5, "recipient_request_unique");
if (_rctx.message_flags_.has(MessageFlagsE::Relayed)) {
_rr.add(_rthis.relay_, _rctx, 6, "relay");
}
} else {
_rr.add(_rthis.flags_, _rctx, 1, "flags");
_rr.add(_rthis.sender_request_id_.index, _rctx, 2, "sender_request_index");
_rr.add(_rthis.sender_request_id_.unique, _rctx, 3, "sender_request_unique");
_rr.add(_rthis.recipient_request_id_.index, _rctx, 4, "recipient_request_index");
_rr.add(_rthis.recipient_request_id_.unique, _rctx, 5, "recipient_request_unique");
_rr.add(
[&_rthis](auto& _rr, auto& _rctx) {
const MessageFlagsT flags(_rthis.flags_);
if (flags.has(MessageFlagsE::Relayed)) {
_rr.add(_rthis.relay_, _rctx, 6, "relay");
}
},
_rctx);
}
}
};
#if defined(SOLID_MPRPC_USE_SHARED_PTR_MESSAGE)
struct Message : SharedCacheable {
#else
struct Message : IntrusiveCacheable {
#endif
using FlagsT = MessageFlagsValueT;
inline static bool is_synchronous(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::Synchronous);
}
inline static bool is_asynchronous(const MessageFlagsT& _flags)
{
return !is_synchronous(_flags);
}
inline static bool is_awaiting_response(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::AwaitResponse);
}
inline static bool is_request(const MessageFlagsT& _flags)
{
return is_awaiting_response(_flags);
}
inline static bool is_idempotent(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::Idempotent);
}
inline static bool is_started_send(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::StartedSend);
}
inline static bool is_done_send(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::DoneSend);
}
inline static bool is_canceled(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::Canceled);
}
inline static bool is_one_shot(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::OneShotSend);
}
inline static bool is_response(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::Response);
}
inline static bool is_response_part(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::ResponsePart);
}
inline static bool is_response_last(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::ResponseLast);
}
inline static bool is_on_sender(const MessageFlagsT& _flags)
{
return !is_on_peer(_flags) && !is_back_on_sender(_flags);
}
inline static bool is_on_peer(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::OnPeer);
}
inline static bool is_back_on_sender(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::BackOnSender);
}
inline static bool is_back_on_peer(const MessageFlagsT& _flags)
{
return is_on_peer(_flags) && is_back_on_sender(_flags);
}
inline static bool is_relayed(const MessageFlagsT& _flags)
{
return _flags.has(MessageFlagsE::Relayed);
}
inline static MessageFlagsT clear_state_flags(MessageFlagsT _flags)
{
_flags.reset(MessageFlagsE::OnPeer).reset(MessageFlagsE::BackOnSender).reset(MessageFlagsE::Relayed);
return _flags;
}
inline static MessageFlagsT state_flags(const MessageFlagsT& _flags)
{
return MessageHeader::fetch_state_flags(_flags);
}
inline static MessageFlagsT update_state_flags(MessageFlagsT _flags)
{
if (is_on_sender(_flags)) {
return _flags | MessageFlagsE::OnPeer;
} else if (is_back_on_peer(_flags)) {
return (_flags | MessageFlagsE::BackOnSender).reset(MessageFlagsE::OnPeer);
} else if (is_on_peer(_flags)) {
return (_flags | MessageFlagsE::BackOnSender).reset(MessageFlagsE::OnPeer);
} else /* if(is_back_on_sender(_flags))*/ {
return _flags | MessageFlagsE::OnPeer;
}
}
Message()
{
}
Message(Message const& _rmsg)
: header_(_rmsg.header_)
{
}
virtual ~Message();
void header(MessageHeader&& _umh)
{
header_ = std::move(_umh);
}
void header(const MessageHeader& _umh)
{
header_ = _umh;
}
void header(frame::mprpc::ConnectionContext& _rctx);
const MessageHeader& header() const
{
return header_;
}
Message& operator=(const Message& _other)
{
header(_other.header_);
return *this;
}
bool isOnSender() const
{
return is_on_sender(flags());
}
bool isOnPeer() const
{
return is_on_peer(flags());
}
bool isBackOnSender() const
{
return is_back_on_sender(flags());
}
bool isBackOnPeer() const
{
return is_back_on_peer(flags());
}
bool isRelayed() const
{
return is_relayed(flags());
}
bool isResponse() const
{
return is_response(flags());
}
bool isResponsePart() const
{
return is_response_part(flags());
}
bool isResponseLast() const
{
return is_response_last(flags());
}
const auto& relay() const
{
return header_.relay_;
}
void clearStateFlags()
{
header_.flags_ = clear_state_flags(header_.flags_).toUnderlyingType();
}
void clearHeader()
{
header_.clear();
}
MessageFlagsT flags() const
{
return MessageFlagsT(header_.flags_);
}
RequestId const& senderRequestId() const
{
return header_.sender_request_id_;
}
private:
friend class Service;
friend class TestEntryway;
friend class Connection;
friend class MessageWriter;
RequestId const& requestId() const
{
return header_.recipient_request_id_;
}
private:
MessageHeader header_;
};
#if defined(SOLID_MPRPC_USE_SHARED_PTR_MESSAGE)
template <class Msg = Message>
using MessagePointerT = std::shared_ptr<Msg>;
template <class Msg, class... Args>
MessagePointerT<Msg> make_message(Args&&... _args)
{
return std::make_shared<Msg>(std::forward<Args>(_args)...);
}
#else
template <class Msg = Message>
using MessagePointerT = IntrusivePtr<Msg>;
template <class Msg, class... Args>
MessagePointerT<Msg> make_message(Args&&... _args)
{
return make_intrusive<Msg>(std::forward<Args>(_args)...);
}
#endif
using MessageCompleteFunctionT = solid_function_t(void(
ConnectionContext&, MessagePointerT<>&, MessagePointerT<>&, ErrorConditionT const&));
} // namespace mprpc
} // namespace frame
} // namespace solid