コード例 #1
0
        /// <summary>
        /// 处理连接请求
        /// </summary>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            //计数    限制进程访问数
            acceptSemaphore.WaitOne();

            //得到一个socket对象
            // Socket clientSocket = e.AcceptSocket;
            ClientPeer clientSocket = clientPeerPool.Dequeue();

            clientSocket.ClientSocket = e.AcceptSocket;

            Console.WriteLine("客户端连接成功 :" + clientSocket.ClientSocket.RemoteEndPoint.ToString());

            //开始接受数据
            StartReceive(clientSocket);

            //再进行其他操作
            e.AcceptSocket = null;
            StartAccept(e);
        }
コード例 #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("请确保当前有客户端连接...");
                }


                //通知应用层,这个客户端断开了
                application.OnDisconnect(client);
                Console.WriteLine(client.ClientSocket.RemoteEndPoint.ToString() + "  客户端断开连接,原因:" + reason);
                client.Disconnect();
                clientPeerPool.Enqueue(client);
                acceptSemaphore.Release();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
コード例 #3
0
        /// <summary>
        /// 处理接收的请求
        /// </summary>
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            ClientPeer client = e.UserToken as ClientPeer;

            //判断网络消息是否接受成功
            int receiveDataLength = client.ReceiveArgs.BytesTransferred;

            if (client.ReceiveArgs.SocketError == SocketError.Success && receiveDataLength > 0)
            {
                //拷贝数据到数组中
                byte[] byteArray = new byte[receiveDataLength];
                Buffer.BlockCopy(client.ReceiveArgs.Buffer, 0, byteArray, 0, receiveDataLength);
                //让客户端自行解析数据包  (粘包)
                client.StartReceive(byteArray);

                StartReceive(client);
            }
            else
            {
                //断开连接了
                //如果没有传输的字节数,表示断开连接了
                if (client.ReceiveArgs.BytesTransferred == 0)
                {
                    if (client.ReceiveArgs.SocketError == SocketError.Success)
                    {
                        //客户端主动断开连接
                        Disconnect(client, "客户端主动断开连接...");
                    }
                    else
                    {
                        //由于网络异常等原因,被动断开连接
                        Disconnect(client, client.ReceiveArgs.SocketError.ToString());
                    }
                }
            }
        }
コード例 #4
0
 /// <summary>
 /// 一条数据解析完成的处理
 /// </summary>
 /// <param name="client">对应的连接对象</param>
 /// <param name="value">解析出来的一个具体能使用的类型</param>
 private void ReceiveCompleted(ClientPeer client, MessageData message)
 {
     //给应用层
     application.OnReceive(client, message);
 }
コード例 #5
0
 public void Enqueue(ClientPeer client)
 {
     clientPeerQueue.Enqueue(client);
 }