Skip to content

Commit

Permalink
src/socketpair.*: log all connection errors
Browse files Browse the repository at this point in the history
QTcpSocket and QTcpServer emit an Qt signal on error, so we create slots
to receive those signals, and print the error that ocurred to stderr
through qCritical. qCritical gives the admin flexibility in choosing
whether or not to make those errors fatal or not through setting or not
the environment variable `QT_FATAL_CRITICALS`. See
https://doc.qt.io/qt-5/qtglobal.html#qCritical
  • Loading branch information
HED-jzignego committed Feb 24, 2022
1 parent 9dca9b7 commit 45f260b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/socketpair.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "socketpair.h"
#include <QIODevice>
#include <QDebug>
#include <QIODevice>
#include <QtGlobal>

SocketPair::SocketPair(QObject *parent)
: QObject(parent)
Expand All @@ -14,6 +15,9 @@ bool SocketPair::create()
connect(dataCheck, SIGNAL(timeout()), this, SLOT(readServerData()));
connect(&server, SIGNAL(newConnection()), this, SLOT(newConnection()), Qt::QueuedConnection);

connect(&server, SIGNAL(acceptError()), this, SLOT(logServerError()));
connect(&clientConnection, SIGNAL(error()), this, SLOT(logClientConnectionError()));

int tries = 5;
while (tries) {
if (!server.isListening()) {
Expand All @@ -40,6 +44,12 @@ bool SocketPair::create()
void SocketPair::newConnection()
{
serverConnection = server.nextPendingConnection();
if(serverConnection != nullptr) {
connect(serverConnection, SIGNAL(error()), this, SLOT(logServerConnectionError()));
} else {
qFatal("%s:%d:%s, server.nextPendingConnection() was NULL.", __FILE__,
__LINE__, __func__);
}

serverConnection->setSocketOption( QAbstractSocket::LowDelayOption, 1 );
serverConnection->setSocketOption( QAbstractSocket::KeepAliveOption, 1 );
Expand Down Expand Up @@ -81,3 +91,24 @@ QTcpSocket* SocketPair::output()
{
return serverConnection;
}

void SocketPair::logServerError(QAbstractSocket::SocketError socketError) {
qCritical("%s:%d:%s, server.serverError() is: '%d', and server.errorString() "
"is: '%s', and socketError is: '%d'", __FILE__, __LINE__, __func__,
server.serverError(), qUtf8Printable(server.errorString()),
socketError);
}

void SocketPair::logServerConnectionError(QAbstractSocket::SocketError socketError) {
qCritical("%s:%d:%s, serverConnection->error() is: '%d', and "
"serverConnection->errorString() is: '%s', and socketError is: '%d'",
__FILE__, __LINE__, __func__, serverConnection->error(),
qUtf8Printable(serverConnection->errorString()), socketError);
}

void SocketPair::logClientConnectionError(QAbstractSocket::SocketError socketError) {
qCritical("%s:%d:%s, clientConnection.error() is: '%d', and "
"clientConnection.errorString() is: '%s', and socketError is: '%d'",
__FILE__, __LINE__, __func__, clientConnection.error(),
qUtf8Printable(clientConnection.errorString()), socketError);
}
6 changes: 6 additions & 0 deletions src/socketpair.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public slots:
QTcpSocket *serverConnection;
QTcpSocket clientConnection;
QTcpServer server;

private slots:
void logServerError(QAbstractSocket::SocketError);
void logServerConnectionError(QAbstractSocket::SocketError);
void logClientConnectionError(QAbstractSocket::SocketError);

};

#endif // SOCKETPAIR_H

0 comments on commit 45f260b

Please sign in to comment.