diff --git a/src/client/connection.rs b/src/client/connection.rs index b322d27..0ea8608 100644 --- a/src/client/connection.rs +++ b/src/client/connection.rs @@ -2,7 +2,7 @@ pub mod error; pub use error::Error; use gio::{ - prelude::{IOStreamExt, TlsConnectionExt}, + prelude::{CancellableExt, IOStreamExt, TlsConnectionExt}, Cancellable, IOStream, NetworkAddress, SocketConnection, TlsCertificate, TlsClientConnection, }; use glib::object::{Cast, IsA}; @@ -50,6 +50,15 @@ impl Connection { // Actions + /// Apply `cancel` action to `Self` [Cancellable](https://docs.gtk.org/gio/method.Cancellable.cancel.html) + /// * return `Error` on `Cancellable` not found + pub fn cancel(&self) -> Result<(), Error> { + match self.cancellable { + Some(ref cancellable) => Ok(cancellable.cancel()), + None => Err(Error::Cancel), + } + } + /// Close owned [SocketConnection](https://docs.gtk.org/gio/class.SocketConnection.html) pub fn close(&self) -> Result<(), Error> { match self.socket_connection.close(self.cancellable.as_ref()) { diff --git a/src/client/connection/error.rs b/src/client/connection/error.rs index 87131f7..45cdd87 100644 --- a/src/client/connection/error.rs +++ b/src/client/connection/error.rs @@ -2,6 +2,7 @@ use std::fmt::{Display, Formatter, Result}; #[derive(Debug)] pub enum Error { + Cancel, Closed, Rehandshake(glib::Error), SocketConnection(glib::Error), @@ -11,6 +12,7 @@ pub enum Error { impl Display for Error { fn fmt(&self, f: &mut Formatter) -> Result { match self { + Self::Cancel => write!(f, "Cancellable not found"), Self::Closed => write!(f, "Connection closed"), Self::Rehandshake(e) => { write!(f, "Rehandshake error: {e}")