Avalon.Network.SocketClient.Disconnect C# (CSharp) Method

Disconnect() public method

Terminates the current socket and removes it from the client list.
public Disconnect ( ) : void
return void
        public void Disconnect()
        {
            if (m_Client != null && m_Client.Socket.Connected != false)
            {
                lock (Program.AvalonSrv.ClientList)
                {
                    Program.AvalonSrv.ClientList.Remove(this);
                }

                Logger.Log(Logger.LogLevel.Access, "Client", "[{0}] Client Disconnected : {1}", Program.AvalonSrv.ClientList.Count, ((IPEndPoint)m_Client.Socket.RemoteEndPoint).Address.ToString());

                // close the socket
                m_Client.Socket.Shutdown(SocketShutdown.Both);
                m_Client.Socket.Close();
            }
            else
            {
                lock (Program.AvalonSrv.ClientList)
                {
                    Program.AvalonSrv.ClientList.Remove(this);
                }

                // Remove WorldServer
                if (this.WServer == true && Program.SquareList.Contains(WSquare) == true)
                {
                    Program.SquareList.Remove(WSquare);
                }
            }
        }

Usage Example

        /// <summary>
        /// Invoked when a client sends data.
        /// </summary>
        /// <param name="async"></param>
        public void OnDataReceive(IAsyncResult async)
        {
            int numRecvBytes = 0;

            try
            {
                clientState = (SocketClient)async.AsyncState;
                numRecvBytes = m_SockConnection.EndReceive(async);

                if (numRecvBytes == 0)
                {
                    clientState.Disconnect();
                    return;
                }

                // copy new data and process packet
                lock (clientState)
                {
                    byte[] newData = new byte[numRecvBytes];
                    Buffer.BlockCopy(m_bRecvBuffer, 0, newData, 0, numRecvBytes);
                    m_bPacketStream = newData;

                    // process packets
                    ProcessPacket(m_bPacketStream, clientState);
                    ProcessQueue(clientState);
                }

                // finished receiving/processing data, go back to listening state.
                this.Socket.BeginReceive(m_bRecvBuffer, 0, 0x1000, SocketFlags.None, new AsyncCallback(OnDataReceive), clientState);

            }
            catch (NullReferenceException)
            {
                clientState.Disconnect();
            }
            catch (ObjectDisposedException)
            {
                clientState.Disconnect();
            }
            catch (SocketException se)
            {
                clientState.Disconnect();
                Logger.Log(Logger.LogLevel.Error, "Server", "{0}", se.Message);
            }
        }