QSocks5SocketEngine: improve buffering in CONNECT mode

Replace QByteArray-based read buffer with a more efficient QRingBuffer.

Change-Id: Id43427a0692463481dfc4dfbe915772afb093364
Reviewed-by: Jesus Fernandez <jesus.fernandez@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Alex Trotsenko 2016-10-31 16:40:19 +02:00
parent 1dcd61d553
commit 7bf81286bd

View File

@ -54,6 +54,7 @@
#include "qurl.h"
#include "qauthenticator.h"
#include "private/qiodevice_p.h"
#include "private/qringbuffer_p.h"
#include <qendian.h>
#include <qnetworkinterface.h>
@ -280,7 +281,7 @@ struct QSocks5Data
struct QSocks5ConnectData : public QSocks5Data
{
QByteArray readBuffer;
QRingBuffer readBuffer;
};
struct QSocks5BindData : public QSocks5Data
@ -1193,7 +1194,7 @@ void QSocks5SocketEnginePrivate::_q_controlSocketReadNotification()
}
if (buf.size()) {
QSOCKS5_DEBUG << dump(buf);
connectData->readBuffer += buf;
connectData->readBuffer.append(buf);
emitReadNotification();
}
break;
@ -1513,7 +1514,7 @@ qint64 QSocks5SocketEngine::read(char *data, qint64 maxlen)
Q_D(QSocks5SocketEngine);
QSOCKS5_Q_DEBUG << "read( , maxlen = " << maxlen << ')';
if (d->mode == QSocks5SocketEnginePrivate::ConnectMode) {
if (d->connectData->readBuffer.size() == 0) {
if (d->connectData->readBuffer.isEmpty()) {
if (d->data->controlSocket->state() == QAbstractSocket::UnconnectedState) {
//imitate remote closed
close();
@ -1525,9 +1526,7 @@ qint64 QSocks5SocketEngine::read(char *data, qint64 maxlen)
return 0; // nothing to be read
}
}
qint64 copy = qMin<qint64>(d->connectData->readBuffer.size(), maxlen);
memcpy(data, d->connectData->readBuffer.constData(), copy);
d->connectData->readBuffer.remove(0, copy);
const qint64 copy = d->connectData->readBuffer.read(data, maxlen);
QSOCKS5_DEBUG << "read" << dump(QByteArray(data, copy));
return copy;
#ifndef QT_NO_UDPSOCKET