using System; using System.Text; public class Packet : Base { // 4 for the package lenght in short ,one for package ID in byte ,and one for policy id in byte protected int ReservedSize = 0; protected int InitSize = 30; internal byte[] buffer; protected int pos; protected int size; //协议号 protected UInt32 m_ProtocolCode; //public byte policyId //{ // get { return m_ProtocolCode; } // set { m_ProtocolCode = value; } //} public int Position { get { return this.pos; } set { this.pos = value; } } public int Size { get { return this.size; } } public int Capability { get { return this.buffer.Length; } } public int Remaining { get { return this.size - this.pos; } } public byte[] Buffer { get { return this.buffer; } } public Packet Copy() { Packet p = new Packet(); p.pos = pos; p.buffer = new byte[buffer.Length]; Array.Copy(buffer, p.buffer, buffer.Length); p.size = size; return p; } public Packet() { } public Packet(int reservedSize) { ReservedSize = reservedSize; } public Packet(byte[] bytes) { Init(bytes); } protected void Init(byte[] bytes) { this.pos = 0; this.buffer = new byte[bytes.Length]; Array.Copy(bytes, this.buffer, bytes.Length); this.size = bytes.Length; } public Packet(string str) { byte[] bytes = Encoding.UTF8.GetBytes(str); int len = bytes.Length; this.buffer = new byte[len + bytes.Length]; byte[] lenbytes = DBitConverter.GetBytes(len, false); Array.Copy(lenbytes, 0, this.buffer, this.pos, 4); this.pos += 2; Array.Copy(bytes, 0, this.buffer, this.pos, len); this.pos += len; this.size = this.pos; } public void BeginPacket(UInt32 protocolCode) { m_ProtocolCode = protocolCode; EnsureCapacity(0); this.EnsureCapacity(2); byte[] bytes = DBitConverter.GetBytes(m_ProtocolCode, false); Array.Copy(bytes, 0, this.buffer, 4, 2); //buffer[5] = m_ProtocolCode; } public void SetPacketId(int packetId) { buffer[4] = (byte)packetId; } public void EndPacket() { int packetLen = (int)(pos - 4); byte[] bPackLen = DBitConverter.GetBytes(packetLen, false); buffer[0] = bPackLen[0]; buffer[1] = bPackLen[1]; buffer[2] = bPackLen[2]; buffer[3] = bPackLen[3]; } public byte[] GetData() { byte[] array = new byte[this.size]; Array.Copy(this.buffer, array, this.size); return array; } public byte readByte() { if (buffer.Length - pos < 1) { LogError("Invalid Length"); return 0; } byte result = buffer[pos]; pos += 1; return result; } public byte[] readBytes(int len) { byte[] array = new byte[len]; Array.Copy(this.buffer, this.pos, array, 0, len); pos += len; return array; } public byte[] readBytes() { var len = size - pos; byte[] array = new byte[len]; Array.Copy(this.buffer, this.pos, array, 0, len); pos += len; return array; } public void writeByte(byte b) { this.EnsureCapacity(1); byte[] bytes = DBitConverter.GetBytes(b); Array.Copy(bytes, 0, this.buffer, this.pos, bytes.Length); this.pos += bytes.Length; if (this.size < this.pos) { this.size = this.pos; } } public void writeBytes(byte[] bytes) { // 确保缓冲区有足够的容量 this.EnsureCapacity(bytes.Length); // 将字节数组复制到缓冲区 Array.Copy(bytes, 0, this.buffer, this.pos, bytes.Length); // 更新位置指针 this.pos += bytes.Length; // 更新缓冲区大小(如果需要) if (this.size < this.pos) this.size = this.pos; } public SByte readSByte() { if (buffer.Length - pos < 1) { LogError("Invalid Length"); return 0; } SByte result = (SByte)buffer[pos]; pos += 1; return result; } public void writeShort(short value) { this.EnsureCapacity(2); byte[] bytes = DBitConverter.GetBytes(value, false); Array.Copy(bytes, 0, this.buffer, this.pos, 2); this.pos += 2; if (this.size < this.pos) { this.size = this.pos; } } public void writeUshort(ushort value) { this.EnsureCapacity(2); byte[] bytes = DBitConverter.GetBytes(value, false); Array.Copy(bytes, 0, this.buffer, this.pos, 2); this.pos += 2; if (this.size < this.pos) { this.size = this.pos; } } public short readShort() { short result = DBitConverter.ToShort(this.buffer, this.pos, false); this.pos += 2; return result; } public ushort readUshort(bool asc = false) { ushort result = DBitConverter.ToUshort(this.buffer, this.pos, asc); this.pos += 2; return result; } public void writeInt(int value) { this.EnsureCapacity(4); byte[] bytes = DBitConverter.GetBytes(value, false); Array.Copy(bytes, 0, this.buffer, this.pos, 4); this.pos += 4; if (this.size < this.pos) { this.size = this.pos; } } public void writeUint(uint value) { this.EnsureCapacity(4); byte[] bytes = DBitConverter.GetBytes(value, false); Array.Copy(bytes, 0, this.buffer, this.pos, 4); this.pos += 4; if (this.size < this.pos) { this.size = this.pos; } } public int readInt() { int result = DBitConverter.ToInt(this.buffer, this.pos, false); this.pos += 4; return result; } public uint readUint() { uint result = DBitConverter.ToUint(this.buffer, this.pos, false); this.pos += 4; return result; } public void writeLong(long value) { this.EnsureCapacity(8); byte[] bytes = DBitConverter.GetBytes(value, false); Array.Copy(bytes, 0, this.buffer, this.pos, 8); this.pos += 8; if (this.size < this.pos) { this.size = this.pos; } } public long readLong() { long result = DBitConverter.ToLong(this.buffer, this.pos, false); this.pos += 8; return result; } public string readUUID() { long result = DBitConverter.ToLong(this.buffer, this.pos, false); this.pos += 8; return result.ToString(); } public void writeFloat(float value) { this.EnsureCapacity(4); byte[] bytes = DBitConverter.GetBytes(value, false); Array.Copy(bytes, 0, this.buffer, this.pos, 4); this.pos += 4; if (this.size < this.pos) { this.size = this.pos; } } public float readFloat() { float result = DBitConverter.ToFloat(this.buffer, this.pos, false); this.pos += 4; return result; } public void writeString(string value) { byte[] bytes = Encoding.UTF8.GetBytes(value); int len = bytes.Length; this.EnsureCapacity(4 + len); this.writeInt((int)len); if (len <= 0) return; Array.Copy(bytes, 0, this.buffer, this.pos, len); this.pos += len; if (this.size < this.pos) { this.size = this.pos; } } public string readString() { int len = this.readInt(); if (len == 0) { return string.Empty; } string result = Encoding.UTF8.GetString(this.buffer, this.pos, (int)len); this.pos += (int)len; return result; } public string[] readUTFP() { string oriStr = this.readString(); byte paramsNum = this.readByte(); string[] res = new string[paramsNum + 1]; res[0] = oriStr; for (int i = 0; i < paramsNum; ++i) { res[i + 1] = this.readString(); } return res; } protected void EnsureCapacity(int increament) { if (buffer == null) { buffer = new byte[InitSize + ReservedSize]; pos = ReservedSize; size = ReservedSize; } int len = buffer.Length; if (len - pos >= increament) { return; } int count = (increament - (len - pos)) / InitSize; count += 1; byte[] tempBuff = new byte[len + InitSize * count]; Array.Copy(buffer, 0, tempBuff, 0, len); buffer = tempBuff; } public bool hasRead() { return this.pos < size; } }