-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlwt_rsocket.ml
More file actions
459 lines (384 loc) · 12.1 KB
/
lwt_rsocket.ml
File metadata and controls
459 lines (384 loc) · 12.1 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
let _log fmt =
(*let k s =
let t0 = Unix.gettimeofday () in
Printf.eprintf "%f %s\n%!" t0 s
in*)
let k s = () in
Printf.ksprintf k fmt
type lwt_rsocket = Lwt_unix.file_descr
let unix_fd_of lwt_rsocket = Lwt_unix.unix_file_descr lwt_rsocket
let rsocket_of lwt_rsocket =
let u = unix_fd_of lwt_rsocket in
let (r : Rsocket.rsocket) = Obj.magic u in
r
let identifier lwt_rsocket =
let u = unix_fd_of lwt_rsocket in
let (i:int) = Obj.magic u in
i
let wrap rsocket =
let (unix_fd : Unix.file_descr) = Obj.magic rsocket in
let lwt_fd = Lwt_unix.of_unix_file_descr unix_fd ~blocking:false in
(lwt_fd : lwt_rsocket)
let socket domain typ proto =
let rsocket = Rsocket.rsocket domain typ proto in
let () = Rsocket.set_nonblock rsocket in
wrap rsocket
let show lwt_rsocket =
let rs = rsocket_of lwt_rsocket in
Rsocket.show rs
open Lwt.Infix
let string_of_address = function
| Unix.ADDR_INET(addr, port) ->
Printf.sprintf
"ADDR_INET(%s,%i)"
(Unix.string_of_inet_addr addr) port
| Unix.ADDR_UNIX s -> Printf.sprintf "ADDR_UNIX %s" s
type state = Lwt_unix.state = Opened | Closed | Aborted of exn
(* this is a copy of Lwt_unix.file_descr *)
type rfile_descr = (* Lwt_unix.file_descr = *) {
fd : Unix.file_descr;
mutable state: state;
mutable set_flags : bool;
mutable blocking : bool Lwt.t Lazy.t;
mutable event_readable : Lwt_engine.event option;
mutable event_writable : Lwt_engine.event option;
hooks_readable : (unit -> unit) Lwt_sequence.t;
hooks_writable : (unit -> unit) Lwt_sequence.t;
}
let set_state rch state =
rch.state <- state
let _rch2ch (rch: rfile_descr) =
let (ch : Lwt_unix.file_descr) = Obj.magic rch in
ch
let _ch2rch (ch: Lwt_unix.file_descr) =
let (rch : rfile_descr) = Obj.magic ch in
rch
let rec _check_descriptor rch =
let id = identifier (_rch2ch rch) in
let () = _log "(%i) check_descriptor" id in
match rch.state with
| Opened ->
let () = _log "(%i) check_descriptor => Opened" id in
()
| Aborted e ->
raise e
| Closed ->
raise (Unix.Unix_error (Unix.EBADF, "check_descriptor", ""))
let clear_events rch =
Lwt_sequence.iter_node_l (fun node -> Lwt_sequence.remove node; Lwt_sequence.get node ()) rch.hooks_readable;
Lwt_sequence.iter_node_l (fun node -> Lwt_sequence.remove node; Lwt_sequence.get node ()) rch.hooks_writable;
begin
match rch.event_readable with
| Some ev ->
rch.event_readable <- None;
Lwt_engine.stop_event ev
| None ->
()
end;
begin
match rch.event_writable with
| Some ev ->
rch.event_writable <- None;
Lwt_engine.stop_event ev
| None ->
()
end
let close lwt_rsocket =
let (rch : rfile_descr) = Obj.magic lwt_rsocket in
if rch.state = Closed then _check_descriptor rch;
set_state rch Closed;
clear_events rch;
(* TODO: via job *)
Lwt.return (Rsocket.rclose (rsocket_of lwt_rsocket))
let bind lwt_rsocket socketaddr =
let rsocket = rsocket_of lwt_rsocket in
Rsocket.rbind rsocket socketaddr
let listen lwt_rsocket n =
let rsocket = rsocket_of lwt_rsocket in
Rsocket.rlisten rsocket n
let setsockopt lwt_rsocket sockopt value =
let rsocket = rsocket_of lwt_rsocket in
Rsocket.rsetsockopt rsocket sockopt value
external _ordma_lwt_unix_recv :
Unix.file_descr ->
Bytes.t -> int -> int -> Unix.msg_flag list -> int =
"ordma_lwt_unix_recv"
external _ordma_lwt_unix_send :
Unix.file_descr -> Bytes.t -> int -> int -> Unix.msg_flag list -> int =
"ordma_lwt_unix_send"
external _unix_stub_readable :
Unix.file_descr -> bool =
"ordma_lwt_unix_readable"
external _unix_stub_writable :
Unix.file_descr -> bool =
"ordma_lwt_unix_writable"
let rec _unix_readable fd =
try
_unix_stub_readable fd
with Unix.Unix_error (Unix.EINTR, _, _) ->
_unix_readable fd
let _readable (rch: rfile_descr) =
(* from Lwt_unix.ml *)
_check_descriptor rch;
_unix_readable rch.fd
let rec _unix_writable fd =
try
_unix_stub_writable fd
with Unix.Unix_error (Unix.EINTR, _, _) ->
_unix_writable fd
let _writable rch =
_check_descriptor rch;
_unix_writable rch.fd
let _wait_read (rch: rfile_descr) =
(* from Lwt_unix.ml *)
Lwt.catch
(fun () ->
if _readable rch
then Lwt.return_unit
else
let ch = _rch2ch rch in
Lwt_unix.register_action Lwt_unix.Read ch ignore)
Lwt.fail
let _wait_write (rch:rfile_descr) =
Lwt.catch
(fun () ->
if _writable rch
then Lwt.return_unit
else
let ch = _rch2ch rch in
Lwt_unix.register_action Lwt_unix.Write ch ignore)
Lwt.fail
(* Wraps a system call *)
let _wrap_syscall event rch action =
_check_descriptor rch;
Lazy.force rch.blocking >>= fun blocking ->
let ch = _rch2ch rch in
let id = identifier ch in
try
if not blocking
|| (event = Lwt_unix.Read && _unix_readable rch.fd)
|| (event = Lwt_unix.Write && _unix_writable rch.fd)
then
let () = _log "(%i) immediate action" id in
let r = action () in
let () = _log "(%i) immediate action done" id
in
Lwt.return r
else
let () = _log "(%i) register action" id in
Lwt_unix.register_action event ch action
with
| Lwt_unix.Retry
| Unix.Unix_error((Unix.EAGAIN | Unix.EWOULDBLOCK | Unix.EINTR), _, _)
| Sys_blocked_io as e ->
let () = _log "(%i) %s => register action anyway" id (Printexc.to_string e) in
(* The action could not be completed immediately, register it: *)
Lwt_unix.register_action event ch action
| Lwt_unix.Retry_read ->
Lwt_unix.register_action Lwt_unix.Read ch action
| Lwt_unix.Retry_write ->
Lwt_unix.register_action Lwt_unix.Write ch action
| e ->
Lwt.fail e
let connect lwt_rsocket socketaddr =
(* from Lwt_unix.ml : *)
let in_progress = ref false in
let rsocket = rsocket_of lwt_rsocket in
_wrap_syscall
Lwt_unix.Write
(_ch2rch lwt_rsocket)
begin fun () ->
if !in_progress
then
(* If the connection is in progress, [getsockopt_error] tells
wether it succceed: *)
match Rsocket.rgetsockopt_error rsocket with
| None ->
(* The socket is connected *)
()
| Some err ->
(* An error happened: *)
raise (Unix.Unix_error(err, "connect", ""))
else
try
(* We should pass only one time here, unless the system call
is interrupted by a signal: *)
Rsocket.rconnect rsocket socketaddr
with
| Unix.Unix_error (Unix.EINPROGRESS, _, _) ->
in_progress := true;
raise Lwt_unix.Retry
end
let send lwt_rsocket buffer offset len flags =
if offset < 0 || len < 0 || offset > Bytes.length buffer - len then
invalid_arg "Lwt_unix.send"
else
_wrap_syscall
Lwt_unix.Write
(_ch2rch lwt_rsocket)
(fun () ->
let rsocket = rsocket_of lwt_rsocket in
Rsocket.rsend rsocket buffer offset len flags)
let recv (lwt_rsocket: lwt_rsocket) buffer offset len flags =
let unix_fd = unix_fd_of lwt_rsocket in
if offset < 0
|| len < 0
|| offset > Bytes.length buffer - len
then
invalid_arg "Lwt_socket.recv"
else
_wrap_syscall
Lwt_unix.Read
(_ch2rch lwt_rsocket)
(fun () ->
let id = identifier lwt_rsocket in
let () = _log "(%i) inside recv action" id in
_ordma_lwt_unix_recv unix_fd buffer offset len flags
)
>>= fun r ->
(* TODO: This is a symptom of something else? *)
if r = 0
then Lwt.fail End_of_file
else Lwt.return r
let accept lwt_rsocket =
(*
(* from Lwt_unix.ml : *)
let accept ch =
wrap_syscall Read ch (fun _ -> let (fd, addr) =
Unix.accept ch.fd in (mk_ch ~blocking:false fd, addr))
*)
(*Lwt_preemptive.detach
(fun () ->
let rsocket = rsocket_of lwt_rsocket in
let client_s,client_addr = Rsocket.raccept rsocket in
let client_rs = wrap client_s in
client_rs,client_addr
)
()
*)
let inner () =
Lwt.catch
(fun () ->
(*
_wait_read lwt_rsocket >>= fun () ->
let id = identifier lwt_rsocket in
Lwt_io.printlf "(%i) ready for read" id >>= fun () ->
let rsocket = rsocket_of lwt_rsocket in
let client_s,client_addr = Rsocket.raccept rsocket in
let client_rs = wrap client_s in
let r = client_rs,client_addr in
Lwt.return (Some r)
*)
_wrap_syscall
Lwt_unix.Read
(_ch2rch lwt_rsocket)
(fun () ->
let (fd, addr) = Rsocket.raccept (rsocket_of lwt_rsocket) in
let () = Rsocket.set_nonblock fd in
Some (wrap fd, addr)
)
)
(fun exn ->
Lwt_log.warning ~exn "failed raccept" >>= fun () ->
Lwt.return_none)
in
let rec loop i =
inner () >>= function
| None ->
(* Todo: don't ignore these symptoms *)
if i > 1000
then
begin
Lwt_log.fatal "Lwt_rsocket.accept : too many things went wrong" >>= fun ()->
Lwt.return (assert false)
end
else
Lwt_unix.sleep 0.1 >>= fun () ->
Lwt_log.info_f "Lwt_rsocket.accept: i = %i" i >>= fun () ->
loop (i+1)
| Some r -> Lwt.return r
in
loop 0
(*
Lwt_unix.wait_read lwt_rsocket >>= fun () ->
let rsocket = rsocket_of lwt_rsocket in
let client_s,client_addr = Rsocket.raccept rsocket in
let client_rs = wrap client_s in
let r = client_rs,client_addr in
Lwt.return r
*)
open Bigarray
type ba = (char, int8_unsigned_elt, c_layout) Array1.t
external _ordma_lwt_unix_bytes_recv:
Unix.file_descr ->
ba -> int -> int -> Unix.msg_flag list -> int =
"ordma_lwt_unix_bytes_recv"
external _ordma_lwt_unix_bytes_send :
Unix.file_descr -> ba -> int -> int -> Unix.msg_flag list -> int =
"ordma_lwt_unix_bytes_send"
module Bytes = struct
type t = ba
let create len =
Array1.create Char C_layout len
let send lwt_rsocket t pos len flags =
if pos < 0
|| len < 0
|| pos > Array1.dim t - len
then
invalid_arg "send"
else
(*let id = identifier lwt_rsocket in*)
let unix_fd = unix_fd_of lwt_rsocket in
_wrap_syscall
Lwt_unix.Write
(_ch2rch lwt_rsocket)
(fun () -> _ordma_lwt_unix_bytes_send unix_fd t pos len flags)
let recv lwt_rsocket buffer offset len flags =
if offset < 0
|| len < 0
|| offset > Array1.dim buffer - len
then
invalid_arg "Lwt_rsocket.recv"
else
let id = identifier lwt_rsocket in
let unix_fd = unix_fd_of lwt_rsocket in
let () = _log "(%i) Bytes.recv _ %i %i\n%!" id offset len in
_wrap_syscall
Lwt_unix.Read
(_ch2rch lwt_rsocket)
(fun () -> _ordma_lwt_unix_bytes_recv unix_fd buffer offset len flags)
>>= fun r ->
if r = 0
then Lwt.fail End_of_file
else Lwt.return r
end
open Lwt_engine
external _rselect_select :
Unix.file_descr list ->
Unix.file_descr list ->
Unix.file_descr list ->
float ->
Unix.file_descr list * Unix.file_descr list = "ordma_rselect_select"
let fds2s fds =
let i_of fd : int = Obj.magic fd in
let s_of fd = i_of fd |> string_of_int in
String.concat ";" (List.map s_of fds)
(* rselect is good enough as it ends up
into rpoll anyway *)
class rselect = object
inherit select_based
method private
select :
Unix.file_descr list ->
Unix.file_descr list ->
float -> Unix.file_descr list * Unix.file_descr list
= fun fds_r fds_w timeout ->
(*let () = _log "rselect.select: ([%s],[%s],%f) begin" (fds2s fds_r) (fds2s fds_w) timeout
in*)
let r = _rselect_select fds_r fds_w [] timeout in
(*
let rr,rw = r in
let () = _log "rselect.select => ([%s],[%s]) end" (fds2s rr) (fds2s rw)
in *)
r
end