Merge pull request #961 from Lord-Grey/Improve_errohandling

Improve UDP-Device Error handling
This commit is contained in:
LordGrey 2020-09-05 15:14:22 +02:00 committed by GitHub
commit 31b5be39b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 30 deletions

View File

@ -41,41 +41,42 @@ 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));
this->setInError ( errortext );
return false;
} }
else else
{ {
Debug( _log, "Successfully parsed %s as a hostname.", deviceConfig["host"].toString().toStdString().c_str()); QString errortext = QString ("Failed resolving IP-address for [%1], (%2) %3").arg(host).arg(hostInfo.error()).arg(hostInfo.errorString());
_address = info.addresses().first(); this->setInError ( errortext );
isInitOK = false;
} }
} }
int config_port = deviceConfig["port"].toInt(_port); if ( !_isDeviceInError )
if ( config_port <= 0 || config_port > MAX_PORT )
{ {
QString errortext = QString ("Invalid target port [%1]!").arg(config_port); int config_port = deviceConfig["port"].toInt(_port);
this->setInError ( errortext ); if ( config_port <= 0 || config_port > MAX_PORT )
isInitOK = false; {
} QString errortext = QString ("Invalid target port [%1]!").arg(config_port);
else this->setInError ( errortext );
{ isInitOK = false;
_port = static_cast<int>(config_port); }
Debug( _log, "UDP using %s:%d", _address.toString().toStdString().c_str() , _port ); else
{
_port = static_cast<int>(config_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;
@ -89,12 +90,15 @@ int ProviderUdp::open()
// Try to bind the UDP-Socket // Try to bind the UDP-Socket
if ( _udpSocket != nullptr ) if ( _udpSocket != nullptr )
{ {
QHostAddress localAddress = QHostAddress::Any; if ( _udpSocket->state() != QAbstractSocket::BoundState )
quint16 localPort = 0;
if ( !_udpSocket->bind(localAddress, localPort) )
{ {
QString warntext = QString ("Could not bind local address: %1, (%2) %3").arg(localAddress.toString()).arg(_udpSocket->error()).arg(_udpSocket->errorString()); QHostAddress localAddress = QHostAddress::Any;
Warning ( _log, "%s", QSTRING_CSTR(warntext)); quint16 localPort = 0;
if ( !_udpSocket->bind(localAddress, localPort) )
{
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));
}
} }
// Everything is OK, device is ready // Everything is OK, device is ready
_isDeviceReady = true; _isDeviceReady = true;
@ -125,7 +129,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

@ -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;
}; };