Example usage for io.netty.handler.codec.http2 Http2ConnectionHandler encoder

List of usage examples for io.netty.handler.codec.http2 Http2ConnectionHandler encoder

Introduction

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

Prototype

Http2ConnectionEncoder encoder

To view the source code for io.netty.handler.codec.http2 Http2ConnectionHandler encoder.

Click Source Link

Usage

From source file:com.linecorp.armeria.client.http.HttpSessionHandler.java

License:Apache License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof SessionProtocol) {
        assert protocol == null;
        assert responseDecoder == null;

        sessionTimeoutFuture.cancel(false);

        // Set the current protocol and its associated WaitsHolder implementation.
        final SessionProtocol protocol = (SessionProtocol) evt;
        this.protocol = protocol;
        switch (protocol) {
        case H1:/*from w  w w . ja  va  2  s .c  om*/
        case H1C:
            requestEncoder = new Http1ObjectEncoder(false);
            responseDecoder = ctx.pipeline().get(Http1ResponseDecoder.class);
            break;
        case H2:
        case H2C:
            final Http2ConnectionHandler handler = ctx.pipeline().get(Http2ConnectionHandler.class);
            requestEncoder = new Http2ObjectEncoder(handler.encoder());
            responseDecoder = ctx.pipeline().get(Http2ClientConnectionHandler.class).responseDecoder();
            break;
        default:
            throw new Error(); // Should never reach here.
        }

        if (!sessionPromise.trySuccess(ctx.channel())) {
            // Session creation has been failed already; close the connection.
            ctx.close();
        }
        return;
    }

    if (evt instanceof SessionProtocolNegotiationException) {
        sessionTimeoutFuture.cancel(false);
        sessionPromise.tryFailure((SessionProtocolNegotiationException) evt);
        ctx.close();
        return;
    }

    logger.warn("{} Unexpected user event: {}", ctx.channel(), evt);
}

From source file:com.linecorp.armeria.client.HttpSessionHandler.java

License:Apache License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof SessionProtocol) {
        assert protocol == null;
        assert responseDecoder == null;

        sessionTimeoutFuture.cancel(false);

        // Set the current protocol and its associated WaitsHolder implementation.
        final SessionProtocol protocol = (SessionProtocol) evt;
        this.protocol = protocol;
        if (protocol == H1 || protocol == H1C) {
            requestEncoder = new Http1ObjectEncoder(false, protocol.isTls());
            responseDecoder = ctx.pipeline().get(Http1ResponseDecoder.class);
        } else if (protocol == H2 || protocol == H2C) {
            final Http2ConnectionHandler handler = ctx.pipeline().get(Http2ConnectionHandler.class);
            requestEncoder = new Http2ObjectEncoder(handler.encoder());
            responseDecoder = ctx.pipeline().get(Http2ClientConnectionHandler.class).responseDecoder();
        } else {//from w ww. j av  a2 s  .  c  o  m
            throw new Error(); // Should never reach here.
        }

        if (!sessionPromise.trySuccess(ctx.channel())) {
            // Session creation has been failed already; close the connection.
            ctx.close();
        }
        return;
    }

    if (evt instanceof SessionProtocolNegotiationException) {
        sessionTimeoutFuture.cancel(false);
        sessionPromise.tryFailure((SessionProtocolNegotiationException) evt);
        ctx.close();
        return;
    }

    if (evt instanceof Http2ConnectionPrefaceAndSettingsFrameWrittenEvent
            || evt instanceof SslCloseCompletionEvent || evt instanceof ChannelInputShutdownReadComplete) {
        // Expected events
        return;
    }

    logger.warn("{} Unexpected user event: {}", ctx.channel(), evt);
}

From source file:com.linecorp.armeria.server.http.HttpServerHandler.java

License:Apache License

private void handleHttp2Settings(ChannelHandlerContext ctx, Http2Settings h2settings) {
    if (h2settings.isEmpty()) {
        logger.trace("{} HTTP/2 settings: <empty>", ctx.channel());
    } else {/*from  w w w  . j  a  v  a  2s . com*/
        logger.debug("{} HTTP/2 settings: {}", ctx.channel(), h2settings);
    }

    switch (protocol) {
    case H1:
        protocol = SessionProtocol.H2;
        break;
    case H1C:
        protocol = SessionProtocol.H2C;
        break;
    }

    final Http2ConnectionHandler handler = ctx.pipeline().get(Http2ConnectionHandler.class);
    if (responseEncoder != null) {
        responseEncoder.close();
    }
    responseEncoder = new Http2ObjectEncoder(handler.encoder());
}

