UXSocket
UXSocket is one TCP connection. It hides what differs between the hosts:
Winsock’s start-up, SOCKET and closesocket on Windows, the two addrinfo
layouts, and SIGPIPE from a peer that has gone. The address comes from
getaddrinfo, IPv4 or IPv6, so nothing builds a sockaddr by hand.
#import "UXSocket.xc"Overview
Section titled “Overview”UXSocket* s = UXSocket.connectTo((u8*)"localhost", (i32)7070);if (s == (UXSocket*)0) { Stdio.printf("no daemon: %s\n", UXSocket.lastError()); return;}s.write(request, n);// each turn of the frame clock:i32 got = s.read(buf, cap); // > 0 bytes, 0 nothing yet, -1 the peer closedread never blocks, so a client can poll it from its frame clock
(UXApplication.everyTurn) while
the run loop keeps turning. waitReadable blocks for up to a
given time, for a tool that has nothing else to do.
There is no TCP in a browser (wasm32) or on XTOS here: connectTo answers null
with the reason.
Topics
Section titled “Topics”Connecting · connectTo · lastError · isOpen · close Data · write · writeData · read · waitReadable
connectTo
Section titled “connectTo”static UXSocket* connectTo(u8* host, i32 port)A connection to host:port, a name or a numeric address. It blocks until the
connection is made or refused. Null when it cannot be made, with
lastError saying why.
lastError
Section titled “lastError”static u8* lastError(void)Why the last connectTo failed, or "" after one that succeeded.
isOpen
Section titled “isOpen”bool isOpen(void)False once the connection is closed, by close or by the peer.
bool write(u8* p, i32 n)Sends every byte. False when the connection has gone, and the socket is then closed.
writeData
Section titled “writeData”bool writeData(UXData* d)write of a UXData’s bytes.
i32 read(u8* p, i32 cap)What has arrived, without waiting: the byte count (up to cap), 0 when
nothing has arrived yet, or -1 when the peer has closed the connection or it
failed. The socket is then closed.
waitReadable
Section titled “waitReadable”bool waitReadable(i32 ms)Waits up to ms milliseconds (0: not at all, -1: as long as it takes) for
something to read or for the peer to go. True when read would return something
other than 0.
void close(void)Closes the connection. A socket also closes when it is freed.