Ejemplo n.º 1
0
        private static bool LoadPacketDefintions()
        {
            string filePath = "Config/Server/Packets.xml";

            if (File.Exists(filePath) == false)
            {
                ServerConsole.WriteLine(GodLesZ.Library.EConsoleColor.Error, "failed, file not found!");
                Core.Kill();
                return(false);
            }

            XmlSerializer xml = new XmlSerializer(typeof(PacketList));

            using (Stream fs = File.OpenRead(filePath)) {
                mPackets = (PacketList)xml.Deserialize(fs);
            }

            // TODO: Make a defaul packet version, holding base definitions
            //		 and let them be overritten from higher versions


            // Load all types from script assemblies
            //List<Type> scriptTypes = new List<Type>();

            /*
             * foreach (Assembly asm in Scripting.ScriptCompiler.Assemblies) {
             *      scriptTypes.AddRange(asm.GetTypes());
             * }
             */
            Assembly lookupAsm = Assembly.GetExecutingAssembly();

            // Attach all packet handlers
            foreach (PacketVersion packets in mPackets)
            {
                foreach (PacketDefinition p in packets.Packets)
                {
                    string asmName = string.Format("Rovolution.Server.Network.Packets.{0}", p.HandlerType);

                    /*
                     * foreach (Type type in scriptTypes) {
                     *      if (type.FullName == asmName) {
                     *              MethodInfo info = type.GetMethod(p.HandlerName, BindingFlags.Public | BindingFlags.Static);
                     *              Delegate dele = Delegate.CreateDelegate(typeof(OnPacketReceive), info);
                     *              PacketHandlers.Register(p.ID, p.Length, (OnPacketReceive)dele);
                     *
                     *              found = true;
                     *              break;
                     *      }
                     * }
                     */
                    Type t = lookupAsm.GetType(asmName);
                    if (t != null)
                    {
                        MethodInfo info = t.GetMethod(p.HandlerName, BindingFlags.Public | BindingFlags.Static);
                        if (info == null)
                        {
                            ServerConsole.ErrorLine("Unable to find Packet handler for definition: {0}.{1}", p.HandlerType, p.HandlerName);
                            continue;
                        }
                        Delegate dele = Delegate.CreateDelegate(typeof(OnPacketReceive), info);
                        PacketHandlers.Register(p.HandlerName, p.ID, p.Length, (OnPacketReceive)dele);
                    }
                    else
                    {
                        ServerConsole.ErrorLine("Unable to find Packet handler for definition: {0}.{1}", p.HandlerType, p.HandlerName);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        public bool HandleReceive(NetState ns)
        {
            if (ns == null || ns.Running == false)
            {
                return(false);
            }
            ByteQueue buffer = ns.Buffer;

            if (buffer == null || buffer.Length <= 0)
            {
                return(true);
            }

            ServerConsole.DebugLine("{0}: Incoming data, {1} bytes", ns, buffer.Length);

            /*
             * Packet Analyse/verify && Parsing
             */

            lock (buffer) {
                int turns  = 0;
                int length = buffer.Length;
                while (length > 0 && ns != null && ns.Running)
                {
                    short packetID = buffer.GetPacketID();

#if DEBUG_PACKETS
                    // debug log
                    using (TextWriter writer = File.CreateText(AppDomain.CurrentDomain.BaseDirectory + @"\packet_" + DateTime.Now.UnixTimestamp() + ".log")) {
                        using (MemoryStream ms = new MemoryStream(buffer.ByteBuffer))
                            Tools.FormatBuffer(writer, ms, (int)ms.Length);
                    }
#endif

                    PacketHandler handler = PacketHandlers.GetHandler(packetID);
                    if (handler == null)
                    {
                        byte[] data = new byte[length];
                        length = buffer.Dequeue(data, 0, length);

                        // Log unknown packets
                        string unknownPacketPath = AppDomain.CurrentDomain.BaseDirectory + @"\packet_" + packetID.ToString("X4") + ".log";
                        if (File.Exists(unknownPacketPath) == false)
                        {
                            using (TextWriter writer = File.CreateText(unknownPacketPath)) {
                                writer.WriteLine("Unknown packet 0x" + packetID.ToString("X4") + ", length " + length);
                                using (MemoryStream ms = new MemoryStream(data)) {
                                    Tools.FormatBuffer(writer, ms, (int)ms.Length);
                                }
                            }
                        }

                        ServerConsole.WarningLine("{0}: P {1:X4}, {2} bytes, no Handler found!", ns, packetID, length);
                        break;
                    }

                    ServerConsole.StatusLine("{0}: [{1}] P {2:X4}, {3} bytes, handled {4}", ns, handler.Name, packetID, length, handler.Length);

                    byte[] packetBuffer;
                    // If we set the length to -1 (dynamic packet length), read the full buffer
                    int bufferLength = (handler.Length > 0 ? handler.Length : length);
                    if (bufferLength < mBufferSize)
                    {
                        packetBuffer = mBuffers.AcquireBuffer();
                    }
                    else
                    {
                        packetBuffer = new byte[bufferLength];
                    }

                    buffer.Dequeue(packetBuffer, 0, bufferLength);

                    PacketReader r = new PacketReader(packetBuffer, bufferLength, 0, true);
                    handler.OnReceive(ns, r);
                    length -= bufferLength;

                    if (bufferLength < mBufferSize)
                    {
                        mBuffers.ReleaseBuffer(packetBuffer);
                    }
                    else
                    {
                        packetBuffer = null;
                    }

                    turns++;
                }         // end while()*/
            }             // end Lock()

            // Clear internal byte buffer for receive data
            if (ns != null && ns.Buffer != null)
            {
                ns.Buffer.Clear();
            }

            return(true);
        }