From 81c9ae2fc8b8f2a2d8cac1867f747796ad51a43d Mon Sep 17 00:00:00 2001 From: yggverse Date: Mon, 16 Sep 2024 10:45:15 +0300 Subject: [PATCH] implement multiprotocol socket api --- src/app/browser/main/tab/page.cpp | 83 +++++++++++++++++++++++-------- src/app/browser/main/tab/page.hpp | 37 +++++++++++++- 2 files changed, 97 insertions(+), 23 deletions(-) diff --git a/src/app/browser/main/tab/page.cpp b/src/app/browser/main/tab/page.cpp index 1fbb59d1..6094cc40 100644 --- a/src/app/browser/main/tab/page.cpp +++ b/src/app/browser/main/tab/page.cpp @@ -186,10 +186,9 @@ void Page::navigation_reload( const bool & ADD_HISTORY ) { // Close previous socket connection (on active) - if (socket__connection != nullptr && socket__connection->is_connected()) - { - socket__connection->close(); - } + Socket::Connection::close( + socket__connection + ); // Update navigation history? if (ADD_HISTORY) @@ -278,23 +277,7 @@ void Page::navigation_reload( if (g_uri_get_scheme(uri) == Glib::ustring("gemini")) { // Create new socket connection - socket__client = Gio::SocketClient::create(); - - socket__client->set_tls( - true - ); - - socket__client->set_tls_validation_flags( - Gio::TlsCertificateFlags::NO_FLAGS - ); - - socket__client->set_protocol( - Gio::Socket::Protocol::TCP - ); - - socket__client->set_timeout( - 15 // @TODO - ); + socket__client = Page::Socket::Client::Gemini::create(); socket__client->connect_to_uri_async( g_uri_to_string( @@ -455,7 +438,9 @@ void Page::navigation_reload( action__update->activate(); } - socket__connection->close(); + Socket::Connection::close( + socket__connection + ); } ); // read_all_async } @@ -641,4 +626,58 @@ sqlite3_int64 Page::DB::SESSION::add( return sqlite3_last_insert_rowid( db ); +} + +// Socket tools +Glib::RefPtr Page::Socket::Client::create( + const int & TIMEOUT +) { + const auto CLIENT = Gio::SocketClient::create(); + + CLIENT->set_timeout( + TIMEOUT + ); + + return CLIENT; +} + +Glib::RefPtr Page::Socket::Client::Gemini::create() +{ + const auto GEMINI_CLIENT = Page::Socket::Client::create(); + + GEMINI_CLIENT->set_tls( + true + ); + + GEMINI_CLIENT->set_tls_validation_flags( + Gio::TlsCertificateFlags::NO_FLAGS + ); + + GEMINI_CLIENT->set_protocol( + Gio::Socket::Protocol::TCP + ); + + return GEMINI_CLIENT; +} + +bool Page::Socket::Connection::is_active( + const Glib::RefPtr & CONNECTION +) { + return CONNECTION != nullptr && CONNECTION->is_connected(); +} + +bool Page::Socket::Connection::close( + Glib::RefPtr & connection +) { + if (Socket::Connection::is_active(connection)) + { + if (connection->close()) + { + connection = nullptr; + + return true; + } + } + + return false; } \ 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 f4cf5b0b..02c8f456 100644 --- a/src/app/browser/main/tab/page.hpp +++ b/src/app/browser/main/tab/page.hpp @@ -39,7 +39,7 @@ namespace app::browser::main::tab }; /* - * Class database + * Page class database * * Allowed parental access to enums and relationship methods */ @@ -77,6 +77,41 @@ namespace app::browser::main::tab }; }; + /* + * Socket helpers + * + * Page works with multiple protocols and requires some extended features below + */ + struct Socket + { + class Client + { + private: + + static Glib::RefPtr create( + const int & TIMEOUT = 15 + ); + + public: + + struct Gemini + { + static Glib::RefPtr create(); + }; + }; + + struct Connection + { + static bool is_active( + const Glib::RefPtr & CONNECTION + ); + + static bool close( + Glib::RefPtr & connection + ); + }; + }; + /* * Internal members */