using UnityEngine; using System.Collections; using System; using System.Text; using System.Collections.Generic; /** 通迅包基类,处理字节流转换 */ public class GameNetPackBasic { public ServerType socketType = ServerType.Main; //默认且唯一 /** 协议号 */ protected ushort _cmd = 0; /** 协议号引用 */ public ushort cmd { get { return _cmd; } } /** 信息转换后的字节包 */ protected byte[] _writedBytes; /** 用于发送的组合帧 */ protected byte[] _combineBytes; /** 信息转换后的字节包的引用 */ public byte[] dataBytes { get { return _writedBytes; } } /** 用于发送的组合帧的引用 */ public byte[] combineBytes { get { return _combineBytes; } } /** 字节写入位置索引 */ protected int _writeIndex = 0; /** 解析位置索引 */ protected int _readIndex = 2; /** 最终合并采用的协议,为0表示不调整 */ protected ushort combineCmd = 0; /** 字符编码机 */ public static Encoding StrEncoding = Encoding.UTF8; //自定义字段,用于后续更新用 private Dictionary fields; public void SetField(string key, object o) { if (fields == null) fields = new Dictionary(); fields[key] = o; } //获取自定义字段 public T GetField(string key) { if (fields == null) { Debug.LogErrorFormat("未设置自定义字段:{0}", key); return default(T); } if (fields.TryGetValue(key, out object o)) { if (o is T) return (T)o; else { Debug.LogErrorFormat("自定义字段:{0},不是类型:{1}", key, typeof(T)); return default(T); } } Debug.LogErrorFormat("不存在自定义字段:{0}", key); return default(T); } /** 子信息类型 */ public enum NetDataType { BYTE, WORD, DWORD, Chars, Int, Double } /** 字符串长度写入的方式 */ public enum StrWriteLen { BYTE, WORD, DWORD } /** 字节占用字节量 */ public static short ByteLeng = 1; /** 布尔值占用字节量 */ public static short BoolLeng = 1; /** 短整型占用字节量 */ public static short ShortLeng = 2; /** 整型占用字节量 */ public static short IntLeng = 4; /** 长整型占用字节量 */ public static short LongLeng = 8; /** 双浮点型占用字节量 */ public static short DoubleLeng = 8; /** 从字节包中解析信息 */ public virtual void ReadFromBytes(byte[] vBytes) { } /** 将信息写成字节包 */ public virtual void WriteToBytes() { } private string _vInfoCont = ""; public string vInfoCont { get { return _vInfoCont; } } /** 组合帧信息 */ public void CombineDatas(GameNetEncode encoder) { try { if (_writedBytes == null) { _writedBytes = new byte[0]; } byte[] vCmdAndSubCmd = new byte[2]; vCmdAndSubCmd[0] = (byte)((_cmd & 0xFF00) >> 8); vCmdAndSubCmd[1] = (byte)(_cmd & 0xFF); byte[] vCmdAndBody = new byte[_writedBytes.Length + vCmdAndSubCmd.Length]; // 协议与内容 //DesignDebug.NetLog("GameNetPackBasic vCmdAndBody:" + vCmdAndBody.Length); Array.Copy(vCmdAndSubCmd, 0, vCmdAndBody, 0, vCmdAndSubCmd.Length); if (_writedBytes.Length > 0) Array.Copy(_writedBytes, 0, vCmdAndBody, vCmdAndSubCmd.Length, _writedBytes.Length); if (combineCmd != 0) // 采用另外的协议号 { byte[] vExCmdAndBody = new byte[vCmdAndBody.Length + 4]; vExCmdAndBody[0] = (byte)((combineCmd & 0xFF00) >> 8); vExCmdAndBody[1] = (byte)(combineCmd & 0xFF); var _lengthBytes = BitConverter.GetBytes((ushort)vCmdAndBody.Length); vExCmdAndBody[2] = _lengthBytes[0]; vExCmdAndBody[3] = _lengthBytes[1]; Array.Copy(vCmdAndBody, 0, vExCmdAndBody, 4, vCmdAndBody.Length); vCmdAndBody = vExCmdAndBody; } vCmdAndBody = encoder.BaseXorAdd(vCmdAndBody); // 加密 _combineBytes = new byte[vCmdAndBody.Length + 6]; // 合并体 byte[] vFrameHead = new byte[] { 255, 204 }; byte[] vMsgBodyLength = BitConverter.GetBytes(vCmdAndBody.Length); Array.Copy(vFrameHead, 0, _combineBytes, 0, vFrameHead.Length); Array.Copy(vMsgBodyLength, 0, _combineBytes, 2, vMsgBodyLength.Length); Array.Copy(vCmdAndBody, 0, _combineBytes, 6, vCmdAndBody.Length); } catch (Exception ex) { Debug.LogFormat("封包信息组合异常 :{0}", ex); } } /** 转换字节段为布尔值 */ protected void TransBytes(out bool vTof, byte[] vBytes, NetDataType vNetDataType) { try { vTof = false; if (vNetDataType == NetDataType.BYTE) { if ((_readIndex + BoolLeng) <= vBytes.Length) { vTof = BitConverter.ToBoolean(vBytes, _readIndex); _readIndex += BoolLeng; } else { Debug.LogError("(line:130) 字节包数据量不足"); } } else { Debug.LogError("请指定正确的数据源类型"); } } catch (Exception ex) { Debug.LogFormat("转换字节段为布尔值异常:{0}", ex); vTof = false; } } /** 转换字节段为字节 */ protected void TransBytes(out byte vbyte, byte[] vBytes, NetDataType vNetDataType) { try { vbyte = 0; if (vNetDataType == NetDataType.BYTE) { if ((_readIndex + ByteLeng) <= vBytes.Length) { vbyte = vBytes[_readIndex]; _readIndex += ByteLeng; } else { Debug.LogError("(line:189) 字节包数据量不足"); } } else { Debug.LogError("请指定正确的数据源类型"); } } catch (Exception ex) { Debug.LogFormat("转换字节段为字节异常:{0}", ex); vbyte = 0; } } /** 转换字节段为短整型 */ protected void TransBytes(out short vShort, byte[] vBytes, NetDataType vNetDataType) { try { vShort = 0; if (vNetDataType == NetDataType.WORD) { if ((_readIndex + ShortLeng) <= vBytes.Length) { vShort = BitConverter.ToInt16(vBytes, _readIndex); _readIndex += ShortLeng; } else { Debug.LogError(" 字节包数据量不足"); } } else if (vNetDataType == NetDataType.BYTE) { if ((_readIndex + ByteLeng) <= vBytes.Length) { byte vTheByte = vBytes[_readIndex]; byte[] vTempBytes = new byte[2]; vTempBytes[0] = vTheByte; if (vTheByte > 127) vTempBytes[1] = 0xFF; vShort = BitConverter.ToInt16(vTempBytes, 0); // DesignDebug.NetLog("vShort:" + vShort); _readIndex += ByteLeng; } else { Debug.LogError(" 字节包数据量不足"); } } else { Debug.LogError("请指定正确的数据源类型"); } } catch (Exception ex) { Debug.LogFormat("转换字节段为短整型异常:{0}", ex); vShort = 0; } } /** 转换字节段为短整型 */ protected void TransBytes(out ushort vShort, byte[] vBytes, NetDataType vNetDataType) { try { vShort = 0; if (vNetDataType == NetDataType.WORD) { if ((_readIndex + ShortLeng) <= vBytes.Length) { vShort = BitConverter.ToUInt16(vBytes, _readIndex); _readIndex += ShortLeng; } else { Debug.LogError("字节包数据量不足"); } } else if (vNetDataType == NetDataType.BYTE) { if ((_readIndex + ByteLeng) <= vBytes.Length) { vShort = (ushort)vBytes[_readIndex]; _readIndex += ByteLeng; } else { Debug.LogError("字节包数据量不足"); } } else { Debug.LogError("请指定正确的数据源类型"); } } catch (Exception ex) { Debug.LogFormat("转换字节段为短整型异常:{0}", ex); vShort = 0; } } /** 转换字节段为整型 */ protected void TransBytes(out int vInt, byte[] vBytes, NetDataType vNetDataType) { try { vInt = 0; if (vNetDataType == NetDataType.DWORD || vNetDataType == NetDataType.Int) { if ((_readIndex + IntLeng) <= vBytes.Length) { vInt = BitConverter.ToInt32(vBytes, _readIndex); _readIndex += IntLeng; } else { Debug.LogError("字节包数据量不足"); } } else if (vNetDataType == NetDataType.BYTE) { vInt = (int)vBytes[_readIndex]; _readIndex += 1; } else { Debug.LogError("请指定正确的数据源类型"); } } catch (Exception ex) { Debug.LogFormat("转换字节段为整型异常:{0}", ex); vInt = 0; } } /** 转换字节段为整型 */ protected void TransBytes(out uint vInt, byte[] vBytes, NetDataType vNetDataType) { try { vInt = 0; if (vNetDataType == NetDataType.DWORD || vNetDataType == NetDataType.Int) { if ((_readIndex + IntLeng) <= vBytes.Length) { vInt = BitConverter.ToUInt32(vBytes, _readIndex); _readIndex += IntLeng; } else { Debug.LogError("(line:228) 字节包数据量不足"); } } else if (vNetDataType == NetDataType.WORD) { if ((_readIndex + ShortLeng) <= vBytes.Length) { vInt = (uint)BitConverter.ToUInt16(vBytes, _readIndex); _readIndex += ShortLeng; } else { Debug.LogError("(line:238) 字节包数据量不足"); } } else if (vNetDataType == NetDataType.BYTE) { vInt = (uint)vBytes[_readIndex]; _readIndex += 1; } else { Debug.LogError("请指定正确的数据源类型"); } } catch (Exception ex) { Debug.LogFormat("转换字节段为整型异常:{0}", ex); vInt = 0; } } protected void TransBytes(out byte[] bytes, byte[] vBytes, NetDataType vNetDataType, byte vLeng) { TransBytes(out bytes, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out byte[] bytes, byte[] vBytes, NetDataType vNetDataType, ushort vLeng) { TransBytes(out bytes, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out byte[] bytes, byte[] vBytes, NetDataType vNetDataType, short vLeng) { TransBytes(out bytes, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out byte[] bytes, byte[] vBytes, NetDataType vNetDataType, uint vLeng) { TransBytes(out bytes, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out byte[] bytes, byte[] vBytes, NetDataType vNetDataType, int vLeng) { try { bytes = new byte[vLeng]; if (vNetDataType == NetDataType.BYTE) { if ((_readIndex + ByteLeng) <= vBytes.Length) { for (int i = 0; i < vLeng; i++) { bytes[i] = vBytes[_readIndex]; _readIndex += ByteLeng; } } else { Debug.LogError("字节包数据量不足"); } } else { Debug.LogError("请指定正确的数据源类型"); } } catch (Exception ex) { Debug.LogFormat("转换字节段为字节数组异常:{0}", ex); bytes = null; } } protected void TransBytes(out ushort[] uShorts, byte[] vBytes, NetDataType vNetDataType, byte vLeng) { TransBytes(out uShorts, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out ushort[] uShorts, byte[] vBytes, NetDataType vNetDataType, short vLeng) { TransBytes(out uShorts, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out ushort[] uShorts, byte[] vBytes, NetDataType vNetDataType, ushort vLeng) { TransBytes(out uShorts, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out ushort[] uShorts, byte[] vBytes, NetDataType vNetDataType, uint vLeng) { TransBytes(out uShorts, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out ushort[] uShorts, byte[] vBytes, NetDataType vNetDataType, int vLeng) { try { uShorts = new ushort[vLeng]; if (vNetDataType == NetDataType.WORD) { if ((_readIndex + ShortLeng) <= vBytes.Length) { for (int i = 0; i < vLeng; i++) { uShorts[i] = BitConverter.ToUInt16(vBytes, _readIndex); _readIndex += ShortLeng; } } else { Debug.LogError("(line:179) 字节包数据量不足"); } } else if (vNetDataType == NetDataType.BYTE) { if ((_readIndex + ByteLeng) <= vBytes.Length) { for (int i = 0; i < vLeng; i++) { uShorts[i] = (ushort)vBytes[_readIndex]; _readIndex += ByteLeng; } } else { Debug.LogError("(line:189) 字节包数据量不足"); } } else { Debug.LogError("请指定正确的数据源类型"); } } catch (Exception ex) { Debug.LogFormat("转换字节段为短整型数组异常:{0}", ex); uShorts = null; } } /** 转换字节段为短整型数组 */ protected void TransBytes(out short[] vShorts, byte[] vBytes, NetDataType vNetDataType, byte vLeng) { TransBytes(out vShorts, vBytes, vNetDataType, (int)vLeng); } /** 转换字节段为短整型数组 */ protected void TransBytes(out short[] vShorts, byte[] vBytes, NetDataType vNetDataType, short vLeng) { TransBytes(out vShorts, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out short[] vShorts, byte[] vBytes, NetDataType vNetDataType, ushort vLeng) { TransBytes(out vShorts, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out short[] vShorts, byte[] vBytes, NetDataType vNetDataType, uint vLeng) { TransBytes(out vShorts, vBytes, vNetDataType, (int)vLeng); } /** 转换字节段为短整型数组 */ protected void TransBytes(out short[] vShorts, byte[] vBytes, NetDataType vNetDataType, int vLeng) { try { vShorts = new short[vLeng]; if (vLeng > 0) { if (vNetDataType == NetDataType.WORD) { for (int i = 0; i < vLeng; i++) { vShorts[i] = (short)BitConverter.ToInt16(vBytes, _readIndex); _readIndex += ShortLeng; } } else if (vNetDataType == NetDataType.BYTE) { byte vTheByte; byte[] vTempBytes; for (int i = 0; i < vLeng; i++) { vTheByte = vBytes[i]; vTempBytes = new byte[2]; vTempBytes[0] = vTheByte; if (vTheByte > 127) vTempBytes[1] = 0xFF; vShorts[i] = BitConverter.ToInt16(vTempBytes, 0); _readIndex += ByteLeng; } } else { Debug.LogError("请指定正确的数据源类型"); } } } catch (Exception ex) { Debug.LogFormat("转换字节段为短整型数组异常:{0}", ex); vShorts = null; } } /** 转换字节段为整型数组 */ protected void TransBytes(out int[] vInts, byte[] vBytes, NetDataType vNetDataType, byte vLeng) { TransBytes(out vInts, vBytes, vNetDataType, (int)vLeng); } /** 转换字节段为整型数组 */ protected void TransBytes(out int[] vInts, byte[] vBytes, NetDataType vNetDataType, short vLeng) { TransBytes(out vInts, vBytes, vNetDataType, (int)vLeng); } /** 转换字节段为整型数组 */ protected void TransBytes(out int[] vInts, byte[] vBytes, NetDataType vNetDataType, ushort vLeng) { TransBytes(out vInts, vBytes, vNetDataType, (int)vLeng); } /** 转换字节段为整型数组 */ protected void TransBytes(out int[] vInts, byte[] vBytes, NetDataType vNetDataType, uint vLeng) { TransBytes(out vInts, vBytes, vNetDataType, (int)vLeng); } /** 转换字节段为整型数组 */ protected void TransBytes(out int[] vInts, byte[] vBytes, NetDataType vNetDataType, int vLeng) { try { vInts = new int[vLeng]; if (vLeng > 0) { if (vNetDataType == NetDataType.DWORD || vNetDataType == NetDataType.Int) { for (int i = 0; i < vLeng; i++) { vInts[i] = (int)BitConverter.ToInt32(vBytes, _readIndex); _readIndex += IntLeng; } } else if (vNetDataType == NetDataType.WORD) { for (int i = 0; i < vLeng; i++) { vInts[i] = (int)BitConverter.ToInt16(vBytes, _readIndex); _readIndex += ShortLeng; } } else if (vNetDataType == NetDataType.BYTE) { for (int i = 0; i < vLeng; i++) { vInts[i] = (int)vBytes[i]; _readIndex += ByteLeng; } } else { Debug.LogError("请指定正确的数据源类型"); } } } catch (Exception ex) { Debug.LogFormat("转换字节段为整型数组异常:{0}", ex); vInts = null; } } protected void TransBytes(out uint[] uInts, byte[] vBytes, NetDataType vNetDataType, byte vLeng) { TransBytes(out uInts, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out uint[] uInts, byte[] vBytes, NetDataType vNetDataType, short vLeng) { TransBytes(out uInts, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out uint[] uInts, byte[] vBytes, NetDataType vNetDataType, ushort vLeng) { TransBytes(out uInts, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out uint[] uInts, byte[] vBytes, NetDataType vNetDataType, uint vLeng) { TransBytes(out uInts, vBytes, vNetDataType, (int)vLeng); } protected void TransBytes(out uint[] uInts, byte[] vBytes, NetDataType vNetDataType, int vLeng) { try { uInts = new uint[vLeng]; if (vLeng > 0) { if (vNetDataType == NetDataType.DWORD || vNetDataType == NetDataType.Int) { for (int i = 0; i < vLeng; i++) { uInts[i] = (uint)BitConverter.ToUInt32(vBytes, _readIndex); _readIndex += IntLeng; } } else if (vNetDataType == NetDataType.WORD) { for (int i = 0; i < vLeng; i++) { uInts[i] = (uint)BitConverter.ToUInt16(vBytes, _readIndex); _readIndex += ShortLeng; } } else if (vNetDataType == NetDataType.BYTE) { for (int i = 0; i < vLeng; i++) { uInts[i] = (uint)vBytes[i]; _readIndex += ByteLeng; } } else { Debug.LogError("请指定正确的数据源类型"); } } } catch (Exception ex) { Debug.LogFormat("转换字节段为无符号整型数组异常:{0}", ex); uInts = null; } } /** 转换字节段为Double型数据 */ protected void TransBytes(out double vDouble, byte[] vBytes, NetDataType vNetDataType) { try { vDouble = 0; if (vNetDataType == NetDataType.Double) { if ((_readIndex + DoubleLeng) <= vBytes.Length) { vDouble = BitConverter.ToDouble(vBytes, _readIndex); _readIndex += DoubleLeng; } else { Debug.LogError("(line:341) 字节包数据量不足"); } } else { Debug.LogError("请指定正确的数据源类型"); } } catch (Exception ex) { Debug.LogFormat("转换字节段为Double型数据异常:{0}", ex); vDouble = 0; } } protected void TransBytes(out string vStr, byte[] vBytes, NetDataType vNetDataType, byte vLeng) { TransBytes(out vStr, vBytes, vNetDataType, (int)vLeng); } /** 转换字节段为字符串 */ protected void TransBytes(out string vStr, byte[] vBytes, NetDataType vNetDataType, ushort vLeng) { TransBytes(out vStr, vBytes, vNetDataType, (int)vLeng); } /** 转换字节段为字符串 */ protected void TransBytes(out string vStr, byte[] vBytes, NetDataType vNetDataType, uint vLeng) { TransBytes(out vStr, vBytes, vNetDataType, (int)vLeng); } /** 转换字节段为字符串 */ protected void TransBytes(out string vStr, byte[] vBytes, NetDataType vNetDataType, int vLeng) { try { vStr = ""; if (vLeng == 0) return; if (vNetDataType == NetDataType.Chars) { if ((_readIndex + vLeng) <= vBytes.Length) { vStr = StrEncoding.GetString(vBytes, _readIndex, vLeng); _readIndex += vLeng; } else { Debug.LogErrorFormat("(line:365) 字节包数据量不足: _readIndex {0}, vLeng {1}, vBytes.Length {2}", _readIndex, vLeng, vBytes.Length); } } else { Debug.LogError("请指定正确的数据源类型"); } } catch (Exception ex) { Debug.LogFormat("转换字节段为字符串异常:{0}", ex); vStr = string.Empty; } } /** 转换布尔值为字节 */ protected void WriteBytes(bool vTof, NetDataType vType) { this.WriteBytes(BitConverter.GetBytes(vTof)); } /** 转换短整型为字节 */ protected void WriteBytes(short vShort, NetDataType vType) { this.WriteBytes((int)vShort, vType); } /** 以固定的字节长度转短整型数值数组为字节 */ protected void WriteBytes(short[] vShorts, NetDataType vType, int vLockLeng) { this.WriteBytes(ShortsToInts(vShorts), vType, vLockLeng); } /** 以固定的字节长度转短整型数值数组为字节 */ protected void WriteBytes(ushort[] uShorts, NetDataType vType, int vLockLeng) { this.WriteBytes(UShortsToInts(uShorts), vType, vLockLeng); } /** 以固定的字节长度转字节数组为字节 */ protected void WriteBytes(byte[] bytes, NetDataType vType, int vLockLeng) { this.WriteBytes(BytesToInts(bytes), vType, vLockLeng); } /** 转短整型数值数组为字节 */ protected void WriteBytes(short[] vShorts, NetDataType vType, StrWriteLen vWriteLen) { this.WriteBytes(ShortsToInts(vShorts), vType, vWriteLen); } /** 无符号短整数组转整型数组 */ private int[] UShortsToInts(ushort[] uShorts) { try { int l = uShorts.Length; int[] vInts = new int[l]; int i; for (i = 0; i < l; i++) vInts[i] = (int)uShorts[i]; return vInts; } catch (Exception ex) { Debug.Log(ex); return null; } } /** 短整数组转整型数组 */ private int[] ShortsToInts(short[] vShorts) { try { int l = vShorts.Length; int[] vInts = new int[l]; int i; for (i = 0; i < l; i++) vInts[i] = (int)vShorts[i]; return vInts; } catch (Exception ex) { Debug.Log(ex); return null; } } /** 字节数组转整型数组 */ private int[] BytesToInts(byte[] bytes) { try { if (bytes != null) { int l = bytes.Length; int[] vInts = new int[l]; int i; for (i = 0; i < l; i++) vInts[i] = (int)bytes[i]; return vInts; } else { int[] vInts = new int[0]; return vInts; } } catch (Exception ex) { Debug.Log(ex); return null; } } /** 转换整型为字节 */ protected void WriteBytes(int vInt, NetDataType vType) { try { byte[] vRst; if (vType == NetDataType.WORD) vRst = BitConverter.GetBytes((short)vInt); else if (vType == NetDataType.BYTE) { vRst = new byte[1]; vRst[0] = (byte)(vInt & 0xFF); // BitConverter.GetBytes ((byte)vInt); } else vRst = BitConverter.GetBytes((int)vInt); this.WriteBytes(vRst); } catch (Exception ex) { Debug.Log(ex); } } protected void WriteBytes(uint[] vInts, NetDataType vType, int vLockLeng) { try { if (vInts.Length != vLockLeng) Array.Resize(ref vInts, vLockLeng); this.WriteBytes(vInts, vType); } catch (Exception ex) { Debug.Log(ex); } } /** 约束数组长度并转为字节段,不写入长度 */ protected void WriteBytes(int[] vInts, NetDataType vType, int vLockLeng) { try { if (vInts.Length != vLockLeng) Array.Resize(ref vInts, vLockLeng); this.WriteBytes(vInts, vType); } catch (Exception ex) { Debug.Log(ex); } } /** 整型数组转为字节段,以指定的数据类型写入长度 */ protected void WriteBytes(int[] vInts, NetDataType vType, StrWriteLen vWriteLen) { try { if (vWriteLen == StrWriteLen.BYTE) this.WriteBytes(new Byte[] { (byte)vInts.Length }); else if (vWriteLen == StrWriteLen.WORD) this.WriteBytes(BitConverter.GetBytes((short)vInts.Length)); else this.WriteBytes(BitConverter.GetBytes((int)vInts.Length)); this.WriteBytes(vInts, vType); } catch (Exception ex) { Debug.Log(ex); } } /** 转整型数值数组为字节 */ private void WriteBytes(uint[] vInts, NetDataType vType) { try { int l = vInts.Length; byte[] vRst; int i; int vBytesLen; if (vType == NetDataType.BYTE) { vBytesLen = l; vRst = new byte[vBytesLen]; for (i = 0; i < l; i++) Array.Copy(BitConverter.GetBytes((byte)vInts[i]), 0, vRst, i, 1); } else if (vType == NetDataType.WORD) { vBytesLen = l * 2; vRst = new byte[vBytesLen]; for (i = 0; i < l; i++) Array.Copy(BitConverter.GetBytes((short)vInts[i]), 0, vRst, i * 2, 2); } else { vBytesLen = l * 4; vRst = new byte[vBytesLen]; for (i = 0; i < l; i++) Array.Copy(BitConverter.GetBytes((int)vInts[i]), 0, vRst, i * 4, 4); } this.WriteBytes(vRst); } catch (Exception ex) { Debug.Log(ex); } } /** 转整型数值数组为字节 */ private void WriteBytes(int[] vInts, NetDataType vType) { try { int l = vInts.Length; byte[] vRst; int i; int vBytesLen; if (vType == NetDataType.BYTE) { vBytesLen = l; vRst = new byte[vBytesLen]; for (i = 0; i < l; i++) Array.Copy(BitConverter.GetBytes((byte)vInts[i]), 0, vRst, i, 1); } else if (vType == NetDataType.WORD) { vBytesLen = l * 2; vRst = new byte[vBytesLen]; for (i = 0; i < l; i++) Array.Copy(BitConverter.GetBytes((short)vInts[i]), 0, vRst, i * 2, 2); } else { vBytesLen = l * 4; vRst = new byte[vBytesLen]; for (i = 0; i < l; i++) Array.Copy(BitConverter.GetBytes((int)vInts[i]), 0, vRst, i * 4, 4); } this.WriteBytes(vRst); } catch (Exception ex) { Debug.Log(ex); } } /** 转换双精度数值为字节段 */ protected void WriteBytes(uint vuint, NetDataType vType) { try { byte[] vRst; vRst = BitConverter.GetBytes(vuint); this.WriteBytes(vRst); } catch (Exception ex) { Debug.Log(ex); } } /** 转换字符串为字节段,byte量定长型,不写入长度 */ protected void WriteBytes(string vStr, NetDataType vType, int vShortLen) { if (vStr == null) { vStr = string.Empty; } try { byte[] vRst = StrEncoding.GetBytes(vStr); if (vRst.Length != vShortLen) Array.Resize(ref vRst, vShortLen); this.WriteBytes(vRst); } catch (Exception ex) { Debug.Log(ex); } } /** 转换字符串为字节段,动长型,前置写入长度 */ protected void WriteBytes(string vStr, NetDataType vType, StrWriteLen vWriteLen) { try { byte[] vRst = StrEncoding.GetBytes(vStr); if (vWriteLen == StrWriteLen.BYTE) this.WriteBytes(new Byte[] { (byte)vRst.Length }); else if (vWriteLen == StrWriteLen.WORD) this.WriteBytes(BitConverter.GetBytes((short)vRst.Length)); else this.WriteBytes(BitConverter.GetBytes((int)vRst.Length)); this.WriteBytes(vRst); } catch (Exception ex) { Debug.Log(ex); } } /** 写入一段字节数据 */ private void WriteBytes(byte[] vBytes) { try { Array.Resize(ref _writedBytes, _writeIndex + vBytes.Length); vBytes.CopyTo(_writedBytes, _writeIndex); _writeIndex = _writedBytes.Length; } catch (Exception ex) { Debug.Log(ex); } } }