예제 #1
0
        public void Close(int statusCode, string reason)
        {
            //The websocket never be opened
            if (Interlocked.CompareExchange(ref m_StateCode, WebSocketStateConst.Closed, WebSocketStateConst.None)
                == WebSocketStateConst.None)
            {
                OnClosed();
                return;
            }

            //The websocket is connecting or in handshake
            if (Interlocked.CompareExchange(ref m_StateCode, WebSocketStateConst.Closing, WebSocketStateConst.Connecting)
                == WebSocketStateConst.Connecting)
            {
                var client = Client;

                if (client != null && client.IsConnected)
                {
                    client.Close();
                    return;
                }

                OnClosed();
                return;
            }

            m_StateCode = WebSocketStateConst.Closing;

            //Disable auto ping
            ClearTimer();
            //Set closing hadnshake checking timer
            m_WebSocketTimer = new Timer(CheckCloseHandshake, null, 5 * 1000, Timeout.Infinite);

            ProtocolProcessor.SendCloseHandshake(this, statusCode, reason);
        }
예제 #2
0
 public void Close(int statusCode, string reason)
 {
     m_ClosedArgs = new ClosedEventArgs((short)statusCode, reason);
     if (Interlocked.CompareExchange(ref m_StateCode, 3, -1) == -1)
     {
         OnClosed();
     }
     else if (Interlocked.CompareExchange(ref m_StateCode, 2, 0) == 0)
     {
         TcpClientSession client = Client;
         if (client != null && client.IsConnected)
         {
             client.Close();
         }
         else
         {
             OnClosed();
         }
     }
     else
     {
         m_StateCode = 2;
         ClearTimer();
         m_WebSocketTimer = new Timer(CheckCloseHandshake, null, 5000, -1);
         try
         {
             ProtocolProcessor.SendCloseHandshake(this, statusCode, reason);
         }
         catch (Exception e)
         {
             if (Client != null)
             {
                 OnError(e);
             }
         }
     }
 }
예제 #3
0
 public void Close(int statusCode, string reason)
 {
     State = WebSocketState.Closing;
     ProtocolProcessor.SendCloseHandshake(this, statusCode, reason);
 }