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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
let max_batch_limit = 64
external send_batch_raw :
Unix.file_descr -> Cstruct.t array -> int array -> int -> int -> int
= "caml_eio_net_sendmsg_batch"
external recv_batch_raw :
Unix.file_descr -> Cstruct.t array -> int array -> int -> int -> int
= "caml_eio_net_recvmsg_batch"
let eio f x =
try f x with Unix.Unix_error (e, fn, arg) -> raise (Eio_unix.Err.v e fn arg)
let use fd f = Eio_unix.Fd.use_exn "Eio_net_unix.Frame" fd f
let check_length ~max_length len =
if len > max_length then raise (Eio_net.Datagram.too_long ~max_length)
let last_received lengths =
let max_batch = Array.length lengths in
let rec received i =
if i < max_batch && lengths.(i) >= 0 then received (i + 1) else i - 1
in
received 1
let receive_rest ~failed fd buffers lengths =
match
use fd (fun fd ->
recv_batch_raw fd buffers lengths 1 (Array.length buffers))
with
| -> extra
| exception Unix.Unix_error (e, fn, arg) ->
failed := Some (Eio_unix.Err.v e fn arg);
last_received lengths
let receiver ~max_length ~max_batch socket fd =
let check = check_length ~max_length in
let recv_buf = Cstruct.create (max_length + 1) in
let recv_buffers =
Array.init max_batch (fun _ -> Cstruct.create (max_length + 1))
in
let recv_lengths = Array.make max_batch 0 in
let failed = ref None in
let raise_failed () =
Option.iter
(fun e ->
failed := None;
raise e)
!failed
in
let receive () =
raise_failed ();
let _source, len = Eio.Net.recv socket recv_buf in
check len;
len
in
let recv () = Cstruct.to_string ~len:(receive ()) recv_buf in
let recv_slice () =
Bytesrw.Bytes.Slice.of_bytes_or_eod
(Cstruct.to_bytes ~len:(receive ()) recv_buf)
in
let recv_batch () =
raise_failed ();
let _source, first_len = Eio.Net.recv socket recv_buffers.(0) in
recv_lengths.(0) <- first_len;
let = receive_rest ~failed fd recv_buffers recv_lengths in
for i = 0 to extra do
check recv_lengths.(i)
done;
List.init (extra + 1) (fun i ->
Cstruct.to_string ~len:recv_lengths.(i) recv_buffers.(i))
in
(recv, recv_slice, recv_batch)
let min_backoff = 50
let max_backoff = 10_000
let sender ~max_length ~max_batch fd =
let send_buffers =
Array.init max_batch (fun _ -> Cstruct.create max_length)
in
let send_lengths = Array.make max_batch 0 in
let rec copy_frames frames index =
match frames with
| [] -> (index, [])
| _ when index = max_batch -> (index, frames)
| frame :: rest ->
let len = Cstruct.lenv frame in
check_length ~max_length len;
ignore (Cstruct.fillv ~src:frame ~dst:send_buffers.(index));
send_lengths.(index) <- len;
copy_frames rest (index + 1)
in
let rec send_prepared ~backoff first count =
if first < count then
match
use fd
(eio (fun fd ->
send_batch_raw fd send_buffers send_lengths first count))
with
| 0 ->
use fd Eio_unix.await_writable;
send_prepared ~backoff:min_backoff first count
| -1 ->
Eio_unix.sleep (float backoff *. 1e-6);
send_prepared ~backoff:(min max_backoff (2 * backoff)) first count
| sent -> send_prepared ~backoff:min_backoff (first + sent) count
in
let rec send_batch = function
| [] -> ()
| frames ->
let count, rest = copy_frames frames 0 in
send_prepared ~backoff:min_backoff 0 count;
send_batch rest
in
send_batch
let of_connected_datagram_fd ?(max_length = 65536) ?(max_batch = 32) ~sw fd =
let fail fmt =
Fmt.invalid_arg ("Eio_net_unix.Frame.of_connected_datagram_fd: " ^^ fmt)
in
if max_length <= 0 then fail "max_length is %d, must be positive" max_length;
if max_batch <= 0 then fail "max_batch is %d, must be positive" max_batch;
if max_batch > max_batch_limit then
fail "max_batch is %d, must be at most %d" max_batch max_batch_limit;
eio Unix.set_nonblock fd;
let socket = Eio_unix.Net.import_socket_datagram ~sw ~close_unix:false fd in
let fd = Eio_unix.Resource.fd socket in
let recv, recv_slice, recv_batch =
receiver ~max_length ~max_batch socket fd
in
let send_batch = sender ~max_length ~max_batch fd in
let recv_lock = Eio.Mutex.create () and send_lock = Eio.Mutex.create () in
let recv () = Eio.Mutex.use_ro recv_lock recv in
let recv_slice () = Eio.Mutex.use_ro recv_lock recv_slice in
let recv_batch () = Eio.Mutex.use_ro recv_lock recv_batch in
let send_batch frames =
Eio.Mutex.use_ro send_lock (fun () -> send_batch frames)
in
Eio_net.Frame.v ~recv_batch ~recv_slice ~send_batch ~recv
~send:(fun frame -> send_batch [ frame ])
()