moonbitstack/moonnet/tcp does not have a README file

    Refused

    pub(all) suberror Refused {
    Secured
    } derive(Eq,
    Debug
    )

    What a connection will not do.

    Refused::equal

    fn Refused::equal(Refused, Refused) -> Bool

    Refused::not_equal

    fn Refused::not_equal(x : Refused, y : Refused) -> Bool

    Refused::to_repr

    Conn

    type Conn

    One TCP connection, in the clear or under TLS 1.3. It reads and writes through @io.Reader and @io.Writer either way, so a protocol driver serves both with the same code.

    One task may read while another writes. Writes are serialised, so two tasks writing at once put whole messages on the wire, one after the other, and TLS records stay in sequence.
    impl Reader for Conn
    impl Writer for Conn

    Conn::_direct_read

    #deprecated("`Conn::_direct_read` is deprecated, use `@moonbitlang/async/io.Reader::_direct_read` instead.")
    async fn Conn::_direct_read(self : Conn, dst : FixedArray[Byte], offset~ : Int, max_len~ : Int) -> Int

    Conn::_get_internal_buffer

    #deprecated("`Conn::_get_internal_buffer` is deprecated, use `@moonbitlang/async/io.Reader::_get_internal_buffer` instead.")
    fn Conn::_get_internal_buffer(self : Conn) ->
    ReaderBuffer

    Conn::alpn

    fn Conn::alpn(self : Conn) -> String?

    The application protocol ALPN settled on (RFC 7301). None in the clear.

    Conn::client

    async fn Conn::client(sock :
    Tcp
    , config :
    Client
    , host : String, rand? : (Int) -> Bytes, now? : Int64) -> Conn

    The client side of a TLS connection to host, returned once the handshake is done and the server's chain has been judged under config. now, in seconds since the epoch, is the time the chain is judged at; the system clock when not given.

    Conn::close

    fn Conn::close(self : Conn) -> Unit

    Close the socket. Call shutdown first for a clean end under TLS.

    Conn::drop

    async fn Conn::drop(self : Conn, len : Int) -> Int

    Conn::is_tls

    fn Conn::is_tls(self : Conn) -> Bool

    Whether the connection runs under TLS.

    Conn::opens_tls

    async fn Conn::opens_tls(self : Conn) -> Bool

    Whether the peer's stream opens with a TLS handshake record (RFC 8446 §5.1, content type 22), without consuming it. No HTTP/1.1 method or HTTP/2 preface starts with that octet.

    Conn::peer

    fn Conn::peer(self : Conn) -> Array[Bytes]

    The certificate chain the peer presented, DER, leaf first. Empty in the clear.

    Conn::plain

    A connection in the clear.

    Conn::read

    async fn Conn::read(self : Conn, dst : FixedArray[Byte], offset? : Int, max_len? : Int) -> Int

    Conn::read_all

    async fn Conn::read_all(self : Conn) -> &
    Data

    Conn::read_exactly

    async fn Conn::read_exactly(self : Conn, len : Int) -> Bytes

    Conn::read_some

    async fn Conn::read_some(self : Conn, max_len? : Int) -> Bytes?

    Conn::read_until

    async fn Conn::read_until(self : Conn, sep : StringView) -> String?

    Conn::secure

    async fn Conn::secure(self : Conn, config :
    Server
    , rand? : (Int) -> Bytes, now? : Int64) -> Conn

    Run the server side of a TLS handshake on a connection that is still in the clear, starting from what a sniff has already read ahead, and return the TLS connection once it is done. This is how one port serves TLS and the clear: look with opens_tls, then secure the ones that do.

    A connection that is already TLS raises Refused::Secured.

    Conn::server

    async fn Conn::server(sock :
    Tcp
    , config :
    Server
    , rand? : (Int) -> Bytes, now? : Int64) -> Conn

    The server side of a TLS connection, returned once the handshake is done. A handshake the client fails raises the alert, after it has been sent. now, in seconds since the epoch, is what tickets are issued and judged at and a client's certificate is judged at; the system clock when not given.

    Conn::server_name

    fn Conn::server_name(self : Conn) -> String?

    The host name the client asked for (RFC 6066 §3). None in the clear.

    Conn::shutdown

    async fn Conn::shutdown(self : Conn) -> Unit

    End the stream: under TLS a close_notify (RFC 8446 §6.1), so the peer can tell the end from a truncation. The peer may still send; read on until the end of its stream. In the clear there is nothing to send.

    Conn::starts_with

    async fn Conn::starts_with(self : Conn, prefix : BytesView) -> Bool

    Whether the peer's stream opens with prefix, without consuming it. Reads only as far as the first byte that differs, so a message shorter than prefix that is not it is not waited on — which is what telling an HTTP/2 preface from an HTTP/1.1 request needs.

    Ask before reading anything else: what a read has already taken is not looked at again.

    Conn::tls

    The TLS connection underneath, for what the accessors here do not say: the suite, the key update, the refusal reason. None in the clear.

    Conn::upgrade

    async fn Conn::upgrade(self : Conn, config :
    Client
    , host : String, rand? : (Int) -> Bytes, now? : Int64) -> Conn

    Run the client side of a TLS handshake on a connection that is still in the clear, and return the TLS connection once it is done: the STARTTLS of SMTP, IMAP and POP3, after the server agreed. Octets already read ahead in the clear are dropped, since nothing protected them (RFC 3207 §4.2, from the client's side). now is as for client.

    A connection that is already TLS raises Refused::Secured.

    Conn::write

    async fn Conn::write(self : Conn, data : &
    Data
    ) -> Unit

    Conn::write_once

    async fn Conn::write_once(self : Conn, src : Bytes, offset~ : Int, len~ : Int) -> Int

    Conn::write_reader

    async fn Conn::write_reader(self : Conn, reader : &
    Reader
    ) -> Unit

    Listener

    type Listener

    A bound, listening socket. Connections queue from the moment listen returns, so a caller can connect to it, or hand its port out, without racing the server.

    Listener::accept

    The next connection and its peer's address, after the TLS handshake when there is TLS. A handshake that fails or runs out of time raises, with the socket closed.

    Listener::addr

    The address listened on; with port 0 asked for, the port the system chose.

    Listener::close

    fn Listener::close(self : Listener) -> Unit

    Stop listening.

    Listener::serve

    async fn Listener::serve(self : Listener, handler : async (Conn,
    Addr
    ) -> Unit, limit? : Int) -> Unit

    Run handler on each connection with the peer's address. When the handler returns, a TLS connection is shut down with a close_notify. A connection whose handshake fails, or whose handler raises, is closed without stopping the server. Runs until cancelled, then closes the listener.

    limit caps the connections served at once; the rest wait in the listen queue.

    dial

    async fn dial(host : String, port~ : Int, tls? :
    Client
    , now? : Int64) -> Conn

    Connect to host:port, under TLS when tls is given, and return once the handshake is done. The host is resolved, then names the server for SNI and for checking its certificate.

    handshake

    let handshake : Int

    How long a TLS handshake may take, in milliseconds: asyncio's and hypercorn's 60 seconds. A peer that connects and never finishes holds a connection slot until then.

    host

    let host : String

    The address a server listens on when none is given: loopback only, as uvicorn and Starlette default to, so a development server is not reachable from the network by accident.

    listen

    async fn listen(host? : String, port? : Int, tls? :
    Server
    , reuse_addr? : Bool, handshake? : Int?, cleartext? : Bool) -> Listener

    Bind host:port and listen, for TLS connections when tls is given. Port 0 asks the system for a free one; addr says which. reuse_addr lets a restarted server bind while the old socket lingers in TIME_WAIT, as uvicorn does. handshake bounds each TLS handshake; None waits for ever.

    cleartext lets a TLS listener take connections in the clear too, on the same port: one that opens with a TLS handshake record is served TLS, any other is left in the clear. Off, a TLS listener takes TLS only.

    port

    let port : Int

    The port every server in the family listens on by default.

    serve

    async fn serve(handler : async (Conn,
    Addr
    ) -> Unit, host? : String, port? : Int, tls? :
    Server
    , limit? : Int, reuse_addr? : Bool, handshake? : Int?) -> Unit

    listen, then serve: accept connections on host:port and run handler on each until cancelled.

    Source Files