diff options
Diffstat (limited to 'src/websockets/qwebsocketdataprocessor.cpp')
-rw-r--r-- | src/websockets/qwebsocketdataprocessor.cpp | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/src/websockets/qwebsocketdataprocessor.cpp b/src/websockets/qwebsocketdataprocessor.cpp index 4f81222..191b992 100644 --- a/src/websockets/qwebsocketdataprocessor.cpp +++ b/src/websockets/qwebsocketdataprocessor.cpp @@ -87,6 +87,10 @@ QWebSocketDataProcessor::QWebSocketDataProcessor(QObject *parent) : m_pTextCodec(QTextCodec::codecForName("UTF-8")) { clear(); + // initialize the internal timeout timer + waitTimer.setInterval(5000); + waitTimer.setSingleShot(true); + waitTimer.callOnTimeout(this, &QWebSocketDataProcessor::timeout); } /*! @@ -119,14 +123,23 @@ quint64 QWebSocketDataProcessor::maxFrameSize() /*! \internal + + Returns \c true if a complete websocket frame has been processed; + otherwise returns \c false. */ -void QWebSocketDataProcessor::process(QIODevice *pIoDevice) +bool QWebSocketDataProcessor::process(QIODevice *pIoDevice) { bool isDone = false; while (!isDone) { - QWebSocketFrame frame = QWebSocketFrame::readFrame(pIoDevice); - if (Q_LIKELY(frame.isValid())) { + frame.readFrame(pIoDevice); + if (!frame.isDone()) { + // waiting for more data available + QObject::connect(pIoDevice, &QIODevice::readyRead, + &waitTimer, &QTimer::stop, Qt::UniqueConnection); + waitTimer.start(); + return false; + } else if (Q_LIKELY(frame.isValid())) { if (frame.isControlFrame()) { isDone = processControlFrame(frame); } else { @@ -136,7 +149,7 @@ void QWebSocketDataProcessor::process(QIODevice *pIoDevice) Q_EMIT errorEncountered(QWebSocketProtocol::CloseCodeProtocolError, tr("Received Continuation frame, while there is " \ "nothing to continue.")); - return; + return true; } if (Q_UNLIKELY(m_isFragmented && frame.isDataFrame() && !frame.isContinuationFrame())) { @@ -144,7 +157,7 @@ void QWebSocketDataProcessor::process(QIODevice *pIoDevice) Q_EMIT errorEncountered(QWebSocketProtocol::CloseCodeProtocolError, tr("All data frames after the initial data frame " \ "must have opcode 0 (continuation).")); - return; + return true; } if (!frame.isContinuationFrame()) { m_opCode = frame.opCode(); @@ -158,7 +171,7 @@ void QWebSocketDataProcessor::process(QIODevice *pIoDevice) clear(); Q_EMIT errorEncountered(QWebSocketProtocol::CloseCodeTooMuchData, tr("Received message is too big.")); - return; + return true; } if (m_opCode == QWebSocketProtocol::OpCodeText) { @@ -171,7 +184,7 @@ void QWebSocketDataProcessor::process(QIODevice *pIoDevice) clear(); Q_EMIT errorEncountered(QWebSocketProtocol::CloseCodeWrongDatatype, tr("Invalid UTF-8 code encountered.")); - return; + return true; } else { m_textMessage.append(frameTxt); Q_EMIT textFrameReceived(frameTxt, frame.isFinalFrame()); @@ -199,7 +212,9 @@ void QWebSocketDataProcessor::process(QIODevice *pIoDevice) clear(); isDone = true; } + frame.clear(); } + return true; } /*! @@ -301,4 +316,14 @@ bool QWebSocketDataProcessor::processControlFrame(const QWebSocketFrame &frame) return mustStopProcessing; } +/*! + \internal + */ +void QWebSocketDataProcessor::timeout() +{ + clear(); + Q_EMIT errorEncountered(QWebSocketProtocol::CloseCodeGoingAway, + tr("Timeout when reading data from socket.")); +} + QT_END_NAMESPACE |