예제 #1
0
 public PacketHandler GetHandler(short packetID)
 {
     return(PacketHandlers.GetHandler(packetID));
 }
예제 #2
0
        public bool HandleReceive(NetState ns)
        {
            ByteQueue buffer = ns.Buffer;

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

            CConsole.DebugLine("{0}: Incoming Data - {1} Bytes", ns, buffer.Length);

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

            lock ( buffer ) {
                int length = buffer.Length;
                // debug Data
                while (length > 0 && ns.Running)
                {
                    short packetID = buffer.GetPacketID();

                    // 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);
                    }

                    CConsole.DebugLine("{0}: packetID {1}, {2} bytes", ns, packetID, length);

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

                        CConsole.ErrorLine("{0}: no Handler found! Data dispose", ns);
                        break;
                    }

                    CConsole.StatusLine("{0}: Handler found ({1} bytes)! Trigger Callback...", ns, handler.Length);

                    byte[] packetBuffer;
                    if (mBufferSize >= handler.Length)
                    {
                        packetBuffer = mBuffers.AcquireBuffer();
                    }
                    else
                    {
                        packetBuffer = new byte[handler.Length];
                    }

                    buffer.Dequeue(packetBuffer, 0, handler.Length);

                    PacketReader r = new PacketReader(packetBuffer, handler.Length, 0);
                    handler.OnReceive(ns, r);
                    length = buffer.Length;

                    if (mBufferSize >= handler.Length)
                    {
                        mBuffers.ReleaseBuffer(packetBuffer);
                    }
                }         // end while()*/
            }             // end Lock()

            return(true);
        }