diff --git a/include/qredis/request.h b/include/qredis/request.h index f6db126..2864024 100644 --- a/include/qredis/request.h +++ b/include/qredis/request.h @@ -3,7 +3,7 @@ #include #include -#include +#include #include "qredis_export.h" @@ -20,17 +20,6 @@ namespace QRedis public: - /** - * @brief Types of replies received from the Redis server - */ - enum ReplyType { - Status, - Error, - Integer, - Bulk, - MultiBulk - }; - /** * @brief Creates a request * @param parent @@ -52,11 +41,34 @@ namespace QRedis Q_SIGNALS: /** - * @brief Emitted when a reply is received - * @param type the type of value received - * @param value the value received + * @brief Emitted when a bulk reply is received + * @param value the value as a byte array */ - void reply(ReplyType type, const QVariant & value); + void bulk(const QByteArray & value); + + /** + * @brief Emitted when an error reply is received + * @param message a descriptive error message + */ + void error(const QString & message); + + /** + * @brief Emitted when an integer reply is received + * @param value the integer value + */ + void integer(qlonglong value); + + /** + * @brief Emitted when a multi-bulk reply is received + * @param a list of bulk values + */ + void multiBulk(const QVariantList & values); + + /** + * @brief Emitted when a status reply is received + * @param message a descriptive status message + */ + void status(const QString & message); private: diff --git a/src/client.cpp b/src/client.cpp index 4198d65..b311d9c 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -12,14 +12,26 @@ ClientPrivate::ClientPrivate(Client * client) connect(&socket, &QTcpSocket::readyRead, this, &ClientPrivate::readReply); } +/* + * Note: error replies actually contain a type and then the error description + * but we just combine them here for simplicity. + */ + bool ClientPrivate::readStatusOrError() { /* Check if the reply contains \r\n. */ int pos = buffer.indexOf("\r\n"); if(pos != -1) { - emit queue.dequeue()->reply(Request::Status, buffer.mid(1, pos - 1)); + Request * request = queue.dequeue(); + + if(buffer[0] == '+') + emit request->status(buffer.mid(1, pos - 1)); + else + emit request->error(buffer.mid(1, pos - 1)); + buffer.remove(0, pos + 2); + return true; } return false; @@ -27,6 +39,16 @@ bool ClientPrivate::readStatusOrError() bool ClientPrivate::readInteger() { + /* Check if the reply contains \r\n. */ + int pos = buffer.indexOf("\r\n"); + if(pos != -1) + { + emit queue.dequeue()->integer(buffer.mid(1, pos -1).toLongLong()); + + buffer.remove(0, pos + 2); + return true; + } + return false; } diff --git a/src/request.cpp b/src/request.cpp index d0b88e9..5a9a285 100644 --- a/src/request.cpp +++ b/src/request.cpp @@ -25,8 +25,12 @@ bool Request::waitForReply(int msecs) timer.setInterval(msecs); timer.setSingleShot(true); - connect(&timer, SIGNAL(timeout()), &d->loop, SLOT(quit())); - connect(this, SIGNAL(reply(ReplyType,QVariant)), d.data(), SLOT(quitEventLoop())); + connect(&timer, SIGNAL(timeout()), &d->loop, SLOT(quit())); + connect(this, SIGNAL(bulk(QByteArray)), d.data(), SLOT(quitEventLoop())); + connect(this, SIGNAL(error(QString)), d.data(), SLOT(quitEventLoop())); + connect(this, SIGNAL(integer(qlonglong)), d.data(), SLOT(quitEventLoop())); + connect(this, SIGNAL(multiBulk(QVariantList)), d.data(), SLOT(quitEventLoop())); + connect(this, SIGNAL(status(QString)), d.data(), SLOT(quitEventLoop())); /* * If the timer fires, the return value will be 0. diff --git a/tests/testclient.cpp b/tests/testclient.cpp index 036942e..4c10348 100644 --- a/tests/testclient.cpp +++ b/tests/testclient.cpp @@ -6,8 +6,6 @@ void TestClient::initTestCase() { - qRegisterMetaType("ReplyType"); - client.connectToHost("localhost"); QVERIFY(client.waitForConnected()); } @@ -23,9 +21,9 @@ void TestClient::cleanupTestCase() void TestClient::testPing() { QRedis::Request * request = client.sendCommand("PING"); - QSignalSpy spy(request, SIGNAL(reply(ReplyType,QVariant))); + QSignalSpy spy(request, SIGNAL(status(QString))); QVERIFY(request->waitForReply()); QCOMPARE(spy.count(), 1); - QCOMPARE(spy[0][1].toString(), QString("PONG")); + QCOMPARE(spy[0][0].toString(), QString("PONG")); }