OnixS C++ CME Market Data Handler  5.4.0
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 
36 
37 /// Iterator over SBE-encoded messages.
40 {
41  // Messages are prefixed with message header.
42  struct Header
43  {
44  MessageSize messageSize;
45  };
46 
47  const Header* header_;
48  const void* end_;
49 
50  void
51  validate() const
52  {
53  if (const
54  size_t
55  availableSize =
57  end_, header_))
58  {
59  if (availableSize < sizeof(Header) ||
60  availableSize < header_->messageSize)
61  {
62  throw std::runtime_error
63  (
64  "Not enough data to retrieve SBE message. "
65  );
66  }
67  }
68  }
69 
70 public:
71  // Identifies iterator category.
72  typedef
73  std::forward_iterator_tag
75 
76  /// Establishes instance difference type.
77  typedef
78  ptrdiff_t
80 
81  /// Defines value type.
82  typedef
83  const BinaryMessage
85 
86  /// Defines pointer to a value type.
87  typedef value_type* pointer;
88 
89  /// Defines reference to a value type.
91 
92  /// Constructs encoded message iterator from raw data pointers.
93  ///
94  /// @throw runtime_error in case when raw data pointers
95  /// cannot be casted to valid encoded message iterator.
97  const void* dataBegin,
98  const void* dataEnd)
99  : header_(
100  static_cast
101  <const Header*>
102  (dataBegin))
103  , end_(
104  dataEnd)
105  {
106  assert(dataBegin <= dataEnd);
107 
108  validate();
109  }
110 
111  /// Gets reference to encoded message.
112  ///
113  /// @warning Behavior is undefined in case
114  /// when iterator points to the packet end.
116  {
117  assert(header_ != end_);
118 
119  return
121  header_ + 1,
122  header_->messageSize -
123  sizeof(Header));
124  }
125 
126  /// Dereferences encoded message.
127  BinaryMessage operator *() const
128  {
129  return message();
130  }
131 
132  /// Advances iterator to next message.
133  /// @throw runtime_error in case when iteration is not possible.
134  BinaryMessageIterator&
135  operator ++()
136  {
137  header_ =
139  header_,
140  header_->messageSize);
141 
142  validate();
143 
144  return *this;
145  }
146 
147  /// Checks whether two iterators are equal.
148  bool
150  const BinaryMessageIterator& other) const
151  {
152  return header_ == other.header_;
153  }
154 
155  /// Checks whether two iterators are not equal.
156  bool
158  const BinaryMessageIterator& other) const
159  {
160  return header_ != other.header_;
161  }
162 };
163 
164 /// Collection of encoded messages stored in single packet.
166 {
167  // Memory block keeping messages.
168  const void* begin_;
169  const void* end_;
170 
171 public:
172  /// Initializes messages container.
174  const void* messagesBegin,
175  const void* messagesEnd)
176  : begin_(messagesBegin)
177  , end_(messagesEnd)
178  {
179  }
180 
181  /// Returns iterator pointing to first encoded message.
183  {
184  return BinaryMessageIterator(begin_, end_);
185  }
186 
187  /// Returns iterator pointing past last encoded message.
189  {
190  return BinaryMessageIterator(end_, end_);
191  }
192 };
193 
194 /// Raises exception on ill-formed packet.
195 inline
196 void
198  PacketSize packetSize,
199  PacketSize minimalSize)
200 {
201  std::string error;
202 
203  error +=
204  "Cannot construct packet over given buffer "
205  "because it doesn't meet requirements on minimal size. "
206  "Size of buffer is ";
207 
208  toStr(error, packetSize);
209 
210  error += " byte(s), whereas packet must have at least ";
211 
212  toStr(error, minimalSize);
213 
214  error += " bytes. ";
215 
216  throw std::runtime_error(error);
217 }
218 
219 /// Used to identify the necessity to omit
220 /// verification where that's applicable.
222 
223 /// Represents CME binary packet containing SBE messages.
225 {
226  struct Header
227  {
228  SequenceNumber seqNumber;
229  Timestamp sendingTime;
230  };
231 
232  const Header* header_;
233  PacketSize size_;
234 
235 public:
236  /// Initializes blank instance.
238  : header_(NULL)
239  , size_(0)
240  {
241  }
242 
243  /// Initializes binary packet over the given buffer.
244  ///
245  /// @throw std::exception if given buffer doesn't
246  /// satisfy base requirements on well-formed CME packet.
248  const void* data,
249  PacketSize size)
250  : header_(
251  static_cast
252  <const Header*>
253  (data))
254  , size_(size)
255  {
256  assert(NULL != header_);
257  assert(0 != size_);
258 
259  const
260  PacketSize
261  headerSize =
262  sizeof(Header);
263 
264  if (size < headerSize)
265  {
267  size, headerSize);
268  }
269  }
270 
271  /// Initializes binary packet over the given buffer.
272  /// Verification of the incoming content is omitted.
274  const void* data,
275  PacketSize size,
276  const NoVerify&)
277  : header_(
278  static_cast
279  <const Header*>
280  (data))
281  , size_(size)
282  {
283  }
284 
285  /// Initializes as a copy of the other instance.
287  const Packet& other)
288  : header_(other.header_)
289  , size_(other.size_)
290  {
291  }
292 
293  /// Does actually nothing.
295  {
296  }
297 
298  /// Indicates whether the given
299  /// instance points to a valid packet.
300  operator bool() const
301  {
302  return 0 != size_;
303  }
304 
305  /// Packet bytes.
306  const void* data() const
307  {
308  assert(NULL != header_);
309 
310  return static_cast<const void*>(header_);
311  }
312 
313  /// Packet size.
314  PacketSize size() const
315  {
316  return size_;
317  }
318 
319  /// Packet sequence number.
321  {
322  assert(NULL != header_);
323 
324  return header_->seqNumber;
325  }
326 
327  /// Packet sending time.
328  const Timestamp& sendingTime() const
329  {
330  assert(NULL != header_);
331 
332  return header_->sendingTime;
333  }
334 
335  /// Returns collection of messages stored by instance.
337  {
338  assert(NULL != header_);
339 
340  return
342  header_ + 1,
344  header_, size_));
345  }
346 
347  /// Resets the instance to the null state.
348  void reset()
349  {
350  header_ = NULL;
351  size_ = 0;
352  }
353 
354  /// Re-initializes the instance
355  /// to refer to the other content.
356  void
358  const void* data,
359  PacketSize size)
360  {
361  const
362  PacketSize
363  headerSize =
364  sizeof(Header);
365 
366  if (data &&
367  size >= headerSize)
368  {
369  header_ =
370  static_cast
371  <const Header*>
372  (data);
373 
374  size_ = size;
375  }
376  else
377  {
379  size, headerSize);
380  }
381  }
382 
383  /// Re-initializes the instance
384  /// to refer to the other content.
385  void
387  const void* data,
388  PacketSize size,
389  const NoVerify&)
390  {
391  header_ =
392  static_cast
393  <const Header*>
394  (data);
395 
396  size_ = size;
397  }
398 
399  /// Re-initializes the instance
400  /// as the copy of the other one.
401  Packet&
402  operator =(
403  const Packet& other)
404  {
405  header_ = other.header_;
406  size_ = other.size_;
407 
408  return *this;
409  }
410 };
411 
413 NetPacket : public Packet
414 {
415  Timestamp receiveTime_;
416  const NetFeed* source_;
417 
418 public:
419  /// Initializes as null packet.
421  : source_(NULL)
422  {
423  }
424 
425  /// Initializes with primary attributes.
427  const void* data,
428  PacketSize size)
429  : Packet(data, size)
430  , source_(NULL)
431  {
432  }
433 
434  /// Initializes as a copy of the other instance.
436  const NetPacket& other)
437  : Packet(
438  static_cast
439  <const Packet&>
440  (other))
441  , receiveTime_(
442  other.receiveTime_)
443  , source_(
444  other.source_)
445  {
446  }
447 
448  /// Does actually nothing.
450  {
451  }
452 
453  /// Identifies source of the packet.
454  const NetFeed& source() const
455  {
456  assert(NULL != source_);
457 
458  return *source_;
459  }
460 
461  /// Updates packet source attribute.
462  void
464  const NetFeed& source)
465  {
466  source_ = &source;
467  }
468 
469  /// Data reception time (if applicable).
470  const Timestamp& receiveTime() const
471  {
472  return receiveTime_;
473  }
474 
475  /// Updates receive time attribute.
476  void
478  const Timestamp& receiveTime)
479  {
480  receiveTime_ = receiveTime;
481  }
482 
483  /// Resets the instances to the blank state.
484  void reset()
485  {
486  Packet::reset();
487 
488  receiveTime_ = Timestamp();
489 
490  source_ = NULL;
491  }
492 
493  /// Re-initializes as a copy of the other instance.
494  NetPacket&
495  operator =(
496  const NetPacket& other)
497  {
498  static_cast<Packet&>(*this) =
499  static_cast<const Packet&>(other);
500 
501  receiveTime_ = other.receiveTime_;
502 
503  source_ = other.source_;
504 
505  return *this;
506  }
507 };
508 
509 /// Alias for a type keeping collection
510 /// of packet-related attributes.
512 
513 ONIXS_CMEMDH_DATA_PACKING_END
NetPacket(const void *data, PacketSize size)
Initializes with primary attributes.
Definition: Packet.h:426
#define ONIXS_CMEMDH_LTWT_CLASS
Definition: Bootstrap.h:94
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:121
BinaryMessageIterator(const void *dataBegin, const void *dataEnd)
Definition: Packet.h:96
value_type & reference
Defines reference to a value type.
Definition: Packet.h:90
~NetPacket()
Does actually nothing.
Definition: Packet.h:449
Packet(const void *data, PacketSize size)
Definition: Packet.h:247
BinaryMessages messages() const
Returns collection of messages stored by instance.
Definition: Packet.h:336
void source(const NetFeed &source)
Updates packet source attribute.
Definition: Packet.h:463
Represents time point without time-zone information.
Definition: Time.h:471
const Timestamp & receiveTime() const
Data reception time (if applicable).
Definition: Packet.h:470
void reset()
Resets the instances to the blank state.
Definition: Packet.h:484
void receiveTime(const Timestamp &receiveTime)
Updates receive time attribute.
Definition: Packet.h:477
BinaryMessages(const void *messagesBegin, const void *messagesEnd)
Initializes messages container.
Definition: Packet.h:173
NetPacket()
Initializes as null packet.
Definition: Packet.h:420
Packet(const Packet &other)
Initializes as a copy of the other instance.
Definition: Packet.h:286
const Timestamp & sendingTime() const
Packet sending time.
Definition: Packet.h:328
Packet(const void *data, PacketSize size, const NoVerify &)
Definition: Packet.h:273
NetPacket PacketArgs
Definition: Packet.h:511
UInt16 MessageSize
Aliases message length type.
void toStr(std::string &str, const Message &message)
Serializes FIX message into tag=value format.
Definition: Message.h:379
Base attributes of market data feed.
Definition: Feed.h:60
value_type * pointer
Defines pointer to a value type.
Definition: Packet.h:87
#define ONIXS_CMEMDH_LTWT_STRUCT
Definition: Bootstrap.h:98
UInt32 SequenceNumber
Definition: PacketTraits.h:31
Iterator over SBE-encoded messages.
Definition: Packet.h:38
NetPacket(const NetPacket &other)
Initializes as a copy of the other instance.
Definition: Packet.h:435
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:152
BinaryMessage message() const
Definition: Packet.h:115
BinaryMessageIterator begin() const
Returns iterator pointing to first encoded message.
Definition: Packet.h:182
ptrdiff_t difference_type
Establishes instance difference type.
Definition: Packet.h:79
Type * advanceByBytes(Type *pointer, ptrdiff_t distance)
Advances given pointer to a given offset (distance) in bytes.
Definition: Memory.h:109
Encapsulates services for manipulating SBE-encoded messages.
#define ONIXS_CMEMDH_LTWT_CLASS_DECL(name)
Definition: Bootstrap.h:106
BinaryMessageIterator end() const
Returns iterator pointing past last encoded message.
Definition: Packet.h:188
void reset(const void *data, PacketSize size, const NoVerify &)
Definition: Packet.h:386
void throwBadPacket(PacketSize packetSize, PacketSize minimalSize)
Raises exception on ill-formed packet.
Definition: Packet.h:197
void reset()
Resets the instance to the null state.
Definition: Packet.h:348
Represents CME binary packet containing SBE messages.
Definition: Packet.h:224
PacketSize size() const
Packet size.
Definition: Packet.h:314
const BinaryMessage value_type
Defines value type.
Definition: Packet.h:84
std::forward_iterator_tag iterator_category
Definition: Packet.h:74
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:99
SequenceNumber seqNumber() const
Packet sequence number.
Definition: Packet.h:320
const NetFeed & source() const
Identifies source of the packet.
Definition: Packet.h:454
Collection of encoded messages stored in single packet.
Definition: Packet.h:165
void reset(const void *data, PacketSize size)
Definition: Packet.h:357
~Packet()
Does actually nothing.
Definition: Packet.h:294
ptrdiff_t byteDistance(Left *left, Right *right)
Returns distance in bytes between two pointers.
Definition: Memory.h:147
UInt16 PacketSize
Integral type for measuring packets.
Definition: PacketTraits.h:36
Packet()
Initializes blank instance.
Definition: Packet.h:237
const void * data() const
Packet bytes.
Definition: Packet.h:306
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:156