refactoring
This commit is contained in:
parent
ebbc4ff5f9
commit
fb4ed9cc37
1 changed files with 4 additions and 8 deletions
|
@ -7,10 +7,6 @@ use std::time::Duration;
|
|||
|
||||
use super::message_handler::StateMessage;
|
||||
|
||||
trait SocketCallback {
|
||||
fn handle_message(message: String);
|
||||
}
|
||||
|
||||
pub struct Socket{
|
||||
|
||||
}
|
||||
|
@ -21,13 +17,14 @@ impl Socket {
|
|||
TcpListener::bind(address).unwrap()
|
||||
}
|
||||
|
||||
pub fn handle_connections(listener: TcpListener, update_tx: Sender<String>) -> (sync_flag::SyncFlagTx, JoinHandle<()>){
|
||||
pub fn handle_connections(listener: TcpListener, messenger_tx: Sender<String>) -> (sync_flag::SyncFlagTx, JoinHandle<()>){
|
||||
let (tx, thread_stop_flag) = sync_flag::new_syncflag(true);
|
||||
|
||||
let handle = thread::spawn(move || {
|
||||
listener.set_nonblocking(true).unwrap();
|
||||
while thread_stop_flag.get() {
|
||||
for (stream, addr) in listener.accept() {
|
||||
Socket::handle_client(stream, update_tx.clone());
|
||||
Socket::handle_client(stream, messenger_tx.clone());
|
||||
}
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
|
@ -37,18 +34,17 @@ impl Socket {
|
|||
}
|
||||
|
||||
pub fn handle_client(mut stream: TcpStream, update_tx: Sender<String>) {
|
||||
|
||||
let mut buffer = [0; 1024];
|
||||
|
||||
let read_size = stream.read(&mut buffer).unwrap();
|
||||
|
||||
//Tcp is supposed to have a 0 byte read if closed by client
|
||||
if read_size == 0 {
|
||||
stream.shutdown(Shutdown::Both).unwrap();
|
||||
return
|
||||
}
|
||||
|
||||
let output = String::from_utf8_lossy(&buffer[0..read_size]);
|
||||
println!("recieved: {}", output);
|
||||
update_tx.send(output.into_owned()).unwrap();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue