diff --git a/.travis.yml b/.travis.yml index fa50637..2240a83 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,13 @@ language: cpp compiler: - gcc before_install: -- sudo apt-get update -qq -- sudo apt-get install -qq qtbase5-dev qtdeclarative5-dev qt5-default build-essential wget git unzip autoconf autopoint bison flex gperf intltool libtool ruby scons p7zip-full python3 libgdk-pixbuf2.0-dev libffi-dev + - sudo apt-get update -qq + - sudo apt-get -yq install gnupg + - sudo echo "deb http://pkg.mxe.cc/repos/apt/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mxeapt.list + - sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys D43A795B73B16ABE9643FE1AFD8FFF16DB45C6AB + - sudo apt-get update -qq + - sudo apt-get install -yq mxe-x86-64-w64-mingw32.static-qtbase mxe-x86-64-w64-mingw32.static-qtdeclarative + - sudo apt-get install -qq qtbase5-dev qtdeclarative5-dev qt5-default build-essential script: ./scripts/ci-build.sh branches: only: @@ -17,7 +22,6 @@ notifications: on_failure: change cache: directories: - - ../mxe - ../XPlaneSDK deploy: provider: releases diff --git a/clients/extplane-client-qt/clientdataref.cpp b/clients/extplane-client-qt/clientdataref.cpp index 8bfdf81..95f697f 100644 --- a/clients/extplane-client-qt/clientdataref.cpp +++ b/clients/extplane-client-qt/clientdataref.cpp @@ -1,55 +1,82 @@ #include "clientdataref.h" +#include "extplaneclient.h" -ClientDataRef::ClientDataRef(QObject *parent, QString newName, double accuracy) : QObject(parent), _name(newName), _accuracy(accuracy) { - _subscribers = 0; - _values.reserve(1); // Always at least 1 size +ClientDataRef::ClientDataRef() : QObject(nullptr) + , m_accuracy(0) + , m_subscribers(0) + ,m_client(nullptr) +{ + m_values.reserve(1); // Always at least 1 size +} + +ClientDataRef::ClientDataRef(QObject *parent, QString newName, double accuracy) : QObject(parent) + , m_name(newName) + , m_accuracy(accuracy) + , m_subscribers(0) + , m_client(nullptr) { + m_values.reserve(1); // Always at least 1 size } QString& ClientDataRef::name() { - return _name; + return m_name; +} + +void ClientDataRef::setName(QString &name) +{ + if (m_name == name) + return; + + m_name = name; + emit nameChanged(m_name); } -QString& ClientDataRef::valueString() { - return _values.first(); +QString ClientDataRef::value() +{ + return m_values.isEmpty() ? "" : m_values.first(); } -QStringList& ClientDataRef::valueStrings() { - return _values; +ExtPlaneClient *ClientDataRef::client() const +{ + return m_client; +} + +QStringList& ClientDataRef::values() { + return m_values; } void ClientDataRef::updateValue(QString newValue) { - if(!_values.isEmpty() && newValue == _values.first()) return; + if(!m_values.isEmpty() && newValue == m_values.first()) return; - if(_values.isEmpty()) { - _values.push_back(newValue); + if(m_values.isEmpty()) { + m_values.push_back(newValue); } else { - _values.replace(0, newValue); + m_values.replace(0, newValue); } emit changed(this); } void ClientDataRef::updateValue(QStringList &newValues) { - if(newValues == _values) return; - _values = newValues; + if(newValues == m_values) return; + m_values = newValues; emit changed(this); } double ClientDataRef::accuracy() { - return _accuracy; + return m_accuracy; } int ClientDataRef::subscribers() { - return _subscribers; + return m_subscribers; } void ClientDataRef::setSubscribers(int sub) { - _subscribers = sub; + m_subscribers = sub; } -void ClientDataRef::setValue(double _newValue, int index) { - if(_values.size() < index + 1) - _values.reserve(index+1); - _values[index] = QString::number(_newValue); +void ClientDataRef::setValue(QString _newValue, int index) { + if(m_values.size() < index + 1) + m_values.reserve(index+1); + m_values[index] = _newValue; emit valueSet(this); } @@ -57,6 +84,40 @@ void ClientDataRef::unsubscribe() { emit unsubscribed(this); } + +void ClientDataRef::setAccuracy(double accuracy) +{ + qWarning("Floating point comparison needs context sanity check"); + if (qFuzzyCompare(m_accuracy, accuracy)) + return; + + m_accuracy = accuracy; + emit accuracyChanged(m_accuracy); +} + +void ClientDataRef::setValues(QStringList values) +{ + if(values == m_values) return; + m_values = values; + emit changed(this); +} + +void ClientDataRef::setClient(ExtPlaneClient *client) +{ + if (m_client == client) + return; + + m_client = client; + emit clientChanged(m_client); +} + +void ClientDataRef::subscribeIfPossible() +{ + if(m_client && !m_name.isEmpty()) { + m_client->subscribeDataRef(m_name, m_accuracy); + } +} + bool ClientDataRef::isArray() { - return _values.size() > 1; + return m_values.size() > 1; } diff --git a/clients/extplane-client-qt/clientdataref.h b/clients/extplane-client-qt/clientdataref.h index 1eb3551..b033a76 100644 --- a/clients/extplane-client-qt/clientdataref.h +++ b/clients/extplane-client-qt/clientdataref.h @@ -4,6 +4,8 @@ #include #include +class ExtPlaneClient; + /** * Represents a single dataref in client side. Can contain multiple * values, if ref is a array. @@ -16,28 +18,50 @@ */ class ClientDataRef : public QObject { Q_OBJECT + Q_PROPERTY(ExtPlaneClient* client READ client WRITE setClient NOTIFY clientChanged) + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + Q_PROPERTY(QString value READ value WRITE setValue NOTIFY changed) + Q_PROPERTY(QStringList values READ values WRITE setValues NOTIFY changed) + Q_PROPERTY(double accuracy READ accuracy WRITE setAccuracy NOTIFY accuracyChanged) + public: + explicit ClientDataRef(); explicit ClientDataRef(QObject *parent, QString newName, double accuracy=0); QString& name(); - QString& valueString(); // Returns first value - QStringList& valueStrings(); // Returns all values + QStringList& values(); // Returns all values double accuracy(); - void updateValue(QString newValue); // Update single value - void updateValue(QStringList &newValues); // Update full array + void updateValue(QString newValue); // Update single value (from simulator) + void updateValue(QStringList &newValues); // Update full array (from simulator) int subscribers(); void setSubscribers(int sub); - void setValue(double _newValue, int index=0); bool isArray(); void unsubscribe(); // Call to unsubscribe ref. + QString value(); // Returns first value + ExtPlaneClient* client() const; + +public slots: + void setName(QString &name); + void setAccuracy(double accuracy); + void setValue(QString _newValue, int index=0); + void setValues(QStringList values); + void setClient(ExtPlaneClient* client); + signals: void changed(ClientDataRef *ref); void valueSet(ClientDataRef *ref); void unsubscribed(ClientDataRef *ref); + void nameChanged(QString name); + void accuracyChanged(double accuracy); + void clientChanged(ExtPlaneClient* client); + private: - QString _name; - QStringList _values; - double _accuracy; - int _subscribers; + void subscribeIfPossible(); + + QString m_name; + QStringList m_values; + double m_accuracy; + int m_subscribers; + ExtPlaneClient* m_client; }; #endif // CLIENTDATAREF_H diff --git a/clients/extplane-client-qt/clientdatarefprovider.cpp b/clients/extplane-client-qt/clientdatarefprovider.cpp index 335f9c3..3b117c3 100644 --- a/clients/extplane-client-qt/clientdatarefprovider.cpp +++ b/clients/extplane-client-qt/clientdatarefprovider.cpp @@ -1,9 +1 @@ #include "clientdatarefprovider.h" - -ClientDataRefProvider::ClientDataRefProvider() -{ -} - -ClientDataRefProvider::~ClientDataRefProvider() -{ -} diff --git a/clients/extplane-client-qt/clientdatarefprovider.h b/clients/extplane-client-qt/clientdatarefprovider.h index 22143dc..1ea198d 100644 --- a/clients/extplane-client-qt/clientdatarefprovider.h +++ b/clients/extplane-client-qt/clientdatarefprovider.h @@ -12,19 +12,17 @@ class ClientDataRefProvider { public: - ClientDataRefProvider(); - virtual ~ClientDataRefProvider(); - virtual ClientDataRef *subscribeDataRef(QString name, double accuracy=0)=0; - virtual void unsubscribeDataRef(ClientDataRef *ref)=0; + virtual ClientDataRef *subscribeDataRef(QString name, double accuracy = 0) = 0; + virtual void unsubscribeDataRef(ClientDataRef *ref) = 0; // virtual void getDataRef(QString name)=0; // get command not supported yet - virtual void keyPress(int id)=0; - virtual void buttonPress(int id)=0; - virtual void buttonRelease(int id)=0; + virtual void keyPress(int id) = 0; + virtual void buttonPress(int id) = 0; + virtual void buttonRelease(int id) = 0; virtual void commandOnce(QString name) = 0; virtual void commandBegin(QString name) = 0; virtual void commandEnd(QString name) = 0; - virtual void setValue(QString name, QString value)=0; // For single value - virtual void setValues(QString name, QStringList values)=0; // For arrays + virtual void setValue(QString name, QString value) = 0; // For single value + virtual void setValues(QString name, QStringList values) = 0; // For arrays }; #endif // CLIENTDATAREFPROVIDER_H diff --git a/clients/extplane-client-qt/extplaneclient.cpp b/clients/extplane-client-qt/extplaneclient.cpp index 3d62bb5..e678cb9 100644 --- a/clients/extplane-client-qt/extplaneclient.cpp +++ b/clients/extplane-client-qt/extplaneclient.cpp @@ -1,54 +1,59 @@ #include "extplaneclient.h" #include "../../util/console.h" -ExtPlaneClient::ExtPlaneClient(QObject *parent, QString name, ClientDataRefProvider *drp) : - QObject(parent), _name(name), _connection(drp) -{ -} +ExtPlaneClient::ExtPlaneClient() : QObject() + , m_name("ExtPlane Qt client") + , m_connection(nullptr) +{} + +ExtPlaneClient::ExtPlaneClient(QObject *parent, QString name, ClientDataRefProvider *drp) : QObject(parent) + , m_name(name) + , m_connection(drp) +{} ExtPlaneClient::~ExtPlaneClient() { - foreach(ClientDataRef *ref, _dataRefs) { - _dataRefs.removeOne(ref); - _connection->unsubscribeDataRef(ref); + for(ClientDataRef *ref : m_dataRefs) { + m_dataRefs.removeOne(ref); + m_connection->unsubscribeDataRef(ref); } } ClientDataRef* ExtPlaneClient::subscribeDataRef(QString name, double accuracy) { - ClientDataRef *ref = _connection->subscribeDataRef(name, accuracy); + ClientDataRef *ref = m_connection->subscribeDataRef(name, accuracy); connect(ref, SIGNAL(changed(ClientDataRef*)), this, SLOT(cdrChanged(ClientDataRef*))); connect(ref, SIGNAL(destroyed(QObject*)), this, SLOT(refDestroyed(QObject*))); - _dataRefs.append(ref); + m_dataRefs.append(ref); return ref; } void ExtPlaneClient::refDestroyed(QObject* refqo) { - _dataRefs.removeOne(static_cast(refqo)); + m_dataRefs.removeOne(static_cast(refqo)); } void ExtPlaneClient::cdrChanged(ClientDataRef *ref) { double value; bool ok; if(ref->isArray()) { - emit refChanged(ref->name(), ref->valueStrings()); + emit refChanged(ref->name(), ref->values()); } else { // Try to convert to double and forward to corresponding slot per default // If that fails, we fallback to the string slot // TODO: Is this really a nice solution? - value = ref->valueString().toDouble(&ok); + value = ref->value().toDouble(&ok); if (ok){ emit refChanged(ref->name(), value); } else { - emit refChanged(ref->name(), ref->valueString()); + emit refChanged(ref->name(), ref->value()); } } } void ExtPlaneClient::unsubscribeDataRef(QString name) { DEBUG << name; - foreach(ClientDataRef *ref, _dataRefs) { + for(ClientDataRef *ref : m_dataRefs) { if(ref->name() == name) { - _dataRefs.removeOne(ref); - _connection->unsubscribeDataRef(ref); + m_dataRefs.removeOne(ref); + m_connection->unsubscribeDataRef(ref); return; } } @@ -56,7 +61,7 @@ void ExtPlaneClient::unsubscribeDataRef(QString name) { } bool ExtPlaneClient::isDataRefSubscribed(QString name) { - foreach(ClientDataRef *ref, _dataRefs) { + for(ClientDataRef *ref : m_dataRefs) { if(ref->name() == name) { return true; } @@ -65,41 +70,55 @@ bool ExtPlaneClient::isDataRefSubscribed(QString name) { } void ExtPlaneClient::keyPress(int id) { - _connection->keyPress(id); + m_connection->keyPress(id); } void ExtPlaneClient::buttonPress(int id) { - _heldButtons.insert(id); - _connection->buttonPress(id); + m_heldButtons.insert(id); + m_connection->buttonPress(id); } void ExtPlaneClient::buttonRelease(int id) { - if(!_heldButtons.contains(id)) return; - _heldButtons.remove(id); - _connection->buttonRelease(id); + if(!m_heldButtons.contains(id)) return; + m_heldButtons.remove(id); + m_connection->buttonRelease(id); } void ExtPlaneClient::commandOnce(QString name) { - _connection->commandOnce(name); + m_connection->commandOnce(name); } void ExtPlaneClient::commandBegin(QString name) { - _runningCommands.insert(name); - _connection->commandBegin(name); + m_runningCommands.insert(name); + m_connection->commandBegin(name); } void ExtPlaneClient::commandEnd(QString name) { - if(!_runningCommands.contains(name)) return; - _runningCommands.remove(name); - _connection->commandEnd(name); + if(!m_runningCommands.contains(name)) return; + m_runningCommands.remove(name); + m_connection->commandEnd(name); +} + +ClientDataRefProvider *ExtPlaneClient::datarefProvider() const +{ + return m_connection; } void ExtPlaneClient::setUpdateInterval(double newInterval) { Q_UNUSED(newInterval); } +void ExtPlaneClient::setDatarefProvider(ClientDataRefProvider *datarefProvider) +{ + if (m_connection == datarefProvider) + return; + + m_connection = datarefProvider; + emit datarefProviderChanged(m_connection); +} + void ExtPlaneClient::valueSet(ClientDataRef *ref) { - _connection->setValue(ref->name(), ref->valueString()); + m_connection->setValue(ref->name(), ref->value()); } void ExtPlaneClient::unsubscribed(ClientDataRef *ref) { diff --git a/clients/extplane-client-qt/extplaneclient.h b/clients/extplane-client-qt/extplaneclient.h index c702d5c..871e9bc 100644 --- a/clients/extplane-client-qt/extplaneclient.h +++ b/clients/extplane-client-qt/extplaneclient.h @@ -13,7 +13,10 @@ */ class ExtPlaneClient : public QObject { Q_OBJECT + Q_PROPERTY(ClientDataRefProvider* datarefProvider READ datarefProvider WRITE setDatarefProvider NOTIFY datarefProviderChanged) + public: + explicit ExtPlaneClient(); explicit ExtPlaneClient(QObject *parent, QString name, ClientDataRefProvider *drp); ~ExtPlaneClient(); ClientDataRef* subscribeDataRef(QString name, double accuracy=0); @@ -25,13 +28,17 @@ class ExtPlaneClient : public QObject { void commandOnce(QString name); void commandBegin(QString name); void commandEnd(QString name); + ClientDataRefProvider* datarefProvider() const; + public slots: void setUpdateInterval(double newInterval); + void setDatarefProvider(ClientDataRefProvider* datarefProvider); signals: void refChanged(QString name, double value); void refChanged(QString name, QString value); void refChanged(QString name, QStringList values); + void datarefProviderChanged(ClientDataRefProvider* datarefProvider); private slots: void cdrChanged(ClientDataRef *ref); @@ -40,12 +47,11 @@ private slots: void refDestroyed(QObject* refqo); private: - QString _name; - QList _dataRefs; - QSet _heldButtons; - QSet _runningCommands; - - ClientDataRefProvider *_connection; // The actual connection class + QString m_name; + QList m_dataRefs; + QSet m_heldButtons; + QSet m_runningCommands; + ClientDataRefProvider *m_connection; // The actual connection class }; #endif // EXTPLANECLIENT_H diff --git a/clients/extplane-client-qt/extplaneconnection.cpp b/clients/extplane-client-qt/extplaneconnection.cpp index 9d24425..ceea87e 100644 --- a/clients/extplane-client-qt/extplaneconnection.cpp +++ b/clients/extplane-client-qt/extplaneconnection.cpp @@ -201,9 +201,9 @@ void ExtPlaneConnection::setValues(QString name, QStringList values) { void ExtPlaneConnection::setValue(ClientDataRef *ref) { if(!ref->isArray()) { - setValue(ref->name(), ref->valueString()); + setValue(ref->name(), ref->value()); } else { - setValues(ref->name(), ref->valueStrings()); + setValues(ref->name(), ref->values()); } } diff --git a/clients/extplane-client-qt/simulatedextplaneconnection.cpp b/clients/extplane-client-qt/simulatedextplaneconnection.cpp index a36f813..1de6fbc 100644 --- a/clients/extplane-client-qt/simulatedextplaneconnection.cpp +++ b/clients/extplane-client-qt/simulatedextplaneconnection.cpp @@ -4,9 +4,7 @@ #include "simulateddatarefs/alternatingsimulateddataref.h" #include "../../util/console.h" -SimulatedExtPlaneConnection::SimulatedExtPlaneConnection(QObject *parent) : - ExtPlaneConnection(parent) -{ +SimulatedExtPlaneConnection::SimulatedExtPlaneConnection(QObject *parent) : ExtPlaneConnection(parent) { enableSimulatedRefs = true; } @@ -19,7 +17,7 @@ void SimulatedExtPlaneConnection::startConnection(QString host, unsigned int por ClientDataRef *SimulatedExtPlaneConnection::createDataRef(QString name, double accuracy) { Q_UNUSED(accuracy); - SimulatedDataRef *simRef = 0; + SimulatedDataRef *simRef = nullptr; if(name=="sim/cockpit2/gauges/indicators/airspeed_kts_pilot") { simRef = new SimulatedDataRef(this, 0, 200, 50.0, false, 0, name); } else if(name=="sim/cockpit2/gauges/indicators/altitude_ft_pilot") { @@ -115,7 +113,7 @@ void SimulatedExtPlaneConnection::unsubscribeDataRef(ClientDataRef *ref) { simulatedRefs.removeOne(simRef); } } - disconnect(ref, 0, this, 0); + disconnect(ref, nullptr, this, nullptr); } void SimulatedExtPlaneConnection::writeLine(QString line) { diff --git a/extplane-server/tcpserver.cpp b/extplane-server/tcpserver.cpp index a6f1148..d73a544 100644 --- a/extplane-server/tcpserver.cpp +++ b/extplane-server/tcpserver.cpp @@ -5,7 +5,6 @@ #include "console.h" TcpServer::TcpServer(QObject *parent, DataRefProvider *refProvider) : QObject(parent) - , server(this) , _refProvider(nullptr) , _clientCount(0) { connect(&server, &QTcpServer::newConnection, this, &TcpServer::clientConnected); diff --git a/extplane-transformer/datasources/flightgeardatasource.cpp b/extplane-transformer/datasources/flightgeardatasource.cpp index ddadf93..1aa2620 100644 --- a/extplane-transformer/datasources/flightgeardatasource.cpp +++ b/extplane-transformer/datasources/flightgeardatasource.cpp @@ -8,8 +8,8 @@ FlightGearDataSource::FlightGearDataSource() : DataSource() { this, &FlightGearDataSource::connectedChanged); connect(&tcpClient, &BasicTcpClient::receivedLine, this, &FlightGearDataSource::readLine); - connect(&tcpClient, &BasicTcpClient::networkError, - this, &FlightGearDataSource::gotNetworkError); + connect(&tcpClient, &BasicTcpClient::networkErrorChanged, + this, &FlightGearDataSource::networkErrorChanged); // Fill the mappings of X-Plane <-> FlightGear data refs. refMap.insert("sim/cockpit2/gauges/indicators/heading_electric_deg_mag_pilot", "/instrumentation/magnetic-compass/indicated-heading-deg"); @@ -107,7 +107,7 @@ void FlightGearDataSource::readLine(QString line) } -void FlightGearDataSource::gotNetworkError(QString errstring) +void FlightGearDataSource::networkErrorChanged(QString errstring) { setNetworkError(errstring); } diff --git a/extplane-transformer/datasources/flightgeardatasource.h b/extplane-transformer/datasources/flightgeardatasource.h index 39d6e6d..3e15715 100644 --- a/extplane-transformer/datasources/flightgeardatasource.h +++ b/extplane-transformer/datasources/flightgeardatasource.h @@ -42,7 +42,7 @@ class FlightGearDataSource : public DataSource private slots: void connectedChanged(bool connected); void readLine(QString line); - void gotNetworkError(QString errstring); + void networkErrorChanged(QString errstring); private: BasicTcpClient tcpClient; diff --git a/scripts/ci-build.sh b/scripts/ci-build.sh index b395df0..382d8f4 100755 --- a/scripts/ci-build.sh +++ b/scripts/ci-build.sh @@ -20,33 +20,6 @@ else ls -lh ../XPlaneSDK fi -# For some reason mxe dir stays after cache clean, make sure it's deleted or valid -if [ ! -f ../mxe/.git ] ; then - rm -rf ../mxe -fi - - -# mxe deps: -# apt install p7zip-full intltool gperf autopoint - -# Get mxe if needed.. -if [ ! -d ../mxe ] ; then - pushd .. -# use pre-built binary as building with travis is way too slow.. - wget http://www.modeemi.fi/~cosmo/mxe.tar.gz - tar xvfz mxe.tar.gz -# git clone https://github.com/mxe/mxe.git - popd -fi - -# Build mxe if needed -#pushd ../mxe -# echo mxe dir: -# ls -# git pull -# make MXE_TARGETS=x86_64-w64-mingw32.static -j`nproc` qt5 -#popd - # Build for linux first.. qmake -r make diff --git a/scripts/cross-compile-win64-from-lin.sh b/scripts/cross-compile-win64-from-lin.sh index 3c83f23..ec1f596 100755 --- a/scripts/cross-compile-win64-from-lin.sh +++ b/scripts/cross-compile-win64-from-lin.sh @@ -1,24 +1,20 @@ #!/bin/bash # # This script compiles ExtPlane plugin for windows 64bit from Linux host. -# Uses mxe cross-compile environment. +# Uses mxe cross-compile environment. Install mxe as debian packages first. # # Make sure XPlaneSDK is installed in directory next to ExtPlane. -# mxe will be downloaded if it doesn't exist. # # Example: # # src/ # ExtPlane -# mxe # XPlaneSDK # # Run this script from the ExtPlane dir: # # ./scripts/cross-compile-win64-from-lin.sh # -# If mxe complains about some dependencies missing, please -# install them. # # Good luck! # @@ -28,15 +24,18 @@ if [ ! -d ../ExtPlane ] ; then exit -1 fi -if [ ! -d ../mxe ] ; then - echo "mxe must be installed in ../mxe!" +if [ ! -d /usr/lib/mxe ] ; then + echo "mxe must be installed in /usr/lib/mxe!" fi -PATH=${PWD}/../mxe/usr/bin:$PATH +PATH=/usr/lib/mxe/usr/bin/:$PATH + +target=x86_64-w64-mingw32.static +mxedir=/usr/lib/mxe/ echo echo Starting ExtPlane build echo -../mxe/usr/x86_64-w64-mingw32.static/qt5/bin/qmake -r +$mxedir/usr/$target/qt5/bin/qmake -r make diff --git a/util/basictcpclient.cpp b/util/basictcpclient.cpp index 7e04921..ead9748 100644 --- a/util/basictcpclient.cpp +++ b/util/basictcpclient.cpp @@ -5,10 +5,11 @@ BasicTcpClient::BasicTcpClient(QObject *parent) : QTcpSocket(parent) , m_lineEnding("\n") , m_port(0) { - connect(this, SIGNAL(connected()), this, SLOT(socketConnected())); connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError))); connect(this, SIGNAL(readyRead()), this, SLOT(readClient())); + connect(this, SIGNAL(stateChanged(QAbstractSocket::SocketState)), + this, SLOT(socketStateChanged(QAbstractSocket::SocketState))); connect(&reconnectTimer, SIGNAL(timeout()), this, SLOT(tryReconnect())); reconnectTimer.setSingleShot(false); } @@ -54,6 +55,11 @@ bool BasicTcpClient::connected() const return state() == ConnectedState; } +QString BasicTcpClient::networkError() const +{ + return m_networkError; +} + void BasicTcpClient::setLineEnding(QString lineEnding) { if (m_lineEnding == lineEnding) @@ -92,17 +98,22 @@ void BasicTcpClient::tryReconnect() { } } -void BasicTcpClient::socketConnected() { - reconnectTimer.stop(); - connectedChanged(true); +void BasicTcpClient::socketStateChanged(QAbstractSocket::SocketState state) +{ + if(connected()) { + reconnectTimer.stop(); + m_networkError = ""; + emit networkErrorChanged(m_networkError); + } + emit connectedChanged(state == QTcpSocket::ConnectedState); } void BasicTcpClient::socketError(QAbstractSocket::SocketError err) { Q_UNUSED(err); INFO << "Socket error:" << errorString(); - emit networkError(errorString() + " : " + m_host + ":" + QString::number(m_port)); + m_networkError = errorString() + " : " + m_host + ":" + QString::number(m_port); + emit networkErrorChanged(m_networkError); emit connectionMessage(errorString() + " : " + hostName() + ":" + QString::number(port())); - connectedChanged(false); } void BasicTcpClient::readClient() { diff --git a/util/basictcpclient.h b/util/basictcpclient.h index 6459e0c..27851da 100644 --- a/util/basictcpclient.h +++ b/util/basictcpclient.h @@ -18,6 +18,7 @@ class BasicTcpClient : public QTcpSocket Q_PROPERTY(QString hostName READ hostName WRITE setHostName NOTIFY connectionChanged) Q_PROPERTY(int port READ port WRITE setPort NOTIFY connectionChanged) Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged) + Q_PROPERTY(QString networkError READ networkError NOTIFY networkErrorChanged) public: explicit BasicTcpClient(QObject *parent = nullptr); @@ -29,6 +30,8 @@ class BasicTcpClient : public QTcpSocket int port() const; bool connected() const; + QString networkError() const; + public slots: void setLineEnding(QString lineEnding); void setHostName(QString hostName); @@ -37,21 +40,22 @@ public slots: signals: void receivedLine(QString & line); void connectionMessage(QString text); - void networkError(QString errorString); + void networkErrorChanged(QString errorString); void lineEndingChanged(QString lineEnding); void connectionChanged(); void connectedChanged(bool connected); private slots: - void socketConnected(); void socketError(QAbstractSocket::SocketError err); void readClient(); void tryReconnect(); + void socketStateChanged(QAbstractSocket::SocketState state); private: QTimer reconnectTimer; QString m_lineEnding; QString m_host; + QString m_networkError; int m_port; };