From 2165ab207005cf5a6684c6c4256ad5f246730cc8 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sat, 17 Dec 2022 19:57:10 +0100 Subject: [PATCH] add toxcore and start on vim test2 --- .gitmodules | 5 + CMakeLists.txt | 1 + external/toxcore/CMakeLists.txt | 7 + external/toxcore/c-toxcore | 1 + external/toxcore/cmake/Findsodium.cmake | 297 +++++++++++++ external/toxcore/toxcore.cmake | 158 +++++++ vim_research/CMakeLists.txt | 13 + vim_research/test2.cpp | 554 ++++++++++++++++++++++++ 8 files changed, 1036 insertions(+) create mode 100644 .gitmodules create mode 100644 external/toxcore/CMakeLists.txt create mode 160000 external/toxcore/c-toxcore create mode 100644 external/toxcore/cmake/Findsodium.cmake create mode 100644 external/toxcore/toxcore.cmake create mode 100644 vim_research/test2.cpp diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..bdea472 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,5 @@ +[submodule "external/toxcore/c-toxcore"] + path = external/toxcore/c-toxcore + url = https://github.com/TokTok/c-toxcore.git + shallow = true + ignore = dirty diff --git a/CMakeLists.txt b/CMakeLists.txt index 613cdea..e70f9d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") add_subdirectory(./external/json) add_subdirectory(./external/zed_net) +add_subdirectory(./external/toxcore) # Bump up warning levels appropriately for clang, gcc & msvc if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") diff --git a/external/toxcore/CMakeLists.txt b/external/toxcore/CMakeLists.txt new file mode 100644 index 0000000..624fe1b --- /dev/null +++ b/external/toxcore/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.9 FATAL_ERROR) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) + +include(./toxcore.cmake) + + diff --git a/external/toxcore/c-toxcore b/external/toxcore/c-toxcore new file mode 160000 index 0000000..d222d70 --- /dev/null +++ b/external/toxcore/c-toxcore @@ -0,0 +1 @@ +Subproject commit d222d708b5756a496917f9b7e713a8bc678334f3 diff --git a/external/toxcore/cmake/Findsodium.cmake b/external/toxcore/cmake/Findsodium.cmake new file mode 100644 index 0000000..a210c00 --- /dev/null +++ b/external/toxcore/cmake/Findsodium.cmake @@ -0,0 +1,297 @@ +# Written in 2016 by Henrik Steffen Gaßmann +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any warranty. +# +# You should have received a copy of the CC0 Public Domain Dedication +# along with this software. If not, see +# +# http://creativecommons.org/publicdomain/zero/1.0/ +# +######################################################################## +# Tries to find the local libsodium installation. +# +# On Windows the sodium_DIR environment variable is used as a default +# hint which can be overridden by setting the corresponding cmake variable. +# +# Once done the following variables will be defined: +# +# sodium_FOUND +# sodium_INCLUDE_DIR +# sodium_LIBRARY_DEBUG +# sodium_LIBRARY_RELEASE +# +# +# Furthermore an imported "sodium" target is created. +# + +if (CMAKE_C_COMPILER_ID STREQUAL "GNU" + OR CMAKE_C_COMPILER_ID STREQUAL "Clang") + set(_GCC_COMPATIBLE 1) +endif() + +# static library option +if (NOT DEFINED sodium_USE_STATIC_LIBS) + option(sodium_USE_STATIC_LIBS "enable to statically link against sodium" OFF) +endif() +if(NOT (sodium_USE_STATIC_LIBS EQUAL sodium_USE_STATIC_LIBS_LAST)) + unset(sodium_LIBRARY CACHE) + unset(sodium_LIBRARY_DEBUG CACHE) + unset(sodium_LIBRARY_RELEASE CACHE) + unset(sodium_DLL_DEBUG CACHE) + unset(sodium_DLL_RELEASE CACHE) + set(sodium_USE_STATIC_LIBS_LAST ${sodium_USE_STATIC_LIBS} CACHE INTERNAL "internal change tracking variable") +endif() + + +######################################################################## +# UNIX +if (UNIX) + # import pkg-config + find_package(PkgConfig QUIET) + if (PKG_CONFIG_FOUND) + pkg_check_modules(sodium_PKG QUIET libsodium) + endif() + + if(sodium_USE_STATIC_LIBS) + foreach(_libname ${sodium_PKG_STATIC_LIBRARIES}) + if (NOT _libname MATCHES "^lib.*\\.a$") # ignore strings already ending with .a + list(INSERT sodium_PKG_STATIC_LIBRARIES 0 "lib${_libname}.a") + endif() + endforeach() + list(REMOVE_DUPLICATES sodium_PKG_STATIC_LIBRARIES) + + # if pkgconfig for libsodium doesn't provide + # static lib info, then override PKG_STATIC here.. + if (NOT sodium_PKG_STATIC_FOUND) + set(sodium_PKG_STATIC_LIBRARIES libsodium.a) + endif() + + set(XPREFIX sodium_PKG_STATIC) + else() + if (NOT sodium_PKG_FOUND) + set(sodium_PKG_LIBRARIES sodium) + endif() + + set(XPREFIX sodium_PKG) + endif() + + find_path(sodium_INCLUDE_DIR sodium.h + HINTS ${${XPREFIX}_INCLUDE_DIRS} + ) + find_library(sodium_LIBRARY_DEBUG NAMES ${${XPREFIX}_LIBRARIES} + HINTS ${${XPREFIX}_LIBRARY_DIRS} + ) + find_library(sodium_LIBRARY_RELEASE NAMES ${${XPREFIX}_LIBRARIES} + HINTS ${${XPREFIX}_LIBRARY_DIRS} + ) + + +######################################################################## +# Windows +elseif (WIN32) + set(sodium_DIR "$ENV{sodium_DIR}" CACHE FILEPATH "sodium install directory") + mark_as_advanced(sodium_DIR) + + find_path(sodium_INCLUDE_DIR sodium.h + HINTS ${sodium_DIR} + PATH_SUFFIXES include + ) + + if (MSVC) + # detect target architecture + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/arch.cpp" [=[ + #if defined _M_IX86 + #error ARCH_VALUE x86_32 + #elif defined _M_X64 + #error ARCH_VALUE x86_64 + #endif + #error ARCH_VALUE unknown + ]=]) + try_compile(_UNUSED_VAR "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/arch.cpp" + OUTPUT_VARIABLE _COMPILATION_LOG + ) + string(REGEX REPLACE ".*ARCH_VALUE ([a-zA-Z0-9_]+).*" "\\1" _TARGET_ARCH "${_COMPILATION_LOG}") + + # construct library path + if (_TARGET_ARCH STREQUAL "x86_32") + string(APPEND _PLATFORM_PATH "Win32") + elseif(_TARGET_ARCH STREQUAL "x86_64") + string(APPEND _PLATFORM_PATH "x64") + else() + message(FATAL_ERROR "the ${_TARGET_ARCH} architecture is not supported by Findsodium.cmake.") + endif() + string(APPEND _PLATFORM_PATH "/$$CONFIG$$") + + if (MSVC_VERSION LESS 1900) + math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 60") + else() + math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 50") + endif() + string(APPEND _PLATFORM_PATH "/v${_VS_VERSION}") + + if (sodium_USE_STATIC_LIBS) + string(APPEND _PLATFORM_PATH "/static") + else() + string(APPEND _PLATFORM_PATH "/dynamic") + endif() + + string(REPLACE "$$CONFIG$$" "Debug" _DEBUG_PATH_SUFFIX "${_PLATFORM_PATH}") + string(REPLACE "$$CONFIG$$" "Release" _RELEASE_PATH_SUFFIX "${_PLATFORM_PATH}") + + find_library(sodium_LIBRARY_DEBUG libsodium.lib + HINTS ${sodium_DIR} + PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX} + ) + find_library(sodium_LIBRARY_RELEASE libsodium.lib + HINTS ${sodium_DIR} + PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX} + ) + if (NOT sodium_USE_STATIC_LIBS) + set(CMAKE_FIND_LIBRARY_SUFFIXES_BCK ${CMAKE_FIND_LIBRARY_SUFFIXES}) + set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll") + find_library(sodium_DLL_DEBUG libsodium + HINTS ${sodium_DIR} + PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX} + ) + find_library(sodium_DLL_RELEASE libsodium + HINTS ${sodium_DIR} + PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX} + ) + set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_BCK}) + endif() + + elseif(_GCC_COMPATIBLE) + if (sodium_USE_STATIC_LIBS) + find_library(sodium_LIBRARY_DEBUG libsodium.a + HINTS ${sodium_DIR} + PATH_SUFFIXES lib + ) + find_library(sodium_LIBRARY_RELEASE libsodium.a + HINTS ${sodium_DIR} + PATH_SUFFIXES lib + ) + else() + find_library(sodium_LIBRARY_DEBUG libsodium.dll.a + HINTS ${sodium_DIR} + PATH_SUFFIXES lib + ) + find_library(sodium_LIBRARY_RELEASE libsodium.dll.a + HINTS ${sodium_DIR} + PATH_SUFFIXES lib + ) + + file(GLOB _DLL + LIST_DIRECTORIES false + RELATIVE "${sodium_DIR}/bin" + "${sodium_DIR}/bin/libsodium*.dll" + ) + find_library(sodium_DLL_DEBUG ${_DLL} libsodium + HINTS ${sodium_DIR} + PATH_SUFFIXES bin + ) + find_library(sodium_DLL_RELEASE ${_DLL} libsodium + HINTS ${sodium_DIR} + PATH_SUFFIXES bin + ) + endif() + else() + message(FATAL_ERROR "this platform is not supported by FindSodium.cmake") + endif() + + +######################################################################## +# unsupported +else() + message(FATAL_ERROR "this platform is not supported by FindSodium.cmake") +endif() + + +######################################################################## +# common stuff + +# extract sodium version +if (sodium_INCLUDE_DIR) + set(_VERSION_HEADER "${_INCLUDE_DIR}/sodium/version.h") + if (EXISTS _VERSION_HEADER) + file(READ "${_VERSION_HEADER}" _VERSION_HEADER_CONTENT) + string(REGEX REPLACE ".*#[ \t]*define[ \t]*SODIUM_VERSION_STRING[ \t]*\"([^\n]*)\".*" "\\1" + sodium_VERSION "${_VERSION_HEADER_CONTENT}") + set(sodium_VERSION "${sodium_VERSION}" PARENT_SCOPE) + endif() +endif() + +# communicate results +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args( + sodium # The name must be either uppercase or match the filename case. + REQUIRED_VARS + sodium_LIBRARY_RELEASE + sodium_LIBRARY_DEBUG + sodium_INCLUDE_DIR + VERSION_VAR + sodium_VERSION +) + +if(Sodium_FOUND) + set(sodium_LIBRARIES + optimized ${sodium_LIBRARY_RELEASE} debug ${sodium_LIBRARY_DEBUG}) +endif() + +# mark file paths as advanced +mark_as_advanced(sodium_INCLUDE_DIR) +mark_as_advanced(sodium_LIBRARY_DEBUG) +mark_as_advanced(sodium_LIBRARY_RELEASE) +if (WIN32) + mark_as_advanced(sodium_DLL_DEBUG) + mark_as_advanced(sodium_DLL_RELEASE) +endif() + +# create imported target +if(sodium_USE_STATIC_LIBS) + set(_LIB_TYPE STATIC) +else() + set(_LIB_TYPE SHARED) +endif() + +if(NOT TARGET sodium) + add_library(sodium ${_LIB_TYPE} IMPORTED) +endif() + +set_target_properties(sodium PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${sodium_INCLUDE_DIR}" + IMPORTED_LINK_INTERFACE_LANGUAGES "C" +) + +if (sodium_USE_STATIC_LIBS) + set_target_properties(sodium PROPERTIES + INTERFACE_COMPILE_DEFINITIONS "SODIUM_STATIC" + IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}" + IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}" + ) +else() + if (UNIX) + set_target_properties(sodium PROPERTIES + IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}" + IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}" + ) + elseif (WIN32) + set_target_properties(sodium PROPERTIES + IMPORTED_IMPLIB "${sodium_LIBRARY_RELEASE}" + IMPORTED_IMPLIB_DEBUG "${sodium_LIBRARY_DEBUG}" + ) + if (NOT (sodium_DLL_DEBUG MATCHES ".*-NOTFOUND")) + set_target_properties(sodium PROPERTIES + IMPORTED_LOCATION_DEBUG "${sodium_DLL_DEBUG}" + ) + endif() + if (NOT (sodium_DLL_RELEASE MATCHES ".*-NOTFOUND")) + set_target_properties(sodium PROPERTIES + IMPORTED_LOCATION_RELWITHDEBINFO "${sodium_DLL_RELEASE}" + IMPORTED_LOCATION_MINSIZEREL "${sodium_DLL_RELEASE}" + IMPORTED_LOCATION_RELEASE "${sodium_DLL_RELEASE}" + ) + endif() + endif() +endif() diff --git a/external/toxcore/toxcore.cmake b/external/toxcore/toxcore.cmake new file mode 100644 index 0000000..de52c2f --- /dev/null +++ b/external/toxcore/toxcore.cmake @@ -0,0 +1,158 @@ +set(TOX_DIR "${CMAKE_CURRENT_SOURCE_DIR}/c-toxcore/") + +# TODO: shared +add_library(toxcore STATIC + ${TOX_DIR}third_party/cmp/cmp.c + ${TOX_DIR}third_party/cmp/cmp.h + + ${TOX_DIR}toxcore/announce.c + ${TOX_DIR}toxcore/announce.h + ${TOX_DIR}toxcore/bin_pack.c + ${TOX_DIR}toxcore/bin_pack.h + ${TOX_DIR}toxcore/bin_unpack.c + ${TOX_DIR}toxcore/bin_unpack.h + ${TOX_DIR}toxcore/ccompat.c + ${TOX_DIR}toxcore/ccompat.h + ${TOX_DIR}toxcore/crypto_core.c + ${TOX_DIR}toxcore/crypto_core.h + ${TOX_DIR}toxcore/DHT.c + ${TOX_DIR}toxcore/DHT.h + ${TOX_DIR}toxcore/events/conference_connected.c + ${TOX_DIR}toxcore/events/conference_invite.c + ${TOX_DIR}toxcore/events/conference_message.c + ${TOX_DIR}toxcore/events/conference_peer_list_changed.c + ${TOX_DIR}toxcore/events/conference_peer_name.c + ${TOX_DIR}toxcore/events/conference_title.c + ${TOX_DIR}toxcore/events/events_alloc.c + ${TOX_DIR}toxcore/events/events_alloc.h + ${TOX_DIR}toxcore/events/file_chunk_request.c + ${TOX_DIR}toxcore/events/file_recv.c + ${TOX_DIR}toxcore/events/file_recv_chunk.c + ${TOX_DIR}toxcore/events/file_recv_control.c + ${TOX_DIR}toxcore/events/friend_connection_status.c + ${TOX_DIR}toxcore/events/friend_lossless_packet.c + ${TOX_DIR}toxcore/events/friend_lossy_packet.c + ${TOX_DIR}toxcore/events/friend_message.c + ${TOX_DIR}toxcore/events/friend_name.c + ${TOX_DIR}toxcore/events/friend_read_receipt.c + ${TOX_DIR}toxcore/events/friend_request.c + ${TOX_DIR}toxcore/events/friend_status.c + ${TOX_DIR}toxcore/events/friend_status_message.c + ${TOX_DIR}toxcore/events/friend_typing.c + ${TOX_DIR}toxcore/events/self_connection_status.c + ${TOX_DIR}toxcore/forwarding.c + ${TOX_DIR}toxcore/forwarding.h + ${TOX_DIR}toxcore/friend_connection.c + ${TOX_DIR}toxcore/friend_connection.h + ${TOX_DIR}toxcore/friend_requests.c + ${TOX_DIR}toxcore/friend_requests.h + ${TOX_DIR}toxcore/group.c + ${TOX_DIR}toxcore/group.h + ${TOX_DIR}toxcore/group_announce.c + ${TOX_DIR}toxcore/group_announce.h + ${TOX_DIR}toxcore/group_moderation.c + ${TOX_DIR}toxcore/group_moderation.h + ${TOX_DIR}toxcore/group_chats.c + ${TOX_DIR}toxcore/group_chats.h + ${TOX_DIR}toxcore/group_common.h + ${TOX_DIR}toxcore/group_connection.c + ${TOX_DIR}toxcore/group_connection.h + ${TOX_DIR}toxcore/group_onion_announce.c + ${TOX_DIR}toxcore/group_onion_announce.h + ${TOX_DIR}toxcore/group_pack.c + ${TOX_DIR}toxcore/group_pack.h + ${TOX_DIR}toxcore/LAN_discovery.c + ${TOX_DIR}toxcore/LAN_discovery.h + ${TOX_DIR}toxcore/list.c + ${TOX_DIR}toxcore/list.h + ${TOX_DIR}toxcore/logger.c + ${TOX_DIR}toxcore/logger.h + ${TOX_DIR}toxcore/Messenger.c + ${TOX_DIR}toxcore/Messenger.h + ${TOX_DIR}toxcore/mono_time.c + ${TOX_DIR}toxcore/mono_time.h + ${TOX_DIR}toxcore/net_crypto.c + ${TOX_DIR}toxcore/net_crypto.h + ${TOX_DIR}toxcore/network.c + ${TOX_DIR}toxcore/network.h + ${TOX_DIR}toxcore/onion_announce.c + ${TOX_DIR}toxcore/onion_announce.h + ${TOX_DIR}toxcore/onion.c + ${TOX_DIR}toxcore/onion_client.c + ${TOX_DIR}toxcore/onion_client.h + ${TOX_DIR}toxcore/onion.h + ${TOX_DIR}toxcore/ping_array.c + ${TOX_DIR}toxcore/ping_array.h + ${TOX_DIR}toxcore/ping.c + ${TOX_DIR}toxcore/ping.h + ${TOX_DIR}toxcore/shared_key_cache.c + ${TOX_DIR}toxcore/shared_key_cache.h + ${TOX_DIR}toxcore/state.c + ${TOX_DIR}toxcore/state.h + ${TOX_DIR}toxcore/TCP_client.c + ${TOX_DIR}toxcore/TCP_client.h + ${TOX_DIR}toxcore/TCP_common.c + ${TOX_DIR}toxcore/TCP_common.h + ${TOX_DIR}toxcore/TCP_connection.c + ${TOX_DIR}toxcore/TCP_connection.h + ${TOX_DIR}toxcore/TCP_server.c + ${TOX_DIR}toxcore/TCP_server.h + ${TOX_DIR}toxcore/timed_auth.c + ${TOX_DIR}toxcore/timed_auth.h + ${TOX_DIR}toxcore/tox_api.c + ${TOX_DIR}toxcore/tox.c + ${TOX_DIR}toxcore/tox_dispatch.c + ${TOX_DIR}toxcore/tox_dispatch.h + ${TOX_DIR}toxcore/tox_events.c + ${TOX_DIR}toxcore/tox_events.h + ${TOX_DIR}toxcore/tox.h + ${TOX_DIR}toxcore/tox_private.c + ${TOX_DIR}toxcore/tox_private.h + ${TOX_DIR}toxcore/tox_unpack.c + ${TOX_DIR}toxcore/tox_unpack.h + ${TOX_DIR}toxcore/util.c + ${TOX_DIR}toxcore/util.h +) + +# HACK: "install" api headers into self +# this is dirty, should be binary dir +# TODO: add the others +configure_file( + ${TOX_DIR}toxcore/tox.h + ${TOX_DIR}tox/tox.h + @ONLY +) + +target_include_directories(toxcore PRIVATE "${TOX_DIR}toxcore") +target_include_directories(toxcore PUBLIC "${TOX_DIR}") + +target_compile_definitions(toxcore PUBLIC USE_IPV6=1) +target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_DEBUG) + +find_package(unofficial-sodium CONFIG QUIET) +find_package(sodium QUIET) +if(unofficial-sodium_FOUND) # vcpkg + target_link_libraries(toxcore unofficial-sodium::sodium unofficial-sodium::sodium_config_public) +elseif(sodium_FOUND) + target_link_libraries(toxcore sodium) +else() + message(SEND_ERROR "missing libsodium") +endif() + +if(WIN32) + target_link_libraries(toxcore ws2_32 iphlpapi) +endif() + +find_package(Threads REQUIRED) +target_link_libraries(toxcore Threads::Threads) + +add_executable(DHT_Bootstrap EXCLUDE_FROM_ALL + ${TOX_DIR}other/DHT_bootstrap.c + ${TOX_DIR}other/bootstrap_node_packets.h + ${TOX_DIR}other/bootstrap_node_packets.c + ${TOX_DIR}testing/misc_tools.h + ${TOX_DIR}testing/misc_tools.c +) + +target_link_libraries(DHT_Bootstrap toxcore) + diff --git a/vim_research/CMakeLists.txt b/vim_research/CMakeLists.txt index 86b1b8d..f74a589 100644 --- a/vim_research/CMakeLists.txt +++ b/vim_research/CMakeLists.txt @@ -14,3 +14,16 @@ target_link_libraries(vim_research_test1 PUBLIC nlohmann_json::nlohmann_json ) +######################################## + +add_executable(vim_research_test2 + ./test2.cpp +) + +target_link_libraries(vim_research_test2 PUBLIC + crdt_version0 + zed_net + nlohmann_json::nlohmann_json + toxcore +) + diff --git a/vim_research/test2.cpp b/vim_research/test2.cpp new file mode 100644 index 0000000..79ba7fd --- /dev/null +++ b/vim_research/test2.cpp @@ -0,0 +1,554 @@ +#include +#include + +extern "C" { +#include +#include +#include +} + +#include +#include +#include +#include +#include +#include + +#include +#include + +// single letter agent, for testing only +//using Agent = char; +using Agent = uint16_t; // tmp local port +using Doc = GreenCRDT::TextDocument; +using ListType = Doc::ListType; + +std::ostream& operator<<(std::ostream& out, const std::optional& id) { + if (id.has_value()) { + out << id.value().id << "-" << id.value().seq; + } else { + out << "null"; + } + return out; +} + +std::ostream& operator<<(std::ostream& out, const ListType::OpAdd& op) { + out + << "Add{ id:" << op.id.id + << "-" << op.id.seq + << ", v:" << op.value + << ", l:" << op.parent_left + << ", r:" << op.parent_right + << " }" + ; + return out; +} + +std::ostream& operator<<(std::ostream& out, const ListType::OpDel& op) { + out + << "Del{ id:" << op.id.id + << "-" << op.id.seq + << " }" + ; + return out; +} + +std::ostream& operator<<(std::ostream& out, const Doc::Op& op) { + if (std::holds_alternative(op)) { + out << std::get(op); + } else if (std::holds_alternative(op)) { + out << std::get(op); + } + return out; +} + +std::ostream& operator<<(std::ostream& out, const std::optional& id) { + if (id.has_value()) { + out << id.value(); + } else { + out << "null"; + } + return out; +} + +std::ostream& operator<<(std::ostream& out, const ListType::Entry& e) { + out + << "{ id:" << e.id.id + << "-" << e.id.seq + << ", v:" << e.value + << ", l:" << e.parent_left + << ", r:" << e.parent_right + << " }" + ; + return out; +} + +namespace vim { + +static bool sendResponse(zed_net_socket_t* remote_socket, const int64_t id, const nlohmann::json& j) { + auto j_msg = nlohmann::json::array(); + + j_msg.push_back(id); + j_msg.push_back(j); + + std::string str = j_msg.dump(); + str += '\n'; + + auto ret = zed_net_tcp_socket_send(remote_socket, str.data(), str.size()); + return ret == 0; +} + +static bool sendCommand(zed_net_socket_t* remote_socket, const std::string_view mode, const std::string_view command) { + auto j = nlohmann::json::array(); + + j.push_back(mode); + j.push_back(command); + + std::string str = j.dump(); + str += '\n'; + + auto ret = zed_net_tcp_socket_send(remote_socket, str.data(), str.size()); + return ret == 0; +} + +static bool sendSetup(zed_net_socket_t* remote_socket) { + return sendCommand(remote_socket, "ex", + +// vars +R"( +let b:green_crdt_timer_can_send = v:true +let b:green_crdt_timer_can_fetch = v:true +let b:green_crdt_dirty = v:true +)" + +// send +R"( +function! GreenCRDTSendTimerCallback(timer) abort + let b:green_crdt_timer_can_send = v:true + call GreenCRDTCheckTimeAndSend() +endfunction +)" + +// TODO: make send sync? (ch_evalexpr()) +R"( +function! GreenCRDTCheckTimeAndSend() abort + if b:green_crdt_timer_can_send && b:green_crdt_dirty + let b:green_crdt_timer_can_send = v:false + call ch_sendexpr(b:channel, [{'cmd': 'full_buffer', 'lines': getbufline(bufnr(), 1, '$')}]) + let b:green_crdt_dirty = v:false + call timer_start(100, 'GreenCRDTSendTimerCallback') + endif +endfunction +)" + +// fetch +R"( +function! GreenCRDTFetchTimerCallback(timer) abort + let b:green_crdt_timer_can_fetch = v:true + call GreenCRDTCheckTimeAndFetch() +endfunction +)" + +R"( +function! GreenCRDTCheckTimeAndFetch() + if reg_executing() isnot# '' | return | endif + + if b:green_crdt_timer_can_fetch + let b:green_crdt_timer_can_fetch = v:false + + " dont update when inserting or visual (or atleast not in visual) + if mode() is# 'n' + let l:response = ch_evalexpr(b:channel, [{'cmd': 'fetch_changes'}]) + for [line_number, line] in l:response + call setline(line_number, line) + endfor + + endif + + let b:green_crdt_fetch_timer = timer_start(503, 'GreenCRDTFetchTimerCallback') + endif +endfunction +)" + +// change event +R"( +function! GreenCRDTChangeEvent() + let b:green_crdt_dirty = v:true + call GreenCRDTCheckTimeAndSend() + call GreenCRDTCheckTimeAndFetch() +endfunction +)" + +// TODO: pull changes + +// cleanup, to be called by user +// delfunction fails for stop... but well +R"( +function! GreenCRDTStop() + augroup green_crdt + au! + augroup END + + call timer_stop(b:green_crdt_fetch_timer) + + call ch_close(b:channel) + + delfunction GreenCRDTCheckTimeAndSend + delfunction GreenCRDTCheckTimeAndFetch + delfunction GreenCRDTSendTimerCallback + delfunction GreenCRDTFetchTimerCallback + delfunction GreenCRDTChangeEvent + "delfunction GreenCRDTStop + let b:green_crdt_timer_can_send = v:true +endfunction +)" + +// this is a hack, bc for some EX mode IPC buggyness reason, it only works as single commands OR inside a function +R"( +function! GreenCRDTSetupEvents() abort + augroup green_crdt + au! + au TextChanged call GreenCRDTChangeEvent() + au TextChangedI call GreenCRDTChangeEvent() + augroup END +endfunction +call GreenCRDTSetupEvents() +delfunction GreenCRDTSetupEvents +)" + +R"( +let b:green_crdt_fetch_timer = timer_start(900, 'GreenCRDTFetchTimerCallback') + +echo 'setup done' +)"); +} + +} // namespace vim + +struct SharedContext { + std::atomic_bool should_quit {false}; + + // tox ngc id for agent + + // remote op queue for receive + // local op list for remote lookups + + // last seq for all known agents + // bool dirty + + Tox* tox {nullptr}; +}; + +namespace tox { + +static std::vector hex2bin(const std::string& str) { + std::vector bin{}; + bin.resize(str.size()/2, 0); + + sodium_hex2bin(bin.data(), bin.size(), str.c_str(), str.length(), nullptr, nullptr, nullptr); + + return bin; +} + +static std::string bin2hex(const std::vector& bin) { + std::string str{}; + str.resize(bin.size()*2, '?'); + + // HECK, std is 1 larger than size returns ('\0') + sodium_bin2hex(str.data(), str.size()+1, bin.data(), bin.size()); + + return str; +} + +// callbacks +static void log_cb(Tox*, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, const char *message, void *); +static void self_connection_status_cb(Tox*, TOX_CONNECTION connection_status, void *); +//static void friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *); +static void group_custom_packet_cb(Tox* tox, uint32_t group_number, uint32_t peer_id, const uint8_t* data, size_t length, void* user_data); +static void group_custom_private_packet_cb(Tox* tox, uint32_t group_number, uint32_t peer_id, const uint8_t* data, size_t length, void* user_data); + +void toxThread(SharedContext* ctx) { + using namespace std::chrono_literals; + + TOX_ERR_OPTIONS_NEW err_opt_new; + Tox_Options* options = tox_options_new(&err_opt_new); + assert(err_opt_new == TOX_ERR_OPTIONS_NEW::TOX_ERR_OPTIONS_NEW_OK); + tox_options_set_log_callback(options, log_cb); +#ifndef USE_TEST_NETWORK + tox_options_set_local_discovery_enabled(options, true); +#endif + tox_options_set_udp_enabled(options, true); + tox_options_set_hole_punching_enabled(options, true); + tox_options_set_tcp_port(options, 0); + + TOX_ERR_NEW err_new; + ctx->tox = tox_new(options, &err_new); + tox_options_free(options); + if (err_new != TOX_ERR_NEW_OK) { + std::cerr << "tox_new failed with error code " << err_new << "\n"; + ctx->should_quit.store(true); + return; + } + + std::cout << "tox instance created\n"; + +#define CALLBACK_REG(x) tox_callback_##x(ctx->tox, x##_cb) + CALLBACK_REG(self_connection_status); + + CALLBACK_REG(group_custom_packet); + CALLBACK_REG(group_custom_private_packet); +#undef CALLBACK_REG + + // dht + + + // public + // 87F3EBA4C0D27926F9ED77E2FF9D8F26F9869D71311D0DB4CA857C1E25A40B18 - green_crdt_test1 + //tox_group_join() + //tox_group_self_get_public_key() + //tox_group_send_custom_packet() + //tox_group_send_custom_private_packet() + + while (!ctx->should_quit) { + // tox iterate + tox_iterate(ctx->tox, ctx); + + // send stuff + + std::this_thread::sleep_for(20ms); + } + + tox_kill(ctx->tox); +} + +} // namespace tox + +int main(void) { + SharedContext ctx; + std::cout << "starting tox thread\n"; + auto tox_thread = std::thread(tox::toxThread, &ctx); + + 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 uint16_t 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"; + + std::cout << "paste these commands into your vim for the current buffer:\n"; + std::cout << " :let b:channel = ch_open('localhost:" << port << "')\n"; + + std::cout << "paste this command to disconnect:\n :call GreenCRDTStop()\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"; + + std::cout << "sending setup\n"; + vim::sendSetup(&remote_socket); + // send doauto text changed for inital buffer + + Doc doc; + doc.local_agent = remote_address.port; // tmp: use local port as id + + while (true) { + // 100MiB + 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"; + + // expect json array(s separated by newlines) + // TODO: new lines + auto view = std::string_view{reinterpret_cast(buffer->data()), static_cast(bytes_received)}; + std::cout << " raw: " << view; + + auto j = nlohmann::json::parse(view, nullptr, false); + if (j.is_discarded()) { + std::cerr << "invalid json\n"; + //break; + continue; // whatever + } + + //std::cout << " j: " << j.dump() << "\n"; + + if (!j.is_array()) { + std::cerr << "json not array!\n"; + break; + } + + int64_t command_seq = j.at(0); + auto j_command_data = j.at(1); + + if (!j_command_data.is_array()) { + std::cerr << "j_command_data not array!\n"; + break; + } + + for (const auto& j_command : j_command_data) { + if (!j_command.is_object()) { + std::cerr << "j_command not obj!\n"; + break; + } + + if (!j_command.count("cmd")) { + std::cerr << "j_command does not have a 'cmd' field!\n"; + break; + } + + if (!j_command.at("cmd").is_string()) { + std::cerr << "command not a string!\n"; + break; + } + + std::string command = j_command.at("cmd"); + + if (command.empty()) { + std::cerr << "command string empty!\n"; + break; + } else if (command == "setup") { // setup callbacks etc, basically the plugin + std::cout << "sending setup\n"; + vim::sendSetup(&remote_socket); + } else if (command == "fetch_changes") { // setup callbacks etc, basically the plugin + // apply changes (some) and gen vim inserts + std::cout << "got fetch changes\n"; + + auto j_res_line_list = nlohmann::json::array(); + + if (true) { // external changes + const auto crdt_text = doc.getText(); + std::string_view text_view {crdt_text}; + for (int64_t i = 1; ; i++) { + const auto nl_pos = text_view.find_first_of("\n"); + if (nl_pos == std::string_view::npos) { + // no more lines + j_res_line_list.push_back(nlohmann::json::array({i, text_view})); + break; + } else { + const auto line = text_view.substr(0, nl_pos); + j_res_line_list.push_back(nlohmann::json::array({i, line})); + + assert(text_view.size() >= nl_pos+1); + text_view = text_view.substr(nl_pos+1); + } + } + } + + vim::sendResponse(&remote_socket, command_seq, j_res_line_list); + } else if (command == "full_buffer") { // vim is sending the full buffer + // array of lines + + if (!j_command.count("lines")) { + std::cerr << "lines list empty!\n"; + continue; + } + + const auto& j_lines = j_command.at("lines"); + if (!j_lines.is_array()) { + std::cerr << "lines list not an array!\n"; + continue; + } + + std::string new_text; + for (size_t i = 0; i < j_lines.size(); i++) { + if (!j_lines.at(i).empty()) { + new_text += static_cast(j_lines.at(i)); + } + + if (i+1 < j_lines.size()) { + new_text += "\n"; + } + } + + //std::cout << "new_text:\n" << new_text << "\n"; + //std::cout << "old_text:\n" << doc.getText() << "\n"; + std::cout << "doc state: "; + for (const auto& e : doc.state.list) { + std::cout << e << " "; + } + std::cout << "\n"; + + const auto ops = doc.merge(new_text); + if (!ops.empty()) { + std::cout << "ops.size: " << ops.size() << "\n"; + std::cout << "ops: "; + for (const auto& op : ops) { + std::cout << op << " "; + } + std::cout << "\n"; + } + assert(doc.getText() == new_text); + } else { + std::cout << "unknown command '" << command << "'\n"; + } + } + } + + std::cout << "shutting down\n"; + + ctx.should_quit.store(true); + + tox_thread.join(); // wait for thread + + zed_net_socket_close(&remote_socket); + zed_net_socket_close(&listen_socket); + zed_net_shutdown(); + return 0; +} + +namespace tox { + +static void log_cb(Tox*, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, const char *message, void *) { + std::cerr << "TOX " << level << " " << file << ":" << line << "(" << func << ") " << message << "\n"; +} + +static void self_connection_status_cb(Tox*, TOX_CONNECTION connection_status, void *) { + std::cout << "self_connection_status_cb " << connection_status << "\n"; +} + +static void group_custom_packet_cb(Tox* tox, uint32_t group_number, uint32_t peer_id, const uint8_t* data, size_t length, void* user_data) { + std::cout << "group_custom_packet_cb\n"; +} + +static void group_custom_private_packet_cb(Tox* tox, uint32_t group_number, uint32_t peer_id, const uint8_t* data, size_t length, void* user_data) { + std::cout << "group_custom_private_packet_cb\n"; +} + +} +