Disconnect() public method

public Disconnect ( string reason ) : void
reason string
return void
示例#1
0
        public bool Read()
        {
            try
            {
                if (socket_.Poll(1000000, SelectMode.SelectRead)) // one-second timeout
                {
                    int bytesRead = socket_.Receive(readBuffer_);
                    if (0 == bytesRead)
                    {
                        throw new SocketException(System.Convert.ToInt32(SocketError.ConnectionReset));
                    }
                    parser_.AddToStream(ref readBuffer_, bytesRead);
                }
                else if (null != session_)
                {
                    session_.Next();
                }
                else
                {
                    throw new QuickFIXException("Initiator timed out while reading socket");
                }

                ProcessStream();
                return(true);
            }
            catch (System.ObjectDisposedException e)
            {
                // this exception means socket_ is already closed when poll() is called
                if (isDisconnectRequested_ == false)
                {
                    // for lack of a better idea, do what the general exception does
                    if (null != session_)
                    {
                        session_.Disconnect(e.ToString());
                    }
                    else
                    {
                        Disconnect();
                    }
                }
                return(false);
            }
            catch (System.Exception e)
            {
                if (null != session_)
                {
                    session_.Disconnect(e.ToString());
                }
                else
                {
                    Disconnect();
                }
                return(false);
            }
        }
        public bool Read()
        {
            try
            {
                int bytesRead = ReadSome(readBuffer_, 1000);
                if (bytesRead > 0)
                {
                    parser_.AddToStream(readBuffer_, bytesRead);
                }
                else if (null != session_)
                {
                    session_.Next();
                }
                else
                {
                    throw new QuickFIXException("Initiator timed out while reading socket");
                }

                ProcessStream();
                return(true);
            }
            catch (System.ObjectDisposedException e)
            {
                // this exception means socket_ is already closed when poll() is called
                if (isDisconnectRequested_ == false)
                {
                    // for lack of a better idea, do what the general exception does
                    if (null != session_)
                    {
                        session_.Disconnect(e.ToString());
                    }
                    else
                    {
                        Disconnect();
                    }
                }
                return(false);
            }
            catch (System.Exception e)
            {
                if (null != session_)
                {
                    session_.Disconnect(e.ToString());
                }
                else
                {
                    Disconnect();
                }
            }
            return(false);
        }
示例#3
0
        /// <summary>
        /// Ad-hoc removal of an existing session
        /// </summary>
        /// <param name="sessionID">ID of session to be removed</param>
        /// <param name="terminateActiveSession">if true, force disconnection and removal of session even if it has an active connection</param>
        /// <returns>true if session removed or not already present; false if could not be removed due to an active connection</returns>
        public bool RemoveSession(SessionID sessionID, bool terminateActiveSession)
        {
            Session session = null;

            if (sessions_.TryGetValue(sessionID, out session))
            {
                if (session.IsLoggedOn && !terminateActiveSession)
                {
                    return(false);
                }
                session.Disconnect("Dynamic session removal");
                foreach (AcceptorSocketDescriptor descriptor in socketDescriptorForAddress_.Values)
                {
                    if (descriptor.RemoveSession(sessionID))
                    {
                        break;
                    }
                }
                sessions_.Remove(sessionID);
                session.Dispose();
                lock (settings_)
                    settings_.Remove(sessionID);
            }
            return(true);
        }
示例#4
0
 public bool Read()
 {
     try
     {
         _log.Info("Read entered calling readsome");
         int bytesRead = ReadSome(readBuffer_, 1000);
         if (bytesRead > 0)
             parser_.AddToStream(System.Text.Encoding.UTF8.GetString(readBuffer_, 0, bytesRead));
         else if (null != session_)
         {
             _log.Info("Read calling sesson Next");
             session_.Next();
             _log.Info("Read called sesson Next");
         }
         else
         {
             throw new QuickFIXException("Initiator timed out while reading socket");
         }
         _log.Info("Read calling sesson ProcessStream");
         ProcessStream();
         _log.Info("Read called sesson ProcessStream");
         return true;
     }
     catch (System.ObjectDisposedException e)
     {
         // this exception means socket_ is already closed when poll() is called
         if (isDisconnectRequested_ == false)
         {
             // for lack of a better idea, do what the general exception does
             if (null != session_)
                 session_.Disconnect(e.ToString());
             else
                 Disconnect();
         }
         return false;                    
     }
     catch (System.Exception e)
     {
         if (null != session_)
             session_.Disconnect(e.ToString());
         else
             Disconnect();
     }
     return false;
 }
