示例#1
0
 private void RaisePlaintextReceived(TcpClientState sender, byte[] datagram)
 {
     if (PlaintextReceived != null)
     {
         PlaintextReceived(this, new TcpDatagramReceivedEventArgs <string>(sender, this.Encoding.GetString(datagram, 0, datagram.Length)));
     }
 }
示例#2
0
        /// <summary>
        /// 接收到数据报文明文事件
        /// </summary>
        //		public event EventHandler<TcpDatagramReceivedEventArgs<string>> PlaintextReceived;

        //		private void RaiseDatagramReceived(TcpClient sender, byte[] datagram)
        private void RaiseDatagramReceived(TcpClientState sender, byte[] datagram)
        {
            if (DatagramReceived != null)
            {
                DatagramReceived(this, new TcpDatagramReceivedEventArgs <byte[]> (sender, datagram));
            }
        }
示例#3
0
        private void HandleTcpClientAccepted(IAsyncResult ar)
        {
            if (IsRunning)
            {
                TcpListener tcpListener = (TcpListener)ar.AsyncState;

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

                SimpleProtocolFilter prot           = new SimpleProtocolFilter();
                TcpClientState       internalClient = new TcpClientState(tcpClient, buffer, prot);
                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);
            }
        }
示例#4
0
 private void ContinueReadBuffer(TcpClientState internalClient, NetworkStream networkStream)
 {
     try {
         networkStream.BeginRead(internalClient.Buffer, 0, internalClient.Buffer.Length, HandleDatagramReceived, internalClient);
     } catch (ObjectDisposedException ex) {
         Debug.LogException(ex);
     }
 }
示例#5
0
        private void HandleDatagramReceived(IAsyncResult ar)
        {
            if (!IsRunning)
            {
                return;
            }

            try
            {
                TcpClientState internalClient = (TcpClientState)ar.AsyncState;
                if (!internalClient.TcpClient.Connected)
                {
                    return;
                }

                NetworkStream networkStream = internalClient.NetworkStream;

                int numberOfReadBytes = 0;
                try
                {
                    // if the remote host has shutdown its connection,
                    // read will immediately return with zero bytes.
                    numberOfReadBytes = networkStream.EndRead(ar);
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                    numberOfReadBytes = 0;
                }

                if (numberOfReadBytes == 0)
                {
                    // connection has been closed
                    TcpClientState internalClientToBeThrowAway;
                    string         tcpClientKey = internalClient.TcpClient.Client.RemoteEndPoint.ToString();
                    _clients.TryRemove(tcpClientKey, out internalClientToBeThrowAway);
                    RaiseClientDisconnected(internalClient.TcpClient);
                    return;
                }

                // received byte and trigger event notification
                var receivedBytes = new byte[numberOfReadBytes];
                Array.Copy(internalClient.Buffer, 0, receivedBytes, 0, numberOfReadBytes);
                // input bytes into protofilter
                internalClient.Prot.input(receivedBytes);
                RaiseDatagramReceived(internalClient, receivedBytes);
                // RaisePlaintextReceived(internalClient.TcpClient, receivedBytes);

                // continue listening for tcp datagram packets
                ContinueReadBuffer(internalClient, networkStream);
            }
            catch (InvalidOperationException ex)
            {
                Debug.LogException(ex);
            }
        }
示例#6
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 (numberOfReadBytes == 0)
                {
                    // connection has been closed
                    lock (this.clients) {
                        this.clients.Remove(internalClient);
                        RaiseClientDisconnected(internalClient.TcpClient);
                        return;
                    }
                }

                // received byte
                byte[] receivedBytes = new byte[numberOfReadBytes];
                Buffer.BlockCopy(
                    internalClient.Buffer, 0,
                    receivedBytes, 0, numberOfReadBytes);
                // input bytes into protofilter
                internalClient.Prot.input(receivedBytes);
                // trigger event notification
//				RaiseDatagramReceived(internalClient.TcpClient, receivedBytes);
//				RaisePlaintextReceived(internalClient.TcpClient, receivedBytes);
                RaiseDatagramReceived(internalClient, receivedBytes);

                // continue listening for tcp datagram packets
                networkStream.BeginRead(
                    internalClient.Buffer,
                    0,
                    internalClient.Buffer.Length,
                    HandleDatagramReceived,
                    internalClient);
            }
        }
示例#7
0
        private void HandleTcpClientAccepted(IAsyncResult ar)
        {
            if (!IsRunning)
            {
                return;
            }

            TcpListener tcpListener = (TcpListener)ar.AsyncState;

            TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);

            if (!tcpClient.Connected)
            {
                return;
            }

            byte[] buffer                       = new byte[tcpClient.ReceiveBufferSize];
            SimpleProtocolFilter prot           = new SimpleProtocolFilter();
            TcpClientState       internalClient = new TcpClientState(tcpClient, buffer, prot);

            // add client connection to cache
            string tcpClientKey = internalClient.TcpClient.Client.RemoteEndPoint.ToString();

            _clients.AddOrUpdate(tcpClientKey, internalClient, (n, o) =>
            {
                return(internalClient);
            });
            RaiseClientConnected(tcpClient);

            // begin to read data
            NetworkStream networkStream = internalClient.NetworkStream;

            ContinueReadBuffer(internalClient, networkStream);

            // keep listening to accept next connection
            ContinueAcceptTcpClient(tcpListener);
        }
示例#8
0
 /// <summary>
 /// 接收到数据报文事件参数
 /// </summary>
 /// <param name="tcpClient">客户端</param>
 /// <param name="datagram">报文</param>
 //		public TcpDatagramReceivedEventArgs(TcpClient tcpClient, T datagram)
 public TcpDatagramReceivedEventArgs(TcpClientState client, T datagram)
 {
     Client    = client;
     TcpClient = client.TcpClient;
     Datagram  = datagram;
 }
示例#9
0
 /// <summary>
 /// 接收到数据报文事件参数
 /// </summary>
 /// <param name="tcpClientState">客户端状态</param>
 /// <param name="datagram">报文</param>
 public TcpDatagramReceivedEventArgs(TcpClientState tcpClientState, T datagram)
 {
     this.Client    = tcpClientState;
     this.TcpClient = tcpClientState.TcpClient;
     this.Datagram  = datagram;
 }