Landin library reference discuss source

platform/windows/sockets/socket_calls.ldn

1--  Windows Sockets' overlapped calls, raw: sending and receiving with an
2--  `overlapped` record, and the extension functions `ConnectEx` and
3--  `AcceptEx`, which `WSAIoctl` gives as addresses.  Each call that the
4--  system takes and has not finished fails `socket_error` with
5--  `error_io_pending` for `socket_last_error`, and ends in a packet on the
6--  completion port the socket is associated with.
7
8import platform/windows
9
10fixed if compiler.os == windows then
11    --- `WSABUF`: one buffer of a call, its length and its first byte.
12    public socket_buffer: type = struct
13        length: u32
14        reserved: u32
15        bytes: usize
16    end socket_buffer
17
18    --- Receive into the `count` buffers at `buffers`, `WSARecv`, with
19    --- `record` an `overlapped` and no completion routine.
20    public extern(c) link(symbol: "WSARecv") receive_overlapped:
21        (socket: windows.handle, buffers: ptr mut socket_buffer, count: u32,
22         received: ptr mut u32, flags: ptr mut u32, record: usize,
23         routine: usize) -> (result: i32)
24
25    --- Send the `count` buffers at `buffers`, `WSASend`.
26    public extern(c) link(symbol: "WSASend") send_overlapped:
27        (socket: windows.handle, buffers: ptr mut socket_buffer, count: u32,
28         sent: ptr mut u32, flags: u32, record: usize, routine: usize)
29        -> (result: i32)
30
31    --- Receive a datagram into the buffers, its sender's address into the
32    --- `sender_length` bytes at `sender`, `WSARecvFrom`; both must outlive the
33    --- call.
34    public extern(c) link(symbol: "WSARecvFrom") receive_from_overlapped:
35        (socket: windows.handle, buffers: ptr mut socket_buffer, count: u32,
36         received: ptr mut u32, flags: ptr mut u32, sender: ptr mut u8,
37         sender_length: ptr mut i32, record: usize, routine: usize)
38        -> (result: i32)
39
40    --- Send a datagram of the buffers to the address at `to`, `WSASendTo`.
41    public extern(c) link(symbol: "WSASendTo") send_to_overlapped:
42        (socket: windows.handle, buffers: ptr mut socket_buffer, count: u32,
43         sent: ptr mut u32, flags: u32, to: ptr u8, to_length: i32,
44         record: usize, routine: usize) -> (result: i32)
45
46    --- How an overlapped call on `socket` ended,
47    --- `WSAGetOverlappedResult`: nonzero with the bytes it moved, or zero
48    --- with the Windows Sockets code for `socket_last_error`.
49    public extern(c) link(symbol: "WSAGetOverlappedResult") socket_overlapped_result:
50        (socket: windows.handle, record: ptr mut windows.overlapped, moved: ptr mut u32,
51         wait: i32, flags: ptr mut u32) -> (done: i32)
52
53    --- A control call on `socket`, `WSAIoctl`, here only to ask for an
54    --- extension function's address.
55    public extern(c) link(symbol: "WSAIoctl") socket_control:
56        (socket: windows.handle, code: u32, given: ptr u8, given_length: u32,
57         into: ptr mut usize, into_length: u32, returned: ptr mut u32,
58         record: usize, routine: usize) -> (result: i32)
59
60    --- `SIO_GET_EXTENSION_FUNCTION_POINTER`.
61    public get_extension_function: u32 = 0xC8000006
62
63    --- The GUID that names `ConnectEx`, `WSAID_CONNECTEX`, as the 16 bytes
64    --- `socket_control` is given.
65    public connect_extension: [16]u8 =
66        [0xB9, 0x07, 0xA2, 0x25, 0xF3, 0xDD, 0x60, 0x46,
67         0x8E, 0xE9, 0x76, 0xE5, 0x8C, 0x74, 0x06, 0x3E]
68    --- The GUID that names `AcceptEx`, `WSAID_ACCEPTEX`.
69    public accept_extension: [16]u8 =
70        [0xF1, 0x7D, 0x36, 0xB5, 0xAC, 0xCB, 0xCF, 0x11,
71         0x95, 0xCA, 0x00, 0x80, 0x5F, 0x48, 0xA1, 0x92]
72
73    --- `ConnectEx`: connect `socket`, which must be bound, to the address
74    --- at `name`, sending nothing, with `record` an `overlapped`.
75    public connect_routine: type = extern(c)
76        (socket: windows.handle, name: ptr u8, name_length: i32, bytes: usize,
77         count: u32, sent: ptr mut u32, record: usize) -> (done: i32)
78
79    --- `AcceptEx`: accept a connection on `listener` into `accepted`, a
80    --- new socket of its family, receiving nothing, both addresses into
81    --- `addresses`, each given `address_room` bytes.
82    public accept_routine: type = extern(c)
83        (listener: windows.handle, accepted: windows.handle, addresses: ptr mut u8,
84         count: u32, local_room: u32, remote_room: u32,
85         received: ptr mut u32, record: usize) -> (done: i32)
86
87    --- What `AcceptEx` takes for each address: the largest socket address
88    --- and 16 bytes more.
89    public address_room: u32 = 44
90
91    --- The option that gives a socket `ConnectEx` connected the state
92    --- `getpeername`, `shutdown` and the rest expect,
93    --- `SO_UPDATE_CONNECT_CONTEXT`.
94    public so_update_connect_context: i32 = 0x7010
95    --- The same for a socket `AcceptEx` accepted, `SO_UPDATE_ACCEPT_CONTEXT`.
96    public so_update_accept_context: i32 = 0x700B
97end if