@Override public byte[] encode(T message) { return message.toByteArray(); }
@Override public byte[] encode(T message) { return message.toByteArray(); }
@Override public byte[] encode(@Nullable T v) { if (v == null) return null; return v.toByteArray(); } }
protected ByteBuffer responseToByteBuffer(GeneratedMessageV3 message) { int length = message.toByteArray().length; byte[] varint = BigInteger.valueOf(length).toByteArray(); long varintLength = varint.length; byte[] varintPrefix = BigInteger.valueOf(varintLength).toByteArray(); ByteBuffer combined = ByteBuffer.allocate(varintPrefix.length + varint.length + length); combined.put(varintPrefix); combined.put(varint); combined.put(message.toByteArray()); combined.flip(); return combined; }
public static <T extends GeneratedMessageV3> void writeProtoToFile(RandomAccessFile raf, T message) { byte[] messageBytes = message.toByteArray(); long crc32 = getCRC32(messageBytes); try { raf.writeLong(crc32); raf.writeInt(messageBytes.length); raf.write(messageBytes); } catch (IOException ex) { LOG.warn("write proto to file error, msg={}", ex.getMessage()); throw new RuntimeException("write proto to file error"); } }
private ByteBuf makeResponseBuffer(ByteBuffer requestBuffer) throws InvalidProtocolBufferException, StatusException { GrpcRequest grpcRequest = requestParser.parse(requestBuffer); GrpcResponse grpcResponse = appLogic.apply(grpcRequest); byte[] dataBytes = grpcResponse.toByteArray(); int length = dataBytes.length; byte[] lengthByteBuffer = ByteBuffer.allocate(4).putInt(length).array(); byte[] compressedByteBuffer = ByteBuffer.allocate(1).put((byte) 0).array(); ByteBuf responseBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(length + METADATA_SIZE, length + METADATA_SIZE); responseBuffer.writeBytes(compressedByteBuffer); responseBuffer.writeBytes(lengthByteBuffer); responseBuffer.writeBytes(dataBytes); return responseBuffer; }
public DiozeroProtos.Response sendMessage(String topic, String correlationId, GeneratedMessageV3 message) throws MqttException { Condition condition = lock.newCondition(); conditions.put(correlationId, condition); lock.lock(); try { mqttClient.publish(topic, message.toByteArray(), MqttProviderConstants.DEFAULT_QOS, MqttProviderConstants.DEFAULT_RETAINED); Logger.info("Waiting for response..."); condition.await(TIMEOUT_MS, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Logger.error(e, "Interrupted: {}", e); } finally { lock.unlock(); } DiozeroProtos.Response response = responses.remove(correlationId); if (response == null) { throw new RuntimeIOException("Cannot find response message for " + correlationId); } return response; }
private ByteBuf bufferFor( com.google.protobuf.GeneratedMessageV3 protoObject, boolean compressed) { byte[] dataBytes = protoObject.toByteArray(); int length = dataBytes.length; byte[] lengthByteBuffer = ByteBuffer.allocate(4).putInt(length).array(); int compressedFlag = compressed ? 1 : 0; byte[] compressedByteBuffer = ByteBuffer.allocate(1).put((byte) compressedFlag).array(); ByteBuf grpcRequestBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(length + 5, length + 5); grpcRequestBuffer.writeBytes(compressedByteBuffer); grpcRequestBuffer.writeBytes(lengthByteBuffer); grpcRequestBuffer.writeBytes(dataBytes); return grpcRequestBuffer; }