@Test public void addValueOverloads() { byte[] value1 = new byte[64]; Arrays.fill(value1, (byte) 'b'); ByteBuf value2Buf = ByteBufAllocator.DEFAULT.buffer(14); byte[] value2 = "This is a test".getBytes(); value2Buf.writeBytes(value2); RtMessage value3Msg = RtMessage.builder().add(RtTag.PAD, new byte[12]).build(); ByteBuf value3Buf = RtWire.toWire(value3Msg); byte[] value3 = new byte[value3Buf.readableBytes()]; value3Buf.readBytes(value3); RtMessage msg = RtMessage.builder() .add(RtTag.INDX, value1) .add(RtTag.MAXT, value2Buf) .add(RtTag.NONC, value3Msg) .build(); assertArrayEquals(msg.get(RtTag.INDX), value1); assertArrayEquals(msg.get(RtTag.MAXT), value2); assertArrayEquals(msg.get(RtTag.NONC), value3); }
protected void writeInContext() { CompositeByteBuf cbb = ByteBufAllocator.DEFAULT.compositeBuffer(); for (;;) { ByteBuf buf = writeQueue.poll(); if (buf == null) { break; } writeQueueSize.decrementAndGet(); cbb.addComponent(true, buf); if (cbb.numComponents() == cbb.maxNumComponents()) { netSocket.write(Buffer.buffer(cbb)); cbb = ByteBufAllocator.DEFAULT.compositeBuffer(); } } if (cbb.isReadable()) { netSocket.write(Buffer.buffer(cbb)); } }
@Override public List<Object> encode(ByteBufAllocator alloc) { ByteBuf meta = metadata(alloc); ByteBuf head = alloc.buffer(FastdfsConstants.FDFS_HEAD_LEN); head.writeLong(meta.readableBytes() + size); head.writeByte(cmd()); head.writeByte(FastdfsConstants.ERRNO_OK); CompositeByteBuf cbb = alloc.compositeBuffer(); cbb.addComponents(head, meta); cbb.writerIndex(head.readableBytes() + meta.readableBytes()); List<Object> requests = new LinkedList<>(); requests.add(cbb); requests.add(content); return requests; }
@Override protected void encode(ChannelHandlerContext channelHandlerContext, Proto proto, List<Object> list) throws Exception { ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(); if (proto.getBody() != null) { byteBuf.writeInt(Proto.HEADER_LENGTH + proto.getBody().length); byteBuf.writeShort(Proto.HEADER_LENGTH); byteBuf.writeShort(Proto.VERSION); byteBuf.writeInt(proto.getOperation()); byteBuf.writeInt(proto.getSeqId()); byteBuf.writeBytes(proto.getBody()); } else { byteBuf.writeInt(Proto.HEADER_LENGTH); byteBuf.writeShort(Proto.HEADER_LENGTH); byteBuf.writeShort(Proto.VERSION); byteBuf.writeInt(proto.getOperation()); byteBuf.writeInt(proto.getSeqId()); } list.add(byteBuf); logger.debug("encode: {}", proto); }
@Override protected void encode(ChannelHandlerContext ctx, Proto proto, List<Object> list) throws Exception { ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(); if (proto.getBody() != null) { byteBuf.writeInt(Proto.HEADER_LENGTH + proto.getBody().length); byteBuf.writeShort(Proto.HEADER_LENGTH); byteBuf.writeShort(Proto.VERSION); byteBuf.writeInt(proto.getOperation()); byteBuf.writeInt(proto.getSeqId()); byteBuf.writeBytes(proto.getBody()); } else { byteBuf.writeInt(Proto.HEADER_LENGTH); byteBuf.writeShort(Proto.HEADER_LENGTH); byteBuf.writeShort(Proto.VERSION); byteBuf.writeInt(proto.getOperation()); byteBuf.writeInt(proto.getSeqId()); } list.add(new BinaryWebSocketFrame(byteBuf)); logger.debug("encode: {}", proto); }
public static String hashToBase64(ByteBuf objectState) { ByteBuffer bf = objectState.internalNioBuffer(objectState.readerIndex(), objectState.readableBytes()); long h1 = LongHashFunction.farmUo().hashBytes(bf); long h2 = LongHashFunction.xx().hashBytes(bf); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE); try { buf.writeLong(h1).writeLong(h2); ByteBuf b = Base64.encode(buf); try { String s = b.toString(CharsetUtil.UTF_8); return s.substring(0, s.length() - 2); } finally { b.release(); } } finally { buf.release(); } }
@Override public Object decode(ByteBuf buf, State state) throws IOException { int decompressSize = buf.readInt(); ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize); try { LZ4SafeDecompressor decompressor = factory.safeDecompressor(); ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes()); int pos = outBuffer.position(); decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer); int compressedLength = outBuffer.position() - pos; out.writerIndex(compressedLength); return innerCodec.getValueDecoder().decode(out, state); } finally { out.release(); } }
@Override public ByteBuf encode(Object in) throws IOException { Kryo kryo = null; ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { ByteBufOutputStream baos = new ByteBufOutputStream(out); Output output = new Output(baos); kryo = kryoPool.get(); kryo.writeClassAndObject(output, in); output.close(); return baos.buffer(); } catch (Exception e) { out.release(); if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RedissonKryoCodecException(e); } finally { if (kryo != null) { kryoPool.yield(kryo); } } }
static ByteBuf encodeRequest(TProtocolFactory protocolFactory, ByteBufAllocator allocator, int sequenceId, MethodMetadata method, List<Object> parameters) throws Exception { TChannelBufferOutputTransport transport = new TChannelBufferOutputTransport(allocator.buffer(1024)); TProtocolWriter protocol = protocolFactory.getProtocol(transport); // Note that though setting message type to ONEWAY can be helpful when looking at packet // captures, some clients always send CALL and so servers are forced to rely on the "oneway" // attribute on thrift method in the interface definition, rather than checking the message // type. protocol.writeMessageBegin(new TMessage(method.getName(), method.isOneway() ? ONEWAY : CALL, sequenceId)); // write the parameters ProtocolWriter writer = new ProtocolWriter(protocol); writer.writeStructBegin(method.getName() + "_args"); for (int i = 0; i < parameters.size(); i++) { Object value = parameters.get(i); ParameterMetadata parameter = method.getParameters().get(i); writer.writeField(parameter.getName(), parameter.getId(), parameter.getCodec(), value); } writer.writeStructEnd(); protocol.writeMessageEnd(); return transport.getOutputBuffer(); }
private void writeMessage(Packet packet) { ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(64); Codecs codecs = connectivityManager.getCodecRegistrar().getCodecs(); buf.writeByte(codecs.getId(packet)); if (packet instanceof SubClientPacket) { SubClientPacket subClientPacket = (SubClientPacket) packet; buf.writeByte(subClientPacket.getSenderSubClientId()); buf.writeByte(subClientPacket.getTargetSubClientId()); } Codec<? extends Packet> codec = connectivityManager.getCodecRegistrar().getCodecs().getCodec(packet.getClass()); codec.encode(packet, buf); ByteBufUtils.writeUnsignedVarInt(out, buf.readableBytes()); out.writeBytes(buf); }
@Test public void encode() { // Given PlayStatePacket expected = createDummyPlayStatePacket(); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(); buf.markReaderIndex(); // When subject.encode(expected, buf); // Then PlayStatePacket actual = subject.decode(buf); assertThat(actual).isEqualTo(expected); Truth.assertThat(actual.getPlayState()).isEqualTo(expected.getPlayState()); }
@Test public void encode() { // Given LoginPacket expected = createDummyLoginPacket(); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(); // When subject.encode(expected, buf); // Then LoginPacket actual = subject.decode(buf); assertThat(actual).isEqualTo(expected); assertThat(actual.getClientNetworkVersion()).isEqualTo(expected.getClientNetworkVersion()); assertThat(actual.getConnectionInfo()).isEqualTo(expected.getConnectionInfo()); }
static ByteBuf preEncode(Object msg, Codec<Object> codec, ByteBufAllocator allocator) throws CodecException { ByteBuf buf = null; try { buf = allocator.buffer(); ByteBufDataWriter writer = new ByteBufDataWriter(buf); doEncode(msg, writer, codec); return buf; } catch (CodecException e) { if (buf != null) { buf.release(); } throw e; } }
private ByteBuf getEncodedTargetAddress(ByteBufAllocator allocator, boolean resolve) throws ProxyConnectException { InetSocketAddress remoteAddress = destinationAddress(); SocksAddressType remoteAddressType; String remoteHost; if (!resolve || remoteAddress.isUnresolved()) { remoteAddressType = SocksAddressType.DOMAIN; remoteHost = remoteAddress.getHostString(); } else { remoteHost = remoteAddress.getAddress().getHostAddress(); if (NetUtil.isValidIpV4Address(remoteHost)) { remoteAddressType = SocksAddressType.IPv4; } else if (NetUtil.isValidIpV6Address(remoteHost)) { remoteAddressType = SocksAddressType.IPv6; } else { throw new ProxyConnectException("unknown address type: " + StringUtil.simpleClassName(remoteHost)); } } int remotePort = remoteAddress.getPort(); SocksCmdRequest request = new SocksCmdRequest(SocksCmdType.UNKNOWN, remoteAddressType, remoteHost, remotePort); return SSocksAddressEncoder.INSTANCE.encode(allocator, request); }
public ByteBuf encode(ByteBufAllocator allocator, SocksCmdRequest msg) { if (LOG.isTraceEnabled()) { LOG.trace("encode target address"); } ByteBuf buf = allocator.directBuffer(); msg.encodeAsByteBuf(buf); buf.skipBytes(3); if (LOG.isTraceEnabled()) { byte[] bytes = new byte[buf.readableBytes()]; buf.getBytes(buf.readerIndex(), bytes); } return buf; }
private static ByteBuf encodeMessageWithOnlySingleByteFixedHeaderAndPacketId( ByteBufAllocator byteBufAllocator, MqttMessage message) { MqttFixedHeader mqttFixedHeader = message.fixedHeader(); MqttPacketIdVariableHeader variableHeader = (MqttPacketIdVariableHeader) message.variableHeader(); int msgId = variableHeader.packetId(); int variableHeaderBufferSize = 2; // variable part only has a message id int fixedHeaderBufferSize = 1 + getVariableLengthInt(variableHeaderBufferSize); ByteBuf buf = byteBufAllocator.buffer(fixedHeaderBufferSize + variableHeaderBufferSize); buf.writeByte(getFixedHeaderByte1(mqttFixedHeader)); writeVariableLengthInt(buf, variableHeaderBufferSize); buf.writeShort(msgId); return buf; }
private static ByteBuf encodeSubAckMessage( ByteBufAllocator byteBufAllocator, MqttSubAckMessage message) { int variableHeaderBufferSize = 2; int payloadBufferSize = message.payload().grantedQoSLevels().size(); int variablePartSize = variableHeaderBufferSize + payloadBufferSize; int fixedHeaderBufferSize = 1 + getVariableLengthInt(variablePartSize); ByteBuf buf = byteBufAllocator.buffer(fixedHeaderBufferSize + variablePartSize); buf.writeByte(getFixedHeaderByte1(message.fixedHeader())); writeVariableLengthInt(buf, variablePartSize); buf.writeShort(message.variableHeader().messageId()); for (int qos : message.payload().grantedQoSLevels()) { buf.writeByte(qos); } return buf; }
private static ByteBuf encodeMessageWithOnlySingleByteFixedHeaderAndMessageId( ByteBufAllocator byteBufAllocator, MqttMessage message) { MqttFixedHeader mqttFixedHeader = message.fixedHeader(); MqttMessageIdVariableHeader variableHeader = (MqttMessageIdVariableHeader) message.variableHeader(); int msgId = variableHeader.messageId(); int variableHeaderBufferSize = 2; // variable part only has a message id int fixedHeaderBufferSize = 1 + getVariableLengthInt(variableHeaderBufferSize); ByteBuf buf = byteBufAllocator.buffer(fixedHeaderBufferSize + variableHeaderBufferSize); buf.writeByte(getFixedHeaderByte1(mqttFixedHeader)); writeVariableLengthInt(buf, variableHeaderBufferSize); buf.writeShort(msgId); return buf; }
@Override public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception { ByteBuf chunk = super.readChunk(allocator); if (chunk == null) { return null; } byte[] prevChunkTrailingBytes = new byte[2]; prevChunkTrailingBytes[0] = trailingBytes[0]; prevChunkTrailingBytes[1] = trailingBytes[1]; updateTrailingBytes(chunk); boolean appendCRLF = isEndOfInput() && !(trailingBytes[0] == CR && trailingBytes[1] == LF); return DotStuffing.createDotStuffedBuffer(allocator, chunk, prevChunkTrailingBytes, appendCRLF ? MessageTermination.ADD_CRLF : MessageTermination.DO_NOT_TERMINATE); }
@Override public List<Object> encode(ByteBufAllocator alloc) { byte[] pathBytes = fileId.pathBytes(); int length = 2 * FDFS_LONG_LEN + FDFS_GROUP_LEN + pathBytes.length; byte cmd = FastdfsConstants.Commands.FILE_DOWNLOAD; ByteBuf buf = alloc.buffer(length + FDFS_HEAD_LEN); buf.writeLong(length); buf.writeByte(cmd); buf.writeByte(ERRNO_OK); buf.writeLong(offset); buf.writeLong(size); writeFixLength(buf, fileId.group(), FDFS_GROUP_LEN); ByteBufUtil.writeUtf8(buf, fileId.path()); return Collections.singletonList(buf); }
@Override public List<Object> encode(ByteBufAllocator alloc) { ByteBuf meta = metadata(alloc); ByteBuf head = alloc.buffer(FDFS_HEAD_LEN); head.writeLong(meta.readableBytes() + size); head.writeByte(cmd()); head.writeByte(ERRNO_OK); CompositeByteBuf cbb = alloc.compositeBuffer(); cbb.addComponents(head, meta); cbb.writerIndex(head.readableBytes() + meta.readableBytes()); List<Object> requests = new LinkedList<>(); requests.add(cbb); requests.add(content); return requests; }
@Override protected ByteBuf metadata(ByteBufAllocator alloc) { int metaLen = FDFS_STORE_PATH_INDEX_LEN + FDFS_PROTO_PKG_LEN_SIZE + FDFS_FILE_EXT_LEN; ByteBuf buf = alloc.buffer(metaLen); buf.writeByte(pathIdx); buf.writeLong(size()); writeFixLength(buf, ext, FDFS_FILE_EXT_LEN); return buf; }
@Override public List<Object> encode(ByteBufAllocator alloc) { int length = isEmpty(group) ? 0 : FDFS_GROUP_LEN; byte cmd = isEmpty(group) ? SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE : SERVICE_QUERY_STORE_WITH_GROUP_ONE; ByteBuf buf = alloc.buffer(length + FDFS_HEAD_LEN); buf.writeLong(length); buf.writeByte(cmd); buf.writeByte(ERRNO_OK); if (!isEmpty(group)) { FastdfsUtils.writeFixLength(buf, group, FDFS_GROUP_LEN); } return Collections.singletonList(buf); }
/** * @return A new {@code RtMessage} by parsing the contents of the provided {@code byte[]}. The * contents of {@code srcBytes} must be a well-formed Roughtime message. */ public static RtMessage fromBytes(byte[] srcBytes) { checkNotNull(srcBytes, "srcBytes"); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(srcBytes.length); buf.writeBytes(srcBytes); return new RtMessage(buf); }
@Override protected ByteBuf metadata(ByteBufAllocator alloc) { byte[] pathBytes = fileId.pathBytes(); int metaSize = 2 * FastdfsConstants.FDFS_PROTO_PKG_LEN_SIZE + pathBytes.length; ByteBuf buf = alloc.buffer(metaSize); buf.writeLong(pathBytes.length); buf.writeLong(size()); buf.writeBytes(pathBytes); return buf; }
@Override protected ByteBuf metadata(ByteBufAllocator alloc) { byte[] pathBytes = fileId.pathBytes(); int metaLen = 3 * FastdfsConstants.FDFS_PROTO_PKG_LEN_SIZE + pathBytes.length; ByteBuf buf = alloc.buffer(metaLen); buf.writeLong(pathBytes.length); buf.writeLong(offset); buf.writeLong(size()); buf.writeBytes(pathBytes); return buf; }
@Override public List<Object> encode(ByteBufAllocator alloc) { byte cmd = cmd(); int length = FastdfsConstants.FDFS_GROUP_LEN + fileId.pathBytes().length; ByteBuf buf = alloc.buffer(length + FastdfsConstants.FDFS_HEAD_LEN); buf.writeLong(length); buf.writeByte(cmd); buf.writeByte(FastdfsConstants.ERRNO_OK); FastdfsUtils.writeFixLength(buf, fileId.group(), FastdfsConstants.FDFS_GROUP_LEN); ByteBufUtil.writeUtf8(buf, fileId.path()); return Collections.singletonList(buf); }
@Override public List<Object> encode(ByteBufAllocator alloc) { byte[] pathBytes = fileId.path().getBytes(UTF_8); int length = 2 * FastdfsConstants.FDFS_PROTO_PKG_LEN_SIZE + pathBytes.length; byte cmd = FastdfsConstants.Commands.FILE_TRUNCATE; ByteBuf buf = alloc.buffer(length + FastdfsConstants.FDFS_HEAD_LEN); buf.writeLong(length); buf.writeByte(cmd); buf.writeByte(FastdfsConstants.ERRNO_OK); buf.writeLong(pathBytes.length); buf.writeLong(truncatedSize); buf.writeBytes(pathBytes); return Collections.singletonList(buf); }
@Override public Object resolveId(String name) { String decode = name.substring(name.indexOf("{") + 1, name.indexOf("}")); ByteBuf b = ByteBufAllocator.DEFAULT.buffer(decode.length()/2); try { b.writeBytes(ByteBufUtil.decodeHexDump(decode)); return codec.getMapKeyDecoder().decode(b, new State(false)); } catch (IOException ex) { throw new IllegalStateException("Unable to decode [" + decode + "] into object", ex); } finally { b.release(); } }
public static byte[] hash(ByteBuf objectState) { ByteBuffer b = objectState.internalNioBuffer(objectState.readerIndex(), objectState.readableBytes()); long h1 = LongHashFunction.farmUo().hashBytes(b); long h2 = LongHashFunction.xx().hashBytes(b); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE); try { buf.writeLong(h1).writeLong(h2); byte[] dst = new byte[buf.readableBytes()]; buf.readBytes(dst); return dst; } finally { buf.release(); } }
@Override public ByteBuf encode(Object in) throws IOException { byte[] payload = (byte[])in; ByteBuf out = ByteBufAllocator.DEFAULT.buffer(payload.length); out.writeBytes(payload); return out; }
@Override public ByteBuf encode(Object in) throws IOException { ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { ByteBufOutputStream os = new ByteBufOutputStream(out); mapper.writeValue((OutputStream)os, in); return os.buffer(); } catch (IOException e) { out.release(); throw e; } }
/** * @return A new {@code RtMessage} by parsing the contents of the provided {@code ByteBuffer}, * which can be direct or heap based. The contents of {@code srcBuf} must be a well-formed * Roughtime message. */ public static RtMessage fromByteBuffer(ByteBuffer srcBuf) { checkNotNull(srcBuf, "srcBuf"); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(srcBuf.remaining()); buf.writeBytes(srcBuf); return new RtMessage(buf); }
@Override public ByteBuf encode(Object in) throws IOException { ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { ByteBufOutputStream os = new ByteBufOutputStream(out); mapObjectMapper.writeValue((OutputStream)os, in); return os.buffer(); } catch (IOException e) { out.release(); throw e; } }
static Segment createSegment(ByteBufAllocator byteBufAllocator, int size) { Segment seg = RECYCLER.get(); if (size == 0) { seg.data = byteBufAllocator.ioBuffer(0, 0); } else { seg.data = byteBufAllocator.ioBuffer(size); } return seg; }
ByteBuf encodeRequest(ByteBufAllocator allocator) throws Exception { try { return messageEncoding.writeRequest(allocator, sequenceId, thriftRequest.getMethod(), thriftRequest.getParameters(), ImmutableMap.of()); } catch (Throwable throwable) { onChannelError(throwable); throw throwable; } }
@Override public ByteBuf writeRequest(ByteBufAllocator allocator, int sequenceId, MethodMetadata method, List<Object> parameters, Map<String, String> headers) throws Exception { ByteBuf message = MessageEncoding.encodeRequest(protocolFactory, allocator, sequenceId, method, parameters); // // describe the encoding (Thrift protocol, compression info) ByteBuf encodingInfo = Unpooled.buffer(3); encodingInfo.writeByte(protocolId); // number of "transforms" encodingInfo.writeByte(gzip ? 1 : 0); if (gzip) { // Note this is actually a vint, but there are only 3 headers for now encodingInfo.writeByte(0x01); } // headers ByteBuf encodedHeaders = encodeHeaders(headers); // Padding - header size must be a multiple of 4 int headerSize = encodingInfo.readableBytes() + encodedHeaders.readableBytes(); ByteBuf padding = getPadding(headerSize); headerSize += padding.readableBytes(); // frame header (magic, flags, sequenceId, headerSize ByteBuf frameHeader = Unpooled.buffer(12); frameHeader.writeShort(HEADER_MAGIC); frameHeader.writeShort(FLAG_SUPPORT_OUT_OF_ORDER); frameHeader.writeInt(sequenceId); frameHeader.writeShort(headerSize >> 2); return Unpooled.wrappedBuffer( frameHeader, encodingInfo, encodedHeaders, padding, message); }
@Override public ByteBuf writeRequest(ByteBufAllocator allocator, int sequenceId, MethodMetadata method, List<Object> parameters, Map<String, String> headers) throws Exception { checkArgument(headers.isEmpty(), "Headers are only supported with header transport"); return MessageEncoding.encodeRequest(protocolFactory, allocator, sequenceId, method, parameters); }
public ByteBuf write(ByteBufAllocator out) { long payloadSize = getPayloadSize(); long totalSize = payloadSize + 1 + 2 + 4; ByteBuf buf = out.buffer((int) totalSize); buf.writeByte(type); buf.writeShort(channel); buf.writeInt((int) payloadSize); writePayload(buf); buf.writeByte(FRAME_END); return buf; }
@Override public void run() { Map<Connection, Client> connections = getConnections(); connections.values() .forEach(client -> { try { EncapsulatedPacket packet = client.getConnection().receive(); if (packet == null) { return; } byte[] data = packet.getPacketData(); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(data.length); buf.writeBytes(data); byte id = buf.readByte(); byte senderSubClientId = 0; byte targetSubClientId = 0; log.trace("ID: 0x{}", Integer.toHexString(id)); if (id != PACKET_BATCH) { // Sub client IDs senderSubClientId = buf.readByte(); targetSubClientId = buf.readByte(); } if (id == PACKET_BATCH) { handleBatchPacket(client, buf); } else if (id == PACKET_ENCRYPTION_RESPONSE) { handlePacket(client, buf, id, senderSubClientId, targetSubClientId); } else { log.error("Received malformed packet"); } } catch (Exception ex) { log.error("Connection error!", ex); } }); }