From 97b79eeb86eedee57630b8d29f6eeab08ccb02b0 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Sun, 30 Nov 2014 13:05:19 +0100 Subject: add flatbuffer support and use that for the handshake --- CMakeLists.txt | 11 ++++++++ client/resourceaccess.cpp | 10 +++++-- cmake/modules/FindFlatBuffers.cmake | 56 +++++++++++++++++++++++++++++++++++++ common/CMakeLists.txt | 1 + common/commands/CMakeLists.txt | 1 + common/commands/handshake.fbs | 9 ++++++ resource/listener.cpp | 8 ++++-- 7 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 cmake/modules/FindFlatBuffers.cmake create mode 100644 common/CMakeLists.txt create mode 100644 common/commands/CMakeLists.txt create mode 100644 common/commands/handshake.fbs diff --git a/CMakeLists.txt b/CMakeLists.txt index e0e3796..91c4568 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 2.8) + # ECM setup find_package(ECM 0.0.10 REQUIRED NO_MODULE) set(CMAKE_MODULE_PATH @@ -18,11 +19,21 @@ include(ECMPackageConfigHelpers) # include(KDECMakeSettings) find_package(Qt5Core REQUIRED) +find_package(FlatBuffers REQUIRED) +function(generate_flatbuffers dest) + # TODO: move the file from ${fbs}_generated.h to just ${fbs}? + foreach(fbs ${ARGN}) + execute_process(COMMAND ${FLATBUFFERS_FLATC_EXECUTABLE} -c -b --gen-includes -o ${dest} ${CMAKE_CURRENT_SOURCE_DIR}/${fbs}) + endforeach(fbs) +endfunction(generate_flatbuffers) set(CMAKE_AUTOMOC ON) add_definitions("-Wall -std=c++0x") include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}) +# common, eventually a lib but right now just the command buffers +add_subdirectory(common) + # the client add_subdirectory(client) diff --git a/client/resourceaccess.cpp b/client/resourceaccess.cpp index 2972316..b0a26e7 100644 --- a/client/resourceaccess.cpp +++ b/client/resourceaccess.cpp @@ -2,6 +2,7 @@ #include "common/console.h" #include "common/commands.h" +#include "common/commands/handshake_generated.h" #include #include @@ -67,12 +68,15 @@ void ResourceAccess::connected() Console::main()->log(QString("Connected: %1").arg(m_socket->fullServerName())); { - const QByteArray name = QString::number((long long)this).toLatin1(); + flatbuffers::FlatBufferBuilder fbb; + auto name = fbb.CreateString("Client PID: " + QString::number((long long)this).toLatin1() + "!"); + auto command = Toynadi::CreateHandshake(fbb, name); + Toynadi::FinishHandshakeBuffer(fbb, command); const int commandId = Commands::HandshakeCommand; - const int dataSize = name.size(); + const int dataSize = fbb.GetSize(); m_socket->write((const char*)&commandId, sizeof(int)); m_socket->write((const char*)&dataSize, sizeof(int)); - m_socket->write(name.data(), name.size()); + m_socket->write((const char*)fbb.GetBufferPointer(), dataSize); } emit ready(true); diff --git a/cmake/modules/FindFlatBuffers.cmake b/cmake/modules/FindFlatBuffers.cmake new file mode 100644 index 0000000..0a54e2a --- /dev/null +++ b/cmake/modules/FindFlatBuffers.cmake @@ -0,0 +1,56 @@ +# Copyright 2014 Stefan.Eilemann@epfl.ch +# Copyright 2014 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Find the flatbuffers schema compiler +# +# Output Variables: +# * FLATBUFFERS_FLATC_EXECUTABLE the flatc compiler executable +# * FLATBUFFERS_FOUND +# +# Provides: +# * FLATBUFFERS_GENERATE_C_HEADERS(Name ) creates the C++ headers +# for the given flatbuffer schema files. +# Returns the header files in ${Name}_OUTPUTS + +find_program(FLATBUFFERS_FLATC_EXECUTABLE NAMES flatc) +find_path(FLATBUFFERS_INCLUDE_DIR NAMES flatbuffers/flatbuffers.h) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(flatbuffers + DEFAULT_MSG FLATBUFFERS_FLATC_EXECUTABLE FLATBUFFERS_INCLUDE_DIR) + +if(FLATBUFFERS_FOUND) + function(FLATBUFFERS_GENERATE_C_HEADERS Name) + set(FLATC_OUTPUTS) + foreach(FILE ${ARGN}) + get_filename_component(FLATC_OUTPUT ${FILE} NAME_WE) + set(FLATC_OUTPUT + "${CMAKE_CURRENT_BINARY_DIR}/${FLATC_OUTPUT}_generated.h") + list(APPEND FLATC_OUTPUTS ${FLATC_OUTPUT}) + + add_custom_command(OUTPUT ${FLATC_OUTPUT} + COMMAND ${FLATBUFFERS_FLATC_EXECUTABLE} + ARGS -c -o "${CMAKE_CURRENT_BINARY_DIR}/" ${FILE} + COMMENT "Building C++ header for ${FILE}" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + endforeach() + set(${Name}_OUTPUTS ${FLATC_OUTPUTS} PARENT_SCOPE) + endfunction() + + set(FLATBUFFERS_INCLUDE_DIRS ${FLATBUFFERS_INCLUDE_DIR}) + include_directories(${CMAKE_BINARY_DIR}) +else() + set(FLATBUFFERS_INCLUDE_DIR) +endif() diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt new file mode 100644 index 0000000..2ceacf8 --- /dev/null +++ b/common/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(commands) diff --git a/common/commands/CMakeLists.txt b/common/commands/CMakeLists.txt new file mode 100644 index 0000000..2a9ba64 --- /dev/null +++ b/common/commands/CMakeLists.txt @@ -0,0 +1 @@ +generate_flatbuffers(${CMAKE_CURRENT_BINARY_DIR} handshake.fbs) diff --git a/common/commands/handshake.fbs b/common/commands/handshake.fbs new file mode 100644 index 0000000..4492ca9 --- /dev/null +++ b/common/commands/handshake.fbs @@ -0,0 +1,9 @@ + +namespace Toynadi; + +table Handshake { + name: string; +} + +root_type Handshake; + diff --git a/resource/listener.cpp b/resource/listener.cpp index da1edc3..645cb8c 100644 --- a/resource/listener.cpp +++ b/resource/listener.cpp @@ -2,6 +2,7 @@ #include "common/console.h" #include "common/commands.h" +#include "common/commands/handshake_generated.h" #include #include @@ -130,11 +131,12 @@ void Listener::processClientBuffer(Client &client) client.commandBuffer.remove(0, headerSize + size); switch (commandId) { - case Commands::HandshakeCommand: - client.name = data; - Console::main()->log(QString(" Handshake from %1").arg(client.name)); + case Commands::HandshakeCommand: { + auto buffer = Toynadi::GetHandshake(data.constData()); + Console::main()->log(QString(" Handshake from %1").arg(buffer->name()->c_str())); //TODO: reply? break; + } default: // client.hasSentCommand = true; break; -- cgit v1.2.3