private static object getArray(ref ByteBuffer buff, string itemTypeName) { //数组长度 int len = buff.removeUshort(); //C#类型 Type itemType = Type.GetType(itemTypeName, false); if (itemType != null) { object[] array = new object[len]; for (int i = 0; i < len; i++) { array[i] = getValue(ref buff, itemType); } return array; } //L#类型 ICLRType clrType = CLRSharpManager.instance.getCLRType(itemTypeName); if (clrType != null) { object[] array = new object[len]; for (int i = 0; i < len; i++) { array[i] = byteBufferToClrObject(ref buff, clrType); } return array; } throw new Exception("协议包含不可解析类型:" + itemTypeName + "[]"); }
private static object getValue(ref ByteBuffer buff, Type type) { switch (type.FullName) { case "System.Int16": return buff.removeShort(); case "System.Int32": return buff.removeInt(); case "System.Int64": return buff.removeLong(); case "System.UInt16": return buff.removeUshort(); case "System.UInt32": return buff.removeUint(); //case "System.UInt64": // return buff.removeUlong(); case "System.Byte": return buff.removeByte(); case "System.Boolean": return buff.removeBool(); case "System.Single": return buff.removeFloat(); case "System.Double": return buff.removeDouble(); case "System.String": return buff.removeString(); default: throw new Exception("协议包含不可解析类型:" + type.FullName); } }
/// <summary> /// 拆包,时间戳 + 协议号 + 内容长度 + 内容 /// </summary> /// <param name="buff"></param> /// <returns></returns> private Package unpack(ByteBuffer buff) { Package package = new Package(); package.timeStamp = buff.removeInt(); package.protocol = buff.removeInt(); package.len = buff.removeUshort(); if (buff.length < package.len) { throw new Exception("协议" + package.protocol + "包体字节长度不对,应是" + package.len + ",但当前是" + buff.length); } if (listenDic.ContainsKey(package.protocol)) { package.data = PackageUtil.byteBufferToClrObject(ref buff, listenDic[package.protocol].clrType); Debug.Log("[接收] " + package.toString()); } else { Debug.Log("[接收,未处理] " + package.toString()); } return package; }