123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Text;
- namespace Engine.Net
- {
- /// <summary>网络字节流对象</summary>
- 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;
- }
- /// <summary>byte[] 数据原</summary>
- public byte[] Data
- {
- get { return data; }
- set { data = value; }
- }
- /// <summary>可读字节数,修改读取位置可修改此值</summary>
- public int ByteLength
- {
- get { return data.Length - byteIndex; }
- }
- /// <summary>总字节数据</summary>
- public int Length
- {
- get { return data.Length; }
- }
- /// <summary>当前读取位置</summary>
- 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;
- }
- }
- }
|