draft gemini connector, remove sigc, implement destructor

This commit is contained in:
yggverse 2024-08-16 17:56:42 +03:00
parent de312cdcb0
commit a741f1edca
4 changed files with 77 additions and 57 deletions

View File

@ -17,10 +17,10 @@ Page::Page()
// Define group actions // Define group actions
action_group->add_action( action_group->add_action(
"update", "update",
sigc::mem_fun( [this]
* this, {
& Page::update Page::update();
) }
); );
insert_action_group( insert_action_group(
@ -46,12 +46,15 @@ Page::Page()
); );
} }
Page::~Page() = default; Page::~Page()
{
delete navbar;
delete content;
}
// Public actions
void Page::update() void Page::update()
{ {
// Route by request protocol // Route by request scheme
if ("file" == navbar->get_request_scheme()) if ("file" == navbar->get_request_scheme())
{ {
// @TODO // @TODO
@ -59,11 +62,56 @@ void Page::update()
else if ("gemini" == navbar->get_request_scheme()) else if ("gemini" == navbar->get_request_scheme())
{ {
connect( // 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_timeout(
15 // @TODO
);
socket_client->connect_to_host_async(
navbar->get_request_host(), navbar->get_request_host(),
navbar->get_request_port().empty() ? 1965 : stoi( navbar->get_request_port().empty() ? 1965 : stoi(
navbar->get_request_port() navbar->get_request_port()
) ),
[this](const Glib::RefPtr<Gio::AsyncResult> & result)
{
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<Gio::AsyncResult> & result)
{
// Response
socket_connection->get_input_stream()->read_all_async( // | read_async
buffer,
sizeof(buffer) - 1,
[this](const Glib::RefPtr<Gio::AsyncResult> & result)
{
content->set(
buffer
);
socket_connection->close();
}
);
}
);
}
); );
} }
@ -71,43 +119,4 @@ void Page::update()
{ {
// @TODO // @TODO
} }
}
// Private helpers
void Page::connect(
const std::string & host,
int port
) {
try
{
socket_client = Gio::SocketClient::create();
socket_client->connect_to_host_async(
host,
port,
[this](const Glib::RefPtr<Gio::AsyncResult> & result)
{
try
{
auto socket = socket_client->connect_finish(
result
);
// @TODO read/write data
socket->close();
}
catch (const Glib::Error & exception)
{
// @TODO exception.what();
}
}
);
}
catch (const std::exception & exception)
{
// @TODO exception.what();
}
} }

View File

@ -1,14 +1,16 @@
#ifndef APP_BROWSER_MAIN_TAB_PAGE_HPP #ifndef APP_BROWSER_MAIN_TAB_PAGE_HPP
#define APP_BROWSER_MAIN_TAB_PAGE_HPP #define APP_BROWSER_MAIN_TAB_PAGE_HPP
#include <giomm/asyncresult.h>
#include <giomm/inputstream.h>
#include <giomm/outputstream.h>
#include <giomm/simpleactiongroup.h> #include <giomm/simpleactiongroup.h>
#include <giomm/socketclient.h>
#include <giomm/socketconnection.h>
#include <glibmm/refptr.h> #include <glibmm/refptr.h>
#include <gtkmm/box.h> #include <gtkmm/box.h>
#include <sigc++/functors/mem_fun.h>
#include <giomm/asyncresult.h> #include <string>
#include <giomm/socketconnection.h>
#include <giomm/socketclient.h>
namespace app::browser::main::tab namespace app::browser::main::tab
{ {
@ -22,17 +24,15 @@ namespace app::browser::main::tab
{ {
private: private:
char buffer[0xfffff];
Glib::RefPtr<Gio::SimpleActionGroup> action_group; Glib::RefPtr<Gio::SimpleActionGroup> action_group;
Glib::RefPtr<Gio::SocketClient> socket_client; Glib::RefPtr<Gio::SocketClient> socket_client;
Glib::RefPtr<Gio::SocketConnection> socket_connection;
page::Navbar * navbar; page::Navbar * navbar;
page::Content * content; page::Content * content;
void connect(
const std::string & host,
int port
);
public: public:
Page(); Page();

View File

@ -1,6 +1,7 @@
#include "content.hpp" #include "content.hpp"
using namespace app::browser::main::tab::page; using namespace app::browser::main::tab::page;
using namespace std;
Content::Content() Content::Content()
{ {
@ -14,3 +15,9 @@ Content::Content()
} }
Content::~Content() = default; Content::~Content() = default;
void Content::set(
string buffer
) {
// @TODO
}

View File

@ -12,6 +12,10 @@ namespace app::browser::main::tab::page
Content(); Content();
~Content(); ~Content();
void set(
std::string buffer
);
}; };
} }