Avalon.Network.PacketReader.ReadUInt16 C# (CSharp) Method

ReadUInt16() public method

Read UInt16 Big Endian
public ReadUInt16 ( ) : ushort
return ushort
        public ushort ReadUInt16()
        {
            if ((m_Index + 2) > m_Size)
                return 0;

            return (ushort)(m_Data[m_Index++] | (m_Data[m_Index++] << 8));
        }

Usage Example

        /// <summary>
        /// ProcessPacket takes a given buffer and imports it to a Packet Factory for
        /// further processing.
        /// </summary>
        /// <param name="buffer"></param>
        public void ProcessPacket(byte[] buffer, SocketClient sockstate)
        {
            int offset = 0;
            pReader = new PacketReader(buffer, buffer.Length, true);

            // traverse packet buffer for cached packets
            while ((m_bPacketStream.Length - offset) >= m_HeaderSize)
            {
                // packet information
                pReader.Seek(offset, System.IO.SeekOrigin.Begin);
                UInt16 Size = pReader.ReadUInt16();
                UInt16 Flag = pReader.ReadUInt16();
                UInt16 Opcode = pReader.ReadUInt16();

                if ((Flag == (UInt16)PacketFlag.Master) && (Size < m_MaxPacketSize))
                {
                    byte[] payload = new byte[Size];
                    Buffer.BlockCopy(m_bPacketStream, offset, payload, 0, Size);

                    Console.WriteLine(Utility.Conversions.Misc.HexBytes(payload));
                    // find request packet
                    if (PacketHandler.OpcodeList.ContainsKey(Opcode))
                    {
                        PacketHandler.OpcodeList[Opcode](payload, sockstate);
                    }

                    offset += Size;
                }
                else
                {
                    break;
                }
            }
        }