public static Packet wrap(MessageLiteOrBuilder message) { if (message instanceof Packet) { return ((Packet) message); } if (message instanceof Packet.Builder) { return ((Packet.Builder) message).build(); } ByteString bytes = toByteString(message); Packet.Builder builder = Packet.newBuilder(); builder.setSequence(SEQUENCE.incrementAndGet()); MessageType messageType = getMessageType(message); builder.setType(messageType); builder.setData(bytes); return builder.build(); }
public static Packet.Builder wrapBuilder(MessageLiteOrBuilder message) { if (message instanceof Packet) { return ((Packet) message).toBuilder(); } if (message instanceof Packet.Builder) { return (Packet.Builder) message; } ByteString bytes = toByteString(message); Packet.Builder builder = Packet.newBuilder(); builder.setSequence(SEQUENCE.incrementAndGet()); MessageType messageType = getMessageType(message); builder.setType(messageType); builder.setData(bytes); return builder; }
@Override protected void encode( ChannelHandlerContext ctx, MessageLiteOrBuilder msg, List<Object> out) throws Exception { // if (msg instanceof MessageLite) { // out.add(wrappedBuffer(((MessageLite) msg).toByteArray())); // return; // } // if (msg instanceof MessageLite.Builder) { // out.add(wrappedBuffer(((MessageLite.Builder) msg).build().toByteArray())); // } Packet packet = PacketWrapper.wrap(msg); if (packet != null) { out.add(wrappedBuffer(packet.toByteArray())); } }
private static <M extends MessageLiteOrBuilder> List<M> filterFrom( List<M> objs, Predicate<M> filter, int firstDiscarded) { List<M> filtered; if (firstDiscarded == 0) { filtered = null; } else { filtered = new ArrayList<>(objs.size() - 1); for (int i = 0; i < firstDiscarded; ++i) { filtered.add(objs.get(i)); } } for (int i = firstDiscarded + 1; i < objs.size(); ++i) { M obj = objs.get(i); if (filter.test(obj)) { if (filtered == null) { filtered = new ArrayList<>(objs.size() - i); } filtered.add(obj); } } return filtered == null ? ImmutableList.<M>of() : filtered; }
@Override protected void encode( ChannelHandlerContext ctx, MessageLiteOrBuilder msg, List<Object> out) throws Exception { if (msg instanceof MessageLite) { out.add(wrappedBuffer(((MessageLite) msg).toByteArray())); return; } if (msg instanceof MessageLite.Builder) { out.add(wrappedBuffer(((MessageLite.Builder) msg).build().toByteArray())); } }
private static ByteString toByteString(MessageLiteOrBuilder message) { ByteString bytes = null; if (message instanceof MessageLite) { bytes = ((MessageLite) message).toByteString(); } else if (message instanceof MessageLite.Builder) { bytes = ((MessageLite.Builder) message).build().toByteString(); } return bytes; }
private static MessageType getMessageType(MessageLiteOrBuilder message) { String key = null; if (message instanceof MessageLite) { key = message.getClass().getSimpleName(); } if (message instanceof MessageLite.Builder) { key = ((MessageLite.Builder) message).build().getClass().getSimpleName(); } return messageTypesByName.get(key.toUpperCase()); }
public static void sendError(HandlerContext ctx, Packet packet, MessageLiteOrBuilder error){ Packet.Builder builder = packet.toBuilder(); if(error instanceof MessageLite.Builder){ builder.setData(((MessageLite.Builder) error).build().toByteString()); }else { builder.setData(((MessageLite) error).toByteString()); } ctx.write(builder.build()); }
public static ByteBuf encodeNoLengthPrefix(MessageLiteOrBuilder msg) { if (msg instanceof MessageLite) { return Unpooled.wrappedBuffer(((MessageLite) msg).toByteArray()); } if (msg instanceof MessageLite.Builder) { return Unpooled.wrappedBuffer(((MessageLite.Builder) msg).build().toByteArray()); } return null; }
/** * Given a message-or-builder, returns a message, invoking the builder if necessary. */ @SuppressWarnings("unchecked") public static <I extends MessageLiteOrBuilder, O extends MessageLite> O built(@Nullable I msg) { return msg instanceof MessageLite.Builder ? (O) ((MessageLite.Builder) msg).build() : (O) msg; }
/** * Given a message-or-builder, return a builder, invoking toBuilder() if necessary. */ @SuppressWarnings("unchecked") public static <I extends MessageLiteOrBuilder, O extends MessageLite.Builder> O builder( @Nullable I msg) { return msg instanceof MessageLite ? (O) ((MessageLite) msg).toBuilder() : (O) msg; }
public void write(MessageLiteOrBuilder message) { if (!connected) { this.connect(); // 发送数据时自动连接 } channel.writeAndFlush(message); }