Пример #1
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);
            }
        }
Пример #2
0
        protected Packet(int packetID)
        {
            PacketID = packetID;

            if (Core.Profiling)
            {
                var prof = PacketSendProfile.Acquire(GetType());
                prof.Increment();
            }
        }
Пример #3
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);
            }
        }
Пример #4
0
        protected Packet(int packetID, int length)
        {
            PacketID = packetID;
            m_Length = length;

            Stream = PacketWriter.CreateInstance(length); // new PacketWriter( length );
            Stream.Write((byte)packetID);

            if (Core.Profiling)
            {
                var prof = PacketSendProfile.Acquire(GetType());
                prof.Increment();
            }
        }
Пример #5
0
        public Packet(short packetID, int packetLength)
        {
            mPacketID = packetID;
            mLength   = packetLength;

            mStream = PacketWriter.CreateInstance();
            mStream.Write((short)mPacketID);
            mStream.Write(mLength);

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

            if (prof != null)
            {
                prof.Created++;
            }
        }
Пример #6
0
        public Packet(short packetID, int packetLength)
        {
            mPacketID = (ushort)packetID;
            mLength   = packetLength;

            mStream = PacketWriter.CreateInstance(packetLength);
            // On <= 0 packetID's we assume its a "special" packet
            // Special packets containing data without IDs..
            if (packetID > 0)
            {
                mStream.Write((short)mPacketID);
            }
            // Fix - not every packet has the length as second parameter..
            //mStream.Write(mLength);

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

            if (prof != null)
            {
                prof.Created++;
            }
        }
Пример #7
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);
                }

                try
                {
                    SendQueue.Gram gram;

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

                    if (gram != null)
                    {
                        try
                        {
                            m_Socket.BeginSend(gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, m_Socket);
                        }
                        catch (Exception ex)
                        {
                            TraceException(ex);
                            Dispose(false);
                        }
                    }
                }
                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.Now, this);
                    op.WriteLine(new System.Diagnostics.StackTrace());
                }
                Dispose();
            }
        }
Пример #8
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();
            }
        }
Пример #9
0
        public virtual void Send(Packet p)
        {
            if (Socket == null || BlockAllPackets)
            {
                p.OnSend();
                return;
            }

            int length;

            var buffer = p.Compile(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();
                }

                var buffered = false;

                if (PacketEncoder != null || PacketEncryptor != null)
                {
                    var packetBuffer = buffer;
                    var packetLength = length;

                    if (BufferStaticPackets && p.State.HasFlag(PacketState.Acquired))
                    {
                        if (packetLength <= SendBufferSize)
                        {
                            packetBuffer = m_SendBufferPool.AcquireBuffer();
                        }
                        else
                        {
                            packetBuffer = new byte[packetLength];
                        }

                        System.Buffer.BlockCopy(buffer, 0, packetBuffer, 0, packetLength);
                    }

                    if (PacketEncoder != null)
                    {
                        PacketEncoder.EncodeOutgoingPacket(this, ref packetBuffer, ref packetLength);
                    }

                    if (PacketEncryptor != null)
                    {
                        PacketEncryptor.EncryptOutgoingPacket(this, ref packetBuffer, ref packetLength);
                    }

                    buffered = buffer != packetBuffer && packetBuffer.Length == SendBufferSize;

                    buffer = packetBuffer;
                    length = packetLength;
                }

                try
                {
                    SendQueue.Gram gram;

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

                        if (buffered && m_SendBufferPool.Count < SendBufferCapacity)
                        {
                            m_SendBufferPool.ReleaseBuffer(buffer);
                        }

                        if (gram != null && !_Sending)
                        {
                            _Sending = true;

                            try
                            {
                                Socket.BeginSend(gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, Socket);
                            }
                            catch (Exception ex)
                            {
                                TraceException(ex);
                                Dispose(false);
                            }
                        }
                    }
                }
                catch (CapacityExceededException)
                {
                    Utility.PushColor(ConsoleColor.Red);
                    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.Red);
                Console.WriteLine("Client: {0}: null buffer send, disconnecting...", this);
                Utility.PopColor();

                using (var 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 (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);
            }
        }