Example usage for io.netty.channel ChannelPipeline addBefore

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

Introduction

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

Prototype

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

Source Link

Document

Inserts a ChannelHandler before an existing handler of 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/*ww w.j  a  v a 2 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.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  av  a 2 s . c  o  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.java 2 s  . com

    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.linecorp.armeria.client.http.HttpClientPipelineConfigurator.java

License:Apache License

void addBeforeSessionHandler(ChannelPipeline pipeline, ChannelHandler handler) {
    // Get the name of the HttpSessionHandler so that we can put our handlers before it.
    final ChannelHandlerContext lastContext = pipeline.lastContext();
    assert lastContext.handler().getClass() == HttpSessionHandler.class;

    pipeline.addBefore(lastContext.name(), null, handler);
}

From source file:com.linecorp.armeria.client.HttpConfigurator.java

License:Apache License

void addBeforeSessionHandler(ChannelPipeline pipeline, ChannelHandler handler) {
    // Get the name of the HttpSessionHandler so that we can put our handlers before it.
    final String sessionHandlerName = pipeline.context(HttpSessionHandler.class).name();
    pipeline.addBefore(sessionHandlerName, null, handler);
}

From source file:com.otcdlink.chiron.downend.Http11ProxyHandler.java

License:Apache License

@Override
protected void addCodec(ChannelHandlerContext ctx) throws Exception {
    ChannelPipeline p = ctx.pipeline();
    String name = ctx.name();/*  w ww  .  ja va 2  s  .c  o m*/
    p.addBefore(name, null, codec);
}

From source file:com.replaymod.sponge.recording.spongecommon.SpongeRecorder.java

License:MIT License

public SpongeRecorder(Game game, SpongeConnection connection) {
    super(game, connection);

    ChannelPipeline pipeline = connection.getChannel().get().pipeline();
    pipeline.addLast("recorder_compression_order", new PipelineOrderHandler());
    pipeline.addBefore("encoder", "outbound_recorder", new OutboundPacketRecorder());
    pipeline.addBefore("decoder", "inbound_recorder", new InboundPacketRecorder());
}

From source file:diskCacheV111.doors.NettyLineBasedDoor.java

License:Open Source License

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    ChannelPipeline pipeline = ctx.pipeline();
    String self = ctx.name();/*from w w  w. ja v a 2  s  .c o  m*/

    if (expectProxyProtocol) {
        pipeline.addBefore("door", "haproxy", new HAProxyMessageDecoder());
    }

    // Decoders
    pipeline.addBefore(self, "frameDecoder", new LineBasedFrameDecoder(KiB.toBytes(64)));
    pipeline.addBefore(self, "stringDecoder", new StringDecoder(charset));

    // Encoder
    pipeline.addBefore(self, "lineEncoder", new LineEncoder(lineSeparator, charset));

    pipeline.addBefore(self, "logger", new LoggingHandler());
}

From source file:io.advantageous.conekt.http.impl.ClientConnection.java

License:Open Source License

synchronized void toWebSocket(String requestURI, MultiMap headers, WebsocketVersion vers, String subProtocols,
        int maxWebSocketFrameSize, Handler<WebSocket> wsConnect) {
    if (ws != null) {
        throw new IllegalStateException("Already websocket");
    }//  www .  ja  v  a 2  s . c om

    try {
        URI wsuri = new URI(requestURI);
        if (!wsuri.isAbsolute()) {
            // Netty requires an absolute url
            wsuri = new URI((ssl ? "https:" : "http:") + "//" + host + ":" + port + requestURI);
        }
        WebSocketVersion version = WebSocketVersion
                .valueOf((vers == null ? WebSocketVersion.V13 : vers).toString());
        HttpHeaders nettyHeaders;
        if (headers != null) {
            nettyHeaders = new DefaultHttpHeaders();
            for (Map.Entry<String, String> entry : headers) {
                nettyHeaders.add(entry.getKey(), entry.getValue());
            }
        } else {
            nettyHeaders = null;
        }
        handshaker = WebSocketClientHandshakerFactory.newHandshaker(wsuri, version, subProtocols, false,
                nettyHeaders, maxWebSocketFrameSize);
        ChannelPipeline p = channel.pipeline();
        p.addBefore("handler", "handshakeCompleter",
                new HandshakeInboundHandler(wsConnect, version != WebSocketVersion.V00));
        handshaker.handshake(channel).addListener(future -> {
            if (!future.isSuccess() && exceptionHandler != null) {
                exceptionHandler.handle(future.cause());
            }
        });
    } catch (Exception e) {
        handleException(e);
    }
}

From source file:io.crate.plugin.PipelineRegistry.java

License:Apache License

public void registerItems(ChannelPipeline pipeline) {
    for (PipelineRegistry.ChannelPipelineItem item : addBeforeList) {
        pipeline.addBefore(item.base, item.name, item.handlerFactory.get());
    }/*www .  j  a  va  2s  . c o m*/

    if (sslContextProvider != null) {
        SslContext sslContext = sslContextProvider.get();
        if (sslContext != null) {
            SslHandler sslHandler = sslContext.newHandler(pipeline.channel().alloc());
            pipeline.addFirst(sslHandler);
        }
    }
}