Example usage for io.netty.channel ChannelPipeline addAfter

List of usage examples for io.netty.channel ChannelPipeline addAfter

Introduction

In this page you can find the example usage for io.netty.channel ChannelPipeline addAfter.

Prototype

ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler);

Source Link

Document

Inserts a ChannelHandler after an existing handler of this pipeline.

Usage

From source file:com.ning.http.client.providers.netty_4.NettyAsyncHttpProvider.java

License:Apache License

private void upgradeProtocol(ChannelPipeline p, String scheme) throws IOException, GeneralSecurityException {
    if (p.get(HTTP_HANDLER) != null) {
        p.remove(HTTP_HANDLER);//  www  . j  av  a 2s  . co m
    }

    if (isSecure(scheme)) {
        if (p.get(SSL_HANDLER) == null) {
            p.addFirst(HTTP_HANDLER, newHttpClientCodec());
            p.addFirst(SSL_HANDLER, new SslHandler(createSSLEngine()));
        } else {
            p.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec());
        }

    } else {
        p.addFirst(HTTP_HANDLER, newHttpClientCodec());
    }
}

From source file:com.quavo.osrs.network.handler.listener.WorldLoginListener.java

License:Open Source License

@Override
public void handleMessage(ChannelHandlerContext ctx, WorldLoginRequest msg) {
    ClientMessage message = evaluateLogin(msg);
    if (message != ClientMessage.SUCCESSFUL) {
        ctx.write(new WorldLoginResponse(message));
        return;/*from ww  w  . j  a  v a2 s  . com*/
    }

    Player player = new Player(ctx.channel());
    ctx.write(new WorldLoginResponse(player, message, msg.getIsaacPair()));

    ChannelPipeline pipeline = ctx.pipeline();
    pipeline.remove("login.encoder");

    // this isnt set automatically.
    pipeline.addAfter("world.decoder", "game.encoder",
            new GamePacketEncoder(msg.getIsaacPair().getEncoderRandom()));
    pipeline.replace("world.decoder", "game.decoder",
            new GamePacketDecoder(player, msg.getIsaacPair().getDecoderRandom()));

    player.init(msg.getDisplayInformation());
}

From source file:com.quavo.osrs.network.protocol.codec.connection.ConnectionEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, ConnectionResponse msg, ByteBuf out) throws Exception {
    ChannelPipeline pipeline = ctx.pipeline();

    switch (msg.getType()) {
    case HANDSHAKE_CONNECTION:
        pipeline.addAfter("decoder", "handshake.encoder", new HandshakeEncoder());
        pipeline.replace("decoder", "handshake.decoder", new HandshakeDecoder());
        break;/*from  w  w  w  .j a v  a 2s.  com*/
    case LOGIN_CONNECTION:
        out.writeByte(ClientMessage.SUCCESSFUL_CONNECTION.getId());
        pipeline.addAfter("decoder", "login.encoder", new LoginEncoder());
        pipeline.replace("decoder", "login.decoder", new LoginDecoder());
        break;
    }

    pipeline.remove(this);
}

From source file:com.quavo.osrs.network.protocol.codec.handshake.HandshakeEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, HandshakeResponse msg, ByteBuf out) throws Exception {
    ChannelPipeline pipeline = ctx.pipeline();
    ClientMessage message = msg.getMessage();

    out.writeByte(message.getId());/*from w w w .java  2 s . c om*/
    if (message == ClientMessage.SUCCESSFUL_CONNECTION) {
        pipeline.addAfter("handshake.decoder", "xor.encrypt", new XOREncryptionEncoder());
        pipeline.addAfter("xor.encrypt", "update.encoder", new UpdateEncoder());
        pipeline.replace("handshake.decoder", "update.decoder", new UpdateDecoder());
    }

    pipeline.remove(this);
}

From source file:com.quavo.osrs.network.protocol.codec.login.LoginEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, LoginResponse msg, ByteBuf out) throws Exception {
    ClientMessage message = msg.getMessage();
    ChannelPipeline pipeline = ctx.pipeline();

    if (message != ClientMessage.SUCCESSFUL) {
        // dont write the id for successful.
        out.writeByte(message.getId());/*from   w  w w  .j ava 2  s  . c  om*/
    } else {
        pipeline.addAfter("login.decoder", "world.encoder", new WorldLoginEncoder());
        pipeline.replace("login.decoder", "world.decoder", new WorldLoginDecoder(msg.getType()));
    }

    pipeline.remove(this);
}

From source file:cpw.mods.fml.common.network.internal.FMLNetworkHandler.java

License:Open Source License

