FIX session protocol pseudocode

Imported from previous forum

Hi all,
I have been playing around with an implementation of FIX protocol for a while.

Please take a look at the psueodcode at:

If you have any mistakes or have any suggestions, I’ll be more than happy to hear from you. You are welcome to send me a pull request as well.

The idea here is that this is basically a minimal FIX session processor. For example, I don’t try to confirm that the data types are correct. I pass on application level message to the application.

I want the session level logic to be production ready, but not try to do anything more than what is minimally required.

btw, ‘ignore msg’ means don’t send the message on to the rest of the application.

Following is pasted text of what you will find in the link above:

//Shahbaz Chaudhary
//shahbazc gmail com

//Persistent variables (survive session crash)
expectedInSeqNum = 1
expectedOutSeqNum = 1 (assume automatically set)

//Session level variables (do not survive session crash)
isLoggedIn = false;
isResendRequested = false;

timeOfLastIn = none
timeOfLastOut = none (assume automatically set)

sender = nothing
target = nothing
version = nothing

msg = valid message arrives
timeOfLastIn = now

//first message must be logon
if not isLoggedIn and msg is not [logon]
ignore msg
error "first message must be [logon]"
disconnect connection

//process logon
if not isLoggedIn and msg is [logon]
if
isLoggedIn = true;
sender = msg[senderCompID]
target = msg[targetCompID]
version = msg[version]
send [logon ack]
else
ignore msg
error "unknown connection"
disconnect connection

//record msg (needed for resend request)
record msg

//duplicate logon
if isLoggedIn and msg is [logon]
ignore msg
error "already logged in"
send [session reject]

if msg is [sequence-reset] and gap-fill is undefined or N
if newseqnum >= expectedInSeqNum
expectedInSeqNum = newseqnum
else
ignore msg
error "can’t reduce seqnums! manual intervention needed"
disconnect connection

//check seqnums
if inSeqNum = expectedInSeqNum
increment expectedInSeqNum
isResendRequested = false
else if inSeqNum < expectedInSeqNum and posdup is Y
ignore msg
else if inSeqNum < expectedInSeqNum and posdup is undefined or N
ignore msg
error "manual intervention needed!"
disconnect connection
else if inSeqNum > expectedInSeqNum
ignore msg
if msg is [resend-request]
for list of requested messages from record
if msg is [logon, logout, resendreq, heartbeat, testreq, seqreset]
send [seq-reset] with gap-fill Y
send msg with posdup = Y
if isResendRequested is false
isResendRequested = true
send [resend-request] for missing messages

if msg is [sequence-reset] and gap-fill is Y
if newseqnum >= expectedInSeqNum
expectedinSeqNum = newseqnum
else
ignore msg
error "can’t reduce seqnums! manual intervention needed"
disconnect connection

//check compids and version
if sender, target or version don’t match
ignore msg
error "sender, target and version must match logon"
send [session-reject]

//real session logic
if msg is [logon]
every heartBeatInt seconds call
if (now - timeOfLastOut) > heartBeatInt
send [heartbeat]
if (now - timeOfLastIn) > heartBeatInt
send [test request]
if (now - timeOfLastIn) > (1.5 * heartBeantInt)
error "no heartbeats from counter party, time to die"
disconnect session

if msg is [logout]
send [logout ack]

if msg is [test request]
send [heartbeat] with test request id

if msg is [resend request]
for list of requested messages from record
if msg is [logon, logout, resendreq, heartbeat, testreq, seqreset]
send [seq-reset] with gap-fill Y
send msg with posdup = Y