Пример #1
0
        public void Send(Packet p)
        {
            if (m_Socket == null || m_BlockAllPackets)
            {
                return;
            }

            PacketProfile prof  = PacketProfile.GetOutgoingProfile((byte)p.PacketID);
            DateTime      start = (prof == null ? DateTime.MinValue : DateTime.Now);

            byte[] buffer = p.Compile(m_CompressionEnabled);

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

                int length = buffer.Length;

                if (m_Encoder != null)
                {
                    m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length);
                }

                bool shouldBegin = false;

                lock (m_SendQueue)
                    shouldBegin = (m_SendQueue.Enqueue(buffer, length));

                if (shouldBegin)
                {
                    int    sendLength = 0;
                    byte[] sendBuffer = m_SendQueue.Peek(ref sendLength);

                    try
                    {
                        m_Socket.BeginSend(sendBuffer, 0, sendLength, SocketFlags.None, m_OnSend, null);
                        //Console.WriteLine( "Send: {0}: Begin send of {1} bytes", this, sendLength );
                    }
                    catch                     // ( Exception ex )
                    {
                        //Console.WriteLine(ex);
                        Dispose(false);
                    }
                }

                if (prof != null)
                {
                    prof.Record(length, DateTime.Now - start);
                }
            }
            else
            {
                Dispose();
            }
        }
Пример #2
0
            public void EncodeOutgoingPacket(NetState to, ref byte[] packetBuffer, ref int packetLength)
            {
                byte[] buffer;
                int    bufferLength = 0;

                byte packetID;

                if (to.CompressionEnabled)
                {
                    var firstByte = Decompressor.DecompressFirstByte(packetBuffer, packetLength);

                    if (!firstByte.HasValue)
                    {
                        Utility.PushColor(ConsoleColor.Yellow);
                        Console.WriteLine("Outgoing Packet Override: Unable to decompress packet!");
                        Utility.PopColor();

                        return;
                    }

                    packetID = firstByte.Value;
                }
                else
                {
                    packetID = packetBuffer[0];
                }

                OutgoingPacketOverrideHandler handler = GetHandler(packetID);

                if (handler == null)
                {
                    handler = GetExtendedHandler(packetID);
                }

                if (handler != null)
                {
                    if (to.CompressionEnabled)
                    {
                        Decompressor.DecompressAll(packetBuffer, packetLength, _UnpackBuffer, ref bufferLength);

                        buffer = new byte[bufferLength];
                        Buffer.BlockCopy(_UnpackBuffer, 0, buffer, 0, bufferLength);
                    }
                    else
                    {
                        buffer       = packetBuffer;
                        bufferLength = packetLength;
                    }

                    handler(to, new PacketReader(buffer, packetLength, true), ref packetBuffer, ref packetLength);
                }

                if (_Successor != null)
                {
                    _Successor.EncodeOutgoingPacket(to, ref packetBuffer, ref packetLength);
                }
            }
Пример #3
0
        public virtual void Send(Packet p)
        {
            if (mSocket == null || mBlockAllPackets)
            {
                p.OnSend();
                return;
            }

            PacketSendProfile prof = PacketSendProfile.Acquire(p.GetType());

            int length = 0;

            byte[] buffer = p.Compile(out length);
            if (buffer == null)
            {
                CConsole.ErrorLine("{0}: null buffer send, disconnecting...", this);
                using (StreamWriter op = new StreamWriter("null_send.log", true)) {
                    op.WriteLine("{0} Client", "{1}: null buffer send, disconnecting...", DateTime.Now, this);
                    op.WriteLine(new System.Diagnostics.StackTrace());
                }
                Dispose();
                return;
            }

            if (buffer.Length <= 0 || length <= 0)
            {
                p.OnSend();
                return;
            }

            if (prof != null)
            {
                prof.Start();
            }

            if (mEncoder != null)
            {
                mEncoder.EncodeOutgoingPacket(this, ref buffer, ref length);
            }

            try {
                Int16 pID = BitConverter.ToInt16(buffer, 0);
                CConsole.DebugLine("{0}: sending Packet 0x{1:X4}", this, pID);
                mSocket.BeginSend(buffer, 0, length, SocketFlags.None, mOnSend, mSocket);
            } catch (Exception ex) {
                ExceptionHandler.Trace(ex);
                Dispose(false);
            }

            p.OnSend();

            if (prof != null)
            {
                prof.Finish(length);
            }
        }
