Http2Handler: Re-order ctor args and rename m_operation

Re-order the argument to the Http2Handler ctor. The parent should come
first. Furthermore rename the m_operation to m_context as I think
context is a more fitting name for this important member.

This improves the readability and makes it easier to follow the code.

Pick-to: 6.10 6.9 6.8
Change-Id: I396e205ec345d80a8cf2cfebe43625f72d39ac6e
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
Dennis Oberst 2025-07-28 14:01:27 +02:00
parent 5bc854e901
commit 1ac37b787a
1 changed files with 24 additions and 25 deletions

View File

@ -278,7 +278,7 @@ public:
}; };
Q_ENUM(State); Q_ENUM(State);
explicit Http2Handler(QGrpcOperationContext *operation, QGrpcHttp2ChannelPrivate *parent, explicit Http2Handler(QGrpcHttp2ChannelPrivate *parent, QGrpcOperationContext *context,
bool endStream); bool endStream);
~Http2Handler() override; ~Http2Handler() override;
@ -290,7 +290,7 @@ public:
void asyncFinish(const QGrpcStatus &status); void asyncFinish(const QGrpcStatus &status);
void cancelWithStatus(const QGrpcStatus &status); void cancelWithStatus(const QGrpcStatus &status);
[[nodiscard]] bool expired() const { return !m_operation; } [[nodiscard]] bool expired() const { return !m_context; }
[[nodiscard]] bool isStreamClosedForSending() const [[nodiscard]] bool isStreamClosedForSending() const
{ {
@ -319,7 +319,7 @@ private:
[[nodiscard]] QGrpcHttp2Channel *channel() const; [[nodiscard]] QGrpcHttp2Channel *channel() const;
[[nodiscard]] bool handleContextExpired(); [[nodiscard]] bool handleContextExpired();
QPointer<QGrpcOperationContext> m_operation; QPointer<QGrpcOperationContext> m_context;
HPack::HttpHeader m_initialHeaders; HPack::HttpHeader m_initialHeaders;
QQueue<QByteArray> m_queue; QQueue<QByteArray> m_queue;
QPointer<QHttp2Stream> m_stream; QPointer<QHttp2Stream> m_stream;
@ -435,29 +435,29 @@ private:
/// ## Http2Handler Implementations /// ## Http2Handler Implementations
/// ///
Http2Handler::Http2Handler(QGrpcOperationContext *operation, QGrpcHttp2ChannelPrivate *parent, Http2Handler::Http2Handler(QGrpcHttp2ChannelPrivate *parent, QGrpcOperationContext *context,
bool endStream) bool endStream)
: QObject(parent), m_operation(operation), m_initialHeaders(constructInitialHeaders()), : QObject(parent), m_context(context), m_initialHeaders(constructInitialHeaders()),
m_endStreamAtFirstData(endStream), m_filterServerMetadata(constructFilterServerMetadata()) m_endStreamAtFirstData(endStream), m_filterServerMetadata(constructFilterServerMetadata())
{ {
// If the context (lifetime bound to the user) is destroyed, this handler // If the context (lifetime bound to the user) is destroyed, this handler
// can no longer perform any meaningful work. We allow it to be deleted; // can no longer perform any meaningful work. We allow it to be deleted;
// QHttp2Stream will handle any outstanding cancellations appropriately. // QHttp2Stream will handle any outstanding cancellations appropriately.
QObject::connect(operation, &QGrpcOperationContext::destroyed, this, QObject::connect(context, &QGrpcOperationContext::destroyed, this,
&Http2Handler::deleteLater); &Http2Handler::deleteLater);
QObject::connect(operation, &QGrpcOperationContext::cancelRequested, this, QObject::connect(context, &QGrpcOperationContext::cancelRequested, this,
&Http2Handler::cancel); &Http2Handler::cancel);
QObject::connect(operation, &QGrpcOperationContext::writesDoneRequested, this, QObject::connect(context, &QGrpcOperationContext::writesDoneRequested, this,
&Http2Handler::writesDone); &Http2Handler::writesDone);
if (!m_endStreamAtFirstData) { if (!m_endStreamAtFirstData) {
QObject::connect(operation, &QGrpcOperationContext::writeMessageRequested, this, QObject::connect(context, &QGrpcOperationContext::writeMessageRequested, this,
&Http2Handler::writeMessage); &Http2Handler::writeMessage);
} }
QObject::connect(operation, &QGrpcOperationContext::finished, &m_deadlineTimer, &QTimer::stop); QObject::connect(context, &QGrpcOperationContext::finished, &m_deadlineTimer, &QTimer::stop);
m_deadlineTimer.setSingleShot(true); m_deadlineTimer.setSingleShot(true);
writeMessage(operation->argument()); writeMessage(context->argument());
} }
Http2Handler::~Http2Handler() Http2Handler::~Http2Handler()
@ -515,7 +515,7 @@ void Http2Handler::attachStream(QHttp2Stream *stream_)
}, },
Qt::SingleShotConnection); Qt::SingleShotConnection);
QObject::connect(m_stream.get(), &QHttp2Stream::dataReceived, m_operation.get(), QObject::connect(m_stream.get(), &QHttp2Stream::dataReceived, m_context.get(),
[this](const QByteArray &data, bool endStream) { [this](const QByteArray &data, bool endStream) {
if (m_state != State::Cancelled) { if (m_state != State::Cancelled) {
m_expectedData.container.append(data); m_expectedData.container.append(data);
@ -528,7 +528,7 @@ void Http2Handler::attachStream(QHttp2Stream *stream_)
qGrpcDebug() << "Full data received:" << data.size() qGrpcDebug() << "Full data received:" << data.size()
<< "dataContainer:" << m_expectedData.container.size() << "dataContainer:" << m_expectedData.container.size()
<< "capacity:" << m_expectedData.expectedSize; << "capacity:" << m_expectedData.expectedSize;
emit m_operation emit m_context
->messageReceived(m_expectedData.container ->messageReceived(m_expectedData.container
.mid(GrpcMessageSizeHeaderSize, .mid(GrpcMessageSizeHeaderSize,
m_expectedData.expectedSize m_expectedData.expectedSize
@ -571,8 +571,8 @@ HPack::HttpHeader Http2Handler::constructInitialHeaders() const
const auto &channelOptions = channel()->channelOptions(); const auto &channelOptions = channel()->channelOptions();
const auto *channel = channelPriv(); const auto *channel = channelPriv();
QByteArray service{ m_operation->service() }; QByteArray service{ m_context->service() };
QByteArray method{ m_operation->method() }; QByteArray method{ m_context->method() };
auto headers = HPack::HttpHeader{ auto headers = HPack::HttpHeader{
{ AuthorityHeader, channel->authorityHeader() }, { AuthorityHeader, channel->authorityHeader() },
{ MethodHeader, MethodValue }, { MethodHeader, MethodValue },
@ -597,14 +597,14 @@ HPack::HttpHeader Http2Handler::constructInitialHeaders() const
}; };
iterateMetadata(channelOptions.metadata(QtGrpc::MultiValue)); iterateMetadata(channelOptions.metadata(QtGrpc::MultiValue));
iterateMetadata(m_operation->callOptions().metadata(QtGrpc::MultiValue)); iterateMetadata(m_context->callOptions().metadata(QtGrpc::MultiValue));
return headers; return headers;
} }
bool Http2Handler::constructFilterServerMetadata() const bool Http2Handler::constructFilterServerMetadata() const
{ {
return m_operation->callOptions() return m_context->callOptions()
.filterServerMetadata() .filterServerMetadata()
.value_or(channel()->channelOptions().filterServerMetadata().value_or(true)); .value_or(channel()->channelOptions().filterServerMetadata().value_or(true));
} }
@ -620,7 +620,7 @@ QGrpcHttp2Channel *Http2Handler::channel() const
bool Http2Handler::handleContextExpired() bool Http2Handler::handleContextExpired()
{ {
if (m_operation) if (m_context)
return false; return false;
m_state = State::Cancelled; m_state = State::Cancelled;
deleteLater(); // m_stream will sendRST_STREAM on destruction deleteLater(); // m_stream will sendRST_STREAM on destruction
@ -668,8 +668,7 @@ void Http2Handler::sendInitialRequest()
m_initialHeaders.clear(); m_initialHeaders.clear();
processQueue(); processQueue();
std::optional<std::chrono::milliseconds> deadline = m_operation->callOptions() std::optional<std::chrono::milliseconds> deadline = m_context->callOptions().deadlineTimeout();
.deadlineTimeout();
if (!deadline) if (!deadline)
deadline = channel()->channelOptions().deadlineTimeout(); deadline = channel()->channelOptions().deadlineTimeout();
if (deadline) { if (deadline) {
@ -707,14 +706,14 @@ void Http2Handler::finish(const QGrpcStatus &status)
return; return;
if (m_state != State::Cancelled) // don't overwrite the Cancelled state if (m_state != State::Cancelled) // don't overwrite the Cancelled state
m_state = State::Finished; m_state = State::Finished;
emit m_operation->finished(status); emit m_context->finished(status);
deleteLater(); deleteLater();
} }
void Http2Handler::asyncFinish(const QGrpcStatus &status) void Http2Handler::asyncFinish(const QGrpcStatus &status)
{ {
if (handleContextExpired()) if (handleContextExpired())
return; return;
QTimer::singleShot(0, m_operation.get(), [this, status]() { finish(status); }); QTimer::singleShot(0, m_context.get(), [this, status]() { finish(status); });
} }
void Http2Handler::cancelWithStatus(const QGrpcStatus &status) void Http2Handler::cancelWithStatus(const QGrpcStatus &status)
@ -852,12 +851,12 @@ void Http2Handler::handleHeaders(const HPack::HttpHeader &headers, HeaderPhase p
switch (phase) { switch (phase) {
case HeaderPhase::Initial: case HeaderPhase::Initial:
m_operation->setServerInitialMetadata(std::move(metadata)); m_context->setServerInitialMetadata(std::move(metadata));
break; break;
case HeaderPhase::TrailersOnly: case HeaderPhase::TrailersOnly:
[[fallthrough]]; [[fallthrough]];
case HeaderPhase::Trailers: case HeaderPhase::Trailers:
m_operation->setServerTrailingMetadata(std::move(metadata)); m_context->setServerTrailingMetadata(std::move(metadata));
finish({ *statusCode, statusMessage }); finish({ *statusCode, statusMessage });
break; break;
default: default:
@ -1031,7 +1030,7 @@ void QGrpcHttp2ChannelPrivate::processOperation(QGrpcOperationContext *operation
return; return;
} }
auto *handler = new Http2Handler(operationContext, this, endStream); auto *handler = new Http2Handler(this, operationContext, endStream);
#if QT_CONFIG(localserver) #if QT_CONFIG(localserver)
if (m_isLocalSocket) { if (m_isLocalSocket) {