Lyricdev
GitHub

class TcpStream

pub class TcpStream :: [Reader, Writer, Closeable]

Conforms to Reader, Writer, Closeable.

A TCP connection: a Reader and a Writer whose calls wait for the socket — a read until bytes come, a write until the system takes some.

Methods

pub static fn connect(addr: SocketAddr): TcpStream throws [IoError, Cancelled]

Opens a connection to addr, waiting until it is made or refused.

Errors

Throws IoError with the address when the connection fails, such as ConnectionRefused, and Cancelled when the waiting task is cancelled.

pub mut fn read(into: Slice<uint8>): int throws [IoError, Cancelled]

Reads bytes from the connection into into, waiting until some come, and returns how many; 0 once the peer has ended its writing.

Errors

Throws IoError — Closed after close() —, and Cancelled when the waiting task is cancelled.

pub mut fn write(from: Slice<uint8>): int throws [IoError, Cancelled]

Writes bytes from from into the connection, waiting until the system takes some, and returns how many — possibly fewer than from holds (writeAll writes the rest).

Errors

Throws IoError — Closed after close(), BrokenPipe for a peer that is gone —, and Cancelled when the waiting task is cancelled.

pub fn shutdown(how: Shutdown): void throws IoError

Ends the reading, the writing or both: a peer reading sees the end once the writing is shut.

Errors

Throws IoError: Closed after close(), else the system's failure.

pub fn setNoDelay(on: bool): void throws IoError

Turns Nagle's algorithm off when on — each small write goes out at once — or on again.

Errors

Throws IoError: Closed after close(), else the system's failure.

pub fn peerAddr(): SocketAddr throws IoError

Returns the address of the connection's other end.

Errors

Throws IoError: Closed after close(), else the system's failure.

pub fn localAddr(): SocketAddr throws IoError

Returns the address of this end.

Errors

Throws IoError: Closed after close(), else the system's failure.

pub fn close(): void throws IoError

Closes the connection; a task that waits in a read or a write throws Closed. A second close does nothing.

Errors

Throws IoError when the system fails to close it.