add socket connection events listener

This commit is contained in:
yggverse 2024-10-27 00:32:12 +03:00
parent 2aff5028f5
commit 08ad677ba4
2 changed files with 52 additions and 29 deletions

View File

@ -15,7 +15,10 @@ use meta::{Meta, Status};
use gtk::{ use gtk::{
gdk_pixbuf::Pixbuf, gdk_pixbuf::Pixbuf,
gio::{Cancellable, SimpleAction, SocketClient, SocketProtocol, TlsCertificateFlags}, gio::{
Cancellable, SimpleAction, SocketClient, SocketClientEvent, SocketProtocol,
TlsCertificateFlags,
},
glib::{ glib::{
gformat, uuid_string_random, Bytes, GString, Priority, Regex, RegexCompileFlags, gformat, uuid_string_random, Bytes, GString, Priority, Regex, RegexCompileFlags,
RegexMatchFlags, Uri, UriFlags, RegexMatchFlags, Uri, UriFlags,
@ -315,9 +318,15 @@ impl Page {
// Interpret status to progress fraction // Interpret status to progress fraction
match self.meta.borrow().status { match self.meta.borrow().status {
Some(Status::Reload) => Some(0.0), Some(Status::Reload) => Some(0.0),
Some(Status::Connecting) => Some(0.25), Some(Status::Resolving) => Some(0.1),
Some(Status::Connected) => Some(0.50), Some(Status::Resolved) => Some(0.2),
// Some(Status::Response) => Some(0.75), Some(Status::Connecting) => Some(0.3),
Some(Status::Connected) => Some(0.4),
Some(Status::ProxyNegotiating) => Some(0.5),
Some(Status::ProxyNegotiated) => Some(0.6),
Some(Status::TlsHandshaking) => Some(0.7),
Some(Status::TlsHandshaked) => Some(0.8),
Some(Status::Complete) => Some(0.9),
Some(Status::Failure | Status::Redirect | Status::Success | Status::Input) => Some(1.0), Some(Status::Failure | Status::Redirect | Status::Success | Status::Input) => Some(1.0),
_ => None, _ => None,
} }
@ -364,13 +373,13 @@ impl Page {
}; };
// Init shared objects (async) // Init shared objects (async)
let id = self.id.to_variant();
let navigation = self.navigation.clone();
let content = self.content.clone();
let input = self.input.clone();
let meta = self.meta.clone();
let action_page_open = self.action_page_open.clone(); let action_page_open = self.action_page_open.clone();
let action_update = self.action_update.clone(); let action_update = self.action_update.clone();
let content = self.content.clone();
let id = self.id.to_variant();
let input = self.input.clone();
let meta = self.meta.clone();
let navigation = self.navigation.clone();
let url = uri.clone().to_str(); let url = uri.clone().to_str();
// Init socket // Init socket
@ -380,16 +389,35 @@ impl Page {
client.set_tls_validation_flags(TlsCertificateFlags::INSECURE); client.set_tls_validation_flags(TlsCertificateFlags::INSECURE);
client.set_tls(true); client.set_tls(true);
// Listen for connection status updates
client.connect_event({
let action_update = action_update.clone();
let id = id.clone();
let meta = meta.clone();
move |_, event, _, _| {
meta.borrow_mut().status = Some(match event {
SocketClientEvent::Resolving => Status::Resolving,
SocketClientEvent::Resolved => Status::Resolved,
SocketClientEvent::Connecting => Status::Connecting,
SocketClientEvent::Connected => Status::Connected,
SocketClientEvent::ProxyNegotiating => Status::ProxyNegotiating,
SocketClientEvent::ProxyNegotiated => Status::ProxyNegotiated,
SocketClientEvent::TlsHandshaking => Status::TlsHandshaking,
SocketClientEvent::TlsHandshaked => Status::TlsHandshaked,
SocketClientEvent::Complete => Status::Complete,
_ => todo!(), // notice on API change
});
action_update.activate(Some(&id));
}
});
// Create connection // Create connection
client.connect_to_uri_async( client.clone().connect_to_uri_async(
url.clone().as_str(), url.clone().as_str(),
1965, 1965,
None::<&Cancellable>, None::<&Cancellable>,
move |connect| match connect { move |connect| match connect {
Ok(connection) => { Ok(connection) => {
// Listen for status updates
// @TODO
// Send request // Send request
connection.output_stream().write_bytes_async( connection.output_stream().write_bytes_async(
&Bytes::from(gformat!("{url}\r\n").as_bytes()), &Bytes::from(gformat!("{url}\r\n").as_bytes()),
@ -398,7 +426,7 @@ impl Page {
move |request| match request { move |request| match request {
Ok(_) => { Ok(_) => {
// Read header from response // Read header from response
connection.clone().input_stream().read_bytes_async( connection.input_stream().read_bytes_async(
1024, 1024,
Priority::DEFAULT, Priority::DEFAULT,
None::<&Cancellable>, None::<&Cancellable>,
@ -515,10 +543,8 @@ impl Page {
); );
}, },
Some( Some(
ClientMime::ImagePng | ClientMime::ImagePng | ClientMime::ImageGif |
ClientMime::ImageGif | ClientMime::ImageJpeg | ClientMime::ImageWebp
ClientMime::ImageJpeg |
ClientMime::ImageWebp
) => { ) => {
match Pixbuf::from_stream( match Pixbuf::from_stream(
&connection.input_stream(), &connection.input_stream(),

View File

@ -1,24 +1,21 @@
use gtk::glib::GString; use gtk::glib::GString;
pub enum Status { pub enum Status {
// SensitiveInput, Complete,
// Complete,
Failure, Failure,
Input, Input,
Connecting, Connecting,
Connected, Connected,
// ProxyNegotiated, ProxyNegotiated,
// ProxyNegotiating, ProxyNegotiating,
Redirect, Redirect,
Reload, Reload,
// Request, Resolved,
// Resolved, Resolving,
// Resolving,
// Response,
Success, Success,
// TlsHandshaked, TlsHandshaked,
// TlsHandshaking, TlsHandshaking,
} // @TODO }
pub struct Meta { pub struct Meta {
pub title: Option<GString>, pub title: Option<GString>,