// PacketFunction
        public void LoadPacketHandler()
        {
            Log.Info("TCPManager", "Loading the Packet Handler");

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type type in assembly.GetTypes())
                {
                    // Pick up a class
                    if (type.IsClass != true)
                    {
                        continue;
                    }

                    foreach (MethodInfo m in type.GetMethods())
                    {
                        foreach (object at in m.GetCustomAttributes(typeof(PacketHandlerAttribute), false))
                        {
                            PacketHandlerAttribute attr    = (PacketHandlerAttribute)at;
                            PacketFunction         handler = (PacketFunction)Delegate.CreateDelegate(typeof(PacketFunction), m);

                            Log.Debug("TCPManager", $"Registering handler for opcode : " +
                                      $"{attr.Opcode.ToString("X8")} " +
                                      $"{handler.Method.Module}" +
                                      $"{handler.Method.DeclaringType.AssemblyQualifiedName}");
                            m_packetHandlers[attr.Opcode] = handler;
                            stateRequirement[attr.Opcode] = (byte)attr.State;
                        }
                    }
                }
            }
        }
Example #2
0
        public void LoadPacketHandler()
        {
            Log.Debug("GameServer: Loading the Packet Handler");

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type type in assembly.GetTypes())
                {
                    // Pick up a class
                    if (type.IsClass != true)
                    {
                        continue;
                    }

                    if (type.IsSubclassOf(typeof(IPacketHandler)))
                    {
                        continue;
                    }

                    foreach (MethodInfo m in type.GetMethods())
                    {
                        foreach (object at in m.GetCustomAttributes(typeof(PacketHandlerAttribute), false))
                        {
                            PacketHandlerAttribute attr    = at as PacketHandlerAttribute;
                            PacketFunction         handler = (PacketFunction)Delegate.CreateDelegate(typeof(PacketFunction), m);

                            Log.Debug("GameServer: Registering handler for opcode : " + attr.Opcode.ToString("X8"));
                            m_packetHandlers[attr.Opcode] = handler;
                        }
                    }
                }
            }
        }
Example #3
0
        public void HandlePacket(Connection conn, PacketIn Packet)
        {
            PacketFunction packetHandler = null;

            if (Packet.Opcode < (ulong)m_packetHandlers.Length)
            {
                packetHandler = m_packetHandlers[Packet.Opcode];
            }
            else
            {
                Log.Error("HandlePacket: ", "Can not handle :" + Packet.Opcode + "(" + Packet.Opcode.ToString("X8") + ")");
            }

            if (packetHandler != null)
            {
                PacketHandlerAttribute[] packethandlerattribs = (PacketHandlerAttribute[])packetHandler.GetType().GetCustomAttributes(typeof(PacketHandlerAttribute), true);

                try
                {
                    packetHandler.Invoke(conn, Packet);
                }
                catch (Exception e)
                {
                    Log.Error("HandlePacket: ", "Packet handler error :" + Packet.Opcode + " " + e.ToString());
                }
            }
            else
            {
                Log.Error("HandlePacket: ", "Can not Handle opcode :" + Packet.Opcode + "(" + Packet.Opcode.ToString("X8") + ")");
            }
        }
Example #4
0
        public void HandlePacket(BaseClient client, PacketIn Packet)
        {
            Log.Dump("packethandle", Packet.ToArray(), 0, Packet.ToArray().Length);
            if (client == null || Packet == null)
            {
                Log.Error("TCPManager", "Packet || Client == null");
                return;
            }

            PacketFunction packetHandler = null;

            if (Packet.Opcode < (ulong)m_packetHandlers.Length)
            {
                packetHandler = m_packetHandlers[Packet.Opcode];
            }
            else if (!Errors.Contains(Packet.Opcode))
            {
                Errors.Add(Packet.Opcode);
                Log.Error("TCPManager", "Can not handle :" + Packet.Opcode + "(" + Packet.Opcode.ToString("X8") + ")");
            }

            if (packetHandler != null)
            {
                PacketHandlerAttribute[] packethandlerattribs = (PacketHandlerAttribute[])packetHandler.GetType().GetCustomAttributes(typeof(PacketHandlerAttribute), true);
                if (packethandlerattribs.Length > 0)
                {
                    if (packethandlerattribs[0].State > client.State)
                    {
                        Log.Error("TCPManager", "Can not handle packet (" + Packet.Opcode.ToString("X8") + "), Invalid client state (" + client.GetIp + ")");
                        return;
                    }
                }

                try
                {
                    packetHandler.Invoke(client, Packet);
                }
                catch (Exception e)
                {
                    Log.Error("TCPManager", "Packet handler error :" + Packet.Opcode + " " + e.ToString());
                }
            }
            else if (!Errors.Contains(Packet.Opcode))
            {
                Errors.Add(Packet.Opcode);
                Log.Error("TCPManager", "Can not Handle opcode :" + Packet.Opcode + "(" + Packet.Opcode.ToString("X8") + ")");
            }
        }
        public void HandlePacket(BaseClient client, PacketIn packet)
        {
            //#if DEBUG
            Log.Info("HandlePacket", $"Packet : {packet.Opcode} ({packet.Opcode.ToString("X8")})");
            Log.Dump("HandlePacket", packet.ToArray(), 0, packet.ToArray().Length);
            //#endif

            if (client == null)
            {
                Log.Error("TCPManager", "Client == null");
                return;
            }

            PacketFunction packetHandler = null;

            if (packet.Opcode < (ulong)m_packetHandlers.Length)
            {
                packetHandler = m_packetHandlers[packet.Opcode];
            }
            else if (!Errors.Contains(packet.Opcode))
            {
                Errors.Add(packet.Opcode);
                Log.Error("TCPManager", $"Can not handle :{packet.Opcode} ({packet.Opcode.ToString("X8")})");
            }

            if (packetHandler != null)
            {
                /*
                 *
                 * The reflection code below seems to have been used to verify the client was in the correct state before the packet was handled.
                 * However, it didn't actually work; eliminating the reflection and using an array implementation broke the emu and testing confirmed
                 * the original check was broken, so I've eliminated it for now.
                 *
                 *
                 * if (stateRequirement[packet.Opcode] > client.State)
                 * {
                 *  Log.Error("TCPManager", $"Can not handle packet ({packet.Opcode.ToString("X8")}), Invalid client state {client.State} (expected {stateRequirement[packet.Opcode]}) ({client.GetIp()})");
                 *  PacketHandlerAttribute[] packethandlerattribs = (PacketHandlerAttribute[])packetHandler.GetType().GetCustomAttributes(typeof(PacketHandlerAttribute), true);
                 *  if (packethandlerattribs.Length > 0)
                 *  {
                 *      Log.Error("TCPManager", $"Old code provided state {packethandlerattribs[0].State}");
                 *      if (packethandlerattribs[0].State > client.State)
                 *          return;
                 *  }
                 *  else
                 *  {
                 *      Log.Error("TCPManager", "Old code was stateless");
                 *  }
                 * }
                 */

                try
                {
                    packetHandler.Invoke(client, packet);
                }
                catch (Exception e)
                {
                    Log.Error("TCPManager", $"Packet handler error :{packet.Opcode} {e}");
                }
            }
            else if (!Errors.Contains(packet.Opcode))
            {
                Errors.Add(packet.Opcode);
                Log.Error("TCPManager", $"Can not Handle opcode :{packet.Opcode} ({packet.Opcode.ToString("X8")})");
            }
        }
 // Enregistre un handler
 public void RegisterPacketHandler(int packetCode, PacketFunction handler)
 {
     m_packetHandlers[packetCode] = handler;
 }