Files
2024-09-23 12:00:04 -04:00

248 lines
12 KiB
C++

#pragma once
/*
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
*
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
* and international copyright treaties.
*
* Access to and use of the software is governed by the terms of the applicable ONIXS Software
* Services Agreement (the Agreement) and Customer end user license agreements granting
* a non-assignable, non-transferable and non-exclusive license to use the software
* for it's own data processing purposes under the terms defined in the Agreement.
*
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
* of this source code or associated reference material to any other location for further reproduction
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
*
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
* the terms of the Agreement is a violation of copyright law.
*/
#include <string>
#include <vector>
#include <map>
#include <OnixS/FIXEngine/ABI.h>
#include <OnixS/FIXEngine/Compiler.h>
#include <OnixS/FIXEngine/FIX/Tag.h>
#include <OnixS/FIXEngine/FIX/ProtocolVersion.h>
#include <OnixS/FIXEngine/FIX/SbeCustomization.h>
namespace OnixS {
namespace FIX {
ONIXS_FIXENGINE_API_DECL(class, Message);
ONIXS_FIXENGINE_API_DECL(class, Dictionary);
namespace SBE {
/// Encodes FIX messages into the SBE representation.
class ONIXS_FIXENGINE_API Encoder
{
public:
/// Creates a FIX to SBE Encoder.
///
/// @param xmlTemplates XML-based SBE templates.
/// @param customCoders The custom coders library.
Encoder(const std::string & xmlTemplates, ISbeCustomCoderLibrary *customCoders = ONIXS_FIXENGINE_NULLPTR);
/// Destructor.
~Encoder(void);
/// Encodes the given FIX message into a SBE stream.
///
/// @param[in] fixMessage The source FIX message to be encoded.
/// @param[in] templateID the SBE Template Identifier which uniquely describes the encoding/decoding rules.
/// @param[in] version The version of the template.
/// @param[in] buffer The supplied buffer that will contain the SBE stream chunk.
/// @param[in] bufferSize The size of the supplied buffer.
/// @param[out] rootBlockLength The size of the root block, used for the encoded message.
///
/// @return The size of the entire encoded SBE message.
size_t encode(const OnixS::FIX::Message & fixMessage, int templateID, int version,
unsigned char * buffer, size_t bufferSize, size_t * rootBlockLength) const;
/// Encodes the given FIX message into a SBE stream and prepends the message header.
///
/// @param[in] fixMessage The source FIX message to be encoded.
/// @param[in] templateID The SBE Template Identifier which uniquely describes the encoding/decoding rules.
/// @param[in] version The version of the template.
/// @param[in] buffer The supplied buffer that will contain the SBE stream chunk.
/// @param[in] bufferSize The size of the supplied buffer.
/// @param[out] rootBlockLength The size of the root block, used for the encoded message.
///
/// @return The size of the entire encoded SBE message.
size_t encodeWithHeader(const OnixS::FIX::Message& fixMessage, int templateID, int version,
unsigned char* buffer, size_t bufferSize, size_t* rootBlockLength) const;
/// The maximum known version of the SBE schema.
unsigned schemaVersion() const;
/// The schema identifier.
unsigned schemaId() const;
/// The semantic version of the SBE schema.
std::string schemaSemanticVersion() const;
/// The name of the encoding type of the message header, which is
/// the same for all messages in a schema.
std::string schemaHeaderType() const;
private:
Encoder(const Encoder &);
Encoder & operator = (const Encoder &);
struct Impl;
Impl * impl_;
};
/// Performs SBE to FIX decoding.
class ONIXS_FIXENGINE_API Decoder
{
public:
/// Creates a SBE to FIX Decoder.
///
/// @param xmlTemplates XML-based SBE templates.
/// @param fixDictionary The customized FIX dictionary used for the decoded message.
/// @param customCoders The custom coders library.
Decoder(const std::string & xmlTemplates, const OnixS::FIX::Dictionary & fixDictionary, ISbeCustomCoderLibrary* customCoders = ONIXS_FIXENGINE_NULLPTR);
/// Creates a SBE to FIX Decoder for the FIX dictionary-independent mode.
/// 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.
///
/// @param xmlTemplates XML-based SBE templates.
/// @param customCoders The custom coders library.
Decoder(const std::string & xmlTemplates, ISbeCustomCoderLibrary* customCoders = ONIXS_FIXENGINE_NULLPTR);
/// Creates a SBE to FIX Decoder for the FIX dictionary-independent mode.
/// 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.
///
/// @param baseVersion The version of the FIX protocol which the dictionary becomes a base for the newly generated FIX dictionary.
/// @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
/// @param customCoders The custom coders library.
Decoder(ProtocolVersion::Enum baseVersion, const std::string & xmlTemplates, ISbeCustomCoderLibrary* customCoders = ONIXS_FIXENGINE_NULLPTR);
/// The destructor.
~Decoder();
/// Decodes the given SBE stream chunk into the corresponding FIX Message.
///
/// @param[in] templateId The identifier of the SBE template, used to decode the input data.
/// @param[in] version The version of the SBE schema, used to decode the input data.
/// @param[in] rootBlockLength The length of the root block. This value should be extracted from the message preamble, or from other source.
/// @param[in] buffer The buffer that contains the SBE stream chunk to be decoded.
/// @param[in] bufferSize The size of the buffer.
/// @param[out] numberOfDecodedBytes The number of bytes that contains the encoded FIX Message.
///
/// @warning The returned message is 'owned' by the Decoder.
/// @warning The previously decoded message is freed by the Decoder when this method is called again.
///
/// @throw An exception if the message cannot be decoded.
///
/// @return The decoded FIX message.
const OnixS::FIX::Message & decode(int templateId, int version, size_t rootBlockLength,
const unsigned char * buffer, size_t bufferSize, size_t * numberOfDecodedBytes = ONIXS_FIXENGINE_NULLPTR) const;
/// Decodes the given SBE stream chunk into the corresponding FIX Message using the message header.
///
/// This method decodes the given SBE stream chunk using the message header decoder that was built on a base of the SBE template.
///
/// @param[in] buffer The buffer that contains the SBE stream chunk to be decoded.
/// @param[in] bufferSize The size of the buffer.
/// @param[out] numberOfDecodedBytes The number of bytes that contained the encoded FIX Message.
/// @param[out] templateId The identifier of the SBE template, that was used to decode the input data.
/// @param[out] version The version of the SBE schema, that was used to decode the input data.
///
/// @warning The returned message is 'owned' by the Decoder.
/// @warning The previously decoded message is freed by the Decoder when this method is called again.
///
/// @throw An exception if the message cannot be decoded.
///
/// @return The decoded FIX message.
const OnixS::FIX::Message& decode(const unsigned char* buffer, size_t bufferSize, size_t* numberOfDecodedBytes = ONIXS_FIXENGINE_NULLPTR,
int *templateId = ONIXS_FIXENGINE_NULLPTR, int *version = ONIXS_FIXENGINE_NULLPTR) const;
/// Tries to decode the given SBE stream buffer into the corresponding FIX Message.
///
/// @param[in] templateId The identifier of the SBE template, used to decode the input data.
/// @param[in] version The version of the SBE schema, used to decode the input data.
/// @param[in] rootBlockLength The length of the root block. This value should be extracted from the message preamble, or from other source.
/// @param[in] buffer The SBE stream chunk to be decoded.
/// @param[in] offset The index in the buffer at which decoding begins.
/// @param[in] count The number of bytes to analyze during the decoding.
/// @param[out] message The pointer to the decoded FIX Message.
/// @param[out] numberOfDecodedBytes The number of bytes that contained the encoded FIX Message.
///
/// @warning the message should point to an existing object. This object is not owned by the Decoder and should be managed by a user.
///
/// @throw An exception if the decoding error is detected.
///
/// @return 'true' if the given bytes could be decoded into a FIX message, otherwise - 'false'.
bool tryDecode(int templateId, int version, size_t rootBlockLength,
const unsigned char * buffer, size_t offset, size_t count, OnixS::FIX::Message * message,
size_t * numberOfDecodedBytes) const;
/// The FIX dictionary used by the decoder instance.
///
/// @return The FIX dictionary currently used by the decoder instance. If the decoder was initialized with the FIX dictionary-independent mode, the method returns
/// a reference to the internally generated FIX dictionary.
OnixS::FIX::Dictionary fixDictionary() const;
/// Manage the maximum number of repeating groups, allowed for decoded messages.
///
/// This parameter is used during the decoding and is useful to detect a broken data, which, in particular cases,
/// can 'need' to allocate an unexpectedly huge number of entries (for an instance - billions of entries,
/// while normally there are just a few ones). This situation results in a memory exhausting and (often)
/// significantly decrease the overall system performance.
///
/// To prevent the memory exhausting and negative consequences of that, this parameter should be configured with the value,
/// which is greater then maximum possible number of entries in the valid input data, but less then the maximum integer value (default one).
/// In a lot of real cases value of 10000 is good enough to detect broken data without a memory overloading.
///
/// Default is the maximum positive value for the @c int type.
///
/// @param value The number of repeating groups, allowed for decoded messages.
void maximumNumberOfRepeatingGroupEntries(int value);
/// The maximum number of repeating groups, allowed for decoded messages.
///
/// @return The current number of repeating groups, allowed for decoded messages. The default value is the maximum positive value for the @c int type.
int maximumNumberOfRepeatingGroupEntries() const;
/// Generates the FIX dictionary XML.
///
/// @param[in] sbeTemplateXml The XML content of the SBE template (the same as used to initialize the Decoder).
///
static Dictionary generateFixDictionary(const std::string & sbeTemplateXml);
/// Generates the FIX dictionary XML.
///
/// @param[in] sbeTemplateXml The XML content of the SBE template (the same as used to initialize the Decoder).
/// @param[in] baseVersion The version of the FIX protocol which the dictionary becomes a base for the newly generated FIX dictionary.
static Dictionary generateFixDictionary(ProtocolVersion::Enum baseVersion,
const std::string & sbeTemplateXml);
/// The maximum known version of the SBE schema.
unsigned schemaVersion() const;
/// The schema identifier.
unsigned schemaId() const;
/// The semantic version of the SBE schema.
std::string schemaSemanticVersion() const;
/// The name of the encoding type of the message header, which is
/// the same for all messages in a schema.
std::string schemaHeaderType() const;
private:
friend ONIXS_FIXENGINE_API_DECL(class, OnixS::FIX::Dictionary);
Decoder(const Decoder &);
Decoder & operator = (const Decoder &);
struct Impl;
Impl * impl_;
};
}
}
}