I have a TCP server that handles file uploads which is responsible for writing the files to the disk and update a metadata file.
For this task I created a thread as soon as the server starts to listening in order to write to the metadata file and for each new incoming request new thread is spawned and sends the file metadata to the other thread.
But the problem here is after first message sent using the tx_pipe
the channel is closed. But how the channel is being dropped if the origin Sender, tx
, didn't went out of scope and tx_pipe
is a clone of tx
?
pub fn listen(&mut self) -> Result<(), std::io::Error>
let listener = TcpListener::bind("0.0.0.0:7878").unwrap();
let (tx, rx) = channel();
let meta_writer = thread::spawn(move || {
let received: Box<Metadata> = rx.recv().unwrap();
println!("Meta: {:#?}", received);
});
for stream in listener.incoming() {
let tx_pipe = tx.clone();
let mut stream = stream.unwrap();
let t = thread::spawn(move ||
let meta = TcpServer::handle_file(stream);
let packet= Box::new(meta);
match tx_pipe.send(packet) {
Ok(x) => println!("All good"),
Err(err) => println!("Erro: {}", err),
}
});
}