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; } } } }
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()); }
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); }
private void OnDisconnected() { if (Disconnected != null) { try { Disconnected(this); } catch (Exception ex) { TraceCorrelation.LogInside(() => NodeServerTrace.Error("Error while Disconnected event raised", ex)); } } }
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)); } } }
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); }
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(); }
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); } } }
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); } } }
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); } }
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)); } } } }
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)); } } } }
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(); }