QGrpcHttp2Channel Class

The QGrpcHttp2Channel class provides a HTTP/2 transport layer for gRPC communication. 更多...

头: #include <QGrpcHttp2Channel>
CMake: find_package(Qt6 REQUIRED COMPONENTS Grpc)
target_link_libraries(mytarget PRIVATE Qt6::Grpc)
Since: Qt 6.5
在 QML: GrpcHttp2Channel
继承: QAbstractGrpcChannel

公共函数

QGrpcHttp2Channel (const QUrl & hostUri )
QGrpcHttp2Channel (const QUrl & hostUri , const QGrpcChannelOptions & options )
virtual ~QGrpcHttp2Channel () override
QUrl hostUri () const

详细描述

The QGrpcHttp2Channel class implements QAbstractGrpcChannel , enabling gRPC communication carried over HTTP/2 framing .

HTTP/2 introduces several advantages over its predecessor, HTTP/1.1, making QGrpcHttp2Channel well-suited for high-performance, real-time applications that require efficient communication, without sacrificing security or reliability, by using multiplexed TCP connections.

The channel can be customized with SSL support, a custom serializationFormat , or other options by constructing it with a QGrpcChannelOptions containing the required customizations.

Transportation scheme

The QGrpcHttp2Channel implementation prefers different transportation methods based on the provided hostUri , scheme and options. The following criteria applies:

Scheme 描述 Default Port 要求 范例
http Unencrypted HTTP/2 over TCP 80 None http://localhost
https TLS-encrypted HTTP/2 over TCP 443 QSslSocket support AND (scheme OR sslConfiguration ) https://localhost
unix Unix domain socket in filesystem path QLocalSocket support AND scheme unix:///tmp/grpc.socket

Content-Type

The content-type in gRPC over HTTP/2 determines the message serialization format. It must start with application/grpc and can include a suffix. The format follows this scheme:

"content-type": "application/grpc" [("+proto" / "+json" / {custom})]
					

例如:

  • application/grpc+proto specifies Protobuf encoding.
  • application/grpc+json specifies JSON encoding.

The serialization format can be configured either by specifying the content-type inside the metadata or by setting the serializationFormat directly. By default, the application/grpc content-type is used.

To configure QGrpcHttp2Channel with the JSON serialization format using content-type metadata:

auto jsonChannel = std::make_shared<QGrpcHttp2Channel>(
    QUrl("http://localhost:50051"_L1),
    QGrpcChannelOptions().setMetadata({
        { "content-type"_ba, "application/grpc+json"_ba },
    })
);
					

For a custom serializer and content-type , you can directly set the serialization format:

class DummySerializer : public QAbstractProtobufSerializer
{
    ...
};
QGrpcSerializationFormat dummyFormat("dummy", std::make_shared<DummySerializer>());
					
auto dummyChannel = std::make_shared<QGrpcHttp2Channel>(
    QUrl("http://localhost:50051"_L1),
    QGrpcChannelOptions().setSerializationFormat(dummyFormat)
);
					

This uses DummySerializer for encoding and decoding messages with the dummy suffix. For HTTP/2 transportation this results in the application/grpc+dummy content-type.

注意: Custom serializers require server support for the specified format.

另请参阅 QAbstractGrpcChannel , QGrpcChannelOptions ,和 QGrpcSerializationFormat .

成员函数文档编制

[explicit] QGrpcHttp2Channel:: QGrpcHttp2Channel (const QUrl & hostUri )

Constructs QGrpcHttp2Channel with hostUri . Please see the Transportation scheme section for more information.

[explicit] QGrpcHttp2Channel:: QGrpcHttp2Channel (const QUrl & hostUri , const QGrpcChannelOptions & options )

Constructs QGrpcHttp2Channel with hostUri and options . Please see the Transportation scheme section for more information.

[override virtual noexcept] QGrpcHttp2Channel:: ~QGrpcHttp2Channel ()

销毁 QGrpcHttp2Channel 对象。

QUrl QGrpcHttp2Channel:: hostUri () const

Returns the host URI for this channel.