OnixS C++ CME MDP Premium Market Data Handler 5.11.1
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
44void throwBadMessage(const Packet* packet);
45
51{
52public:
54 using iterator_category = std::forward_iterator_tag;
55
57 using difference_type = ptrdiff_t;
58
61
64
67
75 BinaryMessageIterator(const void* dataBegin, const void* dataEnd, const Packet* packet)
76 : header_(static_cast<const Header*>(dataBegin))
77 , end_(dataEnd)
78 , packet_(packet)
79 {
80 assert(dataBegin <= dataEnd);
81
82 validate();
83 }
84
94 {
95 assert(header_ != end_);
96
97 return Messaging::constructFromImmutableBlock<value_type>(header_ + 1, header_->messageSize() - sizeof(Header));
98 }
99
108 {
109 return message();
110 }
111
118 {
119 header_ = Messaging::advanceByBytes(header_, header_->messageSize());
120
121 validate();
122
123 return *this;
124 }
125
130 bool operator==(const BinaryMessageIterator& other) const noexcept
131 {
132 return header_ == other.header_;
133 }
134
139 bool operator!=(const BinaryMessageIterator& other) const noexcept
140 {
141 return header_ != other.header_;
142 }
143
144private:
145 using Header = MessagePrefix;
146
147 const Header* header_;
148 const void* end_;
149 const Packet* packet_;
150
151 void validate() const
152 {
153 const auto availableSize = Messaging::byteDistance(end_, header_);
154 const auto headerSize = static_cast<ptrdiff_t>(sizeof(Header));
155
156 if ONIXS_CMEMDH_UNLIKELY(availableSize == 0)
157 return;
158
159 if ONIXS_CMEMDH_UNLIKELY(availableSize < headerSize)
160 throwBadMessage(packet_);
161
162 const auto messageSize = header_->messageSize();
163
164 if ONIXS_CMEMDH_UNLIKELY(messageSize < headerSize || availableSize < messageSize)
165 throwBadMessage(packet_);
166 }
167};
168
173{
174public:
180 BinaryMessages(const void* messagesBegin, const void* messagesEnd, const Packet* packet = nullptr) noexcept
181 : begin_(messagesBegin)
182 , end_(messagesEnd)
183 , packet_(packet)
184 {
185 }
186
194 {
195 return BinaryMessageIterator(begin_, end_, packet_);
196 }
197
203 {
204 return BinaryMessageIterator(end_, end_, packet_);
205 }
206
207private:
208 // Memory block keeping messages.
209 const void* begin_;
210 const void* end_;
211 const Packet* packet_;
212};
213
221void throwBadPacket(PacketSize packetSize, PacketSize minimalSize, const void* data);
222
226
232{
233public:
236
238 Packet() noexcept = default;
239
246 : header_(static_cast<const Header*>(data))
247 , size_(size)
248 {
249 assert(nullptr != header_);
250
251 const PacketSize headerSize = sizeof(Header);
252
253 if ONIXS_CMEMDH_UNLIKELY(size < headerSize)
254 throwBadPacket(size, headerSize, data);
255 }
256
262 Packet(const void* data, PacketSize size, ONIXS_CMEMDH_UNUSED const NoVerify& noVerify) noexcept
263 : header_(static_cast<const Header*>(data))
264 , size_(size)
265 {
266 }
267
270 Packet(const Packet& other) noexcept = default;
271
273 ~Packet() = default;
274
279 operator bool() const noexcept
280 {
281 return 0 != size_;
282 }
283
289 const void* data() const noexcept
290 {
291 assert(nullptr != header_);
292
293 return header_;
294 }
295
301 const Header* header() const noexcept
302 {
303 assert(nullptr != header_);
304
305 return header_;
306 }
307
312 PacketSize size() const noexcept
313 {
314 return size_;
315 }
316
322 SequenceNumber seqNumber() const noexcept
323 {
324 assert(nullptr != header_);
325
326 return header_->seqNumber();
327 }
328
335 {
336 assert(nullptr != header_);
337
338 return header_->sendingTime();
339 }
340
346 BinaryMessages messages() const noexcept
347 {
348 assert(nullptr != header_);
349
350 return BinaryMessages(header_ + 1, Messaging::advanceByBytes(header_, size_), this);
351 }
352
354 void reset() noexcept
355 {
356 header_ = nullptr;
357 size_ = 0;
358 }
359
365 void reset(const void* data, PacketSize size)
366 {
367 const PacketSize headerSize = sizeof(Header);
368
369 if ONIXS_CMEMDH_UNLIKELY(!data || size < headerSize)
370 throwBadPacket(size, headerSize, data);
371
372 header_ = static_cast<const Header*>(data);
373 size_ = size;
374 }
375
382 void reset(const void* data, PacketSize size, ONIXS_CMEMDH_UNUSED const NoVerify& noVerify) noexcept
383 {
384 header_ = static_cast<const Header*>(data);
385
386 size_ = size;
387 }
388
392 Packet& operator=(const Packet& other) noexcept = default;
393
394private:
395 const Header* header_ = nullptr;
396 PacketSize size_ = 0;
397};
398
404void toStr(std::string& str, const Packet& packet);
405
410inline ONIXS_CMEMDH_NODISCARD std::string toStr(const Packet& packet)
411{
412 std::string str;
413 toStr(str, packet);
414 return str;
415}
416
421{
422public:
424 NetPacket() noexcept = default;
425
432 : Packet(data, size)
433 , source_(nullptr)
434 {
435 }
436
439 NetPacket(const NetPacket& other) noexcept = default;
440
442 ~NetPacket() = default;
443
449 const NetFeed& source() const noexcept
450 {
451 assert(nullptr != source_);
452
453 return *source_;
454 }
455
459 void source(const NetFeed& source) noexcept
460 {
461 source_ = &source;
462 }
463
468 const Messaging::Timestamp& receiveTime() const noexcept
469 {
470 return receiveTime_;
471 }
472
477 {
478 receiveTime_ = receiveTime;
479 }
480
482 void reset() noexcept
483 {
485
486 receiveTime_ = Messaging::Timestamp();
487
488 source_ = nullptr;
489 }
490
494 NetPacket& operator=(const NetPacket& other) noexcept = default;
495
496private:
497 Messaging::Timestamp receiveTime_;
498 const NetFeed* source_ = nullptr;
499};
500
503
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:69
#define ONIXS_CMEMDH_LTWT
Definition Bootstrap.h:61
#define ONIXS_CMEMDH_LTWT_CLASS_DECL(name)
Definition Bootstrap.h:63
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:73
#define ONIXS_CMEMDH_DATA_PACKING_END
Definition Compiler.h:197
#define ONIXS_CMEMDH_COLDPATH
Definition Compiler.h:168
#define ONIXS_CMEMDH_DATA_PACKING_BEGIN(alignment)
Definition Compiler.h:196
#define ONIXS_CMEMDH_FORCEINLINE
Definition Compiler.h:176
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:163
#define ONIXS_CMEMDH_NORETURN
Definition Compiler.h:164
#define ONIXS_CMEMDH_NODISCARD
Definition Compiler.h:165
#define ONIXS_CMEMDH_UNUSED
Definition Compiler.h:177
Provides forward iteration over the SBE-encoded messages in a packet buffer.
Definition Packet.h:51
bool operator!=(const BinaryMessageIterator &other) const noexcept
Tests whether two iterators refer to different byte positions.
Definition Packet.h:139
BinaryMessageIterator(const void *dataBegin, const void *dataEnd, const Packet *packet)
Constructs an iterator over a range of encoded message bytes.
Definition Packet.h:75
value_type message() const
Returns a non-owning view of the current encoded message.
Definition Packet.h:93
const Messaging::SbeMessage value_type
Represents an immutable SBE message view.
Definition Packet.h:60
value_type * pointer
Defines a pointer to the iterator value type.
Definition Packet.h:63
BinaryMessageIterator & operator++()
Advances to the next encoded message.
Definition Packet.h:117
std::forward_iterator_tag iterator_category
Identifies this type as a forward iterator.
Definition Packet.h:54
value_type & reference
Defines a reference to the iterator value type.
Definition Packet.h:66
value_type operator*() const
Returns a non-owning view of the current encoded message.
Definition Packet.h:107
bool operator==(const BinaryMessageIterator &other) const noexcept
Tests whether two iterators refer to the same byte position.
Definition Packet.h:130
ptrdiff_t difference_type
Represents the distance between iterators.
Definition Packet.h:57
Provides a non-owning iterable view of the encoded messages in a packet.
Definition Packet.h:173
BinaryMessageIterator begin() const
Returns an iterator to the first encoded message.
Definition Packet.h:193
BinaryMessages(const void *messagesBegin, const void *messagesEnd, const Packet *packet=nullptr) noexcept
Constructs a view over an encoded message range.
Definition Packet.h:180
BinaryMessageIterator end() const
Returns an iterator past the last encoded message.
Definition Packet.h:202
Represents the length prefix preceding each message in an MDP packet.
The time point without the time-zone information.
Definition Time.h:450
Base attributes of market data feed.
Definition Feed.h:55
Extends a packet view with its network source and reception timestamp.
Definition Packet.h:421
void reset() noexcept
Clears the packet view, source feed, and reception timestamp.
Definition Packet.h:482
NetPacket() noexcept=default
Constructs a null network packet without a source.
void receiveTime(const Messaging::Timestamp &receiveTime) noexcept
Sets the packet reception time.
Definition Packet.h:476
const NetFeed & source() const noexcept
Returns the network feed from which the packet was received.
Definition Packet.h:449
void source(const NetFeed &source) noexcept
Associates the packet with its network feed.
Definition Packet.h:459
NetPacket & operator=(const NetPacket &other) noexcept=default
Copies another packet view and its network metadata.
~NetPacket()=default
Destroys the view without releasing the referenced buffer or source feed.
NetPacket(const NetPacket &other) noexcept=default
Copies the packet view and its network metadata without copying referenced objects.
const Messaging::Timestamp & receiveTime() const noexcept
Returns the packet reception time.
Definition Packet.h:468
Represents the sequence number and sending time stored in an MDP packet header.
Provides a non-owning view of a CME binary packet containing SBE messages.
Definition Packet.h:232
Packet & operator=(const Packet &other) noexcept=default
Rebinds this view to the same buffer as another packet.
Packet() noexcept=default
Constructs a null packet that does not refer to a buffer.
PacketHeader Header
Defines the binary header type stored at the start of a packet.
Definition Packet.h:235
void reset(const void *data, PacketSize size)
Rebinds this view to a buffer and validates its minimum size.
Definition Packet.h:365
void reset() noexcept
Resets this object to the null packet state.
Definition Packet.h:354
BinaryMessages messages() const noexcept
Returns an iterable view of the packet's encoded messages.
Definition Packet.h:346
void reset(const void *data, PacketSize size, const NoVerify &noVerify) noexcept
Rebinds this view to a buffer without validating it.
Definition Packet.h:382
Packet(const void *data, PacketSize size, const NoVerify &noVerify) noexcept
Constructs a packet view without validating the buffer.
Definition Packet.h:262
SequenceNumber seqNumber() const noexcept
Returns the packet sequence number.
Definition Packet.h:322
Packet(const Packet &other) noexcept=default
Copies a packet view without copying the referenced buffer.
const void * data() const noexcept
Returns the start of the packet bytes.
Definition Packet.h:289
PacketSize size() const noexcept
Returns the packet size.
Definition Packet.h:312
const Header * header() const noexcept
Returns the packet header.
Definition Packet.h:301
~Packet()=default
Destroys the view without releasing the referenced buffer.
Messaging::Timestamp sendingTime() const noexcept
Returns the packet sending time.
Definition Packet.h:334
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::UInt16 PacketSize
Represents a packet size in bytes.
Messaging::UInt32 SequenceNumber
Identifies a packet within an MDP packet sequence.
NetPacket PacketArgs
Provides the packet and network metadata passed to packet callbacks.
Definition Packet.h:502
void toStr(std::string &str, BookState::Enum bookState)
Appends a string representation of a book state.
void throwBadMessage(const Packet *packet)
Reports that a packet does not contain a complete, well-formed message.
void throwBadPacket(PacketSize packetSize, PacketSize minimalSize, const void *data)
Reports that a buffer is too small to contain a packet.
Selects overloads that omit packet buffer validation.
Definition Packet.h:225