1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! Connection lifecycle hook configuration for `WireframeApp`.
use std::{future::Future, sync::Arc};
use super::WireframeApp;
use crate::{
app::{Packet, error::Result},
codec::FrameCodec,
serializer::Serializer,
};
impl<S, C, E, F> WireframeApp<S, C, E, F>
where
S: Serializer + Send + Sync,
C: Send + 'static,
E: Packet,
F: FrameCodec,
{
/// Register a callback invoked when a new connection is established.
///
/// The callback can perform authentication or other setup tasks and
/// returns connection-specific state stored for the connection's
/// lifetime.
///
/// # Type Parameters
///
/// This method changes the connection state type parameter from `C` to `C2`.
/// This means that any subsequent builder methods will operate on the new connection state type
/// `C2`. Be aware of this type transition when chaining builder methods.
///
/// # Errors
///
/// This function always succeeds currently but uses [`Result`] for
/// consistency with other builder methods.
pub fn on_connection_setup<SetupFn, Fut, C2>(
self,
f: SetupFn,
) -> Result<WireframeApp<S, C2, E, F>>
where
SetupFn: Fn() -> Fut + Send + Sync + 'static,
Fut: Future<Output = C2> + Send + 'static,
C2: Send + 'static,
{
Ok(WireframeApp {
handlers: self.handlers,
routes: tokio::sync::OnceCell::new(),
middleware: self.middleware,
serializer: self.serializer,
app_data: self.app_data,
on_connect: Some(Arc::new(move || Box::pin(f()))),
on_disconnect: None,
protocol: self.protocol,
push_dlq: self.push_dlq,
codec: self.codec,
read_timeout_ms: self.read_timeout_ms,
fragmentation: self.fragmentation,
message_assembler: self.message_assembler,
memory_budgets: self.memory_budgets,
})
}
/// Register a callback invoked when a connection is closed.
///
/// The callback receives the connection state produced by
/// [`on_connection_setup`](Self::on_connection_setup).
///
/// # Errors
///
/// This function always succeeds currently but uses [`Result`] for
/// consistency with other builder methods.
pub fn on_connection_teardown<TeardownFn, Fut>(mut self, f: TeardownFn) -> Result<Self>
where
TeardownFn: Fn(C) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
self.on_disconnect = Some(Arc::new(move |c| Box::pin(f(c))));
Ok(self)
}
}