public void HandleMessage(Packet msg) { if (msg.PacketType == P2PMessageCode.Hello) { HandleHello(Deserialize <HelloMessage>(msg.Data)); Metrics.HellosReceived++; foreach (Capability capability in AgreedCapabilities.GroupBy(c => c.ProtocolCode).Select(c => c.OrderBy(v => v.Version).Last())) { if (Logger.IsTrace) { Logger.Trace($"{P2PSession.RemoteNodeId} Starting protocolHandler for {capability.ProtocolCode} v{capability.Version} on {P2PSession.RemotePort}"); } SubprotocolRequested?.Invoke(this, new ProtocolEventArgs(capability.ProtocolCode, capability.Version)); } } else if (msg.PacketType == P2PMessageCode.Disconnect) { DisconnectMessage disconnectMessage = Deserialize <DisconnectMessage>(msg.Data); if (Logger.IsTrace) { Logger.Trace($"|NetworkTrace| {P2PSession.RemoteNodeId} Received disconnect ({(Enum.IsDefined(typeof(DisconnectReason), (byte)disconnectMessage.Reason) ? ((DisconnectReason)disconnectMessage.Reason).ToString() : disconnectMessage.Reason.ToString())}) on {P2PSession.RemotePort}"); } Close(disconnectMessage.Reason); } else if (msg.PacketType == P2PMessageCode.Ping) { if (Logger.IsTrace) { Logger.Trace($"{P2PSession.RemoteNodeId} Received PING on {P2PSession.RemotePort}"); } HandlePing(); } else if (msg.PacketType == P2PMessageCode.Pong) { if (Logger.IsTrace) { Logger.Trace($"{P2PSession.RemoteNodeId} Received PONG on {P2PSession.RemotePort}"); } HandlePong(msg); } else { Logger.Error($"{P2PSession.RemoteNodeId} Unhandled packet type: {msg.PacketType}"); } }
public override void HandleMessage(Packet msg) { switch (msg.PacketType) { case P2PMessageCode.Hello: { Metrics.HellosReceived++; HandleHello(Deserialize <HelloMessage>(msg.Data)); foreach (Capability capability in AgreedCapabilities.GroupBy(c => c.ProtocolCode).Select(c => c.OrderBy(v => v.Version).Last())) { if (Logger.IsTrace) { Logger.Trace($"{Session} Starting protocolHandler for {capability.ProtocolCode} v{capability.Version} on {Session.RemotePort}"); } SubprotocolRequested?.Invoke(this, new ProtocolEventArgs(capability.ProtocolCode, capability.Version)); } break; } case P2PMessageCode.Disconnect: { DisconnectMessage disconnectMessage = Deserialize <DisconnectMessage>(msg.Data); ReportIn(disconnectMessage); if (Logger.IsTrace) { string reason = Enum.IsDefined(typeof(DisconnectReason), (byte)disconnectMessage.Reason) ? ((DisconnectReason)disconnectMessage.Reason).ToString() : disconnectMessage.Reason.ToString(); Logger.Trace($"{Session} Received disconnect ({reason}) on {Session.RemotePort}"); } Close(disconnectMessage.Reason); break; } case P2PMessageCode.Ping: { if (Logger.IsTrace) { Logger.Trace($"{Session} Received PING on {Session.RemotePort}"); } HandlePing(); break; } case P2PMessageCode.Pong: { if (Logger.IsTrace) { Logger.Trace($"{Session} Received PONG on {Session.RemotePort}"); } HandlePong(msg); break; } case P2PMessageCode.AddCapability: { AddCapabilityMessage message = Deserialize <AddCapabilityMessage>(msg.Data); Capability capability = message.Capability; AgreedCapabilities.Add(message.Capability); SupportedCapabilities.Add(message.Capability); if (Logger.IsTrace) { Logger.Trace($"{Session.RemoteNodeId} Starting handler for {capability} on {Session.RemotePort}"); } SubprotocolRequested?.Invoke(this, new ProtocolEventArgs(capability.ProtocolCode, capability.Version)); break; } default: Logger.Error($"{Session.RemoteNodeId} Unhandled packet type: {msg.PacketType}"); break; } }