示例#1
0
        private void HandleTcpClientAccepted(IAsyncResult ar)
        {
            if (IsRunning)
            {
                try
                {
                    TcpListener tcpListener = (TcpListener)ar.AsyncState;
                    TcpClient   tcpClient   = tcpListener.EndAcceptTcpClient(ar);
                    byte[]      buffer      = new byte[tcpClient.ReceiveBufferSize];

                    TcpClientState internalClient = new TcpClientState(tcpClient, buffer);
                    lock (this.clients)
                    {
                        this.clients.Add(internalClient);
                        RaiseClientConnected(tcpClient);
                    }
                    NetworkStream networkStream = internalClient.CurNetworkStream;
                    if (networkStream != null && networkStream.CanRead)
                    {
                        networkStream.BeginRead(internalClient.Buffer, 0, internalClient.Buffer.Length, HandleDatagramReceived, internalClient);
                        tcpListener.BeginAcceptTcpClient(new AsyncCallback(HandleTcpClientAccepted), ar.AsyncState);
                    }
                }
                catch (Exception ex1)
                {
                    LogHelper.WriteLog(DateTime.Now.ToString("HH-mm-ss:") + ex1.Message);
                }
            }
            else
            {
                return;
            }
        }
示例#2
0
        /// <summary>
        /// 接收回调
        /// </summary>
        /// <param name="ar"></param>
        private void HandleDatagramReceived(IAsyncResult ar)
        {
            if (IsRunning)
            {
                TcpClientState internalClient    = (TcpClientState)ar.AsyncState;
                NetworkStream  networkStream     = internalClient.CurNetworkStream;
                int            numberOfReadBytes = 0;
                try
                {
                    numberOfReadBytes = networkStream.EndRead(ar);
                }
                catch
                {
                    numberOfReadBytes = 0;
                }

                if (numberOfReadBytes == 0)
                {
                    // connection has been closed
                    lock (this.clients)
                    {
                        this.clients.Remove(internalClient);
                        RaiseClientDisconnected(internalClient.TcpClient);
                        return;
                    }
                }

                try
                {
                    // received byte and trigger event notification
                    byte[] receivedBytes = new byte[numberOfReadBytes];
                    Buffer.BlockCopy(internalClient.Buffer, 0, receivedBytes, 0, numberOfReadBytes);
                    RaiseDatagramReceived(internalClient.TcpClient, receivedBytes);
                    RaisePlaintextReceived(internalClient.TcpClient, receivedBytes);

                    // continue listening for tcp datagram packets
                    networkStream.BeginRead(internalClient.Buffer, 0, internalClient.Buffer.Length, HandleDatagramReceived, internalClient);
                }
                catch (Exception e)
                {
                    LogHelper.WriteLog(DateTime.Now.ToString("HH-mm-ss:") + e.Message);
                }
            }
        }