abort connection attempts opn disconnect too
This commit is contained in:
parent
8152fdf944
commit
23800e9f97
@ -36,7 +36,7 @@ void NymeaConnection::connect(const QString &url)
|
||||
|
||||
void NymeaConnection::disconnect()
|
||||
{
|
||||
if (!m_currentInterface || !m_currentInterface->isConnected()) {
|
||||
if (!m_currentInterface || m_currentInterface->connectionState() == NymeaInterface::ConnectionStateDisconnected) {
|
||||
qWarning() << "not connected, cannot disconnect";
|
||||
return;
|
||||
}
|
||||
@ -60,7 +60,7 @@ bool NymeaConnection::isTrusted(const QString &url)
|
||||
|
||||
bool NymeaConnection::connected()
|
||||
{
|
||||
return m_currentInterface && m_currentInterface->isConnected();
|
||||
return m_currentInterface && m_currentInterface->connectionState() == NymeaInterface::ConnectionStateConnected;
|
||||
}
|
||||
|
||||
QString NymeaConnection::url() const
|
||||
|
||||
@ -29,13 +29,20 @@ class NymeaInterface : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum ConnectionState {
|
||||
ConnectionStateDisconnected,
|
||||
ConnectionStateConnecting,
|
||||
ConnectionStateConnected
|
||||
};
|
||||
Q_ENUM(ConnectionState)
|
||||
|
||||
explicit NymeaInterface(QObject *parent = 0);
|
||||
|
||||
virtual QStringList supportedSchemes() const = 0;
|
||||
|
||||
virtual void connect(const QUrl &url) = 0;
|
||||
virtual void disconnect() = 0;
|
||||
virtual bool isConnected() const = 0;
|
||||
virtual ConnectionState connectionState() const = 0;
|
||||
virtual void sendData(const QByteArray &data) = 0;
|
||||
virtual void ignoreSslErrors(const QList<QSslError> &errors) = 0;
|
||||
|
||||
|
||||
@ -61,14 +61,28 @@ void TcpSocketInterface::connect(const QUrl &url)
|
||||
}
|
||||
}
|
||||
|
||||
bool TcpSocketInterface::isConnected() const
|
||||
NymeaInterface::ConnectionState TcpSocketInterface::connectionState() const
|
||||
{
|
||||
return m_socket.state() == QAbstractSocket::ConnectedState;
|
||||
switch (m_socket.state()) {
|
||||
case QAbstractSocket::ConnectedState:
|
||||
return NymeaInterface::ConnectionStateConnected;
|
||||
case QAbstractSocket::ConnectingState:
|
||||
case QAbstractSocket::HostLookupState:
|
||||
return NymeaInterface::ConnectionStateConnecting;
|
||||
default:
|
||||
return NymeaInterface::ConnectionStateDisconnected;
|
||||
}
|
||||
}
|
||||
|
||||
void TcpSocketInterface::disconnect()
|
||||
{
|
||||
qDebug() << "closing socket";
|
||||
m_socket.disconnectFromHost();
|
||||
m_socket.close();
|
||||
// QTcpSocket might endlessly wait for a timeout if we call connectToHost() for an IP which isn't
|
||||
// reable at all (e.g. has disappeared from the network). Closing the socket is not enough, we need
|
||||
// abort the exiting connection attempts too.
|
||||
m_socket.abort();
|
||||
}
|
||||
|
||||
void TcpSocketInterface::socketReadyRead()
|
||||
|
||||
@ -16,7 +16,7 @@ public:
|
||||
QStringList supportedSchemes() const override;
|
||||
|
||||
void connect(const QUrl &url) override;
|
||||
bool isConnected() const override;
|
||||
ConnectionState connectionState() const override;
|
||||
void disconnect() override;
|
||||
void sendData(const QByteArray &data) override;
|
||||
void ignoreSslErrors(const QList<QSslError> &errors) override;
|
||||
|
||||
@ -51,14 +51,24 @@ void WebsocketInterface::connect(const QUrl &url)
|
||||
m_socket->open(QUrl(url));
|
||||
}
|
||||
|
||||
bool WebsocketInterface::isConnected() const
|
||||
NymeaInterface::ConnectionState WebsocketInterface::connectionState() const
|
||||
{
|
||||
return m_socket->state() == QAbstractSocket::ConnectedState;
|
||||
switch (m_socket->state()) {
|
||||
case QAbstractSocket::ConnectedState:
|
||||
return NymeaInterface::ConnectionStateConnected;
|
||||
case QAbstractSocket::ConnectingState:
|
||||
case QAbstractSocket::HostLookupState:
|
||||
return NymeaInterface::ConnectionStateConnecting;
|
||||
default:
|
||||
return NymeaInterface::ConnectionStateDisconnected;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void WebsocketInterface::disconnect()
|
||||
{
|
||||
m_socket->close();
|
||||
m_socket->abort();
|
||||
}
|
||||
|
||||
void WebsocketInterface::sendData(const QByteArray &data)
|
||||
|
||||
@ -35,7 +35,7 @@ public:
|
||||
QStringList supportedSchemes() const override;
|
||||
|
||||
void connect(const QUrl &url) override;
|
||||
bool isConnected() const override;
|
||||
ConnectionState connectionState() const override;
|
||||
void disconnect() override;
|
||||
void sendData(const QByteArray &data) override;
|
||||
void ignoreSslErrors(const QList<QSslError> &errors) override;
|
||||
|
||||
Reference in New Issue
Block a user