using System; using System.Collections.Generic; using System.Text; using System.Linq; public class ProtocolByte : ProtocolBase { public ProtocolByte() { } //创建时指定消息标识字符串 public ProtocolByte(string protocolName) { AddString(protocolName); } public byte[] bytes; public override ProtocolBase Decode(byte[] readBuff, int start, int length) { ProtocolByte protocol = new ProtocolByte(); protocol.bytes = new byte[length]; Array.Copy(readBuff, start, protocol.bytes, 0, length); return protocol; } public override byte[] Encode() { return bytes; } //获取每个字节的编码 public override string GetDesc() { string str = ""; if (bytes == null) return str; for (int i = 0; i < bytes.Length; i++) { int b = (int)bytes[i]; str += b.ToString() + " "; } return str; } public override string GetProtocolName() { return GetString(0); } public void AddStruct(T obj) where T : struct { byte[] objBytes = BytesHelper.StructToBytes(obj); byte[] lenBytes = BitConverter.GetBytes(objBytes.Length); if (bytes == null) { bytes = lenBytes.Concat(objBytes).ToArray(); } else bytes = bytes.Concat(lenBytes).Concat(objBytes).ToArray(); } public T GetStruct(int start, ref int end) where T : struct { if (bytes == null) return default(T); int msgLen = BitConverter.ToInt32(bytes, start); if (bytes.Length < (start + sizeof(Int32) + msgLen)) return default(T); byte[] objBytes = new byte[msgLen]; Array.Copy(bytes, start + sizeof(Int32), objBytes, 0, msgLen); T obj = BytesHelper.BytesToStuct(objBytes); end = start + sizeof(Int32) + msgLen; return obj; } /// /// 添加的类中 尽量不要存在float 或限制float的精度后使用 否则转成字符串后占用空间较大 /// /// /// public void AddClass(T obj) where T : class { byte[] objBytes = BytesHelper.ClassToBytes(obj); byte[] lenBytes = BitConverter.GetBytes(objBytes.Length); if (bytes == null) { bytes = lenBytes.Concat(objBytes).ToArray(); } else bytes = bytes.Concat(lenBytes).Concat(objBytes).ToArray(); } public T GetClass(int start, ref int end) where T : class { if (bytes == null) return default(T); int msgLen = BitConverter.ToInt32(bytes, start); if (bytes.Length < (start + sizeof(Int32) + msgLen)) return default(T); byte[] objBytes = new byte[msgLen]; Array.Copy(bytes, start + sizeof(Int32), objBytes, 0, msgLen); T obj = BytesHelper.BytesToClass(objBytes); end = start + sizeof(Int32) + msgLen; return obj; } public void AddString(string str) { //byte[] lenBytes = BitConverter.GetBytes(str.Length); byte[] strBytes = System.Text.Encoding.UTF8.GetBytes(str); byte[] lenBytes = BitConverter.GetBytes(strBytes.Length); if (bytes == null) { bytes = lenBytes.Concat(strBytes).ToArray(); } else bytes = bytes.Concat(lenBytes).Concat(strBytes).ToArray(); } public string GetString(int start, ref int end) { if (bytes == null) return ""; int msgLen = BitConverter.ToInt32(bytes, start); if (bytes.Length < (start + sizeof(Int32) + msgLen)) return ""; string str = System.Text.Encoding.UTF8.GetString(bytes, start + sizeof(Int32), msgLen); end = start + sizeof(Int32) + msgLen; return str; } public string GetString(int start) { int end = 0; return GetString(start, ref end); } public void AddBool(bool value) { byte[] numbytes = BitConverter.GetBytes(value); if (bytes == null) bytes = numbytes; bytes = bytes.Concat(numbytes).ToArray(); } public bool GetBool(int start, ref int end) { if (bytes == null) return false; if (bytes.Length < sizeof(bool) + start) return false; bool value = BitConverter.ToBoolean(bytes, start); end = start + sizeof(bool); return value; } public void AddInt(int num) { byte[] numbytes = BitConverter.GetBytes(num); if (bytes == null) bytes = numbytes; bytes = bytes.Concat(numbytes).ToArray(); } public int GetInt(int start, ref int end) { if (bytes == null) return 0; if (bytes.Length < sizeof(Int32) + start) return 0; int num = BitConverter.ToInt32(bytes, start); end = start + sizeof(Int32); return num; } public int GetInt(int start) { int end = 0; return GetInt(start, ref end); } public void AddFloat(float num) { byte[] numbytes = BitConverter.GetBytes(num); if (bytes == null) bytes = numbytes; bytes = bytes.Concat(numbytes).ToArray(); } public float GetFloat(int start, ref int end) { if (bytes == null) return 0; if (bytes.Length < sizeof(float) + start) return 0; float num = BitConverter.ToSingle(bytes, start); end = start + sizeof(float); return num; } public int GetFloat(int start) { int end = 0; return GetInt(start, ref end); } }