@@ -5,6 +5,8 @@ use std::{
5
5
6
6
use crate :: { ArcMutex , Client , Result , Sendable } ;
7
7
8
+ use super :: config:: { self , SocketConfig } ;
9
+
8
10
pub struct Server {
9
11
listener : TcpListener ,
10
12
streams : Vec < ArcMutex < Client > > ,
@@ -19,14 +21,10 @@ impl Server {
19
21
streams : vec ! [ ] ,
20
22
} )
21
23
}
22
- /// Creates a new non-blocking server.
23
- pub fn new_nonblocking < T : ToSocketAddrs > ( addrs : T ) -> Result < Self > {
24
- let listener = TcpListener :: bind ( addrs) ?;
25
- listener. set_nonblocking ( true ) ?;
26
- Ok ( Server {
27
- listener,
28
- streams : vec ! [ ] ,
29
- } )
24
+ /// Adds a configuration to the server.
25
+ pub fn with_config ( self , config : SocketConfig ) -> Result < Self > {
26
+ config. apply_listener ( & self . listener ) ?;
27
+ Ok ( self )
30
28
}
31
29
/// Accepts a connection.
32
30
pub fn accept ( & mut self ) -> Result < ArcMutex < Client > > {
@@ -116,7 +114,8 @@ mod test {
116
114
}
117
115
#[ test]
118
116
fn test_nonblocking_server ( ) -> Result < ( ) > {
119
- let mut server = Server :: new_nonblocking ( ( Ipv4Addr :: LOCALHOST , 0 ) ) ?;
117
+ let mut server = Server :: new ( ( Ipv4Addr :: LOCALHOST , 0 ) ) ?
118
+ . with_config ( SocketConfig :: new ( ) . blocking ( false ) ) ?;
120
119
assert ! ( server. accept( ) . is_err( ) ) ;
121
120
if let Err ( e) = server. accept ( ) {
122
121
assert_eq ! ( e. kind( ) , std:: io:: ErrorKind :: WouldBlock ) ;
0 commit comments