diff --git a/native/cpp/CommonCpp/grpc/CMakeLists.txt b/native/cpp/CommonCpp/grpc/CMakeLists.txt index 2deb2a28b..ac0a23fdd 100644 --- a/native/cpp/CommonCpp/grpc/CMakeLists.txt +++ b/native/cpp/CommonCpp/grpc/CMakeLists.txt @@ -1,138 +1,68 @@ project(grpc-comm) cmake_minimum_required(VERSION 3.4) include(GNUInstallDirs) find_package(protobuf REQUIRED) find_package(gRPC REQUIRED) set(CMAKE_CXX_STANDARD 14) -# Allow for tools on PATH to be found -find_program(_PROTOBUF_PROTOC protoc - HINTS "${_PROTOBUF_PROTOC}") -find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin - HINTS "${_GRPC_CPP_PLUGIN_EXECUTABLE}") -set(_proto_dir "${CMAKE_CURRENT_SOURCE_DIR}/protos") -set(_components backup blob tunnelbroker) -set(TARGETS) - -# Iterate through each protobuf file -# and create headers, sources, and export as component library -foreach(component ${_components}) - set(LIB_NAME comm-${component}-grpc) - set(TARGETS ${TARGETS} ${LIB_NAME}) - - set(BIN_PROTO_HDRS - ${component}.pb.h - ${component}.grpc.pb.h - ) - - set(BIN_PROTO_SRCS - ${component}.pb.cc - ${component}.grpc.pb.cc - ) - - set(_proto_file "${_proto_dir}/${component}.proto") - # Generate headers from protobuf files, and copy them to _generated dir - add_custom_command( - OUTPUT ${BIN_PROTO_HDRS} ${BIN_PROTO_SRCS} - COMMAND ${_PROTOBUF_PROTOC} --grpc_out "${CMAKE_CURRENT_BINARY_DIR}" - --cpp_out "${CMAKE_CURRENT_BINARY_DIR}" - -I "${_proto_dir}" - --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}" - "${_proto_file}" - COMMAND sed 's|^// Genera|// @genera|g' ${component}.grpc.pb.h - > ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.grpc.pb.h - COMMAND sed 's|^// Genera|// @genera|g' ${component}.grpc.pb.cc - > ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.grpc.pb.cc - COMMAND sed 's|^// Genera|// @genera|g' ${component}.pb.h - > ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.pb.h - COMMAND sed 's|^// Genera|// @genera|g' ${component}.pb.cc - > ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.pb.cc - DEPENDS "${_proto_file}" - MAIN_DEPENDENCY "${_proto_file}" - COMMENT "Generate protobuf files for ${component}" - ) - - add_library(${LIB_NAME} - ${BIN_PROTO_HDRS} ${BIN_PROTO_SRCS} - ) - - target_link_libraries(${LIB_NAME} - gRPC::grpc++ - protobuf::libprotobuf - ) - - # reference local directory when building - # use installation path when installing - target_include_directories(${LIB_NAME} - PUBLIC - $ - $ - $ - ) - - install(TARGETS ${LIB_NAME} EXPORT comm-export - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${LIB_NAME} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${LIB_NAME} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${LIB_NAME} - ) - - # ensure headers are also installed - install(FILES ${PROTO_HDRS} DESTINATION include/comm/grpc) -endforeach() +add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../../../shared/protos" + "${CMAKE_CURRENT_BINARY_DIR}/protos" +) set(CLIENT_HDRS "Client.h" "ClientGetReadReactor.h" "GRPCStreamHostObject.h" ) set(CLIENT_SRCS "Client.cpp" "ClientGetReadReactor.cpp" "GRPCStreamHostObject.cpp" ) add_library(comm-client ${CLIENT_HDRS} ${CLIENT_SRCS} ) target_link_libraries(comm-client gRPC::grpc++ protobuf::libprotobuf comm-tunnelbroker-grpc ) target_include_directories(comm-client PUBLIC $ + $ $ $ # HACK: add complete cmake support to react-native? PRIVATE "../../../node_modules/react-native/ReactCommon/callinvoker" "../../../node_modules/react-native/ReactCommon/jsi" ) install(TARGETS comm-client EXPORT comm-export RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT comm-client LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT comm-client ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT comm-client ) install(FILES ${CLIENT_HDRS} DESTINATION include/grpc) # For development purposes, able to reference build directory -export(TARGETS ${TARGETS} comm-client - NAMESPACE comm-grpc:: - FILE ${CMAKE_CURRENT_BINARY_DIR}/cmake/comm-grpc/comm-grpc-targets.cmake +export(TARGETS comm-client + NAMESPACE comm:: + FILE ${CMAKE_CURRENT_BINARY_DIR}/cmake/comm/comm-grpc-client-targets.cmake ) # For installation install(EXPORT comm-export FILE comm-grpc-targets.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/comm-grpc NAMESPACE comm-grpc:: ) diff --git a/native/cpp/CommonCpp/grpc/Client.h b/native/cpp/CommonCpp/grpc/Client.h index 728db715d..23f8d995b 100644 --- a/native/cpp/CommonCpp/grpc/Client.h +++ b/native/cpp/CommonCpp/grpc/Client.h @@ -1,62 +1,62 @@ #pragma once #include #include #include #include "ClientGetReadReactor.h" -#include "_generated/tunnelbroker.grpc.pb.h" -#include "_generated/tunnelbroker.pb.h" +#include "tunnelbroker.grpc.pb.h" +#include "tunnelbroker.pb.h" namespace comm { namespace network { using grpc::Channel; using tunnelbroker::CheckResponseType; using tunnelbroker::TunnelbrokerService; class Client { std::unique_ptr stub_; const std::string id; const std::string deviceToken; std::unique_ptr clientGetReadReactor; public: Client( std::string hostname, std::string port, std::shared_ptr credentials, const std::string id, const std::string deviceToken); CheckResponseType checkIfPrimaryDeviceOnline(); bool becomeNewPrimaryDevice(); void sendPong(); grpc::Status send( std::string sessionID, std::string toDeviceID, std::string payload, std::vector blobHashes); void get(std::string sessionID); void setOnReadDoneCallback(std::function callback); void setOnOpenCallback(std::function callback); void setOnCloseCallback(std::function callback); void closeGetStream(); void assignSetReadyStateCallback(std::function callback); std::string sessionSignature(std::string deviceID); std::string newSession( std::string deviceID, std::string publicKey, std::string signature, std::string notifyToken, tunnelbroker::NewSessionRequest_DeviceTypes deviceType, std::string deviceAppVersion, std::string deviceOS); }; } // namespace network } // namespace comm diff --git a/native/cpp/CommonCpp/grpc/ClientGetReadReactor.h b/native/cpp/CommonCpp/grpc/ClientGetReadReactor.h index b70124248..e1d9eb4aa 100644 --- a/native/cpp/CommonCpp/grpc/ClientGetReadReactor.h +++ b/native/cpp/CommonCpp/grpc/ClientGetReadReactor.h @@ -1,39 +1,39 @@ #pragma once #include "../NativeModules/InternalModules/SocketStatus.h" #include -#include "_generated/tunnelbroker.grpc.pb.h" -#include "_generated/tunnelbroker.pb.h" +#include "tunnelbroker.grpc.pb.h" +#include "tunnelbroker.pb.h" class ClientGetReadReactor : public grpc::ClientReadReactor { std::string sessionID; grpc::ClientContext context; tunnelbroker::GetRequest request; tunnelbroker::GetResponse response; std::mutex onReadDoneCallbackMutex; std::mutex onOpenCallbackMutex; std::mutex onCloseCallbackMutex; std::mutex setReadyStateMutex; std::function onReadDoneCallback; std::function onOpenCallback; std::function onCloseCallback; std::function setReadyState; public: ClientGetReadReactor( tunnelbroker::TunnelbrokerService::Stub *stub, std::string sessionID); void OnReadInitialMetadataDone(bool ok) override; void OnReadDone(bool ok) override; void OnDone(const grpc::Status &status) override; void close(); void setOnOpenCallback(std::function onOpenCallback); void setOnReadDoneCallback(std::function onReadDoneCallback); void setOnCloseCallback(std::function onCloseCallback); void assignSetReadyStateCallback(std::function callback); };