Encoding FIX Messages
To encode a FIX message into a FAST stream, the OnixS::FIX::FAST::Encoder class is used.
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.
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());
const bool decodeEachMessageIndependently = true;
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.
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());
const bool decodeEachMessageIndependently = false;
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();
size_t numberOfDecodedBytes = 0;
decoder.reset();
while (decoder.tryDecode(binaryChunk.data(), offset, count, &message, &numberOfDecodedBytes))
{
std::clog << "Decoded " << numberOfDecodedBytes << " bytes: " << message << std::endl;
offset += numberOfDecodedBytes;
count -= numberOfDecodedBytes;
}