类 io.netty.handler.codec.MessageToByteEncoder 源码实例Demo

下面列出了怎么用 io.netty.handler.codec.MessageToByteEncoder 的API类实例代码及写法,或者点击链接到github查看源代码。


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;
}
 

@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);
}
 

@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);
    }
}
 

@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());
        }
    }));
}
 
源代码5 项目: ViaVersion   文件: BungeePipelineUtil.java

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;
}
 
源代码6 项目: ViaVersion   文件: BungeePipelineUtil.java

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();
    }
}
 

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();
    }
}
 

@Override
public MessageToByteEncoder<Object> getEncoder() {
    ByteEncoder encoder = new ByteEncoder();
    encoder.setMessageCodec(msgCodec);
    encoder.setHeadCodec(headCodec);
    return encoder;
}
 

@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());
        }
    }));
}
 

public MessageToByteEncoder getEncoder(){
	return new NettyTOMMessageEncoder(true, sessionTable, macLength,rl, signatureLength, controller.getStaticConf().getUseMACs()==1?true:false);	
}
 

public MessageToByteEncoder getEncoder(){
	return new NettyTOMMessageEncoder(false, sessionTable, macLength,rl,signatureLength, controller.getStaticConf().getUseMACs()==1?true:false);	
}
 

@Override
public BukkitEncodeHandler newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {
    return new BukkitEncodeHandler(info, minecraftEncoder);
}
 
源代码13 项目: ViaVersion   文件: BukkitEncodeHandler.java

public BukkitEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {
    this.info = info;
    this.minecraftEncoder = minecraftEncoder;
}
 
源代码14 项目: ViaVersion   文件: SpongeEncodeHandler.java

public SpongeEncodeHandler(UserConnection info, MessageToByteEncoder<?> minecraftEncoder) {
    this.info = info;
    this.minecraftEncoder = minecraftEncoder;
}
 
源代码15 项目: x-pipe   文件: DefaultProxyConfig.java

@Override
public MessageToByteEncoder<ByteBuf> getCompressEncoder() {
    return new ZstdEncoder();
}
 
源代码16 项目: x-pipe   文件: TestProxyConfig.java

@Override
public MessageToByteEncoder<ByteBuf> getCompressEncoder() {
    return new ZstdEncoder();
}
 

public MessageToByteEncoder getEncoder(){
	return new NettyTOMMessageEncoder(true, sessionTable,rl);	
}
 

public MessageToByteEncoder getEncoder(){
	return new NettyTOMMessageEncoder(false, sessionTable,rl);	
}
 
源代码19 项目: atomix   文件: MessagingProtocolV2.java

@Override
public MessageToByteEncoder<Object> newEncoder() {
  return new MessageEncoderV2(address);
}
 
源代码20 项目: atomix   文件: MessagingProtocolV1.java

@Override
public MessageToByteEncoder<Object> newEncoder() {
  return new MessageEncoderV1(address);
}
 
源代码21 项目: ViaVersion   文件: PipelineUtil.java

/**
 * 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();
    }
}
 
源代码22 项目: multi-engine   文件: ByteCodecFactory.java

/**
 * 获取编码器 @see MessageToByteEncoder
 * 
 * @return 编码器
 */
MessageToByteEncoder<Object> getEncoder();
 
源代码23 项目: atomix   文件: MessagingProtocol.java

/**
 * Returns a new message encoder.
 *
 * @return a new message encoder
 */
MessageToByteEncoder<Object> newEncoder();
 
源代码24 项目: ViaVersion   文件: HandlerConstructor.java

public MessageToByteEncoder newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder); 
源代码25 项目: x-pipe   文件: ProxyConfig.java

MessageToByteEncoder<ByteBuf> getCompressEncoder(); 
 类所在包
 类方法
 同包方法