OnixS C++ CME MDP Conflated TCP Handler 1.3.6
API Documentation
Loading...
Searching...
No Matches
MessageHolder.h
Go to the documentation of this file.
1// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2//
3// This software owned by Onix Solutions Limited [OnixS] and is
4// protected by copyright law and international copyright treaties.
5//
6// Access to and use of the software is governed by the terms of the applicable
7// OnixS Software Services Agreement (the Agreement) and Customer end user license
8// agreements granting a non-assignable, non-transferable and non-exclusive license
9// to use the software for it's own data processing purposes under the terms defined
10// in the Agreement.
11//
12// Except as otherwise granted within the terms of the Agreement, copying or
13// reproduction of any part of this source code or associated reference material
14// to any other location for further reproduction or redistribution, and any
15// amendments to this copyright notice, are expressly prohibited.
16//
17// Any reproduction or redistribution for sale or hiring of the Software not in
18// accordance with the terms of the Agreement is a violation of copyright law.
19//
20
21#pragma once
22
23#include <vector>
24
28
30
33{
35 template <typename SbeMessageType>
36 inline static SbeMessageType createMessage(
37 void* data, MessageSize length, SchemaVersion version)
38 {
39 return SbeMessageType(data, length, version);
40 }
41
43 inline static const char* name() ONIXS_CONFLATEDTCP_NOTHROW
44 {
45 return "FieldsInitPolicy";
46 }
47};
48
51{
53 template <typename SbeMessageType>
54 inline static SbeMessageType createMessage(
55 void* data, MessageSize length, SchemaVersion version)
56 {
57 return SbeMessageType(
58 data, length, SbeMessage::NoFieldsInit(), version);
59 }
60
62 inline static const char* name() ONIXS_CONFLATEDTCP_NOTHROW
63 {
64 return "FieldsNoInitPolicy";
65 }
66};
67
68template <typename MessageType>
70 : public MessageType
71{
73 IsBaseOf<SbeMessage, MessageType>::value));
74
75 typedef MessageType SbeType;
76
77 typedef typename
78 SbeType::EncodedLength
80
82
84 void* data,
85 EncodedLength length,
86 SchemaVersion version)
87 : MessageType(data, length, version)
88 {
89 }
90
92 void* data,
93 EncodedLength length,
95 SchemaVersion version)
96 : MessageType(data, length, version)
97 {
98 }
99
101 void* data,
102 EncodedLength length,
104 : MessageType(data, length)
105 {
106 }
107
109 void* data,
110 EncodedLength length,
114 : MessageType(
115 data,
116 length,
119 {
120 }
121};
122
124template <typename, size_t, typename>
125class MessageHolder;
126template <
127 template <typename, size_t, typename> class HolderType,
128 typename MsgType,
129 size_t MaxMessageSize,
130 typename MessageInitializer>
131inline void toStr(
132 std::string& str,
133 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder)
134{
135 str += "MessageHolder[";
136
137 str += "BufferSize=";
138 toStr(str, holder.BufferSize);
139
140 str += ", MessageInitializer=";
141 str += MessageInitializer::name();
142
143 str += ", ";
144 toStr(str, *holder.header());
145
146 str += ", ";
147 toStr(str, holder.message());
148
149 str += "]";
150}
151
153ONIXS_CONFLATEDTCP_CONST_OR_CONSTEXPR char magicDebugValue = 0x5A;
154
157
161template <
162 typename MessageTypeT,
163 size_t MaxMessageSize =
164 GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
165 typename MessageInitializer = FieldsInitPolicy>
167{
168 typedef typename HeldAdapter<MessageTypeT>::SbeType SbeType;
169
170 ONIXS_CONFLATEDTCP_STATIC_ASSERT_MSG(
171 (MaxMessageSize >= sizeof(MessageHeader)),
172 "MaxMessageSize template parameter is too small");
173
174 ONIXS_CONFLATEDTCP_STATIC_ASSERT_MSG(
175 (MaxMessageSize >= GetMinMessageSize<SbeType>::Size),
176 "The buffer can not fit the message");
177
178 ONIXS_CONFLATEDTCP_STATIC_ASSERT_MSG(
179 (MaxMessageSize <= MaxConflatedTcpMessageSize),
180 "The buffer is too large.");
181
182public:
185
187 enum
188 {
189 BufferSize = MaxMessageSize + sizeof(SimpleOpenFramingHeader)
190 };
191
192 explicit MessageHolder(SchemaVersion version = SbeType::Schema::Version)
193 {
194#ifdef ONIXS_CONFLATEDTCP_MEMCHECK_DEBUG
195 std::memset(buffer_, magicDebugValue, BufferSize);
196#endif
197 messageSize(0);
198
199 message_ = MessageInitializer::template createMessage<MessageType>(
200 buffer_ + sizeof(SimpleOpenFramingHeader), MaxMessageSize, version);
201 }
202
204 {
205 copyFrom(r);
206 }
207
209 {
210 copyFrom(r);
211 return *this;
212 }
213
215 const unsigned char* buffer() const ONIXS_CONFLATEDTCP_NOTHROW
216 {
217 return buffer_;
218 }
219
222 {
223 return header()->size();
224 }
225
228 {
229 return message_;
230 }
231
234 {
235 return message_;
236 }
237
240 {
241 return bufferSize() - sizeof(SimpleOpenFramingHeader);
242 }
243
248
250 {
251 return &message();
252 }
253
254 const MessageType& operator* () const ONIXS_CONFLATEDTCP_NOTHROW
255 {
256 return message();
257 }
258
260 {
261 return message();
262 }
263
266 {
267 return reinterpret_cast<const SimpleOpenFramingHeader*>(buffer_);
268 }
269
272 {
273 return reinterpret_cast<SimpleOpenFramingHeader*>(buffer_);
274 }
275
281 {
282 const MessageSize calculatedMessageSize =
283 message().calculateBinarySize();
284
285 assert(calculatedMessageSize <= MaxMessageSize);
286
287 assert(
288 calculatedMessageSize >=
289 SbeType::blockLength(message().version()) +
292
293 messageSize(calculatedMessageSize);
294 return calculatedMessageSize;
295 }
296
299 std::string toString() const
300 {
301 return toStr(*this);
302 }
303
304private:
305 void messageSize(UInt16 size) ONIXS_CONFLATEDTCP_NOTHROW
306 {
307 header()->setup(size + 2); //size contains only 2 bytes (message size) from SimpleOpenFramingHeader
308 }
309
310 void copyFrom(const MessageHolder& r)
311 {
312#ifdef ONIXS_CONFLATEDTCP_MEMCHECK_DEBUG
313 std::memset(buffer_, magicDebugValue, BufferSize);
314#endif
315
316 const size_t sizeToCopy = r.message_.MessageType::calculateBinarySize() + sizeof(SimpleOpenFramingHeader);
317
318 assert(r.message_.calculateBinarySize() <= MaxMessageSize);
319
320 std::memcpy(
321 buffer_,
322 r.buffer_,
323 sizeToCopy);
324
325 message_ =
326 MessageType(
327 buffer_ + sizeof(SimpleOpenFramingHeader),
328 MaxMessageSize,
331 }
332
333private:
334 MessageType message_;
335 unsigned char buffer_[BufferSize];
336};
337
338template <
339 template <typename, size_t, typename> class HolderType,
340 typename MsgType,
341 size_t MaxMessageSize,
342 typename MessageInitializer>
343inline std::string toStr(
344 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder)
345{
346 std::string res;
347 toStr(res, holder);
348 return res;
349}
350
351template <
352 template <typename, size_t, typename> class HolderType,
353 typename MsgType,
354 size_t MaxMessageSize,
355 typename MessageInitializer>
356std::ostream& operator<<(
357 std::ostream& stream,
358 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& rhs)
359{
360 return stream << toStr(rhs);
361}
362
363#if defined(ONIXS_CONFLATEDTCP_CXX11)
364
365template <
366 typename MessageTypeT,
367 size_t MaxMessageSize =
368 GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
369 typename MessageInitializer = FieldsInitPolicy>
370using MessageHolders = std::vector<MessageHolder<MessageTypeT, MaxMessageSize, MessageInitializer> >;
371
372#endif
373
#define ONIXS_CONFLATEDTCP_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:140
#define ONIXS_CONFLATEDTCP_MESSAGING_NAMESPACE_END
Definition ABI.h:143
#define ONIXS_CONFLATEDTCP_FINAL
Definition Compiler.h:181
#define ONIXS_CONFLATEDTCP_CONST_OR_CONSTEXPR
Definition Compiler.h:178
#define ONIXS_CONFLATEDTCP_NOTHROW
Definition Compiler.h:176
#define ONIXS_CONFLATEDTCP_NODISCARD
Definition Compiler.h:185
#define ONIXS_CONFLATEDTCP_DEFAULT
Definition Compiler.h:202
Template ID and length of message root.
Definition Composites.h:145
Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
MessageHolder & operator=(const MessageHolder &r)
SimpleOpenFramingHeader * header() noexcept
HeldAdapter< MessageTypeT > MessageType
Message type.
const MessageType & message() const noexcept
MessageHolder(SchemaVersion version=SbeType::Schema::Version)
const unsigned char * buffer() const noexcept
MessageSize setHeader() noexcept
Calculates the binary size of the message and updates the Simple Open Framing Header accordingly.
const MessageType * operator->() const noexcept
constexpr UInt8 DefaultMaxGroupItems
Default maximum number of repeating group items.
constexpr UInt16 MaxConflatedTcpMessageSize
Maximum supported message size.
MessageHeader::Version SchemaVersion
SBE-encoded data version type.
UInt16 MessageSize
Message length type.
Definition Aliases.h:29
std::vector< MessageHolder< MessageTypeT, MaxMessageSize, MessageInitializer > > MessageHolders
std::ostream & operator<<(std::ostream &stream, const Negotiate200 &obj)
Serializes into a stream.
void toStr(std::string &str, const Negotiate200 &obj)
Serializes into a string.
The policy to create messages with null-initialized optional fields.
The policy to create messages without initialized optional fields.
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoFieldsInit, SchemaVersion version)
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoInit, SbeMessage::NoCheck) noexcept
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoInit)
ONIXS_CONFLATEDTCP_STATIC_ASSERT((IsBaseOf< SbeMessage, MessageType >::value))
static constexpr MessageSize getMinimalVariableFieldsSize(SchemaVersion version)
Minimal variable fields size (when variable-length fields are empty).
Definition Messages.h:841
static constexpr BlockLength blockLength(SchemaVersion version) noexcept
Size of message body in bytes.
Definition Messages.h:826