Provides functionality for writing primitive binary data.
Example #1
0
        private void InternalCompile(bool compress)
        {
            if (m_Length == 0)
            {
                long streamLen = m_Stream.Length;

                m_Stream.Seek(1, SeekOrigin.Begin);
                m_Stream.Write((ushort)streamLen);
            }
            else if (m_Stream.Length != m_Length)
            {
                int diff = (int)m_Stream.Length - m_Length;

                Console.WriteLine("Packet: 0x{0:X2}: Bad packet length! ({1}{2} bytes)", PacketID, diff >= 0 ? "+" : "",
                                  diff);
            }

            MemoryStream ms = m_Stream.UnderlyingStream;

            m_CompiledBuffer = ms.GetBuffer();
            int length = (int)ms.Length;

            if (compress)
            {
                byte[] buffer = ArrayPool <byte> .Shared.Rent(CompressorBufferSize);

                Compression.Compress(m_CompiledBuffer, 0, length, buffer, out length);

                if (length <= 0)
                {
                    Console.WriteLine("Warning: Compression buffer overflowed on packet 0x{0:X2} ('{1}') (length={2})",
                                      PacketID, GetType().Name, length);
                    using StreamWriter op = new StreamWriter("compression_overflow.log", true);
                    op.WriteLine("{0} Warning: Compression buffer overflowed on packet 0x{1:X2} ('{2}') (length={3})",
                                 DateTime.UtcNow, PacketID, GetType().Name, length);
                    op.WriteLine(new StackTrace());
                }
                else
                {
                    m_CompiledLength = length;

                    if ((m_State & State.Static) != 0)
                    {
                        m_CompiledBuffer = new byte[length];
                        Buffer.BlockCopy(buffer, 0, m_CompiledBuffer, 0, length);
                        ArrayPool <byte> .Shared.Return(buffer);
                    }
                    else
                    {
                        m_CompiledBuffer = buffer;
                        m_State         |= State.Buffered;
                    }
                }
            }
            else if (length > 0)
            {
                byte[] old = m_CompiledBuffer;
                m_CompiledLength = length;

                if ((m_State & State.Static) != 0)
                {
                    m_CompiledBuffer = new byte[length];
                }
                else
                {
                    m_CompiledBuffer = ArrayPool <byte> .Shared.Rent(length);

                    m_State |= State.Buffered;
                }

                Buffer.BlockCopy(old, 0, m_CompiledBuffer, 0, length);
            }

            PacketWriter.ReleaseInstance(m_Stream);
            m_Stream = null;
        }
Example #2
0
 public void EnsureCapacity(int length)
 {
     Stream = PacketWriter.CreateInstance(length); // new PacketWriter( length );
     Stream.Write((byte)PacketID);
     Stream.Write((short)0);
 }
            public SmoothMove(BaseGalleon galleon, object[] objectsonboard)
                : base(0xF6)
            {
                short packetlen = 1 + 17;

                packetlen += (short)(objectsonboard.Length * 10);

                packetlen = (short)(((packetlen & 0xff) << 8) | ((packetlen >> 8) & 0xff));
                //to little endian

                m_Stream = new PacketWriter( packetlen );
                m_Stream.Write((byte)PacketID);

                m_Stream.Write((short)packetlen);

                m_Stream.Write((int)galleon.Serial);
                m_Stream.Write((byte)galleon.Speed);
                m_Stream.Write((byte)galleon.Moving);
                m_Stream.Write((byte)galleon.Direction);

                m_Stream.Write((short)galleon.X);
                m_Stream.Write((short)galleon.Y);
                m_Stream.Write((byte)0xFF);
                m_Stream.Write((sbyte)galleon.Z);

                m_Stream.Write((byte)0x0);
                m_Stream.Write((byte)objectsonboard.Length);

                #region items
                foreach (object o in objectsonboard)
                {
                    if (o is Mobile)
                    {
                            m_Stream.Write((int)((Mobile)o).Serial);
                            m_Stream.Write((short)((Mobile)o).X);
                            m_Stream.Write((short)((Mobile)o).Y);
                            m_Stream.Write((byte)0xFF);
                            m_Stream.Write((sbyte)((Mobile)o).Z);
                    }

                    if (o is Item)
                    {
                        m_Stream.Write((int)((Item)o).Serial);
                        m_Stream.Write((short)((Item)o).X);
                        m_Stream.Write((short)((Item)o).Y);
                        m_Stream.Write((byte)0xFF);
                        m_Stream.Write((sbyte)((Item)o).Z);
                    }
                }
                #endregion


                //    * BYTE[1] 0xF6
                //    * BYTE[2] Packet Length ( little endian format )
                //    * BYTE[4] Serial Multi
                //    * BYTE[1] Movement Speed
                //    * BYTE[1] Movement Direction
                //    * BYTE[1] Multi Direction
                //    * BYTE[2] X
                //    * BYTE[2] Y
                //    * BYTE[1] Fixed 0xFF
                //    * SBYTE[1] Z
                //    * BYTE[1] Fixed 0x0
                //    * BYTE[1] List Length
                //    * BYTE[List Length * 10] List 

                //Each List Entry is Composed by:

                //    * BYTE[4] Item Serial
                //    * BYTE[2] X
                //    * BYTE[2] Y
                //    * BYTE[1] Fixed 0xFF
                //    * SBYTE[1] Z 
            }