OnixS C++ FIX Engine  4.12.0
API Documentation
SBE.h
Go to the documentation of this file.
1 #pragma once
2 /*
3 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
4 *
5 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
6 * and international copyright treaties.
7 *
8 * Access to and use of the software is governed by the terms of the applicable ONIXS Software
9 * Services Agreement (the Agreement) and Customer end user license agreements granting
10 * a non-assignable, non-transferable and non-exclusive license to use the software
11 * for it's own data processing purposes under the terms defined in the Agreement.
12 *
13 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
14 * of this source code or associated reference material to any other location for further reproduction
15 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
16 *
17 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
18 * the terms of the Agreement is a violation of copyright law.
19 */
20 
21 #include <string>
22 #include <vector>
23 #include <map>
24 
25 #include <OnixS/FIXEngine/ABI.h>
30 
31 namespace OnixS {
32 namespace FIX {
33 ONIXS_FIXENGINE_API_DECL(class, Message);
34 ONIXS_FIXENGINE_API_DECL(class, Dictionary);
35 
36 namespace SBE {
37 /// Encodes FIX messages into the SBE representation.
39 {
40 public:
41  /// Creates a FIX to SBE Encoder.
42  ///
43  /// @param xmlTemplates XML-based SBE templates.
44  /// @param customCoders The custom coders library.
45  Encoder(const std::string & xmlTemplates, ISbeCustomCoderLibrary *customCoders = ONIXS_FIXENGINE_NULLPTR);
46 
47  /// Destructor.
48  ~Encoder(void);
49 
50  /// Encodes the given FIX message into a SBE stream.
51  ///
52  /// @param[in] fixMessage The source FIX message to be encoded.
53  /// @param[in] templateID the SBE Template Identifier which uniquely describes the encoding/decoding rules.
54  /// @param[in] version The version of the template.
55  /// @param[in] buffer The supplied buffer that will contain the SBE stream chunk.
56  /// @param[in] bufferSize The size of the supplied buffer.
57  /// @param[out] rootBlockLength The size of the root block, used for the encoded message.
58  ///
59  /// @return The size of the entire encoded SBE message.
60  size_t encode(const OnixS::FIX::Message & fixMessage, int templateID, int version,
61  unsigned char * buffer, size_t bufferSize, size_t * rootBlockLength) const;
62 
63  /// Encodes the given FIX message into a SBE stream and prepends the message header.
64  ///
65  /// @param[in] fixMessage The source FIX message to be encoded.
66  /// @param[in] templateID The SBE Template Identifier which uniquely describes the encoding/decoding rules.
67  /// @param[in] version The version of the template.
68  /// @param[in] buffer The supplied buffer that will contain the SBE stream chunk.
69  /// @param[in] bufferSize The size of the supplied buffer.
70  /// @param[out] rootBlockLength The size of the root block, used for the encoded message.
71  ///
72  /// @return The size of the entire encoded SBE message.
73  size_t encodeWithHeader(const OnixS::FIX::Message& fixMessage, int templateID, int version,
74  unsigned char* buffer, size_t bufferSize, size_t* rootBlockLength) const;
75 
76  /// The maximum known version of the SBE schema.
77  unsigned schemaVersion() const;
78 
79  /// The schema identifier.
80  unsigned schemaId() const;
81 
82  /// The semantic version of the SBE schema.
83  std::string schemaSemanticVersion() const;
84 
85  /// The name of the encoding type of the message header, which is
86  /// the same for all messages in a schema.
87  std::string schemaHeaderType() const;
88 private:
89  Encoder(const Encoder &);
90  Encoder & operator = (const Encoder &);
91 
92  struct Impl;
93  Impl * impl_;
94 };
95 
96 /// Performs SBE to FIX decoding.
98 {
99 public:
100  /// Creates a SBE to FIX Decoder.
101  ///
102  /// @param xmlTemplates XML-based SBE templates.
103  /// @param fixDictionary The customized FIX dictionary used for the decoded message.
104  /// @param customCoders The custom coders library.
105  Decoder(const std::string & xmlTemplates, const OnixS::FIX::Dictionary & fixDictionary, ISbeCustomCoderLibrary* customCoders = ONIXS_FIXENGINE_NULLPTR);
106 
107  /// Creates a SBE to FIX Decoder for the FIX dictionary-independent mode.
108  /// The generic FIX dictionary is created by the provided SBE-template content, uses the FIX 4.0 as a base FIX dictionary and has a generic name.
109  ///
110  /// @param xmlTemplates XML-based SBE templates.
111  /// @param customCoders The custom coders library.
112  Decoder(const std::string & xmlTemplates, ISbeCustomCoderLibrary* customCoders = ONIXS_FIXENGINE_NULLPTR);
113 
114  /// Creates a SBE to FIX Decoder for the FIX dictionary-independent mode.
115  /// The generic FIX dictionary is created by the provided SBE-template content, uses the specified FIX dictionary as a base and has a generic name.
116  ///
117  /// @param baseVersion The version of the FIX protocol which the dictionary becomes a base for the newly generated FIX dictionary.
118  /// @param xmlTemplates XML-based SBE templates used to generate the FIX dictionary. This FIX dictionary takes a generic identifier generated on a base of the XML content and @c baseVersion
119  /// @param customCoders The custom coders library.
120  Decoder(ProtocolVersion::Enum baseVersion, const std::string & xmlTemplates, ISbeCustomCoderLibrary* customCoders = ONIXS_FIXENGINE_NULLPTR);
121 
122  /// The destructor.
123  ~Decoder();
124 
125  /// Decodes the given SBE stream chunk into the corresponding FIX Message.
126  ///
127  /// @param[in] templateId The identifier of the SBE template, used to decode the input data.
128  /// @param[in] version The version of the SBE schema, used to decode the input data.
129  /// @param[in] rootBlockLength The length of the root block. This value should be extracted from the message preamble, or from other source.
130  /// @param[in] buffer The buffer that contains the SBE stream chunk to be decoded.
131  /// @param[in] bufferSize The size of the buffer.
132  /// @param[out] numberOfDecodedBytes The number of bytes that contains the encoded FIX Message.
133  ///
134  /// @warning The returned message is 'owned' by the Decoder.
135  /// @warning The previously decoded message is freed by the Decoder when this method is called again.
136  ///
137  /// @throw An exception if the message cannot be decoded.
138  ///
139  /// @return The decoded FIX message.
140  const OnixS::FIX::Message & decode(int templateId, int version, size_t rootBlockLength,
141  const unsigned char * buffer, size_t bufferSize, size_t * numberOfDecodedBytes = ONIXS_FIXENGINE_NULLPTR) const;
142 
143  /// Decodes the given SBE stream chunk into the corresponding FIX Message using the message header.
144  ///
145  /// This method decodes the given SBE stream chunk using the message header decoder that was built on a base of the SBE template.
146  ///
147  /// @param[in] buffer The buffer that contains the SBE stream chunk to be decoded.
148  /// @param[in] bufferSize The size of the buffer.
149  /// @param[out] numberOfDecodedBytes The number of bytes that contained the encoded FIX Message.
150  /// @param[out] templateId The identifier of the SBE template, that was used to decode the input data.
151  /// @param[out] version The version of the SBE schema, that was used to decode the input data.
152  ///
153  /// @warning The returned message is 'owned' by the Decoder.
154  /// @warning The previously decoded message is freed by the Decoder when this method is called again.
155  ///
156  /// @throw An exception if the message cannot be decoded.
157  ///
158  /// @return The decoded FIX message.
159  const OnixS::FIX::Message& decode(const unsigned char* buffer, size_t bufferSize, size_t* numberOfDecodedBytes = ONIXS_FIXENGINE_NULLPTR,
160  int *templateId = ONIXS_FIXENGINE_NULLPTR, int *version = ONIXS_FIXENGINE_NULLPTR) const;
161 
162 
163  /// Tries to decode the given SBE stream buffer into the corresponding FIX Message.
164  ///
165  /// @param[in] templateId The identifier of the SBE template, used to decode the input data.
166  /// @param[in] version The version of the SBE schema, used to decode the input data.
167  /// @param[in] rootBlockLength The length of the root block. This value should be extracted from the message preamble, or from other source.
168  /// @param[in] buffer The SBE stream chunk to be decoded.
169  /// @param[in] offset The index in the buffer at which decoding begins.
170  /// @param[in] count The number of bytes to analyze during the decoding.
171  /// @param[out] message The pointer to the decoded FIX Message.
172  /// @param[out] numberOfDecodedBytes The number of bytes that contained the encoded FIX Message.
173  ///
174  /// @warning the message should point to an existing object. This object is not owned by the Decoder and should be managed by a user.
175  ///
176  /// @throw An exception if the decoding error is detected.
177  ///
178  /// @return 'true' if the given bytes could be decoded into a FIX message, otherwise - 'false'.
179  bool tryDecode(int templateId, int version, size_t rootBlockLength,
180  const unsigned char * buffer, size_t offset, size_t count, OnixS::FIX::Message * message,
181  size_t * numberOfDecodedBytes) const;
182 
183  /// The FIX dictionary used by the decoder instance.
184  ///
185  /// @return The FIX dictionary currently used by the decoder instance. If the decoder was initialized with the FIX dictionary-independent mode, the method returns
186  /// a reference to the internally generated FIX dictionary.
187  OnixS::FIX::Dictionary fixDictionary() const;
188 
189  /// Manage the maximum number of repeating groups, allowed for decoded messages.
190  ///
191  /// This parameter is used during the decoding and is useful to detect a broken data, which, in particular cases,
192  /// can 'need' to allocate an unexpectedly huge number of entries (for an instance - billions of entries,
193  /// while normally there are just a few ones). This situation results in a memory exhausting and (often)
194  /// significantly decrease the overall system performance.
195  ///
196  /// To prevent the memory exhausting and negative consequences of that, this parameter should be configured with the value,
197  /// which is greater then maximum possible number of entries in the valid input data, but less then the maximum integer value (default one).
198  /// In a lot of real cases value of 10000 is good enough to detect broken data without a memory overloading.
199  ///
200  /// Default is the maximum positive value for the @c int type.
201  ///
202  /// @param value The number of repeating groups, allowed for decoded messages.
203  void maximumNumberOfRepeatingGroupEntries(int value);
204 
205  /// The maximum number of repeating groups, allowed for decoded messages.
206  ///
207  /// @return The current number of repeating groups, allowed for decoded messages. The default value is the maximum positive value for the @c int type.
208  int maximumNumberOfRepeatingGroupEntries() const;
209 
210  /// Generates the FIX dictionary XML.
211  ///
212  /// @param[in] sbeTemplateXml The XML content of the SBE template (the same as used to initialize the Decoder).
213  ///
214  static Dictionary generateFixDictionary(const std::string & sbeTemplateXml);
215 
216  /// Generates the FIX dictionary XML.
217  ///
218  /// @param[in] sbeTemplateXml The XML content of the SBE template (the same as used to initialize the Decoder).
219  /// @param[in] baseVersion The version of the FIX protocol which the dictionary becomes a base for the newly generated FIX dictionary.
220  static Dictionary generateFixDictionary(ProtocolVersion::Enum baseVersion,
221  const std::string & sbeTemplateXml);
222 
223  /// The maximum known version of the SBE schema.
224  unsigned schemaVersion() const;
225 
226  /// The schema identifier.
227  unsigned schemaId() const;
228 
229  /// The semantic version of the SBE schema.
230  std::string schemaSemanticVersion() const;
231 
232  /// The name of the encoding type of the message header, which is
233  /// the same for all messages in a schema.
234  std::string schemaHeaderType() const;
235 private:
237 
238  Decoder(const Decoder &);
239  Decoder & operator = (const Decoder &);
240 
241  struct Impl;
242  Impl * impl_;
243 };
244 
245 }
246 }
247 }
The library that contains user-defined SBE coders.
Performs SBE to FIX decoding.
Definition: SBE.h:97
*decoder tryDecode(buffer, offset, count,&message,&numberOfDecodedBytes)
Characteristics of the input data stream for decoding.
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
Identifies the FIX messages dictionary.
Definition: Dictionary.h:73
Encodes FIX messages into the SBE representation.
Definition: SBE.h:38
ONIXS_FIXENGINE_API_DECL(class, IEngineListener)
ONIXS_FIXENGINE_API_DECL(class, IDecodeListener)
Encapsulates operations over a FIX Message.
Definition: Message.h:49