Establishing FIX Connection
Session Role
A Session can participate in FIX connection in one of two roles: as an Acceptor or as an Initiator.
The Acceptor is the receiving party of the FIX session. It listens for an incoming connection on the pre-defined port. The Acceptor has the responsibility to perform first level authentication and formally declares the connection request "accepted" through transmission of an acknowledgment Logon message.
The Initiator establishes the telecommunications link and initiates the session via transmission of the initial Logon message.
To determine the role of the session use the Role property.
Establishing Connection
To establish a FIX connection as an Acceptor use the LogonAsAcceptor() method.
To establish a FIX connection as an Initiator use the LogonAsInitiator(string, int, int, bool, IMessage, bool) method.
For example:
const string SenderCompId = "SenderCompID";
const string SargetCompId = "TargetCompID";
const ProtocolVersion Version = ProtocolVersion.Fix44;
const string CounterpartyHost = "localhost";
// In this sample the target port is equal to acceptor's port to create a loop-back FIX session.
int CounterpartyPort = Engine.Instance.Settings.ListenPorts[0];
using Session acceptor = new Session(SenderCompId, SargetCompId, Version);
acceptor.LogonAsAcceptor();
using (Session initiator = new Session(SargetCompId, SenderCompId, Version))
{
// Sends the Logon message and waits for the acknowledgment Logon.
initiator.LogonAsInitiator(CounterpartyHost, CounterpartyPort);
// Message exchange and processing logic
// Sends the Logout message and waits for the confirming Logout.
initiator.Logout();
}
acceptor.Logout();
Closing Connection
To disconnect the session use the Logout(bool) method. This member implements a graceful closing of the FIX connection as defined by the FIX Protocol Standard.
Note
LogonAsInitiator(string, int, int, bool, IMessage, bool) and Logout(bool) methods
are blocked until the Logon/Logout response message is received. These methods have a timeout during which the response should be received.
This timeout is equal to the Heartbeat interval (by default 30
sec) + ReasonableTransmissionTime value (by default 20%
)
as a percentage from the heartbeat interval. If the acknowledgment Logon/Logout message is not received during the timeout, then the Logout message will be sent and the FIX connection will be closed.
Reconnection Service
When a FIX session is in the active state, and there are some network issues the FIX Engine will try to restore the FIX connection per ReconnectAttempts and ReconnectInterval settings.
Note
Reconnection service works for already established connections. In other cases, you need to use the Sessions Scheduler component or manually handle LinkErrorException or LogonReplyTimeoutException exceptions of LogonAsInitiator(string, int, int, bool, IMessage, bool) method and try to connect again.
Connection and Session Lifetime
A single FIX session can exist across multiple sequential (not concurrent) physical connections.
Parties can connect and disconnect multiple times while maintaining a single FIX session.
Once the Logout(bool) method is called, the FIX Connection is terminated; however, the FIX session life continues. It is possible to continue the same session later using either LogonAsAcceptor() or LogonAsInitiator(string, int, int, bool, IMessage, bool) methods again.
In other words, a FIX Session consists of one or more FIX connections.