@@ -12,15 +12,21 @@ pub mod flat;
12
12
pub mod json;
13
13
pub mod proto;
14
14
15
- async fn bind < T , E , F > (
15
+ pub struct ServerHandle {
16
+ join_handle : JoinHandle < ( ) > ,
17
+ }
18
+
19
+ pub async fn bind < T , E , F , H > (
20
+ name : & ' static str ,
16
21
options : T ,
17
22
global : Global ,
18
- handle_client : impl Fn ( ( TcpStream , SocketAddr ) , Global ) -> F ,
19
- ) -> Result < ( ) , E >
23
+ handle_client : H ,
24
+ ) -> std :: io :: Result < ServerHandle >
20
25
where
21
- T : ServerConfig ,
26
+ T : ServerConfig + Send + ' static ,
22
27
F : futures:: Future < Output = Result < ( ) , E > > + Send + ' static ,
23
28
E : From < std:: io:: Error > + std:: fmt:: Display + Send + ' static ,
29
+ H : Fn ( ( TcpStream , SocketAddr ) , Global ) -> F + Send + ' static ,
24
30
{
25
31
// Compute binding address
26
32
let address = SocketAddrV4 :: new ( Ipv4Addr :: new ( 0 , 0 , 0 , 0 ) , options. port ( ) ) ;
@@ -31,55 +37,39 @@ where
31
37
// Notify we are listening
32
38
info ! ( "server listening on {}" , address) ;
33
39
34
- loop {
35
- let incoming = listener. accept ( ) . await ?;
36
- tokio:: spawn ( {
37
- let peer_addr = incoming. 1 . clone ( ) ;
38
- let ft = handle_client ( incoming, global. clone ( ) ) ;
40
+ // Spawn accepting loop
41
+ let join_handle = tokio:: spawn ( async move {
42
+ let result: Result < ( ) , _ > = loop {
43
+ match listener. accept ( ) . await {
44
+ Ok ( incoming) => {
45
+ tokio:: spawn ( {
46
+ let peer_addr = incoming. 1 . clone ( ) ;
47
+ let ft = handle_client ( incoming, global. clone ( ) ) ;
39
48
40
- async move {
41
- let result = ft. await ;
49
+ async move {
50
+ let result = ft. await ;
42
51
43
- match result {
44
- Ok ( _) => {
45
- info ! ( "({}) client disconnected" , peer_addr) ;
46
- }
47
- Err ( error) => {
48
- error ! ( "({}) client error:{}" , peer_addr, error) ;
49
- }
52
+ match result {
53
+ Ok ( _) => {
54
+ info ! ( "({}) client disconnected" , peer_addr) ;
55
+ }
56
+ Err ( error) => {
57
+ error ! ( "({}) client error:{}" , peer_addr, error) ;
58
+ }
59
+ }
60
+ }
61
+ } ) ;
50
62
}
63
+ Err ( error) => break Err ( error) ,
51
64
}
52
- } ) ;
53
- }
54
- }
55
-
56
- pub struct ServerHandle {
57
- join_handle : JoinHandle < ( ) > ,
58
- }
59
-
60
- impl ServerHandle {
61
- pub fn spawn < T , E , F , H > (
62
- name : & ' static str ,
63
- options : T ,
64
- global : Global ,
65
- handle_client : H ,
66
- ) -> Self
67
- where
68
- T : ServerConfig + Send + ' static ,
69
- F : futures:: Future < Output = Result < ( ) , E > > + Send + ' static ,
70
- E : From < std:: io:: Error > + std:: fmt:: Display + Send + ' static ,
71
- H : Fn ( ( TcpStream , SocketAddr ) , Global ) -> F + Send + ' static ,
72
- {
73
- Self {
74
- join_handle : tokio:: spawn ( async move {
75
- let result = bind ( options, global, handle_client) . await ;
65
+ } ;
76
66
77
- if let Err ( error) = result {
78
- error ! ( "{} server terminated: {}" , name, error) ;
79
- }
80
- } ) ,
67
+ if let Err ( error) = result {
68
+ error ! ( "{} server terminated: {}" , name, error) ;
81
69
}
82
- }
70
+ } ) ;
71
+
72
+ Ok ( ServerHandle { join_handle } )
83
73
}
84
74
85
75
impl Drop for ServerHandle {
0 commit comments