Crash fix in management server
This commit is contained in:
parent
ff557589ee
commit
c7dafe9c00
2 changed files with 17 additions and 13 deletions
|
@ -44,12 +44,12 @@ void ManagementServer::onNewConnection()
|
||||||
{
|
{
|
||||||
qDebug() << "New incoming connection";
|
qDebug() << "New incoming connection";
|
||||||
|
|
||||||
m_socket = m_tcpServer->nextPendingConnection();
|
m_socket = QPointer<QTcpSocket>(m_tcpServer->nextPendingConnection());
|
||||||
m_tcpServer->close();
|
m_tcpServer->close();
|
||||||
|
|
||||||
QObject::connect(m_socket, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
|
QObject::connect(m_socket.data(), SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
|
||||||
QObject::connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
|
QObject::connect(m_socket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
|
||||||
QObject::connect(m_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
|
QObject::connect(m_socket.data(), SIGNAL(readyRead()), this, SLOT(onReadyRead()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ManagementServer::onSocketError(QAbstractSocket::SocketError socketError)
|
void ManagementServer::onSocketError(QAbstractSocket::SocketError socketError)
|
||||||
|
@ -61,10 +61,10 @@ void ManagementServer::onSocketError(QAbstractSocket::SocketError socketError)
|
||||||
|
|
||||||
void ManagementServer::onSocketDisconnected()
|
void ManagementServer::onSocketDisconnected()
|
||||||
{
|
{
|
||||||
m_socket->deleteLater();
|
if (m_socket) m_socket->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
QTcpSocket* ManagementServer::socket() const
|
QPointer<QTcpSocket> ManagementServer::socket() const
|
||||||
{
|
{
|
||||||
if (!isOpen()) {
|
if (!isOpen()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -80,14 +80,16 @@ void ManagementServer::onReadyRead()
|
||||||
bool ManagementServer::start(const QString& host, unsigned int port)
|
bool ManagementServer::start(const QString& host, unsigned int port)
|
||||||
{
|
{
|
||||||
if (m_tcpServer) {
|
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);
|
m_tcpServer->setMaxPendingConnections(1);
|
||||||
|
|
||||||
connect(m_tcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(onAcceptError(QAbstractSocket::SocketError)));
|
connect(m_tcpServer.data(), SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(onAcceptError(QAbstractSocket::SocketError)));
|
||||||
connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
|
connect(m_tcpServer.data(), SIGNAL(newConnection()), this, SLOT(onNewConnection()));
|
||||||
|
|
||||||
if (m_tcpServer->listen(QHostAddress(host), port)) {
|
if (m_tcpServer->listen(QHostAddress(host), port)) {
|
||||||
emit serverStarted();
|
emit serverStarted();
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
#define MANAGEMENTSERVER_H
|
#define MANAGEMENTSERVER_H
|
||||||
|
|
||||||
#include <QAbstractSocket>
|
#include <QAbstractSocket>
|
||||||
|
#include <QPointer>
|
||||||
|
#include <QSharedPointer>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
class QTcpServer;
|
class QTcpServer;
|
||||||
|
@ -22,7 +24,7 @@ public:
|
||||||
QString readLine();
|
QString readLine();
|
||||||
qint64 writeCommand(const QString& message);
|
qint64 writeCommand(const QString& message);
|
||||||
|
|
||||||
QTcpSocket* socket() const;
|
QPointer<QTcpSocket> socket() const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void readyRead();
|
void readyRead();
|
||||||
|
@ -36,8 +38,8 @@ protected slots:
|
||||||
void onSocketError(QAbstractSocket::SocketError socketError);
|
void onSocketError(QAbstractSocket::SocketError socketError);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QTcpServer* m_tcpServer;
|
QSharedPointer<QTcpServer> m_tcpServer;
|
||||||
QTcpSocket* m_socket;
|
QPointer<QTcpSocket> m_socket;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MANAGEMENTSERVER_H
|
#endif // MANAGEMENTSERVER_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue