Skip to content

Commit

Permalink
Document socket module
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 21, 2023
1 parent fe88c81 commit ec4feff
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
10 changes: 10 additions & 0 deletions lib/internal/socket.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Socket is wrapper around a low-level socket (for example a TcpStream from the standard library) that allows for sending a response from other threads.
use std::{
io::{self, Read, Write},
net::{Shutdown, SocketAddr, TcpStream},
Expand All @@ -15,12 +17,20 @@ use crate::{
trace,
};

/// A [`Stream`] that also implements [`Send`] and [`Sync`].
pub type SocketStream = Box<dyn Stream + Send + Sync>;

/// A trait that represents a socket.
/// This is used to allow for custom socket implementations to be used, for example, to add support for TLS.
pub trait Stream: Read + Write {
/// Get the peer address of the socket.
fn peer_addr(&self) -> io::Result<SocketAddr>;
/// Clone the socket.
/// This is currently only used for websockets.
fn try_clone(&self) -> io::Result<SocketStream>;
/// Shutdown the socket.
fn shutdown(&self, shutdown: Shutdown) -> io::Result<()>;
/// Set the timeout of the socket, for both reading and writing.
fn set_timeout(&self, duration: Option<Duration>) -> io::Result<()>;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod consts {
}

pub mod internal;
use internal::{encoding, handle, router};
use internal::{encoding, router};

#[macro_use]
pub mod trace;
Expand Down
5 changes: 1 addition & 4 deletions lib/proto/http/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ impl Query {
continue;
};

data.push(QueryParameter {
key: key.into(),
value: value.into(),
});
data.push(QueryParameter { key, value });
}

Query { inner: data }
Expand Down
12 changes: 12 additions & 0 deletions lib/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct Server<State: 'static + Send + Sync = ()> {
/// Ip address to listen on.
pub ip: IpAddr,

/// The event loop used to handle incoming connections.
pub event_loop: Box<dyn EventLoop<State>>,

/// Routes to handle.
Expand Down Expand Up @@ -173,6 +174,17 @@ impl<State: Send + Sync> Server<State> {
self
}

/// Change the server's event loop.
/// The default is [`TcpEventLoop`], which uses the standard library's built-in TCP listener.
///
/// The [afire_tls](https://github.com/Basicprogrammer10/afire_tls) crate contains an event loop that uses rustls to handle TLS connections.
pub fn event_loop(self, event_loop: impl EventLoop<State> + 'static) -> Self {
Server {
event_loop: Box::new(event_loop),
..self
}
}

/// Add a new default header to the server.
/// This will be added to every response if it is not already present.
///
Expand Down

0 comments on commit ec4feff

Please sign in to comment.