B2BITS FIX Antenna C++  2.31.0
FIX Session Initiator

Table of Contents

Creating FIX session initiator

Refer to the section FIX Session to read about sessions-initiators. To create session-initiator follow the 2-steps instruction below:

  1. Call one of the methods below to create a session:
    Session *createSession(
    Application *pApp, // a pointer to the Application that will process the incoming messages.
    const std::string& senderCompID, // SenderCompID of the session
    const std::string& targetCompID, // TargetCompID of the session
    FIXVersion ver, // FIX protocol version
    const SessionExtraParameters* pParam = NULL, // an optional session parameters, can be NULL
    const MessageStorageType storageType = persistentMM_storageType, // type of the message storage
    UnderlyingProtocol underlyingProtocolType = FIX_TCP); // session-level FIX protocol version
    Session *createSession(
    Application *pApp, // a pointer to the Application that will process the incoming messages.
    const std::string& senderCompID, // SenderCompID of the session
    const std::string& targetCompID, // TargetCompID of the session
    ProtocolID protocolID, // FIX protocol identifier
    FIXVersion ver, // FIX protocol version
    const SessionExtraParameters* pParam = NULL, // an optional session parameters, can be NULL
    const MessageStorageType storageType = persistentMM_storageType, // type of the message storage
    UnderlyingProtocol underlyingProtocolType = FIX_TCP); // session-level FIX protocol version
  2. After the session is created, use one of the methods below to start session as initiator (session will immediately try to establish connection with counterparty i.e. establish telecommunication link, send FIX Logon message and wait for confirming FIX Logon message from counterparty):

For example:

#include <iostream>
#include <B2BITS_V12.h>
using namespace std;
class Appl : public Engine::Application {
// See "Application description" section
};
try {
// initialization
Appl appl;
// create a session for FIX 4.2
&appl, "SENDER", "TARGET", Engine::FIX42);
// establish the session as initiator
pSn->connect(30, "localhost", 9106)
// ... - business logic
// terminate the FIX session
pSn->disconnect();
// it is vital to do this before the 'release' call
pSn->registerApplication(NULL);
// release resources
pSn->release();
FixEngine::destroy();
} catch(const Exception& ex) {
cout << "EXCEPTION: " << ex.what() << endl;
}

Establish connection

After Engine::Session::connect is called the initiator attempts to establish a FIX session in the background. The following scenario is executed:

Custom Logon message

It is possible to customize the Logon message to be sent by the initiator during connection process. Use the

void Engine::Session::connect(int HBI, const FIXMessage& logonMsg, const std::string& host, int port)

method to send the custom Logon message.

For example:

Appl appl; // see the Application class above
// creates a session
Engine::Session* pSession = Engine::FixEngine::singleton()->createSession(&appl, "sender", "target", version);
// creates a Logon message skeleton
// sets fields
pLogonMsg->set(Engine::FIXField::MaxMessageSize, 20000); // tag 383
// start the session as initiator
pSession->connect(0, *pLogonMsg, "localhost", 9105);

Reconnect

The initiator is responsible for restoring connection once it is broken. When connection error is detected the initiator moves to the "Reconnect" state and starts a reconnection process. The following scenario is executed:

  1. Establish telecommunication link
  2. Send Logon message
  3. Repeat from 1 if 1 or 2 fails
  4. Stop trying if the number of attempts exceeds limit specified in configuration

The number of reconnection attempts and delay can be set in the properties file (see Reconnect.MaxTries and Reconnect.Interval). Refer to Configuration for more details.

Disconnect

Use the following methods to stop session-initiator (this process can be also called "terminate", "delete", "disconnect"):

Release resources

Use the Engine::Session::release() method to release the allocated resources.

It is vital to call Engine::Session::registerApplication(NULL) before calling the release method.

It is vital to call the Engine::Session::release() method only once for the given session.

Send message

To send outgoing application-level messages, use the Engine::Session::put(Engine::FIXMessage *) method. There is no need to send session level messages (e.g. heartbeats), all session level messages are sent automatically when needed by the engine.

Note
It must be mentioned that it is highly NOT recommended to send the same message instance to the multiple sessions in parallel threads. Instead message must be duplicated (Engine::FIXMsgProcessor::clone) as many times as many sessions it is sent to.