/// TCP sockets (native programs only: `louise run --native`, `louise build`). /// /// Operations that would block park the calling actor until the socket is /// ready; other actors keep running. A socket is closed when its last /// reference is dropped. import std/ffi.{type Resource} /// Marks listening sockets (`Resource`). Sockets may be /// used from any actor. @thread_safe pub type TcpListener { TcpListener } /// Marks connections (`Resource`). @thread_safe pub type TcpStream { TcpStream } /// The addresses `host` names, as text (`"127.0.0.1"`, `"::1"`), in the /// resolver's order; an address is itself. A lookup can take a while, so /// it runs on the `io` blocking pool while the actor waits (ADR 0012, /// Revision 2): other actors keep running. @extern c "tl_net_resolve" (blocking io) pub fn resolve :: host: String -> Result, String> / /// Listen on `host:port` (`"0.0.0.0"` for every interface; port 0 picks a /// free port, see `port`): on the first of the addresses `host` names /// that can be bound. A port outside 0 to 65535 is an error. pub fn listen :: host: String port: Int -> Result, String> / { match resolve host { Ok(addresses) -> first_that_works addresses ("no address for " <> host) fn a { listen_at a port } , Error(e) -> Error(e) } } @extern c "tl_net_listen" fn listen_at :: address: String port: Int -> Result, String> / /// The first `Ok` of `attempt` over `addresses`, tried in order; else the /// last error (`none` if there is no address). fn first_that_works :: addresses: List none: String attempt: fn(String) -> Result / -> Result / { match addresses { [] -> Error(none) , [a, ..rest] -> match attempt a { Ok(r) -> Ok(r), Error(e) -> first_that_works rest e attempt } } } /// The port a listener is bound to. @extern c "tl_net_port" pub fn port :: listener: Resource -> Int / /// Wait for the next connection. @extern c "tl_net_accept" pub fn accept :: listener: Resource -> Result, String> / /// Connect to `host:port`: to the first of the addresses `host` resolves /// to that accepts, tried in order (`localhost` may be `::1`, then /// `127.0.0.1`, and a server may listen on only one). The error is the /// last address's. A port outside 0 to 65535 is an error. pub fn connect :: host: String port: Int -> Result, String> / { match resolve host { Ok(addresses) -> first_that_works addresses ("no address for " <> host) fn a { connect_to a port } , Error(e) -> Error(e) } } @extern c "tl_net_connect" fn connect_to :: address: String port: Int -> Result, String> / /// Up to `max` bytes that arrived; empty when the peer closed the /// connection. (A read may end inside a UTF-8 character: decode text once /// it is complete, with `bytes.to_string`.) @extern c "tl_net_read" pub fn read :: conn: Resource max: Int -> Result / /// Send all of `data`. @extern c "tl_net_write" pub fn write :: conn: Resource data: Bytes -> Result<(), String> / /// Send all of `text`, encoded as UTF-8. @extern c "tl_net_write_string" pub fn write_string :: conn: Resource text: String -> Result<(), String> / /// Where a connection's other end is: an IP address as text /// (`"127.0.0.1"`, `"::1"`) and a port. pub type SocketAddress { SocketAddress(host: String, port: Int) } /// The other end of `conn`, as it was when the connection was accepted or /// made (for a server, the client: behind a proxy, the proxy). pub fn peer :: conn: Resource -> Result / { match peer_raw conn { Ok((host, port)) -> Ok(SocketAddress(host, port)), Error(e) -> Error(e) } } @extern c "tl_net_peer" fn peer_raw :: conn: Resource -> Result<(String, Int), String> / /// Shut the connection down in both directions. @extern c "tl_net_close" pub fn close :: conn: Resource -> () / /// Stop listening: new clients are refused, and `accept` fails with "the /// listener is closed", in the actor parked in it too (`http.serve_until` /// stops this way). @extern c "tl_net_close_listener" pub fn close_listener :: listener: Resource -> () / import std/io.{type Stream} impl Stream { fn read :: conn max { read conn max } fn write :: conn data { write conn data } fn close :: conn { close conn } }