Example usage for io.netty.channel ChannelPipeline replace

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

Introduction

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

Prototype

<T extends ChannelHandler> T replace(Class<T> oldHandlerType, String newName, ChannelHandler newHandler);

Source Link

Document

Replaces the ChannelHandler of the specified type with a new handler in this pipeline.

Usage

From source file:co.marcin.novaguilds.impl.versionimpl.v1_8.PacketExtensionImpl.java

License:Open Source License

@Override
public void registerPlayer(final Player player) {
    Channel c = getChannel(player);
    ChannelHandler handler = new ChannelDuplexHandler() {
        @Override/*w  w w. j  a  va2  s .  c  o  m*/
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            if (msg == null) {
                return;
            }

            super.write(ctx, msg, promise);
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            try {
                if (msg == null) {
                    return;
                }

                PacketReceiveEvent event = callEvent(new PacketReceiveEvent(msg, player));

                if (event.isCancelled() || event.getPacket() == null) {
                    return;
                }
                super.channelRead(ctx, event.getPacket());
            } catch (Exception e) {
                super.channelRead(ctx, msg);
            }
        }
    };

    ChannelPipeline cp = c.pipeline();
    if (cp.names().contains("packet_handler")) {
        if (cp.names().contains("NovaGuilds")) {
            cp.replace("NovaGuilds", "NovaGuilds", handler);
        } else {
            cp.addBefore("packet_handler", "NovaGuilds", handler);
        }
    }
}

From source file:com.flysoloing.learning.network.netty.http2.helloworld.multiplex.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  .co 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:com.googlecode.protobuf.pro.duplex.client.DuplexTcpClientPipelineFactory.java

License:Apache License

/**
 * After RPC handshake has taken place, remove the RPC handshake
 * {@link ClientConnectResponseHandler} and add a {@link RpcClientHandler}
 * and {@link RpcServerHandler} to complete the Netty client side Pipeline.
 * //from w w w. j a  v  a 2s .  co m
 * @param rpcClient
 * @return
 */
protected RpcClientHandler completePipeline(RpcClient rpcClient) {
    ChannelPipeline p = rpcClient.getChannel().pipeline();

    if (rpcClient.isCompression()) {
        p.addBefore(Handler.FRAME_DECODER, Handler.COMPRESSOR,
                ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
        p.addAfter(Handler.COMPRESSOR, Handler.DECOMPRESSOR, ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }

    TcpConnectionEventListener informer = new TcpConnectionEventListener() {
        @Override
        public void connectionClosed(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionClosed(client);
            }
        }

        @Override
        public void connectionOpened(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionOpened(client);
            }
        }
    };
    RpcClientHandler rpcClientHandler = new RpcClientHandler(rpcClient, informer);
    p.replace(Handler.CLIENT_CONNECT, Handler.RPC_CLIENT, rpcClientHandler);

    RpcServer rpcServer = new RpcServer(rpcClient, rpcServiceRegistry, rpcServerCallExecutor, logger);
    RpcServerHandler rpcServerHandler = new RpcServerHandler(rpcServer, rpcClientRegistry);
    p.addAfter(Handler.RPC_CLIENT, Handler.RPC_SERVER, rpcServerHandler);

    return rpcClientHandler;
}

From source file:com.googlecode.protobuf.pro.duplex.server.DuplexTcpServerPipelineFactory.java

License:Apache License

public RpcClientHandler completePipeline(RpcClient rpcClient) {
    ChannelPipeline p = rpcClient.getChannel().pipeline();

    if (rpcClient.isCompression()) {
        p.addBefore(Handler.FRAME_DECODER, Handler.COMPRESSOR,
                ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
        p.addAfter(Handler.COMPRESSOR, Handler.DECOMPRESSOR, ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }//from  ww w  . j  ava2s .c o m

    TcpConnectionEventListener informer = new TcpConnectionEventListener() {
        @Override
        public void connectionClosed(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionClosed(client);
            }
        }

        @Override
        public void connectionOpened(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionOpened(client);
            }
        }
    };

    RpcClientHandler rpcClientHandler = new RpcClientHandler(rpcClient, informer);
    p.replace(Handler.SERVER_CONNECT, Handler.RPC_CLIENT, rpcClientHandler);

    RpcServer rpcServer = new RpcServer(rpcClient, getRpcServiceRegistry(), getRpcServerCallExecutor(),
            getLogger());
    RpcServerHandler rpcServerHandler = new RpcServerHandler(rpcServer, getRpcClientRegistry());
    p.addAfter(Handler.RPC_CLIENT, Handler.RPC_SERVER, rpcServerHandler);

    if (log.isDebugEnabled()) {
        log.debug("completed Pipeline to " + rpcClient.getPeerInfo());
    }
    return rpcClientHandler;
}

From source file:com.hop.hhxx.example.http2.helloworld.multiplex.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *///  w w  w.j  a v a2 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(msg);
        }
    });

    p.addLast(new UserEventLogger());
}

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 w  w w  . ja  v a 2s. 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  ww  w  .j  a v a  2s. co  m
    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  ww  w  .j ava  2s . c o m*/
    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());//w w w . j ava  2s  .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:dorkbox.network.connection.registration.remote.RegistrationRemoteHandler.java

License:Apache License

/**
 * upgrades a channel ONE channel at a time
 *///w w w  . j a v a2s  .  c o m
final void upgradeDecoders(final Channel channel, final MetaChannel metaChannel) {
    ChannelPipeline pipeline = channel.pipeline();

    try {
        if (metaChannel.tcpChannel == channel) {
            pipeline.replace(FRAME_AND_KRYO_DECODER, FRAME_AND_KRYO_CRYPTO_DECODER,
                    new KryoDecoderCrypto(this.serializationManager)); // cannot be shared because of possible fragmentation.
        }

        if (metaChannel.udpChannel == channel) {
            pipeline.replace(KRYO_DECODER, KRYO_CRYPTO_DECODER, this.registrationWrapper.kryoUdpDecoderCrypto); // shared encoder
        }
    } catch (Exception e) {
        logger.error("Error during connection pipeline upgrade", e);
    }
}