Exemplo n.º 1
0
        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($"{Session.RemoteNodeId} Starting protocolHandler for {capability.ProtocolCode} v{capability.Version} on {Session.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| {Session.RemoteNodeId} Received disconnect ({(Enum.IsDefined(typeof(DisconnectReason), (byte)disconnectMessage.Reason) ? ((DisconnectReason)disconnectMessage.Reason).ToString() : disconnectMessage.Reason.ToString())}) on {Session.RemotePort}");
                }
                Close(disconnectMessage.Reason);
            }
            else if (msg.PacketType == P2PMessageCode.Ping)
            {
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{Session.RemoteNodeId} Received PING on {Session.RemotePort}");
                }
                HandlePing();
            }
            else if (msg.PacketType == P2PMessageCode.Pong)
            {
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{Session.RemoteNodeId} Received PONG on {Session.RemotePort}");
                }
                HandlePong(msg);
            }
            else if (msg.PacketType == P2PMessageCode.AddCapability)
            {
                var message    = Deserialize <AddCapabilityMessage>(msg.Data);
                var capability = message.Capability;
                AgreedCapabilities.Add(message.Capability);
                SupportedCapabilities.Add(message.Capability);
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{Session.RemoteNodeId} Starting protocolHandler for {capability.ProtocolCode} v{capability.Version} on {Session.RemotePort}");
                }
                SubprotocolRequested?.Invoke(this, new ProtocolEventArgs(capability.ProtocolCode, capability.Version));
            }
            else
            {
                Logger.Error($"{Session.RemoteNodeId} Unhandled packet type: {msg.PacketType}");
            }
        }
Exemplo n.º 2
0
        public override void AddSupportedCapability(Capability capability)
        {
            if (SupportedCapabilities.Contains(capability))
            {
                return;
            }

            SupportedCapabilities.Add(capability);
        }
Exemplo n.º 3
0
        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;
            }
        }