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