コード例 #1
0
        /// <summary>
        /// Event handler that is called when the attached node receives a network message.
        /// </summary>
        /// <param name="node">Node that received the message.</param>
        /// <param name="message">Received message.</param>
        /// <remarks>
        /// This handler only cares about "verack" messages, which are only sent once per node
        /// and at the time they are sent the time offset information is parsed by underlaying logic.
        /// <para>
        /// Note that it is not possible to use "version" message here as <see cref="Node"/>
        /// does not deliver this message for inbound peers to node behaviors.
        /// </para>
        /// </remarks>
        private void AttachedNode_MessageReceived(Node node, IncomingMessage message)
        {
            this.logger.LogTrace("({0}:'{1}',{2}:'{3}')", nameof(node), node.RemoteSocketEndpoint, nameof(message), message.Message.Command);

            VerAckPayload verack = message.Message.Payload as VerAckPayload;

            if (verack != null)
            {
                IPAddress address = node.RemoteSocketAddress;
                if (address != null)
                {
                    VersionPayload version = node.PeerVersion;
                    if (version != null)
                    {
                        TimeSpan timeOffset = version.Timestamp - this.dateTimeProvider.GetTimeOffset();
                        if (timeOffset != null)
                        {
                            this.state.AddTimeData(address, timeOffset, node.Inbound);
                        }
                    }
                    else
                    {
                        this.logger.LogTrace("Node '{0}' does not have an initialized time offset.", node.RemoteSocketEndpoint);
                    }
                }
                else
                {
                    this.logger.LogTrace("Message received from unknown node's address.");
                }
            }

            this.logger.LogTrace("(-)");
        }
コード例 #2
0
        public void TestVerAckMessagePayload()
        {
            MessagePayload payload;

            byte[] expected;

            var commandPayload = new VerAckPayload();

            UInt32 magic = 0xE7E5E7E4;

            payload  = new MessagePayload(magic, "verack", commandPayload);
            expected = new byte[] {
                0xE4, 0xE7, 0xE5, 0xE7,                         // Primecoin Magic Bytes
                0x76, 0x65, 0x72, 0x61, 0x63, 0x6B, 0x00, 0x00, // verack Command zero padded...
                0x00, 0x00, 0x00, 0x00,                         // ...
                0x00, 0x00, 0x00, 0x00,                         // Length (0)
                0x5D, 0xF6, 0xE0, 0xE2,                         // CheckSum
            };
            AssertBytesEqual(expected, payload.ToBytes());
            AssertMessagePayloadsEqual(payload, new MessagePayload(expected));
        }