From fabaae522d0593f3b6c363e25eba2643a0b5d64d Mon Sep 17 00:00:00 2001 From: Green Sky Date: Fri, 16 Dec 2022 18:05:40 +0100 Subject: [PATCH] add vim ipc slave, no functionallity yet --- CMakeLists.txt | 3 ++ vim_research/CMakeLists.txt | 16 ++++++++ vim_research/test1.cpp | 77 +++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 vim_research/CMakeLists.txt create mode 100644 vim_research/test1.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 98277cc..613cdea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") #option(BUILD_TESTING "Enable testing with ctest." OFF) add_subdirectory(./external/json) +add_subdirectory(./external/zed_net) # Bump up warning levels appropriately for clang, gcc & msvc if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") @@ -37,3 +38,5 @@ add_subdirectory(./prototyping EXCLUDE_FROM_ALL) add_subdirectory(./version0) +add_subdirectory(./vim_research) + diff --git a/vim_research/CMakeLists.txt b/vim_research/CMakeLists.txt new file mode 100644 index 0000000..86b1b8d --- /dev/null +++ b/vim_research/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.9 FATAL_ERROR) + +project(vim_research CXX C) + +######################################## + +add_executable(vim_research_test1 + ./test1.cpp +) + +target_link_libraries(vim_research_test1 PUBLIC + crdt_version0 + zed_net + nlohmann_json::nlohmann_json +) + diff --git a/vim_research/test1.cpp b/vim_research/test1.cpp new file mode 100644 index 0000000..4e494cc --- /dev/null +++ b/vim_research/test1.cpp @@ -0,0 +1,77 @@ +#include + +#include +#include + +#include +#include + +// single letter agent, for testing only +using Agent = char; +using DocType = GreenCRDT::TextDocument; +using ListType = DocType::ListType; + +int main(void) { + std::cout << "starting vim ipc server\n"; + + if (zed_net_init() != 0) { + std::cerr << "zed_net_init failed: " << zed_net_get_error() << "\n"; + return -1; + } + + std::cout << "initialized zed_net\n"; + + const unsigned int port {1337}; + zed_net_socket_t listen_socket; + if (zed_net_tcp_socket_open( + &listen_socket, + port, // port + 0, // non blocking + 1 // listen + ) != 0) { + std::cerr << "zed_net_tcp_socket_open failed: " << zed_net_get_error() << "\n"; + zed_net_shutdown(); + return -1; + } + + std::cout << "listening on " << port << "\n"; + + zed_net_socket_t remote_socket; + zed_net_address_t remote_address; + if (zed_net_tcp_accept(&listen_socket, &remote_socket, &remote_address) != 0) { + std::cerr << "zed_net_tcp_accept failed: " << zed_net_get_error() << "\n"; + zed_net_socket_close(&listen_socket); + zed_net_shutdown(); + return -1; + } + + std::cout << "got connection from " << zed_net_host_to_str(remote_address.host) << ":" << remote_address.port << "\n"; + + while (true) { + // 10MiB + auto buffer = std::make_unique>(); + + int64_t bytes_received {0}; + bytes_received = zed_net_tcp_socket_receive(&remote_socket, buffer->data(), buffer->size()); + if (bytes_received < 0) { + std::cerr << "zed_net_tcp_socket_receive failed: " << zed_net_get_error() << "\n"; + zed_net_socket_close(&remote_socket); + zed_net_socket_close(&listen_socket); + zed_net_shutdown(); + return -1; + } else if (bytes_received == 0) { + std::cout << "got 0 bytes?\n"; + break; // connection closed + } + + std::cout << "got " << bytes_received << " bytes\n"; + // do data decoding + } + + std::cout << "shutting down\n"; + + zed_net_socket_close(&listen_socket); + zed_net_shutdown(); + return 0; +} +