Example usage for io.netty.handler.codec.http2 Http2CodecUtil connectionPrefaceBuf

List of usage examples for io.netty.handler.codec.http2 Http2CodecUtil connectionPrefaceBuf

Introduction

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

Prototype

public static ByteBuf connectionPrefaceBuf() 

Source Link

Document

Returns a buffer containing the #CONNECTION_PREFACE .

Usage

From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    // //  w  ww. j  ava  2 s .  co  m
    Http2Settings settings = new Http2Settings();
    settings.initialWindowSize(DEFAULT_FLOW_CONTROL_WINDOW);
    settings.pushEnabled(false);
    settings.maxConcurrentStreams(0);
    ByteBuf preface = Http2CodecUtil.connectionPrefaceBuf().retainedDuplicate();
    ctx.write(preface);
    writer.writeSettings(ctx, settings, ctx.newPromise());
    writer.writeWindowUpdate(ctx, 0, 983041, ctx.newPromise());
    ctx.flush();
    ctx.read();
}

From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java

License:Apache License

private boolean readClientPrefaceString(ByteBuf in) throws Http2Exception {
    ByteBuf clientPrefaceString = Http2CodecUtil.connectionPrefaceBuf();
    int prefaceRemaining = clientPrefaceString.readableBytes();
    int bytesRead = min(in.readableBytes(), prefaceRemaining);

    // If the input so far doesn't match the preface, break the connection.
    if (bytesRead == 0 || !ByteBufUtil.equals(in, in.readerIndex(), clientPrefaceString,
            clientPrefaceString.readerIndex(), bytesRead)) {
        String receivedBytes = hexDump(in, in.readerIndex(),
                min(in.readableBytes(), clientPrefaceString.readableBytes()));
        throw connectionError(PROTOCOL_ERROR,
                "HTTP/2 client preface string missing or corrupt. " + "Hex dump for received bytes: %s",
                receivedBytes);/*from w  w w. ja  v a  2 s  . c  o m*/
    }
    in.skipBytes(bytesRead);
    clientPrefaceString.skipBytes(bytesRead);

    if (!clientPrefaceString.isReadable()) {
        // Entire preface has been read.
        clientPrefaceString.release();
        return true;
    }
    return false;
}

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

License:Apache License

@Override
protected void manualSetUp() throws Exception {
    assertNull("manualSetUp should not run more than once", handler());

    initChannel(new GrpcHttp2ServerHeadersDecoder(GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE));

    // replace the keepAliveManager with spyKeepAliveManager
    spyKeepAliveManager = mock(KeepAliveManager.class, delegatesTo(handler().getKeepAliveManagerForTest()));
    handler().setKeepAliveManagerForTest(spyKeepAliveManager);

    // Simulate receipt of the connection preface
    handler().handleProtocolNegotiationCompleted(Attributes.EMPTY, /*securityInfo=*/ null);
    channelRead(Http2CodecUtil.connectionPrefaceBuf());
    // Simulate receipt of initial remote settings.
    ByteBuf serializedSettings = serializeSettings(new Http2Settings());
    channelRead(serializedSettings);//from  w  w w  . jav  a 2  s .co  m
}

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

License:Apache License

@Test
public void transportReadyDelayedUntilConnectionPreface() throws Exception {
    initChannel(new GrpcHttp2ServerHeadersDecoder(GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE));

    handler().handleProtocolNegotiationCompleted(Attributes.EMPTY, /*securityInfo=*/ null);
    verify(transportListener, never()).transportReady(any(Attributes.class));

    // Simulate receipt of the connection preface
    channelRead(Http2CodecUtil.connectionPrefaceBuf());
    channelRead(serializeSettings(new Http2Settings()));
    verify(transportListener).transportReady(any(Attributes.class));
}