Exemplo n.º 1
0
        /// <summary>
        /// UdpClient 收到数据
        /// </summary>
        /// <param name="ar">Ar.</param>
        private void OnReceive(IAsyncResult ar)
        {
            UdpClient client = (UdpClient)ar.AsyncState;

            lock (LOCK_OBJECT) {
                try {
                    // 不是当前连接
                    if (client != m_client)
                    {
                        client.Close();
                        return;
                    }

                    IPEndPoint remoteEP = null;
                    Byte[]     data     = client.EndReceive(ar, ref remoteEP);
                    m_kcp.Input(data);
                    for (int size = m_kcp.PeekSize(); size > 0; size = m_kcp.PeekSize())
                    {
                        var buffer = new byte[size];
                        if (m_kcp.Recv(buffer) > 0)
                        {
                            m_msgProtocol.Receive(buffer, buffer.Length);
                        }
                    }
                    m_updateDirty = true;

                    // 继续等待异步 OnReceive
                    m_client.BeginReceive(new AsyncCallback(OnReceive), m_client);
                } catch (Exception e) {
                    Close("Error - OnReceive: " + e.Message);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 有读取到数据
        /// </summary>
        /// <param name="ar">Ar.</param>
        private void OnRead(IAsyncResult ar)
        {
            TcpClient     client = (TcpClient)ar.AsyncState;
            NetworkStream stream = null;

            lock (LOCK_OBJECT)
            {
                try
                {
                    stream = client.GetStream();
                    int bytesRead = stream.EndRead(ar);

                    // 不是当前连接
                    if (client != m_client)
                    {
                        stream.Close();
                        client.Close();
                        return;
                    }

                    // 服务端主动断开了连接
                    if (bytesRead == 0)
                    {
                        Close("Server Closed");
                        return;
                    }

                    // 接收数据,清空 buffer,继续等待异步 OnRead
                    m_msgProtocol.Receive(m_readBuffer, bytesRead);
                    //Array.Clear (m_readBuffer, 0, BUFFER_SIZE);
                    m_stream.BeginRead(m_readBuffer, 0, BUFFER_SIZE, new AsyncCallback(OnRead), client);
                }
                catch (Exception e)
                {
                    if (client == m_client)
                    {
                        Close("Error - OnRead: " + e.Message);
                    }
                    else
                    {
                        if (stream != null)
                        {
                            stream.Close();
                        }
                        client.Close();
                    }
                }
            }
        }