示例#5
0
        public void HandleException(Session quickFixSession, System.Exception cause, TcpClient client)
        {
            bool disconnectNeeded = true;
            string reason = cause.Message;

            System.Exception realCause = cause;
            /** TODO
            if(cause is FIXMessageDecoder.DecodeError && cause.InnerException != null)
                realCause = cause.getCause();
            */
            if (realCause is System.Net.Sockets.SocketException)
            {
                string remoteEndPoint = string.Empty;
                try { remoteEndPoint = client.Client.RemoteEndPoint.ToString(); }
                catch { }
                if (quickFixSession != null && quickFixSession.IsEnabled)
                    reason = "Socket exception (" + remoteEndPoint + "): " + cause.Message;
                else
                    reason = "Socket (" + remoteEndPoint + "): " + cause.Message;
                disconnectNeeded = true;
            }
            /** TODO
            else if(realCause is FIXMessageDecoder.CriticalDecodeError)
            {
                reason = "Critical protocol codec error: " + cause;
                disconnectNeeded = true;
            }
            */
            else if(realCause is MessageParseError)
            {
                reason = "Protocol handler exception: " + cause;
                if (quickFixSession == null)
                    disconnectNeeded = true;
                else
                    disconnectNeeded = false;
            }
            else
            {
                reason = cause.ToString();
                disconnectNeeded = false;
            }

            this.Log("SocketReader Error: " + reason);

            if (disconnectNeeded)
            {
                if (null != quickFixSession && quickFixSession.HasResponder)
                    quickFixSession.Disconnect(reason);
                else
                    DisconnectClient(client);
            }
        }
        public bool Read()
        {
            try
            {
                if (socket_.Poll(1000000, SelectMode.SelectRead)) // one-second timeout
                {
                    int bytesRead = socket_.Receive(readBuffer_);
                    if (0 == bytesRead)
                    {
                        throw new SocketException(System.Convert.ToInt32(SocketError.ConnectionReset));
                    }
                    parser_.AddToStream(System.Text.Encoding.UTF8.GetString(readBuffer_, 0, bytesRead));
                }
                else if (null != session_)
                {
                    session_.Next();
                }
                else
                {
                    throw new QuickFIXException("Initiator timed out while reading socket");
                }

                ProcessStream();
                return(true);
            }
            catch (System.Exception e)
            {
                if (null != session_)
                {
                    session_.Disconnect(e.Message);
                }
                else
                {
                    Disconnect();
                }
                return(false);
            }
        }
示例#7
0
        /// <summary>
        /// Ad-hoc removal of an existing session
        /// </summary>
        /// <param name="sessionID">ID of session to be removed</param>
        /// <param name="terminateActiveSession">if true, force disconnection and removal of session even if it has an active connection</param>
        /// <returns>true if session removed or not already present; false if could not be removed due to an active connection</returns>
        public bool RemoveSession(SessionID sessionID, bool terminateActiveSession)
        {
            Session session            = null;
            bool    disconnectRequired = false;

            lock (sync_)
            {
                if (sessionIDs_.Contains(sessionID))
                {
                    session = sessions_[sessionID];
                    if (session.IsLoggedOn && !terminateActiveSession)
                    {
                        return(false);
                    }
                    sessions_.Remove(sessionID);
                    disconnectRequired = IsConnected(sessionID) || IsPending(sessionID);
                    if (disconnectRequired)
                    {
                        SetDisconnected(sessionID);
                    }
                    disconnected_.Remove(sessionID);
                    sessionIDs_.Remove(sessionID);
                }
            }
            lock (_settings)
                _settings.Remove(sessionID);
            if (disconnectRequired)
            {
                session.Disconnect("Dynamic session removal");
            }
            OnRemove(sessionID); // ensure session's reader thread is gone before we dispose session
            if (session != null)
            {
                session.Dispose();
            }
            return(true);
        }
