Ejemplo n.º 1
0
        /// <summary>
        /// Blocks until data is received from the peer represented by this instance.
        /// Messages are parsed as soon as they are available on the wire, and broadcast synchronously
        /// via the MessageReceived event.
        /// </summary>
        private void ReadIncomingMessages()
        {
            using (var reader = _client.GetStreamReader())
            {
                while (true)
                {
                    // If there is no data to read, this will block until the next message becomes available.
                    var header = new MessageHeader();
                    header.Decode(reader);

                    var message = header.Command.CreateMessage();
                    message.ProtocolVersion = 1;
                    message.Decode(reader);

                    OnMessageReceived(new PeerMessageReceivedArgs(header, message));
                }
            }
        }