From 4adae7c3711abbeaa7f03c699a7d366c78f776bc Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 21 Nov 2014 13:52:47 +0100 Subject: a read buffer for client communication --- resource/listener.cpp | 44 +++++++++++++++++++++++++++++++++++++------- resource/listener.h | 2 ++ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/resource/listener.cpp b/resource/listener.cpp index ae16da7..f5920b7 100644 --- a/resource/listener.cpp +++ b/resource/listener.cpp @@ -35,29 +35,38 @@ Listener::~Listener() void Listener::closeAllConnections() { //TODO: close all client connectionsin m_connections + for (Client &client: m_connections) { + if (client.socket) { + client.socket->close(); + delete client.socket; + client.socket = 0; + } + } } void Listener::acceptConnection() { Console::main()->log(QString("Accepting connection")); - QLocalSocket *connection = m_server->nextPendingConnection(); + QLocalSocket *socket = m_server->nextPendingConnection(); - if (!connection) { + if (!socket) { return; } Console::main()->log("Got a connection"); - Client client("Unknown Client" /*fixme: actual names!*/, connection); + Client client("Unknown Client" /*fixme: actual names!*/, socket); + connect(socket, &QIODevice::readyRead, + this, &Listener::readFromSocket); m_connections << client; - connect(connection, &QLocalSocket::disconnected, + connect(socket, &QLocalSocket::disconnected, this, &Listener::clientDropped); } void Listener::clientDropped() { - QLocalSocket *connection = qobject_cast(sender()); - if (!connection) { + QLocalSocket *socket = qobject_cast(sender()); + if (!socket) { return; } @@ -65,7 +74,7 @@ void Listener::clientDropped() QMutableListIterator it(m_connections); while (it.hasNext()) { const Client &client = it.next(); - if (client.socket == connection) { + if (client.socket == socket) { Console::main()->log(QString(" dropped... %1").arg(client.name)); it.remove(); break; @@ -82,3 +91,24 @@ void Listener::checkConnections() emit noClients(); } } + +void Listener::readFromSocket() +{ + QLocalSocket *socket = qobject_cast(sender()); + if (!socket) { + return; + } + + Console::main()->log("Reading from socket..."); + QMutableListIterator it(m_connections); + while (it.hasNext()) { + Client &client = it.next(); + if (client.socket == socket) { + Console::main()->log(QString(" Client: %1").arg(client.name)); + client.commandBuffer += socket->readAll(); + Console::main()->log(QString(" Command: %1").arg(QString(client.commandBuffer))); + break; + } + } + +} diff --git a/resource/listener.h b/resource/listener.h index 1d41467..536ba39 100644 --- a/resource/listener.h +++ b/resource/listener.h @@ -17,6 +17,7 @@ public: QString name; QLocalSocket *socket; + QByteArray commandBuffer; bool hasSentCommand; }; @@ -38,6 +39,7 @@ private Q_SLOTS: void acceptConnection(); void clientDropped(); void checkConnections(); + void readFromSocket(); private: QLocalServer *m_server; -- cgit v1.2.3