Example usage for io.netty.handler.codec.http2 Http2StreamVisitor Http2StreamVisitor

List of usage examples for io.netty.handler.codec.http2 Http2StreamVisitor Http2StreamVisitor

Introduction

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

Prototype

Http2StreamVisitor

Source Link

Usage

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

License:Apache License

/**
 * Handler for the Channel shutting down.
 */// w ww  .  j a v a  2s  .  com
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    try {
        logger.fine("Network channel is closed");
        Status status = Status.UNAVAILABLE.withDescription("Network closed for unknown reason");
        lifecycleManager.notifyShutdown(status);
        try {
            cancelPing(lifecycleManager.getShutdownThrowable());
            // Report status to the application layer for any open streams
            connection().forEachActiveStream(new Http2StreamVisitor() {
                @Override
                public boolean visit(Http2Stream stream) throws Http2Exception {
                    NettyClientStream.TransportState clientStream = clientStream(stream);
                    if (clientStream != null) {
                        clientStream.transportReportStatus(lifecycleManager.getShutdownStatus(), false,
                                new Metadata());
                    }
                    return true;
                }
            });
        } finally {
            lifecycleManager.notifyTerminated(status);
        }
    } finally {
        // Close any open streams
        super.channelInactive(ctx);
        if (keepAliveManager != null) {
            keepAliveManager.onTransportTermination();
        }
    }
}

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/*from ww  w . j  a 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.//ww  w. jav  a 2  s  .c o m
 */
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.NettyServerHandler.java

License:Apache License

/**
 * Handler for the Channel shutting down.
 *//*  w  w w .j  a  v  a 2 s .  com*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    try {
        if (keepAliveManager != null) {
            keepAliveManager.onTransportTermination();
        }
        if (maxConnectionIdleManager != null) {
            maxConnectionIdleManager.onTransportTermination();
        }
        if (maxConnectionAgeMonitor != null) {
            maxConnectionAgeMonitor.cancel(false);
        }
        final Status status = Status.UNAVAILABLE.withDescription("connection terminated for unknown reason");
        // Any streams that are still active must be closed
        connection().forEachActiveStream(new Http2StreamVisitor() {
            @Override
            public boolean visit(Http2Stream stream) throws Http2Exception {
                NettyServerStream.TransportState serverStream = serverStream(stream);
                if (serverStream != null) {
                    serverStream.transportReportStatus(status);
                }
                return true;
            }
        });
    } finally {
        super.channelInactive(ctx);
    }
}

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 2 s.  co  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;
        }
    });
}