Improve UDP-Error handling

This commit is contained in:
LordGrey 2020-08-09 17:43:23 +02:00
parent 4099d12d9a
commit 16353a5906
2 changed files with 38 additions and 34 deletions

View File

@ -45,26 +45,26 @@ bool ProviderUdp::init(const QJsonObject &deviceConfig)
if (_address.setAddress(host) ) if (_address.setAddress(host) )
{ {
Debug( _log, "Successfully parsed %s as an ip address.", deviceConfig["host"].toString().toStdString().c_str()); Debug( _log, "Successfully parsed %s as an IP-address.", QSTRING_CSTR(_address.toString()));
} }
else else
{ {
Debug( _log, "Failed to parse [%s] as an ip address.", deviceConfig["host"].toString().toStdString().c_str()); QHostInfo hostInfo = QHostInfo::fromName(host);
QHostInfo info = QHostInfo::fromName(host); if ( hostInfo.error() == QHostInfo::NoError )
if (info.addresses().isEmpty())
{ {
Debug( _log, "Failed to parse [%s] as a hostname.", deviceConfig["host"].toString().toStdString().c_str()); _address = hostInfo.addresses().first();
QString errortext = QString ("Invalid target address [%1]!").arg(host); Debug( _log, "Successfully resolved IP-address (%s) for hostname (%s).", QSTRING_CSTR(_address.toString()), QSTRING_CSTR(host));
}
else
{
QString errortext = QString ("Failed resolving IP-address for [%1], (%2) %3").arg(host).arg(hostInfo.error()).arg(hostInfo.errorString());
this->setInError ( errortext ); this->setInError ( errortext );
return false; isInitOK = false;
}
else
{
Debug( _log, "Successfully parsed %s as a hostname.", deviceConfig["host"].toString().toStdString().c_str());
_address = info.addresses().first();
} }
} }
if ( !_isDeviceInError )
{
int config_port = deviceConfig["port"].toInt(_port); int config_port = deviceConfig["port"].toInt(_port);
if ( config_port <= 0 || config_port > MAX_PORT ) if ( config_port <= 0 || config_port > MAX_PORT )
{ {
@ -75,13 +75,14 @@ bool ProviderUdp::init(const QJsonObject &deviceConfig)
else else
{ {
_port = static_cast<int>(config_port); _port = static_cast<int>(config_port);
Debug( _log, "UDP using %s:%d", _address.toString().toStdString().c_str() , _port ); Debug( _log, "UDP socket will write to %s:%u", QSTRING_CSTR(_address.toString()) , _port );
_udpSocket = new QUdpSocket(this); _udpSocket = new QUdpSocket(this);
isInitOK = true; isInitOK = true;
} }
} }
}
return isInitOK; return isInitOK;
} }
@ -92,6 +93,8 @@ int ProviderUdp::open()
// Try to bind the UDP-Socket // Try to bind the UDP-Socket
if ( _udpSocket != nullptr ) if ( _udpSocket != nullptr )
{
if ( _udpSocket->state() != QAbstractSocket::BoundState )
{ {
QHostAddress localAddress = QHostAddress::Any; QHostAddress localAddress = QHostAddress::Any;
quint16 localPort = 0; quint16 localPort = 0;
@ -100,6 +103,7 @@ int ProviderUdp::open()
QString warntext = QString ("Could not bind local address: %1, (%2) %3").arg(localAddress.toString()).arg(_udpSocket->error()).arg(_udpSocket->errorString()); QString warntext = QString ("Could not bind local address: %1, (%2) %3").arg(localAddress.toString()).arg(_udpSocket->error()).arg(_udpSocket->errorString());
Warning ( _log, "%s", QSTRING_CSTR(warntext)); Warning ( _log, "%s", QSTRING_CSTR(warntext));
} }
}
// Everything is OK, device is ready // Everything is OK, device is ready
_isDeviceReady = true; _isDeviceReady = true;
retval = 0; retval = 0;
@ -129,7 +133,7 @@ int ProviderUdp::close()
return retval; return retval;
} }
int ProviderUdp::writeBytes(unsigned size, const uint8_t * data) int ProviderUdp::writeBytes(const unsigned size, const uint8_t * data)
{ {
qint64 retVal = _udpSocket->writeDatagram((const char *)data,size,_address,_port); qint64 retVal = _udpSocket->writeDatagram((const char *)data,size,_address,_port);

View File

@ -26,7 +26,7 @@ public:
/// ///
/// @brief Destructor of the UDP LED-device /// @brief Destructor of the UDP LED-device
/// ///
virtual ~ProviderUdp() override; ~ProviderUdp() override;
protected: protected:
@ -36,21 +36,21 @@ protected:
/// @param[in] deviceConfig the JSON device configuration /// @param[in] deviceConfig the JSON device configuration
/// @return True, if success /// @return True, if success
/// ///
virtual bool init(const QJsonObject &deviceConfig) override; bool init(const QJsonObject &deviceConfig) override;
/// ///
/// @brief Opens the output device. /// @brief Opens the output device.
/// ///
/// @return Zero on success (i.e. device is ready), else negative /// @return Zero on success (i.e. device is ready), else negative
/// ///
virtual int open() override; int open() override;
/// ///
/// @brief Closes the UDP device. /// @brief Closes the UDP device.
/// ///
/// @return Zero on success (i.e. device is closed), else negative /// @return Zero on success (i.e. device is closed), else negative
/// ///
virtual int close() override; int close() override;
/// ///
/// @brief Writes the given bytes/bits to the UDP-device and sleeps the latch time to ensure that the /// @brief Writes the given bytes/bits to the UDP-device and sleeps the latch time to ensure that the
@ -61,12 +61,12 @@ protected:
/// ///
/// @return Zero on success, else negative /// @return Zero on success, else negative
/// ///
int writeBytes(unsigned size, const uint8_t *data); int writeBytes(const unsigned size, const uint8_t *data);
/// ///
QUdpSocket * _udpSocket; QUdpSocket * _udpSocket;
QHostAddress _address; QHostAddress _address;
ushort _port; quint16 _port;
QString _defaultHost; QString _defaultHost;
}; };