Пример #1
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)
                {
                    OnClientDisconnected(internalClient);
                    return;
                }

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

                // continue listening for tcp datagram packets
                networkStream.BeginRead(internalClient.Buffer, 0, internalClient.Buffer.Length, HandleDatagramReceived, internalClient);
            }
        }
Пример #2
0
        private void HandleTcpClientAccepted(IAsyncResult ar)
        {
            if (IsRunning)
            {
                TcpListener tcpListener = (TcpListener)ar.AsyncState;

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

                TcpClientState internalClient = new TcpClientState(tcpClient, buffer, GetNewSid());
                OnClientConnected(internalClient);

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

                tcpListener.BeginAcceptTcpClient(new AsyncCallback(HandleTcpClientAccepted), ar.AsyncState);
            }
        }
Пример #3
0
        /// <summary>
        /// 发送报文至指定的客户端
        /// </summary>
        /// <param name="tcpClient">客户端</param>
        /// <param name="datagram">报文</param>
        public void Send(TcpClientState tcpClient, byte[] datagram)
        {
            if (!IsRunning)
            {
                throw new InvalidProgramException("This TCP server has not been started.");
            }

            if (tcpClient == null || tcpClient.TcpClient == null)
            {
                throw new ArgumentNullException("tcpClient");
            }

            if (datagram == null)
            {
                throw new ArgumentNullException("datagram");
            }

            tcpClient.TcpClient.GetStream().BeginWrite(
                datagram, 0, datagram.Length, HandleDatagramWritten, tcpClient);
        }
Пример #4
0
 public void Disconnect(TcpClientState tcpClient)
 {
     tcpClient.TcpClient.Client.Disconnect(false);
 }
Пример #5
0
 /// <summary>
 /// 发送报文至指定的客户端
 /// </summary>
 /// <param name="tcpClient">客户端</param>
 /// <param name="datagram">报文</param>
 public void Send(TcpClientState tcpClient, string datagram)
 {
     Send(tcpClient, this.Encoding.GetBytes(datagram));
 }