Ejemplo n.º 1
0
 private void RaiseSocketClosedEvent(SocketCloseReason CloseReason)
 {
     if (SocketClosed != null)
     {
         SocketClosed(this, new SocketClosedEventArgs(CloseReason));
     }
 }
Ejemplo n.º 2
0
        private void CloseConnection(SocketCloseReason closeReason)
        {
            try
            {
                AsynSocketSession session = SocketAsyncEventArgs.UserToken as AsynSocketSession;
                if (session != null)
                {
                    Socket socket = session.Client;
                    socket.SafeCloseSocket();
                    session.SocketConection.SocketAsyncEventArgs.AcceptSocket.SafeCloseSocket();
                    session.SocketConection.SocketAsyncEventArgs.ConnectSocket.SafeCloseSocket();
                }
                SocketAsyncEventArgs.Completed -= ReceivedCompleted;
                if (OnSocketDisconnected != null)
                {
                    OnSocketDisconnected(this, new SocketClientClosedEventArgs(this, closeReason));
                }

                Thread.Sleep(2000);
                SocketAsyncEventArgs.UserToken = null;
                SocketAsyncEventArgs.SetBuffer(null, 0, 0);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// Called when this session is desired or requested to be closed.
        /// </summary>
        /// <param name="reason">Reason this socket is closing.</param>
        public virtual void Close(SocketCloseReason reason)
        {
            //logger.Info("Session {0}: Closing. Reason: {1}", Id, reason);

            // If this session has already been closed, nothing more to do.
            if (CurrentState == State.Closed)
            {
                return;
            }

            // close the socket associated with the client
            try
            {
                Socket.Close(1000);
            }
            catch (Exception)
            {
                // ignored
            }

            _sendArgs.Completed    -= IoCompleted;
            _receiveArgs.Completed -= IoCompleted;

            // Free the SocketAsyncEventArg so they can be reused by another client
            _argsPool.Push(_sendArgs);
            _argsPool.Push(_receiveArgs);

            CurrentState = State.Closed;

            // Notify the session has been closed.
            OnDisconnected(reason);
        }
Ejemplo n.º 4
0
        protected override void OnClose(TSession session, SocketCloseReason reason)
        {
            // Stop the timeout timer.
            _pingTimer.Change(Timeout.Infinite, Timeout.Infinite);

            base.OnClose(session, reason);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 处理Socket客户端断开事件
 /// </summary>
 /// <param name="session"></param>
 /// <param name="reason"></param>
 private void OnClosed(Session session, SocketCloseReason reason)
 {
     //List<Guid> guids = new List<Guid>();
     //bool removeResult = SubscriptionDic.TryRemove(session, out guids);
     connectedCientSession.Remove(session);
     //string result = removeResult ? "成功!" : "失败!";
     //Logger.Debug($"移除客户端{session.SessionID}订阅信息{result}");
     Logger.Debug($"客户端:Session RemoteEndPoint:{session.RemoteEndPoint}已断开! ");
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Called internally with a session on the server closes.
        /// </summary>
        /// <param name="session">Session which closed.</param>
        /// <param name="reason">Reason the session closed.</param>
        protected override void OnClose(TSession session, SocketCloseReason reason)
        {
            TSession outSession;

            ConnectedSessions.TryRemove(session.Id, out outSession);

            session.IncomingMessage -= OnIncomingMessage;
            session.Dispose();
            base.OnClose(session, reason);
        }
        /// <summary>
        /// Method called when a session closes.
        /// </summary>
        /// <param name="session">Session that closed.</param>
        /// <param name="reason">Reason for the closing of the session.</param>
        protected virtual void OnClose(TSession session, SocketCloseReason reason)
        {
            // If there are no clients connected, stop the timer.
            if (ConnectedSessions.IsEmpty)
            {
                TimeoutTimer.Change(Timeout.Infinite, Timeout.Infinite);
                TimeoutTimerRunning = false;
            }

            Closed?.Invoke(this, new SessionClosedEventArgs <TSession, TConfig>(session, reason));
        }
        protected override void OnClose(TSession session, SocketCloseReason reason)
        {
            MainSocket.Close();

            TSession sessOut;

            // If the session is null, the connection timed out while trying to connect.
            if (session != null)
            {
                ConnectedSessions.TryRemove(Session.Id, out sessOut);
            }

            base.OnClose(session, reason);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Closes this session with the specified reason.
        /// Notifies the recipient connection the reason for the session's closure.
        /// </summary>
        /// <param name="reason">Reason for closing this session.</param>
        public override void Close(SocketCloseReason reason)
        {
            if (CurrentState == State.Closed)
            {
                return;
            }

            MqFrame closeFrame = null;

            if (CurrentState == State.Connected)
            {
                CurrentState = State.Closing;

                closeFrame = CreateFrame(new byte[2], MqFrameType.Command);

                closeFrame.Write(0, (byte)0);
                closeFrame.Write(1, (byte)reason);
            }

            // If we are passed a closing frame, then send it to the other connection.
            if (closeFrame != null)
            {
                MqMessage msg;
                if (_outbox.IsEmpty == false)
                {
                    while (_outbox.TryDequeue(out msg))
                    {
                    }
                }

                msg = new MqMessage(closeFrame);
                _outbox.Enqueue(msg);

                // Process the last bit of data.
                ProcessOutbox();
            }

            base.Close(reason);
        }
Ejemplo n.º 10
0
 public void Close(SocketCloseReason closeReason)
 {
     CloseConnection(closeReason);
 }
Ejemplo n.º 11
0
        private void OnClosed(Session session, CloseReason reason)
        {
            SocketCloseReason closeReason = (SocketCloseReason)reason;

            Closed?.Invoke(session, closeReason);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Constructs a SocketClosedException instance with a reason code.
 /// </summary>
 /// <param name="reason">Identifies the exception reason.</param>
 public SocketClosedException(SocketCloseReason reason)
     : base((reason == SocketCloseReason.LocalClose || reason == SocketCloseReason.RemoteClose) ? 10057 /* WSAENOTCONN */ : 10054 /* WSAECONNRESET */)
 {
 }
 /// <summary>
 /// Creates a new instance of the session closed event args.
 /// </summary>
 /// <param name="session">Closed session.</param>
 /// <param name="closeReason">Reason the session was closed.</param>
 public SessionClosedEventArgs(TSession session, SocketCloseReason closeReason)
 {
     Session     = session;
     CloseReason = closeReason;
 }
 /// <summary>
 /// Called when this session is disconnected from the socket.
 /// </summary>
 /// <param name="reason">Reason this socket is disconnecting</param>
 protected virtual void OnDisconnected(SocketCloseReason reason)
 {
     Closed?.Invoke(this, new SessionClosedEventArgs <TSession, TConfig>((TSession)this, reason));
 }
 public SocketClientClosedEventArgs(AsynSocketConnection connection, SocketCloseReason reason)
 {
     CloseReason      = reason;
     SocketConnection = connection;
 }
Ejemplo n.º 16
0
 internal SocketClosedEventArgs(SocketCloseReason closeReason)
     : base()
 {
     this.closeReason = closeReason;
 }