Пример #4
0
            public void EncodeOutgoingPacket(NetState to, ref byte[] packetBuffer, ref int packetLength)
            {
                byte[] buffer;
                int    bufferLength;
                byte   packetId;

                if (to.CompressionEnabled)
                {
                    var firstByte = Decompressor.DecompressFirstByte(packetBuffer, packetLength);

                    if (!firstByte.HasValue)
                    {
                        Utility.PushColor(ConsoleColor.Yellow);
                        Console.WriteLine("Outgoing Packet Override: Unable to decompress packet!");
                        Utility.PopColor();

                        return;
                    }

                    packetId = firstByte.Value;
                }
                else
                {
                    packetId = packetBuffer[0];
                }

                var oHandler = GetHandler(packetId) ?? GetExtendedHandler(packetId);

                if (oHandler != null)
                {
                    if (to.CompressionEnabled)
                    {
                        Decompressor.DecompressAll(packetBuffer, packetLength, _UnpackBuffer, out bufferLength);

                        buffer = new byte[bufferLength];
                        Buffer.BlockCopy(_UnpackBuffer, 0, buffer, 0, bufferLength);
                    }
                    else
                    {
                        buffer = packetBuffer;
                    }

                    var reader = new PacketReader(buffer, packetLength, false);
                    reader.Seek(0, SeekOrigin.Begin);

                    oHandler(to, reader, ref packetBuffer, ref packetLength);
                }

                if (_Successor != null)
                {
                    _Successor.EncodeOutgoingPacket(to, ref packetBuffer, ref packetLength);
                }
            }
Пример #5
0
        public virtual void Send(Packet p)
        {
            if (mSocket == null || mBlockAllPackets)
            {
                p.OnSend();
                return;
            }

            PacketSendProfile prof = PacketSendProfile.Acquire(p.GetType());

            int length;

            byte[] buffer = p.Compile(out length);
            if (buffer == null)
            {
                CConsole.ErrorLine("{0}: null buffer send, disconnecting...", this);

                Dispose();
                return;
            }

            if (buffer.Length <= 0 || length <= 0)
            {
                p.OnSend();
                return;
            }

            if (prof != null)
            {
                prof.Start();
            }

            if (mEncoder != null)
            {
                mEncoder.EncodeOutgoingPacket(this, ref buffer, ref length);
            }

            try {
                Int16 pID = BitConverter.ToInt16(buffer, 0);
                CConsole.DebugLine("{0}: sending Packet 0x{1:X4}", this, pID);
                mSocket.BeginSend(buffer, 0, length, SocketFlags.None, mOnSend, mSocket);
            } catch (Exception ex) {
                Dispose(false);
                throw;
            }

            p.OnSend();

            if (prof != null)
            {
                prof.Finish(length);
            }
        }
Пример #6
0
        public void EncodeOutgoingPacketTest()
        {
            IPacketEncoder target     = CreateIPacketEncoder(); // TODO: 初始化为适当的值
            NetState       netStateTo = null;                   // TODO: 初始化为适当的值

            byte[] byteBuffer         = null;                   // TODO: 初始化为适当的值
            byte[] byteBufferExpected = null;                   // TODO: 初始化为适当的值
            long   iLength            = 0;                      // TODO: 初始化为适当的值
            long   iLengthExpected    = 0;                      // TODO: 初始化为适当的值

            target.EncodeOutgoingPacket(netStateTo, ref byteBuffer, ref iLength);
            Assert.AreEqual(byteBufferExpected, byteBuffer);
            Assert.AreEqual(iLengthExpected, iLength);
            Assert.Inconclusive("无法验证不返回值的方法。");
        }
