Search in sources :

Example 41 with ChannelHandler

use of io.netty.channel.ChannelHandler in project netty by netty.

the class Http2StreamChannelBootstrap method init.

private void init(Channel channel) {
    ChannelPipeline p = channel.pipeline();
    ChannelHandler handler = this.handler;
    if (handler != null) {
        p.addLast(handler);
    }
    final Map.Entry<ChannelOption<?>, Object>[] optionArray;
    synchronized (options) {
        optionArray = options.entrySet().toArray(EMPTY_OPTION_ARRAY);
    }
    setChannelOptions(channel, optionArray);
    setAttributes(channel, attrs.entrySet().toArray(EMPTY_ATTRIBUTE_ARRAY));
}
Also used : ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 42 with ChannelHandler

use of io.netty.channel.ChannelHandler in project netty by netty.

the class ServerBootstrap method init.

@Override
void init(Channel channel) {
    setChannelOptions(channel, newOptionsArray(), logger);
    setAttributes(channel, newAttributesArray());
    ChannelPipeline p = channel.pipeline();
    final EventLoopGroup currentChildGroup = childGroup;
    final ChannelHandler currentChildHandler = childHandler;
    final Entry<ChannelOption<?>, Object>[] currentChildOptions = newOptionsArray(childOptions);
    final Entry<AttributeKey<?>, Object>[] currentChildAttrs = newAttributesArray(childAttrs);
    p.addLast(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(final Channel ch) {
            final ChannelPipeline pipeline = ch.pipeline();
            ChannelHandler handler = config.handler();
            if (handler != null) {
                pipeline.addLast(handler);
            }
            ch.eventLoop().execute(new Runnable() {

                @Override
                public void run() {
                    pipeline.addLast(new ServerBootstrapAcceptor(ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
                }
            });
        }
    });
}
Also used : Entry(java.util.Map.Entry) EventLoopGroup(io.netty.channel.EventLoopGroup) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 43 with ChannelHandler

use of io.netty.channel.ChannelHandler in project netty by netty.

the class UdtNetty method main.

public static void main(final String[] args) throws Exception {
    log.info("init");
    TrafficControl.delay(0);
    final AtomicBoolean isOn = new AtomicBoolean(true);
    final InetSocketAddress addr1 = UnitHelp.localSocketAddress();
    final InetSocketAddress addr2 = UnitHelp.localSocketAddress();
    final ChannelHandler handler1 = new EchoMessageHandler(rate, size);
    final ChannelHandler handler2 = new EchoMessageHandler(null, size);
    final NioEventLoopGroup group1 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
    final NioEventLoopGroup group2 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
    final Bootstrap peerBoot1 = new Bootstrap();
    peerBoot1.group(group1).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr1).remoteAddress(addr2).handler(handler1);
    final Bootstrap peerBoot2 = new Bootstrap();
    peerBoot2.group(group2).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr2).remoteAddress(addr1).handler(handler2);
    final ChannelFuture peerFuture1 = peerBoot1.connect();
    final ChannelFuture peerFuture2 = peerBoot2.connect();
    CustomReporter.enable(3, TimeUnit.SECONDS);
    Thread.sleep(time);
    isOn.set(false);
    Thread.sleep(1000);
    peerFuture1.channel().close().sync();
    peerFuture2.channel().close().sync();
    Thread.sleep(1000);
    group1.shutdownGracefully();
    group2.shutdownGracefully();
    Metrics.defaultRegistry().shutdown();
    TrafficControl.delay(0);
    log.info("done");
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InetSocketAddress(java.net.InetSocketAddress) EchoMessageHandler(io.netty.test.udt.util.EchoMessageHandler) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelHandler(io.netty.channel.ChannelHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 44 with ChannelHandler

use of io.netty.channel.ChannelHandler in project netty by netty.

the class AbstractBootstrapConfig method toString.

@Override
public String toString() {
    StringBuilder buf = new StringBuilder().append(StringUtil.simpleClassName(this)).append('(');
    EventLoopGroup group = group();
    if (group != null) {
        buf.append("group: ").append(StringUtil.simpleClassName(group)).append(", ");
    }
    @SuppressWarnings("deprecation") ChannelFactory<? extends C> factory = channelFactory();
    if (factory != null) {
        buf.append("channelFactory: ").append(factory).append(", ");
    }
    SocketAddress localAddress = localAddress();
    if (localAddress != null) {
        buf.append("localAddress: ").append(localAddress).append(", ");
    }
    Map<ChannelOption<?>, Object> options = options();
    if (!options.isEmpty()) {
        buf.append("options: ").append(options).append(", ");
    }
    Map<AttributeKey<?>, Object> attrs = attrs();
    if (!attrs.isEmpty()) {
        buf.append("attrs: ").append(attrs).append(", ");
    }
    ChannelHandler handler = handler();
    if (handler != null) {
        buf.append("handler: ").append(handler).append(", ");
    }
    if (buf.charAt(buf.length() - 1) == '(') {
        buf.append(')');
    } else {
        buf.setCharAt(buf.length() - 2, ')');
        buf.setLength(buf.length() - 1);
    }
    return buf.toString();
}
Also used : AttributeKey(io.netty.util.AttributeKey) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelOption(io.netty.channel.ChannelOption) ChannelHandler(io.netty.channel.ChannelHandler) SocketAddress(java.net.SocketAddress)

Example 45 with ChannelHandler

use of io.netty.channel.ChannelHandler in project netty by netty.

the class HttpServerUpgradeHandlerTest method upgradesPipelineInSameMethodInvocation.

@Test
public void upgradesPipelineInSameMethodInvocation() {
    final HttpServerCodec httpServerCodec = new HttpServerCodec();
    final UpgradeCodecFactory factory = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            return new TestUpgradeCodec();
        }
    };
    ChannelHandler testInStackFrame = new ChannelDuplexHandler() {

        // marker boolean to signal that we're in the `channelRead` method
        private boolean inReadCall;

        private boolean writeUpgradeMessage;

        private boolean writeFlushed;

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            assertFalse(inReadCall);
            assertFalse(writeUpgradeMessage);
            inReadCall = true;
            try {
                super.channelRead(ctx, msg);
                // All in the same call stack, the upgrade codec should receive the message,
                // written the upgrade response, and upgraded the pipeline.
                assertTrue(writeUpgradeMessage);
                assertFalse(writeFlushed);
                assertNull(ctx.pipeline().get(HttpServerCodec.class));
                assertNotNull(ctx.pipeline().get("marker"));
            } finally {
                inReadCall = false;
            }
        }

        @Override
        public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
            // We ensure that we're in the read call and defer the write so we can
            // make sure the pipeline was reformed irrespective of the flush completing.
            assertTrue(inReadCall);
            writeUpgradeMessage = true;
            ctx.channel().eventLoop().execute(new Runnable() {

                @Override
                public void run() {
                    ctx.write(msg, promise);
                }
            });
            promise.addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) {
                    writeFlushed = true;
                }
            });
        }
    };
    HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, factory);
    EmbeddedChannel channel = new EmbeddedChannel(testInStackFrame, httpServerCodec, upgradeHandler);
    String upgradeString = "GET / HTTP/1.1\r\n" + "Host: example.com\r\n" + "Connection: Upgrade, HTTP2-Settings\r\n" + "Upgrade: nextprotocol\r\n" + "HTTP2-Settings: AAMAAABkAAQAAP__\r\n\r\n";
    ByteBuf upgrade = Unpooled.copiedBuffer(upgradeString, CharsetUtil.US_ASCII);
    assertFalse(channel.writeInbound(upgrade));
    assertNull(channel.pipeline().get(HttpServerCodec.class));
    assertNotNull(channel.pipeline().get("marker"));
    channel.flushOutbound();
    ByteBuf upgradeMessage = channel.readOutbound();
    String expectedHttpResponse = "HTTP/1.1 101 Switching Protocols\r\n" + "connection: upgrade\r\n" + "upgrade: nextprotocol\r\n\r\n";
    assertEquals(expectedHttpResponse, upgradeMessage.toString(CharsetUtil.US_ASCII));
    assertTrue(upgradeMessage.release());
    assertFalse(channel.finishAndReleaseAll());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Test(org.junit.jupiter.api.Test)

Aggregations

ChannelHandler (io.netty.channel.ChannelHandler)186 Test (org.junit.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)44 Channel (io.netty.channel.Channel)26 ChannelPipeline (io.netty.channel.ChannelPipeline)25 SslHandler (io.netty.handler.ssl.SslHandler)25 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FilterChainMatchingHandler (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler)20 ChannelFuture (io.netty.channel.ChannelFuture)20 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)20 FilterChainSelector (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector)19 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)18 DownstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext)17 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)17 InetSocketAddress (java.net.InetSocketAddress)16 Test (org.junit.jupiter.api.Test)16 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Bootstrap (io.netty.bootstrap.Bootstrap)11 ArrayList (java.util.ArrayList)11