OnixS C++ FIX Engine  4.8.0
API Documentation
FAST.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 FAST {
37 /// Encodes FIX messages into FAST representation.
39 {
40 public:
41  /// Creates a FIX to FAST Encoder.
42  ///
43  /// @param xmlTemplates XML-based FAST templates.
44  /// @param fixVersion FIX Protocol version.
45  /// @param encodeEachMessageIndependently Option to reset the previous values dictionaries before encoding a new FIX message.
46  Encoder(const std::string & xmlTemplates, ProtocolVersion::Enum fixVersion,
47  bool encodeEachMessageIndependently);
48 
49  /// Creates a FIX to FAST Encoder.
50  ///
51  /// @param xmlTemplates XML-based FAST templates.
52  /// @param fixDictionaryId FIX dictionary ID.
53  /// @param encodeEachMessageIndependently Option to reset the previous values dictionaries before encoding a new FIX message.
54  Encoder(const std::string & xmlTemplates, const std::string & fixDictionaryId,
55  bool encodeEachMessageIndependently);
56 
57  /// Creates a FIX to FAST Encoder for FIX dictionary-independent mode.
58  /// The generic FIX dictionary is created by the provided FAST-template content, uses FIX 4.0 as base and has generic name.
59  ///
60  /// @param xmlTemplates XML-based FAST templates.
61  /// @param encodeEachMessageIndependently Option to reset the previous
62  /// values dictionaries before encoding next FIX message.
63  Encoder(const std::string & xmlTemplates, bool encodeEachMessageIndependently);
64 
65  /// Creates a FIX to FAST Encoder for FIX dictionary-independent mode.
66  /// The generic FIX dictionary is created by the provided FAST-template content, uses specified FIX dictionary as base and has generic name.
67  ///
68  /// @param baseVersion Version of FIX protocol which dictionary becomes base for the newly generated FIX dictionary.
69  /// @param xmlTemplates XML-based FAST templates used to generate FIX dictionary. This FIX dictionary takes generic identifier generated on base of the XML contanet and @c baseVersion
70  /// @param encodeEachMessageIndependently Option to reset the previous
71  /// values dictionaries before encoding next FIX message.
72  Encoder(ProtocolVersion::Enum baseVersion, const std::string & xmlTemplates,
73  bool encodeEachMessageIndependently);
74 
75  /// Creates a FIX to FAST Encoder for FIX dictionary-independent mode.
76  /// The generic FIX dictionary is created by the provided FAST-template content, uses specified FIX dictionary as base and has the specified name.
77  /// @note New FIX dictionary instance will be generated if and only if there is no FIX dictionary with the same name.
78  ///
79  /// @param baseVersion Version of FIX protocol which dictionary becomes base for the newly generated FIX dictionary.
80  /// @param xmlTemplates XML-based FAST templates.
81  /// @param genericFixDictionaryId Identifier which is applied to generic FIX dictionary. This FIX dictionary is generated using @c xmlTemplates and @c baseVersion.
82  /// @param encodeEachMessageIndependently Option to reset the previous
83  /// values dictionaries before encoding next FIX message.
84  Encoder(ProtocolVersion::Enum baseVersion, const std::string & xmlTemplates,
85  const std::string & genericFixDictionaryId, bool encodeEachMessageIndependently);
86 
87  /// Destructor.
88  ~Encoder(void);
89 
90  /// Encodes the given FIX message into a FAST stream.
91  ///
92  /// @param fixMessage Source FIX message to be encoded.
93  /// @param templateID FAST Template Identifier which uniquely describes the encoding/decoding rules.
94  /// @param buffer Supplied buffer that will contain the FAST stream chunk.
95  /// @param bufferSize Size of the supplied buffer.
96  ///
97  /// @return the size of the encoded FAST stream chunk.
98  size_t encode(const OnixS::FIX::Message & fixMessage, int templateID, char * buffer,
99  size_t bufferSize); // Deprecated
100  size_t encode(const OnixS::FIX::Message & fixMessage, int templateID, unsigned char * buffer,
101  size_t bufferSize);
102 
103  /// Encodes the given FIX message into a FAST stream using the default template ID.
104  ///
105  /// @param fixMessage Source FIX message to be encoded.
106  /// @param buffer Supplied buffer that will contain the FAST stream chunk.
107  /// @param bufferSize Size of the supplied buffer.
108  ///
109  /// @return the size of the encoded FAST stream chunk.
110  size_t encode(const OnixS::FIX::Message & fixMessage, char * buffer,
111  size_t bufferSize); // Deprecated
112  size_t encode(const OnixS::FIX::Message & fixMessage, unsigned char * buffer,
113  size_t bufferSize);
114 
115  /// Resets the state of the previous values dictionaries (sets the state of the previous values to undefined).
116  ///
117  /// @see encodeEachMessageIndependently.
118  void reset();
119 
120  /// FIX dictionary used by the encoder instance.
121  ///
122  /// @return FIX dictionary currently used by the encoder instance. If the encoder was initialized for FIX dictionary-independent mode, the method returns
123  /// reference to internally generated FIX dictionary.
124  OnixS::FIX::Dictionary fixDictionary() const;
125 
126 private:
128 
129  Encoder(const Encoder &);
130  Encoder & operator = (const Encoder &);
131 
132  struct Impl;
133  Impl * impl_;
134 };
135 
136 /// Performs FAST to FIX decoding.
138 {
139 public:
140  /// Creates a FAST to FIX Decoder.
141  ///
142  /// @param xmlTemplates XML-based FAST templates.
143  /// @param fixDictionary Customized FIX dictionary used for decoded message.
144  /// @param decodeEachMessageIndependently Option to reset the previous
145  /// values dictionaries before decoding a new FAST stream chunk.
146  /// @param inputDataTraits Traits of input data, has effect only when @c decodeEachMessageIndependently is @c false. See InputDataTraits::Enum for details.
147  Decoder(const std::string & xmlTemplates, const OnixS::FIX::Dictionary & fixDictionary,
148  bool decodeEachMessageIndependently, InputDataTraits::Enum inputDataTraits);
149 
150  /// Creates a FAST to FIX Decoder for FIX dictionary-independent mode.
151  /// The generic FIX dictionary is created by the provided FAST-template content, uses FIX 4.0 as base FIX dictionary and has generic name.
152  ///
153  /// @param xmlTemplates XML-based FAST templates.
154  /// @param decodeEachMessageIndependently Option to reset the previous
155  /// values dictionaries before decoding a new FAST stream chunk.
156  /// @param inputDataTraits Traits of input data, has effect only when @c decodeEachMessageIndependently is @c false. See InputDataTraits::Enum for details.
157  Decoder(const std::string & xmlTemplates, bool decodeEachMessageIndependently,
158  InputDataTraits::Enum inputDataTraits);
159 
160  /// Creates a FAST to FIX Decoder for FIX dictionary-independent mode.
161  /// The generic FIX dictionary is created by the provided FAST-template content, uses specified FIX dictionary as base and has generic name.
162  ///
163  /// @param baseVersionForGeneratedDictionary Version of FIX protocol which dictionary becomes base for the newly generated FIX dictionary.
164  /// @param xmlTemplates XML-based FAST templates used to generate FIX dictionary. This FIX dictionary takes generic identifier generated on base of the XML contanet and @c baseVersion
165  /// @param decodeEachMessageIndependently Option to reset the previous
166  /// values dictionaries before decoding a new FAST stream chunk.
167  /// @param inputDataTraits Traits of input data, has effect only when @c decodeEachMessageIndependently is @c false. See InputDataTraits::Enum for details.
168  Decoder(ProtocolVersion::Enum baseVersionForGeneratedDictionary,
169  const std::string & xmlTemplates, bool decodeEachMessageIndependently,
170  InputDataTraits::Enum inputDataTraits);
171 
172  /// Creates a FAST to FIX Decoder for FIX dictionary-independent mode.
173  /// The generic FIX dictionary is created by the provided FAST-template content, uses specified FIX dictionary as base and has the specified name.
174  /// @note New FIX dictionary instance will be generated if and only if there is no FIX dictionary with the same name.
175  ///
176  /// @param baseVersionForGeneratedDictionary Version of FIX protocol which dictionary becomes base for the newly generated FIX dictionary.
177  /// @param xmlTemplates XML-based FAST templates.
178  /// @param genericFixDictionaryId Identifier which is applied to generic FIX dictionary. This FIX dictionary is generated using @c xmlTemplates and @c baseVersion.
179  /// @param decodeEachMessageIndependently Option to reset the previous
180  /// values dictionaries before decoding a new FAST stream chunk.
181  /// @param inputDataTraits Traits of input data, has effect only when @c decodeEachMessageIndependently is @c false. See InputDataTraits::Enum for details.
182  Decoder(ProtocolVersion::Enum baseVersionForGeneratedDictionary,
183  const std::string & xmlTemplates, const std::string & genericFixDictionaryId,
184  bool decodeEachMessageIndependently, InputDataTraits::Enum inputDataTraits);
185 
186  /// Destructor.
187  ~Decoder();
188 
189  /// Input data traits of the decoder.
190  /// @return Input data traits provided during construction of the decoder.
191  InputDataTraits::Enum inputDataTraits() const;
192 
193  /// Decodes the given FAST stream chunk into the corresponding FIX Message.
194  ///
195  /// @param buffer Buffer that contains the FAST stream chunk to be decoded.
196  /// @param bufferSize Size of the buffer.
197  ///
198  /// @warning The returned message is 'owned' by Decoder.
199  /// @warning The previously decoded message is freed by Decoder when this method is called again.
200  ///
201  /// @throw Exception if the message cannot be decoded.
202  ///
203  /// @return the decoded FIX message.
204  const OnixS::FIX::Message & decode(const char * buffer, size_t bufferSize); // Deprecated
205  const OnixS::FIX::Message & decode(const unsigned char * buffer, size_t bufferSize);
206 
207  /// Decodes the given FAST stream chunk into the corresponding FIX Message.
208  ///
209  /// @param chunk The FAST stream chunk to be decoded.
210  ///
211  /// @warning The returned message is 'owned' by Decoder.
212  /// @warning The previously decoded message is freed by Decoder when this method is called again.
213  ///
214  /// @throw Exception if the message cannot be decoded.
215  ///
216  /// @return the decoded FIX message.
217  const OnixS::FIX::Message & decode(const std::vector<char> & chunk); // Deprecated
218  const OnixS::FIX::Message & decode(const OnixS::Sockets::Bytes & chunk);
219 
220  /// Tries to decode the given FAST stream buffer into the corresponding FIX Message.
221  ///
222  /// @param[in] buffer The FAST stream chunk to be decoded.
223  /// @param[in] offset The index in the buffer at which decoding begins.
224  /// @param[in] count Number of bytes to analyze during the decoding.
225  /// @param[out] message pointer to decoded FIX Message.
226  /// @param[out] numberOfDecodedBytes Number of bytes that contained the encoded FIX Message.
227  ///
228  /// @warning message should point to an existing object. This object is not owned by Decoder and should be managed by user.
229  ///
230  /// @throw Exception if the decoding error is detected.
231  ///
232  /// @return 'true' if the given bytes could be decoded into a FIX message, otherwise - 'false'.
233  bool tryDecode(const char * buffer, size_t offset, size_t count, OnixS::FIX::Message * message,
234  size_t * numberOfDecodedBytes); // Deprecated
235  bool tryDecode(const unsigned char * buffer, size_t offset, size_t count,
236  OnixS::FIX::Message * message, size_t * numberOfDecodedBytes);
237 
238  /// Decodes the FAST-encoded unsigned integer.
239  ///
240  /// @param buffer Buffer that contains the FAST stream chunk to be decoded.
241  /// @param bufferSize Size of the buffer.
242  /// @param value Decoded value.
243  /// @param fieldLength Number of bytes that contained the encoded value.
244  ///
245  /// @return 'true' if the stop bit was found and the value was decoded, otherwise - 'false'.
246  static bool tryDecodeUnsignedInteger(const char * buffer, size_t bufferSize,
247  unsigned int * value, size_t * fieldLength); // Deprecated
248  static bool tryDecodeUnsignedInteger(const unsigned char * buffer, size_t bufferSize,
249  unsigned int * value, size_t * fieldLength);
250 
251  /// Resets the state of the previous values dictionaries (sets the state of the previous values to undefined).
252  ///
253  /// @see decodeEachMessageIndependently.
254  void reset();
255 
256  /// FIX dictionary used by the decoder instance.
257  ///
258  /// @return FIX dictionary currently used by the decoder instance. If the decoder was initialized with FIX dictionary-independent mode, the method returns
259  /// reference to internally generated FIX dictionary.
260  OnixS::FIX::Dictionary fixDictionary() const;
261 
262  /// Manage maximum number of repeating groups, allowed for decoded messages.
263  ///
264  /// This parameter is used during decoding and is useful to detect a broken data, which, in particular cases,
265  /// can 'need' to allocate unexpectedly huge number of entries (for instance - billions of entries,
266  /// while normally there are just a few ones). This situation results in memory exhausting and (often)
267  /// significantly decrease overall system performance.
268  ///
269  /// To prevent memory exhausting and negative consequences of that, this parameter should be configured with the value,
270  /// which is greater then maximum possible number of entries in valid input data, but less then maximum integer value (default one).
271  /// In a lot of real cases value of 10000 is good enough to detect broken data without memory overloading.
272  ///
273  /// Default is maximum positive value for @c int type.
274  ///
275  /// @param value Number of repeating groups, allowed for decoded messages.
276  void maximumNumberOfRepeatingGroupEntries(int value);
277 
278  /// Maximum number of repeating groups, allowed for decoded messages.
279  ///
280  /// @return Current number of repeating groups, allowed for decoded messages. Default value is maximum positive value for @c int type.
281  int maximumNumberOfRepeatingGroupEntries() const;
282 
283  /// Generates FIX dictionary XML.
284  ///
285  /// @param[in] fastTemplateXml XML content of FAST template (the same as used to initialize Decoder).
286  ///
287  /// @param[in] fixDictionaryId Output FIX dictionary identifier (see "id" attribute of "FIX" node).
288  /// @param[out] fixDictionaryXml XML with generated FIX dictionary.
289  static void generateFixDictionary(const std::string & fastTemplateXml,
290  const std::string & fixDictionaryId, std::string * fixDictionaryXml);
291 
292  /// Generates FIX dictionary XML.
293  ///
294  /// @param[in] fastTemplateXml XML content of FAST template (the same as used to initialize Decoder).
295  /// @param[in] baseVersion Version of FIX protocol which dictionary becomes base for the newly generated FIX dictionary.
296  /// @param[in] fixDictionaryId Output FIX dictionary identifier (see "id" attribute of "FIX" node).
297  /// @param[out] fixDictionaryXml XML with generated FIX dictionary.
298  static void generateFixDictionary(ProtocolVersion::Enum baseVersion,
299  const std::string & fastTemplateXml, const std::string & fixDictionaryId,
300  std::string * fixDictionaryXml);
301 private:
303 
304  Decoder(const Decoder &);
305  Decoder & operator = (const Decoder &);
306 
307  struct Impl;
308  Impl * impl_;
309 };
310 
311 }
312 }
313 }
Performs FAST to FIX decoding.
Definition: FAST.h:137
std::vector< Byte > Bytes
Sequence of bytes.
Definition: Definitions.h:43
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
Identifies FIX messages dictionary.
Definition: Dictionary.h:73
ONIXS_FIXENGINE_API_DECL(class, IEngineListener)
Encodes FIX messages into FAST representation.
Definition: FAST.h:38
Encapsulates operations over a FIX Message.
Definition: Message.h:49