Пример #7
0
        public virtual void Send(Packet p)
        {
            if (m_Socket == null || m_BlockAllPackets)
            {
                p.OnSend();
                return;
            }

            int length;

            byte[] buffer = p.Compile(m_CompressionEnabled, out length);

            if (buffer != null)
            {
                if (buffer.Length <= 0 || length <= 0)
                {
                    p.OnSend();
                    return;
                }

                if (m_Encoder != null)
                {
                    m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length);
                }

                try
                {
                    SendQueue.Gram gram;

                    lock (m_SendQueue)
                    {
                        gram = m_SendQueue.Enqueue(buffer, length);
                    }

                    if (gram != null)
                    {
#if Framework_4_0
                        m_SendEventArgs.SetBuffer(gram.Buffer, 0, gram.Length);
                        Send_Start();
#else
                        try {
                            m_Socket.BeginSend(gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, m_Socket);
                        } catch (Exception ex) {
                            TraceException(ex);
                            Dispose(false);
                        }
#endif
                    }
                }
                catch (CapacityExceededException)
                {
                    Console.WriteLine("Client: {0}: Too much data pending, disconnecting...", this);
                    Dispose(false);
                }

                p.OnSend();
            }
            else
            {
                Console.WriteLine("Client: {0}: null buffer send, disconnecting...", this);
                using (StreamWriter op = new StreamWriter("null_send.log", true))
                {
                    op.WriteLine("{0} Client: {1}: null buffer send, disconnecting...", DateTime.Now, this);
                    op.WriteLine(new System.Diagnostics.StackTrace());
                }
                Dispose();
            }
        }
Пример #8
0
        public void Send(Packet p)
        {
            if (m_Socket == null || m_BlockAllPackets)
            {
                p.OnSend();
                return;
            }

            PacketProfile prof  = PacketProfile.GetOutgoingProfile((byte)p.PacketID);
            DateTime      start = (prof == null ? DateTime.MinValue : DateTime.Now);

            int length;

            byte[] buffer = p.Compile(m_CompressionEnabled, out length);

            if (buffer != null)
            {
                if (buffer.Length <= 0 || length <= 0)
                {
                    p.OnSend();
                    return;
                }

                if (m_Encoder != null)
                {
                    m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length);
                }

                SendEnqueueResult enqueueResult;

                lock (m_SendQueue)
                    enqueueResult = (m_SendQueue.Enqueue(buffer, length));

                if (enqueueResult == SendEnqueueResult.Begin)
                {
                    int    sendLength = 0;
                    byte[] sendBuffer = m_SendQueue.Peek(ref sendLength);

                    try
                    {
                        IAsyncResult res = m_Socket.BeginSend(sendBuffer, 0, sendLength, SocketFlags.None, m_OnSend, null);
                        //Console.WriteLine( "Send: {0}: Begin send of {1} bytes", this, sendLength );
                    }
                    catch                     // ( Exception ex )
                    {
                        //Console.WriteLine(ex);
                        Dispose(false);
                    }
                }
                else if (enqueueResult == SendEnqueueResult.Overflow)
                {
                    Console.WriteLine("Client: {0}: Too much data pending, disconnecting...", this);
                    Dispose(false);
                }

                p.OnSend();

                if (prof != null)
                {
                    prof.Record(length, DateTime.Now - start);
                }
            }
            else
            {
                Dispose();
            }
        }
Пример #9
0
        public virtual void Send(Packet p)
        {
            if (m_Socket == null || m_BlockAllPackets)
            {
                p.OnSend();
                return;
            }

            int length;
            var buffer = p.Compile(m_CompressionEnabled, out length);

            if (buffer != null)
            {
                if (buffer.Length <= 0 || length <= 0)
                {
                    p.OnSend();
                    return;
                }

                PacketSendProfile prof = null;

                if (Core.Profiling)
                {
                    prof = PacketSendProfile.Acquire(p.GetType());
                }

                if (prof != null)
                {
                    prof.Start();
                }

                if (m_Encoder != null)
                {
                    m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length);
                }

                try
                {
                    SendQueue.Gram gram;

                    lock (_sendL)
                    {
                        lock (m_SendQueue)
                            gram = m_SendQueue.Enqueue(buffer, length);

                        if (gram != null && !_sending)
                        {
                            _sending = true;
#if NewAsyncSockets
                            m_SendEventArgs.SetBuffer(gram.Buffer, 0, gram.Length);
                            Send_Start();
#else
                            try
                            {
                                m_Socket.BeginSend(gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, m_Socket);
                            }
                            catch (Exception ex)
                            {
                                TraceException(ex);
                                Dispose(false);
                            }
#endif
                        }
                    }
                }
                catch (CapacityExceededException)
                {
                    Utility.PushColor(ConsoleColor.DarkRed);
                    Console.WriteLine("Client: {0}: Too much data pending, disconnecting...", this);
                    Utility.PopColor();
                    Dispose(false);
                }

                p.OnSend();

                if (prof != null)
                {
                    prof.Finish(length);
                }
            }
            else
            {
                Utility.PushColor(ConsoleColor.DarkRed);
                Console.WriteLine("Client: {0}: null buffer send, disconnecting...", this);
                Utility.PopColor();
                using (StreamWriter op = new StreamWriter("null_send.log", true))
                {
                    op.WriteLine("{0} Client: {1}: null buffer send, disconnecting...", DateTime.UtcNow, this);
                    op.WriteLine(new StackTrace());
                }
                Dispose();
            }
        }
