Crash fix in management server

This commit is contained in:
pokamest 2021-01-07 19:10:24 +03:00
parent ff557589ee
commit c7dafe9c00
2 changed files with 17 additions and 13 deletions

View file

@ -44,12 +44,12 @@ void ManagementServer::onNewConnection()
{
qDebug() << "New incoming connection";
m_socket = m_tcpServer->nextPendingConnection();
m_socket = QPointer<QTcpSocket>(m_tcpServer->nextPendingConnection());
m_tcpServer->close();
QObject::connect(m_socket, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
QObject::connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
QObject::connect(m_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
QObject::connect(m_socket.data(), SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
QObject::connect(m_socket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
QObject::connect(m_socket.data(), SIGNAL(readyRead()), this, SLOT(onReadyRead()));
}
void ManagementServer::onSocketError(QAbstractSocket::SocketError socketError)
@ -61,10 +61,10 @@ void ManagementServer::onSocketError(QAbstractSocket::SocketError socketError)
void ManagementServer::onSocketDisconnected()
{
m_socket->deleteLater();
if (m_socket) m_socket->deleteLater();
}
QTcpSocket* ManagementServer::socket() const
QPointer<QTcpSocket> ManagementServer::socket() const
{
if (!isOpen()) {
return nullptr;
@ -80,14 +80,16 @@ void ManagementServer::onReadyRead()
bool ManagementServer::start(const QString& host, unsigned int port)
{
if (m_tcpServer) {
delete m_tcpServer;
m_tcpServer->deleteLater();
}
m_tcpServer = new QTcpServer(this);
m_tcpServer = QSharedPointer<QTcpServer>(new QTcpServer(this), [](QTcpServer *s){
if (s) s->deleteLater();
});
m_tcpServer->setMaxPendingConnections(1);
connect(m_tcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(onAcceptError(QAbstractSocket::SocketError)));
connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
connect(m_tcpServer.data(), SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(onAcceptError(QAbstractSocket::SocketError)));
connect(m_tcpServer.data(), SIGNAL(newConnection()), this, SLOT(onNewConnection()));
if (m_tcpServer->listen(QHostAddress(host), port)) {
emit serverStarted();

View file

@ -2,6 +2,8 @@
#define MANAGEMENTSERVER_H
#include <QAbstractSocket>
#include <QPointer>
#include <QSharedPointer>
#include <QString>
class QTcpServer;
@ -22,7 +24,7 @@ public:
QString readLine();
qint64 writeCommand(const QString& message);
QTcpSocket* socket() const;
QPointer<QTcpSocket> socket() const;
signals:
void readyRead();
@ -36,8 +38,8 @@ protected slots:
void onSocketError(QAbstractSocket::SocketError socketError);
protected:
QTcpServer* m_tcpServer;
QTcpSocket* m_socket;
QSharedPointer<QTcpServer> m_tcpServer;
QPointer<QTcpSocket> m_socket;
};
#endif // MANAGEMENTSERVER_H