From 9229f4aea0765265b74111ea51798efde7d07997 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 21 Nov 2014 17:56:35 +0100 Subject: how a command accross the wire might look: initial handshake --- resource/listener.cpp | 31 ++++++++++++++++++++++++++++++- resource/listener.h | 1 + 2 files changed, 31 insertions(+), 1 deletion(-) (limited to 'resource') diff --git a/resource/listener.cpp b/resource/listener.cpp index f5920b7..da1edc3 100644 --- a/resource/listener.cpp +++ b/resource/listener.cpp @@ -1,6 +1,7 @@ #include "listener.h" #include "common/console.h" +#include "common/commands.h" #include #include @@ -106,9 +107,37 @@ void Listener::readFromSocket() 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))); + processClientBuffer(client); break; } } +} +void Listener::processClientBuffer(Client &client) +{ + static const int headerSize = (sizeof(int) * 2); + Console::main()->log(QString("processing %1").arg(client.commandBuffer.size())); + if (client.commandBuffer.size() < headerSize) { + return; + } + + int commandId, size; + commandId = *(int*)client.commandBuffer.constData(); + size = *(int*)(client.commandBuffer.constData() + sizeof(int)); + + if (size <= client.commandBuffer.size() - headerSize) { + QByteArray data = client.commandBuffer.mid(headerSize, size); + client.commandBuffer.remove(0, headerSize + size); + + switch (commandId) { + case Commands::HandshakeCommand: + client.name = data; + Console::main()->log(QString(" Handshake from %1").arg(client.name)); + //TODO: reply? + break; + default: + // client.hasSentCommand = true; + break; + } + } } diff --git a/resource/listener.h b/resource/listener.h index 536ba39..bfafe5f 100644 --- a/resource/listener.h +++ b/resource/listener.h @@ -42,6 +42,7 @@ private Q_SLOTS: void readFromSocket(); private: + void processClientBuffer(Client &client); QLocalServer *m_server; QList m_connections; }; \ No newline at end of file -- cgit v1.2.3