Exemplo n.º 1
0
        private void InitProtocol(ISession session, string protocolCode, int version, bool addCapabilities = false)
        {
            if (session.State < SessionState.Initialized)
            {
                throw new InvalidOperationException($"{nameof(InitProtocol)} called on {session}");
            }

            if (session.State != SessionState.Initialized)
            {
                return;
            }

            string code = protocolCode.ToLowerInvariant();

            if (!_protocolFactories.TryGetValue(code, out Func <ISession, int, IProtocolHandler> protocolFactory))
            {
                throw new NotSupportedException($"Protocol {code} {version} is not supported");
            }

            IProtocolHandler protocolHandler = protocolFactory(session, version);

            protocolHandler.SubprotocolRequested += (s, e) => InitProtocol(session, e.ProtocolCode, e.Version);
            session.AddProtocolHandler(protocolHandler);
            if (addCapabilities)
            {
                foreach (Capability capability in _capabilities)
                {
                    session.AddSupportedCapability(capability);
                }
            }

            protocolHandler.Init();
        }
Exemplo n.º 2
0
        private void InitProtocol(ISession session, string protocolCode, int version)
        {
            if (session.State < SessionState.Initialized)
            {
                throw new InvalidOperationException($"{nameof(InitProtocol)} called on {session}");
            }

            if (session.State != SessionState.Initialized)
            {
                return;
            }

            protocolCode = protocolCode.ToLowerInvariant();
            IProtocolHandler protocolHandler;

            switch (protocolCode)
            {
            case Protocol.P2P:
                P2PProtocolHandler handler = new P2PProtocolHandler(session, _localPeer.LocalNodeId, _stats, _serializer, _perfService, _logManager);
                session.PingSender = handler;
                InitP2PProtocol(session, handler);
                protocolHandler = handler;
                break;

            case Protocol.Eth:
                if (version < 62 || version > 63)
                {
                    throw new NotSupportedException($"Eth protocol version {version} is not supported.");
                }

                Eth62ProtocolHandler ethHandler = version == 62
                        ? new Eth62ProtocolHandler(session, _serializer, _stats, _syncManager, _logManager, _perfService, _transactionPool)
                        : new Eth63ProtocolHandler(session, _serializer, _stats, _syncManager, _logManager, _perfService, _transactionPool);
                InitEthProtocol(session, ethHandler);
                protocolHandler = ethHandler;
                break;

            default:
                throw new NotSupportedException($"Protocol {protocolCode} {version} is not supported");
            }

            protocolHandler.SubprotocolRequested += (sender, args) => InitProtocol(session, args.ProtocolCode, args.Version);
            session.AddProtocolHandler(protocolHandler);
            protocolHandler.Init();
        }