OnixS C++ CME MDP Premium Market Data Handler 5.10.2
Users' manual and API documentation
Loading...
Searching...
No Matches
Packet.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 <cassert>
24#include <stdexcept>
25
29
31
34
37
40void throwBadMessage(const Packet* packet);
41
44{
45public:
46 // Identifies iterator category.
47 typedef std::forward_iterator_tag iterator_category;
48
50 typedef ptrdiff_t difference_type;
51
54
57
60
65 BinaryMessageIterator(const void* dataBegin, const void* dataEnd, const Packet* packet)
66 : header_(static_cast<const Header*>(dataBegin))
67 , end_(dataEnd)
68 , packet_(packet)
69 {
70 assert(dataBegin <= dataEnd);
71
72 validate();
73 }
74
81 {
82 assert(header_ != end_);
83
84 return Messaging::constructFromImmutableBlock<value_type>(header_ + 1, header_->messageSize() - sizeof(Header));
85 }
86
90 {
91 return message();
92 }
93
97 {
98 header_ = Messaging::advanceByBytes(header_, header_->messageSize());
99
100 validate();
101
102 return *this;
103 }
104
106 bool operator==(const BinaryMessageIterator& other) const
107 {
108 return header_ == other.header_;
109 }
110
112 bool operator!=(const BinaryMessageIterator& other) const
113 {
114 return header_ != other.header_;
115 }
116
117private:
118 using Header = MessagePrefix;
119
120 const Header* header_;
121 const void* end_;
122 const Packet* packet_;
123
124 void validate() const
125 {
126 const size_t availableSize = Messaging::byteDistance(end_, header_);
127
128 if ONIXS_CMEMDH_LIKELY(availableSize != 0)
129 if ONIXS_CMEMDH_UNLIKELY(availableSize < sizeof(Header) || availableSize < header_->messageSize())
130 throwBadMessage(packet_);
131 }
132};
133
136{
137public:
139 BinaryMessages(const void* messagesBegin, const void* messagesEnd, const Packet* packet = nullptr)
140 : begin_(messagesBegin)
141 , end_(messagesEnd)
142 , packet_(packet)
143 {
144 }
145
149 {
150 return BinaryMessageIterator(begin_, end_, packet_);
151 }
152
156 {
157 return BinaryMessageIterator(end_, end_, packet_);
158 }
159
160private:
161 // Memory block keeping messages.
162 const void* begin_;
163 const void* end_;
164 const Packet* packet_;
165};
166
170void throwBadPacket(PacketSize packetSize, PacketSize minimalSize, const void* data);
171
175
178{
179public:
181 Packet() noexcept
182 : header_(nullptr)
183 , size_(0)
184 {
185 }
186
192 : header_(static_cast<const Header*>(data))
193 , size_(size)
194 {
195 assert(nullptr != header_);
196 assert(0 != size_);
197
198 const PacketSize headerSize = sizeof(Header);
199
200 if ONIXS_CMEMDH_UNLIKELY(size < headerSize)
201 throwBadPacket(size, headerSize, data);
202 }
203
206 Packet(const void* data, PacketSize size, const NoVerify&) noexcept
207 : header_(static_cast<const Header*>(data))
208 , size_(size)
209 {
210 }
211
213 Packet(const Packet& other) noexcept
214 : header_(other.header_)
215 , size_(other.size_)
216 {
217 }
218
220 ~Packet() = default;
221
225 operator bool() const noexcept
226 {
227 return 0 != size_;
228 }
229
232 const void* data() const noexcept
233 {
234 assert(nullptr != header_);
235
236 return static_cast<const void*>(header_);
237 }
238
241 PacketSize size() const noexcept
242 {
243 return size_;
244 }
245
248 SequenceNumber seqNumber() const noexcept
249 {
250 assert(nullptr != header_);
251
252 return header_->seqNumber();
253 }
254
258 {
259 assert(nullptr != header_);
260
261 return header_->sendingTime();
262 }
263
267 {
268 assert(nullptr != header_);
269
270 return BinaryMessages(header_ + 1, Messaging::advanceByBytes(header_, size_), this);
271 }
272
274 void reset() noexcept
275 {
276 header_ = nullptr;
277 size_ = 0;
278 }
279
282 void reset(const void* data, PacketSize size)
283 {
284 const PacketSize headerSize = sizeof(Header);
285
286 if ONIXS_CMEMDH_UNLIKELY(!data || size < headerSize)
287 throwBadPacket(size, headerSize, data);
288
289 header_ = static_cast<const Header*>(data);
290 size_ = size;
291 }
292
296 void reset(const void* data, PacketSize size, const NoVerify&) noexcept
297 {
298 header_ = static_cast<const Header*>(data);
299
300 size_ = size;
301 }
302
305 Packet& operator=(const Packet& other) noexcept
306 {
307 header_ = other.header_;
308 size_ = other.size_;
309
310 return *this;
311 }
312
313private:
314 using Header = PacketHeader;
315
316 const Header* header_;
317 PacketSize size_;
318};
319
321void toStr(std::string& str, const Packet& packet);
322
323inline std::string toStr(const Packet& packet)
324{
325 std::string str;
326 toStr(str, packet);
327 return str;
328}
329
331{
332public:
334 NetPacket() noexcept
335 : source_(nullptr)
336 {
337 }
338
341 : Packet(data, size)
342 , source_(nullptr)
343 {
344 }
345
347 NetPacket(const NetPacket& other)
348 : Packet(static_cast<const Packet&>(other))
349 , receiveTime_(other.receiveTime_)
350 , source_(other.source_)
351 {
352 }
353
355 ~NetPacket() = default;
356
359 const NetFeed& source() const noexcept
360 {
361 assert(nullptr != source_);
362
363 return *source_;
364 }
365
368 void source(const NetFeed& source) noexcept
369 {
370 source_ = &source;
371 }
372
375 const Messaging::Timestamp& receiveTime() const noexcept
376 {
377 return receiveTime_;
378 }
379
383 {
384 receiveTime_ = receiveTime;
385 }
386
388 void reset()
389 {
391
392 receiveTime_ = Messaging::Timestamp();
393
394 source_ = nullptr;
395 }
396
399 {
400 static_cast<Packet&>(*this) = static_cast<const Packet&>(other);
401
402 receiveTime_ = other.receiveTime_;
403
404 source_ = other.source_;
405
406 return *this;
407 }
408
409private:
410 Messaging::Timestamp receiveTime_;
411 const NetFeed* source_;
412};
413
417
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:54
#define ONIXS_CMEMDH_LTWT
Definition Bootstrap.h:46
#define ONIXS_CMEMDH_LTWT_CLASS_DECL(name)
Definition Bootstrap.h:48
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:55
#define ONIXS_CMEMDH_DATA_PACKING_END
Definition Compiler.h:176
#define ONIXS_CMEMDH_COLDPATH
Definition Compiler.h:153
#define ONIXS_CMEMDH_DATA_PACKING_BEGIN(alignment)
Definition Compiler.h:175
#define ONIXS_CMEMDH_FORCEINLINE
Definition Compiler.h:161
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:148
Iterator over SBE-encoded messages.
Definition Packet.h:44
BinaryMessageIterator(const void *dataBegin, const void *dataEnd, const Packet *packet)
Definition Packet.h:65
std::forward_iterator_tag iterator_category
Definition Packet.h:47
bool operator!=(const BinaryMessageIterator &other) const
Checks whether two iterators are not equal.
Definition Packet.h:112
BinaryMessageIterator & operator++()
Definition Packet.h:96
bool operator==(const BinaryMessageIterator &other) const
Checks whether two iterators are equal.
Definition Packet.h:106
const Messaging::SbeMessage value_type
Defines value type.
Definition Packet.h:53
value_type & reference
Defines reference to a value type.
Definition Packet.h:59
value_type operator*() const
Dereferences encoded message.
Definition Packet.h:89
ptrdiff_t difference_type
Establishes instance difference type.
Definition Packet.h:50
value_type * pointer
Defines pointer to a value type.
Definition Packet.h:56
Collection of encoded messages stored in single packet.
Definition Packet.h:136
BinaryMessages(const void *messagesBegin, const void *messagesEnd, const Packet *packet=nullptr)
Initializes messages container.
Definition Packet.h:139
BinaryMessageIterator begin() const
Returns iterator pointing to first encoded message.
Definition Packet.h:148
BinaryMessageIterator end() const
Returns iterator pointing past last encoded message.
Definition Packet.h:155
Messages are prefixed with message header.
The time point without the time-zone information.
Definition Time.h:425
Base attributes of market data feed.
Definition Feed.h:55
void receiveTime(const Messaging::Timestamp &receiveTime) noexcept
Updates receive time attribute.
Definition Packet.h:382
const NetFeed & source() const noexcept
Identifies source of the packet.
Definition Packet.h:359
void source(const NetFeed &source) noexcept
Updates packet source attribute.
Definition Packet.h:368
~NetPacket()=default
Does actually nothing.
NetPacket(const void *data, PacketSize size)
Initializes with primary attributes.
Definition Packet.h:340
NetPacket & operator=(const NetPacket &other)
Re-initializes as a copy of the other instance.
Definition Packet.h:398
void reset()
Resets the instances to the blank state.
Definition Packet.h:388
NetPacket() noexcept
Initializes as null packet.
Definition Packet.h:334
NetPacket(const NetPacket &other)
Initializes as a copy of the other instance.
Definition Packet.h:347
const Messaging::Timestamp & receiveTime() const noexcept
Data reception time (if applicable).
Definition Packet.h:375
Represents CME binary packet containing SBE messages.
Definition Packet.h:178
Packet(const void *data, PacketSize size)
Definition Packet.h:191
void reset(const void *data, PacketSize size)
Definition Packet.h:282
void reset() noexcept
Resets the instance to the null state.
Definition Packet.h:274
void reset(const void *data, PacketSize size, const NoVerify &) noexcept
Definition Packet.h:296
Packet(const Packet &other) noexcept
Initializes as a copy of the other instance.
Definition Packet.h:213
Packet() noexcept
Initializes blank instance.
Definition Packet.h:181
Packet & operator=(const Packet &other) noexcept
Definition Packet.h:305
Packet(const void *data, PacketSize size, const NoVerify &) noexcept
Definition Packet.h:206
SequenceNumber seqNumber() const noexcept
Packet sequence number.
Definition Packet.h:248
const void * data() const noexcept
Packet bytes.
Definition Packet.h:232
BinaryMessages messages() const
Returns collection of messages stored by instance.
Definition Packet.h:266
PacketSize size() const noexcept
Packet size.
Definition Packet.h:241
~Packet()=default
Does actually nothing.
Messaging::Timestamp sendingTime() const noexcept
Packet sending time.
Definition Packet.h:257
const SbeMessage constructFromImmutableBlock(const void *data, MessageSize size, typename std::enable_if< std::is_same< typename std::remove_cv< Message >::type, SbeMessage >::value >::type *=nullptr)
Constructs the given message from an immutable memory block.
Messaging::UInt32 SequenceNumber
void toStr(std::string &, BookState::Enum)
Serializes book state value into a string.
NetPacket PacketArgs
Definition Packet.h:416
void throwBadMessage(const Packet *packet)
Raises exception on ill-formed message.
void throwBadPacket(PacketSize packetSize, PacketSize minimalSize, const void *data)
Raises exception on ill-formed packet.
Messaging::UInt16 PacketSize
Integral type for measuring packets.