proposals/sockets/TcpSocketOperationalSemantics-0.3.0-draft.md
WASI TCP sockets must behave as-if they are implemented using the state machine described in this document.
Note: These refer to the states of the TCP socket, not the TCP connection
In pseudo code:
interface tcp {
variant state {
unbound,
bound,
listening(accept-stream),
connecting(connect-future),
connected,
closed(option<error-code>),
}
}
The following diagram describes the exhaustive set of all possible state transitions:
stateDiagram-v2
state "unbound" as Unbound
state "bound" as Bound
state "connecting" as Connecting
state "connected" as Connected
state "listening" as Listening
state "closed" as Closed
[*] --> Unbound: create() -> ok
Unbound --> Bound: bind() -> ok
Unbound --> Connecting: connect()
Connecting --> Connected: «task resolves successfully»
Connecting --> Closed: «task resolves with error»
Connected --> Closed: «connection terminated»
Bound --> Connecting: connect()
Bound --> Listening: listen() -> ok
Unbound --> Listening: listen() -> ok
-> ok only apply when the method returns successfully.error(invalid-state) and does not affect the state of the socket.listen() are immediately in the connected state.