/// <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; }
/// <summary> /// 封包,时间戳 + 协议号 + 内容长度 + 内容 /// </summary> /// <param name="protocol"></param> /// <param name="data"></param> /// <returns></returns> private ByteBuffer packet(int protocol, object data) { ByteBuffer buff = PackageUtil.clrObjectToByteBuffer(data as CLRSharp_Instance); Package package = new Package(); package.timeStamp = TimeUtil.getTimeStamp(); package.protocol = protocol; package.len = (ushort)buff.length; package.data = data; Debug.Log("[发送] " + package.toString()); ByteBuffer buffer = new ByteBuffer(); buffer.appendInt(package.timeStamp); buffer.appendInt(package.protocol); buffer.appendUshort(package.len); buffer.appendBuffer(buff); return buffer; }