I am making a simple demo where 3 clients (processes) can connect and communicate with each other with TCP (localhost with different ports).
I am facing the problem of managing several TCP connections. I want to send serialized objects (
enum Message), for example using
I imagine having a
HashMap<SocketAddr, TcpStream> for each client. Each client will need to listen to new messages from each of its
TcpStreams, as well as be able to write to those
TcpStreams at arbitrary times.
So I thought about spawning a thread for each
TcpStream to read objects and push them to some global queue. These threads will have to call blocking functions on the
TcpStream. Either by using
TcpStream::read(). However, I haven’t found any good way of terminating a thread while it is in a blocking call. For the most part, setting some shared signal variable is recommended. So this doesn’t seem possible.
I was told
tokio may be a solution, but that it may not be worth the bother if I just want a small, fast prototype. However, I don’t see any simple solution to this.