OnixS C++ FIX Engine  4.2.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>
28 #include <OnixS/FIXEngine/FIX/InputDataTraits.h>
29 #include <OnixS/FIXEngine/Sockets/Definitions.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 SBE representation.
39 {
40 public:
41  /// Creates a FIX to SBE Encoder.
42  ///
43  /// @param xmlTemplates XML-based SBE templates.
44  Encoder(const std::string & xmlTemplates);
45 
46  /// Destructor.
47  ~Encoder(void);
48 
49  /// Encodes the given FIX message into a SBE stream.
50  ///
51  /// @param[in] fixMessage Source FIX message to be encoded.
52  /// @param[in] templateID SBE Template Identifier which uniquely describes the encoding/decoding rules.
53  /// @param[in] version of the template.
54  /// @param[in] buffer Supplied buffer that will contain the SBE stream chunk.
55  /// @param[in] bufferSize Size of the supplied buffer.
56  /// @param[out] rootBlockLength Size of root block, used for the encoded message.
57  ///
58  /// @return the size of the entire encoded SBE message.
59  size_t encode(const OnixS::FIX::Message & fixMessage, int templateID, int version,
60  unsigned char * buffer, size_t bufferSize, size_t * rootBlockLength) const;
61 
62  /// Maximum known version of the SBE schema.
63  unsigned schemaVersion() const;
64 
65  /// Schema identifier.
66  unsigned schemaId() const;
67 
68  /// Semantic version of the SBE schema.
69  std::string schemaSemanticVersion() const;
70 
71  /// Name of the encoding type of the message header, which is
72  /// the same for all messages in a schema.
73  std::string schemaHeaderType() const;
74 private:
75  Encoder(const Encoder &);
76  Encoder & operator = (const Encoder &);
77 
78  struct Impl;
79  Impl * impl_;
80 };
81 
82 /// Performs SBE to FIX decoding.
84 {
85 public:
86  /// Creates a SBE to FIX Decoder.
87  ///
88  /// @param xmlTemplates XML-based SBE templates.
89  /// @param fixDictionary Customized FIX dictionary used for decoded message.
90  Decoder(const std::string & xmlTemplates, const OnixS::FIX::Dictionary & fixDictionary);
91 
92  /// Creates a SBE to FIX Decoder for FIX dictionary-independent mode.
93  /// The generic FIX dictionary is created by the provided SBE-template content, uses FIX 4.0 as base FIX dictionary and has generic name.
94  ///
95  /// @param xmlTemplates XML-based SBE templates.
96  Decoder(const std::string & xmlTemplates);
97 
98  /// Creates a SBE to FIX Decoder for FIX dictionary-independent mode.
99  /// The generic FIX dictionary is created by the provided SBE-template content, uses specified FIX dictionary as base and has generic name.
100  ///
101  /// @param baseVersion Version of FIX protocol which dictionary becomes base for the newly generated FIX dictionary.
102  /// @param xmlTemplates XML-based SBE templates used to generate FIX dictionary. This FIX dictionary takes generic identifier generated on base of the XML contanet and @c baseVersion
103  Decoder(ProtocolVersion::Enum baseVersion, const std::string & xmlTemplates);
104 
105  /// Destructor.
106  ~Decoder();
107 
108  /// Decodes the given SBE stream chunk into the corresponding FIX Message.
109  ///
110  /// @param[in] templateId Identifier of the SBE template, used to decode the input data.
111  /// @param[in] version Version of the SBE schema, used to decode the input data.
112  /// @param[in] rootBlockLength Length of the root block. This value should be extracted from the message preamble, or from other source.
113  /// @param[in] buffer Buffer that contains the SBE stream chunk to be decoded.
114  /// @param[in] bufferSize Size of the buffer.
115  /// @param[out] numberOfDecodedBytes Number of bytes that contained the encoded FIX Message.
116  ///
117  /// @warning The returned message is 'owned' by Decoder.
118  /// @warning The previously decoded message is freed by Decoder when this method is called again.
119  ///
120  /// @throw Exception if the message cannot be decoded.
121  ///
122  /// @return the decoded FIX message.
123  const OnixS::FIX::Message & decode(int templateId, int version, size_t rootBlockLength,
124  const unsigned char * buffer, size_t bufferSize, size_t * numberOfDecodedBytes = NULL) const;
125 
126  /// Tries to decode the given SBE stream buffer into the corresponding FIX Message.
127  ///
128  /// @param[in] templateId Identifier of the SBE template, used to decode the input data.
129  /// @param[in] version Version of the SBE schema, used to decode the input data.
130  /// @param[in] rootBlockLength Length of the root block. This value should be extracted from the message preamble, or from other source.
131  /// @param[in] buffer The SBE stream chunk to be decoded.
132  /// @param[in] offset The index in the buffer at which decoding begins.
133  /// @param[in] count Number of bytes to analyze during the decoding.
134  /// @param[out] message pointer to decoded FIX Message.
135  /// @param[out] numberOfDecodedBytes Number of bytes that contained the encoded FIX Message.
136  ///
137  /// @warning message should point to an existing object. This object is not owned by Decoder and should be managed by user.
138  ///
139  /// @throw Exception if the decoding error is detected.
140  ///
141  /// @return 'true' if the given bytes could be decoded into a FIX message, otherwise - 'false'.
142  bool tryDecode(int templateId, int version, size_t rootBlockLength,
143  const unsigned char * buffer, size_t offset, size_t count, OnixS::FIX::Message * message,
144  size_t * numberOfDecodedBytes) const;
145 
146  /// FIX dictionary used by the decoder instance.
147  ///
148  /// @return FIX dictionary currently used by the decoder instance. If the decoder was initialized with FIX dictionary-independent mode, the method returns
149  /// reference to internally generated FIX dictionary.
150  OnixS::FIX::Dictionary fixDictionary() const;
151 
152  /// Manage maximum number of repeating groups, allowed for decoded messages.
153  ///
154  /// This parameter is used during decoding and is useful to detect a broken data, which, in particular cases,
155  /// can 'need' to allocate unexpectedly huge number of entries (for instance - billions of entries,
156  /// while normally there are just a few ones). This situation results in memory exhausting and (often)
157  /// significantly decrease overall system performance.
158  ///
159  /// To prevent memory exhausting and negative consequences of that, this parameter should be configured with the value,
160  /// which is greater then maximum possible number of entries in valid input data, but less then maximum integer value (default one).
161  /// In a lot of real cases value of 10000 is good enough to detect broken data without memory overloading.
162  ///
163  /// Default is maximum positive value for @c int type.
164  ///
165  /// @param value Number of repeating groups, allowed for decoded messages.
166  void maximumNumberOfRepeatingGroupEntries(int value);
167 
168  /// Maximum number of repeating groups, allowed for decoded messages.
169  ///
170  /// @return Current number of repeating groups, allowed for decoded messages. Default value is maximum positive value for @c int type.
171  int maximumNumberOfRepeatingGroupEntries() const;
172 
173  /// Generates FIX dictionary XML.
174  ///
175  /// @param[in] sbeTemplateXml XML content of SBE template (the same as used to initialize Decoder).
176  ///
177  static Dictionary generateFixDictionary(const std::string & sbeTemplateXml);
178 
179  /// Generates FIX dictionary XML.
180  ///
181  /// @param[in] sbeTemplateXml XML content of SBE template (the same as used to initialize Decoder).
182  /// @param[in] baseVersion Version of FIX protocol which dictionary becomes base for the newly generated FIX dictionary.
183  static Dictionary generateFixDictionary(ProtocolVersion::Enum baseVersion,
184  const std::string & sbeTemplateXml);
185 
186  /// Maximum known version of the SBE schema.
187  unsigned schemaVersion() const;
188 
189  /// Schema identifier.
190  unsigned schemaId() const;
191 
192  /// Semantic version of the SBE schema.
193  std::string schemaSemanticVersion() const;
194 
195  /// Name of the encoding type of the message header, which is
196  /// the same for all messages in a schema.
197  std::string schemaHeaderType() const;
198 private:
200 
201  Decoder(const Decoder &);
202  Decoder & operator = (const Decoder &);
203 
204  struct Impl;
205  Impl * impl_;
206 };
207 
208 }
209 }
210 }
Identifies FIX messages dictionary.
Definition: Dictionary.h:79
ONIXS_FIXENGINE_API_DECL(class, IEngineListener)
Encodes FIX messages into SBE representation.
Definition: SBE.h:38
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
ONIXS_FIXENGINE_API_DECL(class, IDecodeListener)
Performs SBE to FIX decoding.
Definition: SBE.h:83
Encapsulates operations over a FIX Message.
Definition: Message.h:49