Example usage for io.netty.handler.codec.http2 StreamBufferingEncoder connection

List of usage examples for io.netty.handler.codec.http2 StreamBufferingEncoder connection

Introduction

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

Prototype

@Override
    public Http2Connection connection() 

Source Link

Usage

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

License:Apache License

private NettyClientHandler(Http2ConnectionDecoder decoder, StreamBufferingEncoder encoder,
        Http2Settings settings, ClientTransportLifecycleManager lifecycleManager,
        KeepAliveManager keepAliveManager, Supplier<Stopwatch> stopwatchFactory,
        final Runnable tooManyPingsRunnable, TransportTracer transportTracer, Attributes eagAttributes,
        String authority) {/*from   www  .j a  v a 2 s.c o  m*/
    super(/* channelUnused= */ null, decoder, encoder, settings);
    this.lifecycleManager = lifecycleManager;
    this.keepAliveManager = keepAliveManager;
    this.stopwatchFactory = stopwatchFactory;
    this.transportTracer = Preconditions.checkNotNull(transportTracer);
    this.eagAttributes = eagAttributes;
    this.authority = authority;
    this.attributes = Attributes.newBuilder().set(GrpcAttributes.ATTR_CLIENT_EAG_ATTRS, eagAttributes).build();

    // Set the frame listener on the decoder.
    decoder().frameListener(new FrameListener());

    Http2Connection connection = encoder.connection();
    streamKey = connection.newKey();

    connection.addListener(new Http2ConnectionAdapter() {
        @Override
        public void onGoAwayReceived(int lastStreamId, long errorCode, ByteBuf debugData) {
            byte[] debugDataBytes = ByteBufUtil.getBytes(debugData);
            goingAway(statusFromGoAway(errorCode, debugDataBytes));
            if (errorCode == Http2Error.ENHANCE_YOUR_CALM.code()) {
                String data = new String(debugDataBytes, UTF_8);
                logger.log(Level.WARNING, "Received GOAWAY with ENHANCE_YOUR_CALM. Debug data: {1}", data);
                if ("too_many_pings".equals(data)) {
                    tooManyPingsRunnable.run();
                }
            }
        }

        @Override
        public void onStreamActive(Http2Stream stream) {
            if (connection().numActiveStreams() == 1 && NettyClientHandler.this.keepAliveManager != null) {
                NettyClientHandler.this.keepAliveManager.onTransportActive();
            }
        }

        @Override
        public void onStreamClosed(Http2Stream stream) {
            // Although streams with CALL_OPTIONS_RPC_OWNED_BY_BALANCER are not marked as "in-use" in
            // the first place, we don't propagate that option here, and it's safe to reset the in-use
            // state for them, which will be a cheap no-op.
            inUseState.updateObjectInUse(stream, false);
            if (connection().numActiveStreams() == 0 && NettyClientHandler.this.keepAliveManager != null) {
                NettyClientHandler.this.keepAliveManager.onTransportIdle();
            }
        }
    });
}