7 use crossbeam_channel::{bounded, Receiver, Sender};
10 stdio::{make_io_threads, IoThreads},
14 pub(crate) fn socket_transport(
16 ) -> (Sender<Message>, Receiver<Message>, IoThreads) {
17 let (reader_receiver, reader) = make_reader(stream.try_clone().unwrap());
18 let (writer_sender, writer) = make_write(stream.try_clone().unwrap());
19 let io_threads = make_io_threads(reader, writer);
20 (writer_sender, reader_receiver, io_threads)
23 fn make_reader(stream: TcpStream) -> (Receiver<Message>, thread::JoinHandle<io::Result<()>>) {
24 let (reader_sender, reader_receiver) = bounded::<Message>(0);
25 let reader = thread::spawn(move || {
26 let mut buf_read = BufReader::new(stream);
27 while let Some(msg) = Message::read(&mut buf_read).unwrap() {
28 let is_exit = matches!(&msg, Message::Notification(n) if n.is_exit());
29 reader_sender.send(msg).unwrap();
36 (reader_receiver, reader)
39 fn make_write(mut stream: TcpStream) -> (Sender<Message>, thread::JoinHandle<io::Result<()>>) {
40 let (writer_sender, writer_receiver) = bounded::<Message>(0);
41 let writer = thread::spawn(move || {
42 writer_receiver.into_iter().try_for_each(|it| it.write(&mut stream)).unwrap();
45 (writer_sender, writer)