NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/Net/NetReader.cs

126 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//////////////////////////////////////////////////////////////////////////
//
// 文件Assets/Code/Scripts/Framework/Net/Base/NetReader.cs
// 作者Xoen Xie
// 时间2023/07/18
// 描述:二进制流式读,小端
// 说明:
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Text;
namespace Framework
{
public sealed class NetReader
{
private byte[] m_Buffer;
private int m_nCurPos;
public byte[] Buffer { get { return m_Buffer; } }
public int CurPos { get { return m_nCurPos; } }
public void Init(byte[] buffer, int pos = 0)
{
m_Buffer = buffer;
m_nCurPos = pos;
}
public void ReadByte(out byte value)
{
value = m_Buffer[m_nCurPos++];
}
public void ReasSbyte(out sbyte value)
{
value = (sbyte)m_Buffer[m_nCurPos++];
}
public void ReadShort(out short value)
{
value = BitConverter.ToInt16(m_Buffer, m_nCurPos);
m_nCurPos += sizeof(short);
}
public void ReadUShort(out ushort value)
{
value = BitConverter.ToUInt16(m_Buffer, m_nCurPos);
m_nCurPos += sizeof(ushort);
}
public void ReadInt(out int value)
{
value = BitConverter.ToInt32(m_Buffer, m_nCurPos);
m_nCurPos += sizeof(int);
}
public void ReadUInt(out uint value)
{
value = BitConverter.ToUInt32(m_Buffer, m_nCurPos);
m_nCurPos += sizeof(uint);
}
public void ReadLong(out long value)
{
value = BitConverter.ToInt64(m_Buffer, m_nCurPos);
m_nCurPos += sizeof(long);
}
public void ReadULong(out ulong value)
{
value = BitConverter.ToUInt64(m_Buffer, m_nCurPos);
m_nCurPos += sizeof(ulong);
}
public void ReadBool(out bool value)
{
value = BitConverter.ToBoolean(m_Buffer, m_nCurPos);
m_nCurPos += sizeof(bool);
}
public void ReadFloat(out float value)
{
value = BitConverter.ToSingle(m_Buffer, m_nCurPos);
m_nCurPos += sizeof(float);
}
public void ReadDouble(out double value)
{
value = BitConverter.ToDouble(m_Buffer, m_nCurPos);
m_nCurPos += sizeof(double);
}
public void ReadU8String(out string value)
{
byte length = 0;
ReadByte(out length);
if (length == 0)
{
value = null;
return;
}
value = Encoding.UTF8.GetString(m_Buffer, m_nCurPos, length);
m_nCurPos += length;
}
public void ReadU16String(out string value)
{
ushort length = 0;
ReadUShort(out length);
if (length == 0)
{
value = null;
return;
}
value = Encoding.UTF8.GetString(m_Buffer, m_nCurPos, length);
m_nCurPos += length;
}
}
}