Fix crash in client connection if the server is already gone bu data where pending, update monitor
This commit is contained in:
parent
5b39e12449
commit
8ee90aaccd
@ -45,8 +45,7 @@ TcpSocketServer::~TcpSocketServer()
|
|||||||
|
|
||||||
void TcpSocketServer::sendData(const QUuid &clientId, const QByteArray &data)
|
void TcpSocketServer::sendData(const QUuid &clientId, const QByteArray &data)
|
||||||
{
|
{
|
||||||
QTcpSocket *client = nullptr;
|
QTcpSocket *client = m_clientList.value(clientId);
|
||||||
client = m_clientList.value(clientId);
|
|
||||||
if (!client) {
|
if (!client) {
|
||||||
qCWarning(dcTcpSocketServer()) << "Client" << clientId << "unknown to this transport";
|
qCWarning(dcTcpSocketServer()) << "Client" << clientId << "unknown to this transport";
|
||||||
return;
|
return;
|
||||||
@ -67,6 +66,7 @@ void TcpSocketServer::killClientConnection(const QUuid &clientId, const QString
|
|||||||
}
|
}
|
||||||
|
|
||||||
qCDebug(dcTcpSocketServer()) << "Killing client connection" << clientId.toString() << "Reason:" << killReason;
|
qCDebug(dcTcpSocketServer()) << "Killing client connection" << clientId.toString() << "Reason:" << killReason;
|
||||||
|
client->flush();
|
||||||
client->close();
|
client->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,31 +83,6 @@ bool TcpSocketServer::running() const
|
|||||||
return m_server->isListening();
|
return m_server->isListening();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpSocketServer::onDataAvailable(QSslSocket *client, const QByteArray &data)
|
|
||||||
{
|
|
||||||
//qCDebug(dcTcpSocketServerTraffic()) << "Emitting data available internal.";
|
|
||||||
QUuid clientId = m_clientList.key(qobject_cast<QTcpSocket *>(client));
|
|
||||||
emit dataAvailable(clientId, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TcpSocketServer::onSocketConnected(QSslSocket *client)
|
|
||||||
{
|
|
||||||
QUuid clientId = QUuid::createUuid();
|
|
||||||
qCDebug(dcTcpSocketServer()) << "New client connected:" << client << client->peerAddress().toString() << clientId.toString();
|
|
||||||
m_clientList.insert(clientId, client);
|
|
||||||
emit clientConnected(clientId, client->peerAddress());
|
|
||||||
}
|
|
||||||
|
|
||||||
void TcpSocketServer::onSocketDisconnected(QSslSocket *client)
|
|
||||||
{
|
|
||||||
QUuid clientId = m_clientList.key(client);
|
|
||||||
qCDebug(dcTcpSocketServer()) << "Client disconnected:" << client << client->peerAddress().toString() << clientId.toString();
|
|
||||||
if (m_clientList.take(clientId)) {
|
|
||||||
// Note: the SslServer is deleting the socket object
|
|
||||||
emit clientDisconnected(clientId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TcpSocketServer::startServer()
|
bool TcpSocketServer::startServer()
|
||||||
{
|
{
|
||||||
qCDebug(dcTcpSocketServer()) << "Starting TCP server" << m_serverUrl.toString();
|
qCDebug(dcTcpSocketServer()) << "Starting TCP server" << m_serverUrl.toString();
|
||||||
@ -128,21 +103,53 @@ bool TcpSocketServer::startServer()
|
|||||||
|
|
||||||
bool TcpSocketServer::stopServer()
|
bool TcpSocketServer::stopServer()
|
||||||
{
|
{
|
||||||
|
if (!m_server)
|
||||||
|
return true;
|
||||||
|
|
||||||
// Clean up client connections
|
// Clean up client connections
|
||||||
foreach (const QUuid &clientId, m_clientList.keys()) {
|
foreach (const QUuid &clientId, m_clientList.keys()) {
|
||||||
killClientConnection(clientId, "Stop server");
|
killClientConnection(clientId, "Stop server");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_server)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
qCDebug(dcTcpSocketServer()) << "Stop server" << m_serverUrl.toString();
|
qCDebug(dcTcpSocketServer()) << "Stop server" << m_serverUrl.toString();
|
||||||
|
|
||||||
m_server->close();
|
m_server->close();
|
||||||
delete m_server;
|
m_server->deleteLater();
|
||||||
m_server = nullptr;
|
m_server = nullptr;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TcpSocketServer::onDataAvailable(QSslSocket *client, const QByteArray &data)
|
||||||
|
{
|
||||||
|
//qCDebug(dcTcpSocketServerTraffic()) << "Emitting data available internal.";
|
||||||
|
QUuid clientId = m_clientList.key(client);
|
||||||
|
emit dataAvailable(clientId, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TcpSocketServer::onSocketConnected(QSslSocket *client)
|
||||||
|
{
|
||||||
|
QUuid clientId = QUuid::createUuid();
|
||||||
|
qCDebug(dcTcpSocketServer()) << "New client connected:" << client << client->peerAddress().toString() << clientId.toString();
|
||||||
|
m_clientList.insert(clientId, client);
|
||||||
|
emit clientConnected(clientId, client->peerAddress());
|
||||||
|
}
|
||||||
|
|
||||||
|
void TcpSocketServer::onSocketDisconnected(QSslSocket *client)
|
||||||
|
{
|
||||||
|
QUuid clientId = m_clientList.key(client);
|
||||||
|
if (clientId.isNull()) {
|
||||||
|
qCWarning(dcTcpSocketServer()) << "Socket disconnected but the uuid is null." << client << client->peerAddress().toString() << clientId.toString();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
qCDebug(dcTcpSocketServer()) << "Client disconnected:" << client << client->peerAddress().toString() << clientId.toString();
|
||||||
|
m_clientList.take(clientId);
|
||||||
|
// Note: the SslServer is deleting the socket object
|
||||||
|
emit clientDisconnected(clientId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SslServer::SslServer(bool sslEnabled, const QSslConfiguration &config, QObject *parent) :
|
SslServer::SslServer(bool sslEnabled, const QSslConfiguration &config, QObject *parent) :
|
||||||
QTcpServer(parent),
|
QTcpServer(parent),
|
||||||
m_sslEnabled(sslEnabled),
|
m_sslEnabled(sslEnabled),
|
||||||
@ -155,11 +162,11 @@ void SslServer::incomingConnection(qintptr socketDescriptor)
|
|||||||
{
|
{
|
||||||
QSslSocket *sslSocket = new QSslSocket(this);
|
QSslSocket *sslSocket = new QSslSocket(this);
|
||||||
qCDebug(dcTcpSocketServer()) << "Incomming connection" << sslSocket;
|
qCDebug(dcTcpSocketServer()) << "Incomming connection" << sslSocket;
|
||||||
connect(sslSocket, &QSslSocket::readyRead, this, &SslServer::onSocketReadyRead);
|
|
||||||
connect(sslSocket, &QSslSocket::disconnected, this, &SslServer::onSocketDisconnected);
|
connect(sslSocket, &QSslSocket::disconnected, this, &SslServer::onSocketDisconnected);
|
||||||
|
|
||||||
typedef void (QAbstractSocket:: *errorSignal)(QAbstractSocket::SocketError);
|
typedef void (QAbstractSocket:: *errorSignal)(QAbstractSocket::SocketError);
|
||||||
connect(sslSocket, static_cast<errorSignal>(&QAbstractSocket::error), this, &SslServer::onSocketError);
|
connect(sslSocket, static_cast<errorSignal>(&QAbstractSocket::error), this, &SslServer::onSocketError);
|
||||||
|
|
||||||
connect(sslSocket, &QSslSocket::encrypted, this, [this, sslSocket](){
|
connect(sslSocket, &QSslSocket::encrypted, this, [this, sslSocket](){
|
||||||
qCDebug(dcTcpSocketServer()) << "SSL encryption established for" << sslSocket;
|
qCDebug(dcTcpSocketServer()) << "SSL encryption established for" << sslSocket;
|
||||||
emit socketConnected(sslSocket);
|
emit socketConnected(sslSocket);
|
||||||
@ -167,12 +174,14 @@ void SslServer::incomingConnection(qintptr socketDescriptor)
|
|||||||
|
|
||||||
typedef void (QSslSocket:: *sslErrorsSignal)(const QList<QSslError> &);
|
typedef void (QSslSocket:: *sslErrorsSignal)(const QList<QSslError> &);
|
||||||
connect(sslSocket, static_cast<sslErrorsSignal>(&QSslSocket::sslErrors), this, [](const QList<QSslError> &errors) {
|
connect(sslSocket, static_cast<sslErrorsSignal>(&QSslSocket::sslErrors), this, [](const QList<QSslError> &errors) {
|
||||||
qCWarning(dcTcpSocketServer()) << "SSL Errors happened in the client connections:";
|
qCWarning(dcTcpSocketServer()) << "SSL error occurred in the client connection:";
|
||||||
foreach (const QSslError &error, errors) {
|
foreach (const QSslError &error, errors) {
|
||||||
qCWarning(dcTcpSocketServer()) << "SSL Error:" << error.error() << error.errorString();
|
qCWarning(dcTcpSocketServer()) << "SSL error:" << error.error() << error.errorString();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
connect(sslSocket, &QSslSocket::readyRead, this, &SslServer::onSocketReadyRead);
|
||||||
|
|
||||||
if (!sslSocket->setSocketDescriptor(socketDescriptor)) {
|
if (!sslSocket->setSocketDescriptor(socketDescriptor)) {
|
||||||
qCWarning(dcTcpSocketServer()) << "Failed to set SSL socket descriptor.";
|
qCWarning(dcTcpSocketServer()) << "Failed to set SSL socket descriptor.";
|
||||||
delete sslSocket;
|
delete sslSocket;
|
||||||
@ -182,7 +191,6 @@ void SslServer::incomingConnection(qintptr socketDescriptor)
|
|||||||
if (m_sslEnabled) {
|
if (m_sslEnabled) {
|
||||||
qCDebug(dcTcpSocketServer()) << "Start SSL encryption for" << sslSocket;
|
qCDebug(dcTcpSocketServer()) << "Start SSL encryption for" << sslSocket;
|
||||||
sslSocket->setSslConfiguration(m_config);
|
sslSocket->setSslConfiguration(m_config);
|
||||||
//addPendingConnection(sslSocket);
|
|
||||||
sslSocket->startServerEncryption();
|
sslSocket->startServerEncryption();
|
||||||
} else {
|
} else {
|
||||||
emit socketConnected(sslSocket);
|
emit socketConnected(sslSocket);
|
||||||
@ -191,7 +199,7 @@ void SslServer::incomingConnection(qintptr socketDescriptor)
|
|||||||
|
|
||||||
void SslServer::onSocketDisconnected()
|
void SslServer::onSocketDisconnected()
|
||||||
{
|
{
|
||||||
QSslSocket *sslSocket = qobject_cast<QSslSocket *>(sender());
|
QSslSocket *sslSocket = static_cast<QSslSocket *>(sender());
|
||||||
qCDebug(dcTcpSocketServer()) << "Client socket disconnected:" << sslSocket << sslSocket->peerAddress().toString();;
|
qCDebug(dcTcpSocketServer()) << "Client socket disconnected:" << sslSocket << sslSocket->peerAddress().toString();;
|
||||||
emit socketDisconnected(sslSocket);
|
emit socketDisconnected(sslSocket);
|
||||||
sslSocket->deleteLater();
|
sslSocket->deleteLater();
|
||||||
@ -199,7 +207,7 @@ void SslServer::onSocketDisconnected()
|
|||||||
|
|
||||||
void SslServer::onSocketReadyRead()
|
void SslServer::onSocketReadyRead()
|
||||||
{
|
{
|
||||||
QSslSocket *sslSocket = qobject_cast<QSslSocket *>(sender());
|
QSslSocket *sslSocket = static_cast<QSslSocket *>(sender());
|
||||||
QByteArray data = sslSocket->readAll();
|
QByteArray data = sslSocket->readAll();
|
||||||
qCDebug(dcTcpSocketServerTraffic()) << "Data from socket" << sslSocket->peerAddress().toString() << data;
|
qCDebug(dcTcpSocketServerTraffic()) << "Data from socket" << sslSocket->peerAddress().toString() << data;
|
||||||
emit dataAvailable(sslSocket, data);
|
emit dataAvailable(sslSocket, data);
|
||||||
@ -207,7 +215,26 @@ void SslServer::onSocketReadyRead()
|
|||||||
|
|
||||||
void SslServer::onSocketError(QAbstractSocket::SocketError error)
|
void SslServer::onSocketError(QAbstractSocket::SocketError error)
|
||||||
{
|
{
|
||||||
qCWarning(dcTcpSocketServer()) << "Socket error occurred" << error;
|
QSslSocket *sslSocket = static_cast<QSslSocket *>(sender());
|
||||||
|
qCWarning(dcTcpSocketServer()) << "Socket error occurred" << error << sslSocket->errorString();
|
||||||
|
switch(error) {
|
||||||
|
case QAbstractSocket::SocketResourceError:
|
||||||
|
case QAbstractSocket::SocketTimeoutError:
|
||||||
|
case QAbstractSocket::DatagramTooLargeError:
|
||||||
|
case QAbstractSocket::NetworkError:
|
||||||
|
case QAbstractSocket::SslHandshakeFailedError:
|
||||||
|
case QAbstractSocket::UnfinishedSocketOperationError:
|
||||||
|
case QAbstractSocket::SslInternalError:
|
||||||
|
case QAbstractSocket::SslInvalidUserDataError:
|
||||||
|
case QAbstractSocket::TemporaryError:
|
||||||
|
case QAbstractSocket::UnknownSocketError:
|
||||||
|
qCWarning(dcTcpSocketServer()) << "Explitily closing the socket due to error.";
|
||||||
|
sslSocket->close();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -45,10 +45,6 @@ public:
|
|||||||
explicit SslServer(bool sslEnabled, const QSslConfiguration &config, QObject *parent = nullptr);
|
explicit SslServer(bool sslEnabled, const QSslConfiguration &config, QObject *parent = nullptr);
|
||||||
~SslServer() override = default;
|
~SslServer() override = default;
|
||||||
|
|
||||||
private:
|
|
||||||
bool m_sslEnabled = false;
|
|
||||||
QSslConfiguration m_config;
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void socketConnected(QSslSocket *socket);
|
void socketConnected(QSslSocket *socket);
|
||||||
void socketDisconnected(QSslSocket *socket);
|
void socketDisconnected(QSslSocket *socket);
|
||||||
@ -57,6 +53,10 @@ signals:
|
|||||||
protected:
|
protected:
|
||||||
void incomingConnection(qintptr socketDescriptor) override;
|
void incomingConnection(qintptr socketDescriptor) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool m_sslEnabled = false;
|
||||||
|
QSslConfiguration m_config;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onSocketDisconnected();
|
void onSocketDisconnected();
|
||||||
void onSocketReadyRead();
|
void onSocketReadyRead();
|
||||||
@ -79,11 +79,15 @@ public:
|
|||||||
|
|
||||||
bool running() const override;
|
bool running() const override;
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
bool startServer() override;
|
||||||
|
bool stopServer() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_sslEnabled;
|
bool m_sslEnabled;
|
||||||
QSslConfiguration m_sslConfiguration;
|
QSslConfiguration m_sslConfiguration;
|
||||||
|
|
||||||
QHash<QUuid, QTcpSocket *> m_clientList;
|
QHash<QUuid, QSslSocket *> m_clientList;
|
||||||
|
|
||||||
SslServer *m_server = nullptr;
|
SslServer *m_server = nullptr;
|
||||||
|
|
||||||
@ -92,9 +96,6 @@ private slots:
|
|||||||
void onSocketConnected(QSslSocket *client);
|
void onSocketConnected(QSslSocket *client);
|
||||||
void onSocketDisconnected(QSslSocket *client);
|
void onSocketDisconnected(QSslSocket *client);
|
||||||
|
|
||||||
public slots:
|
|
||||||
bool startServer() override;
|
|
||||||
bool stopServer() override;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -357,7 +357,11 @@ void TunnelProxyServer::onClientDataAvailable(const QUuid &clientId, const QByte
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Q_ASSERT_X(clientConnection->serverConnection(), "TunnelProxyServer", "The client has not been registered to a server connection");
|
if (!clientConnection->serverConnection()) {
|
||||||
|
qCWarning(dcTunnelProxyServer()) << "Valid client wants to send data to the server, but there is no server registered for this client.";
|
||||||
|
Q_ASSERT_X(clientConnection->serverConnection(), "TunnelProxyServer", "The client has not been registered to a server connection");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
SlipDataProcessor::Frame frame;
|
SlipDataProcessor::Frame frame;
|
||||||
frame.socketAddress = clientConnection->socketAddress();
|
frame.socketAddress = clientConnection->socketAddress();
|
||||||
|
|||||||
@ -75,8 +75,9 @@ void NonInteractiveMonitor::onConnected()
|
|||||||
serverLinePrint.prepend("├┬─");
|
serverLinePrint.prepend("├┬─");
|
||||||
}
|
}
|
||||||
|
|
||||||
serverLinePrint += QString("%1 | %2 RX: %3 TX: %4 | %5")
|
serverLinePrint += QString("%1 | %2 | %3 RX: %4 TX: %5 | %6")
|
||||||
.arg(serverConnectionTime)
|
.arg(serverConnectionTime)
|
||||||
|
.arg(serverMap.value("serverUuid").toString())
|
||||||
.arg(serverMap.value("address").toString(), - 15)
|
.arg(serverMap.value("address").toString(), - 15)
|
||||||
.arg(Utils::humanReadableTraffic(serverMap.value("rxDataCount").toInt()), - 9)
|
.arg(Utils::humanReadableTraffic(serverMap.value("rxDataCount").toInt()), - 9)
|
||||||
.arg(Utils::humanReadableTraffic(serverMap.value("txDataCount").toInt()), - 9)
|
.arg(Utils::humanReadableTraffic(serverMap.value("txDataCount").toInt()), - 9)
|
||||||
@ -93,8 +94,9 @@ void NonInteractiveMonitor::onConnected()
|
|||||||
clientLinePrint.prepend("│├─");
|
clientLinePrint.prepend("│├─");
|
||||||
}
|
}
|
||||||
|
|
||||||
clientLinePrint += QString("%1 | %2 RX: %3 TX: %4 | %5")
|
clientLinePrint += QString("%1 | %2 | %3 RX: %4 TX: %5 | %6")
|
||||||
.arg(QDateTime::fromTime_t(clientMap.value("timestamp").toUInt()).toString("dd.MM.yyyy hh:mm:ss"))
|
.arg(QDateTime::fromTime_t(clientMap.value("timestamp").toUInt()).toString("dd.MM.yyyy hh:mm:ss"))
|
||||||
|
.arg(clientMap.value("clientUuid").toString())
|
||||||
.arg(clientMap.value("address").toString(), - 15)
|
.arg(clientMap.value("address").toString(), - 15)
|
||||||
.arg(Utils::humanReadableTraffic(serverMap.value("rxDataCount").toInt()), - 9)
|
.arg(Utils::humanReadableTraffic(serverMap.value("rxDataCount").toInt()), - 9)
|
||||||
.arg(Utils::humanReadableTraffic(serverMap.value("txDataCount").toInt()), - 9)
|
.arg(Utils::humanReadableTraffic(serverMap.value("txDataCount").toInt()), - 9)
|
||||||
|
|||||||
Reference in New Issue
Block a user