OnixS C++ B3 BOE Binary Order Entry  1.2.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 BOE message.
31 {
32 public:
33  /// Constructs an empty instance.
35  : data_(ONIXS_B3_BOE_NULLPTR)
36  , size_(0)
37  {}
38 
39  /// Constructs NetworkMessage from a data block. Performs no checks.
41  : data_(ONIXS_B3_BOE_NULLPTR)
42  , size_(0)
43  {
44  assert(size >= sizeof(SimpleOpenFramingHeader));
45 
46  const SimpleOpenFramingHeader* const header = reinterpret_cast<const SimpleOpenFramingHeader*>(data);
47 
48  assert(header->encoding() == B3BOESbeEncodingType);
49  assert(header->size() <= size);
50 
51 #ifndef NDEBUG
52  SbeMessage(toByteBlock(toMutable(data)) + sizeof(SimpleOpenFramingHeader), header->size() - sizeof(SimpleOpenFramingHeader));
53 #endif
54 
55  data_ = data;
56  size_ = header->size();
57  }
58 
59  /// Constructs NetworkMessage from a data block.
60  NetworkMessage(const void* data, size_t size)
61  : data_(ONIXS_B3_BOE_NULLPTR)
62  , size_(0)
63  {
64  if ONIXS_B3_BOE_UNLIKELY(size < sizeof(SimpleOpenFramingHeader))
65  throwBinaryBlockIsTooSmall(static_cast<MessageSize>(size), sizeof(SimpleOpenFramingHeader), "SimpleOpenFramingHeader");
66 
67  const SimpleOpenFramingHeader* const header = reinterpret_cast<const SimpleOpenFramingHeader*>(data);
68 
69  if ONIXS_B3_BOE_UNLIKELY(header->encoding() != B3BOESbeEncodingType)
70  throwIncorrectEncoding(header->encoding(), data, static_cast<MessageSize>(size));
71 
72  if ONIXS_B3_BOE_UNLIKELY(header->size() > size)
73  throwNetPacketIsTooSmall(static_cast<MessageSize>(size), header->size());
74 
75  SbeMessage(toByteBlock(toMutable(data)) + sizeof(SimpleOpenFramingHeader), header->size() - sizeof(SimpleOpenFramingHeader));
76 
77  data_ = data;
78  size_ = header->size();
79  }
80 
81  ///
82  const void* data() const ONIXS_B3_BOE_NOTHROW
83  {
84  return data_;
85  }
86 
87  ///
89  {
90  return size_;
91  }
92 
93  /// Blank the instance.
95  {
96  *this = NetworkMessage();
97  assert(!valid());
98  }
99 
100  /// \return `true` if the instance refers to a valid packet, otherwise - `false`.
101 
103  {
104  return (ONIXS_B3_BOE_NULLPTR != data_);
105  }
106 
107  /// Retrieves the underlying SBE message.
108 
110  {
111  assert(valid());
112 
113 #ifndef NDEBUG
114  SbeMessage(toByteBlock(toMutable(data_)) + sizeof(SimpleOpenFramingHeader), size_ - sizeof(SimpleOpenFramingHeader));
115 #endif
116 
117  return SbeMessage(
118  toByteBlock(toMutable(data_)) + sizeof(SimpleOpenFramingHeader),
119  size_ - sizeof(SimpleOpenFramingHeader), SbeMessage::NoCheck());
120  }
121 
122 private:
123  const void* data_;
124  MessageSize size_;
125 };
126 
127 /// Serializes into a string.
129 void toStr(std::string&, const NetworkMessage&);
130 
131 inline
132 std::string toStr(const NetworkMessage& message)
133 {
134  std::string res;
135  toStr(res, message);
136  return res;
137 }
138 
const void * data() const noexcept
std::string toStr(const NetworkMessage &message)
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_END
Definition: ABI.h:144
#define ONIXS_B3_BOE_NULLPTR
Definition: Compiler.h:188
#define ONIXS_B3_BOE_NOTHROW
Definition: Compiler.h:182
SbeMessage message() const noexcept
Retrieves the underlying SBE message.
#define ONIXS_B3_BOE_EXPORTED
Definition: Compiler.h:181
NetworkMessage() noexcept
Constructs an empty instance.
UInt16 MessageSize
Message length type.
Definition: Aliases.h:29
void clear() noexcept
Blank the instance.
NetworkMessage(const void *data, size_t size, SbeMessage::NoCheck) noexcept
Constructs NetworkMessage from a data block. Performs no checks.
MessageSize size() const noexcept
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:140
#define ONIXS_B3_BOE_LTWT_CLASS
Definition: ABI.h:84
NetworkMessage(const void *data, size_t size)
Constructs NetworkMessage from a data block.
void size(UInt16 val) noexcept
Sets the size.
#define ONIXS_B3_BOE_UNUSED
Definition: Compiler.h:207