Landin library reference discuss source

hosted/events/windows_network.ldn

1--  The event provider's `net.network` on Windows, over Windows Sockets'
2--  overlapped calls: a socket is made overlapped and not inherited, as
3--  hosted/blocking makes one, and every call that would wait is issued
4--  with its record's `overlapped`, `WSARecv`, `WSASend`, `WSARecvFrom`,
5--  `WSASendTo`, and `ConnectEx` and `AcceptEx`, which `WSAIoctl` gives,
6--  and parks its fibre until the call's packet reaches the loop (D486).
7--  The limits `set_timeouts` keeps on the socket, as hosted/blocking
8--  keeps them, are each call's deadline: when one passes the call is
9--  cancelled, and fails `timed_out` once its cancelling's packet has come,
10--  unless it ended first.  Windows' own limits cover no overlapped call.
11--  Shutting a side through the provider cancels the calls waiting on that
12--  side, and closing it every call, each failing `io_failed` (D449).
13
14import core/io
15import core/net
16import core/time
17import hosted/system
18import platform/windows
19import platform/windows/sockets
20
21fixed if compiler.os == windows then
22    --  How many connections may wait to be accepted.
23    backlog: i32 = 128
24
25    --  Whether a code says the system does not have the address here.
26    unavailable: (code: u32) -> (yes: bool) =
27        yes = code == sockets.wsa_address_unavailable
28              or code == sockets.wsa_family_unsupported
29    end unavailable
30
31    --  Whether a code says the peer has gone.
32    gone: (code: u32) -> (yes: bool) =
33        yes = code == sockets.wsa_connection_reset
34              or code == sockets.wsa_connection_aborted
35              or code == sockets.wsa_not_connected
36              or code == sockets.wsa_shut_down
37              or code == sockets.wsa_network_reset
38              or code == sockets.wsa_timed_out
39    end gone
40
41    --  The code the failing call left.
42    last: () -> (code: u32) =
43        code = sockets.socket_last_error()
44    end last
45
46    --  Close a socket no caller is told about.
47    quietly_closed: (closing: windows.handle) -> none =
48        _ = sockets.close_socket(closing)
49    end quietly_closed
50
51    --  A new overlapped socket of `family` and `kind` that no child
52    --  inherits, or the reason there is none: Windows Sockets started
53    --  first.
54    family_socket: (family: i32, kind: i32)
55                   -> (socket: windows.handle, code: u32) =
56        socket = sockets.invalid_socket
57        code = sockets.started()
58        return when code <> 0
59        socket = sockets.make_socket(family, kind, 0, 0, 0,
60                                     sockets.wsa_flag_overlapped
61                                     | sockets.wsa_flag_no_handle_inherit)
62        if socket == sockets.invalid_socket then code = last() end if
63    end family_socket
64
65    --  The family of socket an address needs.
66    family_of: (at: net.address) -> (family: i32) =
67        family = if at.family == net.ipv4 then sockets.af_inet
68                 else sockets.af_inet6 end if
69    end family_of
70
71    --  A socket bound to `at`, a listener when `listening`: the reason it
72    --  could not be, or zero.  An IPv6 socket takes IPv6 alone, as on
73    --  every system.
74    bound: (at: net.address, kind: i32, listening: bool)
75           -> (socket: windows.handle, code: u32) =
76        (socket: made, code: given) := family_socket(family_of(at), kind)
77        socket = made
78        code = given
79        return when code <> 0
80        if at.family == net.ipv6 then
81            on: u32 = 1
82            if sockets.set_option(socket, sockets.ipproto_ipv6,
83                                  sockets.ipv6_only, ptr(usize(addr on)),
84                                  4) <> 0 then
85                code = last()
86            end if
87        end if
88        if code == 0 then
89            record_of: sockets.socket_address =
90                system.internet_address(at) else (problem)
91                    _ = problem
92                    quietly_closed(socket)
93                    socket = sockets.invalid_socket
94                    code = sockets.wsa_address_unavailable
95                    return
96                end
97            if sockets.bind(socket, addr record_of.bytes[0],
98                            record_of.length) <> 0 then
99                code = last()
100            elsif listening and sockets.listen(socket, backlog) <> 0 then
101                code = last()
102            end if
103        end if
104        if code <> 0 then
105            quietly_closed(socket)
106            socket = sockets.invalid_socket
107        end if
108    end bound
109
110    bind_failure: (self: host, code: u32)
111                  -> none ! net.address_in_use | net.address_unavailable
112                          | io.no_access | io.io_failed =
113        record(self, code)
114        fail net.address_in_use when code == sockets.wsa_address_in_use
115        fail net.address_unavailable when unavailable(code)
116        fail io.no_access when code == sockets.wsa_access
117        fail io.io_failed
118    end bind_failure
119
120    events_listen: (self: host, at: net.address) -> (listener: io.file)
121                   ! net.address_in_use | net.address_unavailable
122                   | io.no_access | io.io_failed =
123        cleared(self)
124        (socket, code) := bound(at, sockets.sock_stream, true)
125        if code <> 0 then
126            try bind_failure(self, code)
127        end if
128        listener = system.socket_of(socket)
129    end events_listen
130
131    events_open_datagram: (self: host, at: net.address) -> (socket: io.file)
132                          ! net.address_in_use | net.address_unavailable
133                          | io.no_access | io.io_failed =
134        cleared(self)
135        (socket: made, code) := bound(at, sockets.sock_dgram, false)
136        if code <> 0 then
137            try bind_failure(self, code)
138        end if
139        socket = system.socket_of(made)
140    end events_open_datagram
141
142    --  A span as Windows Sockets' milliseconds, rounded up so that a limit
143    --  is never shorter than asked, zero for none.
144    milliseconds_of: (span: time.duration) -> (count: u32) =
145        mut whole: u64 = (u64(span) + 999999) / 1000000
146        if whole > 0x7FFFFFFF then whole = 0x7FFFFFFF end if
147        count = u32(whole)
148    end milliseconds_of
149
150    --  The deadline of a call on `socket` limited by what `set_timeouts`
151    --  gave it, for sending or receiving: zero for none.
152    limit_of: (socket: windows.handle, sending: bool) -> (deadline: u64) =
153        mut value: u32 = 0
154        mut length: i32 = 4
155        option: i32 = if sending then sockets.so_send_timeout
156                      else sockets.so_receive_timeout end if
157        deadline = 0
158        return when sockets.get_option(socket, sockets.sol_socket, option,
159                                       ptr(usize(addr value)),
160                                       addr length) <> 0
161        return when value == 0
162        deadline = deadline_after(time.milliseconds(u64(value)))
163    end limit_of
164
165    --  Wait for the socket call `entry` records, which the system was
166    --  given and answered `issued` with, and return how it ended, its code
167    --  Windows Sockets': `wsa_timed_out` when its deadline cancelled it.
168    --  A call the system refused at once queues no packet and is taken off
169    --  the list.
170    socket_completed: (self: host, socket: windows.handle,
171                       entry: ptr mut waiting, issued: i32)
172                      -> (result: call_result) =
173        if issued <> 0 then
174            code: u32 = last()
175            if code <> windows.error_io_pending then
176                unlink(self.state, entry)
177                result = (moved: 0, code: code, answer: failed)
178                return
179            end if
180        end if
181        answer: u8 = answered(self, entry)
182        mut got: u32 = 0
183        mut flags: u32 = 0
184        mut code: u32 = 0
185        if sockets.socket_overlapped_result(socket, addr entry.val.asked,
186                                            addr got, 0, addr flags) == 0 then
187            code = last()
188            if answer == expired and code == windows.error_operation_aborted then
189                code = sockets.wsa_timed_out
190            end if
191        end if
192        result = (moved: got, code: code, answer: answer)
193    end socket_completed
194
195    --  The most one call sends or receives, which an int counts.
196    most_bytes: usize = 0x40000000
197
198    --  One receive on a connection into `into`, until `deadline`.
199    received_once: (self: host, socket: windows.handle, into: []mut u8,
200                    deadline: u64) -> (result: call_result) ! io.io_failed =
201        try joined(self, socket)
202        mut want: usize = lenof into
203        if want > most_bytes then want = most_bytes end if
204        mut buffer: sockets.socket_buffer =
205            (length: u32(want), reserved: 0, bytes: usize(addr into[0]))
206        mut entry: waiting = on_handle(socket, deadline)
207        link(self.state, addr entry)
208        mut got: u32 = 0
209        mut flags: u32 = 0
210        issued: i32 = sockets.receive_overlapped(socket, addr buffer, 1,
211                                                 addr got, addr flags,
212                                                 usize(addr entry.asked), 0)
213        result = socket_completed(self, socket, addr entry, issued)
214    end received_once
215
216    --  One send on a connection of `bytes`, until `deadline`.
217    sent_once: (self: host, socket: windows.handle, bytes: []u8,
218                deadline: u64) -> (result: call_result) ! io.io_failed =
219        try joined(self, socket)
220        mut want: usize = lenof bytes
221        if want > most_bytes then want = most_bytes end if
222        mut buffer: sockets.socket_buffer =
223            (length: u32(want), reserved: 0, bytes: usize(addr bytes[0]))
224        mut entry: waiting = on_handle(socket, deadline)
225        entry.output = true
226        link(self.state, addr entry)
227        mut put: u32 = 0
228        issued: i32 = sockets.send_overlapped(socket, addr buffer, 1, addr put,
229                                              0, usize(addr entry.asked), 0)
230        result = socket_completed(self, socket, addr entry, issued)
231    end sent_once
232
233    --  Fail a connection's call as the concept says of its code.
234    connection_failure: (self: host, code: u32)
235                        -> none ! net.disconnected | net.timed_out
236                                | io.io_failed =
237        record(self, code)
238        fail net.timed_out when code == sockets.wsa_timed_out
239        fail net.disconnected when gone(code)
240        fail io.io_failed
241    end connection_failure
242
243    events_send: (self: host, connection: io.file, bytes: []u8)
244                 -> none ! net.disconnected | net.timed_out | io.io_failed =
245        cleared(self)
246        socket: windows.handle = system.handle_of(connection)
247        mut sent: usize = 0
248        while sent < lenof bytes do
249            ended: call_result = try sent_once(self, socket,
250                                               bytes[sent..<lenof bytes],
251                                               limit_of(socket, true))
252            if ended.code <> 0 then
253                if ended.answer == failed then
254                    record(self, ended.code)
255                    fail io.io_failed
256                end if
257                try connection_failure(self, ended.code)
258            end if
259            fail io.io_failed when ended.moved == 0
260              or usize(ended.moved) > lenof bytes - sent
261            sent += usize(ended.moved)
262        end while
263    end events_send
264
265    events_receive: (self: host, connection: io.file, into: []mut u8)
266                    -> (count: usize)
267                    ! net.disconnected | net.timed_out | io.io_failed =
268        cleared(self)
269        count = 0
270        return when lenof into == 0
271        socket: windows.handle = system.handle_of(connection)
272        ended: call_result = try received_once(self, socket, into,
273                                               limit_of(socket, false))
274        if ended.code <> 0 then
275            if ended.answer == failed then
276                record(self, ended.code)
277                fail io.io_failed
278            end if
279            try connection_failure(self, ended.code)
280        end if
281        fail io.io_failed when usize(ended.moved) > lenof into
282        count = usize(ended.moved)
283    end events_receive
284
285    --  An extension function of Windows Sockets, as `socket_control` gives
286    --  its address for `socket`: zero, with the code kept, when it does not.
287    extension: (self: host, socket: windows.handle, named: [16]u8)
288               -> (address: usize) =
289        mut found: usize = 0
290        mut returned: u32 = 0
291        address = 0
292        if sockets.socket_control(socket, sockets.get_extension_function,
293                                  addr named[0], 16, addr found,
294                                  u32(sizeof usize), addr returned, 0,
295                                  0) <> 0 then
296            record(self, last())
297            return
298        end if
299        address = found
300    end extension
301
302    connect_failure: (self: host, code: u32)
303                     -> none ! net.refused | net.unreachable
304                             | net.address_unavailable | net.timed_out
305                             | io.io_failed =
306        record(self, code)
307        fail net.refused when code == sockets.wsa_connection_refused
308        fail net.unreachable when code == sockets.wsa_network_unreachable
309          or code == sockets.wsa_host_unreachable
310        fail net.address_unavailable when unavailable(code)
311        fail net.timed_out when code == sockets.wsa_timed_out
312        fail io.io_failed
313    end connect_failure
314
315    --  The address a socket of `to`'s family binds to before `ConnectEx`,
316    --  which needs a bound socket: any address, any port.
317    anywhere: (to: net.address) -> (at: net.address) =
318        at = (family: to.family, octets: zeroed, port: 0, zone: zeroed)
319    end anywhere
320
321    --  Connect a new socket to `to` by `deadline`, zero for none: the
322    --  socket, or the reason it was not connected.  Nobody else holds the
323    --  socket yet, so nothing closes it while the fibre waits.
324    connected: (self: host, to: net.address, deadline: u64)
325               -> (socket: windows.handle, code: u32) ! io.io_failed =
326        (socket: made, code: given) := bound(anywhere(to), sockets.sock_stream,
327                                             false)
328        socket = made
329        code = given
330        return when code <> 0
331        target: sockets.socket_address = system.internet_address(to)
332            else (problem)
333            _ = problem
334            quietly_closed(socket)
335            socket = sockets.invalid_socket
336            code = sockets.wsa_address_unavailable
337            return
338        end
339        address: usize = extension(self, socket, sockets.connect_extension)
340        if address == 0 then
341            quietly_closed(socket)
342            socket = sockets.invalid_socket
343            fail io.io_failed
344        end if
345        joined(self, socket) else (problem)
346            quietly_closed(socket)
347            fail problem
348        end
349        connect: sockets.connect_routine = sockets.connect_routine(address)
350        mut entry: waiting = on_handle(socket, deadline)
351        entry.output = true
352        link(self.state, addr entry)
353        mut sent: u32 = 0
354        done: i32 = connect(socket, addr target.bytes[0], target.length, 0, 0,
355                            addr sent, usize(addr entry.asked))
356        ended: call_result = socket_completed(self, socket, addr entry,
357                                              if done <> 0 then 0 else -1
358                                              end if)
359        code = ended.code
360        if code == 0
361          and sockets.set_option(socket, sockets.sol_socket,
362                                 sockets.so_update_connect_context,
363                                 ptr(usize(addr sent)), 0) <> 0 then
364            code = last()
365        end if
366        if code <> 0 then
367            quietly_closed(socket)
368            socket = sockets.invalid_socket
369        end if
370    end connected
371
372    connection: (self: host, to: net.address, deadline: u64)
373                -> (opened: io.file)
374                ! net.refused | net.unreachable | net.address_unavailable
375                | net.timed_out | io.io_failed =
376        cleared(self)
377        (socket, code) := try connected(self, to, deadline)
378        if code <> 0 then
379            try connect_failure(self, code)
380        end if
381        opened = system.socket_of(socket)
382    end connection
383
384    events_connect: (self: host, to: net.address) -> (opened: io.file)
385                    ! net.refused | net.unreachable | net.address_unavailable
386                    | net.timed_out | io.io_failed =
387        opened = try connection(self, to, 0)
388    end events_connect
389
390    events_connect_within: (self: host, to: net.address,
391                            limit: time.duration)
392                           -> (opened: io.file)
393                           ! net.refused | net.unreachable
394                           | net.address_unavailable | net.timed_out
395                           | io.io_failed =
396        deadline: u64 = if u64(limit) == 0 then 0
397                        else deadline_after(limit) end if
398        opened = try connection(self, to, deadline)
399    end events_connect_within
400
401    --  The family of the socket `socket`, from its own address.
402    family_named: (self: host, socket: windows.handle) -> (family: i32)
403                  ! io.io_failed =
404        mut found: sockets.socket_address = (bytes: zeroed, length: 28)
405        if sockets.socket_name(socket, addr found.bytes[0],
406                               addr found.length) <> 0 then
407            record(self, last())
408            fail io.io_failed
409        end if
410        family = i32(u16(found.bytes[0]) | (u16(found.bytes[1]) << 8))
411    end family_named
412
413    events_accept: (self: host, listener: io.file) -> (accepted: io.file)
414                   ! net.timed_out | io.io_failed =
415        cleared(self)
416        socket: windows.handle = system.handle_of(listener)
417        deadline: u64 = limit_of(socket, false)
418        family: i32 = try family_named(self, socket)
419        address: usize = extension(self, socket, sockets.accept_extension)
420        fail io.io_failed when address == 0
421        accept: sockets.accept_routine = sockets.accept_routine(address)
422        try joined(self, socket)
423        loop do
424            (socket: made, code: given) := family_socket(family,
425                                                         sockets.sock_stream)
426            if given <> 0 then
427                record(self, given)
428                fail io.io_failed
429            end if
430            mut addresses: [88]u8 = zeroed
431            mut entry: waiting = on_handle(socket, deadline)
432            link(self.state, addr entry)
433            mut received: u32 = 0
434            done: i32 = accept(socket, made, addr addresses[0], 0,
435                               sockets.address_room, sockets.address_room,
436                               addr received, usize(addr entry.asked))
437            ended: call_result = socket_completed(self, socket, addr entry,
438                                                  if done <> 0 then 0
439                                                  else -1 end if)
440            mut code: u32 = ended.code
441            if code == 0
442              and sockets.set_option(made, sockets.sol_socket,
443                                     sockets.so_update_accept_context,
444                                     ptr(usize(addr socket)),
445                                     i32(sizeof windows.handle)) <> 0 then
446                code = last()
447            end if
448            if code == 0 then
449                accepted = system.socket_of(made)
450                return
451            end if
452            quietly_closed(made)
453            --  A connection its peer reset before it was taken is no
454            --  connection.
455            continue when ended.answer <> failed
456              and code == sockets.wsa_connection_reset
457            record(self, code)
458            fail net.timed_out when code == sockets.wsa_timed_out
459            fail io.io_failed
460        end loop
461    end events_accept
462
463    events_send_to: (self: host, socket: io.file, bytes: []u8,
464                     to: net.address) -> none
465                    ! net.unreachable | net.timed_out | io.io_failed =
466        cleared(self)
467        made: windows.handle = system.handle_of(socket)
468        target: sockets.socket_address = system.internet_address(to)
469            else (problem)
470            _ = problem
471            record(self, sockets.wsa_address_unavailable)
472            fail net.unreachable
473        end
474        fail io.io_failed when lenof bytes > most_bytes
475        try joined(self, made)
476        mut spare: [1]u8 = zeroed
477        first: usize = if lenof bytes > 0 then usize(addr bytes[0])
478                       else usize(addr spare[0]) end if
479        mut buffer: sockets.socket_buffer =
480            (length: u32(lenof bytes), reserved: 0, bytes: first)
481        mut entry: waiting = on_handle(made, limit_of(made, true))
482        entry.output = true
483        link(self.state, addr entry)
484        mut put: u32 = 0
485        issued: i32 = sockets.send_to_overlapped(made, addr buffer, 1, addr put,
486                                                 0, addr target.bytes[0],
487                                                 target.length,
488                                                 usize(addr entry.asked), 0)
489        ended: call_result = socket_completed(self, made, addr entry, issued)
490        if ended.code <> 0 then
491            record(self, ended.code)
492            fail io.io_failed when ended.answer == failed
493            fail net.unreachable
494                when ended.code == sockets.wsa_network_unreachable
495                  or ended.code == sockets.wsa_host_unreachable
496            fail net.timed_out when ended.code == sockets.wsa_timed_out
497            fail io.io_failed
498        end if
499        --  A datagram goes whole or not at all.
500        fail io.io_failed when usize(ended.moved) <> lenof bytes
501    end events_send_to
502
503    events_receive_from: (self: host, socket: io.file, into: []mut u8)
504                         -> (taken: net.datagram)
505                         ! net.timed_out | io.io_failed =
506        cleared(self)
507        made: windows.handle = system.handle_of(socket)
508        try joined(self, made)
509        mut sender: sockets.socket_address = (bytes: zeroed, length: 28)
510        mut spare: [1]u8 = zeroed
511        mut want: usize = lenof into
512        if want > most_bytes then want = most_bytes end if
513        first: usize = if want > 0 then usize(addr into[0])
514                       else usize(addr spare[0]) end if
515        mut buffer: sockets.socket_buffer =
516            (length: u32(want), reserved: 0, bytes: first)
517        mut entry: waiting = on_handle(made, limit_of(made, false))
518        link(self.state, addr entry)
519        mut got: u32 = 0
520        mut flags: u32 = 0
521        issued: i32 = sockets.receive_from_overlapped(
522            made, addr buffer, 1, addr got, addr flags, addr sender.bytes[0],
523            addr sender.length, usize(addr entry.asked), 0)
524        ended: call_result = socket_completed(self, made, addr entry, issued)
525        mut count: usize = usize(ended.moved)
526        if ended.code <> 0 then
527            --  A datagram longer than `into` is cut to it.
528            if ended.code == sockets.wsa_message_size
529              or ended.code == windows.error_more_data then
530                count = want
531            else
532                record(self, ended.code)
533                fail io.io_failed when ended.answer == failed
534                fail net.timed_out when ended.code == sockets.wsa_timed_out
535                fail io.io_failed
536            end if
537        end if
538        fail io.io_failed when count > lenof into
539        from_address: net.address = system.address_of(sender) else (problem)
540            _ = problem
541            record(self, sockets.wsa_family_unsupported)
542            fail io.io_failed
543        end
544        taken = (count: count, sender: from_address)
545    end events_receive_from
546
547    --  Cancel every call waiting on `made` in the directions shut, failing
548    --  each (D449).
549    shut: (self: host, made: windows.handle, input: bool, output: bool)
550          -> none =
551        state: ptr mut loop_state = self.state
552        mut at: usize = state.val.first
553        while at <> 0 do
554            here: ptr mut waiting = entry_at(at)
555            at = here.val.next
556            if here.val.handle == made and here.val.flight
557              and ((here.val.output and output)
558                   or (not here.val.output and input)) then
559                cut_short(here, failed)
560            end if
561        end while
562    end shut
563
564    --  Shutting a side wakes every fibre waiting on that side, failing its
565    --  call (D449).
566    events_shutdown: (self: host, connection: io.file,
567                      stopping: net.direction) -> none ! io.io_failed =
568        cleared(self)
569        made: windows.handle = system.handle_of(connection)
570        how: i32 = match stopping
571            net.receiving: sockets.sd_receive
572            net.sending: sockets.sd_send
573            net.both_directions: sockets.sd_both
574        end match
575        if sockets.shutdown(made, how) <> 0 then
576            record(self, last())
577            fail io.io_failed
578        end if
579        shut(self, made, stopping <> net.sending, stopping <> net.receiving)
580    end events_shutdown
581
582    --  The internet address of a socket's own end, or with `peer` of the
583    --  other end.
584    named: (self: host, socket: io.file, peer: bool) -> (at: net.address)
585           ! io.io_failed =
586        mut found: sockets.socket_address = (bytes: zeroed, length: 28)
587        status: i32 =
588            if peer then sockets.peer_name(system.handle_of(socket),
589                                           addr found.bytes[0],
590                                           addr found.length)
591            else sockets.socket_name(system.handle_of(socket),
592                                     addr found.bytes[0], addr found.length)
593            end if
594        if status <> 0 then
595            record(self, last())
596            fail io.io_failed
597        end if
598        at = system.address_of(found) else (problem)
599            _ = problem
600            record(self, sockets.wsa_family_unsupported)
601            fail io.io_failed
602        end
603    end named
604
605    events_local_address: (self: host, socket: io.file) -> (at: net.address)
606                          ! io.io_failed =
607        cleared(self)
608        at = try named(self, socket, false)
609    end events_local_address
610
611    events_peer_address: (self: host, connection: io.file)
612                         -> (at: net.address) ! io.io_failed =
613        cleared(self)
614        at = try named(self, connection, true)
615    end events_peer_address
616
617    --  The limits are kept on the socket, as hosted/blocking keeps them,
618    --  and each call reads them back as its deadline.
619    events_set_timeouts: (self: host, socket: io.file,
620                          receive_limit: time.duration,
621                          send_limit: time.duration)
622                         -> none ! io.io_failed =
623        cleared(self)
624        receiving: u32 = milliseconds_of(receive_limit)
625        sending: u32 = milliseconds_of(send_limit)
626        made: windows.handle = system.handle_of(socket)
627        if sockets.set_option(made, sockets.sol_socket,
628                              sockets.so_receive_timeout,
629                              ptr(usize(addr receiving)), 4) <> 0
630          or sockets.set_option(made, sockets.sol_socket,
631                                sockets.so_send_timeout,
632                                ptr(usize(addr sending)), 4) <> 0 then
633            record(self, last())
634            fail io.io_failed
635        end if
636    end events_set_timeouts
637
638    events_set_no_delay: (self: host, connection: io.file, on: bool)
639                         -> none ! io.io_failed =
640        cleared(self)
641        value: u32 = if on then 1 else 0 end if
642        if sockets.set_option(system.handle_of(connection),
643                              sockets.ipproto_tcp, sockets.tcp_no_delay,
644                              ptr(usize(addr value)), 4) <> 0 then
645            record(self, last())
646            fail io.io_failed
647        end if
648    end events_set_no_delay
649
650    --  Closing wakes every fibre waiting on the socket first (D449).
651    events_close_socket: (self: host, sink socket: io.file)
652                         -> none ! io.io_failed =
653        cleared(self)
654        closing: windows.handle = system.handle_of(socket)
655        forget(self, closing)
656        if sockets.close_socket(closing) <> 0 then
657            record(self, last())
658            fail io.io_failed
659        end if
660    end events_close_socket
661
662    host is net.network
663        (connect: events_connect, connect_within: events_connect_within,
664         listen: events_listen,
665         accept: events_accept, open_datagram: events_open_datagram,
666         send: events_send, receive: events_receive,
667         send_to: events_send_to,
668         receive_from: events_receive_from, shutdown: events_shutdown,
669         local_address: events_local_address,
670         peer_address: events_peer_address,
671         set_timeouts: events_set_timeouts,
672         set_no_delay: events_set_no_delay,
673         close: events_close_socket)
674end if