@SideOnly(Side.CLIENT)
private static void addClientHandlers() {
    ChannelPipeline pipeline = channelPair.get(Side.CLIENT).pipeline();
    String targetName = channelPair.get(Side.CLIENT).findChannelHandlerNameForType(FMLRuntimeCodec.class);
    pipeline.addAfter(targetName, "GuiHandler", new OpenGuiHandler());
    pipeline.addAfter(targetName, "EntitySpawnHandler", new EntitySpawnHandler());
}

From source file:de.minigames.mclib.nms.v18.NetworkManager1_8.java

License:Open Source License

/**
 * Hook into network traffic and listen for resource pack status changes. Spigot 1.8 does not have the proper event to listen for this packet.
 * //from   ww  w.ja v a  2  s .c o m
 * @param player
 * @param consumer
 */
public static void hookResourcePackStatus(Player player, BiConsumer<Player, ResourcePackStatus> consumer) {
    final NetworkManager manager = ((CraftPlayer) player).getHandle().playerConnection.networkManager;
    Channel channel = null;
    try {
        channel = (Channel) channelField.get(manager);
    } catch (IllegalArgumentException | IllegalAccessException e) {
        // TODO logging
        return;
    }

    final ChannelPipeline pipe = channel.pipeline();

    final ChannelHandler handler = new MessageToMessageDecoder<Packet>() {
        @Override
        protected void decode(ChannelHandlerContext chc, Packet packet, List<Object> out) throws Exception {
            if (packet instanceof PacketPlayInResourcePackStatus) {
                try {
                    final EnumResourcePackStatus status = (EnumResourcePackStatus) enumField.get(packet);
                    switch (status) {
                    case ACCEPTED:
                        consumer.accept(player, ResourcePackStatus.ACCEPTED);
                        break;
                    case DECLINED:
                        consumer.accept(player, ResourcePackStatus.DECLINED);
                        break;
                    case FAILED_DOWNLOAD:
                        consumer.accept(player, ResourcePackStatus.FAILED_DOWNLOAD);
                        break;
                    case SUCCESSFULLY_LOADED:
                        consumer.accept(player, ResourcePackStatus.SUCCESSFULLY_LOADED);
                        break;
                    default:
                        break;
                    }
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    // TODO logging
                }
            }
            out.add(packet);
        }
    };

    pipe.addAfter("decoder", //$NON-NLS-1$
            "mclib", //$NON-NLS-1$
            handler);
}

From source file:example.http2.helloworld.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *//*from  w  w w  . j  a va  2 s. c o  m*/
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();
    final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec,
            upgradeCodecFactory);
    final CleartextHttp2ServerUpgradeHandler cleartextHttp2ServerUpgradeHandler = new CleartextHttp2ServerUpgradeHandler(
            sourceCodec, upgradeHandler, new HelloWorldHttp2HandlerBuilder().build());

    p.addLast(cleartextHttp2ServerUpgradeHandler);
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null,
                    new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:http2.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *//* w  w w  .j  a  va  2 s.c o  m*/
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();

    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    // ?
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null,
                    new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:io.gatling.http.client.impl.DefaultHttpClient.java

License:Apache License

private Future<Channel> installSslHandler(HttpTx tx, Channel channel) {

    SslContext sslCtx = tx.request.isAlpnRequired() ? alpnSslContext : sslContext;

    try {/*from w w  w  . j a  v a 2  s  .  com*/
        SslHandler sslHandler = SslHandlers.newSslHandler(sslCtx, channel.alloc(), tx.request.getUri(),
                tx.request.getVirtualHost(), config);
        tx.listener.onTlsHandshakeAttempt();

        ChannelPipeline pipeline = channel.pipeline();
        String after = pipeline.get(PROXY_HANDLER) != null ? PROXY_HANDLER : PINNED_HANDLER;
        pipeline.addAfter(after, SSL_HANDLER, sslHandler);

        return sslHandler.handshakeFuture().addListener(f -> {

            SSLSession sslSession = sslHandler.engine().getHandshakeSession();
            if (sslSession != null && sslSession.isValid() && config.isDisableSslSessionResumption()) {
                sslSession.invalidate();
            }

            if (tx.requestTimeout.isDone()) {
                return;
            }

            if (f.isSuccess()) {
                tx.listener.onTlsHandshakeSuccess();
            } else {
                tx.requestTimeout.cancel();
                tx.listener.onTlsHandshakeFailure(f.cause());
                tx.listener.onThrowable(f.cause());
            }
        });
    } catch (RuntimeException e) {
        tx.requestTimeout.cancel();
        tx.listener.onThrowable(e);
        return new DefaultPromise<Channel>(ImmediateEventExecutor.INSTANCE).setFailure(e);
    }
}