-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp_util.af
More file actions
63 lines (52 loc) · 1.68 KB
/
Copy pathhttp_util.af
File metadata and controls
63 lines (52 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use' lang.af
use' net.af
use' errno.af
use' pthread.af
\ Nice guide on libc networking: https://beej.us/guide/bgnet/html/
\ Single-threaded accept loop. Convenient for examples/tests.
fun: net_accept_loop { sock handler }
alloca' Sockaddr_storage { addr }
alloca' Cuint { size }
loop
addr size' Sockaddr_storage .memzero
size' Sockaddr_storage size !cuint
sock addr size .accept .cint_to_cell { conn }
if conn <0 .then
.errno { code }
if code EINTR = .then again end
code .strerror { msg }
" [srv] unable to accept connection; code: %zd; msg: %s" code msg .elogf .elf
again
end
" [srv] accepted connection" .elog .elf
conn handler .call
end
end
\ Multi-threaded accept loop. Spawns one detached worker per accepted conn.
\ The handler receives one input: `conn`. On spawn failure, closes `conn`.
\ No cancellation support. No thread limit.
fun: net_accept_loop_threaded { sock handler }
alloca' Sockaddr_storage { addr }
alloca' Cuint { size }
loop
addr size' Sockaddr_storage .memzero
size' Sockaddr_storage size !cuint
sock addr size .accept .cint_to_cell { conn }
if conn <0 .then
.errno { code }
if code EINTR = .then again end
code .strerror { msg }
" [srv] unable to accept connection; code: %zd; msg: %s" code msg .elogf .elf
again
end
" [srv] accepted connection" .elog .elf
handler conn .thread_spawn_detached { _ err }
if err .then
" [srv] unable to spawn worker: %s" err .elogf .elf
conn .sock_close { err }
if err .then
" [srv] unable to close connection %zd: %s" conn err .elogf .elf
end
end
end
end