Skip to content

Commit 1af0e99

Browse files
committed
Non-blocking TCP logic, option 2
1 parent 98c3641 commit 1af0e99

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/tcp.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ pub fn start() {
3737

3838
let mut connections = connections.lock().unwrap();
3939

40-
let mut next_connections = vec![];
41-
42-
while let Some(mut stream) = connections.pop() {
40+
connections.retain_mut(|stream| {
4341
let mut buffer = [0; 1024];
4442

4543
let received = stream.read(&mut buffer);
@@ -49,19 +47,20 @@ pub fn start() {
4947
if num_bytes_read == 0 {
5048
let addr = stream.peer_addr().unwrap();
5149
println!("Connection closed by: {}", addr);
52-
} else {
53-
parse_packets(buffer, &mut stream);
54-
next_connections.push(stream);
50+
return false;
5551
}
52+
parse_packets(buffer, stream);
53+
true
5654
}
5755
Err(e) => match e.kind() {
58-
WouldBlock => next_connections.push(stream),
59-
_ => println!("Unexpected error from stream.read(): {:?}", e),
56+
WouldBlock => true,
57+
_ => {
58+
println!("Unexpected error from stream.read(): {:?}", e);
59+
false
60+
}
6061
},
6162
}
62-
}
63-
64-
*connections = next_connections;
63+
});
6564
}
6665
}
6766

0 commit comments

Comments
 (0)