Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-65604

add QNetworkProxy support to QMqttClient/QMqttConnection

    XMLWordPrintable

Details

    • Task
    • Resolution: Out of scope
    • P2: Important
    • None
    • 5.10.0
    • MQTT
    • None

    Description

      I just try the MQTT modules based on Qt 5.10 and had some issues on connecting to public broker (behind a proxy).

      In this case I just added setProxy(..)/proxy() methods to the QMqttClient and forward this proxy to the QMqttConnection class (assigend to socket in method QMqttConnection ::ensureTransport)

      Without setting a QNetworkProxy it is not possible to run a connection behind a system proxy.

      So it is a kind of important to add this feature to next release.

      1. initial m_proxy to QNetworkProxy::NoProxy

      QMqttConnection::QMqttConnection(QObject *parent) : QObject(parent)
      {
          m_proxy = QNetworkProxy::NoProxy;
          m_pingTimer.setSingleShot(false);
          m_pingTimer.connect(&m_pingTimer, &QTimer::timeout, this, &QMqttConnection::sendControlPingRequest);
      }
      

      2. assign proxy to current socket

      bool QMqttConnection::ensureTransport(bool createSecureIfNeeded)
      {
          qCDebug(lcMqttConnection) << Q_FUNC_INFO << m_transport;
      
          if (m_transport)
              return true;
      
          // We are asked to create a transport layer
          if (m_clientPrivate->m_hostname.isEmpty() || m_clientPrivate->m_port == 0) {
              qWarning("Trying to create a transport layer, but no hostname is specified");
              return false;
          }
          auto socket =
      #ifndef QT_NO_SSL
                  createSecureIfNeeded ? new QSslSocket() :
      #endif
                                         new QTcpSocket();
          m_transport = socket;
          m_ownTransport = true;
          m_transportType = createSecureIfNeeded ? QMqttClient::SecureSocket : QMqttClient::AbstractSocket;
      
          // assign proxy to current socket
          socket->setProxy(m_proxy);
      
          connect(socket, &QAbstractSocket::connected, this, &QMqttConnection::transportConnectionEstablished);
          connect(socket, &QAbstractSocket::disconnected, this, &QMqttConnection::transportConnectionClosed);
          connect(m_transport, &QIODevice::aboutToClose, this, &QMqttConnection::transportConnectionClosed);
          connect(m_transport, &QIODevice::readyRead, this, &QMqttConnection::transportReadReady);
          return true;
      }
      

      3. add setter/getter methods to QMqttConnection for proxy

      4. add setter/getter methods to QMqttClient for proxy

      void QMqttClient::setProxy(const QNetworkProxy &proxy)
      {
          Q_D(QMqttClient);
          if (d->m_connection.proxy() == proxy)
              return;
      
          d->m_connection.setProxy(proxy);
          emit proxyChanged(proxy);
      }
      

      5. extend example to use QNetworkProxy

      ...
          QNetworkProxy proxy;
          proxy.setType(QNetworkProxy::HttpProxy);
          proxy.setHostName("private.proxy.domain");
          proxy.setPort(80);
          proxy.setUser("username");
          proxy.setPassword("password");
          QNetworkProxy::setApplicationProxy(proxy);
      
          m_client = new QMqttClient(this);
          m_client->setProxy(proxy);
          m_client->setHostname(ui->lineEditHost->text());
          m_client->setPort(ui->spinBoxPort->value());
      ...
      

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            mkalinow Maurice Kalinowski
            jorgenvikinggod Jürgen Skrotzky
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 1 hour
                1h
                Remaining:
                Remaining Estimate - 1 hour
                1h
                Logged:
                Time Spent - Not Specified
                Not Specified

                Gerrit Reviews

                  There are no open Gerrit changes