From source file:com.linecorp.armeria.server.http.HttpServerPipelineConfigurator.java

License:Apache License

private Http2ConnectionHandler newHttp2ConnectionHandler(ChannelPipeline pipeline) {

    final Http2Connection conn = new DefaultHttp2Connection(true);
    conn.addListener(new Http2GoAwayListener(pipeline.channel()));

    Http2FrameReader reader = new DefaultHttp2FrameReader(true);
    Http2FrameWriter writer = new DefaultHttp2FrameWriter();

    Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(conn, writer);
    Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(conn, encoder, reader);

    final Http2ConnectionHandler handler = new Http2ServerConnectionHandler(decoder, encoder,
            new Http2Settings());

    // Setup post build options
    final Http2RequestDecoder listener = new Http2RequestDecoder(pipeline.channel(), handler.encoder());

    handler.connection().addListener(listener);
    handler.decoder().frameListener(listener);
    handler.gracefulShutdownTimeoutMillis(config.idleTimeoutMillis());

    return handler;
}

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

License:Apache License

private void handleHttp2Settings(ChannelHandlerContext ctx, Http2Settings h2settings) {
    if (h2settings.isEmpty()) {
        logger.trace("{} HTTP/2 settings: <empty>", ctx.channel());
    } else {/*w w  w .  jav a  2s .  c  om*/
        logger.debug("{} HTTP/2 settings: {}", ctx.channel(), h2settings);
    }

    if (protocol == H1) {
        protocol = H2;
    } else if (protocol == H1C) {
        protocol = H2C;
    }

    final Http2ConnectionHandler handler = ctx.pipeline().get(Http2ConnectionHandler.class);
    if (responseEncoder == null) {
        responseEncoder = new Http2ObjectEncoder(handler.encoder());
    } else if (responseEncoder instanceof Http1ObjectEncoder) {
        responseEncoder.close();
        responseEncoder = new Http2ObjectEncoder(handler.encoder());
    }
}

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

License:Apache License

private Http2ConnectionHandler newHttp2ConnectionHandler(ChannelPipeline pipeline) {

    final Http2Connection conn = new DefaultHttp2Connection(true);
    conn.addListener(new Http2GoAwayListener(pipeline.channel()));

    final Http2FrameReader reader = new DefaultHttp2FrameReader(true);
    final Http2FrameWriter writer = new DefaultHttp2FrameWriter();

    final Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(conn, writer);
    final Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(conn, encoder, reader);

    final Http2ConnectionHandler handler = new Http2ServerConnectionHandler(decoder, encoder,
            new Http2Settings());

    // Setup post build options
    final Http2RequestDecoder listener = new Http2RequestDecoder(config, pipeline.channel(), handler.encoder());

    handler.connection().addListener(listener);
    handler.decoder().frameListener(listener);
    handler.gracefulShutdownTimeoutMillis(config.idleTimeoutMillis());

    return handler;
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.http2.InboundHttp2ServerInitializer.java

License:Open Source License

/**
 * Channel initialization// ww w  . ja  v  a 2 s . c om
 *
 * @param ch
 */
@Override
public void initChannel(SocketChannel ch) {
    Http2Connection conn = new DefaultHttp2Connection(true);
    Http2FrameListenAdapter listenAdapter = new Http2FrameListenAdapter();
    Http2ConnectionHandler connectionHandler = new Http2ConnectionHandlerBuilder().connection(conn)
            .frameLogger(logger).frameListener(listenAdapter).build();
    InboundHttp2SourceHandler sourceHandler = new InboundHttp2SourceHandler(this.config, conn,
            connectionHandler.encoder());
    if (sslCtx != null) {
        configureSsl(ch, connectionHandler, sourceHandler);
    } else {
        configureClearText(ch, connectionHandler, sourceHandler);
    }
}