OnixS C++ CME MDP Premium Market Data Handler 5.9.0
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
26#include <OnixS/CME/MDH/Time.h>
28
31
33ONIXS_CMEMDH_DATA_PACKING_BEGIN(1)
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
80 {
81 assert(header_ != end_);
82
83 return BinaryMessage(header_ + 1, header_->messageSize - sizeof(Header));
84 }
85
88 {
89 return message();
90 }
91
95 {
96 header_ = advanceByBytes(header_, header_->messageSize);
97
98 validate();
99
100 return *this;
101 }
102
104 bool operator==(const BinaryMessageIterator& other) const
105 {
106 return header_ == other.header_;
107 }
108
110 bool operator!=(const BinaryMessageIterator& other) const
111 {
112 return header_ != other.header_;
113 }
114
115private:
116 // Messages are prefixed with message header.
117 struct Header
118 {
119 MessageSize messageSize;
120 };
121
122 const Header* header_;
123 const void* end_;
124 const Packet* packet_;
125
126 void validate() const
127 {
128 const size_t availableSize = byteDistance(end_, header_);
129
130 if ONIXS_CMEMDH_LIKELY(availableSize != 0)
131 if ONIXS_CMEMDH_UNLIKELY(availableSize < sizeof(Header) || availableSize < header_->messageSize)
132 throwBadMessage(packet_);
133 }
134};
135
138{
139public:
141 BinaryMessages(const void* messagesBegin, const void* messagesEnd, const Packet* packet = ONIXS_CMEMDH_NULLPTR)
142 : begin_(messagesBegin)
143 , end_(messagesEnd)
144 , packet_(packet)
145 {
146 }
147
150 {
151 return BinaryMessageIterator(begin_, end_, packet_);
152 }
153
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
169void throwBadPacket(PacketSize packetSize, PacketSize minimalSize, const void* data);
170
175
178{
179public:
182 : header_(ONIXS_CMEMDH_NULLPTR)
183 , size_(0)
184 {
185 }
186
192 : header_(static_cast<const Header*>(data))
193 , size_(size)
194 {
195 assert(ONIXS_CMEMDH_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&)
207 : header_(static_cast<const Header*>(data))
208 , size_(size)
209 {
210 }
211
213 Packet(const Packet& other)
214 : header_(other.header_)
215 , size_(other.size_)
216 {
217 }
218
221
224 operator bool() const
225 {
226 return 0 != size_;
227 }
228
230 const void* data() const
231 {
232 assert(ONIXS_CMEMDH_NULLPTR != header_);
233
234 return static_cast<const void*>(header_);
235 }
236
239 {
240 return size_;
241 }
242
245 {
246 assert(ONIXS_CMEMDH_NULLPTR != header_);
247
248 return header_->seqNumber;
249 }
250
252 const Timestamp& sendingTime() const
253 {
254 assert(ONIXS_CMEMDH_NULLPTR != header_);
255
256 return header_->sendingTime;
257 }
258
261 {
262 assert(ONIXS_CMEMDH_NULLPTR != header_);
263
264 return BinaryMessages(header_ + 1, advanceByBytes(header_, size_), this);
265 }
266
268 void reset()
269 {
270 header_ = ONIXS_CMEMDH_NULLPTR;
271 size_ = 0;
272 }
273
276 void reset(const void* data, PacketSize size)
277 {
278 const PacketSize headerSize = sizeof(Header);
279
280 if ONIXS_CMEMDH_UNLIKELY(!data || size < headerSize)
281 throwBadPacket(size, headerSize, data);
282
283 header_ = static_cast<const Header*>(data);
284 size_ = size;
285 }
286
289 void reset(const void* data, PacketSize size, const NoVerify&)
290 {
291 header_ = static_cast<const Header*>(data);
292
293 size_ = size;
294 }
295
298 Packet& operator=(const Packet& other)
299 {
300 header_ = other.header_;
301 size_ = other.size_;
302
303 return *this;
304 }
305
306private:
307 struct Header
308 {
309 SequenceNumber seqNumber;
310 Timestamp sendingTime;
311 };
312
313 const Header* header_;
314 PacketSize size_;
315};
316
318void toStr(std::string& str, const Packet& packet);
319
320inline std::string toStr(const Packet& packet)
321{
322 std::string str;
323 toStr(str, packet);
324 return str;
325}
326
328{
329public:
332 : source_(ONIXS_CMEMDH_NULLPTR)
333 {
334 }
335
338 : Packet(data, size)
339 , source_(ONIXS_CMEMDH_NULLPTR)
340 {
341 }
342
344 NetPacket(const NetPacket& other)
345 : Packet(static_cast<const Packet&>(other))
346 , receiveTime_(other.receiveTime_)
347 , source_(other.source_)
348 {
349 }
350
353
355 const NetFeed& source() const
356 {
357 assert(ONIXS_CMEMDH_NULLPTR != source_);
358
359 return *source_;
360 }
361
363 void source(const NetFeed& source)
364 {
365 source_ = &source;
366 }
367
369 const Timestamp& receiveTime() const
370 {
371 return receiveTime_;
372 }
373
376 {
377 receiveTime_ = receiveTime;
378 }
379
381 void reset()
382 {
384
385 receiveTime_ = Timestamp();
386
387 source_ = ONIXS_CMEMDH_NULLPTR;
388 }
389
392 {
393 static_cast<Packet&>(*this) = static_cast<const Packet&>(other);
394
395 receiveTime_ = other.receiveTime_;
396
397 source_ = other.source_;
398
399 return *this;
400 }
401
402private:
403 Timestamp receiveTime_;
404 const NetFeed* source_;
405};
406
410
411ONIXS_CMEMDH_DATA_PACKING_END
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:67
#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:68
#define ONIXS_CMEMDH_NULLPTR
Definition Compiler.h:178
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:171
Iterator over SBE-encoded messages.
Definition Packet.h:44
BinaryMessageIterator(const void *dataBegin, const void *dataEnd, const Packet *packet)
Constructs encoded message iterator from raw data pointers.
Definition Packet.h:65
BinaryMessage operator*() const
Dereferences encoded message.
Definition Packet.h:87
std::forward_iterator_tag iterator_category
Definition Packet.h:47
BinaryMessage message() const
Gets reference to encoded message.
Definition Packet.h:79
bool operator!=(const BinaryMessageIterator &other) const
Checks whether two iterators are not equal.
Definition Packet.h:110
BinaryMessageIterator & operator++()
Advances iterator to next message.
Definition Packet.h:94
bool operator==(const BinaryMessageIterator &other) const
Checks whether two iterators are equal.
Definition Packet.h:104
value_type & reference
Defines reference to a value type.
Definition Packet.h:59
const BinaryMessage value_type
Defines value type.
Definition Packet.h:53
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
Encapsulates services for manipulating SBE-encoded messages.
Collection of encoded messages stored in single packet.
Definition Packet.h:138
BinaryMessages(const void *messagesBegin, const void *messagesEnd, const Packet *packet=nullptr)
Initializes messages container.
Definition Packet.h:141
BinaryMessageIterator begin() const
Returns iterator pointing to first encoded message.
Definition Packet.h:149
BinaryMessageIterator end() const
Returns iterator pointing past last encoded message.
Definition Packet.h:155
Base attributes of market data feed.
Definition Feed.h:55
void source(const NetFeed &source)
Updates packet source attribute.
Definition Packet.h:363
~NetPacket()
Does actually nothing.
Definition Packet.h:352
const Timestamp & receiveTime() const
Data reception time (if applicable).
Definition Packet.h:369
NetPacket()
Initializes as null packet.
Definition Packet.h:331
NetPacket(const void *data, PacketSize size)
Initializes with primary attributes.
Definition Packet.h:337
NetPacket & operator=(const NetPacket &other)
Re-initializes as a copy of the other instance.
Definition Packet.h:391
void reset()
Resets the instances to the blank state.
Definition Packet.h:381
NetPacket(const NetPacket &other)
Initializes as a copy of the other instance.
Definition Packet.h:344
void receiveTime(const Timestamp &receiveTime)
Updates receive time attribute.
Definition Packet.h:375
const NetFeed & source() const
Identifies source of the packet.
Definition Packet.h:355
Represents CME binary packet containing SBE messages.
Definition Packet.h:178
const Timestamp & sendingTime() const
Packet sending time.
Definition Packet.h:252
Packet(const void *data, PacketSize size)
Initializes binary packet over the given buffer.
Definition Packet.h:191
void reset(const void *data, PacketSize size)
Re-initializes the instance to refer to the other content.
Definition Packet.h:276
const void * data() const
Packet bytes.
Definition Packet.h:230
~Packet()
Does actually nothing.
Definition Packet.h:220
PacketSize size() const
Packet size.
Definition Packet.h:238
SequenceNumber seqNumber() const
Packet sequence number.
Definition Packet.h:244
Packet()
Initializes blank instance.
Definition Packet.h:181
void reset(const void *data, PacketSize size, const NoVerify &)
Re-initializes the instance to refer to the other content.
Definition Packet.h:289
BinaryMessages messages() const
Returns collection of messages stored by instance.
Definition Packet.h:260
void reset()
Resets the instance to the null state.
Definition Packet.h:268
Packet(const Packet &other)
Initializes as a copy of the other instance.
Definition Packet.h:213
Packet & operator=(const Packet &other)
Re-initializes the instance as the copy of the other one.
Definition Packet.h:298
Packet(const void *data, PacketSize size, const NoVerify &)
Initializes binary packet over the given buffer.
Definition Packet.h:206
Represents time point without time-zone information.
Definition Time.h:388
Type * advanceByBytes(Type *pointer, ptrdiff_t distance)
Advances given pointer to a given offset (distance) in bytes.
Definition Memory.h:75
void toStr(std::string &, BookState::Enum)
Serializes book state value into a string.
UInt16 MessageSize
Aliases message length type.
UInt32 SequenceNumber
Integral type used to identify packets in a sequence transmitted by MDP.
NetPacket PacketArgs
Alias for a type keeping collection of packet-related attributes.
Definition Packet.h:409
UInt16 PacketSize
Integral type for measuring packets.
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.
Used to identify the necessity to omit verification where that's applicable.
Definition Packet.h:174