QT NetworkManager初始化和异步问题

时间:2015-04-11 07:07:46

标签: c++ qt qnetworkaccessmanager qnetworkrequest qnetworkreply

首先,我想在创建此类时触发初始化过程。 (对已知主机进行DNS查找,并在指定数量的插槽上与主机进行预握手 - 一旦经过测试,不得对其进行硬编码) - 正在创建所有类,正在读取配置等。

其次,我想允许并行请求发送,具体取决于特定主机的允许(和打开)时隙。

坦率地说,以下代码存在多个问题;

  • Google似乎重定向,同时多次请求此主机似乎导致灾难性故障,当x号或请求触发完成信号,并且该信号调用我的重定向检查并且第一个请求被重定向,我似乎得到了第一个回复,带有x错误消息。
  • 出于同样的原因,似乎无法从httpFinished调用deleteLater()
  • 我找不到办法等待请求的插槽号完成以启动队列中的下一个请求

主要目标是使用单个网络类从多个API收集数据,这些请求可能同时发生,也可能不发生。 还有另外一个请求就像POST等一样发出 - 也许是一个类/ API?

请注意,代码只是测试概念,仅用于测试目的,并且在修复后应该进行清理,这不是我打算工作的方式。

nebula.pro

QT += core network
QT -= gui

TARGET = nebula
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
    network.cpp

HEADERS += \
    network.h

network.h

#ifndef NETWORK_H
#define NETWORK_H

#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QHostInfo>
#include <QtNetwork/QSslConfiguration>
#include <QEventLoop>

class network : public QObject
{
    Q_OBJECT

public:
    explicit network(QObject *parent = 0);
    ~network();
    void sendGet(const QUrl &url);

private:
    QNetworkAccessManager networkAccessManager;
    QHash<QString, QByteArray> usedSession;
    QNetworkReply *reply;
    QUrl url;
    Q_INVOKABLE void init();
    void replyFinished(QUrl &url, QNetworkReply *reply);
    QList<QString> provideTcpHosts();
    QList<QString> provideSslHosts();
    QEventLoop eventLoop;

private slots:
    void httpError();
    void sslErrors(const QList<QSslError> &errors);
    void httpFinished();
};

#endif // NETWORK_H

network.cpp

#include "network.h"

/**
 * @brief network::network
 * initialazing pre-networking initialization once the eventpool is ready
 * @param parent
 */
network::network(QObject *parent) : QObject(parent)
{
    QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection);
}

network::~network()
{
    networkAccessManager.deleteLater();
}

/**
 * @brief network::init
 * dns chache warming for all hosts and pre-connecting to all hosts via 4 sockets
 */
void network::init()
{
    QList<QString> tcpHosts = provideTcpHosts();
    QList<QString> sslHosts = provideSslHosts();

    // tcp hosts initialazation
    for( int i=0; i<tcpHosts.count(); ++i )
    {
        // pre-dns lookup cache warming
        QHostInfo::lookupHost(tcpHosts[i], 0, 0);
        qDebug() << "pre-dns lookup cache warming for: " + tcpHosts[i];
        // tcp pre-handshake with known hosts over 4 sockets with known http hosts
        for(int a=0; a<4; ++a)
        {
            networkAccessManager.connectToHost(tcpHosts[i], 80);
            qDebug() << "connecting " + QString::number(a+1) + "th socket for " + tcpHosts[i];
        }

    }

    // tcp hosts initialazation
    for( int i=0; i<sslHosts.count(); ++i )
    {
        // pre-dns lookup cache warming
        QHostInfo::lookupHost(sslHosts[i], 0, 0);
        qDebug() << "pre-dns lookup cache warming for: " + sslHosts[i];

        // tcp pre-handshake with known hosts over 4 sockets with known http hosts
        for(int a=0; a<4; ++a)
        {
            networkAccessManager.connectToHostEncrypted(sslHosts[i], 443);
            qDebug() << "connecting " + QString::number(a+1) + "th socket for " + sslHosts[i];
        }
    }
}

/**
 * @brief network::replyFinished
 * storing previous ssl session tickets for re-use, and error handling for finished requests
 * @param url
 * @param reply
 */
void network::replyFinished(QUrl &url, QNetworkReply *reply)
{
    if(!usedSession.contains((QString)url.toString()))
    {
        usedSession.insert((QString)url.toString(), (QByteArray)reply->sslConfiguration().sessionTicket());
        qDebug() << "saved ssl session ticket for" + url.toString();
    }

    reply->deleteLater();
}

/**
 * @brief network::sendGet
 * sending a simple GET request to specified url
 * @param url
 */
void network::sendGet(const QUrl &url)
{
    connect(&networkAccessManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));

    qDebug() << "Sending a GET request to" + (QString)url.toString();
    QNetworkRequest request;
    request.setUrl(url);
    request.setRawHeader("User-Agent", "nebula");

    // reusing an ssl session ticket if exists for url
    if(usedSession.contains((QString)url.toString()))
    {
        QSslConfiguration qssl;
        qssl.setSslOption(QSsl::SslOptionDisableSessionPersistence, false);
        qssl.setSessionTicket(usedSession.value((QString)url.toString()));
        request.setSslConfiguration(qssl);
        qDebug() << "used ssl session ticket for" + url.toString();
    }

    reply = networkAccessManager.get(request);

    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(httpError()));
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));
    connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
}

/**
 * @brief network::provideTcpHosts
 * @return
 */
QList<QString> network::provideTcpHosts()
{
    return QList<QString>()
            << (QString)"http://www.google.com"
            << (QString)"http://www.bing.com";
}

/**
 * @brief network::provideSslHosts
 * @return
 */
QList<QString> network::provideSslHosts()
{
    return QList<QString>()
            << (QString)"https://www.ssllabs.com/ssltest/";
}

/*SLOTS*/

/**
 * @brief network::slotTcpError
 * @param tcpError
 */
void network::httpError()
{
    qDebug() << "QNetworkRequest::HttpStatusCodeAttribute " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute ) << "received";
    qDebug() << reply->errorString();
    reply->deleteLater();
}

/**
 * @brief network::slotSslErrors
 * @param sslErrors
 */
void network::sslErrors(const QList<QSslError> &errors)
{
    QString errorString;
    foreach (const QSslError &error, errors) {
        if (!errorString.isEmpty())
            errorString += ", ";
        errorString += error.errorString();
    }

    qDebug() << "ssl error recieved: " + errorString;

    reply->deleteLater();
}

void network::httpFinished()
{
    // possible redirect url
     QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);

     if (!redirectionTarget.isNull()) {
         this->sendGet(reply->url().resolved(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl()));
     } else {
         qDebug() << "QNetworkRequest::HttpStatusCodeAttribute " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute ) << "received ";
         qDebug() << reply->errorString();
         qDebug() << reply->readAll();
     }
}

的main.cpp

#include <QCoreApplication>
#include <network.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    network networkAccessManager;

    for(int i = 0; i < 50; ++i)
    {
        networkAccessManager.sendGet((QString)"http://www.google.com");
    }

    return a.exec();
}

请注意,我对高级C ++项目和QT都很陌生,这个项目部分 - 但是一旦解开它就会用于我的精彩项目 - 是为了帮助我熟悉这些主题。

你能帮助我吗,哦,光荣的陌生人? :)

的问候; OneEyedSpaceFish

0 个答案:

没有答案