From aed3fca0bfa366290947141677868d38956cf76d Mon Sep 17 00:00:00 2001 From: Mikko Ahlroth Date: Mon, 7 Jul 2014 15:16:28 +0300 Subject: [PATCH] Fix protocol errors and connection to UI. Make sure protocol handles nulls --- qml/harbour-weechatrelay.qml | 3 +- qml/js/connection.js | 5 +++ src/connectionhandler.cpp | 1 + src/connectionhandler.h | 1 + src/protocolhandler.h | 1 + src/weechatprotocolhandler.cpp | 75 +++++++++++++++++++++------------- src/weechatprotocolhandler.h | 2 +- 7 files changed, 57 insertions(+), 31 deletions(-) diff --git a/qml/harbour-weechatrelay.qml b/qml/harbour-weechatrelay.qml index 5995d1f..5f9f3bc 100644 --- a/qml/harbour-weechatrelay.qml +++ b/qml/harbour-weechatrelay.qml @@ -23,6 +23,7 @@ ApplicationWindow connectionHandler.connected.connect(C.onConnected); connectionHandler.disconnected.connect(C.onDisconnected); connectionHandler.displayDebugData.connect(C.onDisplayDebugData); + connectionHandler.newEvent.connect(C.newEvent); connectionHandler.sslError.connect(C.onSslError); } @@ -30,5 +31,3 @@ ApplicationWindow id: connectionHandler } } - - diff --git a/qml/js/connection.js b/qml/js/connection.js index 6eb0c1d..ba7da7d 100644 --- a/qml/js/connection.js +++ b/qml/js/connection.js @@ -34,6 +34,11 @@ function init(connectionHandler, pageStack) { // Signal handlers for incoming data from connection +function newEvent(id, data) { + debugViewPage.display("Event received: " + id); + debugViewPage.display(JSON.stringify(data)); +} + function onDisplayDebugData(str) { debugViewPage.display(str); } diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp index dba2cdc..386b5e5 100644 --- a/src/connectionhandler.cpp +++ b/src/connectionhandler.cpp @@ -184,4 +184,5 @@ void ConnectionHandler::connectSignals() QObject::connect(handler, &ProtocolHandler::debugData, this, &ConnectionHandler::handleDebugData); QObject::connect(handler, &ProtocolHandler::sendData, connection, &RelayConnection::write); + QObject::connect(handler, SIGNAL(newEvent(QString,QVariant)), this, SIGNAL(newEvent(QString,QVariant))); } diff --git a/src/connectionhandler.h b/src/connectionhandler.h index 7ad94a4..22dd3ad 100644 --- a/src/connectionhandler.h +++ b/src/connectionhandler.h @@ -48,6 +48,7 @@ signals: void disconnected(); void displayDebugData(QString data); + void newEvent(QString id, QVariant data); void sslError(QString errorStrings, QString issuerInfo, QDateTime startDate, QDateTime expiryDate, QString digest); diff --git a/src/protocolhandler.h b/src/protocolhandler.h index 795453f..b433fc0 100644 --- a/src/protocolhandler.h +++ b/src/protocolhandler.h @@ -26,6 +26,7 @@ public: signals: void debugData(QString hexString); + void newEvent(QString id, QVariant data); // ProtocolHandler wants to send data to the associated network stream void sendData(QByteArray data); diff --git a/src/weechatprotocolhandler.cpp b/src/weechatprotocolhandler.cpp index 6005887..b55bbf4 100644 --- a/src/weechatprotocolhandler.cpp +++ b/src/weechatprotocolhandler.cpp @@ -103,7 +103,7 @@ qint64 WeeChatProtocolHandler::handleLong(QDataStream* data) if (i == 0) { - if (current == 45) // ASCII '-' + if (current == '-') { magnitude = -1; } @@ -121,28 +121,26 @@ qint64 WeeChatProtocolHandler::handleLong(QDataStream* data) QString WeeChatProtocolHandler::handleString(QDataStream* data) { - // Strings are stored as length (4 bytes) + data (no \0) - QString ret; - quint32 length; - *data >> length; + // Strings are just buffers interpreted as string data + QByteArray bytes = handleBuffer(data); - quint8 current; - for (quint32 i = 0; i < length; ++i) - { - *data >> current; - ret.append(static_cast(current)); - } - - return ret; + // Lovely WeeChat sends everything our way in UTF-8 <3 + return QString::fromUtf8(bytes); } QByteArray WeeChatProtocolHandler::handleBuffer(QDataStream* data) { - // Buffer is the same as string, but just bytes + // Buffers consist of a length (4 bytes) + data QByteArray ret; quint32 length; *data >> length; + // If length is all 1s, the content is empty + if (length == 0xffffffff) + { + return QByteArray(); + } + quint8 current; for (quint32 i = 0; i < length; ++i) { @@ -160,13 +158,24 @@ QString WeeChatProtocolHandler::handlePointer(QDataStream* data) quint8 length; *data >> length; + qDebug() << "Pointer length: " << length; + quint8 current; for (quint8 i = 0; i < length; ++i) { *data >> current; + + // Convert 0 to '0' to handle null pointer + if (current == 0) + { + current = '0'; + } + ret.append(static_cast(current)); } + qDebug() << "Pointer data: " << ret; + return ret.prepend("0x"); } @@ -217,13 +226,16 @@ QVariantHash WeeChatProtocolHandler::handleHdata(QDataStream* data) * of parts in H-path * * List of objects, with the same keys and types as defined in Keys */ + QVariantHash hdata; QVariantList objectSets; QVariantHash keys; - QString hPath = WeeChatProtocolHandler::handleString(data); + QString hPath = handleString(data); + qDebug() << "H-path: " << hPath; - QString keyString = WeeChatProtocolHandler::handleString(data); + QString keyString = handleString(data); + qDebug() << "Keystr: " << keyString; QStringList keyList = keyString.split(","); QStringList orderedKeys; @@ -236,7 +248,7 @@ QVariantHash WeeChatProtocolHandler::handleHdata(QDataStream* data) orderedKeys.append(keyName); } - qint32 objectSetCount = WeeChatProtocolHandler::handleInt(data); + qint32 objectSetCount = handleInt(data); for (qint32 i = 0; i < objectSetCount; ++i) { @@ -246,9 +258,10 @@ QVariantHash WeeChatProtocolHandler::handleHdata(QDataStream* data) // separated by / QVariantList pointers; int pointerCount = hPath.split("/").size(); + qDebug() << "PointerCount: " << pointerCount; for (int r = 0; r < pointerCount; ++r) { - pointers.append(WeeChatProtocolHandler::handlePointer(data)); + pointers.append(handlePointer(data)); } objectSet.insert(QString("__path"), pointers); @@ -256,7 +269,8 @@ QVariantHash WeeChatProtocolHandler::handleHdata(QDataStream* data) for (QString keyName : orderedKeys) { Type type = stringToType(keys.value(keyName).toString()); - objectSet.insert(keyName, WeeChatProtocolHandler::handleDynamicType(type, data)); + qDebug() << "Handling key " << keyName; + objectSet.insert(keyName, handleDynamicType(type, data)); } objectSets.append(objectSet); @@ -413,6 +427,10 @@ void WeeChatProtocolHandler::handleBody(QDataStream* data) qDebug() << "ID: " << id; + Type type = readType(data); + qDebug() << "Type: " << type; + + // IDs for all event types if (id == "_pong") @@ -427,10 +445,12 @@ void WeeChatProtocolHandler::handleBody(QDataStream* data) { handleUpgradeEnded(id, data); } - else - { + else if (type == HDATA) { handleDefaultEvent(id, data); } + else { + qDebug() << "Unknown type!"; + } delete[] dataArr; @@ -442,27 +462,26 @@ void WeeChatProtocolHandler::handleBody(QDataStream* data) void WeeChatProtocolHandler::handleDefaultEvent(QString id, QDataStream* data) { - // Type for default events is hda, so skip it - readType(data); - + qDebug() << "Making hdata!"; QVariantHash hdata = handleHdata(data); - emit event(id, hdata); + qDebug() << "Got hdata made!"; + emit newEvent(id, hdata); } void WeeChatProtocolHandler::handlePong(QString id, QDataStream* data) { QString response = handleString(data); - emit event(id, response); + emit newEvent(id, response); } void WeeChatProtocolHandler::handleUpgrade(QString id, QDataStream* data) { // There is no data to read for this message - emit event(id, QVariant()); + emit newEvent(id, QVariant()); } void WeeChatProtocolHandler::handleUpgradeEnded(QString id, QDataStream* data) { // There is no data to read for this message - emit event(id, QVariant()); + emit newEvent(id, QVariant()); } diff --git a/src/weechatprotocolhandler.h b/src/weechatprotocolhandler.h index 5b7599c..d1f6fa5 100644 --- a/src/weechatprotocolhandler.h +++ b/src/weechatprotocolhandler.h @@ -63,7 +63,7 @@ public: static Type stringToType(QString type); signals: - void event(QString id, QVariant data); + void newEvent(QString id, QVariant data); public slots: void handleNewData(RelayConnection* connection);