Example usage for io.netty.handler.codec.http2 Http2Stream close

List of usage examples for io.netty.handler.codec.http2 Http2Stream close

Introduction

In this page you can find the example usage for io.netty.handler.codec.http2 Http2Stream close.

Prototype

Http2Stream close();

Source Link

Document

Closes the stream.

Usage

From source file:com.linecorp.armeria.server.Http2RequestDecoder.java

License:Apache License

@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream)
        throws Http2Exception {

    final DecodedHttpRequest req = requests.get(streamId);
    if (req == null) {
        throw connectionError(PROTOCOL_ERROR, "received a DATA Frame for an unknown stream: %d", streamId);
    }/*from ww w .  j  av  a 2 s .  c  o  m*/

    final int dataLength = data.readableBytes();
    if (dataLength == 0) {
        // Received an empty DATA frame
        if (endOfStream) {
            req.close();
        }
        return padding;
    }

    req.increaseTransferredBytes(dataLength);

    final long maxContentLength = req.maxRequestLength();
    if (maxContentLength > 0 && req.transferredBytes() > maxContentLength) {
        if (req.isOpen()) {
            req.close(ContentTooLargeException.get());
        }

        if (isWritable(streamId)) {
            writeErrorResponse(ctx, streamId, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE);
        } else {
            // Cannot write to the stream. Just close it.
            final Http2Stream stream = writer.connection().stream(streamId);
            stream.close();
        }
    } else if (req.isOpen()) {
        try {
            req.write(new ByteBufHttpData(data.retain(), endOfStream));
        } catch (Throwable t) {
            req.close(t);
            throw connectionError(INTERNAL_ERROR, t, "failed to consume a DATA frame");
        }

        if (endOfStream) {
            req.close();
        }
    }

    // All bytes have been processed.
    return dataLength + padding;
}

From source file:io.grpc.netty.NettyClientHandler.java

License:Apache License

private void forcefulClose(final ChannelHandlerContext ctx, final ForcefulCloseCommand msg,
        ChannelPromise promise) throws Exception {
    // close() already called by NettyClientTransport, so just need to clean up streams
    connection().forEachActiveStream(new Http2StreamVisitor() {
        @Override/* w w  w  .  ja va2 s .  c o  m*/
        public boolean visit(Http2Stream stream) throws Http2Exception {
            NettyClientStream.TransportState clientStream = clientStream(stream);
            Tag tag = clientStream != null ? clientStream.tag() : PerfMark.createTag();
            PerfMark.startTask("NettyClientHandler.forcefulClose", tag);
            PerfMark.linkIn(msg.getLink());
            try {
                if (clientStream != null) {
                    clientStream.transportReportStatus(msg.getStatus(), true, new Metadata());
                    resetStream(ctx, stream.id(), Http2Error.CANCEL.code(), ctx.newPromise());
                }
                stream.close();
                return true;
            } finally {
                PerfMark.stopTask("NettyClientHandler.forcefulClose", tag);
            }
        }
    });
    promise.setSuccess();
}

From source file:io.grpc.netty.NettyClientHandler.java

License:Apache License

/**
 * Handler for a GOAWAY being received. Fails any streams created after the
 * last known stream.//from  w ww .ja v a2  s.com
 */
private void goingAway(Status status) {
    lifecycleManager.notifyShutdown(status);
    final Status goAwayStatus = lifecycleManager.getShutdownStatus();
    final int lastKnownStream = connection().local().lastStreamKnownByPeer();
    try {
        connection().forEachActiveStream(new Http2StreamVisitor() {
            @Override
            public boolean visit(Http2Stream stream) throws Http2Exception {
                if (stream.id() > lastKnownStream) {
                    NettyClientStream.TransportState clientStream = clientStream(stream);
                    if (clientStream != null) {
                        clientStream.transportReportStatus(goAwayStatus, RpcProgress.REFUSED, false,
                                new Metadata());
                    }
                    stream.close();
                }
                return true;
            }
        });
    } catch (Http2Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:io.grpc.netty.NettyClientHandlerTest.java

License:Apache License

@Override
protected NettyClientHandler newHandler() throws Http2Exception {
    Http2Connection connection = new DefaultHttp2Connection(false);

    // Create and close a stream previous to the nextStreamId.
    Http2Stream stream = connection.local().createStream(streamId - 2, true);
    stream.close();

    final Ticker ticker = new Ticker() {
        @Override/*  w w w  .  j  av a 2 s  .c o m*/
        public long read() {
            return nanoTime;
        }
    };
    Supplier<Stopwatch> stopwatchSupplier = new Supplier<Stopwatch>() {
        @Override
        public Stopwatch get() {
            return Stopwatch.createUnstarted(ticker);
        }
    };
    return NettyClientHandler.newHandler(connection, frameReader(), frameWriter(), lifecycleManager,
            mockKeepAliveManager, flowControlWindow, maxHeaderListSize, stopwatchSupplier, tooManyPingsRunnable,
            transportTracer, Attributes.EMPTY, "someauthority");
}

From source file:io.grpc.netty.NettyServerHandler.java

License:Apache License

private void forcefulClose(final ChannelHandlerContext ctx, final ForcefulCloseCommand msg,
        ChannelPromise promise) throws Exception {
    super.close(ctx, promise);
    connection().forEachActiveStream(new Http2StreamVisitor() {
        @Override/*from  w  w w.j a v a 2s. c  o m*/
        public boolean visit(Http2Stream stream) throws Http2Exception {
            NettyServerStream.TransportState serverStream = serverStream(stream);
            if (serverStream != null) {
                PerfMark.startTask("NettyServerHandler.forcefulClose", serverStream.tag());
                PerfMark.linkIn(msg.getLink());
                try {
                    serverStream.transportReportStatus(msg.getStatus());
                    resetStream(ctx, stream.id(), Http2Error.CANCEL.code(), ctx.newPromise());
                } finally {
                    PerfMark.stopTask("NettyServerHandler.forcefulClose", serverStream.tag());
                }
            }
            stream.close();
            return true;
        }
    });
}