-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudi.ml
1491 lines (1411 loc) · 45.9 KB
/
cloudi.ml
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*-*-Mode:ocaml;coding:utf-8;tab-width:2;c-basic-offset:2;indent-tabs-mode:()-*-
ex: set ft=ocaml fenc=utf-8 sts=2 ts=2 sw=2 et nomod: *)
(*
MIT License
Copyright (c) 2017-2025 Michael Truog <mjtruog at protonmail dot com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*)
let message_init = 1
let message_send_async = 2
let message_send_sync = 3
let message_recv_async = 4
let message_return_async = 5
let message_return_sync = 6
let message_returns_async = 7
let message_keepalive = 8
let message_reinit = 9
let message_subscribe_count = 10
let message_term = 11
type request_type =
ASYNC
| SYNC
type source = Erlang.Pid.t
type response =
Response of string
| ResponseInfo of string * string
| Forward of string * string * string
| Forward_ of string * string * string * int * int
| Null
| NullError of string
type callback_result =
ReturnI of string * string
| ForwardI of string * string * string * int * int
| Finished
module Instance = struct
type 's t = {
state : 's;
terminate_exception : bool;
socket : Unix.file_descr;
use_header : bool;
mutable initialization_complete : bool;
mutable fatal_exceptions : bool;
mutable terminate : bool;
fragment_size : int;
fragment_recv : bytes;
callbacks : (string, (
request_type ->
string -> string ->
string -> string ->
int -> int -> string -> source ->
's -> 's t ->
response) Queue.t) Hashtbl.t;
buffer_recv : Buffer.t;
mutable process_index : int;
mutable process_count : int;
mutable process_count_max : int;
mutable process_count_min : int;
mutable prefix : string;
mutable timeout_initialize : int;
mutable timeout_async : int;
mutable timeout_sync : int;
mutable timeout_terminate : int;
mutable priority_default : int;
mutable response_info : string;
mutable response : string;
mutable trans_id : string;
mutable trans_ids : string array;
mutable subscribe_count : int;
}
let make ~state ~terminate_exception
~socket ~use_header ~fragment_size ~fragment_recv
~callbacks ~buffer_recv ~timeout_terminate =
{state; terminate_exception; socket; use_header;
initialization_complete = false;
fatal_exceptions = false;
terminate = false;
fragment_size; fragment_recv; callbacks; buffer_recv;
process_index = 0;
process_count = 0;
process_count_max = 0;
process_count_min = 0;
prefix = "";
timeout_initialize = 0;
timeout_async = 0;
timeout_sync = 0;
timeout_terminate;
priority_default = 0;
response_info = "";
response = "";
trans_id = "";
trans_ids = Array.make 0 "";
subscribe_count = 0}
let init api process_index process_count
process_count_max process_count_min prefix
timeout_initialize timeout_async timeout_sync timeout_terminate
priority_default fatal_exceptions =
api.process_index <- process_index ;
api.process_count <- process_count ;
api.process_count_max <- process_count_max ;
api.process_count_min <- process_count_min ;
api.prefix <- prefix ;
api.timeout_initialize <- timeout_initialize ;
api.timeout_async <- timeout_async ;
api.timeout_sync <- timeout_sync ;
api.timeout_terminate <- timeout_terminate ;
api.priority_default <- priority_default ;
api.fatal_exceptions <- fatal_exceptions ;
()
let reinit api process_count timeout_async timeout_sync
priority_default fatal_exceptions =
api.process_count <- process_count ;
api.timeout_async <- timeout_async ;
api.timeout_sync <- timeout_sync ;
api.priority_default <- priority_default ;
api.fatal_exceptions <- fatal_exceptions ;
()
let set_response api response_info response trans_id =
api.response_info <- response_info ;
api.response <- response ;
api.trans_id <- trans_id ;
()
let set_trans_id api trans_id =
api.trans_id <- trans_id ;
()
let set_trans_ids api trans_ids_str trans_id_count =
api.trans_ids <- Array.init trans_id_count (fun i ->
String.sub trans_ids_str (i * 16) 16
) ;
()
let set_subscribe_count api count =
api.subscribe_count <- count ;
()
let callbacks_add api pattern f =
let key = api.prefix ^ pattern in
let value = try Hashtbl.find api.callbacks key
with Not_found -> (
let value_new = Queue.create () in
Hashtbl.add api.callbacks key value_new ;
value_new)
in
Queue.push f value ;
()
let callbacks_remove api pattern =
let key = api.prefix ^ pattern in
let value = Hashtbl.find api.callbacks key in
let _f = Queue.pop value in
if Queue.is_empty value then
Hashtbl.remove api.callbacks key ;
()
end
type 's callback = (
request_type ->
string -> string ->
string -> string ->
int -> int -> string -> source ->
's -> 's Instance.t ->
response)
let trans_id_null = String.make 16 '\x00'
let invalid_input_error = "Invalid Input"
let message_decoding_error = "Message Decoding Error"
let terminate_error = "Terminate"
exception ReturnSync
exception ReturnAsync
exception ForwardSync
exception ForwardAsync
exception Terminate
exception FatalError
let print_exception str =
prerr_endline ("Exception: " ^ str)
let print_error str =
prerr_endline ("Error: " ^ str)
let str_replace input output =
Str.global_replace (Str.regexp_string input) output
let str_split_on_char sep str =
(* based on https://github.com/ocaml/ocaml/blob/trunk/stdlib/string.ml
* (split_on_char) for use with OCaml 4.03.0
*)
let r = ref [] in
let j = ref (String.length str) in
for i = String.length str - 1 downto 0 do
if str.[i] = sep then begin
r := String.sub str (i + 1) (!j - i - 1) :: !r;
j := i
end
done;
String.sub str 0 !j :: !r
let list_append l1 l2 = List.rev_append (List.rev l1) l2
let backtrace (e : exn) : string =
let indent = " " in
(Printexc.to_string e) ^ "\n" ^ indent ^
(String.trim (str_replace "\n" ("\n" ^ indent) (Printexc.get_backtrace ())))
let null_response _ _ _ _ _ _ _ _ _ _ _ =
Null
let getenv (name : string) : string =
try Sys.getenv name
with Not_found -> ""
let getenv_to_uint (name : string) : (int, string) result =
let value = try int_of_string (Sys.getenv name)
with _ -> -1
in
if value < 0 then
Error (invalid_input_error)
else
Ok (value)
let fd_of_int (fd: int) : Unix.file_descr = Obj.magic fd
let unpack_uint32_native i binary : (int, string) result =
let byte0 = int_of_char binary.[i + (if Sys.big_endian then 0 else 3)]
and byte1 = int_of_char binary.[i + (if Sys.big_endian then 1 else 2)]
and byte2 = int_of_char binary.[i + (if Sys.big_endian then 2 else 1)]
and byte3 = int_of_char binary.[i + (if Sys.big_endian then 3 else 0)] in
if byte0 > max_int lsr 24 then
(* 32 bit system *)
Error ("ocaml int overflow")
else
Ok (
(byte0 lsl 24) lor (
(byte1 lsl 16) lor (
(byte2 lsl 8) lor byte3
)
)
)
let unpack_int32_native i binary : (int, string) result =
let byte0 = int_of_char binary.[i + (if Sys.big_endian then 0 else 3)]
and byte1 = int_of_char binary.[i + (if Sys.big_endian then 1 else 2)]
and byte2 = int_of_char binary.[i + (if Sys.big_endian then 2 else 1)]
and byte3 = int_of_char binary.[i + (if Sys.big_endian then 3 else 0)] in
let byte0u = 0x7f land byte0 in
let byte0s = if (byte0 lsr 7) = 1 then
-128 + byte0u
else
byte0u in
if byte0u > max_int lsr 24 then
(* 32 bit system *)
Error ("ocaml int overflow")
else
Ok (
(byte0s lsl 24) lor (
(byte1 lsl 16) lor (
(byte2 lsl 8) lor byte3
)
)
)
let unpack_uint32_big i binary : (int, string) result =
let byte0 = int_of_char binary.[i]
and byte1 = int_of_char binary.[i + 1]
and byte2 = int_of_char binary.[i + 2]
and byte3 = int_of_char binary.[i + 3] in
if byte0 > max_int lsr 24 then
(* 32 bit system *)
Error ("ocaml int overflow")
else
Ok (
(byte0 lsl 24) lor (
(byte1 lsl 16) lor (
(byte2 lsl 8) lor byte3
)
)
)
let unpack_uint8 i binary : int =
int_of_char binary.[i]
let unpack_int8 i binary : int =
let byte0 = int_of_char binary.[i] in
if (byte0 lsr 7) = 1 then
-128 + (0x7f land byte0)
else
byte0
let pack_uint32_big (value : int) buffer : unit =
let byte0 = (value lsr 24) land 0xff
and byte1 = (value lsr 16) land 0xff
and byte2 = (value lsr 8) land 0xff
and byte3 = value land 0xff in
Buffer.add_char buffer (char_of_int byte0) ;
Buffer.add_char buffer (char_of_int byte1) ;
Buffer.add_char buffer (char_of_int byte2) ;
Buffer.add_char buffer (char_of_int byte3)
let send {Instance.socket; use_header; _} data : (unit, string) result =
let length = String.length data in
let sent = if not use_header then
(Unix.write_substring socket data 0 length) = length
else (
let total = 4 + length in
let buffer = Buffer.create total in
pack_uint32_big length buffer ;
Buffer.add_string buffer data ;
(Unix.write socket (Buffer.to_bytes buffer) 0 total) = total)
in
if sent then
Ok (())
else
Error ("send failed")
let recv api : (string * int, string) result =
let {Instance.socket; use_header;
fragment_size; fragment_recv; buffer_recv; _} = api in
if use_header then (
let rec get_header () =
if (Buffer.length buffer_recv) >= 4 then
unpack_uint32_big 0 (Buffer.sub buffer_recv 0 4)
else
let i = Unix.read socket fragment_recv 0 fragment_size in
if i = 0 then
Error ("recv failed")
else (
Buffer.add_subbytes buffer_recv fragment_recv 0 i ;
get_header ())
in
let rec get_body (total : int) =
if (Buffer.length buffer_recv) >= total then (
let data = (Buffer.sub buffer_recv 4 (total - 4))
and data_remaining =
Buffer.sub buffer_recv total ((Buffer.length buffer_recv) - total) in
Buffer.clear buffer_recv ;
Buffer.add_string buffer_recv data_remaining ;
Ok (data))
else
let i = Unix.read socket fragment_recv 0 fragment_size in
if i = 0 then
Error ("recv failed")
else (
Buffer.add_subbytes buffer_recv fragment_recv 0 i ;
get_body total)
in
match get_header () with
| Error (error) ->
Error (error)
| Ok (length) ->
match get_body (4 + length) with
| Error (error) ->
Error (error)
| Ok (data) ->
Ok ((data, length)))
else (
let rec get_body () =
let i = Unix.read socket fragment_recv 0 fragment_size in
Buffer.add_subbytes buffer_recv fragment_recv 0 i ;
let ready = if i = fragment_size then
let (reading, _, _) = Unix.select [socket] [] [] 0.0 in
(List.length reading) > 0
else
false
in
if ready then
get_body ()
else (
let data_all = Buffer.contents buffer_recv in
Buffer.clear buffer_recv ;
data_all)
in
let data = get_body () in
if (String.length data) = 0 then
Error ("recv failed")
else
Ok ((data, String.length data)))
let forward_async_i
api name request_info request timeout priority trans_id source :
(unit, string) result =
match Erlang.term_to_binary (
Erlang.OtpErlangTuple ([
Erlang.OtpErlangAtom ("forward_async");
Erlang.OtpErlangString (name);
Erlang.OtpErlangBinary (request_info);
Erlang.OtpErlangBinary (request);
Erlang.OtpErlangInteger (timeout);
Erlang.OtpErlangInteger (priority);
Erlang.OtpErlangBinary (trans_id);
Erlang.OtpErlangPid (source)])) with
| Error (error) ->
Error (error)
| Ok (forward) ->
send api forward
let forward_async
api name request_info request timeout priority trans_id source :
(unit, string) result =
match forward_async_i
api name request_info request timeout priority trans_id source with
| Error (error) ->
Error (error)
| Ok _ ->
raise ForwardAsync
let forward_sync_i
api name request_info request timeout priority trans_id source :
(unit, string) result =
match Erlang.term_to_binary (
Erlang.OtpErlangTuple ([
Erlang.OtpErlangAtom ("forward_sync");
Erlang.OtpErlangString (name);
Erlang.OtpErlangBinary (request_info);
Erlang.OtpErlangBinary (request);
Erlang.OtpErlangInteger (timeout);
Erlang.OtpErlangInteger (priority);
Erlang.OtpErlangBinary (trans_id);
Erlang.OtpErlangPid (source)])) with
| Error (error) ->
Error (error)
| Ok (forward) ->
send api forward
let forward_sync
api name request_info request timeout priority trans_id source :
(unit, string) result =
match forward_sync_i
api name request_info request timeout priority trans_id source with
| Error (error) ->
Error (error)
| Ok _ ->
raise ForwardSync
let forward_
api request_type name request_info request timeout priority trans_id source :
(unit, string) result =
match request_type with
| ASYNC ->
forward_async api name request_info request
timeout priority trans_id source
| SYNC ->
forward_sync api name request_info request
timeout priority trans_id source
let return_async_i
api name pattern response_info response timeout trans_id source :
(unit, string) result =
match Erlang.term_to_binary (
Erlang.OtpErlangTuple ([
Erlang.OtpErlangAtom ("return_async");
Erlang.OtpErlangString (name);
Erlang.OtpErlangString (pattern);
Erlang.OtpErlangBinary (response_info);
Erlang.OtpErlangBinary (response);
Erlang.OtpErlangInteger (timeout);
Erlang.OtpErlangBinary (trans_id);
Erlang.OtpErlangPid (source)])) with
| Error (error) ->
Error (error)
| Ok (return) ->
send api return
let return_async
api name pattern response_info response timeout trans_id source :
(unit, string) result =
match return_async_i
api name pattern response_info response timeout trans_id source with
| Error (error) ->
Error (error)
| Ok _ ->
raise ReturnAsync
let return_sync_i
api name pattern response_info response timeout trans_id source :
(unit, string) result =
match Erlang.term_to_binary (
Erlang.OtpErlangTuple ([
Erlang.OtpErlangAtom ("return_sync");
Erlang.OtpErlangString (name);
Erlang.OtpErlangString (pattern);
Erlang.OtpErlangBinary (response_info);
Erlang.OtpErlangBinary (response);
Erlang.OtpErlangInteger (timeout);
Erlang.OtpErlangBinary (trans_id);
Erlang.OtpErlangPid (source)])) with
| Error (error) ->
Error (error)
| Ok (return) ->
send api return
let return_sync
api name pattern response_info response timeout trans_id source :
(unit, string) result =
match return_sync_i
api name pattern response_info response timeout trans_id source with
| Error (error) ->
Error (error)
| Ok _ ->
raise ReturnSync
let return_
api request_type name pattern response_info response timeout trans_id source :
(unit, string) result =
match request_type with
| ASYNC ->
return_async api name pattern response_info response
timeout trans_id source
| SYNC ->
return_sync api name pattern response_info response
timeout trans_id source
let handle_events api ext data data_size i cmd : (bool, string) result =
let i_cmd =
if cmd = 0 then
match unpack_uint32_native i data with
| Error (error) ->
Error (error)
| Ok (value) ->
Ok ((i + 4, value))
else
Ok ((i, cmd))
in
match i_cmd with
| Error (error) ->
Error (error)
| Ok ((i0, cmd_value)) ->
let rec loop i1 cmd_event =
if cmd_event = message_term then (
api.Instance.terminate <- true ;
if ext then
Ok (false)
else
Error (terminate_error))
else if cmd_event = message_reinit then (
match unpack_uint32_native i1 data with
| Error (error) ->
Error (error)
| Ok (process_count) ->
match unpack_uint32_native (i1 + 4) data with
| Error (error) ->
Error (error)
| Ok (timeout_async) ->
match unpack_uint32_native (i1 + 8) data with
| Error (error) ->
Error (error)
| Ok (timeout_sync) ->
let priority_default = unpack_int8 (i1 + 12) data
and fatal_exceptions = (unpack_uint8 (i1 + 13) data) != 0
and i2 = i1 + 14 in
Instance.reinit api
process_count
timeout_async
timeout_sync
priority_default
fatal_exceptions ;
loop_cmd i2)
else if cmd_event = message_keepalive then
match Erlang.term_to_binary (Erlang.OtpErlangAtom ("keepalive")) with
| Error (error) ->
Error (error)
| Ok (keepalive) ->
match send api keepalive with
| Error (error) ->
Error (error)
| Ok _ ->
loop_cmd i1
else
Error (message_decoding_error)
and loop_cmd i1 =
if i1 > data_size then
Error (message_decoding_error)
else if i1 = data_size then
Ok (true)
else
match unpack_uint32_native i1 data with
| Error (error) ->
Error (error)
| Ok (cmd_next) ->
loop (i1 + 4) cmd_next
in
loop i0 cmd_value
let rec poll_request_loop api timeout ext poll_timer: (bool, string) result =
let {Instance.socket; buffer_recv; _} = api
and timeout_value =
if timeout < 0 then
-1.0
else if timeout = 0 then
0.0
else
(float_of_int timeout) *. 0.001
in
let (reading, _, excepting) =
if (Buffer.length buffer_recv) > 0 then
([socket], [], [])
else
Unix.select [socket] [] [socket] timeout_value
in
if (List.length excepting) > 0 then
Ok (false)
else if (List.length reading) = 0 then
Ok (true)
else
match recv api with
| Error (error) ->
Error (error)
| Ok (data, data_size) ->
match poll_request_data api ext data data_size 0 with
| Error (error) ->
Error (error)
| Ok (Some value) ->
Ok (value)
| Ok (None) ->
let poll_timer_new =
if timeout > 0 then
Unix.gettimeofday ()
else
0.0
in
let timeout_new =
if timeout > 0 then
let elapsed = truncate
((poll_timer_new -. poll_timer) *. 1000.0) in
if elapsed <= 0 then
timeout
else if elapsed >= timeout then
0
else
timeout - elapsed
else
timeout
in
if timeout = 0 then
Ok (true)
else
poll_request_loop api timeout_new ext poll_timer_new
and callback
api request_type name pattern request_info request
timeout priority trans_id source : (bool option, string) result =
let {Instance.fatal_exceptions; state; callbacks; _} = api in
let callback_get () =
let function_queue = Hashtbl.find callbacks pattern in
let f = Queue.pop function_queue in
Queue.push f function_queue ;
f
in
let callback_f =
try callback_get ()
with Not_found -> null_response
in
let callback_result_value =
match request_type with
| ASYNC -> (
try Some (
callback_f
request_type name pattern request_info request
timeout priority trans_id source state api)
with
| Terminate ->
Some (Null)
| ReturnAsync ->
None
| ReturnSync ->
api.Instance.terminate <- true ;
print_exception "Synchronous Call Return Invalid" ;
None
| ForwardAsync ->
None
| ForwardSync ->
api.Instance.terminate <- true ;
print_exception "Synchronous Call Forward Invalid" ;
None
| e ->
print_exception (backtrace e) ;
match e with
| Assert_failure _ ->
exit 1 ;
| FatalError ->
exit 1 ;
| _ when fatal_exceptions ->
exit 1 ;
| _ ->
Some (Null))
| SYNC -> (
try Some (
callback_f
request_type name pattern request_info request
timeout priority trans_id source state api)
with
| Terminate ->
Some (Null)
| ReturnSync ->
None
| ReturnAsync ->
api.Instance.terminate <- true ;
print_exception "Asynchronous Call Return Invalid" ;
None
| ForwardSync ->
None
| ForwardAsync ->
api.Instance.terminate <- true ;
print_exception "Asynchronous Call Forward Invalid" ;
None
| e ->
print_exception (backtrace e) ;
match e with
| Assert_failure _ ->
exit 1 ;
| FatalError ->
exit 1 ;
| _ when fatal_exceptions ->
exit 1 ;
| _ ->
Some (Null))
in
let callback_result_type =
match callback_result_value with
| Some (ResponseInfo (v0, v1)) ->
ReturnI (v0, v1)
| Some (Response (v0)) ->
ReturnI ("", v0)
| Some (Forward (v0, v1, v2)) ->
ForwardI (v0, v1, v2, timeout, priority)
| Some (Forward_ (v0, v1, v2, v3, v4)) ->
ForwardI (v0, v1, v2, v3, v4)
| Some (Null) ->
ReturnI ("", "")
| Some (NullError (error)) ->
print_error error ;
ReturnI ("", "")
| None ->
Finished
in
let return_result =
match request_type with
| ASYNC -> (
match callback_result_type with
| Finished ->
Ok (())
| ReturnI (response_info, response) ->
return_async_i
api name pattern response_info response timeout trans_id source
| ForwardI (name_, request_info_, request_, timeout_, priority_) ->
forward_async_i
api name_ request_info_ request_ timeout_ priority_ trans_id source)
| SYNC -> (
match callback_result_type with
| Finished ->
Ok (())
| ReturnI (response_info, response) ->
return_sync_i
api name pattern response_info response timeout trans_id source
| ForwardI (name_, request_info_, request_, timeout_, priority_) ->
forward_sync_i
api name_ request_info_ request_ timeout_ priority_ trans_id source)
in
match return_result with
| Error (error) ->
Error (error)
| Ok _ ->
Ok (None)
and poll_request_data api ext data data_size i : (bool option, string) result =
match unpack_uint32_native i data with
| Error (error) ->
Error (error)
| Ok (cmd) ->
if cmd = message_init then
match unpack_uint32_native (i + 4) data with
| Error (error) ->
Error (error)
| Ok (process_index) ->
match unpack_uint32_native (i + 8) data with
| Error (error) ->
Error (error)
| Ok (process_count) ->
match unpack_uint32_native (i + 12) data with
| Error (error) ->
Error (error)
| Ok (process_count_max) ->
match unpack_uint32_native (i + 16) data with
| Error (error) ->
Error (error)
| Ok (process_count_min) ->
match unpack_uint32_native (i + 20) data with
| Error (error) ->
Error (error)
| Ok (prefix_size) ->
let i0 = i + 24 in
let prefix = String.sub data i0 (prefix_size - 1)
and i1 = i0 + prefix_size in
match unpack_uint32_native i1 data with
| Error (error) ->
Error (error)
| Ok (timeout_initialize) ->
match unpack_uint32_native (i1 + 4) data with
| Error (error) ->
Error (error)
| Ok (timeout_async) ->
match unpack_uint32_native (i1 + 8) data with
| Error (error) ->
Error (error)
| Ok (timeout_sync) ->
match unpack_uint32_native (i1 + 12) data with
| Error (error) ->
Error (error)
| Ok (timeout_terminate) ->
let priority_default =
unpack_int8 (i1 + 16) data
and fatal_exceptions =
(unpack_uint8 (i1 + 17) data) != 0 in
match unpack_int32_native (i1 + 18) data with
| Error (error) ->
Error (error)
| Ok (bind) ->
if bind >= 0 then
Error (invalid_input_error)
else
let i2 = i1 + 22 in
Instance.init api
process_index
process_count
process_count_max
process_count_min
prefix
timeout_initialize
timeout_async
timeout_sync
timeout_terminate
priority_default
fatal_exceptions ;
if i2 <> data_size then
match handle_events
api ext data data_size i2 0 with
| Error (error) ->
Error (error)
| Ok _ ->
Ok (Some false)
else
Ok (Some false)
else if cmd = message_send_async || cmd = message_send_sync then
match unpack_uint32_native (i + 4) data with
| Error (error) ->
Error (error)
| Ok (name_size) ->
let i0 = i + 8 in
let name = String.sub data i0 (name_size - 1)
and i1 = i0 + name_size in
match unpack_uint32_native i1 data with
| Error (error) ->
Error (error)
| Ok (pattern_size) ->
let i2 = i1 + 4 in
let pattern = String.sub data i2 (pattern_size - 1)
and i3 = i2 + pattern_size in
match unpack_uint32_native i3 data with
| Error (error) ->
Error (error)
| Ok (request_info_size) ->
let i4 = i3 + 4 in
let request_info = String.sub data i4 request_info_size
and i5 = i4 + request_info_size + 1 in
match unpack_uint32_native i5 data with
| Error (error) ->
Error (error)
| Ok (request_size) ->
let i6 = i5 + 4 in
let request = String.sub data i6 request_size
and i7 = i6 + request_size + 1 in
match unpack_uint32_native i7 data with
| Error (error) ->
Error (error)
| Ok (timeout) ->
let priority = unpack_int8 (i7 + 4) data
and trans_id = String.sub data (i7 + 5) 16
and i8 = i7 + 4 + 1 + 16 in
match unpack_uint32_native i8 data with
| Error (error) ->
Error (error)
| Ok (source_size) ->
let i9 = i8 + 4 in
let source_data = String.sub data i9 source_size
and i10 = i9 + source_size in
match Erlang.binary_to_term source_data with
| Error (error) ->
Error (error)
| Ok (
Erlang.OtpErlangInteger _
| Erlang.OtpErlangIntegerBig _
| Erlang.OtpErlangFloat _
| Erlang.OtpErlangAtom _
| Erlang.OtpErlangAtomUTF8 _
| Erlang.OtpErlangAtomCacheRef _
| Erlang.OtpErlangAtomBool _
| Erlang.OtpErlangString _
| Erlang.OtpErlangBinary _
| Erlang.OtpErlangBinaryBits (_, _)
| Erlang.OtpErlangList _
| Erlang.OtpErlangListImproper _
| Erlang.OtpErlangTuple _
| Erlang.OtpErlangMap _
| Erlang.OtpErlangPort _
| Erlang.OtpErlangReference _
| Erlang.OtpErlangFunction _) ->
Error (message_decoding_error)
| Ok (Erlang.OtpErlangPid (source)) ->
let handled =
if i10 <> data_size then
handle_events api ext data data_size i10 0
else
Ok (true)
in
match handled with
| Error (error) ->
Error (error)
| Ok (false) ->
Ok (Some false)
| Ok (true) ->
let request_type =
if cmd = message_send_async then
ASYNC
else (* cmd = message_send_sync *)
SYNC
in
let callback_result =
callback
api request_type name pattern request_info request
timeout priority trans_id source in
if api.Instance.terminate then
Ok (Some false)
else
callback_result
else if cmd = message_recv_async || cmd = message_return_sync then
match unpack_uint32_native (i + 4) data with
| Error (error) ->
Error (error)
| Ok (response_info_size) ->
let i0 = i + 8 in
let response_info = String.sub data i0 response_info_size
and i1 = i0 + response_info_size + 1 in
match unpack_uint32_native i1 data with
| Error (error) ->
Error (error)
| Ok (response_size) ->
let i2 = i1 + 4 in
let response = String.sub data i2 response_size
and i3 = i2 + response_size + 1 in
let trans_id = String.sub data i3 16
and i4 = i3 + 16 in
Instance.set_response api
response_info
response
trans_id ;
if i4 <> data_size then
match handle_events api ext data data_size i4 0 with
| Error (error) ->
Error (error)
| Ok _ ->
Ok (Some false)
else
Ok (Some false)
else if cmd = message_return_async then (
let i0 = i + 4 in
let trans_id = String.sub data i0 16
and i1 = i0 + 16 in
Instance.set_trans_id api
trans_id ;
if i1 <> data_size then
match handle_events api ext data data_size i1 0 with
| Error (error) ->
Error (error)
| Ok _ ->
Ok (Some false)
else
Ok (Some false))
else if cmd = message_returns_async then