OnixS C++ CME MDP Premium Market Data Handler 5.10.3
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
183 Packet() noexcept
184 : header_(nullptr)
185 , size_(0)
186 {
187 }
188
194 : header_(static_cast<const Header*>(data))
195 , size_(size)
196 {
197 assert(nullptr != header_);
198 assert(0 != size_);
199
200 const PacketSize headerSize = sizeof(Header);
201
202 if ONIXS_CMEMDH_UNLIKELY(size < headerSize)
203 throwBadPacket(size, headerSize, data);
204 }
205
208 Packet(const void* data, PacketSize size, const NoVerify&) noexcept
209 : header_(static_cast<const Header*>(data))
210 , size_(size)
211 {
212 }
213
215 Packet(const Packet& other) noexcept
216 : header_(other.header_)
217 , size_(other.size_)
218 {
219 }
220
222 ~Packet() = default;
223
227 operator bool() const noexcept
228 {
229 return 0 != size_;
230 }
231
234 const void* data() const noexcept
235 {
236 assert(nullptr != header_);
237
238 return header_;
239 }
240
243 const Header* header() const noexcept
244 {
245 assert(nullptr != header_);
246
247 return header_;
248 }
249
252 PacketSize size() const noexcept
253 {
254 return size_;
255 }
256
259 SequenceNumber seqNumber() const noexcept
260 {
261 assert(nullptr != header_);
262
263 return header_->seqNumber();
264 }
265
269 {
270 assert(nullptr != header_);
271
272 return header_->sendingTime();
273 }
274
278 {
279 assert(nullptr != header_);
280
281 return BinaryMessages(header_ + 1, Messaging::advanceByBytes(header_, size_), this);
282 }
283
285 void reset() noexcept
286 {
287 header_ = nullptr;
288 size_ = 0;
289 }
290
293 void reset(const void* data, PacketSize size)
294 {
295 const PacketSize headerSize = sizeof(Header);
296
297 if ONIXS_CMEMDH_UNLIKELY(!data || size < headerSize)
298 throwBadPacket(size, headerSize, data);
299
300 header_ = static_cast<const Header*>(data);
301 size_ = size;
302 }
303
307 void reset(const void* data, PacketSize size, const NoVerify&) noexcept
308 {
309 header_ = static_cast<const Header*>(data);
310
311 size_ = size;
312 }
313
316 Packet& operator=(const Packet& other) noexcept
317 {
318 header_ = other.header_;
319 size_ = other.size_;
320
321 return *this;
322 }
323
324private:
325 const Header* header_;
326 PacketSize size_;
327};
328
330void toStr(std::string& str, const Packet& packet);
331
332inline std::string toStr(const Packet& packet)
333{
334 std::string str;
335 toStr(str, packet);
336 return str;
337}
338
340{
341public:
343 NetPacket() noexcept
344 : source_(nullptr)
345 {
346 }
347
350 : Packet(data, size)
351 , source_(nullptr)
352 {
353 }
354
356 NetPacket(const NetPacket& other)
357 : Packet(static_cast<const Packet&>(other))
358 , receiveTime_(other.receiveTime_)
359 , source_(other.source_)
360 {
361 }
362
364 ~NetPacket() = default;
365
368 const NetFeed& source() const noexcept
369 {
370 assert(nullptr != source_);
371
372 return *source_;
373 }
374
377 void source(const NetFeed& source) noexcept
378 {
379 source_ = &source;
380 }
381
384 const Messaging::Timestamp& receiveTime() const noexcept
385 {
386 return receiveTime_;
387 }
388
392 {
393 receiveTime_ = receiveTime;
394 }
395
397 void reset()
398 {
400
401 receiveTime_ = Messaging::Timestamp();
402
403 source_ = nullptr;
404 }
405
408 {
409 static_cast<Packet&>(*this) = static_cast<const Packet&>(other);
410
411 receiveTime_ = other.receiveTime_;
412
413 source_ = other.source_;
414
415 return *this;
416 }
417
418private:
419 Messaging::Timestamp receiveTime_;
420 const NetFeed* source_;
421};
422
426
#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:455
Base attributes of market data feed.
Definition Feed.h:55
void receiveTime(const Messaging::Timestamp &receiveTime) noexcept
Updates receive time attribute.
Definition Packet.h:391
const NetFeed & source() const noexcept
Identifies source of the packet.
Definition Packet.h:368
void source(const NetFeed &source) noexcept
Updates packet source attribute.
Definition Packet.h:377
~NetPacket()=default
Does actually nothing.
NetPacket(const void *data, PacketSize size)
Initializes with primary attributes.
Definition Packet.h:349
NetPacket & operator=(const NetPacket &other)
Re-initializes as a copy of the other instance.
Definition Packet.h:407
void reset()
Resets the instances to the blank state.
Definition Packet.h:397
NetPacket() noexcept
Initializes as null packet.
Definition Packet.h:343
NetPacket(const NetPacket &other)
Initializes as a copy of the other instance.
Definition Packet.h:356
const Messaging::Timestamp & receiveTime() const noexcept
Data reception time (if applicable).
Definition Packet.h:384
Represents CME binary packet containing SBE messages.
Definition Packet.h:178
Packet(const void *data, PacketSize size)
Definition Packet.h:193
PacketHeader Header
Definition Packet.h:180
void reset(const void *data, PacketSize size)
Definition Packet.h:293
void reset() noexcept
Resets the instance to the null state.
Definition Packet.h:285
void reset(const void *data, PacketSize size, const NoVerify &) noexcept
Definition Packet.h:307
Packet(const Packet &other) noexcept
Initializes as a copy of the other instance.
Definition Packet.h:215
Packet() noexcept
Initializes blank instance.
Definition Packet.h:183
Packet & operator=(const Packet &other) noexcept
Definition Packet.h:316
Packet(const void *data, PacketSize size, const NoVerify &) noexcept
Definition Packet.h:208
SequenceNumber seqNumber() const noexcept
Packet sequence number.
Definition Packet.h:259
const void * data() const noexcept
Packet bytes.
Definition Packet.h:234
BinaryMessages messages() const
Returns collection of messages stored by instance.
Definition Packet.h:277
PacketSize size() const noexcept
Packet size.
Definition Packet.h:252
const Header * header() const noexcept
Packet header.
Definition Packet.h:243
~Packet()=default
Does actually nothing.
Messaging::Timestamp sendingTime() const noexcept
Packet sending time.
Definition Packet.h:268
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:425
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.