From 26e7635b3baa95f27a0a25d983f10c6c98395234 Mon Sep 17 00:00:00 2001 From: Kurt Pattyn Date: Tue, 13 Aug 2013 16:42:28 +0200 Subject: Split frameReceived into textFrameReceived, binaryFrameReceived and controlFrameReceived --- source/dataprocessor.cpp | 9 +++++---- source/dataprocessor.h | 4 +++- source/websocket.cpp | 24 +++++++++--------------- source/websocket.h | 2 +- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/source/dataprocessor.cpp b/source/dataprocessor.cpp index 40b9614..b399d7f 100644 --- a/source/dataprocessor.cpp +++ b/source/dataprocessor.cpp @@ -464,10 +464,10 @@ void DataProcessor::process(QTcpSocket *pSocket) { if (frame.isControlFrame()) { - Q_EMIT frameReceived(frame.getOpCode(), frame.getPayload(), true); + Q_EMIT controlFrameReceived(frame.getOpCode(), frame.getPayload()); isDone = true; //exit the loop after a control frame, so we can get a chance to close the socket if necessary } - else //we have a dataframe + else //we have a dataframe; opcode can be OC_CONTINUE, OC_TEXT or OC_BINARY { if (!m_isFragmented && frame.isContinuationFrame()) { @@ -478,7 +478,7 @@ void DataProcessor::process(QTcpSocket *pSocket) if (m_isFragmented && frame.isDataFrame() && !frame.isContinuationFrame()) { clear(); - Q_EMIT errorEncountered(WebSocketProtocol::CC_PROTOCOL_ERROR, "All data frames after the initial data frame must have opcode 0"); + Q_EMIT errorEncountered(WebSocketProtocol::CC_PROTOCOL_ERROR, "All data frames after the initial data frame must have opcode 0 (continuation)."); return; } if (!frame.isContinuationFrame()) @@ -507,13 +507,14 @@ void DataProcessor::process(QTcpSocket *pSocket) else { m_textMessage.append(frameTxt); + Q_EMIT textFrameReceived(frameTxt, frame.isFinalFrame()); } } else { m_binaryMessage.append(frame.getPayload()); + Q_EMIT binaryFrameReceived(frame.getPayload(), frame.isFinalFrame()); } - Q_EMIT frameReceived(m_opCode, frame.getPayload(), frame.isFinalFrame()); if (frame.isFinalFrame()) { diff --git a/source/dataprocessor.h b/source/dataprocessor.h index bbf37a7..cb3a832 100644 --- a/source/dataprocessor.h +++ b/source/dataprocessor.h @@ -21,7 +21,9 @@ public: virtual ~DataProcessor(); Q_SIGNALS: - void frameReceived(WebSocketProtocol::OpCode opCode, QByteArray frame, bool lastFrame); + void controlFrameReceived(WebSocketProtocol::OpCode opCode, QByteArray frame); + void textFrameReceived(QString frame, bool lastFrame); + void binaryFrameReceived(QByteArray frame, bool lastFrame); void textMessageReceived(QString message); void binaryMessageReceived(QByteArray message); void errorEncountered(WebSocketProtocol::CloseCode code, QString description); diff --git a/source/websocket.cpp b/source/websocket.cpp index 0c59132..abb1949 100644 --- a/source/websocket.cpp +++ b/source/websocket.cpp @@ -481,7 +481,9 @@ void WebSocket::makeConnections(const QTcpSocket *pTcpSocket) connect(pTcpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(processStateChanged(QAbstractSocket::SocketState))); connect(pTcpSocket, SIGNAL(readyRead()), this, SLOT(processData())); - connect(&m_dataProcessor, SIGNAL(frameReceived(WebSocketProtocol::OpCode, QByteArray, bool)), this, SLOT(processFrame(WebSocketProtocol::OpCode, QByteArray, bool))); + connect(&m_dataProcessor, SIGNAL(controlFrameReceived(WebSocketProtocol::OpCode, QByteArray)), this, SLOT(processControlFrame(WebSocketProtocol::OpCode, QByteArray))); + connect(&m_dataProcessor, SIGNAL(textFrameReceived(QString,bool)), this, SIGNAL(textFrameReceived(QString,bool))); + connect(&m_dataProcessor, SIGNAL(binaryFrameReceived(QByteArray,bool)), this, SIGNAL(binaryFrameReceived(QByteArray,bool))); connect(&m_dataProcessor, SIGNAL(binaryMessageReceived(QByteArray)), this, SIGNAL(binaryMessageReceived(QByteArray))); connect(&m_dataProcessor, SIGNAL(textMessageReceived(QString)), this, SIGNAL(textMessageReceived(QString))); connect(&m_dataProcessor, SIGNAL(errorEncountered(WebSocketProtocol::CloseCode,QString)), this, SLOT(close(WebSocketProtocol::CloseCode,QString))); @@ -505,7 +507,9 @@ void WebSocket::releaseConnections(const QTcpSocket *pTcpSocket) disconnect(pTcpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(processStateChanged(QAbstractSocket::SocketState))); disconnect(pTcpSocket, SIGNAL(readyRead()), this, SLOT(processData())); } - disconnect(&m_dataProcessor, SIGNAL(frameReceived(WebSocketProtocol::OpCode,QByteArray,bool)), this, SLOT(processFrame(WebSocketProtocol::OpCode,QByteArray,bool))); + disconnect(&m_dataProcessor, SIGNAL(controlFrameReceived(WebSocketProtocol::OpCode,QByteArray)), this, SLOT(processControlFrame(WebSocketProtocol::OpCode,QByteArray))); + disconnect(&m_dataProcessor, SIGNAL(textFrameReceived(QString,bool)), this, SIGNAL(textFrameReceived(QString,bool))); + disconnect(&m_dataProcessor, SIGNAL(binaryFrameReceived(QByteArray,bool)), this, SIGNAL(binaryFrameReceived(QByteArray,bool))); disconnect(&m_dataProcessor, SIGNAL(binaryMessageReceived(QByteArray)), this, SIGNAL(binaryMessageReceived(QByteArray))); disconnect(&m_dataProcessor, SIGNAL(textMessageReceived(QString)), this, SIGNAL(textMessageReceived(QString))); disconnect(&m_dataProcessor, SIGNAL(errorEncountered(WebSocketProtocol::CloseCode,QString)), this, SLOT(close(WebSocketProtocol::CloseCode,QString))); @@ -965,25 +969,13 @@ void WebSocket::processData() } } -//TODO: implement separate signals for textframereceived and binaryframereceived in dataprocessor -//in that way the UTF8 can be sent as is from within the dataprocessor /*! \internal */ -void WebSocket::processFrame(WebSocketProtocol::OpCode opCode, QByteArray frame, bool isLastFrame) +void WebSocket::processControlFrame(WebSocketProtocol::OpCode opCode, QByteArray frame) { switch (opCode) { - case WebSocketProtocol::OC_BINARY: - { - Q_EMIT binaryFrameReceived(frame, isLastFrame); - break; - } - case WebSocketProtocol::OC_TEXT: - { - Q_EMIT textFrameReceived(QString::fromUtf8(frame.constData(), frame.length()), isLastFrame); - break; - } case WebSocketProtocol::OC_PING: { quint32 maskingKey = 0; @@ -1040,6 +1032,8 @@ void WebSocket::processFrame(WebSocketProtocol::OpCode opCode, QByteArray frame, break; } case WebSocketProtocol::OC_CONTINUE: + case WebSocketProtocol::OC_BINARY: + case WebSocketProtocol::OC_TEXT: case WebSocketProtocol::OC_RESERVED_3: case WebSocketProtocol::OC_RESERVED_4: case WebSocketProtocol::OC_RESERVED_5: diff --git a/source/websocket.h b/source/websocket.h index 8a8dfba..99f3271 100644 --- a/source/websocket.h +++ b/source/websocket.h @@ -84,7 +84,7 @@ Q_SIGNALS: private Q_SLOTS: void processData(); - void processFrame(WebSocketProtocol::OpCode opCode, QByteArray frame, bool isLastFrame); + void processControlFrame(WebSocketProtocol::OpCode opCode, QByteArray frame); void processHandshake(QTcpSocket *pSocket); void processStateChanged(QAbstractSocket::SocketState socketState); -- cgit v1.2.3