Merge PR #716: Fixes in NymeaConnection

This commit is contained in:
Jenkins nymea 2021-12-01 00:41:48 +01:00
commit e88de18d8f
3 changed files with 32 additions and 4 deletions

View File

@ -81,6 +81,17 @@ NymeaConnection::NymeaConnection(QObject *parent) : QObject(parent)
});
}
NymeaConnection::~NymeaConnection()
{
QList<NymeaTransportInterfaceFactory*> deletedTransports;
foreach (NymeaTransportInterfaceFactory* transport, m_transportFactories) {
if (!deletedTransports.contains(transport)) {
delete transport;
deletedTransports.append(transport);
}
}
}
NymeaConnection::BearerTypes NymeaConnection::availableBearerTypes() const
{
return m_availableBearerTypes;
@ -312,7 +323,11 @@ void NymeaConnection::onDisconnected()
return;
}
m_transportCandidates.remove(m_currentTransport);
delete m_currentTransport;
disconnect(m_currentTransport);
disconnect(m_currentTransport, nullptr, nullptr, nullptr);
// FIXME: directly deleting crashes with a stack trace that never passes any of our code.
// Seems to happen for both, Tcp and WebSocket
m_currentTransport->deleteLater();
m_currentTransport = nullptr;
foreach (NymeaTransportInterface *candidate, m_transportCandidates.keys()) {
@ -330,6 +345,7 @@ void NymeaConnection::onDisconnected()
emit connectedChanged(false);
}
if (!m_currentHost) {
return;
}
@ -345,6 +361,16 @@ void NymeaConnection::onDisconnected()
}
}
void NymeaConnection::onDataAvailable(const QByteArray &data)
{
NymeaTransportInterface *t = static_cast<NymeaTransportInterface*>(sender());
if (t == m_currentTransport) {
emit dataAvailable(data);
} else {
qCDebug(dcNymeaConnection()) << "Received data from a transport that is not the current one:" << t->url();
}
}
void NymeaConnection::updateActiveBearers()
{
NymeaConnection::BearerTypes availableBearerTypes;
@ -502,12 +528,12 @@ bool NymeaConnection::connectInternal(Connection *connection)
}
// Create a new transport
NymeaTransportInterface* newTransport = m_transportFactories.value(connection->url().scheme())->createTransport();
NymeaTransportInterface* newTransport = m_transportFactories.value(connection->url().scheme())->createTransport(this);
QObject::connect(newTransport, &NymeaTransportInterface::sslErrors, this, &NymeaConnection::onSslErrors);
QObject::connect(newTransport, &NymeaTransportInterface::error, this, &NymeaConnection::onError);
QObject::connect(newTransport, &NymeaTransportInterface::connected, this, &NymeaConnection::onConnected);
QObject::connect(newTransport, &NymeaTransportInterface::disconnected, this, &NymeaConnection::onDisconnected);
QObject::connect(newTransport, &NymeaTransportInterface::dataReady, this, &NymeaConnection::dataAvailable);
QObject::connect(newTransport, &NymeaTransportInterface::dataReady, this, &NymeaConnection::onDataAvailable);
// // Load any certificate we might have for this url
// QByteArray pem;

View File

@ -82,6 +82,7 @@ public:
};
Q_ENUM(ConnectionStatus)
explicit NymeaConnection(QObject *parent = nullptr);
~NymeaConnection();
void registerTransport(NymeaTransportInterfaceFactory *transportFactory);
@ -118,6 +119,7 @@ private slots:
void onError(QAbstractSocket::SocketError error);
void onConnected();
void onDisconnected();
void onDataAvailable(const QByteArray &data);
void updateActiveBearers();
void hostConnectionsUpdated();

View File

@ -258,7 +258,7 @@ bool NymeaHostsFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &s
if (m_jsonRpcClient && !m_showUneachableBearers) {
bool hasReachableConnection = false;
for (int i = 0; i < host->connections()->rowCount(); i++) {
qCritical() << "checking host for available bearer" << host->name() << host->connections()->get(i)->url() << "available bearer types:" << m_jsonRpcClient->availableBearerTypes() << "hosts bearer types" << host->connections()->get(i)->bearerType();
// qCritical() << "checking host for available bearer" << host->name() << host->connections()->get(i)->url() << "available bearer types:" << m_jsonRpcClient->availableBearerTypes() << "hosts bearer types" << host->connections()->get(i)->bearerType();
// Either enable a connection when the Bearer type is directly available
switch (host->connections()->get(i)->bearerType()) {
case Connection::BearerTypeLan: