From 9c31df56c894892f20a30dc336cfe608b14679e0 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Tue, 2 Dec 2014 15:19:48 +0100 Subject: consolidate command writing to avoid duplicating those 4 lines everywhere --- client/resourceaccess.cpp | 8 ++------ common/CMakeLists.txt | 1 + common/commands.h | 6 ++++++ resource/listener.cpp | 13 +++---------- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/client/resourceaccess.cpp b/client/resourceaccess.cpp index 6d48c51..641c203 100644 --- a/client/resourceaccess.cpp +++ b/client/resourceaccess.cpp @@ -73,14 +73,10 @@ void ResourceAccess::connected() { flatbuffers::FlatBufferBuilder fbb; - auto name = fbb.CreateString("Client PID: " + QString::number((long long)this).toLatin1() + "!"); + auto name = fbb.CreateString(QString::number((long long)this).toLatin1()); auto command = Akonadi::CreateHandshake(fbb, name); Akonadi::FinishHandshakeBuffer(fbb, command); - const int commandId = Commands::HandshakeCommand; - const int dataSize = fbb.GetSize(); - m_socket->write((const char*)&commandId, sizeof(int)); - m_socket->write((const char*)&dataSize, sizeof(int)); - m_socket->write((const char*)fbb.GetBufferPointer(), dataSize); + Commands::write(m_socket, Commands::HandshakeCommand, fbb); } emit ready(true); diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 6533e28..d409828 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -3,6 +3,7 @@ generate_flatbuffers(commands/handshake commands/revisionupdate) set(command_SRCS + commands.cpp console.cpp ${CMAKE_CURRENT_BINARY_DIR}/commands/handshake_generated.h) diff --git a/common/commands.h b/common/commands.h index 68a7c1e..534308d 100644 --- a/common/commands.h +++ b/common/commands.h @@ -1,5 +1,9 @@ #pragma once +#include + +class QIODevice; + namespace Commands { @@ -10,4 +14,6 @@ enum CommandIds { CustomCommand = 0xffff }; +void write(QIODevice *device, int commandId, flatbuffers::FlatBufferBuilder &fbb); + } \ No newline at end of file diff --git a/resource/listener.cpp b/resource/listener.cpp index 038b3fa..4b3e4ff 100644 --- a/resource/listener.cpp +++ b/resource/listener.cpp @@ -173,11 +173,7 @@ void Listener::sendCurrentRevision(Client &client) flatbuffers::FlatBufferBuilder fbb; auto command = Akonadi::CreateRevisionUpdate(fbb, m_revision); Akonadi::FinishRevisionUpdateBuffer(fbb, command); - const int commandId = Commands::RevisionUpdateCommand; - const int dataSize = fbb.GetSize(); - client.socket->write((const char*)&commandId, sizeof(int)); - client.socket->write((const char*)&dataSize, sizeof(int)); - client.socket->write((const char*)fbb.GetBufferPointer(), dataSize); + Commands::write(client.socket, Commands::RevisionUpdateCommand, fbb); } void Listener::updateClientsWithRevision() @@ -185,15 +181,12 @@ void Listener::updateClientsWithRevision() flatbuffers::FlatBufferBuilder fbb; auto command = Akonadi::CreateRevisionUpdate(fbb, m_revision); Akonadi::FinishRevisionUpdateBuffer(fbb, command); - const int commandId = Commands::RevisionUpdateCommand; - const int dataSize = fbb.GetSize(); for (const Client &client: m_connections) { if (!client.socket || !client.socket->isValid()) { continue; } - client.socket->write((const char*)&commandId, sizeof(int)); - client.socket->write((const char*)&dataSize, sizeof(int)); - client.socket->write((const char*)fbb.GetBufferPointer(), dataSize); + + Commands::write(client.socket, Commands::RevisionUpdateCommand, fbb); } } -- cgit v1.2.3