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,
certificate,
Some(network_address),
cancellable.clone(),
) {
Ok(connection) => {
// Wrap to shared reference support clone semantics

View file

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

View file

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