最近忙的,抽点时间写写最近遇到的问题,最近做了一个下载工具,正好最近提升性能,就想着把最近遇到的性能问题提高,还有就是提高SQLITE的插入性能,
bool DownTaskinfoThread::http_outtime_repeat(std::function<bool(QNetworkReply* pobj) > f, QString url, QString tokenstr)
{
int maxRetry = HttpClient::m_retryMax;
int curCnt = 0;
while (curCnt < maxRetry) {
if (HttpClient::http_get(f,url, tokenstr)) {
return true;
}
curCnt++;
}
if (curCnt >= maxRetry) {
QString msg;
msg.sprintf("Failed to download m_downTaskinfo.currentpage : %d", m_downTaskinfo.currentpage);
TDToolslogs::GetInstance().output_log(msg, ENUM_RED);
return false;
}
}
以上是一个超时重试的机制,解决网络原因导致超时
bool HttpClient::http_get(std::function<bool(QNetworkReply* pobj) > f, QString url, QString tokenstr)
{
QNetworkAccessManager m_manager;
QNetworkRequest req;
req.setRawHeader("Content-Type", "application/json");
req.setRawHeader("Authorization", tokenstr.toLocal8Bit());
req.setUrl(QUrl(url));
QNetworkReply* reply = m_manager.get(req);
QEventLoop m_loop;
QTimer timeout_t;
connect(&m_manager, SIGNAL(finished(QNetworkReply*)), &m_loop, SLOT(quit()));
connect(&m_manager, SIGNAL(error(QNetworkReply*)), &m_loop, SLOT(quit()));
connect(&timeout_t, SIGNAL(timeout()), &m_loop, SLOT(quit()));
timeout_t.start(HttpClient::m_timeout);
m_loop.exec();
if (!timeout_t.isActive()) {
return false;
}
return f(reply);
}
这个是一个HTTP get方式的一个超时回调处理,增加同步请求的访问成功率
bool HttpClient::downloadFileFromByte(QString url, QByteArray& byteArray)
{
QNetworkAccessManager m_manager;
QNetworkRequest request;
request.setUrl(QUrl(url));
QNetworkReply* reply = m_manager.get(request);
QEventLoop m_loop;
QTimer timeout_t;
timeout_t.setSingleShot(true);
connect(&m_manager, SIGNAL(finished(QNetworkReply*)), &m_loop, SLOT(quit()));
connect(&m_manager, SIGNAL(error(QNetworkReply*)), &m_loop, SLOT(quit()));
connect(&timeout_t, SIGNAL(timeout()), &m_loop, SLOT(quit()));
//connect(this, SIGNAL(sgn_ackRight()), &m_loop, SLOT(quit()));
timeout_t.start(HttpClient::m_timeout);
m_loop.exec();
QVariant statuscode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (statuscode.toInt() != 200) {
return false;
}
bool bRet = timeout_t.isActive();
if (bRet)
{
byteArray = reply->readAll();
}
return bRet;
}
QT http 获取流数据
bool HttpClient::downloadFileFromURL(QString url, QString localpath)
{
QNetworkAccessManager m_manager;
QFile m_file;
m_file.setFileName(localpath);
m_file.open(QIODevice::WriteOnly);
if (!m_file.isOpen()) {
return false;
}
QNetworkRequest request;
request.setUrl(QUrl(url));
QNetworkReply* reply = m_manager.get(request);
QEventLoop m_loop;
QTimer timeout_t;
timeout_t.setSingleShot(true);
connect(&m_manager, SIGNAL(finished(QNetworkReply*)), &m_loop, SLOT(quit()));
connect(&m_manager, SIGNAL(error(QNetworkReply*)), &m_loop, SLOT(quit()));
connect(&timeout_t, SIGNAL(timeout()), &m_loop, SLOT(quit()));
//connect(this, SIGNAL(sgn_ackRight()), &m_loop, SLOT(quit()));
timeout_t.start(HttpClient::m_timeout);
m_loop.exec();
QVariant statuscode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (statuscode.toInt() != 200) {
m_file.close();
return false;
}
bool bRet = timeout_t.isActive();
if (bRet)
{
m_file.write(reply->readAll());
}
m_file.close();
return bRet;
}
HTTP 下载文件
bool TDGISToolsSqliteDataCentre::fun_insert_file(QByteArray buffer, QString sqlstr)
{
sqlite3_stmt *stmt = NULL;
//插入的sql语句,这里用?占位,代表BLOB数据
auto rc = sqlite3_prepare_v2(m_db, sqlstr.toStdString().c_str(), -1, &stmt, NULL);
if (rc != SQLITE_OK) { return false; }
else {
rc = sqlite3_bind_blob(stmt, 1, buffer.data(), buffer.size(), SQLITE_STATIC);
if (rc != SQLITE_OK) {
return false;
}
else {
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
return false;
}
}
sqlite3_finalize(stmt);
buffer.clear();
buffer.squeeze();
return true;
}
bool TDGISToolsSqliteDataCentre::fun_insert_files(QVector<QByteArray> vecbuffer, QString sqlstr)
{
sqlite3_stmt *stmt = NULL;
//插入的sql语句,这里用?占位,代表BLOB数据
auto rc = sqlite3_prepare_v2(m_db, sqlstr.toStdString().c_str(), -1, &stmt, NULL);
if (rc != SQLITE_OK) { return false; }
else {
for (int i = 0; i < vecbuffer.size(); i++)
{
rc = sqlite3_bind_blob(stmt, i+1, vecbuffer.at(i).data(), vecbuffer.at(i).size(), SQLITE_STATIC);
if (rc != SQLITE_OK) {
return false;
}
}
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
return false;
}
sqlite3_finalize(stmt);
vecbuffer.clear();
return true;
}
在sqlite中存取二进制文件