Skip to content

Commit f99697e

Browse files
committed
Add with_config to Server
Also remove new_nonblocking because with_config makes it irreverent
1 parent cdccd15 commit f99697e

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

lazuli_core/src/client/server.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::{
55

66
use crate::{ArcMutex, Client, Result, Sendable};
77

8+
use super::config::{self, SocketConfig};
9+
810
pub struct Server {
911
listener: TcpListener,
1012
streams: Vec<ArcMutex<Client>>,
@@ -19,14 +21,10 @@ impl Server {
1921
streams: vec![],
2022
})
2123
}
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)
3028
}
3129
/// Accepts a connection.
3230
pub fn accept(&mut self) -> Result<ArcMutex<Client>> {
@@ -116,7 +114,8 @@ mod test {
116114
}
117115
#[test]
118116
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))?;
120119
assert!(server.accept().is_err());
121120
if let Err(e) = server.accept() {
122121
assert_eq!(e.kind(), std::io::ErrorKind::WouldBlock);

0 commit comments

Comments
 (0)