Work on issue #90

This commit is contained in:
Alex Spataru 2022-01-23 22:45:51 -06:00
parent 3cd9a799f0
commit 415d934ab0
5 changed files with 66 additions and 1 deletions

View File

@ -90,6 +90,7 @@ Item {
property alias udpLocalPort: network.udpLocalPort
property alias udpRemotePort: network.udpRemotePort
property alias udpMulticastEnabled: network.udpMulticastEnabled
property alias udpProcessDatagramsDirectly: network.udpProcessDatagramsDirectly
//
// MQTT settings

View File

@ -37,6 +37,7 @@ Control {
property alias udpRemotePort: _udpRemotePort.text
property alias socketType: _typeCombo.currentIndex
property alias udpMulticastEnabled: _udpMulticast.checked
property alias udpProcessDatagramsDirectly: _udpProcessDatagrams.checked
//
// Signals
@ -241,6 +242,30 @@ Control {
root.uiChanged()
}
}
//
// UDP multicast checkbox
//
Label {
text: qsTr("Each datagram is a frame") + ":"
opacity: _udpProcessDatagrams.enabled ? 1 : 0.5
visible: Cpp_IO_Network.socketTypeIndex === 1
} CheckBox {
id: _udpProcessDatagrams
opacity: enabled ? 1 : 0.5
Layout.alignment: Qt.AlignLeft
Layout.leftMargin: -app.spacing
checked: Cpp_IO_Network.udpIgnoreFrameSequences
visible: Cpp_IO_Network.socketTypeIndex === 1
enabled: Cpp_IO_Network.socketTypeIndex === 1 && !Cpp_IO_Manager.connected
onCheckedChanged: {
if (Cpp_IO_Network.udpIgnoreFrameSequences !== checked)
Cpp_IO_Network.udpIgnoreFrameSequences = checked
root.uiChanged()
}
}
}
//

View File

@ -31,6 +31,7 @@ IO::DataSources::Network::Network()
: m_hostExists(false)
, m_udpMulticast(false)
, m_lookupActive(false)
, m_udpIgnoreFrameSequences(false)
{
setRemoteAddress("");
setTcpPort(defaultTcpPort());
@ -155,6 +156,15 @@ StringList IO::DataSources::Network::socketTypes() const
return StringList { "TCP", "UDP" };
}
/**
* Returns @c true if the @c IO::Manager should not check for start/end
* frame sequences when an UDP datagram is received.
*/
bool IO::DataSources::Network::udpIgnoreFrameSequences() const
{
return m_udpIgnoreFrameSequences;
}
/**
* Returns the socket type. Valid return values are:
*
@ -288,6 +298,19 @@ void IO::DataSources::Network::setRemoteAddress(const QString &address)
Q_EMIT addressChanged();
}
/**
* If @a ignore is set to @c true, then the @c IO::Manager should not check
* for start/end frame sequences when an UDP datagram is received.
*
* Otherwise, the @c IO::Manager shall check for start/end sequences to identify
* incoming frames.
*/
void IO::DataSources::Network::setUdpIgnoreFrameSequences(const bool ignore)
{
m_udpIgnoreFrameSequences = ignore;
Q_EMIT udpIgnoreFrameSequencesChanged();
}
/**
* Performs a DNS lookup for the given @a host name
*/

View File

@ -90,6 +90,10 @@ class Network : public QObject
READ udpMulticast
WRITE setUdpMulticast
NOTIFY udpMulticastChanged)
Q_PROPERTY(bool udpIgnoreFrameSequences
READ udpIgnoreFrameSequences
WRITE setUdpIgnoreFrameSequences
NOTIFY udpIgnoreFrameSequencesChanged)
// clang-format on
Q_SIGNALS:
@ -98,6 +102,7 @@ Q_SIGNALS:
void socketTypeChanged();
void udpMulticastChanged();
void lookupActiveChanged();
void udpIgnoreFrameSequencesChanged();
private:
explicit Network();
@ -122,6 +127,7 @@ public:
int socketTypeIndex() const;
bool configurationOk() const;
StringList socketTypes() const;
bool udpIgnoreFrameSequences() const;
QAbstractSocket::SocketType socketType() const;
QTcpSocket *tcpSocket() { return &m_tcpSocket; }
@ -145,6 +151,7 @@ public Q_SLOTS:
void setSocketTypeIndex(const int index);
void setUdpRemotePort(const quint16 port);
void setRemoteAddress(const QString &address);
void setUdpIgnoreFrameSequences(const bool ignore);
void setSocketType(const QAbstractSocket::SocketType type);
private Q_SLOTS:
@ -159,6 +166,7 @@ private:
bool m_lookupActive;
quint16 m_udpLocalPort;
quint16 m_udpRemotePort;
bool m_udpIgnoreFrameSequences;
QAbstractSocket::SocketType m_socketType;
QTcpSocket m_tcpSocket;

View File

@ -514,10 +514,18 @@ void IO::Manager::onDataReceived()
auto udpSocket = DataSources::Network::instance().udpSocket();
while (udpSocket->hasPendingDatagrams())
{
// Read datagram data
QByteArray datagram;
datagram.resize(int(udpSocket->pendingDatagramSize()));
udpSocket->readDatagram(datagram.data(), datagram.size());
data.append(datagram);
// Add datagram to data buffer
if (!DataSources::Network::instance().udpIgnoreFrameSequences())
data.append(datagram);
// Ingore start/end sequences & process frame directly
else
processPayload(datagram);
}
}
}