Enqueue() public method

public Enqueue ( byte buffer, int length ) : Gram
buffer byte
length int
return Gram
Example #1
0
        public void Send(byte[] buffer, int length)
        {
            if (m_Socket == null)
            {
                return;
            }

            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, OnSend, null);
                    m_Sending = true;
                }
                catch (Exception ex)
                {
                    TraceException(ex);
                    Dispose(false);
                }
            }
        }
Example #2
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);
                        m_Sending = true;
                        //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();
            }
        }
Example #3
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();
            }
        }
Example #4
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();
            }
        }
Example #5
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();
            }
        }
Example #6
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();
            }
        }
Example #7
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();
            }
        }