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