diff --git a/CMakeLists.txt b/CMakeLists.txt index 264795d..23bfa78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,13 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.16) project(autocat_gnome) +include(ExternalProject) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -if(APPLE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcoroutines-ts") -endif() +#if(APPLE) +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-coroutines-ts") +#endif() #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -g -Og") @@ -14,21 +15,14 @@ if(APPLE) set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:/usr/local/opt/libsoup@2/lib/pkgconfig:/usr/local/opt/icu4c/lib/pkgconfig") set(OPENSSL_ROOT_DIR /usr/local/opt/openssl) set(OPENSSL_LIBRARIES /usr/local/opt/openssl/lib) - set(FOLLY_DEP folly) - include(FetchContent) - FetchContent_Declare( - folly - GIT_REPOSITORY https://github.com/facebook/folly.git - GIT_TAG v2022.05.16.00 - ) - FetchContent_MakeAvailable(folly) -else() - find_package(folly REQUIRED) - set(FOLLY_DEP Folly::folly) endif() +#list(APPEND CMAKE_MODULE_PATH /usr/local/lib/cmake/concurrencpp-0.1.4) + find_package(PkgConfig REQUIRED) find_package(nlohmann_json REQUIRED) +find_package(concurrencpp REQUIRED) +find_package(Threads REQUIRED) pkg_check_modules(GTK REQUIRED gtk4) pkg_check_modules(GLIB REQUIRED glib-2.0) @@ -36,6 +30,8 @@ pkg_check_modules(LIBSOUP REQUIRED libsoup-2.4) pkg_check_modules(LIBADWAITA REQUIRED libadwaita-1) pkg_check_modules(LIBSIGCPP REQUIRED sigc++-3.0) +#ExternalProject_Add(concurrencpp GIT_REPOSITORY https://github.com/David-Haim/concurrencpp) + include_directories(${GTK_INCLUDE_DIRS} ${GLIB_INCLUDE_DIRS} ${LIBSOUP_INCLUDE_DIRS} @@ -89,7 +85,8 @@ target_link_libraries(autocat_gnome ${GTK_LIBRARIES} ${LIBADWAITA_LIBRARIES} ${LIBSIGCPP_LIBRARIES} nlohmann_json::nlohmann_json - ${FOLLY_DEP}) + concurrencpp::concurrencpp + Threads::Threads) set(XML gui/MainWindow.xml gui/AddNumberDialog.xml) diff --git a/coro/GLibMainContextExecutor.cpp b/coro/GLibMainContextExecutor.cpp index a49605f..b3fd561 100644 --- a/coro/GLibMainContextExecutor.cpp +++ b/coro/GLibMainContextExecutor.cpp @@ -5,6 +5,8 @@ #include "GLibMainContextExecutor.h" #include +/* + int callback(void* data) { auto executor = reinterpret_cast(data); executor->runFront(); @@ -30,3 +32,4 @@ folly::Executor::KeepAlive GLibMainContextExecutor::ins static GLibMainContextExecutor instance; return folly::getKeepAliveToken(instance); } +*/ diff --git a/coro/GLibMainContextExecutor.h b/coro/GLibMainContextExecutor.h index 1530b0f..cd80790 100644 --- a/coro/GLibMainContextExecutor.h +++ b/coro/GLibMainContextExecutor.h @@ -5,20 +5,16 @@ #ifndef AUTOCAT_GNOME_GLIBMAINCONTEXTEXECUTOR_H #define AUTOCAT_GNOME_GLIBMAINCONTEXTEXECUTOR_H -#include #include #include -class GLibMainContextExecutor: public folly::Executor { +class GLibMainContextExecutor { private: - std::queue _tasks; + //std::queue _tasks; std::mutex _mutex; public: - static folly::Executor::KeepAlive instance(); - ~GLibMainContextExecutor() override = default; - void add(folly::Func func) override; - void runFront(); + //~GLibMainContextExecutor() override = default; }; diff --git a/gui/LoginWindow.cpp b/gui/LoginWindow.cpp index 14baa11..d924c3e 100644 --- a/gui/LoginWindow.cpp +++ b/gui/LoginWindow.cpp @@ -12,8 +12,6 @@ #include #include -#include -#include LoginWindow::LoginWindow(std::shared_ptr app): gtkpp::Window(std::move(app), false) { @@ -57,7 +55,7 @@ void LoginWindow::loginClicked() { _spinner.start(); try { - User user = co_await Api::login(email, password).scheduleOn(GLibMainContextExecutor::instance()); + User user = co_await Api::login(email, password); if(auto app = this->application()) { auto mainWindow = std::make_shared(app); mainWindow->show(); diff --git a/main.cpp b/main.cpp index f0c245f..ea2244d 100644 --- a/main.cpp +++ b/main.cpp @@ -5,7 +5,6 @@ #include "gtkpp/Window.h" #include -#include #include std::shared_ptr createMainWindow(std::shared_ptr app) { @@ -19,7 +18,6 @@ std::shared_ptr createMainWindow(std::shared_ptr("pro.aliencat.autocat"); diff --git a/models/User.h b/models/User.h index 4b2504e..8720a76 100644 --- a/models/User.h +++ b/models/User.h @@ -19,6 +19,7 @@ public: public: User() = default; User(const User& user) = default; + User(User&&) = default; User(std::string_view email, std::string_view token); NLOHMANN_DEFINE_TYPE_INTRUSIVE(User, email, token) diff --git a/services/Api.cpp b/services/Api.cpp index 1b06814..8d256eb 100644 --- a/services/Api.cpp +++ b/services/Api.cpp @@ -5,53 +5,39 @@ #include "Api.h" #include "Settings.h" #include -#include - -template -struct Callback { - void(*function)(Args..., void*) = nullptr; - void* state = nullptr; -}; - -template -Callback voidify(Lambda&& l) { - using Func = typename std::decay::type; - auto data = new Func(std::forward(l)); - return { - +[](Args... args, void* v)->void { - Func* f = static_cast< Func* >(v); - (*f)(std::forward(args)...); - delete f; - }, - data - }; -} const std::string Api::_baseUrl = "https://vps.aliencat.pro:8443/"; SoupSession* Api::_session = soup_session_new(); template -folly::Future Api::post(const std::string &method, const nlohmann::json& params) { +void callback(SoupSession* session, SoupMessage* message, gpointer userData) { + auto promise = reinterpret_cast*>(userData); + + if(message->status_code >= 200 && message->status_code < 300) { + auto responseString = std::string(message->response_body->data, message->response_body->length); + auto json = nlohmann::json::parse(responseString); + if(json["success"].get()) { + //std::cout << "response: " << responseString << std::endl; + auto data = json["data"].get(); + promise->set_result(data); + } else { + auto error = json["error"].get(); + promise->set_exception(std::make_exception_ptr(std::runtime_error(error))); + } + } else { + promise->set_exception(std::make_exception_ptr(std::runtime_error(message->reason_phrase))); + } + + delete promise; +} + +template +cc::result Api::post(const std::string &method, const nlohmann::json& params) { std::string url = _baseUrl + method; auto msg = soup_message_new(SOUP_METHOD_POST, url.c_str()); - auto promise = std::make_shared>(); - auto callback = voidify([&, promise](SoupSession* session, SoupMessage* message) { - if(message->status_code >= 200 && message->status_code < 300) { - auto responseString = std::string(message->response_body->data, message->response_body->length); - auto json = nlohmann::json::parse(responseString); - if(json["success"].get()) { - //std::cout << "response: " << responseString << std::endl; - auto user = json["data"].get(); - promise->setValue(user); - } else { - auto error = json["error"].get(); - promise->setException(std::runtime_error(error)); - } - } else { - promise->setException(std::runtime_error(message->reason_phrase)); - } - }); + auto promise = new cc::result_promise(); + auto result = promise->get_result(); auto jsonStr = params.dump(); @@ -60,12 +46,12 @@ folly::Future Api::post(const std::string &method, const nlohmann::json& para jsonStr.c_str(), jsonStr.size()); - soup_session_queue_message(_session, msg, callback.function, callback.state); + soup_session_queue_message(_session, msg, callback, promise); - return promise->getFuture(); + return result; } -fc::Task Api::login(std::string email, std::string password) { +cc::result Api::login(std::string email, std::string password) { nlohmann::json params = { { "email", email }, diff --git a/services/Api.h b/services/Api.h index fc68d13..6071ce0 100644 --- a/services/Api.h +++ b/services/Api.h @@ -8,13 +8,12 @@ #include #include #include -#include -#include +#include #include "../models/User.h" #include "../coro/Coro.h" -namespace fc = folly::coro; +namespace cc = concurrencpp; class Api { private: @@ -23,10 +22,10 @@ private: private: template - static folly::Future post(const std::string& method, const nlohmann::json& params); + static cc::result post(const std::string& method, const nlohmann::json& params); public: - static fc::Task login(std::string email, std::string password); + static cc::result login(std::string email, std::string password); };