Ejemplo n.º 1
0
        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="client">表示断开的客户端连接对象</param>
        /// <param name="reason">断开的原因</param>
        public void Disconnect(ClientPeer client, string reason)
        {
            try
            {
                if (client == null)
                {
                    throw new Exception("当前指定的客户端连接对象为空,无法断开连接");
                }

                Console.WriteLine(client.ClientSocket.RemoteEndPoint.ToString() + "客户端断开连接 原因:" + reason);

                //通知应用层 这个客户的断开连接了
                application.OnDisconnect(client);

                client.Disconnect();
                //回收对象方便下次使用
                clientPeerPool.Enqueue(client);
                acceptSemaphore.Release();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="client">当前指定的客户端</param>
        /// <param name="reason">断开原因</param>
        public void Disconnect(ClientPeer client, string reason)
        {
            try
            {
                if (client == null)
                {
                    throw new Exception("当前客户端为空 , 无法断开连接");
                }

                // 通知应用层,客户端断开连接了
                app.OnDisconnect(client);

                client.Disconnect();
                clientPool.EnqueueClient(client); // 回收对象,以便下次使用
                sema.Release();                   // 退出信号量 , 返回前一个计数
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }