OnixS C++ CME MDP Premium Market Data Handler  5.8.9
API Documentation
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>
27 #include <OnixS/CME/MDH/Integral.h>
28 
31 
33 ONIXS_CMEMDH_DATA_PACKING_BEGIN(1)
34 
37 
38 /// Raises exception on ill-formed message.
40 void throwBadMessage(const Packet* packet);
41 
42 /// Iterator over SBE-encoded messages.
44 {
45 public:
46  // Identifies iterator category.
47  typedef std::forward_iterator_tag iterator_category;
48 
49  /// Establishes instance difference type.
50  typedef ptrdiff_t difference_type;
51 
52  /// Defines value type.
53  typedef const BinaryMessage value_type;
54 
55  /// Defines pointer to a value type.
56  typedef value_type* pointer;
57 
58  /// Defines reference to a value type.
59  typedef value_type& reference;
60 
61  /// Constructs encoded message iterator from raw data pointers.
62  ///
63  /// @throw runtime_error in case when raw data pointers
64  /// cannot be casted to valid encoded message iterator.
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 
75  /// Gets reference to encoded message.
76  ///
77  /// @warning Behavior is undefined in case
78  /// when iterator points to the packet end.
80  {
81  assert(header_ != end_);
82 
83  return BinaryMessage(header_ + 1, header_->messageSize - sizeof(Header));
84  }
85 
86  /// Dereferences encoded message.
88  {
89  return message();
90  }
91 
92  /// Advances iterator to next message.
93  /// @throw runtime_error in case when iteration is not possible.
94  BinaryMessageIterator& operator++()
95  {
96  header_ = advanceByBytes(header_, header_->messageSize);
97 
98  validate();
99 
100  return *this;
101  }
102 
103  /// Checks whether two iterators are equal.
104  bool operator==(const BinaryMessageIterator& other) const
105  {
106  return header_ == other.header_;
107  }
108 
109  /// Checks whether two iterators are not equal.
110  bool operator!=(const BinaryMessageIterator& other) const
111  {
112  return header_ != other.header_;
113  }
114 
115 private:
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 
136 /// Collection of encoded messages stored in single packet.
138 {
139 public:
140  /// Initializes messages container.
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 
148  /// Returns iterator pointing to first encoded message.
150  {
151  return BinaryMessageIterator(begin_, end_, packet_);
152  }
153 
154  /// Returns iterator pointing past last encoded message.
156  {
157  return BinaryMessageIterator(end_, end_, packet_);
158  }
159 
160 private:
161  // Memory block keeping messages.
162  const void* begin_;
163  const void* end_;
164  const Packet* packet_;
165 };
166 
167 /// Raises exception on ill-formed packet.
169 void throwBadPacket(PacketSize packetSize, PacketSize minimalSize, const void* data);
170 
171 /// Used to identify the necessity to omit
172 /// verification where that's applicable.
174 {};
175 
176 /// Represents CME binary packet containing SBE messages.
178 {
179 public:
180  /// Initializes blank instance.
182  : header_(ONIXS_CMEMDH_NULLPTR)
183  , size_(0)
184  {
185  }
186 
187  /// Initializes binary packet over the given buffer.
188  ///
189  /// @throw std::exception if given buffer doesn't
190  /// satisfy base requirements on well-formed CME packet.
191  Packet(const void* data, PacketSize size)
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 
204  /// Initializes binary packet over the given buffer.
205  /// Verification of the incoming content is omitted.
206  Packet(const void* data, PacketSize size, const NoVerify&)
207  : header_(static_cast<const Header*>(data))
208  , size_(size)
209  {
210  }
211 
212  /// Initializes as a copy of the other instance.
213  Packet(const Packet& other)
214  : header_(other.header_)
215  , size_(other.size_)
216  {
217  }
218 
219  /// Does actually nothing.
220  ~Packet() {}
221 
222  /// Indicates whether the given
223  /// instance points to a valid packet.
224  operator bool() const
225  {
226  return 0 != size_;
227  }
228 
229  /// Packet bytes.
230  const void* data() const
231  {
232  assert(ONIXS_CMEMDH_NULLPTR != header_);
233 
234  return static_cast<const void*>(header_);
235  }
236 
237  /// Packet size.
238  PacketSize size() const
239  {
240  return size_;
241  }
242 
243  /// Packet sequence number.
245  {
246  assert(ONIXS_CMEMDH_NULLPTR != header_);
247 
248  return header_->seqNumber;
249  }
250 
251  /// Packet sending time.
252  const Timestamp& sendingTime() const
253  {
254  assert(ONIXS_CMEMDH_NULLPTR != header_);
255 
256  return header_->sendingTime;
257  }
258 
259  /// Returns collection of messages stored by instance.
261  {
262  assert(ONIXS_CMEMDH_NULLPTR != header_);
263 
264  return BinaryMessages(header_ + 1, advanceByBytes(header_, size_), this);
265  }
266 
267  /// Resets the instance to the null state.
268  void reset()
269  {
270  header_ = ONIXS_CMEMDH_NULLPTR;
271  size_ = 0;
272  }
273 
274  /// Re-initializes the instance
275  /// to refer to the other content.
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 
287  /// Re-initializes the instance
288  /// to refer to the other content.
289  void reset(const void* data, PacketSize size, const NoVerify&)
290  {
291  header_ = static_cast<const Header*>(data);
292 
293  size_ = size;
294  }
295 
296  /// Re-initializes the instance
297  /// as the copy of the other one.
298  Packet& operator=(const Packet& other)
299  {
300  header_ = other.header_;
301  size_ = other.size_;
302 
303  return *this;
304  }
305 
306 private:
307  struct Header
308  {
309  SequenceNumber seqNumber;
310  Timestamp sendingTime;
311  };
312 
313  const Header* header_;
314  PacketSize size_;
315 };
316 
318 void toStr(std::string& str, const Packet& packet);
319 
320 inline std::string toStr(const Packet& packet)
321 {
322  std::string str;
323  toStr(str, packet);
324  return str;
325 }
326 
328 {
329 public:
330  /// Initializes as null packet.
332  : source_(ONIXS_CMEMDH_NULLPTR)
333  {
334  }
335 
336  /// Initializes with primary attributes.
337  NetPacket(const void* data, PacketSize size)
338  : Packet(data, size)
339  , source_(ONIXS_CMEMDH_NULLPTR)
340  {
341  }
342 
343  /// Initializes as a copy of the other instance.
344  NetPacket(const NetPacket& other)
345  : Packet(static_cast<const Packet&>(other))
346  , receiveTime_(other.receiveTime_)
347  , source_(other.source_)
348  {
349  }
350 
351  /// Does actually nothing.
353 
354  /// Identifies source of the packet.
355  const NetFeed& source() const
356  {
357  assert(ONIXS_CMEMDH_NULLPTR != source_);
358 
359  return *source_;
360  }
361 
362  /// Updates packet source attribute.
363  void source(const NetFeed& source)
364  {
365  source_ = &source;
366  }
367 
368  /// Data reception time (if applicable).
369  const Timestamp& receiveTime() const
370  {
371  return receiveTime_;
372  }
373 
374  /// Updates receive time attribute.
375  void receiveTime(const Timestamp& receiveTime)
376  {
377  receiveTime_ = receiveTime;
378  }
379 
380  /// Resets the instances to the blank state.
381  void reset()
382  {
383  Packet::reset();
384 
385  receiveTime_ = Timestamp();
386 
387  source_ = ONIXS_CMEMDH_NULLPTR;
388  }
389 
390  /// Re-initializes as a copy of the other instance.
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 
402 private:
403  Timestamp receiveTime_;
404  const NetFeed* source_;
405 };
406 
407 /// Alias for a type keeping collection
408 /// of packet-related attributes.
410 
411 ONIXS_CMEMDH_DATA_PACKING_END
NetPacket(const void *data, PacketSize size)
Initializes with primary attributes.
Definition: Packet.h:337
value_type & reference
Defines reference to a value type.
Definition: Packet.h:59
~NetPacket()
Does actually nothing.
Definition: Packet.h:352
Packet(const void *data, PacketSize size)
Initializes binary packet over the given buffer.
Definition: Packet.h:191
BinaryMessages messages() const
Returns collection of messages stored by instance.
Definition: Packet.h:260
#define ONIXS_CMEMDH_NULLPTR
Definition: Compiler.h:167
void source(const NetFeed &source)
Updates packet source attribute.
Definition: Packet.h:363
Represents time point without time-zone information.
Definition: Time.h:387
const Timestamp & receiveTime() const
Data reception time (if applicable).
Definition: Packet.h:369
BinaryMessage operator*() const
Dereferences encoded message.
Definition: Packet.h:87
void reset()
Resets the instances to the blank state.
Definition: Packet.h:381
void receiveTime(const Timestamp &receiveTime)
Updates receive time attribute.
Definition: Packet.h:375
NetPacket()
Initializes as null packet.
Definition: Packet.h:331
BinaryMessageIterator & operator++()
Advances iterator to next message.
Definition: Packet.h:94
Packet(const Packet &other)
Initializes as a copy of the other instance.
Definition: Packet.h:213
bool operator!=(const BinaryMessageIterator &other) const
Checks whether two iterators are not equal.
Definition: Packet.h:110
Packet & operator=(const Packet &other)
Re-initializes the instance as the copy of the other one.
Definition: Packet.h:298
#define ONIXS_CMEMDH_LTWT
Definition: Bootstrap.h:46
std::string toStr(const Packet &packet)
Definition: Packet.h:320
const Timestamp & sendingTime() const
Packet sending time.
Definition: Packet.h:252
Packet(const void *data, PacketSize size, const NoVerify &)
Initializes binary packet over the given buffer.
Definition: Packet.h:206
NetPacket PacketArgs
Alias for a type keeping collection of packet-related attributes.
Definition: Packet.h:409
UInt16 MessageSize
Aliases message length type.
Base attributes of market data feed.
Definition: Feed.h:54
value_type * pointer
Defines pointer to a value type.
Definition: Packet.h:56
UInt32 SequenceNumber
Integral type used to identify packets in a sequence transmitted by MDP.
Definition: PacketTraits.h:29
Iterator over SBE-encoded messages.
Definition: Packet.h:43
NetPacket(const NetPacket &other)
Initializes as a copy of the other instance.
Definition: Packet.h:344
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:67
BinaryMessage message() const
Gets reference to encoded message.
Definition: Packet.h:79
BinaryMessageIterator begin() const
Returns iterator pointing to first encoded message.
Definition: Packet.h:149
ptrdiff_t difference_type
Establishes instance difference type.
Definition: Packet.h:50
Type * advanceByBytes(Type *pointer, ptrdiff_t distance)
Advances given pointer to a given offset (distance) in bytes.
Definition: Memory.h:75
Encapsulates services for manipulating SBE-encoded messages.
#define ONIXS_CMEMDH_EXPORTED
Definition: Compiler.h:160
#define ONIXS_CMEMDH_LTWT_CLASS_DECL(name)
Definition: Bootstrap.h:48
bool operator==(const BinaryMessageIterator &other) const
Checks whether two iterators are equal.
Definition: Packet.h:104
BinaryMessageIterator end() const
Returns iterator pointing past last encoded message.
Definition: Packet.h:155
void reset(const void *data, PacketSize size, const NoVerify &)
Re-initializes the instance to refer to the other content.
Definition: Packet.h:289
NetPacket & operator=(const NetPacket &other)
Re-initializes as a copy of the other instance.
Definition: Packet.h:391
BinaryMessages(const void *messagesBegin, const void *messagesEnd, const Packet *packet=nullptr)
Initializes messages container.
Definition: Packet.h:141
void reset()
Resets the instance to the null state.
Definition: Packet.h:268
BinaryMessageIterator(const void *dataBegin, const void *dataEnd, const Packet *packet)
Constructs encoded message iterator from raw data pointers.
Definition: Packet.h:65
Represents CME binary packet containing SBE messages.
Definition: Packet.h:177
PacketSize size() const
Packet size.
Definition: Packet.h:238
const BinaryMessage value_type
Defines value type.
Definition: Packet.h:53
std::forward_iterator_tag iterator_category
Definition: Packet.h:47
SequenceNumber seqNumber() const
Packet sequence number.
Definition: Packet.h:244
const NetFeed & source() const
Identifies source of the packet.
Definition: Packet.h:355
Collection of encoded messages stored in single packet.
Definition: Packet.h:137
void reset(const void *data, PacketSize size)
Re-initializes the instance to refer to the other content.
Definition: Packet.h:276
Used to identify the necessity to omit verification where that&#39;s applicable.
Definition: Packet.h:173
~Packet()
Does actually nothing.
Definition: Packet.h:220
void throwBadMessage(const Packet *packet)
Raises exception on ill-formed message.
ptrdiff_t byteDistance(Left *left, Right *right)
Returns distance in bytes between two pointers.
Definition: Memory.h:90
UInt16 PacketSize
Integral type for measuring packets.
Definition: PacketTraits.h:32
Packet()
Initializes blank instance.
Definition: Packet.h:181
const void * data() const
Packet bytes.
Definition: Packet.h:230
void throwBadPacket(PacketSize packetSize, PacketSize minimalSize, const void *data)
Raises exception on ill-formed packet.
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:68