louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#91 · main · 15 min 44 s
0Étoiles
0Forks
40Issues
| 1 | /// TCP sockets (native programs only: `louise run --native`, `louise build`). |
| 2 | /// |
| 3 | /// Operations that would block park the calling actor until the socket is |
| 4 | /// ready; other actors keep running. A socket is closed when its last |
| 5 | /// reference is dropped. |
| 6 | |
| 7 | import std/ffi.{type Resource} |
| 8 | |
| 9 | /// Marks listening sockets (`Resource<TcpListener>`). Sockets may be |
| 10 | /// used from any actor. |
| 11 | @thread_safe |
| 12 | pub type TcpListener { TcpListener } |
| 13 | |
| 14 | /// Marks connections (`Resource<TcpStream>`). |
| 15 | @thread_safe |
| 16 | pub type TcpStream { TcpStream } |
| 17 | |
| 18 | /// The addresses `host` names, as text (`"127.0.0.1"`, `"::1"`), in the |
| 19 | /// resolver's order; an address is itself. A lookup can take a while, so |
| 20 | /// it runs on the `io` blocking pool while the actor waits (ADR 0012, |
| 21 | /// Revision 2): other actors keep running. |
| 22 | @extern c "tl_net_resolve" (blocking io) |
| 23 | pub fn resolve :: host: String -> Result<List<String>, String> / <io> |
| 24 | |
| 25 | /// Listen on `host:port` (`"0.0.0.0"` for every interface; port 0 picks a |
| 26 | /// free port, see `port`): on the first of the addresses `host` names |
| 27 | /// that can be bound. A port outside 0 to 65535 is an error. |
| 28 | pub fn listen :: |
| 29 | host: String |
| 30 | port: Int |
| 31 | -> Result<Resource<TcpListener>, String> / <io> |
| 32 | { |
| 33 | match resolve host |
| 34 | { Ok(addresses) -> |
| 35 | first_that_works |
| 36 | addresses |
| 37 | ("no address for " <> host) |
| 38 | fn a { listen_at a port } |
| 39 | , Error(e) -> Error(e) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | @extern c "tl_net_listen" |
| 44 | fn listen_at :: |
| 45 | address: String |
| 46 | port: Int |
| 47 | -> Result<Resource<TcpListener>, String> / <io> |
| 48 | |
| 49 | /// The first `Ok` of `attempt` over `addresses`, tried in order; else the |
| 50 | /// last error (`none` if there is no address). |
| 51 | fn first_that_works :: |
| 52 | addresses: List<String> |
| 53 | none: String |
| 54 | attempt: fn(String) -> Result<r, String> / <io> |
| 55 | -> Result<r, String> / <io> |
| 56 | { |
| 57 | match addresses |
| 58 | { [] -> Error(none) |
| 59 | , [a, ..rest] -> match attempt a |
| 60 | { Ok(r) -> Ok(r), Error(e) -> first_that_works rest e attempt } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /// The port a listener is bound to. |
| 65 | @extern c "tl_net_port" |
| 66 | pub fn port :: listener: Resource<TcpListener> -> Int / <io> |
| 67 | |
| 68 | /// Wait for the next connection. |
| 69 | @extern c "tl_net_accept" |
| 70 | pub fn accept :: |
| 71 | listener: Resource<TcpListener> |
| 72 | -> Result<Resource<TcpStream>, String> / <io> |
| 73 | |
| 74 | /// Connect to `host:port`: to the first of the addresses `host` resolves |
| 75 | /// to that accepts, tried in order (`localhost` may be `::1`, then |
| 76 | /// `127.0.0.1`, and a server may listen on only one). The error is the |
| 77 | /// last address's. A port outside 0 to 65535 is an error. |
| 78 | pub fn connect :: |
| 79 | host: String |
| 80 | port: Int |
| 81 | -> Result<Resource<TcpStream>, String> / <io> |
| 82 | { |
| 83 | match resolve host |
| 84 | { Ok(addresses) -> |
| 85 | first_that_works |
| 86 | addresses |
| 87 | ("no address for " <> host) |
| 88 | fn a { connect_to a port } |
| 89 | , Error(e) -> Error(e) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | @extern c "tl_net_connect" |
| 94 | fn connect_to :: |
| 95 | address: String |
| 96 | port: Int |
| 97 | -> Result<Resource<TcpStream>, String> / <io> |
| 98 | |
| 99 | /// Up to `max` bytes that arrived; empty when the peer closed the |
| 100 | /// connection. (A read may end inside a UTF-8 character: decode text once |
| 101 | /// it is complete, with `bytes.to_string`.) |
| 102 | @extern c "tl_net_read" |
| 103 | pub fn read :: |
| 104 | conn: Resource<TcpStream> |
| 105 | max: Int |
| 106 | -> Result<Bytes, String> / <io> |
| 107 | |
| 108 | /// Send all of `data`. |
| 109 | @extern c "tl_net_write" |
| 110 | pub fn write :: |
| 111 | conn: Resource<TcpStream> |
| 112 | data: Bytes |
| 113 | -> Result<(), String> / <io> |
| 114 | |
| 115 | /// Send all of `text`, encoded as UTF-8. |
| 116 | @extern c "tl_net_write_string" |
| 117 | pub fn write_string :: |
| 118 | conn: Resource<TcpStream> |
| 119 | text: String |
| 120 | -> Result<(), String> / <io> |
| 121 | |
| 122 | /// Where a connection's other end is: an IP address as text |
| 123 | /// (`"127.0.0.1"`, `"::1"`) and a port. |
| 124 | pub type SocketAddress { SocketAddress(host: String, port: Int) } |
| 125 | |
| 126 | /// The other end of `conn`, as it was when the connection was accepted or |
| 127 | /// made (for a server, the client: behind a proxy, the proxy). |
| 128 | pub fn peer :: conn: Resource<TcpStream> -> Result<SocketAddress, String> / <io> |
| 129 | { |
| 130 | match peer_raw conn |
| 131 | { Ok((host, port)) -> Ok(SocketAddress(host, port)), Error(e) -> Error(e) } |
| 132 | } |
| 133 | |
| 134 | @extern c "tl_net_peer" |
| 135 | fn peer_raw :: conn: Resource<TcpStream> -> Result<(String, Int), String> / <io> |
| 136 | |
| 137 | /// Shut the connection down in both directions. |
| 138 | @extern c "tl_net_close" |
| 139 | pub fn close :: conn: Resource<TcpStream> -> () / <io> |
| 140 | |
| 141 | /// Stop listening: new clients are refused, and `accept` fails with "the |
| 142 | /// listener is closed", in the actor parked in it too (`http.serve_until` |
| 143 | /// stops this way). |
| 144 | @extern c "tl_net_close_listener" |
| 145 | pub fn close_listener :: listener: Resource<TcpListener> -> () / <io> |
| 146 | |
| 147 | import std/io.{type Stream} |
| 148 | |
| 149 | impl Stream<TcpStream> |
| 150 | { |
| 151 | fn read :: conn max |
| 152 | { |
| 153 | read conn max |
| 154 | } |
| 155 | fn write :: conn data |
| 156 | { |
| 157 | write conn data |
| 158 | } |
| 159 | fn close :: conn |
| 160 | { |
| 161 | close conn |
| 162 | } |
| 163 | } |