Пример #1
0
        public ShardPacketRouter(TcpSession session, IGrainFactory grainFactory, string shardName) : base(session)
        {
            GrainFactory         = grainFactory;
            ShardSession         = GrainFactory.GetGrain <IShardSession>(Session.Id);
            twoZeroBytes         = new byte[] { 0, 0 };
            packetCipher         = new ShardPacketCipher();
            packetReader         = new ShardPacketReader(packetCipher, typeof(ClientPacketAttribute).GetTypeInfo().Assembly);
            authenticationFailed = false;
            this.shardName       = shardName;

            // populate the packet handler map
            var handlerMapBuilder = ImmutableDictionary.CreateBuilder <Type, MethodInfo>();
            var typeInfo          = GetType().GetTypeInfo();

            foreach (var method in typeInfo.DeclaredMethods)
            {
                foreach (var handlerAttribute in method.GetCustomAttributes <HandlerAttribute>(false))
                {
                    if (method.ReturnType != typeof(Task))
                    {
                        throw new DramaException($"packet handler method {method.Name} must return {nameof(Task)}");
                    }

                    handlerMapBuilder.Add(handlerAttribute.PacketType, method);
                }
            }

            PacketHandlers = handlerMapBuilder.ToImmutable();
        }
Пример #2
0
        public ShardPacketReader(ShardPacketCipher packetCipher, Assembly packetDefinitionAssembly)
        {
            this.packetCipher = packetCipher;

            var annotatedTypes =
                from type in packetDefinitionAssembly.GetExportedTypes()
                where type.GetTypeInfo().GetCustomAttributes <ClientPacketAttribute>().Any()
                select type;

            packetMap = ImmutableDictionary.CreateRange(
                annotatedTypes.SelectMany(
                    annotatedType => annotatedType.GetTypeInfo().GetCustomAttributes <ClientPacketAttribute>().Select(
                        attribute => new KeyValuePair <ShardClientOpcode, Type>(attribute.Opcode, annotatedType)
                        )
                    )
                );
        }