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 --- client/resourceaccess.cpp | 12 +++++++++++- resource/listener.cpp | 31 ++++++++++++++++++++++++++++++- resource/listener.h | 1 + 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/client/resourceaccess.cpp b/client/resourceaccess.cpp index 7c92331..2972316 100644 --- a/client/resourceaccess.cpp +++ b/client/resourceaccess.cpp @@ -1,6 +1,7 @@ #include "resourceaccess.h" #include "common/console.h" +#include "common/commands.h" #include #include @@ -64,7 +65,16 @@ void ResourceAccess::connected() { m_startingProcess = false; Console::main()->log(QString("Connected: %1").arg(m_socket->fullServerName())); - m_socket->write("100 command " + QString::number((long long)this).toLatin1()); + + { + const QByteArray name = QString::number((long long)this).toLatin1(); + const int commandId = Commands::HandshakeCommand; + const int dataSize = name.size(); + m_socket->write((const char*)&commandId, sizeof(int)); + m_socket->write((const char*)&dataSize, sizeof(int)); + m_socket->write(name.data(), name.size()); + } + emit ready(true); } 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