Search in sources :

Example 36 with ChannelPipeline

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

the class LocalServerChannel method serve0.

private void serve0(final LocalChannel child) {
    inboundBuffer.add(child);
    if (acceptInProgress) {
        acceptInProgress = false;
        ChannelPipeline pipeline = pipeline();
        for (; ; ) {
            Object m = inboundBuffer.poll();
            if (m == null) {
                break;
            }
            pipeline.fireChannelRead(m);
        }
        pipeline.fireChannelReadComplete();
    }
}
Also used : ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 37 with ChannelPipeline

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

the class EmbeddedChannel method writeInbound.

/**
     * Write messages to the inbound of this {@link Channel}.
     *
     * @param msgs the messages to be written
     *
     * @return {@code true} if the write operation did add something to the inbound buffer
     */
public boolean writeInbound(Object... msgs) {
    ensureOpen();
    if (msgs.length == 0) {
        return isNotEmpty(inboundMessages);
    }
    ChannelPipeline p = pipeline();
    for (Object m : msgs) {
        p.fireChannelRead(m);
    }
    flushInbound(false, voidPromise());
    return isNotEmpty(inboundMessages);
}
Also used : ChannelPipeline(io.netty.channel.ChannelPipeline) DefaultChannelPipeline(io.netty.channel.DefaultChannelPipeline)

Example 38 with ChannelPipeline

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

the class WebSocketClientHandshaker method processHandshake.

/**
     * Process the opening handshake initiated by {@link #handshake}}.
     *
     * @param channel
     *            Channel
     * @param response
     *            HTTP response containing the closing handshake details
     * @param promise
     *            the {@link ChannelPromise} to notify once the handshake completes.
     * @return future
     *            the {@link ChannelFuture} which is notified once the handshake completes.
     */
public final ChannelFuture processHandshake(final Channel channel, HttpResponse response, final ChannelPromise promise) {
    if (response instanceof FullHttpResponse) {
        try {
            finishHandshake(channel, (FullHttpResponse) response);
            promise.setSuccess();
        } catch (Throwable cause) {
            promise.setFailure(cause);
        }
    } else {
        ChannelPipeline p = channel.pipeline();
        ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
        if (ctx == null) {
            ctx = p.context(HttpClientCodec.class);
            if (ctx == null) {
                return promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "a HttpResponseDecoder or HttpClientCodec"));
            }
        }
        // Add aggregator and ensure we feed the HttpResponse so it is aggregated. A limit of 8192 should be more
        // then enough for the websockets handshake payload.
        //
        // TODO: Make handshake work without HttpObjectAggregator at all.
        String aggregatorName = "httpAggregator";
        p.addAfter(ctx.name(), aggregatorName, new HttpObjectAggregator(8192));
        p.addAfter(aggregatorName, "handshaker", new SimpleChannelInboundHandler<FullHttpResponse>() {

            @Override
            protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
                // Remove ourself and do the actual handshake
                ctx.pipeline().remove(this);
                try {
                    finishHandshake(channel, msg);
                    promise.setSuccess();
                } catch (Throwable cause) {
                    promise.setFailure(cause);
                }
            }

            @Override
            public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                // Remove ourself and fail the handshake promise.
                ctx.pipeline().remove(this);
                promise.setFailure(cause);
            }

            @Override
            public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                // Fail promise if Channel was closed
                promise.tryFailure(CLOSED_CHANNEL_EXCEPTION);
                ctx.fireChannelInactive();
            }
        });
        try {
            ctx.fireChannelRead(ReferenceCountUtil.retain(response));
        } catch (Throwable cause) {
            promise.setFailure(cause);
        }
    }
    return promise;
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 39 with ChannelPipeline

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

the class WebSocketClientHandshaker method handshake.

/**
     * Begins the opening handshake
     *
     * @param channel
     *            Channel
     * @param promise
     *            the {@link ChannelPromise} to be notified when the opening handshake is sent
     */
public final ChannelFuture handshake(Channel channel, final ChannelPromise promise) {
    FullHttpRequest request = newHandshakeRequest();
    HttpResponseDecoder decoder = channel.pipeline().get(HttpResponseDecoder.class);
    if (decoder == null) {
        HttpClientCodec codec = channel.pipeline().get(HttpClientCodec.class);
        if (codec == null) {
            promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "a HttpResponseDecoder or HttpClientCodec"));
            return promise;
        }
    }
    channel.writeAndFlush(request).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                ChannelPipeline p = future.channel().pipeline();
                ChannelHandlerContext ctx = p.context(HttpRequestEncoder.class);
                if (ctx == null) {
                    ctx = p.context(HttpClientCodec.class);
                }
                if (ctx == null) {
                    promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "a HttpRequestEncoder or HttpClientCodec"));
                    return;
                }
                p.addAfter(ctx.name(), "ws-encoder", newWebSocketEncoder());
                promise.setSuccess();
            } else {
                promise.setFailure(future.cause());
            }
        }
    });
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 40 with ChannelPipeline

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

the class Http2FrameWriterBenchmark method boostrapEnvWithTransport.

private static Environment boostrapEnvWithTransport(final EnvironmentType environmentType) {
    final EnvironmentParameters params = environmentType.params();
    ServerBootstrap sb = new ServerBootstrap();
    Bootstrap cb = new Bootstrap();
    final TransportEnvironment environment = new TransportEnvironment(cb, sb);
    EventLoopGroup serverEventLoopGroup = params.newEventLoopGroup();
    sb.group(serverEventLoopGroup, serverEventLoopGroup);
    sb.channel(params.serverChannelClass());
    sb.option(ChannelOption.ALLOCATOR, params.serverAllocator());
    sb.childOption(ChannelOption.ALLOCATOR, params.serverAllocator());
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
        }
    });
    cb.group(params.newEventLoopGroup());
    cb.channel(params.clientChannelClass());
    cb.option(ChannelOption.ALLOCATOR, params.clientAllocator());
    final CountDownLatch latch = new CountDownLatch(1);
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            Http2Connection connection = new DefaultHttp2Connection(false);
            Http2RemoteFlowController remoteFlowController = params.remoteFlowController();
            if (remoteFlowController != null) {
                connection.remote().flowController(params.remoteFlowController());
            }
            Http2LocalFlowController localFlowController = params.localFlowController();
            if (localFlowController != null) {
                connection.local().flowController(localFlowController);
            }
            environment.writer(new DefaultHttp2FrameWriter());
            Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, environment.writer());
            Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, new DefaultHttp2FrameReader());
            Http2ConnectionHandler connectionHandler = new Http2ConnectionHandlerBuilder().encoderEnforceMaxConcurrentStreams(false).frameListener(new Http2FrameAdapter()).codec(decoder, encoder).build();
            p.addLast(connectionHandler);
            environment.context(p.lastContext());
            // Must wait for context to be set.
            latch.countDown();
        }
    });
    environment.serverChannel(sb.bind(params.address()));
    params.address(environment.serverChannel().localAddress());
    environment.clientChannel(cb.connect(params.address()));
    try {
        if (!latch.await(5, SECONDS)) {
            throw new RuntimeException("Channel did not initialize in time");
        }
    } catch (InterruptedException ie) {
        throw new RuntimeException(ie);
    }
    return environment;
}
Also used : Http2FrameAdapter(io.netty.handler.codec.http2.Http2FrameAdapter) DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) Http2Connection(io.netty.handler.codec.http2.Http2Connection) Http2ConnectionHandler(io.netty.handler.codec.http2.Http2ConnectionHandler) DefaultHttp2ConnectionEncoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder) Http2RemoteFlowController(io.netty.handler.codec.http2.Http2RemoteFlowController) Http2ConnectionDecoder(io.netty.handler.codec.http2.Http2ConnectionDecoder) DefaultHttp2ConnectionDecoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionDecoder) Http2ConnectionHandlerBuilder(io.netty.handler.codec.http2.Http2ConnectionHandlerBuilder) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) DefaultHttp2FrameReader(io.netty.handler.codec.http2.DefaultHttp2FrameReader) DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) OioServerSocketChannel(io.netty.channel.socket.oio.OioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) OioSocketChannel(io.netty.channel.socket.oio.OioSocketChannel) Channel(io.netty.channel.Channel) DefaultHttp2ConnectionDecoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionDecoder) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultHttp2FrameWriter(io.netty.handler.codec.http2.DefaultHttp2FrameWriter) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) Http2LocalFlowController(io.netty.handler.codec.http2.Http2LocalFlowController) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder) DefaultHttp2ConnectionEncoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder)

Aggregations

ChannelPipeline (io.netty.channel.ChannelPipeline)161 SocketChannel (io.netty.channel.socket.SocketChannel)42 Channel (io.netty.channel.Channel)40 Bootstrap (io.netty.bootstrap.Bootstrap)38 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)38 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)36 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)36 ChannelFuture (io.netty.channel.ChannelFuture)33 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)28 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)28 EventLoopGroup (io.netty.channel.EventLoopGroup)25 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)25 SslHandler (io.netty.handler.ssl.SslHandler)21 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)18 InetSocketAddress (java.net.InetSocketAddress)17 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)16 LoggingHandler (io.netty.handler.logging.LoggingHandler)16 ChannelHandler (io.netty.channel.ChannelHandler)15 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)15 IOException (java.io.IOException)15