FIX Session
Persistent session
All messages are saved in persistent storage.
Session data is stored to hard drive and can be restored after failure. The following session information is stored in this case:
- Session parameters (SenderCompID, TargetCompID, FIX version etc.)
- Session incoming sequence number
- Incoming messages (optional)
- Outgoing messages
Session state
From the moment of session creation to the moment of its termination
the session uses a state that dictates its reaction to events.
The session state can be obtained using the session.GetSessionState()
method, which returns SessionState
.
SessionState
class provides a set of predefined public static fields for each state available. SessionState.ToString()
returns values listed below:
- CONNECTING - connection process is in progress
- WAITING_FOR_LOGON - connection has been estabished, but Logon has not been received yet.
- CONNECTED - session is connected
- WAITING_FOR_LOGOFF - waiting for a Logoff message
- DISCONNECTED - session is disconnected
- LOGON_RECEIVED - Logon received (for acceptor sessions)
- DEAD - session is disposed
- DISCONNECTED_ABNORMALLY - session has been disconnected abnormally
- RECONNECTING - session is reconnecting
- WAITING_FOR_FORCED_LOGOFF - session is waiting for Logout trying to terminate gracefully due to the certain FIX condition, but will be disconnected after a while even if Logout is not received. Engine will wait for Logout answer during next heartbeat interval.
- WAITING_FOR_FORCED_DISCONNECT - session was disconnected due to error (for example, received lower sequence number). The Logoff was sent to counterpartн and session is waiting a bit () before close connection. According to FIX specifications the engine ignores all incoming messages in this state.
Sequence number handling
A session always has two sequence numbers:
- Incoming sequence number is a counter of incoming messages
- Outgoing sequence number is a counter of outgoing messages
Both sides must maintain those two values and control their synchronization. There are two types of sequence numbers desynchronization:
- Sequence number too high indicates message loss and leads to resend procedure.
- Sequence number too low indicates some serios problem and must lead to immediate session termination and manual sequence number synchronization.
Both sequence numbers are started from 1 when session is created from scratch. If connection is terminated, sequence numbers continue after restoration. A lot of service providers never reset sequence numbers during the day. The are also some, that reset sequence numbers once per week. There are several cases to handle such a deviation from the standard in FIX Antenna
Resetting sequence number
To reset sequence numbers simply set incomingSequenceNumber
and
outgoingSequenceNumber
in SessionParameters
to 1 or any other desired value.
By default it is set to 0 (automatically restore sequence number).
It is also possible to remove FIX session persistent files from logs
directory. Since FIX Antenna stores session state in those files,
absence of such files will be treated as session being created from scratch
and thus sequence numbers will be set to 1. Files can be removed during
the End-Of-Day or End-Of-Week procedure.
- ForceSeqNumReset mode makes the session reset sequence number each time on Logon and force a counter-party to do the same (standard FIX mechanism). This is not a recommended way since messages sent during inactivity time will be lost.
Session qualifier
Session qualifier gives user the ability to create several sessions with the same SenderCompId
and TargetCompId
and to give ability to address these session by unique ID.
To establish several sessions with the same SenderCompId
and TargetCompId
, both Initiator and Acceptor should create several session objects which differ from each other by SessionQualifier property. SessionQualifier property on Initiator side must be the same as SessionQualifier property of the corresponding session on acceptor side. When Initiator connects to Acceptor, it sends SessionQualifier along with SenderCompId and TargetCompId in logon message. The SessionQualifier tag in logon message is optional and can be configured in fixengine.properties file. When Acceptor receives logon message with the SessionQualifier it searches for registered session with SessionId corresponding to received TargetCompId, SenderCompId and SessionQualifier.
The FIX engine will automatically insert SessionQualifier tag into logon message if user creates session with qualifier on initiator side and SessionQualifier tag is appropriately configured in fixengine.properties. To configure SessionQualifier tag user needs to add the lines like below to config file:
logonMessageSessionQualifierTag = 9012
To create session with qualifier you need set unique qualifier to SessionParameters:
// define session
SessionParameters details = new SessionParameters();
details.SetSenderCompId("senderId");
details.SetTargetCompId("targetId");
// set session qualifier
details.SetSessionQualifier("1");