net.jamo (built in)

net: web requests over HTTP and HTTPS, with WinHTTP. use "net.jamo"

A request waits until the answer has come, or the time runs out. Problems along the way, like a server that can't be reached or takes too long, don't stop the program: the response's error says what went wrong, and its status is 0. Only mistakes in the program itself, like a web address that isn't one, stop it.

Background requests don't wait: start_get and start_post give back a Request at once, and done() checks on it each frame. WinHTTP tells us about progress by calling net_signal on its own threads, so that function only writes numbers into the request's block, which is memory from LocalAlloc that the memory cleaner never sees. Everything else happens on the program's own thread, in done(). A block is only given back once WinHTTP says the request's handle is closed, since it may still write there until then. The block: sent at 0, headers at 4, data waiting at 8 with how much at 12, read done at 16, failed at 20 with the error at 24, closed at 28. The 64 KB from 64 take each piece of the answer, and what's sent with a post follows at 65600. After that come the server name, the method, the path and the headers as Windows text, since WinHTTP may read them after the call that took them has returned.

Links are TCP connections between programs, for multiplayer games. One program listens on a port and others connect to it. Each send() is one whole message: it goes out with its length in front, as 4 bytes, so messages() gives back exactly what was sent, however TCP splits it up on the way. Sockets never wait: messages() sends what's waiting to go and reads what has come, once a frame.

structResponse

struct Response
    status: int
    text: string
    error: string
end

structNet

struct Net
    agent: int
    session: int
    background: int
    closing: [int]
    seconds: float
    sockets_started: bool
    open: bool
end

structListener

struct Listener
    socket: int
    port: int
    open: bool
end

A program waiting for others to connect, on one port

structLink

struct Link
    socket: int
    connecting: bool
    open: bool
    error: string
    outgoing: int
    out_size: int
    out_start: int
    out_used: int
    incoming: int
    in_size: int
    in_used: int
    piece: int
end

A connection to another program. messages() gives back what came, a whole message at a time

structRequest

struct Request
    net: Net
    block: int
    connection: int
    request: int
    stage: int
    status: int
    buffer: int
    capacity: int
    used: int
    waiting: int
    address: NetAddress
    result: Response
    deadline: float
    clock: int
    finished: bool
end

A request running in the background. Check done() each frame, then read response()

structNetAddress

struct NetAddress
    secure: bool
    host: string
    port: int
    path: string
end

funcstart_net

func start_net(): Net

funcNet.set_timeout

func Net.set_timeout(seconds: float)

How long a request may take at each stage before it gives up with a timeout error

funcNet.must_be_open

func Net.must_be_open()

funcNet.close

func Net.close()

funcNet.get

func Net.get(url: string): Response

funcNet.post

func Net.post(url: string, text: string): Response

Sends text to a web address, as plain UTF-8 text

funcNet.request

func Net.request(verb: string, url: string, body: string): Response

funcnet_time_limits

func net_time_limits(request: int, seconds: float)

Sets every time limit on one request, including the wait for the answer's headers, which WinHttpSetTimeouts leaves at 90 seconds

funcnet_failed

func net_failed(code: int, address: NetAddress): Response

funcnet_address

func net_address(url: string): NetAddress

funcnet_starts

func net_starts(text: string, front: string): bool

funcNet.start_get

func Net.start_get(url: string): Request

funcNet.start_post

func Net.start_post(url: string, text: string): Request

funcNet.start

func Net.start(verb: string, url: string, body: string): Request

funcRequest.done

func Request.done(): bool

Takes the request's next steps, and answers true once it has finished, worked or not

funcnet_clock

func net_clock(place: int): float

Seconds on the PC's steady clock. WinHTTP's own time limits only get checked when something arrives, so each request keeps its own deadline instead

funcRequest.ask_for_data

func Request.ask_for_data()

funcRequest.keep

func Request.keep(from: int, count: int)

Copies a piece of the answer out of the block, into memory that grows

funcRequest.response

func Request.response(): Response

What came back. Only after done() has answered true

funcRequest.cancel

func Request.cancel()

Stops a request that isn't needed any more

funcRequest.fail_with

func Request.fail_with(code: int)

funcRequest.finish

func Request.finish(result: Response)

funcNet.tidy

func Net.tidy()

Gives back the blocks of requests whose handles WinHTTP has finished closing

funcNet.closing_count

func Net.closing_count(): int

How many finished requests WinHTTP is still closing

funcnet_signal

func net_signal(handle: int, block: int, status: int, info: int): int

WinHTTP calls this on its own threads. It only writes numbers into the request's block: it must never make text, arrays or structs, because the memory cleaner works on the program's own thread

funcNet.start_sockets

func Net.start_sockets()

funcNet.listen

func Net.listen(port: int): Listener

Waits for other programs to connect on a port, from any PC that can reach this one. Windows may ask whether to let the program through its firewall

funcNet.listen_here

func Net.listen_here(port: int): Listener

The same, but only programs on this PC can connect. Good for testing

funcNet.listen_on

func Net.listen_on(port: int, address: int): Listener

funcListener.new_links

func Listener.new_links(): [Link]

The links of programs that connected since the last call. Call it once a frame

funcListener.close

func Listener.close()

funcNet.connect

func Net.connect(host: string, port: int): Link

Connects to a program listening on a port. host is an address like "127.0.0.1", or "localhost". The link starts out connecting: check connected(), or just send, since messages wait until it's ready

funcnet_link

func net_link(s: int, connecting: bool): Link

funcnet_new_socket

func net_new_socket(): int

funcnet_no_waiting

func net_no_waiting(s: int)

Sockets that never wait

funcnet_socket_address

func net_socket_address(address: int, port: int): int

A sockaddr_in: the family, the port with its high byte first, then the address

funcnet_ipv4

func net_ipv4(host: string): int

Turns "1.2.3.4" or "localhost" into the 4 bytes an address takes, first number lowest

funcLink.connected

func Link.connected(): bool

True once the link has finished connecting

funcLink.check_connecting

func Link.check_connecting()

funcLink.send

func Link.send(text: string)

Sends one message. It goes out as soon as it can, during messages()

funcLink.make_room

func Link.make_room(extra: int)

funcLink.flush

func Link.flush()

Sends as much of what's waiting as the connection takes now, up to 64 KB at a time

funcLink.messages

func Link.messages(): [string]

Sends what's waiting, reads what has come, and gives back the whole messages. Call it once a frame

funcLink.keep_incoming

func Link.keep_incoming(count: int)

funcLink.take_messages

func Link.take_messages(found: [string])

Moves every whole message from what has come into found

funcLink.problem

func Link.problem(): string

Why the link stopped, or "" while it works

funcLink.is_open

func Link.is_open(): bool

funcLink.broken

func Link.broken(why: string)

funcLink.close

func Link.close()

funcnet_copy_wide

func net_copy_wide(place: int, text: string): int

Copies text as Windows text, ending in a 0, to a place outside Jamo's memory. Gives back the bytes used

funcnet_lasting_wide

func net_lasting_wide(text: string): int

Windows text that stays put until it's given back with LocalFree