io.netty.handler.codec.MessageToByteEncoder Java Examples

The following examples show how to use io.netty.handler.codec.MessageToByteEncoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractNetTcp.java    From lippen-network-tool with Apache License 2.0 6 votes vote down vote up
protected AbstractNetTcp(DataManager data, MessageReceivedListener listener, String netName) {
    super(data, netName);
    initializer = new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast("encoder", new MessageToByteEncoder<byte[]>() {
                @Override
                protected void encode(ChannelHandlerContext ctx, byte[] msg, ByteBuf out) throws Exception {
                    out.writeBytes(msg);
                }
            });
            ch.pipeline().addLast("handler", new TCPHandler(data, listener));
        }
    };
    this.data = data;
}
 
Example #2
Source File: BukkitChannelInitializer.java    From ViaVersion with MIT License 6 votes vote down vote up
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    UserConnection info = new UserConnection(socketChannel);
    // init protocol
    new ProtocolPipeline(info);
    // Add originals
    this.method.invoke(this.original, socketChannel);

    HandlerConstructor constructor = ClassGenerator.getConstructor();
    // Add our transformers
    MessageToByteEncoder encoder = constructor.newEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder"));
    ByteToMessageDecoder decoder = constructor.newDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder"));
    BukkitPacketHandler chunkHandler = new BukkitPacketHandler(info);

    socketChannel.pipeline().replace("encoder", "encoder", encoder);
    socketChannel.pipeline().replace("decoder", "decoder", decoder);
    socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler);
}
 
Example #3
Source File: SpongeChannelInitializer.java    From ViaVersion with MIT License 6 votes vote down vote up
@Override
protected void initChannel(Channel channel) throws Exception {
    // Ensure ViaVersion is loaded
    if (ProtocolRegistry.SERVER_PROTOCOL != -1
            && channel instanceof SocketChannel) { // channel can be LocalChannel on internal server
        UserConnection info = new UserConnection((SocketChannel) channel);
        // init protocol
        new ProtocolPipeline(info);
        // Add originals
        this.method.invoke(this.original, channel);
        // Add our transformers
        MessageToByteEncoder encoder = new SpongeEncodeHandler(info, (MessageToByteEncoder) channel.pipeline().get("encoder"));
        ByteToMessageDecoder decoder = new SpongeDecodeHandler(info, (ByteToMessageDecoder) channel.pipeline().get("decoder"));
        SpongePacketHandler chunkHandler = new SpongePacketHandler(info);

        channel.pipeline().replace("encoder", "encoder", encoder);
        channel.pipeline().replace("decoder", "decoder", decoder);
        channel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler);
    } else {
        this.method.invoke(this.original, channel);
    }
}
 
Example #4
Source File: HttpContentEncoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected Result beginEncode(HttpResponse headers, String acceptEncoding) {
    return new Result("test", new EmbeddedChannel(new MessageToByteEncoder<ByteBuf>() {
        @Override
        protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
            out.writeBytes(String.valueOf(in.readableBytes()).getBytes(CharsetUtil.US_ASCII));
            in.skipBytes(in.readableBytes());
        }
    }));
}
 
Example #5
Source File: HttpContentEncoderTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected Result beginEncode(HttpResponse headers, String acceptEncoding) {
    return new Result("test", new EmbeddedChannel(new MessageToByteEncoder<ByteBuf>() {
        @Override
        protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
            out.writeBytes(String.valueOf(in.readableBytes()).getBytes(CharsetUtil.US_ASCII));
            in.skipBytes(in.readableBytes());
        }
    }));
}
 
