using UnityEngine; using System.Collections; using System; using System.Text; namespace Engine.Net { /// 网络字节流对象 public class NetBytes { public const int BYTE_LEN = 1; public const int SHORT16_LEN = 2; public const int INT32_LEN = 4; public const int FLOAT_LEN = 4; public const int LONG_LEN = 8; public const int DOUBLE_LEN = 8; private byte[] data; private int byteIndex = 0; public NetBytes(byte[] bytes = null) { data = (bytes == null) ? new byte[0] : bytes; } /// byte[] 数据原 public byte[] Data { get { return data; } set { data = value; } } /// 可读字节数,修改读取位置可修改此值 public int ByteLength { get { return data.Length - byteIndex; } } /// 总字节数据 public int Length { get { return data.Length; } } /// 当前读取位置 public int Position { get { return byteIndex; } set { byteIndex = value; } } public byte ReadByte() { return this.ReadBytes(1)[0]; } public bool ReadBool() { byte bt = this.ReadByte(); return (bt == (byte)1) ? true : false; } public short ReadShort() { return BitConverter.ToInt16(this.ReadBytes(SHORT16_LEN), 0); } public int ReadInt() { return BitConverter.ToInt32(this.ReadBytes(INT32_LEN), 0); } public long ReadLong() { return BitConverter.ToInt64(this.ReadBytes(LONG_LEN), 0); } public float ReadFloat() { return BitConverter.ToSingle(this.ReadBytes(FLOAT_LEN), 0); } public double ReadDouble() { return BitConverter.ToDouble(this.ReadBytes(DOUBLE_LEN), 0); } public string ReadString() { return Encoding.UTF8.GetString(this.ReadBytes(this.ReadShort(), false)); } public char[] ReadChars() { string x =Encoding.UTF8.GetString(this.ReadBytes(this.ReadShort(), false)); return x.ToCharArray(); } public byte[] ReadByteArray() { return this.ReadBytes(this.ReadShort(), false); } public Vector3 ReadVector3() { Vector3 v = Vector3.zero; v.x = BitConverter.ToSingle(this.ReadBytes(FLOAT_LEN), 0); v.y = BitConverter.ToSingle(this.ReadBytes(FLOAT_LEN), 0); v.z = BitConverter.ToSingle(this.ReadBytes(FLOAT_LEN), 0); return v; } public NetBytes ReadUnit() { int leng = this.ReadShort(); return new NetBytes(this.ReadBytes(leng, false)); } public byte[] ReadBytes(int leng, bool isReverse = true) { if (leng > this.ByteLength) { throw new Exception("超出字节流长度"); } //这里重复开辟了内存,会产生内存垃圾,可能会有性能问题 byte[] byets = new byte[leng]; for (int i = 0; i < leng; i++) { byets[i] = data[byteIndex]; byteIndex++; } if (isReverse) { //这里和真实后端通信的时候记得要反序 //Array.Reverse(byets); } return byets; } public void WriteByte(byte by) { byte[] bytes = new byte[BYTE_LEN]; bytes[0] = by; this.WriteBytes(bytes); } public void WriteBool(bool flag) { byte b = (byte)0; if (flag) b = (byte)1; this.WriteByte(b); } public void WriteShort(short value) { byte[] bytes = BitConverter.GetBytes(value); this.WriteBytes(bytes); } public void WriteInt(int value) { byte[] bytes = BitConverter.GetBytes(value); this.WriteBytes(bytes); } public void WriteLong(int value) { byte[] bytes = BitConverter.GetBytes(value); this.WriteBytes(bytes); } public void WriteDouble(double value) { byte[] bytes = BitConverter.GetBytes(value); this.WriteBytes(bytes); } public void WriteFloat(float value) { byte[] bytes = BitConverter.GetBytes(value); this.WriteBytes(bytes); } public void WriteString(string str, short len = -1) { if (len == (short)-1) { len = (short)Encoding.UTF8.GetByteCount(str); } this.WriteShort(len); byte[] bytes = new byte[len]; if (str == null) { CDebug.Log("string is null"); } Encoding.UTF8.GetBytes(str, 0, str.Length, bytes, 0); this.WriteBytes(bytes, false); } public void WriteChars(char[] str) { short len = 12; this.WriteShort(len); byte[] bytes = new byte[len]; Encoding.UTF8.GetBytes(str, 0, str.Length, bytes, 0); this.WriteBytes(bytes, false); } public void WriteByteArray(byte[] bs) { short len = (short)bs.Length; this.WriteShort(len); byte[] bytes = new byte[len]; Array.Copy(bs, bytes, len); // Encoding.UTF8.GetBytes(bs, 0, bs.Length, bytes, 0); this.WriteBytes(bytes, false); } public void WriteVector3(Vector3 v) { this.WriteFloat(v.x); this.WriteFloat(v.y); this.WriteFloat(v.z); } public void WriteUnit(byte[] bytes) { this.WriteShort((short)bytes.Length); this.WriteBytes(bytes, false); } public void WriteBytes(byte[] bytes, bool isReverse = true) { if (isReverse) { //这里和真实后端通信的时候记得要反序 //Array.Reverse(bytes); } byteIndex = data.Length; int leng = bytes.Length; //这里重复开辟了内存,会产生内存垃圾,可能会有性能问题 byte[] newData = new byte[byteIndex + leng]; data.CopyTo(newData, 0); Array.Copy(bytes, 0, newData, byteIndex, leng); data = newData; byteIndex = data.Length; } public void Dispose() { data = null; } } }