TCP v0.1.0

Source
Listeners that accept connections, and the streams they produce.

TcpListener

pub struct TcpListener {
    // private fields
}

A socket listening for TCP connections. Not copied.

Source

Associated functions

Bind

pub func Bind(address: SocketAddress) -> TcpListener ! IoError

Listens at address. Port zero lets the system choose a free port, which LocalAddress then gives.

NameTypeDescription
addressSocketAddressthe address to listen at, such as 127.0.0.1:8080, or 0.0.0.0:8080 for every address

Returns: the listener, or fails with AddressInUse, AddressUnavailable or PermissionDenied

Source

Methods

Accept

pub func Accept(self: &TcpListener) -> TcpStream ! IoError

Waits for a connection and accepts it.

Returns: the connected stream, whose PeerAddress says who connected

Source

LocalAddress

pub func LocalAddress(self: &TcpListener) -> SocketAddress ! IoError

The address the listener is bound to, with the port the system chose for port zero.

Returns: the address

Source

SetNonBlocking

pub func SetNonBlocking(
    self: &TcpListener,
    enabled: bool
) -> ! IoError

Makes calls on the listener fail with WouldBlock when they would wait, or wait again. A non-blocking socket is meant for a Poller, which says when a call would not wait. Accept then fails with WouldBlock when no connection is waiting; the streams it gives start blocking all the same.

NameTypeDescription
enabledboolwhether calls fail rather than wait

Returns: success when the mode is set

Source

PollSource

pub func PollSource(self: &TcpListener) -> PollSource

The listener as a Poller watches it, valid while the listener is open.

Returns: the source to register

Source

Destructor

~TcpListener

func ~TcpListener(self: &var TcpListener)

Source

TcpStream

pub struct TcpStream {
    // private fields
}

A connected TCP stream: an Io::Reader of what the peer sends and an Io::Writer to it. Not copied.

Source

Associated functions

Connect

pub func Connect(address: SocketAddress) -> TcpStream ! IoError

Connects to address, waiting until the connection is made or refused.

NameTypeDescription
addressSocketAddresswhere to connect

Returns: the stream, or fails with ConnectionRefused, Unreachable or TimedOut

Source

Methods

LocalAddress

pub func LocalAddress(self: &TcpStream) -> SocketAddress ! IoError

The address of this end of the connection.

Returns: the address

Source

PeerAddress

pub func PeerAddress(self: &TcpStream) -> SocketAddress ! IoError

The address of the other end.

Returns: the address

Source

Shutdown

pub func Shutdown(
    self: &TcpStream,
    direction: ShutdownDirection
) -> ! IoError

Ends one or both directions of the connection while the socket stays open.

NameTypeDescription
directionShutdownDirectionwhat to end

Returns: nothing, or fails when the connection is already gone

Source

SetNoDelay

pub func SetNoDelay(
    self: &TcpStream,
    enabled: bool
) -> ! IoError

Sends small writes at once rather than gathering them, which suits request and response exchanges.

NameTypeDescription
enabledboolwhether to

Returns: nothing, or fails when the system refused

Source

SetReadTimeout

pub func SetReadTimeout(
    self: &TcpStream,
    timeout: Duration?
) -> ! IoError

How long a read may wait before failing with TimedOut, or none to wait without a limit.

NameTypeDescription
timeoutDuration?the limit

Returns: nothing, or fails when the system refused

Source

SetWriteTimeout

pub func SetWriteTimeout(
    self: &TcpStream,
    timeout: Duration?
) -> ! IoError

How long a write may wait before failing with TimedOut, or none to wait without a limit.

NameTypeDescription
timeoutDuration?the limit

Returns: nothing, or fails when the system refused

Source

SetNonBlocking

pub func SetNonBlocking(
    self: &TcpStream,
    enabled: bool
) -> ! IoError

Makes calls on the stream fail with WouldBlock when they would wait, or wait again. A non-blocking socket is meant for a Poller, which says when a call would not wait.

NameTypeDescription
enabledboolwhether calls fail rather than wait

Returns: success when the mode is set

Source

PollSource

pub func PollSource(self: &TcpStream) -> PollSource

The stream as a Poller watches it, valid while the stream is open.

Returns: the source to register

Source

Implements Reader

TcpStream conforms to Reader.

Read

pub func Read(
    self: &var TcpStream,
    into: var char8[..]
) -> uint ! IoError

Reads what the peer has sent, waiting while it has sent nothing.

NameTypeDescription
intovar char8[..]where to put the bytes

Returns: how many were read, or fails with EndOfStream once the peer closed its side

Source

Implements Writer

TcpStream conforms to Writer.

Write

pub func Write(
    self: &var TcpStream,
    bytes: char8[..]
) -> uint ! IoError

Sends some of bytes, waiting while the connection's buffer is full.

NameTypeDescription
byteschar8[..]the bytes

Returns: how many were sent

Source

Flush

pub func Flush(self: &var TcpStream) -> ! IoError

Does nothing: a stream sends what is written to it without holding any back.

Returns: nothing

Source

Destructor

~TcpStream

func ~TcpStream(self: &var TcpStream)

Source

ShutdownDirection

pub enum ShutdownDirection: int32 {
    Read = 0,
    Write = 1,
    Both = 2
}

Which directions of a connection Shutdown ends. Copy.

Source

Cases

NameValueDescription
Read0Nothing more will be read.
Write1Nothing more will be written; the peer reads the end of the stream.
Both2Both.