private static void append(object field, ref ByteBuffer buff) { Type type = field.GetType(); switch (type.FullName) { case "System.Int16": buff.appendShort((short)field); break; case "System.Int32": buff.appendInt((int)field); break; case "System.Int64": buff.appendLong((long)field); break; case "System.UInt16": buff.appendUshort((ushort)field); break; case "System.UInt32": buff.appendUint((uint)field); break; //case "System.UInt64": // buff.appendUlong((ulong)field); // break; case "System.Byte": buff.appendByte((byte)field); break; case "System.Boolean": buff.appendBool((bool)field); break; case "System.Single": buff.appendFloat((float)field); break; case "System.Double": buff.appendDouble((double)field); break; case "System.String": buff.appendString((string)field); break; default: throw new Exception("协议包含不可解析类型:" + type.FullName); } }
/// <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; }