예제 #1
0
 public void Dispose()
 {
     if (!_Cancel.IsCancellationRequested)
     {
         _Cancel.Cancel();
         _Trace.LogInside(() => NodeServerTrace.Information("Stopping node server..."));
         lock (_Resources)
         {
             foreach (var resource in _Resources)
             {
                 resource.Dispose();
             }
         }
         try
         {
             _ConnectedNodes.DisconnectAll();
         }
         finally
         {
             if (socket != null)
             {
                 Utils.SafeCloseSocket(socket);
                 socket = null;
             }
         }
     }
 }
예제 #2
0
        public void SendMessage(Payload payload)
        {
            var message = new Message();

            message.Magic = NodeServer.Network.Magic;
            message.UpdatePayload(payload, Version);
            TraceCorrelation.LogInside(() => NodeServerTrace.Information("Sending message " + message));
            _Connection.Socket.Send(message.ToBytes());
        }
예제 #3
0
파일: Node.cs 프로젝트: jannickj/NBitcoin
        public void SendMessage(Payload payload)
        {
            var message = new Message();

            message.Magic = Network.Magic;
            message.UpdatePayload(payload, Version);
            TraceCorrelation.LogInside(() => NodeServerTrace.Verbose("Sending message " + message));

            var bytes = message.ToBytes();

            Counter.AddWritten(bytes.LongLength);
            var result = _Connection.Socket.Send(bytes);
        }
예제 #4
0
파일: Node.cs 프로젝트: jannickj/NBitcoin
 private void OnDisconnected()
 {
     if (Disconnected != null)
     {
         try
         {
             Disconnected(this);
         }
         catch (Exception ex)
         {
             TraceCorrelation.LogInside(() => NodeServerTrace.Error("Error while Disconnected event raised", ex));
         }
     }
 }
예제 #5
0
파일: Node.cs 프로젝트: jannickj/NBitcoin
 private void OnStateChanged(NodeState previous)
 {
     if (StateChanged != null)
     {
         try
         {
             StateChanged(this, previous);
         }
         catch (Exception ex)
         {
             TraceCorrelation.LogInside(() => NodeServerTrace.Error("Error while Disconnected event raised", ex));
         }
     }
 }
예제 #6
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);
        }
예제 #7
0
 internal Node(Peer peer, NodeServer nodeServer, Socket socket, VersionPayload version)
 {
     _Peer        = peer;
     _NodeServer  = nodeServer;
     _Connection  = new NodeConnection(this, socket);
     _FullVersion = version;
     Version      = version.Version;
     LastSeen     = peer.NetworkAddress.Time;
     TraceCorrelation.LogInside(() =>
     {
         NodeServerTrace.Information("Connected to advertised node " + _Peer.NetworkAddress.Endpoint);
         State = NodeState.Connected;
     });
     _Connection.BeginListen();
 }
예제 #8
0
 private void FireFilters(IEnumerator <INodeFilter> enumerator, IncomingMessage message)
 {
     if (enumerator.MoveNext())
     {
         var filter = enumerator.Current;
         try
         {
             filter.OnReceivingMessage(message, () => FireFilters(enumerator, message));
         }
         catch (Exception ex)
         {
             TraceCorrelation.LogInside(() => NodeServerTrace.Error("Unhandled exception raised by a node filter (OnReceivingMessage)", ex.InnerException), false);
         }
     }
 }
예제 #9
0
 private void FireFilters(IEnumerator <INodeFilter> enumerator, Object payload)
 {
     if (enumerator.MoveNext())
     {
         var filter = enumerator.Current;
         try
         {
             filter.OnSendingMessage(this, payload, () => FireFilters(enumerator, payload));
         }
         catch (Exception ex)
         {
             TraceCorrelation.LogInside(() => NodeServerTrace.Error("Unhandled exception raised by a node filter (OnSendingMessage)", ex.InnerException), false);
         }
     }
 }
예제 #10
0
 public bool AdvertiseMyself()
 {
     if (NodeServer.IsListening && NodeServer.ExternalEndpoint.Address.IsRoutable(NodeServer.AllowLocalPeers))
     {
         TraceCorrelation.LogInside(() => NodeServerTrace.Information("Advertizing myself"));
         SendMessage(new AddrPayload(new NetworkAddress()
         {
             Ago      = TimeSpan.FromSeconds(0),
             Endpoint = NodeServer.ExternalEndpoint
         }));
         return(true);
     }
     else
     {
         return(false);
     }
 }
예제 #11
0
        private void OnDisconnected()
        {
            var disconnected = Disconnected;

            if (disconnected != null)
            {
                foreach (var handler in disconnected.GetInvocationList().Cast <NodeEventHandler>())
                {
                    try
                    {
                        handler.DynamicInvoke(this);
                    }
                    catch (TargetInvocationException ex)
                    {
                        TraceCorrelation.LogInside(() => NodeServerTrace.Error("Error while Disconnected event raised", ex.InnerException));
                    }
                }
            }
        }
예제 #12
0
        private void OnStateChanged(NodeState previous)
        {
            var stateChanged = StateChanged;

            if (stateChanged != null)
            {
                foreach (var handler in stateChanged.GetInvocationList().Cast <NodeStateEventHandler>())
                {
                    try
                    {
                        handler.DynamicInvoke(this, previous);
                    }
                    catch (TargetInvocationException ex)
                    {
                        TraceCorrelation.LogInside(() => NodeServerTrace.Error("Error while StateChanged event raised", ex.InnerException));
                    }
                }
            }
        }
예제 #13
0
 internal Node(NetworkAddress peer, NetworkInfo network, NodeConnectionParameters parameters, Socket socket, VersionPayload peerVersion)
 {
     _RemoteSocketAddress = ((IPEndPoint)socket.RemoteEndPoint).Address;
     _RemoteSocketPort    = ((IPEndPoint)socket.RemoteEndPoint).Port;
     Inbound      = true;
     _Behaviors   = new NodeBehaviorsCollection(this);
     _MyVersion   = parameters.CreateVersion(peer.Endpoint, network);
     Network      = network;
     _Peer        = peer;
     _Connection  = new NodeConnection(this, socket);
     _PeerVersion = peerVersion;
     LastSeen     = peer.Time;
     ConnectedAt  = DateTimeOffset.UtcNow;
     TraceCorrelation.LogInside(() =>
     {
         NodeServerTrace.Information("Connected to advertised node " + _Peer.Endpoint);
         State = NodeState.Connected;
     });
     InitDefaultBehaviors(parameters);
     _Connection.BeginListen();
 }