fixed segfault on multiple incoming connections

master
bernhard.trinnes 2020-04-14 21:16:08 +07:00
parent ac6dcf0765
commit ab7c906bd4
7 changed files with 124 additions and 69 deletions

View File

@ -68,7 +68,7 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info)
if (tcpServer->isValid()) {
m_tcpServer.insert(tcpServer, thing);
connect(tcpServer, &TcpServer::connectionChanged, this, &IntegrationPluginTcpCommander::onTcpServerConnectionChanged);
connect(tcpServer, &TcpServer::connectionCountChanged, this, &IntegrationPluginTcpCommander::onTcpServerConnectionCountChanged);
connect(tcpServer, &TcpServer::commandReceived, this, &IntegrationPluginTcpCommander::onTcpServerCommandReceived);
return info->finish(Thing::ThingErrorNoError);
} else {
@ -128,13 +128,20 @@ void IntegrationPluginTcpCommander::onTcpSocketConnectionChanged(bool connected)
}
void IntegrationPluginTcpCommander::onTcpServerConnectionChanged(bool connected)
void IntegrationPluginTcpCommander::onTcpServerConnectionCountChanged(int connections)
{
TcpServer *tcpServer = static_cast<TcpServer *>(sender());
Thing *thing = m_tcpServer.value(tcpServer);
qDebug(dcTCPCommander()) << thing->name() << "Tcp Server Client connected" ;
if (!thing)
return;
qDebug(dcTCPCommander()) << thing->name() << "Tcp Server Client connected";
if (thing->thingClassId() == tcpInputThingClassId) {
thing->setStateValue(tcpInputConnectedStateTypeId, connected);
if (connections > 0) {
thing->setStateValue(tcpInputConnectedStateTypeId, true);
} else {
thing->setStateValue(tcpInputConnectedStateTypeId, false);
}
thing->setStateValue(tcpInputConnectionCountStateTypeId, connections);
}
}

View File

@ -58,7 +58,7 @@ private:
private slots:
void onTcpSocketConnectionChanged(bool connected);
void onTcpServerConnectionChanged(bool connected);
void onTcpServerConnectionCountChanged(int connections);
void onTcpServerCommandReceived(QByteArray message);
};

View File

