QGrpcHttp2Channel: unify socket error handler

De-duplicate the logic for the error handlers.

Pick-to: 6.9.2 6.8
Change-Id: I1f8745046ba996ba81eb1bf2f5c3882b6c31059b
Reviewed-by: Tatiana Borisova <tatiana.borisova@qt.io>
(cherry picked from commit 4cf1dd3093)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 6c4c494395)
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
Dennis Oberst 2025-07-24 12:36:48 +02:00
parent 9a08967617
commit a33e3191d9
1 changed files with 20 additions and 24 deletions

View File

@ -371,7 +371,18 @@ private:
bool createHttp2Stream(Http2Handler *handler); bool createHttp2Stream(Http2Handler *handler);
void createHttp2Connection(); void createHttp2Connection();
void handleSocketError();
#if QT_CONFIG(localserver)
void handleLocalSocketError(QLocalSocket::LocalSocketError error)
{
handleSocketError(QDebug::toBytes(error));
}
#endif
void handleAbstractSocketError(QAbstractSocket::SocketError error)
{
handleSocketError(QDebug::toBytes(error));
}
void handleSocketError(const QByteArray &errorCode);
template <typename T> template <typename T>
T *initSocket() T *initSocket()
@ -532,7 +543,6 @@ void Http2Handler::attachStream(QHttp2Stream *stream_)
QObject::connect(m_stream.get(), &QHttp2Stream::uploadFinished, this, QObject::connect(m_stream.get(), &QHttp2Stream::uploadFinished, this,
&Http2Handler::processQueue); &Http2Handler::processQueue);
} }
// Builds HTTP/2 headers for the initial gRPC request. // Builds HTTP/2 headers for the initial gRPC request.
@ -899,13 +909,8 @@ QGrpcHttp2ChannelPrivate::QGrpcHttp2ChannelPrivate(const QUrl &uri, QGrpcHttp2Ch
QObject::connect(localSocket, &QLocalSocket::connected, this, QObject::connect(localSocket, &QLocalSocket::connected, this,
&QGrpcHttp2ChannelPrivate::createHttp2Connection); &QGrpcHttp2ChannelPrivate::createHttp2Connection);
QObject::connect(localSocket, &QLocalSocket::errorOccurred, this, QObject::connect(localSocket, &QLocalSocket::errorOccurred, this,
[this](QLocalSocket::LocalSocketError error) { &QGrpcHttp2ChannelPrivate::handleLocalSocketError);
qGrpcDebug()
<< "Error occurred(" << error << "):"
<< static_cast<QLocalSocket *>(m_socket.get())->errorString()
<< hostUri;
handleSocketError();
});
m_reconnectFunction = [localSocket, this] { m_reconnectFunction = [localSocket, this] {
localSocket->connectToServer(hostUri.host() + hostUri.path()); localSocket->connectToServer(hostUri.host() + hostUri.path());
}; };
@ -936,13 +941,8 @@ QGrpcHttp2ChannelPrivate::QGrpcHttp2ChannelPrivate(const QUrl &uri, QGrpcHttp2Ch
QObject::connect(sslSocket, &QSslSocket::encrypted, this, QObject::connect(sslSocket, &QSslSocket::encrypted, this,
&QGrpcHttp2ChannelPrivate::createHttp2Connection); &QGrpcHttp2ChannelPrivate::createHttp2Connection);
QObject::connect(sslSocket, &QAbstractSocket::errorOccurred, this, QObject::connect(sslSocket, &QAbstractSocket::errorOccurred, this,
[this](QAbstractSocket::SocketError error) { &QGrpcHttp2ChannelPrivate::handleAbstractSocketError);
qDebug()
<< "Error occurred(" << error << "):"
<< static_cast<QAbstractSocket *>(m_socket.get())->errorString()
<< hostUri;
handleSocketError();
});
m_reconnectFunction = [sslSocket, this] { m_reconnectFunction = [sslSocket, this] {
sslSocket->connectToHostEncrypted(hostUri.host(), static_cast<quint16>(hostUri.port())); sslSocket->connectToHostEncrypted(hostUri.host(), static_cast<quint16>(hostUri.port()));
}; };
@ -960,13 +960,8 @@ QGrpcHttp2ChannelPrivate::QGrpcHttp2ChannelPrivate(const QUrl &uri, QGrpcHttp2Ch
QObject::connect(httpSocket, &QAbstractSocket::connected, this, QObject::connect(httpSocket, &QAbstractSocket::connected, this,
&QGrpcHttp2ChannelPrivate::createHttp2Connection); &QGrpcHttp2ChannelPrivate::createHttp2Connection);
QObject::connect(httpSocket, &QAbstractSocket::errorOccurred, this, QObject::connect(httpSocket, &QAbstractSocket::errorOccurred, this,
[this](QAbstractSocket::SocketError error) { &QGrpcHttp2ChannelPrivate::handleAbstractSocketError);
qGrpcDebug()
<< "Error occurred(" << error << "):"
<< static_cast<QAbstractSocket *>(m_socket.get())->errorString()
<< hostUri;
handleSocketError();
});
m_reconnectFunction = [httpSocket, this] { m_reconnectFunction = [httpSocket, this] {
httpSocket->connectToHost(hostUri.host(), static_cast<quint16>(hostUri.port())); httpSocket->connectToHost(hostUri.host(), static_cast<quint16>(hostUri.port()));
}; };
@ -1065,8 +1060,9 @@ void QGrpcHttp2ChannelPrivate::createHttp2Connection()
for_each_non_expired_handler([this](Http2Handler *handler) { createHttp2Stream(handler); }); for_each_non_expired_handler([this](Http2Handler *handler) { createHttp2Stream(handler); });
} }
void QGrpcHttp2ChannelPrivate::handleSocketError() void QGrpcHttp2ChannelPrivate::handleSocketError(const QByteArray &errorCode)
{ {
qGrpcDebug() << "Error occurred(" << errorCode << "):" << m_socket->errorString() << hostUri;
delete m_connection; delete m_connection;
m_connection = nullptr; m_connection = nullptr;
m_state = ConnectionState::Error; m_state = ConnectionState::Error;