Exemplo n.º 1
0
        /// <summary>
        /// The worker method of the listener thread.
        /// </summary>
        /// <param name="data">The arguments of the thread.</param>
        private void ListenerWorker(object data)
        {
            ListenerThreadArgs args = (ListenerThreadArgs)data;

            byte[] receiveBuffer = new byte[8192];
            int    receivedBytes = 0;

            while (!args.Stop)
            {
                try
                {
                    receivedBytes = this.Stream.Read(receiveBuffer, 0, receiveBuffer.Length);
                }
                catch
                {
                    this.FireConnectionLost(this, EventArgs.Empty);
                    return;
                }

                if (receivedBytes > 0)
                {
                    this.FireDataReceived(this, new DataReceivedEventArgs(receiveBuffer.Take(receivedBytes).ToArray()));
                }

                Thread.Sleep(args.PollDelay);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EnhancedTcpClient"/> class.
        /// </summary>
        /// <param name="ipEndPoint">The IPEndPoint of the client.</param>
        public EnhancedTcpClient(IPEndPoint ipEndPoint)
        {
            this.Client = new TcpClient();
            try
            {
                this.Client.Connect(ipEndPoint);
            }
            catch
            {
                this.FireConnectionFailed(this, EventArgs.Empty);
            }

            this.listenerThread     = new Thread(this.ListenerWorker);
            this.listenerThreadArgs = new ListenerThreadArgs(PollDelay);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EnhancedTcpClient"/> class.
 /// </summary>
 /// <param name="client">An instance of the <see cref="TcpClient"/> class.</param>
 public EnhancedTcpClient(TcpClient client)
 {
     this.Client             = client;
     this.listenerThread     = new Thread(this.ListenerWorker);
     this.listenerThreadArgs = new ListenerThreadArgs(PollDelay);
 }