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