Skip to content

Commit

Permalink
Fixes and enhancements to UDP protocol support
Browse files Browse the repository at this point in the history
  • Loading branch information
vranki committed Mar 3, 2021
1 parent e533a2e commit 4f823d4
Show file tree
Hide file tree
Showing 19 changed files with 208 additions and 211 deletions.
1 change: 0 additions & 1 deletion UDP.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,3 @@ Repeated double_count times:

* ref_id (2, uint16) - ref id
* value (8, double) - value of the double dataref

34 changes: 23 additions & 11 deletions clients/extplane-client-qt/clientdataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ void ClientDataRef::setName(QString &name) {
emit nameChanged(m_name);
}

QString ClientDataRef::value() const {
return m_values.isEmpty() ? "" : m_values.first();
QString ClientDataRef::value() {
if(m_values.isEmpty()) {
m_values.append(valueString());
}
return m_values.first();
}

int ClientDataRef::valueInt()
Expand Down Expand Up @@ -74,12 +77,22 @@ void ClientDataRef::clientDestroyed() {
setClient(nullptr);
}

QString ClientDataRef::valueString()
{
if(m_valueIntValid) return QString::number(m_valueInt);
if(!qIsNaN(m_valueFloat)) return QString::number(m_valueFloat);
if(!qIsNaN(m_valueDouble)) return QString::number(m_valueDouble);
// qDebug() << Q_FUNC_INFO << "No value set for" << name();
return "";
}

void ClientDataRef::invalidateValue()
{
// Invalidate value types
m_valueIntValid = false;
m_valueFloat = std::nanf("");
m_valueDouble = std::nan("");
m_values.clear();
}

void ClientDataRef::setDataFormat(QString dataFormat) {
Expand All @@ -95,25 +108,19 @@ QStringList& ClientDataRef::values() {
}

void ClientDataRef::updateValue(QString newValue) {
if(m_changedOnce && !m_values.isEmpty() && newValue == m_values.first()) return;
invalidateValue();

if(m_values.isEmpty()) {
m_values.push_back(newValue);
} else {
m_values.replace(0, newValue);
}
m_changedOnce = true;

invalidateValue();

emit changed(this);
}

void ClientDataRef::updateValue(QStringList &newValues) {
if(m_changedOnce && newValues == m_values) return;
m_values = newValues;
m_changedOnce = true;
invalidateValue();
m_values = newValues;
emit changed(this);
}

Expand All @@ -122,25 +129,30 @@ void ClientDataRef::updateValue(int newValue)
invalidateValue();
m_valueInt = newValue;
m_valueIntValid = true;
emit changed(this);
}

void ClientDataRef::updateValue(float newValue)
{
invalidateValue();
m_valueFloat = newValue;
emit changed(this);
}

void ClientDataRef::updateValue(double newValue)
{
invalidateValue();
m_valueDouble = newValue;
emit changed(this);
}

void ClientDataRef::setValue(QString _newValue, int index) {
if(m_values.size() <= 1)
invalidateValue();

while(m_values.size() < index + 1) // Resize list if needed
m_values.append(QString(""));
m_values[index] = _newValue;
invalidateValue();
emit valueSet(this);
}

Expand Down
4 changes: 2 additions & 2 deletions clients/extplane-client-qt/clientdataref.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ClientDataRef : public QObject {
QString& name();
QStringList& values(); // Returns all values
double accuracy();
QString value() const; // Returns first value
QString value(); // Returns first value
int valueInt();
float valueFloat();
double valueDouble();
Expand Down Expand Up @@ -80,6 +80,7 @@ private slots:
void clientDestroyed();

private:
QString valueString(); // Generate string from raw values, if possible
void invalidateValue();
QString m_name;
QStringList m_values; // Length 1 if not array
Expand All @@ -91,7 +92,6 @@ private slots:
int m_subscribers = 0;
ExtPlaneClient* m_client = nullptr;
QString m_dataFormat;
bool m_changedOnce = false; // False until first update sent.
QStringList m_modifiers;
quint16 m_udpId = 0;
};
Expand Down
22 changes: 13 additions & 9 deletions clients/extplane-client-qt/extplaneconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ bool ExtPlaneConnection::isConnected() const
ClientDataRef *ExtPlaneConnection::subscribeDataRef(QString name, double accuracy) {
ClientDataRef *ref = dataRefs[name];
if(ref) {
DEBUG << QString("Ref %1 already subscribed %2 times").arg(name).arg(ref->subscribers());
// DEBUG << QString("Ref %1 already subscribed %2 times").arg(name).arg(ref->subscribers());
ref->setSubscribers(ref->subscribers()+1);
if(accuracy < ref->accuracy()) {
if(server_ok)
subRef(ref); // Re-subscribe with higher accuracy
}
} else {
DEBUG << QString("Subscribing to new ref %1").arg(name);
// DEBUG << "Subscribing to new ref" << name << "accuracy" << accuracy;
ref = createDataRef(name, accuracy);

dataRefs[ref->name()] = ref;
Expand All @@ -97,7 +97,6 @@ ClientDataRef *ExtPlaneConnection::createDataRef(QString name, double accuracy)
}

void ExtPlaneConnection::unsubscribeDataRef(ClientDataRef *ref) {
qDebug() << Q_FUNC_INFO << ref->name();
if(m_udpClient)
m_udpClient->unsubscribedRef(ref);

Expand All @@ -117,11 +116,11 @@ void ExtPlaneConnection::receivedLineSlot(QString & line) {
if(line=="EXTPLANE 1") {
server_ok = true;
emit connectionMessage(QString("Connected to ExtPlane at %1:%2").arg(hostName()).arg(port()));
DEBUG << "Setting update interval to " << m_updateInterval;
// DEBUG << "Setting update interval to " << m_updateInterval;
setUpdateInterval(m_updateInterval);
// Sub all refs
for(auto &refPair : dataRefs) {
DEBUG << "Subscribing to ref" << refPair.second->name();
// DEBUG << "Subscribing to ref" << refPair.second->name();
subRef(refPair.second);
}
emit connectedChanged(true);
Expand Down Expand Up @@ -154,7 +153,7 @@ void ExtPlaneConnection::receivedLineSlot(QString & line) {
m_udpClient = new ExtPlaneUdpClient(m_clientId, this);
}
m_udpClient->subscribedRef(ref);
INFO << "Tracking ref via UDP" << cmd.value(3) << id;
// INFO << "Tracking ref via UDP" << cmd.value(3) << id;
} else {
INFO << "Can't find dataref" << cmd.value(3);
}
Expand Down Expand Up @@ -212,14 +211,14 @@ void ExtPlaneConnection::subRef(ClientDataRef *ref) {
writeLine(subLine);
}

void ExtPlaneConnection::writeLine(QString line) {
void ExtPlaneConnection::writeLine(const QString &line) {
if(!server_ok) {
emit connectionMessage(QString("Connection not ok! Tried to write %1").arg(line));
DEBUG << "Connection not ok! Tried to write" << line;
return;
}
line +="\n";
write(line.toUtf8());
write("\n");
}

void ExtPlaneConnection::keyPress(int id) {
Expand Down Expand Up @@ -253,7 +252,12 @@ void ExtPlaneConnection::commandEnd(QString name) {
}

void ExtPlaneConnection::setValue(QString name, QString value) {
QString line = "set " + name + " " + value;
if(value.isEmpty()) {
DEBUG << "Trying to set empty value for ref" << name;
return;
}

QString line = QString("set %1 %2").arg(name, value);
writeLine(line);
}

Expand Down
2 changes: 1 addition & 1 deletion clients/extplane-client-qt/extplaneconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private slots:

protected:
void subRef(ClientDataRef *ref);
virtual void writeLine(QString line);
virtual void writeLine(const QString &line);
virtual ClientDataRef *createDataRef(QString newName, double accuracy = 0);
std::set<ExtPlaneClient*> clients;
std::map<QString, ClientDataRef*> dataRefs;
Expand Down
18 changes: 9 additions & 9 deletions clients/extplane-client-qt/extplaneudpclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

ExtPlaneUdpClient::ExtPlaneUdpClient(quint8 clientId, QObject *parent) : QObject(parent)
, m_clientId(clientId)
{
m_udpPort = UDP_OUT_PORT_BASE + m_clientId;
, m_udpPort(UDP_OUT_PORT_BASE + m_clientId)
{
if(m_socket.bind(m_udpPort)) {
connect(&m_socket, &QUdpSocket::readyRead, this, &ExtPlaneUdpClient::readPendingDatagrams);
INFO << "UDP listener bound to port" << m_udpPort;
Expand All @@ -25,26 +25,26 @@ void ExtPlaneUdpClient::unsubscribedRef(ClientDataRef *ref) {
m_idRefMap.erase(ref->udpId());
}

void ExtPlaneUdpClient::readPendingDatagrams()
{
void ExtPlaneUdpClient::readPendingDatagrams() {
while(m_socket.hasPendingDatagrams()) {
QNetworkDatagram dg = m_socket.receiveDatagram();
processDatagram(dg);
}
}

void ExtPlaneUdpClient::processDatagram(QNetworkDatagram &dg)
{
void ExtPlaneUdpClient::processDatagram(QNetworkDatagram &dg) {
QByteArray data = dg.data();
QDataStream s(&data, QIODevice::ReadOnly);
if(!data.startsWith("EXTP_")) {
qWarning() << Q_FUNC_INFO << "Wrong header, not extplane packet??";
qWarning() << Q_FUNC_INFO << "Wrong header, non extplane packet sen to port" << m_udpPort;
return;
}
s.skipRawData(5);

quint8 clientid;
quint16 id;
quint16 dataCount;

s >> clientid;
if(m_clientId && (clientid != m_clientId)) {
// Packet meant for another client, ignore
Expand All @@ -64,10 +64,10 @@ void ExtPlaneUdpClient::processDatagram(QNetworkDatagram &dg)
}
}
s.skipRawData(2);
dataCount = 0;
s >> dataCount;
for(unsigned int i=0;i<dataCount;i++) {
float data;
s.setFloatingPointPrecision(QDataStream::SinglePrecision);
s >> id >> data;
auto ref = m_idRefMap[id];
if(id) {
Expand All @@ -79,10 +79,10 @@ void ExtPlaneUdpClient::processDatagram(QNetworkDatagram &dg)
}
}
s.skipRawData(2);
dataCount = 0;
s >> dataCount;
for(unsigned int i=0;i<dataCount;i++) {
double data;
s.setFloatingPointPrecision(QDataStream::DoublePrecision);
s >> id >> data;
auto ref = m_idRefMap[id];
if(id) {
Expand Down
2 changes: 1 addition & 1 deletion clients/extplane-client-qt/extplaneudpclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private slots:
void dataReceived(quint16 id, QString value);
QUdpSocket m_socket;
std::map<quint16, ClientDataRef*> m_idRefMap;
quint8 m_clientId=0;
quint8 m_clientId = 0;
quint16 m_udpPort = 0;
};

Expand Down
6 changes: 5 additions & 1 deletion extplane-plugin/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ PLUGIN_API void XPluginStop() {

PLUGIN_API void XPluginDisable() {
DEBUG;
if(globalPlugin)
globalPlugin->pluginDisable();
}

PLUGIN_API int XPluginEnable() {
DEBUG;
return 1;
if(globalPlugin)
return globalPlugin->pluginEnable();
return 0;
}

PLUGIN_API void XPluginReceiveMessage(
Expand Down
Loading

0 comments on commit 4f823d4

Please sign in to comment.