コード例 #1
0
ファイル: NetworkPeer.cs プロジェクト: glasgowdev/purple
        /// <summary>
        /// Calls event handlers when a new message is received from the peer.
        /// </summary>
        /// <param name="message">Message that was received.</param>
        private void CallMessageReceivedHandlers(IncomingMessage message)
        {
            this.logger.LogTrace("({0}:'{1}')", nameof(message), message.Message.Command);

            NetworkPeerMessageReceivedEventHandler messageReceivedPriority = this.MessageReceivedPriority;

            if (messageReceivedPriority != null)
            {
                foreach (NetworkPeerMessageReceivedEventHandler handler in messageReceivedPriority.GetInvocationList().Cast <NetworkPeerMessageReceivedEventHandler>())
                {
                    try
                    {
                        handler.DynamicInvoke(this, message);
                    }
                    catch (TargetInvocationException ex)
                    {
                        this.logger.LogError("Exception occurred: {0}", ex.InnerException.ToString());
                    }
                }
            }

            NetworkPeerMessageReceivedEventHandler messageReceived = this.MessageReceived;

            if (messageReceived != null)
            {
                foreach (NetworkPeerMessageReceivedEventHandler handler in messageReceived.GetInvocationList().Cast <NetworkPeerMessageReceivedEventHandler>())
                {
                    try
                    {
                        handler.DynamicInvoke(this, message);
                    }
                    catch (TargetInvocationException ex)
                    {
                        this.logger.LogError("Exception occurred: {0}", ex.InnerException.ToString());
                    }
                }
            }

            this.logger.LogTrace("(-)");
        }
コード例 #2
0
        /// <summary>
        /// Calls event handlers when a new message is received from the peer.
        /// </summary>
        /// <param name="message">Message that was received.</param>
        public void OnMessageReceived(IncomingMessage message)
        {
            this.logger.LogTrace("({0}:'{1}')", nameof(message), message.Message.Command);

            var version = message.Message.Payload as VersionPayload;

            if ((version != null) && (this.State == NetworkPeerState.HandShaked))
            {
                if (message.NetworkPeer.Version >= ProtocolVersion.REJECT_VERSION)
                {
                    message.NetworkPeer.SendMessageVoidAsync(new RejectPayload()
                    {
                        Code = RejectCode.DUPLICATE
                    });
                }
            }

            if (version != null)
            {
                this.TimeOffset = this.dateTimeProvider.GetTimeOffset() - version.Timestamp;
                if ((version.Services & NetworkPeerServices.NODE_WITNESS) != 0)
                {
                    this.SupportedTransactionOptions |= NetworkOptions.Witness;
                }
            }

            if (message.Message.Payload is HaveWitnessPayload)
            {
                this.SupportedTransactionOptions |= NetworkOptions.Witness;
            }

            this.MessageProducer.PushMessage(message);
            NetworkPeerMessageReceivedEventHandler messageReceivedPriority = MessageReceivedPriority;

            if (messageReceivedPriority != null)
            {
                foreach (NetworkPeerMessageReceivedEventHandler handler in messageReceivedPriority.GetInvocationList().Cast <NetworkPeerMessageReceivedEventHandler>())
                {
                    try
                    {
                        handler.DynamicInvoke(this, message);
                    }
                    catch (TargetInvocationException ex)
                    {
                        this.logger.LogError("Exception occurred: {0}", ex.InnerException.ToString());
                    }
                }
            }

            NetworkPeerMessageReceivedEventHandler messageReceived = MessageReceived;

            if (messageReceived != null)
            {
                foreach (NetworkPeerMessageReceivedEventHandler handler in messageReceived.GetInvocationList().Cast <NetworkPeerMessageReceivedEventHandler>())
                {
                    try
                    {
                        handler.DynamicInvoke(this, message);
                    }
                    catch (TargetInvocationException ex)
                    {
                        this.logger.LogError("Exception occurred: {0}", ex.InnerException.ToString());
                    }
                }
            }


            this.logger.LogTrace("(-)");
        }