TCP v0.1.0
TcpListener
pub struct TcpListener {
// private fields
}
A socket listening for TCP connections. Not copied.
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.
| Name | Type | Description |
|---|---|---|
address | SocketAddress | the 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
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
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
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.
| Name | Type | Description |
|---|---|---|
enabled | bool | whether calls fail rather than wait |
Returns: success when the mode is set
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
Destructor
~TcpListener
func ~TcpListener(self: &var TcpListener)
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.
Associated functions
Connect
pub func Connect(address: SocketAddress) -> TcpStream ! IoError
Connects to address, waiting until the connection is made or refused.
| Name | Type | Description |
|---|---|---|
address | SocketAddress | where to connect |
Returns: the stream, or fails with ConnectionRefused, Unreachable or TimedOut
Methods
LocalAddress
pub func LocalAddress(self: &TcpStream) -> SocketAddress ! IoError
The address of this end of the connection.
Returns: the address
PeerAddress
pub func PeerAddress(self: &TcpStream) -> SocketAddress ! IoError
The address of the other end.
Returns: the address
Shutdown
pub func Shutdown(
self: &TcpStream,
direction: ShutdownDirection
) -> ! IoError
Ends one or both directions of the connection while the socket stays open.
| Name | Type | Description |
|---|---|---|
direction | ShutdownDirection | what to end |
Returns: nothing, or fails when the connection is already gone
SetNoDelay
pub func SetNoDelay(
self: &TcpStream,
enabled: bool
) -> ! IoError
Sends small writes at once rather than gathering them, which suits request and response exchanges.
| Name | Type | Description |
|---|---|---|
enabled | bool | whether to |
Returns: nothing, or fails when the system refused
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.
| Name | Type | Description |
|---|---|---|
timeout | Duration? | the limit |
Returns: nothing, or fails when the system refused
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.
| Name | Type | Description |
|---|---|---|
timeout | Duration? | the limit |
Returns: nothing, or fails when the system refused
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.
| Name | Type | Description |
|---|---|---|
enabled | bool | whether calls fail rather than wait |
Returns: success when the mode is set
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
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.
| Name | Type | Description |
|---|---|---|
into | var char8[..] | where to put the bytes |
Returns: how many were read, or fails with EndOfStream once the peer closed its side
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.
| Name | Type | Description |
|---|---|---|
bytes | char8[..] | the bytes |
Returns: how many were sent
Flush
pub func Flush(self: &var TcpStream) -> ! IoError
Does nothing: a stream sends what is written to it without holding any back.
Returns: nothing
Destructor
~TcpStream
func ~TcpStream(self: &var TcpStream)
ShutdownDirection
pub enum ShutdownDirection: int32 {
Read = 0,
Write = 1,
Both = 2
}
Which directions of a connection Shutdown ends. Copy.
Cases
| Name | Value | Description |
|---|---|---|
Read | 0 | Nothing more will be read. |
Write | 1 | Nothing more will be written; the peer reads the end of the stream. |
Both | 2 | Both. |