Commit bd22e08b authored by Thiago C. Santini's avatar Thiago C. Santini

Adds running state to network stream

As to not try to access the UDP socket after it has been deleted
parent f5792482
...@@ -11,6 +11,8 @@ NetworkStream::~NetworkStream() ...@@ -11,6 +11,8 @@ NetworkStream::~NetworkStream()
void NetworkStream::onThreadStarted() void NetworkStream::onThreadStarted()
{ {
running = true;
if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) { if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
tcpServer = new QTcpServer(); tcpServer = new QTcpServer();
if (! listen() ) if (! listen() )
...@@ -19,18 +21,21 @@ void NetworkStream::onThreadStarted() ...@@ -19,18 +21,21 @@ void NetworkStream::onThreadStarted()
socket = new QUdpSocket(this); socket = new QUdpSocket(this);
socket->bind(QHostAddress(settings.value("dataStream/ip").toString()), settings.value("dataStream/port").toInt()); socket->bind(QHostAddress(settings.value("dataStream/ip").toString()), settings.value("dataStream/port").toInt());
socket->connectToHost(QHostAddress(settings.value("dataStream/ip").toString()), settings.value("dataStream/port").toInt()); socket->connectToHost(QHostAddress(settings.value("dataStream/ip").toString()), settings.value("dataStream/port").toInt());
socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
} }
} }
void NetworkStream::onThreadFinishing() void NetworkStream::onThreadFinishing()
{ {
running = false;
if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) { if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
tcpServer->close(); tcpServer->close();
tcpServer->deleteLater(); tcpServer->deleteLater();
if (connected) if (connected)
socket->deleteLater(); socket->deleteLater();
} else if (settings.value("dataStream/transportProtocol").toString().compare("UDP", Qt::CaseInsensitive) == 0) { } else if (settings.value("dataStream/transportProtocol").toString().compare("UDP", Qt::CaseInsensitive) == 0) {
delete socket; socket->deleteLater();
} }
qDebug() << QThread::currentThread()->objectName() << " done."; qDebug() << QThread::currentThread()->objectName() << " done.";
} }
...@@ -62,7 +67,7 @@ void NetworkStream::closeSocket() ...@@ -62,7 +67,7 @@ void NetworkStream::closeSocket()
void NetworkStream::push(JournalEntry journalEntry) void NetworkStream::push(JournalEntry journalEntry)
{ {
if (!socket) if (!running)
return; return;
if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) { if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
......
...@@ -28,6 +28,7 @@ public slots: ...@@ -28,6 +28,7 @@ public slots:
QTcpServer *tcpServer; QTcpServer *tcpServer;
QAbstractSocket *socket; QAbstractSocket *socket;
bool connected; bool connected;
bool running;
bool listen(); bool listen();
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment