use core::marker::PhantomData;
use std::{
io,
sync::{Arc, Mutex},
time::Duration,
};
use bincode::error::DecodeError;
use futures::future::BoxFuture;
use tokio::{net::TcpListener, sync::oneshot};
use crate::{
app::{Envelope, Packet, WireframeApp},
codec::{FrameCodec, LengthDelimitedFrameCodec},
preamble::Preamble,
serializer::{BincodeSerializer, Serializer},
};
pub(crate) fn default_worker_count() -> usize {
std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get)
}
pub trait PreambleSuccessHandler<T>:
for<'a> Fn(&'a T, &'a mut tokio::net::TcpStream) -> BoxFuture<'a, io::Result<()>>
+ Send
+ Sync
+ 'static
{
}
impl<T, F> PreambleSuccessHandler<T> for F where
F: for<'a> Fn(&'a T, &'a mut tokio::net::TcpStream) -> BoxFuture<'a, io::Result<()>>
+ Send
+ Sync
+ 'static
{
}
pub type PreambleHandler<T> = Arc<dyn PreambleSuccessHandler<T>>;
pub type PreambleFailure = Arc<
dyn for<'a> Fn(&'a DecodeError, &'a mut tokio::net::TcpStream) -> BoxFuture<'a, io::Result<()>>
+ Send
+ Sync
+ 'static,
>;
pub trait FactoryResult<App> {
type Error: std::error::Error + Send + Sync + 'static;
fn into_result(self) -> Result<App, Self::Error>;
}
impl<Ser, Ctx, E, Codec> FactoryResult<Self> for WireframeApp<Ser, Ctx, E, Codec>
where
Ser: Serializer + Send + Sync,
Ctx: Send + 'static,
E: Packet,
Codec: FrameCodec,
{
type Error = std::convert::Infallible;
fn into_result(self) -> Result<Self, Self::Error> { Ok(self) }
}
impl<Ser, Ctx, E, Codec, Err> FactoryResult<WireframeApp<Ser, Ctx, E, Codec>>
for Result<WireframeApp<Ser, Ctx, E, Codec>, Err>
where
Ser: Serializer + Send + Sync,
Ctx: Send + 'static,
E: Packet,
Codec: FrameCodec,
Err: std::error::Error + Send + Sync + 'static,
{
type Error = Err;
fn into_result(self) -> Result<WireframeApp<Ser, Ctx, E, Codec>, Self::Error> { self }
}
pub trait AppFactory<Ser, Ctx, E, Codec>: Send + Sync + Clone + 'static
where
Ser: Serializer + Send + Sync,
Ctx: Send + 'static,
E: Packet,
Codec: FrameCodec,
{
type Error: std::error::Error + Send + Sync + 'static;
fn build(&self) -> Result<WireframeApp<Ser, Ctx, E, Codec>, Self::Error>;
}
#[doc(hidden)]
pub struct PrebuiltAppFactory<Ser, Ctx, E, Codec>
where
Ser: Serializer + Send + Sync,
Ctx: Send + 'static,
E: Packet,
Codec: FrameCodec,
{
app: PrebuiltAppSlot<Ser, Ctx, E, Codec>,
}
type PrebuiltAppSlot<Ser, Ctx, E, Codec> = Arc<Mutex<Option<WireframeApp<Ser, Ctx, E, Codec>>>>;
impl<Ser, Ctx, E, Codec> Clone for PrebuiltAppFactory<Ser, Ctx, E, Codec>
where
Ser: Serializer + Send + Sync,
Ctx: Send + 'static,
E: Packet,
Codec: FrameCodec,
{
fn clone(&self) -> Self {
Self {
app: Arc::clone(&self.app),
}
}
}
impl<Ser, Ctx, E, Codec> PrebuiltAppFactory<Ser, Ctx, E, Codec>
where
Ser: Serializer + Send + Sync + 'static,
Ctx: Send + 'static,
E: Packet,
Codec: FrameCodec,
{
pub(crate) fn new(app: WireframeApp<Ser, Ctx, E, Codec>) -> Self {
Self {
app: Arc::new(Mutex::new(Some(app))),
}
}
}
impl<Ser, Ctx, E, Codec> AppFactory<Ser, Ctx, E, Codec> for PrebuiltAppFactory<Ser, Ctx, E, Codec>
where
Ser: Serializer + Send + Sync + 'static,
Ctx: Send + 'static,
E: Packet,
Codec: FrameCodec,
{
type Error = io::Error;
fn build(&self) -> Result<WireframeApp<Ser, Ctx, E, Codec>, Self::Error> {
let mut app = self
.app
.lock()
.map_err(|_| io::Error::other("pre-built application factory lock poisoned"))?;
app.take().ok_or_else(|| {
io::Error::other("pre-built application was already consumed by a server run")
})
}
}
impl<F, R, Ser, Ctx, E, Codec> AppFactory<Ser, Ctx, E, Codec> for F
where
F: Fn() -> R + Send + Sync + Clone + 'static,
R: FactoryResult<WireframeApp<Ser, Ctx, E, Codec>>,
Ser: Serializer + Send + Sync,
Ctx: Send + 'static,
E: Packet,
Codec: FrameCodec,
{
type Error = R::Error;
fn build(&self) -> Result<WireframeApp<Ser, Ctx, E, Codec>, Self::Error> {
(self)().into_result()
}
}
pub struct WireframeServer<
F,
T = (),
S = Unbound,
Ser = BincodeSerializer,
Ctx = (),
E = Envelope,
Codec = LengthDelimitedFrameCodec,
> where
F: AppFactory<Ser, Ctx, E, Codec>,
T: Preamble,
S: ServerState,
Ser: Serializer + Send + Sync,
Ctx: Send + 'static,
E: Packet,
Codec: FrameCodec,
{
pub(crate) factory: F,
pub(crate) workers: usize,
pub(crate) on_preamble_success: Option<PreambleHandler<T>>,
pub(crate) on_preamble_failure: Option<PreambleFailure>,
pub(crate) ready_tx: Option<oneshot::Sender<()>>,
pub(crate) backoff_config: BackoffConfig,
pub(crate) preamble_timeout: Option<Duration>,
pub(crate) state: S,
pub(crate) _app: PhantomData<(Ser, Ctx, E, Codec)>,
pub(crate) _preamble: PhantomData<T>,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Unbound;
#[derive(Debug, Clone)]
pub struct Bound {
pub(crate) listener: Arc<TcpListener>,
}
pub trait ServerState: sealed::Sealed {}
mod sealed {
pub trait Sealed {}
impl Sealed for super::Unbound {}
impl Sealed for super::Bound {}
}
impl ServerState for Unbound {}
impl ServerState for Bound {}
mod config;
pub use config::preamble::DEFAULT_PREAMBLE_TIMEOUT;
pub use config::{binding, preamble};
mod connection_spawner;
pub mod error;
pub use error::ServerError;
mod runtime;
mod shutdown;
pub use runtime::BackoffConfig;
pub use shutdown::ServerShutdown;
pub(in crate::server) use shutdown::ServerTerminal;
#[cfg(test)]
pub(crate) mod test_util;