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