OnixS C++ FIX Engine  4.3.0
API Documentation
FAST Protocol Decoding and Encoding using Message object

Encoding FIX Messages

To encode a FIX message into a FAST stream, the OnixS::FIX::FAST::Encoder class is used.

using namespace OnixS::FIX;
using namespace OnixS::FIX::FAST;
std::string xmlFastTemplate = readTextFile("UdpFastTemplate.xml");
const bool EncodeEachMessageIndependently = true;
Encoder encoder(xmlFastTemplate, sourceFixMessage.dictionary().version(), EncodeEachMessageIndependently);
const int FastTemplateID = 36;
const size_t BufferSize = 1024;
char fastStreamChunk[BufferSize];
size_t chunkSize = encoder.encode(sourceFixMessage, FastTemplateID, fastStreamChunk, BufferSize);

Decoding FAST binary chunks

To decode a part of a FAST stream back into a FIX message, the OnixS::FIX::FAST::Decoder class is used.

using namespace OnixS::FIX;
using namespace OnixS::FIX::FAST;
EngineSettings settings;
settings.listenPort(-1)
.dictionaryFile("dictionaries/FixDictionary_FDMM.xml");
Engine::init(settings);
std::ifstream templatesStream("templates/FastTemplates_FDMM.xml");
assert(templatesStream);
const std::string xmlFastTemplates((std::istreambuf_iterator<char>(templatesStream)), std::istreambuf_iterator<char>());
assert(!xmlFastTemplates.empty());
Dictionary dictionary("FDMM");
const bool decodeEachMessageIndependently = true;
// Please note that inputDataTraits parameter has no matter since decodeEachMessageIndependently == true
FAST::Decoder decoder(xmlFastTemplates, dictionary, decodeEachMessageIndependently, InputDataTraits::CompleteMessagesOnly);
std::ifstream dataStream("data/fastChunk_FDMM.bin", std::ios_base::in | std::ios_base::binary);
assert(dataStream);
const std::string binaryChunk((std::istreambuf_iterator<char>(dataStream)), std::istreambuf_iterator<char>());
assert(!binaryChunk.empty());
const Message& message = decoder.decode(binaryChunk.data(), binaryChunk.size());
std::clog << "Decoded: " << message << std::endl;

Decoding multiple FIX Messages from the same binary chunk.

To decode multiple FIX Messages from the same binary chunk the OnixS::FIX::FAST::Decoder::tryDecode method is used.

Note
Quite often it is needed to reset the Decoder state (using the OnixS::FIX::FAST::Decoder::reset method) before decoding a new FAST binary chunk.
using namespace OnixS::FIX;
using namespace OnixS::FIX::FAST;
EngineSettings settings;
settings.listenPort(-1)
.dictionaryFile("dictionaries/FixDictionary_FDMM.xml");
Engine::init(settings);
std::ifstream templatesStream("templates/FastTemplates_FDMM.xml");
assert(templatesStream);
const std::string xmlFastTemplates((std::istreambuf_iterator<char>(templatesStream)), std::istreambuf_iterator<char>());
assert(!xmlFastTemplates.empty());
Dictionary dictionary("FDMM");
const bool decodeEachMessageIndependently = false;
// Please note that only InputDataTraits::CouldContainPartialMessages value could be used here.
FAST::Decoder decoder(xmlFastTemplates, dictionary, decodeEachMessageIndependently, InputDataTraits::CouldContainPartialMessages);
std::ifstream dataStream("data/fastChunk_FDMM.bin", std::ios_base::in | std::ios_base::binary);
assert(dataStream);
const std::string binaryChunk((std::istreambuf_iterator<char>(dataStream)), std::istreambuf_iterator<char>());
assert(!binaryChunk.empty());
size_t offset = 0;
size_t count = binaryChunk.length();
Message message;
size_t numberOfDecodedBytes = 0;
decoder.reset(); // to make sure that we start the FAST decoding from scratch.
while (decoder.tryDecode(binaryChunk.data(), offset, count, &message, &numberOfDecodedBytes))
{
std::clog << "Decoded " << numberOfDecodedBytes << " bytes: " << message << std::endl;
offset += numberOfDecodedBytes;
count -= numberOfDecodedBytes;
}