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
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to