Exemplo n.º 1
0
        /// <summary>
        /// The HandleConnect_Entrypoint method is the asynchronous
        /// entrypoint for the session's connect handling logic.
        /// </summary>
        /// <param name="state">
        /// Ignored parameter; required for the method to meet the
        /// required signature for use with the sequencer.
        /// </param>
        private void HandleConnect_Entrypoint(object state)
        {
            // REC: Transition the session to the active state so
            // that it can begin handling messages:
            _currentState = SessionStates.Session_Pending;

            // REC: Reset the receive timestamp:
            this._lastRxTicks = DateTime.Now.Ticks;

            // REC: Reset the transmit timestamp:
            this._lastTxTicks = DateTime.Now.Ticks;


            // REC: Acquire the session's persisted session
            // state from the database:
            if (_fixDatabase != null)
            {
                _sessionRecord = _fixDatabase.AcquireSession(_sessionId);
            }

            // REC: The session's timer is started in order
            // to drive the heartbeat and timeout logic:
            this._fixTimer.Start();

            _handler.OnSessionOpened(this);

            // REC: The client session needs to dispatch a logon
            // message to the peer as soon as it connects:
            FixMessage msgLogon = _fixAssembler.CreateMessage(_sxVersion, _axVersion, "A");

            if (msgLogon != null)
            {
                Dispatch_AdmMessage(msgLogon);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// The HandleSession_Logon method is invoked to handle
        /// a logon message that has been received from the peer
        /// session that this session is communicating with.
        /// </summary>
        /// <param name="msg">
        /// The FIX logon message received from the peer.
        /// </param>
        private void HandleSession_Logon(FixMessage msg)
        {
            // REC: Ensure that we're in the appropriate state
            // to handle a logon message:
            if (_currentState == SessionStates.Session_Pending || _currentState == SessionStates.Session_Closed)
            {
                // REC: Retrieve the FIX SenderCompID of the peer
                // from the logon message:
                FixField fldSenderCompID = msg.Header.GetField(49);
                if (fldSenderCompID != null)
                {
                    // REC: The SenderCompID from the peer becomes the
                    // session's TargetCompID for outgoing messages:
                    _fixTargetCompID = fldSenderCompID.Content;

                    // REC: Now that the FIX SenderCompID of the peer
                    // session is known, it is used to construct the
                    // identifier for the session - used to retrieve
                    // the session's details from the database:
                    _sessionId = string.Format("{0}-{1}", _fixSenderCompID, _fixTargetCompID);

                    // REC: Attempt to retrieve the session details
                    // from the session database:
                    if (_fixDatabase != null)
                    {
                        // REC: Note that once a session record is acquired
                        // from the database, it must be released when it is
                        // no longer needed by the session instance...
                        _sessionRecord = _fixDatabase.AcquireSession(_sessionId);
                        if (this._resetSequence == true)
                        {
                            _sessionRecord.RxSequence = 1;
                            _sessionRecord.TxSequence = 1;
                        }
                    }

                    // REC: Register the peer session's SenderCompID as
                    // the TargetCompID for outgoing messages:
                    _fixAssembler.SetField(new FixField(56, _fixTargetCompID));

                    // REC: Assemble the response message:
                    FixMessage response = _fixAssembler.CreateMessage(_sxVersion, _axVersion, "A");

                    // REC: Handle the sequence number reset flag
                    // if it is present in the logon message:
                    FixField fieldReset = msg.GetField(141);
                    if (fieldReset != null)
                    {
                        response.AddField(new FixField(141, "Y"));
                    }

                    // REC: Transition to the opened state:
                    _currentState = SessionStates.Session_Opened;

                    // REC: Notify the session's owner that the peer
                    // session has sent the logon request:
                    _handler.OnSessionLogon(this, msg);

                    // REC: Dispatch the administrative message out
                    // to the session's owner:
                    Dispatch_AdmMessage(response);
                }
                else
                {
                    // REC: Throw an exception since the session has not
                    // yet been established and there will be no handler
                    // at the application level that can deal with this:
                    throw new ArgumentException("Logon message missing required field - SenderCompID.");
                }
            }
            else
            {
                // REC: Notify the session handler that an
                // administrative message has been received
                // from the peer session:
                _handler.OnSessionRxAdmMessage(this, msg);

                // REC: Throw an exception that indicates the
                // session is not in the appropriate state to
                // handle the received message:

                //throw new InvalidOperationException("Session state invalid for received message.");
            }
        }