Example #6
Source File: BungeePipelineUtil.java    From ViaVersion with MIT License 5 votes vote down vote up
public static ByteBuf callEncode(MessageToByteEncoder encoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException {
    ByteBuf output = ctx.alloc().buffer();
    try {
        BungeePipelineUtil.ENCODE_METHOD.invoke(encoder, ctx, input, output);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return output;
}
 
Example #7
Source File: BungeePipelineUtil.java    From ViaVersion with MIT License 5 votes vote down vote up
public static ByteBuf compress(ChannelHandlerContext ctx, ByteBuf bytebuf) {
    try {
        return callEncode((MessageToByteEncoder) ctx.pipeline().get("compress"), ctx.pipeline().context("compress"), bytebuf);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        return ctx.alloc().buffer();
    }
}
 
Example #8
Source File: VelocityEncodeHandler.java    From ViaVersion with MIT License 5 votes vote down vote up
private void recompress(ChannelHandlerContext ctx, ByteBuf buf) throws InvocationTargetException {
    ByteBuf compressed = ctx.alloc().buffer();
    try {
        PipelineUtil.callEncode((MessageToByteEncoder<?>) ctx.pipeline().get("compression-encoder"), ctx, buf, compressed);
        buf.clear().writeBytes(compressed);
    } finally {
        compressed.release();
    }
}
 
Example #9
Source File: DefaultByteCodecFactory.java    From multi-engine with Apache License 2.0 5 votes vote down vote up
@Override
public MessageToByteEncoder<Object> getEncoder() {
    ByteEncoder encoder = new ByteEncoder();
    encoder.setMessageCodec(msgCodec);
    encoder.setHeadCodec(headCodec);
    return encoder;
}
 
Example #10
Source File: MessagingProtocolV1.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public MessageToByteEncoder<Object> newEncoder() {
  return new MessageEncoderV1(address);
}
 
Example #11
Source File: MessagingProtocolV2.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public MessageToByteEncoder<Object> newEncoder() {
  return new MessageEncoderV2(address);
}
 
Example #12
Source File: NettyServerPipelineFactory.java    From library with Apache License 2.0 4 votes vote down vote up
public MessageToByteEncoder getEncoder(){
	return new NettyTOMMessageEncoder(false, sessionTable,rl);	
}
 
Example #13
Source File: NettyClientPipelineFactory.java    From library with Apache License 2.0 4 votes vote down vote up
public MessageToByteEncoder getEncoder(){
	return new NettyTOMMessageEncoder(true, sessionTable,rl);	
}
 
Example #14
Source File: TestProxyConfig.java    From x-pipe with Apache License 2.0 4 votes vote down vote up
@Override
public MessageToByteEncoder<ByteBuf> getCompressEncoder() {
    return new ZstdEncoder();
}
 
Example #15
Source File: DefaultProxyConfig.java    From x-pipe with Apache License 2.0 4 votes vote down vote up
@Override
public MessageToByteEncoder<ByteBuf> getCompressEncoder() {
    return new ZstdEncoder();
}
 
Example #16
Source File: SpongeEncodeHandler.java    From ViaVersion with MIT License 4 votes vote down vote up
public SpongeEncodeHandler(UserConnection info, MessageToByteEncoder<?> minecraftEncoder) {
    this.info = info;
    this.minecraftEncoder = minecraftEncoder;
}
 
Example #17
Source File: BukkitEncodeHandler.java    From ViaVersion with MIT License 4 votes vote down vote up
public BukkitEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {
    this.info = info;
    this.minecraftEncoder = minecraftEncoder;
}
 
Example #18
Source File: BasicHandlerConstructor.java    From ViaVersion with MIT License 4 votes vote down vote up
@Override
public BukkitEncodeHandler newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {
    return new BukkitEncodeHandler(info, minecraftEncoder);
}
 
Example #19
Source File: NettyServerPipelineFactory.java    From protect with MIT License 4 votes vote down vote up
public MessageToByteEncoder getEncoder(){
	return new NettyTOMMessageEncoder(false, sessionTable, macLength,rl,signatureLength, controller.getStaticConf().getUseMACs()==1?true:false);	
}
 
Example #20
Source File: NettyClientPipelineFactory.java    From protect with MIT License 4 votes vote down vote up
public MessageToByteEncoder getEncoder(){
	return new NettyTOMMessageEncoder(true, sessionTable, macLength,rl, signatureLength, controller.getStaticConf().getUseMACs()==1?true:false);	
}
 
Example #21
Source File: PipelineUtil.java    From ViaVersion with MIT License 3 votes vote down vote up
/**
 * Call the encode method on a netty MessageToByteEncoder
 *
 * @param encoder The encoder
 * @param ctx     The current context
 * @param msg     The packet to encode
 * @param output  The bytebuf to write the output to
 * @throws InvocationTargetException If an exception happens while executing
 */
public static void callEncode(MessageToByteEncoder encoder, ChannelHandlerContext ctx, Object msg, ByteBuf output) throws InvocationTargetException {
    try {
        PipelineUtil.ENCODE_METHOD.invoke(encoder, ctx, msg, output);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}
 
Example #22
Source File: ByteCodecFactory.java    From multi-engine with Apache License 2.0 2 votes vote down vote up
/**
 * 获取编码器 @see MessageToByteEncoder
 * 
 * @return 编码器
 */
MessageToByteEncoder<Object> getEncoder();
 
Example #23
Source File: MessagingProtocol.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a new message encoder.
 *
 * @return a new message encoder
 */
MessageToByteEncoder<Object> newEncoder();
 
Example #24
Source File: ProxyConfig.java    From x-pipe with Apache License 2.0 votes vote down vote up
MessageToByteEncoder<ByteBuf> getCompressEncoder(); 
Example #25
Source File: HandlerConstructor.java    From ViaVersion with MIT License votes vote down vote up
public MessageToByteEncoder newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder);