QAbstractSocket::SocketError QWebSocketPrivate::error() const
{
QAbstractSocket::SocketError err = QAbstractSocket::OperationError;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
err = m_pSocket->error();
}
return err;
bool QWebSocketPrivate::flush()
{
bool result = true;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
result = m_pSocket->flush();
}
return result;
*/
qint64 QWebSocketPrivate::write(const char *message)
{
- //TODO: create a QByteArray from message, and directly call doWriteFrames
- //now the data is converted to a string, and then converted back to a bytearray
- return write(QString::fromUtf8(message));
+ return doWriteFrames(QByteArray::fromRawData(message, qstrlen(message)), false);
}
/*!
*/
qint64 QWebSocketPrivate::write(const char *message, qint64 maxSize)
{
- //TODO: create a QByteArray from message, and directly call doWriteFrames
- //now the data is converted to a string, and then converted back to a bytearray
- return write(QString::fromUtf8(message, static_cast<int>(maxSize)));
+ return write(QByteArray::fromRawData(message, maxSize), false);
}
/*!
void QWebSocketPrivate::ignoreSslErrors()
{
m_configuration.m_ignoreSslErrors = true;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
QSslSocket *pSslSocket = qobject_cast<QSslSocket *>(m_pSocket.data());
- if (pSslSocket) {
+ if (Q_LIKELY(pSslSocket)) {
pSslSocket->ignoreSslErrors();
}
}
QObject *parent)
{
QWebSocket *pWebSocket = new QWebSocket(pTcpSocket, response.acceptedVersion(), parent);
- pWebSocket->d_func()->setExtension(response.acceptedExtension());
- pWebSocket->d_func()->setOrigin(request.origin());
- pWebSocket->d_func()->setRequestUrl(request.requestUrl());
- pWebSocket->d_func()->setProtocol(response.acceptedProtocol());
- pWebSocket->d_func()->setResourceName(request.requestUrl().toString(QUrl::RemoveUserInfo));
- //a server should not send masked frames
- pWebSocket->d_func()->enableMasking(false);
+ if (Q_LIKELY(pWebSocket)) {
+ pWebSocket->d_func()->setExtension(response.acceptedExtension());
+ pWebSocket->d_func()->setOrigin(request.origin());
+ pWebSocket->d_func()->setRequestUrl(request.requestUrl());
+ pWebSocket->d_func()->setProtocol(response.acceptedProtocol());
+ pWebSocket->d_func()->setResourceName(request.requestUrl().toString(QUrl::RemoveUserInfo));
+ //a server should not send masked frames
+ pWebSocket->d_func()->enableMasking(false);
+ }
return pWebSocket;
}
*/
void QWebSocketPrivate::close(QWebSocketProtocol::CloseCode closeCode, QString reason)
{
- if (!m_pSocket) {
+ if (Q_UNLIKELY(!m_pSocket)) {
return;
}
if (!m_isClosingHandshakeSent) {
pTcpSocket->deleteLater();
}
//if (m_url != url)
- if (!m_pSocket) {
+ if (Q_LIKELY(!m_pSocket)) {
Q_Q(QWebSocket);
m_dataProcessor.clear();
} else {
QSslSocket *sslSocket = new QSslSocket(this);
m_pSocket.reset(sslSocket);
-
- makeConnections(m_pSocket.data());
- connect(sslSocket, SIGNAL(encryptedBytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)));
- setSocketState(QAbstractSocket::ConnectingState);
-
- sslSocket->setSslConfiguration(m_configuration.m_sslConfiguration);
- if (m_configuration.m_ignoreSslErrors) {
- sslSocket->ignoreSslErrors();
+ if (Q_LIKELY(m_pSocket)) {
+ m_pSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
+ m_pSocket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
+
+ makeConnections(m_pSocket.data());
+ connect(sslSocket, SIGNAL(encryptedBytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)));
+ setSocketState(QAbstractSocket::ConnectingState);
+
+ sslSocket->setSslConfiguration(m_configuration.m_sslConfiguration);
+ if (Q_UNLIKELY(m_configuration.m_ignoreSslErrors)) {
+ sslSocket->ignoreSslErrors();
+ } else {
+ sslSocket->ignoreSslErrors(m_configuration.m_ignoredSslErrors);
+ }
+ #ifndef QT_NO_NETWORKPROXY
+ sslSocket->setProxy(m_configuration.m_proxy);
+ #endif
+ sslSocket->connectToHostEncrypted(url.host(), url.port(443));
} else {
- sslSocket->ignoreSslErrors(m_configuration.m_ignoredSslErrors);
+ const QString message = tr("Out of memory.");
+ setErrorString(message);
+ emit q->error(QAbstractSocket::SocketResourceError);
}
-#ifndef QT_NO_NETWORKPROXY
- sslSocket->setProxy(m_configuration.m_proxy);
-#endif
- sslSocket->connectToHostEncrypted(url.host(), url.port(443));
}
} else
#endif
if (url.scheme() == QStringLiteral("ws")) {
m_pSocket.reset(new QTcpSocket(this));
+ if (Q_LIKELY(m_pSocket)) {
+ m_pSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
+ m_pSocket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
- makeConnections(m_pSocket.data());
- connect(m_pSocket.data(), SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)));
- setSocketState(QAbstractSocket::ConnectingState);
-#ifndef QT_NO_NETWORKPROXY
- m_pSocket->setProxy(m_configuration.m_proxy);
-#endif
- m_pSocket->connectToHost(url.host(), url.port(80));
+ makeConnections(m_pSocket.data());
+ connect(m_pSocket.data(), SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)));
+ setSocketState(QAbstractSocket::ConnectingState);
+ #ifndef QT_NO_NETWORKPROXY
+ m_pSocket->setProxy(m_configuration.m_proxy);
+ #endif
+ m_pSocket->connectToHost(url.host(), url.port(80));
+ } else {
+ const QString message = tr("Out of memory.");
+ setErrorString(message);
+ emit q->error(QAbstractSocket::SocketResourceError);
+ }
} else {
const QString message = tr("Unsupported websockets scheme: %1").arg(url.scheme());
setErrorString(message);
m_pingTimer.restart();
QByteArray pingFrame = getFrameHeader(QWebSocketProtocol::OC_PING, payload.size(), 0 /*do not mask*/, true);
pingFrame.append(payload);
- writeFrame(pingFrame);
+ (void)writeFrame(pingFrame);
}
/*!
Q_ASSERT(pTcpSocket);
Q_Q(QWebSocket);
- if (pTcpSocket) {
+ if (Q_LIKELY(pTcpSocket)) {
//pass through signals
connect(pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), q, SIGNAL(error(QAbstractSocket::SocketError)));
connect(pTcpSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)), q, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)));
*/
void QWebSocketPrivate::releaseConnections(const QTcpSocket *pTcpSocket)
{
- if (pTcpSocket) {
+ if (Q_LIKELY(pTcpSocket)) {
disconnect(pTcpSocket);
}
disconnect(&m_dataProcessor);
quint8 byte = 0x00;
bool ok = payloadLength <= 0x7FFFFFFFFFFFFFFFULL;
- if (ok) {
+ if (Q_LIKELY(ok)) {
//FIN, RSV1-3, opcode
byte = static_cast<quint8>((opCode & 0x0F) | (lastFrame ? 0x80 : 0x00)); //FIN, opcode
//RSV-1, RSV-2 and RSV-3 are zero
qint64 QWebSocketPrivate::doWriteFrames(const QByteArray &data, bool isBinary)
{
qint64 payloadWritten = 0;
- if (!m_pSocket) {
+ if (Q_UNLIKELY(!m_pSocket)) {
return payloadWritten;
}
Q_Q(QWebSocket);
int numFrames = data.size() / FRAME_SIZE_IN_BYTES;
QByteArray tmpData(data);
- //TODO: really necessary?
tmpData.detach();
char *payload = tmpData.data();
quint64 sizeLeft = quint64(data.size()) % FRAME_SIZE_IN_BYTES;
- if (sizeLeft) {
+ if (Q_LIKELY(sizeLeft)) {
++numFrames;
}
//catch the case where the payload is zero bytes;
//in this case, we still need to send a frame
- if (numFrames == 0) {
+ if (Q_UNLIKELY(numFrames == 0)) {
numFrames = 1;
}
quint64 currentPosition = 0;
bytesWritten += m_pSocket->write(getFrameHeader(opcode, size, maskingKey, isLastFrame));
//write payload
- if (size > 0) {
+ if (Q_LIKELY(size > 0)) {
char *currentData = payload + currentPosition;
if (m_mustMask) {
QWebSocketProtocol::mask(currentData, size, maskingKey);
}
qint64 written = m_pSocket->write(currentData, static_cast<qint64>(size));
- if (written > 0) {
+ if (Q_LIKELY(written > 0)) {
bytesWritten += written;
payloadWritten += written;
} else {
currentPosition += size;
bytesLeft -= size;
}
- if (payloadWritten != data.size()) {
+ if (Q_UNLIKELY(payloadWritten != data.size())) {
setErrorString(tr("Bytes written %1 != %2.").arg(payloadWritten).arg(data.size()));
Q_EMIT q->error(QAbstractSocket::NetworkError);
}
/*!
\internal
*/
-QString QWebSocketPrivate::calculateAcceptKey(const QString &key) const
+QString QWebSocketPrivate::calculateAcceptKey(const QByteArray &key) const
{
- const QString tmpKey = key % QStringLiteral("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
- const QByteArray hash = QCryptographicHash::hash(tmpKey.toLatin1(), QCryptographicHash::Sha1);
- return QString::fromLatin1(hash.toBase64());
+ const QByteArray tmpKey = key + QByteArrayLiteral("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
+ const QByteArray hash = QCryptographicHash::hash(tmpKey, QCryptographicHash::Sha1).toBase64();
+ return QString::fromLatin1(hash);
}
/*!
qint64 QWebSocketPrivate::writeFrames(const QList<QByteArray> &frames)
{
qint64 written = 0;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
QList<QByteArray>::const_iterator it;
for (it = frames.cbegin(); it < frames.cend(); ++it) {
written += writeFrame(*it);
qint64 QWebSocketPrivate::writeFrame(const QByteArray &frame)
{
qint64 written = 0;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
written = m_pSocket->write(frame);
}
return written;
{
Q_ASSERT(pSocket);
QString line;
- if (pSocket) {
- char c;
- while (pSocket->getChar(&c)) {
- if (c == char('\r')) {
- pSocket->getChar(&c);
- break;
- } else {
- line.append(QChar::fromLatin1(c));
- }
+ char c;
+ while (pSocket->getChar(&c)) {
+ if (c == char('\r')) {
+ pSocket->getChar(&c);
+ break;
+ } else {
+ line.append(QChar::fromLatin1(c));
}
}
return line;
int httpStatusCode;
QString httpStatusMessage;
const QRegularExpressionMatch match = regExp.match(statusLine);
- if (match.hasMatch()) {
+ if (Q_LIKELY(match.hasMatch())) {
QStringList tokens = match.capturedTexts();
tokens.removeFirst(); //remove the search string
if (tokens.length() == 3) {
ok = true;
}
}
- if (!ok) {
+ if (Q_UNLIKELY(!ok)) {
errorDescription = tr("Invalid statusline in response: %1.").arg(statusLine);
} else {
QString headerLine = readLine(pSocket);
//const QString protocol = headers.value(QStringLiteral("Sec-WebSocket-Protocol"), QStringLiteral(""));
const QString version = headers.value(QStringLiteral("Sec-WebSocket-Version"), QStringLiteral(""));
- if (httpStatusCode == 101) {
+ if (Q_LIKELY(httpStatusCode == 101)) {
//HTTP/x.y 101 Switching Protocols
bool conversionOk = false;
const float version = httpProtocol.midRef(5).toFloat(&conversionOk);
(upgrade.toLower() != QStringLiteral("websocket")) ||
(connection.toLower() != QStringLiteral("upgrade")));
if (ok) {
- const QString accept = calculateAcceptKey(QString::fromLatin1(m_key));
+ const QString accept = calculateAcceptKey(m_key);
ok = (accept == acceptKey);
if (!ok) {
errorDescription = tr("Accept-Key received from server %1 does not match the client key %2.").arg(acceptKey).arg(accept);
bool QWebSocketPrivate::waitForConnected(int msecs)
{
bool result = false;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
result = m_pSocket->waitForConnected(msecs);
}
return result;
bool QWebSocketPrivate::waitForDisconnected(int msecs)
{
bool result = false;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
result = m_pSocket->waitForDisconnected(msecs);
}
return result;
QHostAddress QWebSocketPrivate::localAddress() const
{
QHostAddress address;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
address = m_pSocket->localAddress();
}
return address;
quint16 QWebSocketPrivate::localPort() const
{
quint16 port = 0;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
port = m_pSocket->localPort();
}
return port;
QAbstractSocket::PauseModes QWebSocketPrivate::pauseMode() const
{
QAbstractSocket::PauseModes mode = QAbstractSocket::PauseNever;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
mode = m_pSocket->pauseMode();
}
return mode;
QHostAddress QWebSocketPrivate::peerAddress() const
{
QHostAddress address;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
address = m_pSocket->peerAddress();
}
return address;
QString QWebSocketPrivate::peerName() const
{
QString name;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
name = m_pSocket->peerName();
}
return name;
quint16 QWebSocketPrivate::peerPort() const
{
quint16 port = 0;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
port = m_pSocket->peerPort();
}
return port;
qint64 QWebSocketPrivate::readBufferSize() const
{
qint64 size = 0;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
size = m_pSocket->readBufferSize();
}
return size;
*/
void QWebSocketPrivate::resume()
{
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
m_pSocket->resume();
}
}
*/
void QWebSocketPrivate::setPauseMode(QAbstractSocket::PauseModes pauseMode)
{
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
m_pSocket->setPauseMode(pauseMode);
}
}
*/
void QWebSocketPrivate::setReadBufferSize(qint64 size)
{
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
m_pSocket->setReadBufferSize(size);
}
}
*/
void QWebSocketPrivate::setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value)
{
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
m_pSocket->setSocketOption(option, value);
}
}
QVariant QWebSocketPrivate::socketOption(QAbstractSocket::SocketOption option)
{
QVariant val;
- if (m_pSocket) {
+ if (Q_LIKELY(m_pSocket)) {
val = m_pSocket->socketOption(option);
}
return val;