add shared cancellable holder

This commit is contained in:
yggverse 2024-11-30 05:08:45 +02:00
parent 1dfaf68267
commit c779ca3788
3 changed files with 14 additions and 8 deletions

View file

@ -113,6 +113,7 @@ impl Client {
connection, connection,
certificate, certificate,
Some(network_address), Some(network_address),
cancellable.clone(),
) { ) {
Ok(connection) => { Ok(connection) => {
// Wrap to shared reference support clone semantics // Wrap to shared reference support clone semantics

View file

@ -8,9 +8,10 @@ use gio::{
use glib::object::{Cast, IsA}; use glib::object::{Cast, IsA};
pub struct Connection { pub struct Connection {
pub cancellable: Option<Cancellable>,
pub server_identity: Option<NetworkAddress>,
pub socket_connection: SocketConnection, pub socket_connection: SocketConnection,
pub tls_client_connection: Option<TlsClientConnection>, pub tls_client_connection: Option<TlsClientConnection>,
pub server_identity: Option<NetworkAddress>,
} }
impl Connection { impl Connection {
@ -21,12 +22,14 @@ impl Connection {
socket_connection: SocketConnection, socket_connection: SocketConnection,
certificate: Option<TlsCertificate>, certificate: Option<TlsCertificate>,
server_identity: Option<NetworkAddress>, server_identity: Option<NetworkAddress>,
cancellable: Option<Cancellable>,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
if socket_connection.is_closed() { if socket_connection.is_closed() {
return Err(Error::Closed); return Err(Error::Closed);
} }
Ok(Self { Ok(Self {
cancellable,
server_identity: server_identity.clone(), server_identity: server_identity.clone(),
socket_connection: socket_connection.clone(), socket_connection: socket_connection.clone(),
tls_client_connection: match certificate { tls_client_connection: match certificate {
@ -49,16 +52,16 @@ impl Connection {
/// Close owned [SocketConnection](https://docs.gtk.org/gio/class.SocketConnection.html) /// Close owned [SocketConnection](https://docs.gtk.org/gio/class.SocketConnection.html)
/// and [TlsClientConnection](https://docs.gtk.org/gio/iface.TlsClientConnection.html) if active /// and [TlsClientConnection](https://docs.gtk.org/gio/iface.TlsClientConnection.html) if active
pub fn close(&self, cancellable: Option<&Cancellable>) -> Result<(), Error> { pub fn close(&self) -> Result<(), Error> {
if let Some(ref tls_client_connection) = self.tls_client_connection { if let Some(ref tls_client_connection) = self.tls_client_connection {
if !tls_client_connection.is_closed() { if !tls_client_connection.is_closed() {
if let Err(e) = tls_client_connection.close(cancellable) { if let Err(e) = tls_client_connection.close(self.cancellable.as_ref()) {
return Err(Error::TlsClientConnection(e)); return Err(Error::TlsClientConnection(e));
} }
} }
} }
if !self.socket_connection.is_closed() { if !self.socket_connection.is_closed() {
if let Err(e) = self.socket_connection.close(cancellable) { if let Err(e) = self.socket_connection.close(self.cancellable.as_ref()) {
return Err(Error::SocketConnection(e)); return Err(Error::SocketConnection(e));
} }
} }
@ -69,8 +72,10 @@ impl Connection {
/// * useful for certificate change in runtime /// * useful for certificate change in runtime
/// * support guest and user sessions /// * support guest and user sessions
pub fn rehandshake(&self) -> Result<(), Error> { pub fn rehandshake(&self) -> Result<(), Error> {
match self.tls_client_connection()?.handshake(Cancellable::NONE) { match self
// @TODO shared `Cancellable` .tls_client_connection()?
.handshake(self.cancellable.as_ref())
{
Ok(()) => Ok(()), Ok(()) => Ok(()),
Err(e) => Err(Error::Rehandshake(e)), Err(e) => Err(Error::Rehandshake(e)),
} }

View file

@ -4,7 +4,7 @@ pub use error::Error;
use super::Connection; use super::Connection;
use gio::{ use gio::{
prelude::{TlsCertificateExt, TlsConnectionExt}, prelude::{TlsCertificateExt, TlsConnectionExt},
Cancellable, TlsCertificate, TlsCertificate,
}; };
use glib::Uri; use glib::Uri;
use std::{cell::RefCell, collections::HashMap, rc::Rc}; use std::{cell::RefCell, collections::HashMap, rc::Rc};
@ -69,7 +69,7 @@ impl Session {
} }
// Close connection if active yet // Close connection if active yet
if let Err(e) = connection.close(Cancellable::NONE) { if let Err(e) = connection.close() {
return Err(Error::Connection(e)); return Err(Error::Connection(e));
} }
} }