public static string ReadUTF8(this IReadableBuffer br) { var dat_len = br.ReadUnsignedShort(); if (dat_len > 0) { using (var temp = ByteBuffer.Pool.Get()) { temp.Write(dat_len, br, (data, offset, len, param) => param.Read(data, offset, len)); return(CodingUtil.EncodeUTF8(temp.data, 0, dat_len)); } } return(null); }
public static void WriteUTF8(this IWritableBuffer bw, string str) { if (str != null) { using (var temp = ByteBuffer.Pool.Get()) { CodingUtil.DecodeUTF8(str, temp); bw.WriteUnsignedShort(temp.Length); bw.Write(temp.data, 0, temp.Length); } } else { bw.WriteShort(0); } }
public static void Encode(Packet p, ByteBuffer bw) { if (p.msgType == MessageType.UNKNOW) { Logger.Error("Packet is not initialized or has been recycled!"); return; } var msgType = p.msgType; var msgFlag = Flag.NONE; var msgRoute = p.msgRoute; var msgData = p.msgData; ByteBuffer msgCompressData = null; var packetSize = 0; // -- 3. message token if (p.msgAttach != null) { msgFlag |= Flag.ATTACH; packetSize += CodingUtil.GetByteCountUTF8(p.msgAttach) + 2; } // -- 4. message route var msgRouteCode = Protocol.Instance.GetRouteCode(msgRoute); if (msgRouteCode != 0) { msgFlag |= Flag.ENCODE_ROUTE; packetSize += 2; } else { packetSize += CodingUtil.GetByteCountUTF8(msgRoute) + 2; } // -- 5. message request id if (msgType == MessageType.REQUEST || msgType == MessageType.RESPONSE) { packetSize += 4; } // -- 6. message body if (compressor != null && msgData.Length > 256) { msgFlag |= Flag.COMPRESS; msgCompressData = ByteBuffer.Pool.Get(); //TODO 处理压缩数据 //TODO END packetSize += msgCompressData.Length; } else { packetSize += msgData.Length; } // -- END var packetHead = (byte)( ((byte)msgType) | ((byte)msgFlag << 4) ); // -- // -- 1. packet head bw.WriteByte(packetHead); // -- 2. packet size bw.WriteInt24(packetSize); // -- 3. message token if (p.msgAttach != null) { bw.WriteUTF8(p.msgAttach); } // -- 4. message route if (msgRouteCode != 0) { bw.WriteUnsignedShort(msgRouteCode); } else { bw.WriteUTF8(msgRoute); } // -- 5. message request id if (msgType == MessageType.REQUEST || msgType == MessageType.RESPONSE) { bw.WriteInt(p.msgRequestId); } // -- 6. message body if (msgCompressData != null) { bw.Write(msgCompressData.data, 0, msgCompressData.Length); ((IDisposable)msgCompressData).Dispose(); msgCompressData = null; } else { bw.Write(msgData.data, 0, msgData.Length); } }
public static float ReadFloat16(this IReadableBuffer br) { var v = (short)br.ReadInt16(); return(CodingUtil.DecodeFloat16(v)); }
public static void WriteFloat16(this IWritableBuffer bw, float val) { var i_v = CodingUtil.EncodeFloat16(val); bw.WriteShort(i_v); }
public static float ReadFloat(this IReadableBuffer br) { var value = br.ReadInt32(); return(CodingUtil.DecodeFloat32(value)); }