High-level interface to libuv's TCP functionality

Type tcp_err_data

type tcp_err_data = {err_name: str, err_msg: str,}

Contains raw, string-based, error information returned from libuv

Enum tcp_connect_err_data

Details returned as part of a result::err result from tcp::connect

Variants

Implementation tcp_socket for tcp_socket

Convenience methods extending net::tcp::tcp_socket

Method read_start

fn read_start() ->
   result::result<comm::port<result::result<~[u8], tcp_err_data>>,
                  tcp_err_data>

Method read_stop

fn read_stop(-read_port: comm::port<result::result<~[u8], tcp_err_data>>) ->
   result::result<(), tcp_err_data>

Method read

fn read(timeout_msecs: uint) -> result::result<~[u8], tcp_err_data>

Method read_future

fn read_future(timeout_msecs: uint) ->
   future::future<result::result<~[u8], tcp_err_data>>

Method write

fn write(raw_write_data: ~[u8]) -> result::result<(), tcp_err_data>

Method write_future

fn write_future(raw_write_data: ~[u8]) ->
   future::future<result::result<(), tcp_err_data>>

Implementation tcp_socket_buf of io::reader for @tcp_socket_buf

Implementation of io::reader iface for a buffered net::tcp::tcp_socket

Method read_bytes

fn read_bytes(amt: uint) -> ~[u8]

Method read_byte

fn read_byte() -> int

Method unread_byte

fn unread_byte(amt: int)

Method eof

fn eof() -> bool

Method seek

fn seek(dist: int, seek: io::seek_style)

Method tell

fn tell() -> uint

Implementation tcp_socket_buf of io::writer for @tcp_socket_buf

Implementation of io::reader iface for a buffered net::tcp::tcp_socket

Method write

fn write(data: & [const u8])

Method seek

fn seek(dist: int, seek: io::seek_style)

Method tell

fn tell() -> uint

Method flush

fn flush() -> int

Function accept

fn accept(new_conn: tcp_new_connection) ->
   result::result<tcp_socket, tcp_err_data>

Bind an incoming client connection to a net::tcp::tcp_socket

Notes

It is safe to call net::tcp::accept only within the context of the new_connect_cb callback provided as the final argument to the net::tcp::listen function.

The new_conn opaque value is provided only as the first argument to the new_connect_cb provided as a part of net::tcp::listen. It can be safely sent to another task but it must be used (via net::tcp::accept) before the new_connect_cb call it was provided to returns.

This implies that a port/chan pair must be used to make sure that the new_connect_cb call blocks until an attempt to create a net::tcp::tcp_socket is completed.

Example

Here, the new_conn is used in conjunction with accept from within a task spawned by the new_connect_cb passed into listen

net::tcp::listen(remote_ip, remote_port, backlog)
    // this callback is ran once after the connection is successfully
    // set up
    {|kill_ch|
      // pass the kill_ch to your main loop or wherever you want
      // to be able to externally kill the server from
    }
    // this callback is ran when a new connection arrives
    {|new_conn, kill_ch|
    let cont_po = comm::port::<option<tcp_err_data>>();
    let cont_ch = comm::chan(cont_po);
    task::spawn {||
        let accept_result = net::tcp::accept(new_conn);
        if accept_result.is_err() {
            comm::send(cont_ch, result::get_err(accept_result));
            // fail?
        }
        else {
            let sock = result::get(accept_result);
            comm::send(cont_ch, true);
            // do work here
        }
    };
    alt comm::recv(cont_po) {
      // shut down listen()
      some(err_data) { comm::send(kill_chan, some(err_data)) }
      // wait for next connection
      none {}
    }
};

Arguments

Returns

On success, this function will return a net::tcp::tcp_socket as the ok variant of a result. The net::tcp::tcp_socket is anchored within the task that accept was called within for its lifetime. On failure, this function will return a net::tcp::tcp_err_data record as the err variant of a result.

Function connect

fn connect(-input_ip: ip::ip_addr, port: uint, iotask: iotask) ->
   result::result<tcp_socket, tcp_connect_err_data>

Initiate a client connection over TCP/IP

Arguments

Returns

A result that, if the operation succeeds, contains a net::net::tcp_socket that can be used to send and receive data to/from the remote host. In the event of failure, a net::tcp::tcp_connect_err_data instance will be returned

Function listen

fn listen(-host_ip: ip::ip_addr, port: uint, backlog: uint, iotask: iotask,
          on_establish_cb: fn~(comm::chan<option<tcp_err_data>>),
          +new_connect_cb:
              fn~(tcp_new_connection, comm::chan<option<tcp_err_data>>)) ->
   result::result<(), tcp_listen_err_data>

Bind to a given IP/port and listen for new connections

Arguments

returns

a result instance containing empty data of type () on a successful/normal shutdown, and a tcp_listen_err_data enum in the event of listen exiting because of an error

Function read_start

fn read_start(sock: tcp_socket) ->
   result::result<comm::port<result::result<~[u8], tcp_err_data>>,
                  tcp_err_data>

Begin reading binary data from an open TCP connection; used with read_stop

Arguments

Returns

Function read_stop

fn read_stop(sock: tcp_socket,
             -read_port: comm::port<result::result<~[u8], tcp_err_data>>) ->
   result::result<(), tcp_err_data>

Stop reading from an open TCP connection; used with read_start

Arguments

Function socket_buf

fn socket_buf(-sock: tcp_socket) -> tcp_socket_buf

Convert a net::tcp::tcp_socket to a net::tcp::tcp_socket_buf.

This function takes ownership of a net::tcp::tcp_socket, returning it stored within a buffered wrapper, which can be converted to a io::reader or io::writer

Arguments

Returns

A buffered wrapper that you can cast as an io::reader or io::writer

Function write

fn write(sock: tcp_socket, raw_write_data: ~[u8]) ->
   result::result<(), tcp_err_data>

Write binary data to a tcp stream; Blocks until operation completes

Arguments

Returns

A result object with a nil value as the ok variant, or a tcp_err_data value as the err variant

Function write_future

fn write_future(sock: tcp_socket, raw_write_data: ~[u8]) ->
   future::future<result::result<(), tcp_err_data>>

Write binary data to tcp stream; Returns a future::future value immediately

Safety

This function can produce unsafe results if:

  1. the call to write_future is made
  2. the future::future value returned is never resolved via future::get
  3. and then the tcp_socket passed in to write_future leaves scope and is destructed before the task that runs the libuv write operation completes.

As such: If using write_future, always be sure to resolve the returned future so as to ensure libuv doesn't try to access a released write handle. Otherwise, use the blocking tcp::write function instead.

Arguments

Returns

A future value that, once the write operation completes, resolves to a result object with a nil value as the ok variant, or a tcp_err_data value as the err variant