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
use crate::{ConnectEventType, TCPPeer, TCPServer};
use aqueue::Actor;
use std::future::Future;
use std::marker::PhantomData;
use std::sync::Arc;
use tokio::net::tcp::OwnedReadHalf;
use tokio::net::ToSocketAddrs;
pub struct Builder<I, R, A, T> {
input: Option<I>,
connect_event: Option<ConnectEventType>,
addr: A,
_phantom1: PhantomData<R>,
_phantom2: PhantomData<T>,
}
impl<I, R, A, T> Builder<I, R, A, T>
where
I: Fn(OwnedReadHalf, Arc<Actor<TCPPeer>>, T) -> R + Send + Sync + 'static,
R: Future<Output = ()> + Send + 'static,
A: ToSocketAddrs,
T: Clone + Send + 'static,
{
pub fn new(addr: A) -> Builder<I, R, A, T> {
Builder {
input: None,
connect_event: None,
addr,
_phantom1: PhantomData::default(),
_phantom2: PhantomData::default(),
}
}
pub fn set_input_event(mut self, f: I) -> Self {
self.input = Some(f);
self
}
pub fn set_connect_event(mut self, c: ConnectEventType) -> Self {
self.connect_event = Some(c);
self
}
pub async fn build(mut self) -> Arc<Actor<TCPServer<I, R, T>>> {
if let Some(input) = self.input.take() {
return if let Some(connect) = self.connect_event.take() {
TCPServer::new(self.addr, input, Some(connect))
.await
.unwrap()
} else {
TCPServer::new(self.addr, input, None).await.unwrap()
};
}
panic!("input event is no settings,please use set_input_event function set input event.");
}
}