5

I'm using Mioco. mio::net::tcp::TcpStream does not implement Clone, so it seems that it's not possible to share a TcpStream across multiple threads/coroutines (or if it is possible, I'm not sure how; I'm pretty new to Rust). I've hence assumed that for simultaneous reading/writing to a single TcpStream, it's necessary to use a single coroutine to do both the reading and writing.

In order to avoid blocking indefinitely on reading when incoming data is infrequent, it seems like it would be necessary to use a timeout when reading from the TcpStream. std::net::TcpStream has set_read_timeout for achieving this, but I can't find an equivalent for mio::net::tcp::TcpStream. How would I go about this? Or is there a way to share the mio TcpStream across multiple coroutines, avoiding the need for a timeout?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
LogicChains
  • 4,332
  • 2
  • 18
  • 27
  • 2
    I haven't used MIO, but my (limited) understanding of async I/O is that your event loop isn't even notified unless there is data to read. Said another way, it should be *impossible* to block while trying to read — you'd just get a partial read instead. I'm sure I'm missing something obvious though! – Shepmaster Dec 04 '15 at 16:06
  • 1
    @Shepmaster As example, imagine I have (in pseudocode): `run_in_new_coroutine(|miocoHandle, tcpStream|{ let stream = miocoHandle.wrap(tcpStream); loop{ stream.read(): let outVal = someMailbox.read(); stream.write(outVal); })` When it reaches `stream.read`, that coroutine will be swapped out, and won't resume until the stream can be read. If this takes 500ms, for instance, then the mailbox won't be read for 500ms, as it will only be read after `stream.read()` is completed. – LogicChains Dec 05 '15 at 04:08
  • That makes sense. I'd encourage you to move that into your question as it helps people like me better understand the problem. – Shepmaster Dec 05 '15 at 16:12

0 Answers0