High-level interface to libuv's TCP functionality
TcpConnectErrData
- Details returned as part of a Result::Err
result from tcp::connect
TcpListenErrData
- Details returned as part of a Result::Err
result from tcp::listen
TcpErrData
- Contains raw, string-based, error information returned from libuvTcpSocket
- Encapsulates an open TCP/IP connection through libuvTcpSocketBuf
- A buffered wrapper for net::tcp::TcpSocket
of Drop for TcpSocket
for TcpSocket
- Convenience methods extending net::tcp::TcpSocket
of io::Reader for TcpSocketBuf
- Implementation of io::Reader
trait for a buffered net::tcp::TcpSocket
of io::Writer for TcpSocketBuf
- Implementation of io::Reader
trait for a buffered net::tcp::TcpSocket
of ToTcpErr for uv::ll::uv_err_data
TcpSocket
TcpSocketBuf
accept
- Bind an incoming client connection to a net::tcp::TcpSocket
connect
- Initiate a client connection over TCP/IPlisten
- Bind to a given IP/port and listen for new connectionsread
- Reads a single chunk of data from TcpSocket
; block until data/error recv'dread_start
- Begin reading binary data from an open TCP connection; used with read_stop
read_stop
- Stop reading from an open TCP connection; used with read_start
socket_buf
- Convert a net::tcp::TcpSocket
to a net::tcp::TcpSocketBuf
.write
- Write binary data to a tcp stream; Blocks until operation completeswrite_future
- Write binary data to tcp stream; Returns a future::Future
value immediatelynet_tcp::rustrt
TcpConnectErrData
Details returned as part of a Result::Err
result from tcp::connect
GenericConnectErr(~str, ~str)
- Some unplanned-for error. The first and second fields correspond to libuv's err_name
and err_msg
fields, respectively.
ConnectionRefused
- Invalid IP or invalid port
TcpListenErrData
Details returned as part of a Result::Err
result from tcp::listen
GenericListenErr(~str, ~str)
- Some unplanned-for error. The first and second fields correspond to libuv's err_name
and err_msg
fields, respectively.
AddressInUse
- Failed to bind to the requested IP/Port, because it is already in use.
AccessDenied
- Request to bind to an IP/Port was denied by the system.
TcpErrData
pub struct TcpErrData {
err_name: ~str,
err_msg: ~str,
}
Contains raw, string-based, error information returned from libuv
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.
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.
Drop
for TcpSocket
drop
fn drop(&self)
TcpSocket
Convenience methods extending net::tcp::TcpSocket
read_start
fn read_start(&self) ->
result::Result<@Port<result::Result<~[u8], TcpErrData>>, TcpErrData>
read_stop
fn read_stop(&self) -> result::Result<(), TcpErrData>
read
fn read(&self, timeout_msecs: uint) -> result::Result<~[u8], TcpErrData>
read_future
fn read_future(&self, timeout_msecs: uint) ->
future::Future<result::Result<~[u8], TcpErrData>>
write
fn write(&self, raw_write_data: ~[u8]) -> result::Result<(), TcpErrData>
write_future
fn write_future(&self, raw_write_data: ~[u8]) ->
future::Future<result::Result<(), TcpErrData>>
get_peer_addr
fn get_peer_addr(&self) -> ip::IpAddr
io::Reader
for TcpSocketBuf
Implementation of io::Reader
trait for a buffered net::tcp::TcpSocket
read
fn read(&self, buf: &mut [u8], len: uint) -> uint
read_byte
fn read_byte(&self) -> int
eof
fn eof(&self) -> bool
seek
fn seek(&self, dist: int, seek: io::SeekStyle)
tell
fn tell(&self) -> uint
io::Writer
for TcpSocketBuf
Implementation of io::Reader
trait for a buffered net::tcp::TcpSocket
write
fn write(&self, data: &[u8])
seek
fn seek(&self, dist: int, seek: io::SeekStyle)
tell
fn tell(&self) -> uint
flush
fn flush(&self) -> int
get_type
fn get_type(&self) -> io::WriterType
ToTcpErr
for uv::ll::uv_err_data
to_tcp_err
fn to_tcp_err(&self) -> TcpErrData
TcpSocket
fn TcpSocket(socket_data: @TcpSocketData) -> TcpSocket
TcpSocketBuf
fn TcpSocketBuf(data: @mut TcpBufferedSocketData) -> TcpSocketBuf
accept
fn accept(new_conn: TcpNewConnection) -> result::Result<TcpSocket, TcpErrData>
Bind an incoming client connection to a net::tcp::TcpSocket
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.
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 => ()
}
};
new_conn
- an opaque value used to create a new TcpSocket
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
.
connect
fn connect(input_ip: ip::IpAddr, port: uint, iotask: &IoTask) ->
result::Result<TcpSocket, TcpConnectErrData>
Initiate a client connection over TCP/IP
input_ip
- The IP address (versions 4 or 6) of the remote hostport
- the unsigned integer of the desired remote host portiotask
- a uv::iotask
that the tcp request will run onA 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
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
host_ip
- a net::ip::IpAddr
representing a unique IP (versions 4 or 6)port
- a uint representing the port to listen onbacklog
- a uint representing the number of incoming connections to cache in memoryhl_loop
- a uv_iotask::IoTask
that the tcp request will run onon_establish_cb
- a callback that is evaluated if/when the listener is successfully established. it takes no parametersnew_connect_cb
- a callback to be evaluated, on the libuv thread, whenever a client attempts to conect on the provided ip/port. the callback's arguments are:
new_conn
- an opaque type that can be passed to net::tcp::accept
in order to be converted to a TcpSocket
.kill_ch
- channel of type std::comm::Chan<Option<tcp_err_data>>
. this channel can be used to send a message to cause listen
to begin closing the underlying libuv data structures.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
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.
sock
- a net::tcp::TcpSocket
that you wish to read fromtimeout_msecs
- a uint
value, in msecs, to wait before dropping the read attempt. Pass 0u
to wait indefinitelyread_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
net::tcp::TcpSocket
for the connection to read fromResult
instance that will either contain a std::comm::Port<Result<~[u8], TcpErrData>>
that the user can read (and * optionally, loop on) from until read_stop
is called, or a TcpErrData
recordread_stop
fn read_stop(sock: &TcpSocket) -> result::Result<(), TcpErrData>
Stop reading from an open TCP connection; used with read_start
sock
- a net::tcp::TcpSocket
that you wish to stop reading onsocket_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
sock
-- a net::tcp::TcpSocket
that you want to bufferA buffered wrapper that you can cast as an io::Reader
or io::Writer
write
fn write(sock: &TcpSocket, raw_write_data: ~[u8]) ->
result::Result<(), TcpErrData>
Write binary data to a tcp stream; Blocks until operation completes
TcpSocket
to write to~[u8]
that will be written to the stream. This value must remain valid for the duration of the write
callA Result
object with a ()
value as the Ok
variant, or a TcpErrData
value as the Err
variant
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
This function can produce unsafe results if:
write_future
is madefuture::Future
value returned is never resolved via Future::get
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.
TcpSocket
to write to~[u8]
that will be written to the stream. This value must remain valid for the duration of the write
callA 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