Пример #10
0
        public virtual void Send(Packet p)
        {
            if (m_Socket == null || m_BlockAllPackets)
            {
                p.OnSend();
                return;
            }
            if (this.Version != null && this.Version.Major <= 3)
            {
                if (p is ObjectPropertyList || p is OPLInfo)
                {
                    return;
                }

                if (p is MobileMovingOld)
                {
                    var packet = p as MobileMovingOld;

                    var newBody = Mythik.BodyConverter.ConvertBody203(packet.Body, this.Mobile);
                    if (packet.Body != newBody)
                    {
                        var oldpos = p.UnderlyingStream.Position;
                        p.UnderlyingStream.UnderlyingStream.Seek(5, SeekOrigin.Begin);
                        p.UnderlyingStream.Write((short)newBody);
                        p.UnderlyingStream.UnderlyingStream.Position = oldpos;
                    }
                }
                if (p is MobileUpdateOld)
                {
                    var packet = p as MobileUpdateOld;

                    var newBody = Mythik.BodyConverter.ConvertBody203(packet.Body, this.Mobile);
                    if (packet.Body != newBody)
                    {
                        var oldpos = p.UnderlyingStream.Position;
                        p.UnderlyingStream.UnderlyingStream.Seek(5, SeekOrigin.Begin);
                        p.UnderlyingStream.Write((short)newBody);
                        p.UnderlyingStream.UnderlyingStream.Position = oldpos;
                    }
                }
                //skils gump, journal gump, char creation
                //Lets check for unsupported packets.
                if (p is MessageLocalized)
                {
                    var packet = p as MessageLocalized;
                    if (packet.labelNumber > 0)// 1053000)
                    {
                        string nameString = "";
                        if (packet.labelNumber > 0 || packet.m_ItemID > 0)
                        {
                            nameString = CliLoc.LocToString(packet.labelNumber, packet.args);
                        }
                        Send(new AsciiMessage(packet.m_Serial, packet.m_ItemID, packet.type, packet.hue, packet.font, "", nameString));
                    }

                    return;
                }
                if (p is MessageLocalizedAffix)
                {
                    var packet     = p as MessageLocalizedAffix;
                    var nameString = "";
                    nameString = CliLoc.LocToString(packet.number, packet.args);
                    Send(new AsciiMessage(packet.m_Serial, packet.m_ItemID, packet.label, packet.hue, packet.font, "", packet.affix.Replace(":", "") + " " + nameString));
                    return;
                }
                if (p is UnicodeMessage)
                {
                    var packet = p as UnicodeMessage;
                    Send(new AsciiMessage(packet._serial, packet._graphic, packet._type, packet._hue, packet._font, packet._name, packet._text));
                    return;
                }
            }

            PacketSendProfile prof = PacketSendProfile.Acquire(p.GetType());

            int length;

            byte[] buffer = p.Compile(m_CompressionEnabled, out length);

            if (buffer != null)
            {
                if (buffer.Length <= 0 || length <= 0)
                {
                    p.OnSend();
                    return;
                }

                if (prof != null)
                {
                    prof.Start();
                }

                if (m_Encoder != null)
                {
                    m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length);
                }

                try {
                    SendQueue.Gram gram;

                    lock ( m_SendQueue ) {
                        gram = m_SendQueue.Enqueue(buffer, length);
                    }

                    if (gram != null)
                    {
#if NewAsyncSockets
                        m_SendEventArgs.SetBuffer(gram.Buffer, 0, gram.Length);
                        Send_Start();
#else
                        try {
                            m_Socket.BeginSend(gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, m_Socket);
                        } catch (Exception ex) {
                            TraceException(ex);
                            Dispose(false);
                        }
#endif
                    }
                } catch (CapacityExceededException) {
                    Console.WriteLine("Client: {0}: Too much data pending, disconnecting...", this);
                    Dispose(false);
                }

                p.OnSend();

                if (prof != null)
                {
                    prof.Finish(length);
                }
            }
            else
            {
                Console.WriteLine("Client: {0}: null buffer send, disconnecting...", this);
                using (StreamWriter op = new StreamWriter("null_send.log", true))
                {
                    op.WriteLine("{0} Client: {1}: null buffer send, disconnecting...", DateTime.UtcNow, this);
                    op.WriteLine(new System.Diagnostics.StackTrace());
                }
                Dispose();
            }
        }
