Skip to content

Commit

Permalink
handle Titan requests
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Jan 22, 2025
1 parent caa61bb commit ee0216a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
45 changes: 33 additions & 12 deletions src/client/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use response::Response;
// Local dependencies

use gio::{
prelude::{IOStreamExt, OutputStreamExtManual, TlsConnectionExt},
prelude::{IOStreamExt, OutputStreamExt, OutputStreamExtManual, TlsConnectionExt},
Cancellable, IOStream, NetworkAddress, SocketConnection, TlsCertificate, TlsClientConnection,
};
use glib::{
Expand Down Expand Up @@ -60,23 +60,44 @@ impl Connection {
cancellable: Cancellable,
callback: impl FnOnce(Result<Response, Error>) + 'static,
) {
self.stream().output_stream().write_async(
let output_stream = self.stream().output_stream();
output_stream.clone().write_async(
request.header().into_bytes(),
priority,
Some(&cancellable.clone()),
move |result| match result {
Ok(_) => {
// Read response
Response::from_connection_async(self, priority, cancellable, move |result| {
callback(match result {
Ok(response) => Ok(response),
Err(e) => Err(Error::Response(e)),
Ok(_) => match request {
Request::Gemini(..) => {
Response::from_connection_async(self, priority, cancellable, |result| {
callback(match result {
Ok(response) => Ok(response),
Err(e) => Err(Error::Response(e)),
})
})
})
}
Err((b, e)) => callback(Err(Error::Request((b, e)))),
}
Request::Titan(this) => output_stream.write_bytes_async(
&this.data,
priority,
Some(&cancellable.clone()),
move |result| match result {
Ok(_) => Response::from_connection_async(
self,
priority,
cancellable,
|result| {
callback(match result {
Ok(response) => Ok(response),
Err(e) => Err(Error::Response(e)),
})
},
),
Err(e) => callback(Err(Error::Request(e))),
},
),
},
Err((_, e)) => callback(Err(Error::Request(e))),
},
);
)
}

// Getters
Expand Down
4 changes: 2 additions & 2 deletions src/client/connection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use std::fmt::{Display, Formatter, Result};

#[derive(Debug)]
pub enum Error {
Request((Vec<u8>, glib::Error)),
Request(glib::Error),
Response(crate::client::connection::response::Error),
TlsClientConnection(glib::Error),
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
Self::Request((_, e)) => {
Self::Request(e) => {
write!(f, "Request error: {e}")
}
Self::Response(e) => {
Expand Down

0 comments on commit ee0216a

Please sign in to comment.