High-level interface to libuv's TCP functionality

Enum TcpConnectErrData

Details returned as part of a Result::Err result from tcp::connect

Variants

Enum TcpListenErrData

Details returned as part of a Result::Err result from tcp::listen

Variants

Struct TcpErrData

pub struct TcpErrData {
    err_name: ~str,
    err_msg: ~str,
}

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

Struct TcpSocket

pub struct TcpSocket {
    socket_data: @TcpSocketData,
}

Encapsulates an open TCP/IP connection through libuv

TcpSocket is non-copyable/sendable and automagically handles closing the underlying libuv data structures when it goes out of scope. This is the data structure that is used for read/write operations over a TCP stream.

Struct TcpSocketBuf

pub struct TcpSocketBuf {
    data: @mut TcpBufferedSocketData,
    end_of_stream: @mut bool,
}

A buffered wrapper for net::tcp::TcpSocket

It is created with a call to net::tcp::socket_buf() and has impls that satisfy both the io::Reader and io::Writer traits.

Implementation of Drop for TcpSocket

Method drop

fn drop(&self)

Implementation for TcpSocket

Convenience methods extending net::tcp::TcpSocket

Method read_start

fn read_start(&self) ->
 result::Result<@Port<result::Result<~[u8], TcpErrData>>, TcpErrData>

Method read_stop

fn read_stop(&self) -> result::Result<(), TcpErrData>

Method read

fn read(&self, timeout_msecs: uint) -> result::Result<~[u8], TcpErrData>

Method read_future

fn read_future(&self, timeout_msecs: uint) ->
 future::Future<result::Result<~[u8], TcpErrData>>

Method write

fn write(&self, raw_write_data: ~[u8]) -> result::Result<(), TcpErrData>

Method write_future

fn write_future(&self, raw_write_data: ~[u8]) ->
 future::Future<result::Result<(), TcpErrData>>

Method get_peer_addr

fn get_peer_addr(&self) -> ip::IpAddr

Implementation of io::Reader for TcpSocketBuf

Implementation of io::Reader trait for a buffered net::tcp::TcpSocket

Method read

fn read(&self, buf: &mut [u8], len: uint) -> uint

Method read_byte

fn read_byte(&self) -> int

Method eof

fn eof(&self) -> bool

Method seek

fn seek(&self, dist: int, seek: io::SeekStyle)

Method tell

fn tell(&self) -> uint

Implementation of io::Writer for TcpSocketBuf

Implementation of io::Reader trait for a buffered net::tcp::TcpSocket

Method write

fn write(&self, data: &[u8])

Method seek

fn seek(&self, dist: int, seek: io::SeekStyle)

Method tell

fn tell(&self) -> uint

Method flush

fn flush(&self) -> int

Method get_type

fn get_type(&self) -> io::WriterType

Implementation of ToTcpErr for uv::ll::uv_err_data

Method to_tcp_err

fn to_tcp_err(&self) -> TcpErrData

Function TcpSocket

fn TcpSocket(socket_data: @TcpSocketData) -> TcpSocket

Function TcpSocketBuf

fn TcpSocketBuf(data: @mut TcpBufferedSocketData) -> TcpSocketBuf

Function accept

fn accept(new_conn: TcpNewConnection) -> result::Result<TcpSocket, TcpErrData>

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

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::TcpSocket 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

do net::tcp::listen(remote_ip, remote_port, backlog, iotask,
    // 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, cont_ch) = comm::stream::<option::Option<TcpErrData>>();
    do task::spawn {
        let accept_result = net::tcp::accept(new_conn);
        match accept_result {
            Err(accept_error) => {
                cont_ch.send(Some(accept_error));
                // fail?
            },
            Ok(sock) => {
                cont_ch.send(None);
                // do work here
            }
        }
    };
    match cont_po.recv() {
      // shut down listen()
      Some(err_data) => kill_ch.send(Some(err_data)),
      // wait for next connection
      None => ()
    }
};

Arguments

Returns

On success, this function will return a net::tcp::TcpSocket as the Ok variant of a Result. The net::tcp::TcpSocket is anchored within the task that accept was called within for its lifetime. On failure, this function will return a net::tcp::TcpErrData record as the Err variant of a Result.

Function connect

fn connect(input_ip: ip::IpAddr, port: uint, iotask: &IoTask) ->
 result::Result<TcpSocket, TcpConnectErrData>

Initiate a client connection over TCP/IP

Arguments

Returns

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

Function listen

fn listen(host_ip: ip::IpAddr, port: uint, backlog: uint, iotask: &IoTask,
          on_establish_cb: ~fn(SharedChan<Option<TcpErrData>>),
          new_connect_cb:
              ~fn(TcpNewConnection, SharedChan<Option<TcpErrData>>)) ->
 result::Result<(), TcpListenErrData>

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 TcpListenErrData enum in the event of listen exiting because of an error

Function read

fn read(sock: &TcpSocket, timeout_msecs: uint) ->
 result::Result<~[u8], TcpErrData>

Reads a single chunk of data from TcpSocket; block until data/error recv'd

Does a blocking read operation for a single chunk of data from a TcpSocket until a data arrives or an error is received. The provided timeout_msecs value is used to raise an error if the timeout period passes without any data received.

Arguments

Function read_start

fn read_start(sock: &TcpSocket) ->
 result::Result<@Port<result::Result<~[u8], TcpErrData>>, TcpErrData>

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

Arguments

Returns

Function read_stop

fn read_stop(sock: &TcpSocket) -> result::Result<(), TcpErrData>

Stop reading from an open TCP connection; used with read_start

Arguments

Function socket_buf

fn socket_buf(sock: TcpSocket) -> TcpSocketBuf

Convert a net::tcp::TcpSocket to a net::tcp::TcpSocketBuf.

This function takes ownership of a net::tcp::TcpSocket, 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: &TcpSocket, raw_write_data: ~[u8]) ->
 result::Result<(), TcpErrData>

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

Arguments

Returns

A Result object with a () value as the Ok variant, or a TcpErrData value as the Err variant

Function write_future

fn write_future(sock: &TcpSocket, raw_write_data: ~[u8]) ->
 future::Future<result::Result<(), TcpErrData>>

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 TcpSocket 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 TcpErrData value as the Err variant