@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(SocketChannel socketChannel) throws Exception { // Ensure ViaVersion is loaded if (ProtocolRegistry.SERVER_PROTOCOL != -1) { UserConnection info = new UserConnection(socketChannel); // init protocol new ProtocolPipeline(info); // Add originals this.method.invoke(this.original, socketChannel); // Add our transformers MessageToByteEncoder encoder = new SpongeEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder")); ByteToMessageDecoder decoder = new SpongeDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder")); SpongePacketHandler chunkHandler = new SpongePacketHandler(info); socketChannel.pipeline().replace("encoder", "encoder", encoder); socketChannel.pipeline().replace("decoder", "decoder", decoder); socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler); } else { this.method.invoke(this.original, socketChannel); } }
static void autoAddHttpExtractor(NettyContext c, String name, ChannelHandler handler){ if (handler instanceof ByteToMessageDecoder || handler instanceof ByteToMessageCodec || handler instanceof CombinedChannelDuplexHandler) { String extractorName = name+"$extractor"; if(c.channel().pipeline().context(extractorName) != null){ return; } c.channel().pipeline().addBefore(name, extractorName, HTTP_EXTRACTOR); if(NettyContext.isPersistent(c.channel())){ c.onClose(() -> c.removeHandler(extractorName)); } } }
ChannelHandler newSslInitiator() { return new ByteToMessageDecoder() { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if(in.readableBytes() < 1) { return; } if('S' != in.readByte()) { ctx.fireExceptionCaught(new IllegalStateException("SSL required but not supported by backend server")); return; } ctx.pipeline().remove(this); ctx.pipeline().addFirst( SslContextBuilder .forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build() .newHandler(ctx.alloc())); } }; }
/** * Creates a LengthFieldBasedFrameDecoder where the first 8 bytes are the length of the frame. * This is used before all decoders. */ public static ByteToMessageDecoder createFrameDecoder() { // maxFrameLength = 2G // lengthFieldOffset = 0 // lengthFieldLength = 8 // lengthAdjustment = -8, i.e. exclude the 8 byte length itself // initialBytesToStrip = 8, i.e. strip out the length field itself return new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 8, -8, 8); }
@Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast("openChannels", transport.serverOpenChannels); final HttpRequestDecoder decoder = new HttpRequestDecoder( Math.toIntExact(transport.maxInitialLineLength.getBytes()), Math.toIntExact(transport.maxHeaderSize.getBytes()), Math.toIntExact(transport.maxChunkSize.getBytes())); decoder.setCumulator(ByteToMessageDecoder.COMPOSITE_CUMULATOR); ch.pipeline().addLast("decoder", decoder); ch.pipeline().addLast("decoder_compress", new HttpContentDecompressor()); ch.pipeline().addLast("encoder", new HttpResponseEncoder()); final HttpObjectAggregator aggregator = new HttpObjectAggregator(Math.toIntExact(transport.maxContentLength.getBytes())); if (transport.maxCompositeBufferComponents != -1) { aggregator.setMaxCumulationBufferComponents(transport.maxCompositeBufferComponents); } ch.pipeline().addLast("aggregator", aggregator); if (transport.compression) { ch.pipeline().addLast("encoder_compress", new HttpContentCompressor(transport.compressionLevel)); } if (SETTING_CORS_ENABLED.get(transport.settings())) { ch.pipeline().addLast("cors", new Netty4CorsHandler(transport.getCorsConfig())); } if (transport.pipelining) { ch.pipeline().addLast("pipelining", new HttpPipeliningHandler(transport.pipeliningMaxEvents)); } ch.pipeline().addLast("handler", requestHandler); }
/** * Call the decode method on a netty ByteToMessageDecoder * * @param decoder The decoder * @param ctx The current context * @param input The packet to decode * @return A list of the decoders output * @throws InvocationTargetException If an exception happens while executing */ public static List<Object> callDecode(ByteToMessageDecoder decoder, ChannelHandlerContext ctx, Object input) throws InvocationTargetException { List<Object> output = new ArrayList<>(); try { PipelineUtil.DECODE_METHOD.invoke(decoder, ctx, input, output); } catch (IllegalAccessException e) { e.printStackTrace(); } return output; }
@Override public ByteToMessageDecoder getDecoder() { ByteDecoder decoder = new ByteDecoder(); decoder.setMessageCodec(msgCodec); decoder.setHeadCodec(headCodec); return decoder; }
protected void initChannel(final Channel channel) throws Exception { super.initChannel(channel); channel.pipeline().addFirst("sslDetectionHandler", new ByteToMessageDecoder() { @Override protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception { if (byteBuf.readableBytes() < 5) { // To detect if SSL must be used we need to have at least 5 bytes, so return here and try again // once more bytes a ready. return; } if (SslHandler.isEncrypted(byteBuf)) { // Connection uses SSL/TLS, replace the detection handler with a SslHandler and so use // encryption. SslHandler sslHandler = createSslHandler(); channelHandlerContext.pipeline().replace(this, "ssl", sslHandler); } else { // Connection use no TLS/SSL encryption, just remove the detection handler and continue without // SslHandler in the pipeline. channelHandlerContext.pipeline().remove(this); } } }); }
@Override protected void initChannel(SocketChannel ch) throws Exception { // Initialize our session Object when the channel is initialized, attach // it to the channel. ch.attr(NetworkConstants.SESSION_KEY).setIfAbsent(new PlayerIO(ch)); // Initialize the pipeline channel handlers. ChannelDuplexHandler timeout = new IdleStateHandler(NetworkConstants.INPUT_TIMEOUT, 0, 0); ByteToMessageDecoder loginHandshakeHandler = new LoginHandshakeHandler(); ch.pipeline().addLast("login-handshake", loginHandshakeHandler); ch.pipeline().addLast("channel-handler", channelHandler); ch.pipeline().addLast("timeout", timeout); }
@Override public BukkitDecodeHandler newDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) { return new BukkitDecodeHandler(info, minecraftDecoder); }
public BukkitDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) { this.info = info; this.minecraftDecoder = minecraftDecoder; }
public SpongeDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) { this.info = info; this.minecraftDecoder = minecraftDecoder; }
/** * 获取解码器 @see ByteToMessageDecoder * * @return 解码器 */ ByteToMessageDecoder getDecoder();
public ByteToMessageDecoder newDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder);