Search in sources :

Example 51 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project grpc-java by grpc.

the class NettyServerHandler method closeStreamWhenDone.

private void closeStreamWhenDone(ChannelPromise promise, int streamId) throws Http2Exception {
    final NettyServerStream.TransportState stream = serverStream(requireHttp2Stream(streamId));
    promise.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) {
            stream.complete();
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 52 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project grpc-java by grpc.

the class NettyServerTransport method start.

public void start(ServerTransportListener listener) {
    Preconditions.checkState(this.listener == null, "Handler already registered");
    this.listener = listener;
    // Create the Netty handler for the pipeline.
    final NettyServerHandler grpcHandler = createHandler(listener);
    HandlerSettings.setAutoWindow(grpcHandler);
    // Notify when the channel closes.
    channel.closeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            notifyTerminated(grpcHandler.connectionError());
        }
    });
    ChannelHandler negotiationHandler = protocolNegotiator.newHandler(grpcHandler);
    channel.pipeline().addLast(negotiationHandler);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelHandler(io.netty.channel.ChannelHandler) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 53 with ChannelFutureListener

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

the class SpdyFrameCodec method handlerAdded.

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    super.handlerAdded(ctx);
    this.ctx = ctx;
    ctx.channel().closeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            spdyHeaderBlockDecoder.end();
            spdyHeaderBlockEncoder.end();
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException)

Example 54 with ChannelFutureListener

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

the class WebSocketServerHandshaker method handshake.

/**
     * Performs the opening handshake
     *
     * When call this method you <strong>MUST NOT</strong> retain the {@link FullHttpRequest} which is passed in.
     *
     * @param channel
     *            Channel
     * @param req
     *            HTTP Request
     * @param responseHeaders
     *            Extra headers to add to the handshake response or {@code null} if no extra headers should be added
     * @param promise
     *            the {@link ChannelPromise} to be notified when the opening handshake is done
     * @return future
     *            the {@link ChannelFuture} which is notified when the opening handshake is done
     */
public final ChannelFuture handshake(Channel channel, FullHttpRequest req, HttpHeaders responseHeaders, final ChannelPromise promise) {
    if (logger.isDebugEnabled()) {
        logger.debug("{} WebSocket version {} server handshake", channel, version());
    }
    FullHttpResponse response = newHandshakeResponse(req, responseHeaders);
    ChannelPipeline p = channel.pipeline();
    if (p.get(HttpObjectAggregator.class) != null) {
        p.remove(HttpObjectAggregator.class);
    }
    if (p.get(HttpContentCompressor.class) != null) {
        p.remove(HttpContentCompressor.class);
    }
    ChannelHandlerContext ctx = p.context(HttpRequestDecoder.class);
    final String encoderName;
    if (ctx == null) {
        // this means the user use a HttpServerCodec
        ctx = p.context(HttpServerCodec.class);
        if (ctx == null) {
            promise.setFailure(new IllegalStateException("No HttpDecoder and no HttpServerCodec in the pipeline"));
            return promise;
        }
        p.addBefore(ctx.name(), "wsdecoder", newWebsocketDecoder());
        p.addBefore(ctx.name(), "wsencoder", newWebSocketEncoder());
        encoderName = ctx.name();
    } else {
        p.replace(ctx.name(), "wsdecoder", newWebsocketDecoder());
        encoderName = p.context(HttpResponseEncoder.class).name();
        p.addBefore(encoderName, "wsencoder", newWebSocketEncoder());
    }
    channel.writeAndFlush(response).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                ChannelPipeline p = future.channel().pipeline();
                p.remove(encoderName);
                promise.setSuccess();
            } else {
                promise.setFailure(future.cause());
            }
        }
    });
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelPipeline(io.netty.channel.ChannelPipeline) ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 55 with ChannelFutureListener

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

the class MessageAggregator method decode.

@Override
protected void decode(final ChannelHandlerContext ctx, I msg, List<Object> out) throws Exception {
    if (isStartMessage(msg)) {
        handlingOversizedMessage = false;
        if (currentMessage != null) {
            currentMessage.release();
            currentMessage = null;
            throw new MessageAggregationException();
        }
        @SuppressWarnings("unchecked") S m = (S) msg;
        // Send the continue response if necessary (e.g. 'Expect: 100-continue' header)
        // Check before content length. Failing an expectation may result in a different response being sent.
        Object continueResponse = newContinueResponse(m, maxContentLength, ctx.pipeline());
        if (continueResponse != null) {
            // Cache the write listener for reuse.
            ChannelFutureListener listener = continueResponseWriteListener;
            if (listener == null) {
                continueResponseWriteListener = listener = new ChannelFutureListener() {

                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        if (!future.isSuccess()) {
                            ctx.fireExceptionCaught(future.cause());
                        }
                    }
                };
            }
            // Make sure to call this before writing, otherwise reference counts may be invalid.
            boolean closeAfterWrite = closeAfterContinueResponse(continueResponse);
            handlingOversizedMessage = ignoreContentAfterContinueResponse(continueResponse);
            final ChannelFuture future = ctx.writeAndFlush(continueResponse).addListener(listener);
            if (closeAfterWrite) {
                future.addListener(ChannelFutureListener.CLOSE);
                return;
            }
            if (handlingOversizedMessage) {
                return;
            }
        } else if (isContentLengthInvalid(m, maxContentLength)) {
            // if content length is set, preemptively close if it's too large
            invokeHandleOversizedMessage(ctx, m);
            return;
        }
        if (m instanceof DecoderResultProvider && !((DecoderResultProvider) m).decoderResult().isSuccess()) {
            O aggregated;
            if (m instanceof ByteBufHolder && ((ByteBufHolder) m).content().isReadable()) {
                aggregated = beginAggregation(m, ((ByteBufHolder) m).content().retain());
            } else {
                aggregated = beginAggregation(m, EMPTY_BUFFER);
            }
            finishAggregation(aggregated);
            out.add(aggregated);
            return;
        }
        // A streamed message - initialize the cumulative buffer, and wait for incoming chunks.
        CompositeByteBuf content = ctx.alloc().compositeBuffer(maxCumulationBufferComponents);
        if (m instanceof ByteBufHolder) {
            appendPartialContent(content, ((ByteBufHolder) m).content());
        }
        currentMessage = beginAggregation(m, content);
    } else if (isContentMessage(msg)) {
        if (currentMessage == null) {
            // until the begging of the next request/response.
            return;
        }
        // Merge the received chunk into the content of the current message.
        CompositeByteBuf content = (CompositeByteBuf) currentMessage.content();
        @SuppressWarnings("unchecked") final C m = (C) msg;
        // Handle oversized message.
        if (content.readableBytes() > maxContentLength - m.content().readableBytes()) {
            // By convention, full message type extends first message type.
            @SuppressWarnings("unchecked") S s = (S) currentMessage;
            invokeHandleOversizedMessage(ctx, s);
            return;
        }
        // Append the content of the chunk.
        appendPartialContent(content, m.content());
        // Give the subtypes a chance to merge additional information such as trailing headers.
        aggregate(currentMessage, m);
        final boolean last;
        if (m instanceof DecoderResultProvider) {
            DecoderResult decoderResult = ((DecoderResultProvider) m).decoderResult();
            if (!decoderResult.isSuccess()) {
                if (currentMessage instanceof DecoderResultProvider) {
                    ((DecoderResultProvider) currentMessage).setDecoderResult(DecoderResult.failure(decoderResult.cause()));
                }
                last = true;
            } else {
                last = isLastContentMessage(m);
            }
        } else {
            last = isLastContentMessage(m);
        }
        if (last) {
            finishAggregation(currentMessage);
            // All done
            out.add(currentMessage);
            currentMessage = null;
        }
    } else {
        throw new MessageAggregationException();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBufHolder(io.netty.buffer.ByteBufHolder)

Aggregations

ChannelFutureListener (io.netty.channel.ChannelFutureListener)92 ChannelFuture (io.netty.channel.ChannelFuture)87 Channel (io.netty.channel.Channel)29 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)24 Bootstrap (io.netty.bootstrap.Bootstrap)20 Test (org.junit.Test)19 ByteBuf (io.netty.buffer.ByteBuf)18 ClosedChannelException (java.nio.channels.ClosedChannelException)17 CountDownLatch (java.util.concurrent.CountDownLatch)16 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)15 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)15 IOException (java.io.IOException)12 ChannelPromise (io.netty.channel.ChannelPromise)11 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)11 ConnectException (java.net.ConnectException)11 InetSocketAddress (java.net.InetSocketAddress)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 AbstractChannel (io.netty.channel.AbstractChannel)6 EventLoopGroup (io.netty.channel.EventLoopGroup)6 ChannelPipeline (io.netty.channel.ChannelPipeline)5