Exemple #1
0
        protected void OnMessageReceived(IncomingMessage message)
        {
            message.IfPayloadIs <VersionPayload>(version =>
            {
                if (State == NodeState.HandShaked)
                {
                    if (message.Node.Version >= ProtocolVersion.REJECT_VERSION)
                    {
                        message.Node.SendMessageAsync(new RejectPayload()
                        {
                            Code = RejectCode.DUPLICATE
                        });
                    }
                }
            });
            //if(version != null)
            //{
            //	if((version.Services & NodeServices.NODE_WITNESS) != 0)
            //		_SupportedTransactionOptions |= TransactionOptions.Witness;
            //}
            //var havewitness = message.Message.Payload as HaveWitnessPayload;
            //if(havewitness != null)
            //	_SupportedTransactionOptions |= TransactionOptions.Witness;

            var last = new ActionFilter((m, n) =>
            {
                MessageProducer.PushMessage(m);
                var messageReceived = MessageReceived;

                if (messageReceived != null)
                {
                    foreach (var handler in messageReceived.GetInvocationList().Cast <NodeEventMessageIncoming>())
                    {
                        try
                        {
                            handler.DynamicInvoke(this, m);
                        }
                        catch (TargetInvocationException ex)
                        {
                            TraceCorrelation.LogInside(() => NodeServerTrace.Error("Error while OnMessageReceived event raised", ex.InnerException), false);
                        }
                    }
                }
            });

            var enumerator = Filters.Concat(new[] { last }).GetEnumerator();

            FireFilters(enumerator, message);
        }
Exemple #2
0
        private void ProcessMessageCore(IncomingMessage message)
        {
            message.IfPayloadIs <VersionPayload>(version =>
            {
                var connectedToSelf = version.Nonce == Nonce;
                if (message.Node != null && connectedToSelf)
                {
                    NodeServerTrace.ConnectionToSelfDetected();
                    message.Node.DisconnectAsync();
                    return;
                }

                if (message.Node == null)
                {
                    var remoteEndpoint = version.AddressFrom;
                    if (!remoteEndpoint.Address.IsRoutable(AllowLocalPeers))
                    {
                        //Send his own endpoint
                        remoteEndpoint = new IPEndPoint(((IPEndPoint)message.Socket.RemoteEndPoint).Address, Network.DefaultPort);
                    }

                    var peer = new NetworkAddress()
                    {
                        Endpoint = remoteEndpoint,
                        Time     = DateTimeOffset.UtcNow
                    };
                    var node = new Node(peer, Network, CreateNodeConnectionParameters(), message.Socket, version);

                    if (connectedToSelf)
                    {
                        node.SendMessage(CreateNodeConnectionParameters().CreateVersion(node.Peer.Endpoint, Network));
                        NodeServerTrace.ConnectionToSelfDetected();
                        node.Disconnect();
                        return;
                    }

                    CancellationTokenSource cancel = new CancellationTokenSource();
                    cancel.CancelAfter(TimeSpan.FromSeconds(10.0));
                    try
                    {
                        ConnectedNodes.Add(node);
                        node.StateChanged += node_StateChanged;
                        node.RespondToHandShake(cancel.Token);
                    }
                    catch (OperationCanceledException ex)
                    {
                        NodeServerTrace.Error("The remote node did not respond fast enough (10 seconds) to the handshake completion, dropping connection", ex);
                        node.DisconnectAsync();
                        throw;
                    }
                    catch (Exception)
                    {
                        node.DisconnectAsync();
                        throw;
                    }
                }
            });

            var messageReceived = MessageReceived;

            if (messageReceived != null)
            {
                messageReceived(this, message);
            }
        }