From a741f1edcad126687a77d257db28ba276ea00761 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 16 Aug 2024 17:56:42 +0300 Subject: [PATCH] draft gemini connector, remove sigc, implement destructor --- src/app/browser/main/tab/page.cpp | 99 ++++++++++++----------- src/app/browser/main/tab/page.hpp | 18 ++--- src/app/browser/main/tab/page/content.cpp | 7 ++ src/app/browser/main/tab/page/content.hpp | 4 + 4 files changed, 74 insertions(+), 54 deletions(-) diff --git a/src/app/browser/main/tab/page.cpp b/src/app/browser/main/tab/page.cpp index 477edf4b..e53a64b6 100644 --- a/src/app/browser/main/tab/page.cpp +++ b/src/app/browser/main/tab/page.cpp @@ -17,10 +17,10 @@ Page::Page() // Define group actions action_group->add_action( "update", - sigc::mem_fun( - * this, - & Page::update - ) + [this] + { + Page::update(); + } ); insert_action_group( @@ -46,12 +46,15 @@ Page::Page() ); } -Page::~Page() = default; +Page::~Page() +{ + delete navbar; + delete content; +} -// Public actions void Page::update() { - // Route by request protocol + // Route by request scheme if ("file" == navbar->get_request_scheme()) { // @TODO @@ -59,55 +62,61 @@ void Page::update() else if ("gemini" == navbar->get_request_scheme()) { - connect( - navbar->get_request_host(), - navbar->get_request_port().empty() ? 1965 : stoi( - navbar->get_request_port() - ) + // Create new socket connection + socket_client = Gio::SocketClient::create(); + + socket_client->set_tls( + true ); - } - else - { - // @TODO - } -} + socket_client->set_tls_validation_flags( + Gio::TlsCertificateFlags::NO_FLAGS + ); -// Private helpers -void Page::connect( - const std::string & host, - int port -) { - try - { - socket_client = Gio::SocketClient::create(); + socket_client->set_timeout( + 15 // @TODO + ); socket_client->connect_to_host_async( - host, - port, + navbar->get_request_host(), + navbar->get_request_port().empty() ? 1965 : stoi( + navbar->get_request_port() + ), [this](const Glib::RefPtr & result) { - try - { - auto socket = socket_client->connect_finish( - result - ); - - // @TODO read/write data - - socket->close(); - } - - catch (const Glib::Error & exception) - { - // @TODO exception.what(); - } + socket_connection = socket_client->connect_to_host_finish( + result + ); + + // Request + const std::string request = navbar->get_request() + "\r\n"; + + socket_connection->get_output_stream()->write_async( + request.data(), + request.size(), + [this](const Glib::RefPtr & result) + { + // Response + socket_connection->get_input_stream()->read_all_async( // | read_async + buffer, + sizeof(buffer) - 1, + [this](const Glib::RefPtr & result) + { + content->set( + buffer + ); + + socket_connection->close(); + } + ); + } + ); } ); } - catch (const std::exception & exception) + else { - // @TODO exception.what(); + // @TODO } } \ No newline at end of file diff --git a/src/app/browser/main/tab/page.hpp b/src/app/browser/main/tab/page.hpp index 766a7641..a71b59eb 100644 --- a/src/app/browser/main/tab/page.hpp +++ b/src/app/browser/main/tab/page.hpp @@ -1,14 +1,16 @@ #ifndef APP_BROWSER_MAIN_TAB_PAGE_HPP #define APP_BROWSER_MAIN_TAB_PAGE_HPP +#include +#include +#include #include +#include +#include #include #include -#include -#include -#include -#include +#include namespace app::browser::main::tab { @@ -22,17 +24,15 @@ namespace app::browser::main::tab { private: + char buffer[0xfffff]; + Glib::RefPtr action_group; Glib::RefPtr socket_client; + Glib::RefPtr socket_connection; page::Navbar * navbar; page::Content * content; - void connect( - const std::string & host, - int port - ); - public: Page(); diff --git a/src/app/browser/main/tab/page/content.cpp b/src/app/browser/main/tab/page/content.cpp index 1c94bb76..afced092 100644 --- a/src/app/browser/main/tab/page/content.cpp +++ b/src/app/browser/main/tab/page/content.cpp @@ -1,6 +1,7 @@ #include "content.hpp" using namespace app::browser::main::tab::page; +using namespace std; Content::Content() { @@ -14,3 +15,9 @@ Content::Content() } Content::~Content() = default; + +void Content::set( + string buffer +) { + // @TODO +} diff --git a/src/app/browser/main/tab/page/content.hpp b/src/app/browser/main/tab/page/content.hpp index c9d1d6df..798bf653 100644 --- a/src/app/browser/main/tab/page/content.hpp +++ b/src/app/browser/main/tab/page/content.hpp @@ -12,6 +12,10 @@ namespace app::browser::main::tab::page Content(); ~Content(); + + void set( + std::string buffer + ); }; }