OnixS C++ CME iLink 3 Binary Order Entry Handler  1.18.0
API Documentation
NetworkMessage.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 
26 
28 
29 /// A network iLink3 message.
30 
31 /// https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+-+Simple+Binary+Encoding
33 {
34 public:
35  /// Constructs an empty instance.
37  : data_(ONIXS_ILINK3_NULLPTR)
38  , size_(0)
39  {}
40 
41  /// Constructs NetworkMessage from a data block. Performs no checks.
42 
44  : data_(ONIXS_ILINK3_NULLPTR)
45  , size_(0)
46  {
47  assert(size >= sizeof(SimpleOpenFramingHeader));
48 
49  const SimpleOpenFramingHeader* const header = reinterpret_cast<const SimpleOpenFramingHeader*>(data);
50 
51  assert(header->encoding() == CmeSbeEncodingType);
52  assert(header->size() <= size);
53 
54 #ifndef NDEBUG
55  SbeMessage(toByteBlock(toMutable(data)) + sizeof(SimpleOpenFramingHeader), header->size() - sizeof(SimpleOpenFramingHeader));
56 #endif
57 
58  data_ = data;
59  size_ = header->size();
60  }
61 
62  /// Constructs NetworkMessage from a data block.
63 
64  NetworkMessage(const void* data, size_t size)
65  : data_(ONIXS_ILINK3_NULLPTR)
66  , size_(0)
67  {
68  if ONIXS_ILINK3_UNLIKELY(size < sizeof(SimpleOpenFramingHeader))
69  throwBinaryBlockIsTooSmall(static_cast<MessageSize>(size), sizeof(SimpleOpenFramingHeader), "SimpleOpenFramingHeader");
70 
71  const SimpleOpenFramingHeader* const header = reinterpret_cast<const SimpleOpenFramingHeader*>(data);
72 
73  if ONIXS_ILINK3_UNLIKELY(header->encoding() != CmeSbeEncodingType)
74  throwIncorrectEncoding(header->encoding(), data, static_cast<MessageSize>(size));
75 
76  if ONIXS_ILINK3_UNLIKELY(header->size() > size)
77  throwNetPacketIsTooSmall(static_cast<MessageSize>(size), header->size());
78 
79  SbeMessage(toByteBlock(toMutable(data)) + sizeof(SimpleOpenFramingHeader), header->size() - sizeof(SimpleOpenFramingHeader));
80 
81  data_ = data;
82  size_ = header->size();
83  }
84 
85  ///
86  const void* data() const ONIXS_ILINK3_NOTHROW
87  {
88  return data_;
89  }
90 
91  ///
93  {
94  return size_;
95  }
96 
97  /// Blank the instance.
99  {
100  *this = NetworkMessage();
101  assert(!valid());
102  }
103 
104  /// \return `true` if the instance refers to a valid packet, otherwise - `false`.
105 
107  {
108  return (ONIXS_ILINK3_NULLPTR != data_);
109  }
110 
111  /// Retrieves the underlying SBE message.
112 
114  {
115  assert(valid());
116 
117 #ifndef NDEBUG
118  SbeMessage(toByteBlock(toMutable(data_)) + sizeof(SimpleOpenFramingHeader), size_ - sizeof(SimpleOpenFramingHeader));
119 #endif
120 
121  return SbeMessage(
122  toByteBlock(toMutable(data_)) + sizeof(SimpleOpenFramingHeader),
123  size_ - sizeof(SimpleOpenFramingHeader), SbeMessage::NoCheck());
124  }
125 
126 private:
127  const void* data_;
128  MessageSize size_;
129 };
130 
131 /// Serializes into a string.
133 void toStr(std::string&, const NetworkMessage&);
134 
135 inline
136 std::string toStr(const NetworkMessage& message)
137 {
138  std::string res;
139  toStr(res, message);
140  return res;
141 }
142 
#define ONIXS_ILINK3_NULLPTR
Definition: Compiler.h:182
std::string toStr(const NetworkMessage &message)
NetworkMessage() noexcept
Constructs an empty instance.
#define ONIXS_ILINK3_LTWT_CLASS
Definition: ABI.h:84
NetworkMessage(const void *data, size_t size, SbeMessage::NoCheck) noexcept
Constructs NetworkMessage from a data block. Performs no checks.
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_END
Definition: ABI.h:144
#define ONIXS_ILINK3_EXPORTED
Definition: Compiler.h:175
UInt16 MessageSize
Message length type.
Definition: Aliases.h:29
NetworkMessage(const void *data, size_t size)
Constructs NetworkMessage from a data block.
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:140
#define ONIXS_ILINK3_UNUSED
Definition: Compiler.h:201
SbeMessage message() const noexcept
Retrieves the underlying SBE message.
#define ONIXS_ILINK3_NOTHROW
Definition: Compiler.h:176
void clear() noexcept
Blank the instance.