@ -1,6 +1,6 @@
{
"name": "TCPCommander",
"displayName": "tcp commander",
"displayName": "Tcp Commander",
"id": "741b7b0a-0c9c-4c93-be99-0d0bcf5a4643",
"vendors": [
{
@ -80,14 +80,24 @@
"displayName": "Connected",
"type": "bool",
"defaultValue": false,
"cached": false,
"displayNameEvent": "Connection status changed"
},
{
"id": "e959417f-55d4-4316-88f4-d3fa29976841",
"name": "connectionCount",
"displayName": "Connection count",
"type": "int",
"defaultValue": 0,
"cached": false,
"displayNameEvent": "Connection count changed"
}
],
"eventTypes": [
{
"id": "6d7c6df6-cb61-4d9e-b0d7-37c43911ca4b",
"name": "triggered",
"displayName": "Data recieved",
"displayName": "Data received",
"paramTypes": [
{
"id": "97d7ee8c-d9db-40b4-9855-4ceecd64c411",

View File

@ -45,7 +45,6 @@ TcpServer::TcpServer(const QHostAddress address, const quint16 &port, QObject *p
}
}
TcpServer::TcpServer(const quint16 &port, QObject *parent) :
QObject(parent)
{
@ -77,38 +76,46 @@ int TcpServer::serverPort()
return m_tcpServer->serverPort();
}
int TcpServer::connectionCount()
{
return m_connectionCount;
}
void TcpServer::newConnection()
{
qDebug(dcTCPCommander()) << "TCP Server new Connection request";
m_socket = m_tcpServer->nextPendingConnection();
m_socket->flush();
QTcpSocket *socket = m_tcpServer->nextPendingConnection();
socket->flush();
emit connectionChanged(true);
connect(m_socket, &QTcpSocket::disconnected, this, &TcpServer::onDisconnected);
connect(m_socket, &QTcpSocket::readyRead, this, &TcpServer::readData);
m_connectionCount++;
emit connectionCountChanged(m_connectionCount);
connect(socket, &QTcpSocket::disconnected, this, &TcpServer::onDisconnected);
connect(socket, &QTcpSocket::readyRead, this, &TcpServer::readData);
// Note: error signal will be interpreted as function, not as signal in C++11
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
}
void TcpServer::onDisconnected()
{
qDebug(dcTCPCommander()) << "TCP Server connection aborted";
disconnect(m_socket, &QTcpSocket::disconnected, this, &TcpServer::onDisconnected);
disconnect(m_socket, &QTcpSocket::readyRead, this, &TcpServer::readData);
m_socket->deleteLater();
emit connectionChanged(false);
m_connectionCount--;
if (m_connectionCount < 0)
m_connectionCount = 0;
emit connectionCountChanged(m_connectionCount);
}
void TcpServer::readData()
{
QByteArray data = m_socket->readAll();
QTcpSocket *socket = static_cast<QTcpSocket *>(sender());
QByteArray data = socket->readAll();
qDebug(dcTCPCommander()) << "TCP Server data received: " << data;
m_socket->write("OK\n");
socket->write("OK\n");
emit commandReceived(data);
}
void TcpServer::onError(QAbstractSocket::SocketError error)
{
qWarning(dcTCPCommander()) << "Socket Error" << m_socket->errorString() << error;
QTcpSocket *socket = static_cast<QTcpSocket *>(sender());
qWarning(dcTCPCommander()) << "Socket Error" << socket->errorString() << error;
}

View File

@ -50,15 +50,16 @@ public:
void setPort(int port);
void setServerAddress(const QHostAddress &address);
int connectionCount();
private:
QTcpServer *m_tcpServer = nullptr;
QTcpSocket *m_socket = nullptr;
int m_connectionCount = 0;
signals:
void newPendingConnection();
void commandReceived(QByteArray message);
void connectionChanged(bool connected);
void connectionCountChanged(int connections);
private slots:
void newConnection();

View File

@ -6,39 +6,33 @@
<message>
<location filename="../integrationplugintcpcommander.cpp" line="56"/>
<source>Error connecting to remote server.</source>
<translation type="unfinished"></translation>
<translation>Fehler beim Herstellen einer Verbindung zum Remote-Server.</translation>
</message>
<message>
<location filename="../integrationplugintcpcommander.cpp" line="77"/>
<source>Error opening TCP port.</source>
<translation type="unfinished"></translation>
<translation>Fehler beim Öffnen des TCP-Ports.</translation>
</message>
</context>
<context>
<name>TCPCommander</name>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="85"/>
<source>tcp commander</source>
<extracomment>The name of the plugin TCPCommander ({741b7b0a-0c9c-4c93-be99-0d0bcf5a4643})</extracomment>
<translation>TCP Commander</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="79"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="91"/>
<source>TCP Output</source>
<extracomment>The name of the ThingClass ({c67d059f-694f-47cb-8e1d-9e3e6d014c1a})</extracomment>
<translation>TCP Ausgang</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="64"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="76"/>
<source>IPv4 Address</source>
<extracomment>The name of the ParamType (ThingClass: tcpOutput, Type: thing, ID: {2a3fcb23-931b-4ba1-b134-c49b656c76f7})</extracomment>
<translation>IPv4 Adresse</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="37"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="40"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="43"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="46"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="49"/>
<source>Connected</source>
<extracomment>The name of the ParamType (ThingClass: tcpInput, EventType: connected, ID: {a2eb1619-261c-45ee-9587-6b5994633ad0})
----------
@ -47,26 +41,41 @@ The name of the StateType ({a2eb1619-261c-45ee-9587-6b5994633ad0}) of ThingClass
The name of the ParamType (ThingClass: tcpOutput, EventType: connected, ID: {725b541a-9e0c-4634-81eb-e415c0b8f025})
----------
The name of the StateType ({725b541a-9e0c-4634-81eb-e415c0b8f025}) of ThingClass tcpOutput</extracomment>
<translation type="unfinished"></translation>
<translation>Verbunden</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="49"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="52"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="55"/>
<source>Connection count</source>
<extracomment>The name of the ParamType (ThingClass: tcpInput, EventType: connectionCount, ID: {e959417f-55d4-4316-88f4-d3fa29976841})
----------
The name of the StateType ({e959417f-55d4-4316-88f4-d3fa29976841}) of ThingClass tcpInput</extracomment>
<translation>Verbindungsanzahl</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="58"/>
<source>Connection count changed</source>
<extracomment>The name of the EventType ({e959417f-55d4-4316-88f4-d3fa29976841}) of ThingClass tcpInput</extracomment>
<translation>Verbindungsanzahl geändert</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="61"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="64"/>
<source>Connection status changed</source>
<extracomment>The name of the EventType ({a2eb1619-261c-45ee-9587-6b5994633ad0}) of ThingClass tcpInput
----------
The name of the EventType ({725b541a-9e0c-4634-81eb-e415c0b8f025}) of ThingClass tcpOutput</extracomment>
<translation type="unfinished"></translation>
<translation>Verbindungsstatus geändert</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="61"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="73"/>
<source>Data recieved</source>
<extracomment>The name of the EventType ({6d7c6df6-cb61-4d9e-b0d7-37c43911ca4b}) of ThingClass tcpInput</extracomment>
<translation type="unfinished"></translation>
<translation>Daten empfangen</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="67"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="70"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="79"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="82"/>
<source>Port</source>
<extracomment>The name of the ParamType (ThingClass: tcpInput, Type: thing, ID: {88eb227d-13f7-451c-babf-1b141c219fd4})
----------
@ -74,20 +83,26 @@ The name of the ParamType (ThingClass: tcpOutput, Type: thing, ID: {bee8b151-815
<translation>Port</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="82"/>
<source>nymea</source>
<extracomment>The name of the vendor ({2062d64d-3232-433c-88bc-0d33c0ba2ba6})</extracomment>
<translation type="unfinished"></translation>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="94"/>
<source>Tcp Commander</source>
<extracomment>The name of the plugin TCPCommander ({741b7b0a-0c9c-4c93-be99-0d0bcf5a4643})</extracomment>
<translation>TCP Commander</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="73"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="97"/>
<source>nymea</source>
<extracomment>The name of the vendor ({2062d64d-3232-433c-88bc-0d33c0ba2ba6})</extracomment>
<translation>nymea</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="85"/>
<source>Send Data</source>
<extracomment>The name of the ActionType ({6bc52462-b192-46a4-a6df-92cc5a479c89}) of ThingClass tcpOutput</extracomment>
<translation>Sende Daten</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="55"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="58"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="67"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="70"/>
<source>Data</source>
<extracomment>The name of the ParamType (ThingClass: tcpInput, EventType: triggered, ID: {97d7ee8c-d9db-40b4-9855-4ceecd64c411})
----------
@ -95,7 +110,7 @@ The name of the ParamType (ThingClass: tcpOutput, ActionType: trigger, ID: {6604
<translation>Daten</translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="76"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="88"/>
<source>TCP Input</source>
<extracomment>The name of the ThingClass ({bc40e84a-69c4-4fd2-beb0-bd65f19aa8ff})</extracomment>
<translation>TCP Eingang</translation>

View File

@ -17,28 +17,22 @@
<context>
<name>TCPCommander</name>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="85"/>
<source>tcp commander</source>
<extracomment>The name of the plugin TCPCommander ({741b7b0a-0c9c-4c93-be99-0d0bcf5a4643})</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="79"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="91"/>
<source>TCP Output</source>
<extracomment>The name of the ThingClass ({c67d059f-694f-47cb-8e1d-9e3e6d014c1a})</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="64"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="76"/>
<source>IPv4 Address</source>
<extracomment>The name of the ParamType (ThingClass: tcpOutput, Type: thing, ID: {2a3fcb23-931b-4ba1-b134-c49b656c76f7})</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="37"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="40"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="43"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="46"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="49"/>
<source>Connected</source>
<extracomment>The name of the ParamType (ThingClass: tcpInput, EventType: connected, ID: {a2eb1619-261c-45ee-9587-6b5994633ad0})
----------
@ -50,8 +44,23 @@ The name of the StateType ({725b541a-9e0c-4634-81eb-e415c0b8f025}) of ThingClass
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="49"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="52"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="55"/>
<source>Connection count</source>
<extracomment>The name of the ParamType (ThingClass: tcpInput, EventType: connectionCount, ID: {e959417f-55d4-4316-88f4-d3fa29976841})
----------
The name of the StateType ({e959417f-55d4-4316-88f4-d3fa29976841}) of ThingClass tcpInput</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="58"/>
<source>Connection count changed</source>
<extracomment>The name of the EventType ({e959417f-55d4-4316-88f4-d3fa29976841}) of ThingClass tcpInput</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="61"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="64"/>
<source>Connection status changed</source>
<extracomment>The name of the EventType ({a2eb1619-261c-45ee-9587-6b5994633ad0}) of ThingClass tcpInput
----------
@ -59,14 +68,14 @@ The name of the EventType ({725b541a-9e0c-4634-81eb-e415c0b8f025}) of ThingClass
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="61"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="73"/>
<source>Data recieved</source>
<extracomment>The name of the EventType ({6d7c6df6-cb61-4d9e-b0d7-37c43911ca4b}) of ThingClass tcpInput</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="67"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="70"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="79"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="82"/>
<source>Port</source>
<extracomment>The name of the ParamType (ThingClass: tcpInput, Type: thing, ID: {88eb227d-13f7-451c-babf-1b141c219fd4})
----------
@ -74,20 +83,26 @@ The name of the ParamType (ThingClass: tcpOutput, Type: thing, ID: {bee8b151-815
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="82"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="94"/>
<source>Tcp Commander</source>
<extracomment>The name of the plugin TCPCommander ({741b7b0a-0c9c-4c93-be99-0d0bcf5a4643})</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="97"/>
<source>nymea</source>
<extracomment>The name of the vendor ({2062d64d-3232-433c-88bc-0d33c0ba2ba6})</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="73"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="85"/>
<source>Send Data</source>
<extracomment>The name of the ActionType ({6bc52462-b192-46a4-a6df-92cc5a479c89}) of ThingClass tcpOutput</extracomment>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="55"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="58"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="67"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="70"/>
<source>Data</source>
<extracomment>The name of the ParamType (ThingClass: tcpInput, EventType: triggered, ID: {97d7ee8c-d9db-40b4-9855-4ceecd64c411})
----------
@ -95,7 +110,7 @@ The name of the ParamType (ThingClass: tcpOutput, ActionType: trigger, ID: {6604
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="76"/>
<location filename="../../../build-nymea-plugins-Desktop-Debug/tcpcommander/plugininfo.h" line="88"/>
<source>TCP Input</source>
<extracomment>The name of the ThingClass ({bc40e84a-69c4-4fd2-beb0-bd65f19aa8ff})</extracomment>
<translation type="unfinished"></translation>