AFAIK there is no solution to this at the moment.

One proposal was to add a `close()` method to `TcpStream` that would close it 
immediately without waiting for it to go out of scope. This seems like the 
simplest solution, if someone wants to implement it.

A better (but much more complicated) solution is to have a Select functionality 
that allows for handling Channels and TcpStreams at the same time. That’s 
something a bunch of us want, but it’s more complicated to implement.

-Kevin

On Mar 7, 2014, at 3:04 PM, Rodrigo Rivas <rodrigorivasco...@gmail.com> wrote:

> Hello!
> 
> I'm writing my first non trivial program in Rust and I'm facing now a
> blocking issue (pun intended): I have to read/write data from/to a
> TcpStream, so I create a task for reading and a task for writing.
> 
> fn do_tcp_stuff(sck : TcpStream) {
>    let mut wtr = ~BufferedWriter::new(sck.clone());
>    let mut rdr = ~BufferedReader::new(sck);
> 
>    let (inport, inchan) = Chan::new();
>    let (outport, outchan) = Chan::new();
> 
>    spawn(proc() { do_tcp_write(wtr, outport); });
>    spawn(proc() { do_tcp_read(rdr, inchan); });
> 
>    loop {
>       // do interesting things, select!() and such
>    }
> 
> }
> 
> fn do_tcp_write(mut wtr : ~Writer, port : Port<~[u8]>) -> IoResult<()> {
>    loop {
>        let data = port.recv();
>        try!(wtr.write(data));
>        wtr.flush();
>    }
>    Ok(())
> }
> 
> fn do_tcp_read(mut rdr : ~Reader, chan : Chan<~[u8]>) -> IoResult<()> {
>    loop {
>        let block = try!(rdr.read_bytes(1024));
>        chan.send(block);
>    }
>    Ok(())
> }
> 
> And all works perfectly... until I want to close the connection and
> kill the tasks:
> 
> - The "do_tcp_write()" function is blocked in "port.recv()", so if I
> close the "outchan" it will finish automatically. Nice!
> - But the "do_tcp_read()" function is blocked in "rdr.read_bytes()" so
> closing "inport" will not affect it, unless it happens to receive some
> data.
> 
> I've read that in older iterations of the library I could use linked
> tasks or something like that. But in master that seems to have
> disappeared. I tried also closing the connection, but I couldn't find
> how.
> 
> Is there any way to do what I want? Or am I doing something fundamentally 
> wrong?
> 
> Thank you in advance for your help!
> -- 
> Rodrigo
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to