public void Disconnect(DisconnectReason reason, string details) { if (Logger.IsDebug) { Logger.Debug($"Disconnecting {Node:c} bacause of the {details}"); } Session.InitiateDisconnect(reason, details); }
protected async Task CheckProtocolInitTimeout() { var receivedInitMsgTask = _initCompletionSource.Task; var firstTask = await Task.WhenAny(receivedInitMsgTask, Task.Delay(InitTimeout)); if (firstTask != receivedInitMsgTask) { if (Logger.IsTrace) { Logger.Trace($"Disconnecting due to timeout for protocol init message ({GetType().Name}): {Session.RemoteNodeId}"); } Session.InitiateDisconnect(DisconnectReason.ReceiveMessageTimeout, "protocol init timeout"); } }
protected async Task CheckProtocolInitTimeout() { Task <MessageBase> receivedInitMsgTask = _initCompletionSource.Task; CancellationTokenSource delayCancellation = new CancellationTokenSource(); Task firstTask = await Task.WhenAny(receivedInitMsgTask, Task.Delay(InitTimeout, delayCancellation.Token)); if (firstTask != receivedInitMsgTask) { if (Logger.IsTrace) { Logger.Trace($"Disconnecting due to timeout for protocol init message ({GetType().Name}): {Session.RemoteNodeId}"); } Session.InitiateDisconnect(DisconnectReason.ReceiveMessageTimeout, "protocol init timeout"); } else { delayCancellation.Cancel(); } }
private void HandleHello(HelloMessage hello) { ReportIn(hello); bool isInbound = !_sentHello; if (Logger.IsTrace) { Logger.Trace($"{Session} P2P received hello."); } if (!hello.NodeId.Equals(Session.RemoteNodeId)) { if (Logger.IsDebug) { Logger.Debug($"Inconsistent Node ID details - expected {Session.RemoteNodeId}, " + $"received hello with {hello.NodeId} " + $"on {(isInbound ? "IN connection" : "OUT connection")}"); } // it does not really matter if there is mismatch - we do not use it anywhere // throw new NodeDetailsMismatchException(); } RemoteClientId = hello.ClientId; Session.Node.ClientId = hello.ClientId; if (Logger.IsTrace) { Logger.Trace(!_sentHello ? $"{Session.RemoteNodeId} P2P initiating inbound {hello.Protocol}.{hello.P2PVersion} " + $"on {hello.ListenPort} ({hello.ClientId})" : $"{Session.RemoteNodeId} P2P initiating outbound {hello.Protocol}.{hello.P2PVersion} " + $"on {hello.ListenPort} ({hello.ClientId})"); } // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-8.md // Clients implementing a newer version simply send a packet with higher version and possibly additional list elements. // * If such a packet is received by a node with lower version, // it will blindly assume that the remote end is backwards-compatible and respond with the old handshake. // * If the packet is received by a node with equal version, // new features of the protocol can be used. // * If the packet is received by a node with higher version, // it can enable backwards-compatibility logic or drop the connection. ProtocolVersion = hello.P2PVersion; List <Capability> capabilities = hello.Capabilities; AvailableCapabilities = new List <Capability>(capabilities); foreach (Capability theirCapability in capabilities) { if (SupportedCapabilities.Contains(theirCapability)) { if (Logger.IsTrace) { Logger.Trace($"{Session.RemoteNodeId} Agreed on {theirCapability.ProtocolCode} v{theirCapability.Version}"); } AgreedCapabilities.Add(theirCapability); } else { if (Logger.IsTrace) { Logger.Trace($"{Session.RemoteNodeId} Capability not supported " + $"{theirCapability.ProtocolCode} v{theirCapability.Version}"); } } } if (!capabilities.Any(c => SupportedCapabilities.Contains(c))) { Session.InitiateDisconnect( DisconnectReason.UselessPeer, $"capabilities: {string.Join(", ", capabilities)}"); } ReceivedProtocolInitMsg(hello); P2PProtocolInitializedEventArgs eventArgs = new P2PProtocolInitializedEventArgs(this) { P2PVersion = ProtocolVersion, ClientId = RemoteClientId, Capabilities = capabilities, ListenPort = hello.ListenPort }; ProtocolInitialized?.Invoke(this, eventArgs); }