Пример #11
0
        public virtual void Send(Packet p)
        {
            if (m_Socket == null || m_BlockAllPackets)
            {
                p.OnSend();
                return;
            }

            PacketSendProfile prof = PacketSendProfile.Acquire(p.GetType());

            int length;

            byte[] buffer = p.Compile(m_CompressionEnabled, out length);

            if (buffer != null)
            {
                if (buffer.Length <= 0 || length <= 0)
                {
                    p.OnSend();
                    return;
                }

                if (prof != null)
                {
                    prof.Start();
                }

                if (m_Encoder != null)
                {
                    m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length);
                }
                // Alan's packet debugging mod
                if (m_LogPackets)
                {
                    if (Logger == null)
                    {
                        Logger = new PacketSimulationLogger();
                    }
                    Logger.AddSendPacket(buffer, length, null, p.GetType());
                }
                // end Alan's mod

                try {
                    SendQueue.Gram gram;

                    lock ( m_SendQueue ) {
                        gram = m_SendQueue.Enqueue(buffer, length);
                    }

                    if (gram != null)
                    {
#if NewAsyncSockets
                        m_SendEventArgs.SetBuffer(gram.Buffer, 0, gram.Length);
                        Send_Start();
#else
                        try {
                            m_Socket.BeginSend(gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, m_Socket);
                        } catch (Exception ex) {
                            TraceException(ex);
                            Dispose(false);
                        }
#endif
                    }
                } catch (CapacityExceededException) {
                    Console.WriteLine("Client: {0}: Too much data pending, disconnecting...", this);
                    Dispose(false);
                }

                p.OnSend();

                if (prof != null)
                {
                    prof.Finish(length);
                }
            }
            else
            {
                Console.WriteLine("Client: {0}: null buffer send, disconnecting...", this);
                using (StreamWriter op = new StreamWriter("null_send.log", true))
                {
                    op.WriteLine("{0} Client: {1}: null buffer send, disconnecting...", DateTime.UtcNow, this);
                    op.WriteLine(new System.Diagnostics.StackTrace());
                }
                Dispose();
            }
        }
Пример #12
0
        public virtual void Send(Packet p)
        {
            if (mSocket == null || mBlockAllPackets)
            {
                ServerConsole.ErrorLine("{0}: Socket is null! Packet {1:X4} ({2} bytes) cant be send", this, p.PacketID, p.Length);
                // Wont send packet, but trigger OnSend to free data..
                p.OnSend(this);
                Dispose();
                return;
            }

            // Allow APIs to break sending
            if (p.OnBeforeSend(this) == false)
            {
                // Didnt send the packet, so let the APIs know that (2nd param, false)
                p.OnSend(this, false);
                return;
            }
            PacketSendProfile prof = PacketSendProfile.Acquire(p.GetType());

            int length = 0;

            byte[] buffer = p.Compile(out length);
            if (buffer == null)
            {
                ServerConsole.ErrorLine("{0}: null buffer send, disconnecting...", this);
                using (StreamWriter op = new StreamWriter("null_send.log", true)) {
                    op.WriteLine("{0} Client", "{1}: null buffer send, disconnecting...", DateTime.Now, this);
                    op.WriteLine(new System.Diagnostics.StackTrace());
                }
                Dispose();
                return;
            }

            if (buffer.Length <= 0 || length <= 0)
            {
                p.OnSend(this);
                return;
            }

            if (prof != null)
            {
                prof.Start();
            }

            if (mEncoder != null)
            {
                mEncoder.EncodeOutgoingPacket(this, ref buffer, ref length);
            }

            try {
                ServerConsole.DebugLine("{0}: sending Packet 0x{1:X4} ({2} bytes)", this, p.PacketID, length);
                mSocket.BeginSend(buffer, 0, length, SocketFlags.None, mOnSend, mSocket);
            } catch (Exception ex) {
                ExceptionHandler.Trace(ex);
                Dispose(false);
            }

            p.OnSend(this);

            if (prof != null)
            {
                prof.Finish(length);
            }
        }