예제 #1
0
 static GatewayHandlers()
 {
     foreach (var method in typeof(GatewayHandlers).GetMethods(BindingFlags.Static | BindingFlags.Public))
     {
         var attr = method.GetCustomAttributes(typeof(PacketHandlerAttribute), false).Cast <PacketHandlerAttribute>().FirstOrDefault();
         if (attr == null)
         {
             continue;
         }
         PacketHandlers.Add((WorldOpcodes)attr.PacketId, (StaticCommandHandler <GatewayConnection>)method.CreateDelegate(typeof(StaticCommandHandler <GatewayConnection>), null));
     }
 }
        public static bool AddPacketHandler(PacketType packetType, PacketMethod handler)
        {
            bool result = false;

            lock (PacketHandlers) {
                if (!PacketHandlers.ContainsKey(packetType))
                {
                    PacketHandlers.Add(packetType, handler);
                    result = true;
                }
            }
            return(result);
        }
예제 #3
0
 /// <summary>
 /// Registers a set of packet handlers to be used by new connections
 /// </summary>
 /// <param name="handlers">The handlers to register</param>
 public void RegisterPacketHandlers(IEnumerable <IPacketHandler> handlers)
 {
     foreach (IPacketHandler handler in handlers)
     {
         if (!PacketHandlers.ContainsKey(handler.Type))
         {
             PacketHandlers.Add(handler.Type, new List <IPacketHandler>(new[] { handler }));
         }
         else
         {
             PacketHandlers[handler.Type].Add(handler);
         }
     }
 }
        public void SetPacketHandler(ClientPacketId id, NetPacketHandler packetHandler)
        {
            if (packetHandler == null)
            {
                throw new ArgumentNullException(nameof(packetHandler));
            }

            if (PacketHandlers.ContainsKey(id))
            {
                throw new ArgumentException($"A packet handler is already registered for \"{id}\".", nameof(id));
            }

            PacketHandlers.Add(id, packetHandler);
        }
예제 #5
0
        public void RegisterPacketHandler(IPacket packet, PacketHandler handler)
        {
            //TODO: Allow multiple handlers for a single packet.
            if (packet == null)
            {
                throw new ArgumentNullException("packet");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            PacketHandlers.Add(packet.ID, handler);
        }
예제 #6
0
        public IClient Build()
        {
            var packetHandlers = new PacketHandlers();

            foreach (var packetHandlerModule in this.modules)
            {
                foreach (var packetHandler in packetHandlerModule.GetPacketHandlers())
                {
                    this.serviceCollection.AddSingleton(packetHandler.Value);
                }
            }

            this.serviceCollection.AddSingleton(this.options);
            this.serviceCollection.AddSingleton <IClient, Client>();
            this.serviceCollection.AddSingleton <IClientPacketProcessor, ClientPacketProcessor>();
            this.serviceCollection.AddSingleton <IPacketHandlers>(packetHandlers);
            this.serviceCollection.AddSingleton <ILogLevelProvider, LogLevelProvider>();

            if (this.logger == null)
            {
                this.serviceCollection.AddSingleton <ILogger, NoOpLogger>();
            }

            var serviceProvider = this.serviceCollection.BuildServiceProvider();

            PacketSerialiserProvider.PacketSerialiser = serviceProvider.GetService <IPacketSerialiser>();
            serviceProvider.GetService <ILogLevelProvider>().SetLogLevel(this.options.LogLevel);

            foreach (var packetHandlerModule in this.modules)
            {
                foreach (var packetHandler in packetHandlerModule.GetPacketHandlers())
                {
                    packetHandlers.Add(PacketSerialiserProvider.PacketSerialiser.CanReadName ? packetHandler.Key.Name : "Default",
                                       (IPacketHandler)serviceProvider.GetService(packetHandler.Value));
                }
            }

            if (!PacketSerialiserProvider.PacketSerialiser.CanReadName && packetHandlers.GetPacketHandlers().Count > 1)
            {
                throw new Exception("A PacketSerialiser which cannot identify a packet can only support up to one packet type");
            }

            return(serviceProvider.GetService <IClient>());
        }
예제 #7
0
 public void RegisterPacketHandler(IPacket packet, PacketHandler handler)
 {
     PacketHandlers.Add(packet.ID, handler);
 }