示例#8
0
        private void HandleExceptionInternal(Session quickFixSession, System.Exception cause)
        {
            bool   disconnectNeeded = true;
            string reason           = cause.Message;

            System.Exception realCause = cause;

            // Unwrap socket exceptions from IOException in order for code below to work
            if (realCause is IOException && realCause.InnerException is SocketException)
            {
                realCause = realCause.InnerException;
            }

            /*
             * TODO
             * if(cause is FIXMessageDecoder.DecodeError && cause.InnerException != null)
             *  realCause = cause.getCause();
             */
            if (realCause is System.Net.Sockets.SocketException)
            {
                if (quickFixSession != null && quickFixSession.IsEnabled)
                {
                    reason = "Socket exception (" + tcpClient_.Client.RemoteEndPoint + "): " + cause.Message;
                }
                else
                {
                    reason = "Socket (" + tcpClient_.Client.RemoteEndPoint + "): " + cause.Message;
                }
                disconnectNeeded = true;
            }

            /*
             * TODO
             * else if(realCause is FIXMessageDecoder.CriticalDecodeError)
             * {
             *  reason = "Critical protocol codec error: " + cause;
             *  disconnectNeeded = true;
             * }
             */
            else if (realCause is MessageParseError)
            {
                reason = "Protocol handler exception: " + cause;
                if (quickFixSession == null)
                {
                    disconnectNeeded = true;
                }
                else
                {
                    disconnectNeeded = false;
                }
            }
            else
            {
                reason           = cause.ToString();
                disconnectNeeded = false;
            }

            this.Log("SocketReader Error: " + reason);

            if (disconnectNeeded)
            {
                if (null != quickFixSession && quickFixSession.HasResponder)
                {
                    quickFixSession.Disconnect(reason);
                }
                else
                {
                    DisconnectClient();
                }
            }
        }
示例#9
0
        private void HandleExceptionInternal(Session quickFixSession, System.Exception cause)
        {
            bool disconnectNeeded = true;
            string reason = cause.Message;

            System.Exception realCause = cause;

            // Unwrap socket exceptions from IOException in order for code below to work
            if (realCause is IOException && realCause.InnerException is SocketException)
                realCause = realCause.InnerException;

            /** TODO
            if(cause is FIXMessageDecoder.DecodeError && cause.InnerException != null)
                realCause = cause.getCause();
            */
            if (realCause is System.Net.Sockets.SocketException)
            {
                if (quickFixSession != null && quickFixSession.IsEnabled)
                    reason = "Socket exception (" + tcpClient_.Client.RemoteEndPoint + "): " + cause.Message;
                else
                    reason = "Socket (" + tcpClient_.Client.RemoteEndPoint + "): " + cause.Message;
                disconnectNeeded = true;
            }
            /** TODO
            else if(realCause is FIXMessageDecoder.CriticalDecodeError)
            {
                reason = "Critical protocol codec error: " + cause;
                disconnectNeeded = true;
            }
            */
            else if (realCause is MessageParseError)
            {
                reason = "Protocol handler exception: " + cause;
                if (quickFixSession == null)
                    disconnectNeeded = true;
                else
                    disconnectNeeded = false;
            }
            else
            {
                reason = cause.ToString();
                disconnectNeeded = false;
            }

            this.Log("SocketReader Error: " + reason);

            if (disconnectNeeded)
            {
                if (null != quickFixSession && quickFixSession.HasResponder)
                    quickFixSession.Disconnect(reason);
                else
                    DisconnectClient();
            }
        }