summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Chu <ryan.chu@qt.io>2019-06-30 13:56:25 +0200
committerRyan Chu <ryan.chu@qt.io>2019-08-23 15:28:40 +0200
commit24894c032719157a2d738f03e0c70d3ff0cf1782 (patch)
treee4b34ff85b5f5007093347f4019774e2ac82b78c
parent140246105d3581ceb238134a02261d49417296c7 (diff)
downloadqtwebsockets-24894c032719157a2d738f03e0c70d3ff0cf1782.tar.gz
Make QWebSocketFrame::readFrame as a non-static public function
The static function QWebSocketFrame::readFrame used to return a parsed QWebSocketFrame read from QIODevice. This change make QWebSocketFrame reusable. It will base on its internal state and keep processing the incoming data from QIODevice. Change-Id: Ic6dea59529fa935cdb8034519e633ea67e869674 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
-rw-r--r--src/websockets/qwebsocketdataprocessor.cpp3
-rw-r--r--src/websockets/qwebsocketdataprocessor_p.h2
-rw-r--r--src/websockets/qwebsocketframe.cpp34
-rw-r--r--src/websockets/qwebsocketframe_p.h2
-rw-r--r--tests/auto/websockets/websocketframe/tst_websocketframe.cpp12
5 files changed, 28 insertions, 25 deletions
diff --git a/src/websockets/qwebsocketdataprocessor.cpp b/src/websockets/qwebsocketdataprocessor.cpp
index 4f81222..ee28969 100644
--- a/src/websockets/qwebsocketdataprocessor.cpp
+++ b/src/websockets/qwebsocketdataprocessor.cpp
@@ -125,7 +125,7 @@ void QWebSocketDataProcessor::process(QIODevice *pIoDevice)
bool isDone = false;
while (!isDone) {
- QWebSocketFrame frame = QWebSocketFrame::readFrame(pIoDevice);
+ frame.readFrame(pIoDevice);
if (Q_LIKELY(frame.isValid())) {
if (frame.isControlFrame()) {
isDone = processControlFrame(frame);
@@ -199,6 +199,7 @@ void QWebSocketDataProcessor::process(QIODevice *pIoDevice)
clear();
isDone = true;
}
+ frame.clear();
}
}
diff --git a/src/websockets/qwebsocketdataprocessor_p.h b/src/websockets/qwebsocketdataprocessor_p.h
index e80a843..1d8024e 100644
--- a/src/websockets/qwebsocketdataprocessor_p.h
+++ b/src/websockets/qwebsocketdataprocessor_p.h
@@ -55,6 +55,7 @@
#include <QtCore/QByteArray>
#include <QtCore/QString>
#include <QtCore/QTextCodec>
+#include "qwebsocketframe_p.h"
#include "qwebsocketprotocol.h"
#include "qwebsocketprotocol_p.h"
@@ -111,6 +112,7 @@ private:
quint64 m_payloadLength;
QTextCodec::ConverterState *m_pConverterState;
QTextCodec *m_pTextCodec;
+ QWebSocketFrame frame;
bool processControlFrame(const QWebSocketFrame &frame);
};
diff --git a/src/websockets/qwebsocketframe.cpp b/src/websockets/qwebsocketframe.cpp
index fd814a4..5b10e02 100644
--- a/src/websockets/qwebsocketframe.cpp
+++ b/src/websockets/qwebsocketframe.cpp
@@ -294,46 +294,44 @@ bool QWebSocketFrame::isValid() const
#define WAIT_FOR_MORE_DATA(returnState) \
{ needMoreData = true; \
- frame.m_processingState = (returnState); }
+ m_processingState = (returnState); }
/*!
\internal
*/
-QWebSocketFrame QWebSocketFrame::readFrame(QIODevice *pIoDevice)
+void QWebSocketFrame::readFrame(QIODevice *pIoDevice)
{
bool isDone = false;
- QWebSocketFrame frame;
-
while (!isDone)
{
bool needMoreData = false;
- switch (frame.m_processingState) {
+ switch (m_processingState) {
case PS_READ_HEADER:
- frame.m_processingState = frame.readFrameHeader(pIoDevice);
- if (frame.m_processingState == PS_WAIT_FOR_MORE_DATA)
+ m_processingState = readFrameHeader(pIoDevice);
+ if (m_processingState == PS_WAIT_FOR_MORE_DATA)
WAIT_FOR_MORE_DATA(PS_READ_HEADER);
break;
case PS_READ_PAYLOAD_LENGTH:
- frame.m_processingState = frame.readFramePayloadLength(pIoDevice);
- if (frame.m_processingState == PS_WAIT_FOR_MORE_DATA)
+ m_processingState = readFramePayloadLength(pIoDevice);
+ if (m_processingState == PS_WAIT_FOR_MORE_DATA)
WAIT_FOR_MORE_DATA(PS_READ_PAYLOAD_LENGTH);
break;
case PS_READ_MASK:
- frame.m_processingState = frame.readFrameMask(pIoDevice);
- if (frame.m_processingState == PS_WAIT_FOR_MORE_DATA)
+ m_processingState = readFrameMask(pIoDevice);
+ if (m_processingState == PS_WAIT_FOR_MORE_DATA)
WAIT_FOR_MORE_DATA(PS_READ_MASK);
break;
case PS_READ_PAYLOAD:
- frame.m_processingState = frame.readFramePayload(pIoDevice);
- if (frame.m_processingState == PS_WAIT_FOR_MORE_DATA)
+ m_processingState = readFramePayload(pIoDevice);
+ if (m_processingState == PS_WAIT_FOR_MORE_DATA)
WAIT_FOR_MORE_DATA(PS_READ_PAYLOAD);
break;
case PS_DISPATCH_RESULT:
- frame.m_processingState = PS_DISPATCH_RESULT;
+ m_processingState = PS_DISPATCH_RESULT;
isDone = true;
break;
@@ -348,14 +346,12 @@ QWebSocketFrame QWebSocketFrame::readFrame(QIODevice *pIoDevice)
// the GUI will hang for at most 5 seconds
// maybe, a QStateMachine should be used
if (!pIoDevice->waitForReadyRead(5000)) {
- frame.setError(QWebSocketProtocol::CloseCodeGoingAway,
- tr("Timeout when reading data from socket."));
- frame.m_processingState = PS_DISPATCH_RESULT;
+ setError(QWebSocketProtocol::CloseCodeGoingAway,
+ tr("Timeout when reading data from socket."));
+ m_processingState = PS_DISPATCH_RESULT;
}
}
}
-
- return frame;
}
/*!
diff --git a/src/websockets/qwebsocketframe_p.h b/src/websockets/qwebsocketframe_p.h
index b442d01..be79a6e 100644
--- a/src/websockets/qwebsocketframe_p.h
+++ b/src/websockets/qwebsocketframe_p.h
@@ -102,7 +102,7 @@ public:
bool isValid() const;
- static QWebSocketFrame readFrame(QIODevice *pIoDevice);
+ void readFrame(QIODevice *pIoDevice);
private:
QWebSocketProtocol::CloseCode m_closeCode;
diff --git a/tests/auto/websockets/websocketframe/tst_websocketframe.cpp b/tests/auto/websockets/websocketframe/tst_websocketframe.cpp
index 5614df8..6b9aaaf 100644
--- a/tests/auto/websockets/websocketframe/tst_websocketframe.cpp
+++ b/tests/auto/websockets/websocketframe/tst_websocketframe.cpp
@@ -197,7 +197,8 @@ void tst_WebSocketFrame::tst_copyConstructorAndAssignment()
QBuffer buffer(&payload);
buffer.open(QIODevice::ReadOnly);
- QWebSocketFrame frame = QWebSocketFrame::readFrame(&buffer);
+ QWebSocketFrame frame;
+ frame.readFrame(&buffer);
buffer.close();
{
@@ -330,7 +331,8 @@ void tst_WebSocketFrame::tst_goodFrames()
QBuffer buffer;
buffer.setData(wireRepresentation);
buffer.open(QIODevice::ReadOnly);
- QWebSocketFrame frame = QWebSocketFrame::readFrame(&buffer);
+ QWebSocketFrame frame;
+ frame.readFrame(&buffer);
buffer.close();
QVERIFY(frame.isValid());
QCOMPARE(frame.rsv1(), rsv1);
@@ -495,7 +497,8 @@ void tst_WebSocketFrame::tst_invalidFrames()
QBuffer buffer;
buffer.setData(wireRepresentation);
buffer.open(QIODevice::ReadOnly);
- QWebSocketFrame frame = QWebSocketFrame::readFrame(&buffer);
+ QWebSocketFrame frame;
+ frame.readFrame(&buffer);
buffer.close();
QVERIFY(!frame.isValid());
@@ -606,7 +609,8 @@ void tst_WebSocketFrame::tst_malformedFrames()
QBuffer buffer;
buffer.setData(payload);
buffer.open(QIODevice::ReadOnly);
- QWebSocketFrame frame = QWebSocketFrame::readFrame(&buffer);
+ QWebSocketFrame frame;
+ frame.readFrame(&buffer);
buffer.close();
QVERIFY(!frame.isValid());