Net.TCP

lib/Net/TCP.gob

TCP connections: a Socket to read and write, and a Listener that accepts them.

let d = Net.resolve("example.com", 80, Net.publicInternet, Duration.seconds(10))?
let s = TCP.connect(d, Duration.seconds(10))?
writeFrom(s, request)?

Waiting parks the task. A read, a write, an accept or a connect that has to wait suspends the task on the poller, and its worker runs other tasks meanwhile; nothing here holds a worker while the network is slow.

Connecting. connect takes a Net.Destination, so it reaches only addresses a policy allowed (Net.resolve). It tries them in order under one deadline. An address that refuses the connection, or that no route reaches, moves it on to the next; the last one's failure is the answer if none connects. A deadline that passes first is TimedOut, whichever address it was trying.

A Socket is a Read and a Write, and its errors are IOErrors (Net.NetError). A read answers 0 at the end of input, which is where the peer's shutdownWrite or close lands. A write sends all of its buffer or fails; the bytes go out as soon as they are written, since Nagle's algorithm is off (TCP_NODELAY), so a caller that wants a header and a body in one segment writes them in one buffer. A write to a connection the peer has closed fails with BrokenPipe or ConnectionReset, and never ends the process with SIGPIPE.

Sharing. Any number of tasks may hold one Socket. Reads take turns with reads, and writes with writes, so one write's bytes are never split by another's; a read and a write run at once.

A deadline set with setDeadline bounds every read and write on the socket from then on, and one that passes is TimedOut. A write that timed out may have sent part of its buffer, so a protocol that frames its messages has to close the connection after one.

Closing is idempotent, and ends every read, write and accept parked on the socket or listener with Closed, from whichever task closes it. That is how a server stops accepting: another task closes its listener. The descriptor number is released only when the last operation using it has returned, so a closed socket is never read through a number reused for another file.

Every socket is close-on-exec, so a program started later does not inherit it.

type Socket

A connection: its descriptor, the point every read and write on it gives up at (on Turkey.Wait.now's clock, or -1), the nanoseconds each may wait from its first wait (or -1), and what its errors name: the host and port it was connected to, or the peer it was accepted from.

type Listener

A listening socket, and the address and port it is bound to.

fun connect : [Timeout t] fun(Destination, t) -> Either IOError Socket

A connection to one of d's addresses, tried in order, all within timeout.

fun listen : fun(IP, Int) -> Either IOError Net.TCP.Listener

A listener on address and port, which takes connections as soon as this returns; port 0 asks the system for a free one, which listenerAddress tells. SO_REUSEADDR is set, so a server restarted at once can bind a port its old connections still hold.

fun listenerAddress : fun(Net.TCP.Listener) -> (IP, Int)

The address and port l is bound to.

fun localAddress : fun(Socket) -> Either IOError (IP, Int)

The address and port s is bound to on this machine.

fun peerAddress : fun(Socket) -> Either IOError (IP, Int)

The address and port of s's peer.

instance Read Socket
instance Write Socket
instance Listener Net.TCP.Listener

The next connection to l, waiting for one to arrive: Closed if l is closed, before or during the wait. Tasks that call it at once take turns, and each connection goes to one of them. Closing l ends the waits.

instance Connection Socket

shutdownWrite waits for a write in progress to finish, so it never lands inside one. A deadline bounds every read and write from when it is set. setLimits' each starts at an operation's first wait, so a read or write that does not wait reads no clock for it.

instance HasConnectionInfo Socket

The addresses of both ends, None for one the system would not report.

accept : [Listener l] fun(l) -> Either IOError (Accepted l) -- from Net.accept
clearDeadline : [Connection c] fun(c) -> Unit -- from Net.clearDeadline
close : [Connection c] fun(c) -> Either (WriteError c) Unit -- from Net.close
closeListener : [Listener l] fun(l) -> Either IOError Unit -- from Net.closeListener
setDeadline : [Connection c, Timeout t] fun(c, t) -> Unit -- from Net.setDeadline
setLimits : [Connection c] fun(c, Option Deadline, Duration) -> Unit -- from Net.setLimits
shutdownWrite : [Connection c] fun(c) -> Either (WriteError c) Unit -- from Net.shutdownWrite