From ffebc53586bd6ca010dd9ec762639c68f1549137 Mon Sep 17 00:00:00 2001 From: f4exb Date: Fri, 27 Nov 2015 08:49:36 +0100 Subject: [PATCH] TCPSrc channel: process connexion/deconnnexion through messages --- plugins/channel/tcpsrc/tcpsrc.cpp | 36 +++++++++++++++++++++++++++---- plugins/channel/tcpsrc/tcpsrc.h | 21 ++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/plugins/channel/tcpsrc/tcpsrc.cpp b/plugins/channel/tcpsrc/tcpsrc.cpp index 3409492ca..99c11baee 100644 --- a/plugins/channel/tcpsrc/tcpsrc.cpp +++ b/plugins/channel/tcpsrc/tcpsrc.cpp @@ -252,6 +252,19 @@ bool TCPSrc::handleMessage(const Message& cmd) return true; } + else if (MsgTCPConnection::match(cmd)) + { + MsgTCPConnection& con = (MsgTCPConnection&) cmd; + + if (con.getConnect()) + { + processNewConnection(); + } + else + { + processDeconnection(); + } + } else { if(m_spectrum != 0) @@ -275,13 +288,21 @@ void TCPSrc::closeAllSockets(Sockets* sockets) } } + void TCPSrc::onNewConnection() { qDebug("TCPSrc::onNewConnection"); + MsgTCPConnection *cmd = MsgTCPConnection::create(true); + getInputMessageQueue()->push(cmd); +} + +void TCPSrc::processNewConnection() +{ + qDebug("TCPSrc::processNewConnection"); while(m_tcpServer->hasPendingConnections()) { - qDebug("TCPSrc::onNewConnection: has a pending connection"); + qDebug("TCPSrc::processNewConnection: has a pending connection"); QTcpSocket* connection = m_tcpServer->nextPendingConnection(); connection->setSocketOption(QAbstractSocket:: KeepAliveOption, 1); connect(connection, SIGNAL(disconnected()), this, SLOT(onDisconnected())); @@ -301,7 +322,7 @@ void TCPSrc::onNewConnection() case FormatS16LE: { - qDebug("TCPSrc::onNewConnection: establish new S16LE connection"); + qDebug("TCPSrc::processNewConnection: establish new S16LE connection"); quint32 id = (FormatS16LE << 24) | m_nextS16leId; MsgTCPSrcConnection* msg = MsgTCPSrcConnection::create(true, id, connection->peerAddress(), connection->peerPort()); m_nextS16leId = (m_nextS16leId + 1) & 0xffffff; @@ -318,11 +339,18 @@ void TCPSrc::onNewConnection() } void TCPSrc::onDisconnected() +{ + qDebug("TCPSrc::onDisconnected"); + MsgTCPConnection *cmd = MsgTCPConnection::create(false); + getInputMessageQueue()->push(cmd); +} + +void TCPSrc::processDeconnection() { quint32 id; QTcpSocket* socket = 0; - qDebug("TCPSrc::onDisconnected"); + qDebug("TCPSrc::processDeconnection"); for(int i = 0; i < m_ssbSockets.count(); i++) { @@ -342,7 +370,7 @@ void TCPSrc::onDisconnected() { if(m_s16leSockets[i].socket == sender()) { - qDebug("TCPSrc::onDisconnected: remove S16LE socket #%d", i); + qDebug("TCPSrc::processDeconnection: remove S16LE socket #%d", i); id = m_s16leSockets[i].id; socket = m_s16leSockets[i].socket; diff --git a/plugins/channel/tcpsrc/tcpsrc.h b/plugins/channel/tcpsrc/tcpsrc.h index bbb26d0cf..836ae36f6 100644 --- a/plugins/channel/tcpsrc/tcpsrc.h +++ b/plugins/channel/tcpsrc/tcpsrc.h @@ -118,6 +118,25 @@ protected: m_enabled(enabled) { } }; + class MsgTCPConnection : public Message { + MESSAGE_CLASS_DECLARATION + + public: + bool getConnect() const { return m_connect; } + + static MsgTCPConnection* create(bool connect) + { + return new MsgTCPConnection(connect); + } + + private: + bool m_connect; + + MsgTCPConnection(bool connect) : + Message(), + m_connect(connect) + { } + }; MessageQueue* m_uiMessageQueue; TCPSrcGUI* m_tcpSrcGUI; @@ -162,6 +181,8 @@ protected: QMutex m_settingsMutex; void closeAllSockets(Sockets* sockets); + void processNewConnection(); + void processDeconnection(); protected slots: void onNewConnection();