示例#1
0
        private void HandleTcpClientAccepted(IAsyncResult ar)
        {
            if (IsRunning)
            {
                TcpListener tcpListener = (TcpListener)ar.AsyncState;

                TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
                tcpClient.ReceiveBufferSize = (int)ReceiveBufferSize;
                byte[] buffer = new byte[tcpClient.ReceiveBufferSize];

                TcpClientState internalClient
                    = new TcpClientState(tcpClient, buffer);
                lock (this.clients)
                {
                    this.clients.Add(internalClient);
                    RaiseClientConnected(tcpClient);
                }

                NetworkStream networkStream = internalClient.NetworkStream;
                networkStream.BeginRead(
                    internalClient.Buffer,
                    0,
                    internalClient.Buffer.Length,
                    HandleDatagramReceived,
                    internalClient);

                tcpListener.BeginAcceptTcpClient(
                    new AsyncCallback(HandleTcpClientAccepted), ar.AsyncState);
            }
        }
示例#2
0
        private void HandleDatagramReceived(IAsyncResult ar)
        {
            if (IsRunning)
            {
                TcpClientState internalClient = (TcpClientState)ar.AsyncState;
                NetworkStream  networkStream  = internalClient.NetworkStream;

                int numberOfReadBytes = 0;
                try
                {
                    numberOfReadBytes = networkStream.EndRead(ar);
                }
                catch
                {
                    numberOfReadBytes = 0;
                }

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

                // received byte and trigger event notification
                if (receivedBytes.Length != numberOfReadBytes)
                {
                    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
                if (IsRunning)
                {
                    networkStream.BeginRead(
                        internalClient.Buffer,
                        0,
                        internalClient.Buffer.Length,
                        HandleDatagramReceived,
                        internalClient);
                }
            }
        }