package gapi-ocaml

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file netsys_win32.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
(* $Id$ *)

(* Security of named pipes:

   - http://www.blakewatts.com/namedpipepaper.html
   - impersonation: http://msdn.microsoft.com/en-us/library/aa378832(VS.85).aspx
 *)

open Netsys_types
open Printf

external fill_random : Bytes.t -> unit = "netsys_fill_random"

external get_full_path_name : string -> string = "netsys_get_full_path_name"
external get_long_path_name : string -> string = "netsys_get_long_path_name"

type c_event
type c_pipe_helper

(* How the proxy table works:

   The i_* records are the values in the proxy table. The keys are the
   proxy descriptors (which must be open all the time). When the last
   reference to the proxy descriptor is released, the GC will call the
   finalizer, and (with some trickery) the entry is removed from the
   proxy table. Of course, the i_* records must not contain the proxy
   descriptor - otherwise there would be a self-reference in the table,
   and the entry is never released.

   Because of this we define w_* types as a pair of the i_* records and
   the proxy descriptors. The w_* types are the public types. As no
   i_* can escape from its w_* value outside this module, it is ensured
   that all public references of i_* also imply public references of the
   proxy descriptor. So as long as there are w_* values the i_* values
   cannot be collected.

   When the proxy descriptor is accessed from outside the module, the
   caller becomes responsible for closing it. Therefore we track whether
   this is the case. The proxy descriptor is also stored in the c_* values 
   (i.e. in the values handled by the C part of this module), and so
   the C-written finalizer can close the proxy descriptor if required.
   There is a flag whether to do so (auto_close_*_proxy), and this flag
   is cleared when the caller takes over the ownership of the descriptor.
 *)

type i_event = c_event
type w32_event = i_event * Unix.file_descr
    (* The descriptor is the proxy descriptor *)

type pipe_mode = Pipe_in | Pipe_out | Pipe_duplex

type i_pipe =
    { pipe_name : string;
      pipe_mode : pipe_mode;
      pipe_helper : c_pipe_helper;
      (* mutable pipe_signal : w32_event option; *)
      pipe_rd_event : w32_event;
      pipe_wr_event : w32_event;
    }

type w32_pipe = i_pipe * Unix.file_descr
    (* The descriptor is the proxy descriptor *)

type i_pipe_server =
    { psrv_name : string;
      psrv_mode : pipe_mode;
      psrv_max : int;
      mutable psrv_first : bool;
      mutable psrv_queue : c_pipe_helper list;
      (* The queue of pipes waiting for an incoming connection *)
      mutable psrv_listen : int;
      (* The backlog parameter of [listen] (target length of psrv_queue) *)
      psrv_ready : c_pipe_helper Queue.t;
      (* The already accepted but not yet reported connections *)
      psrv_cn_event : w32_event;
      psrv_proxy_handle : c_event;
      psrv_mutex : Netsys_oothr.mutex;
    }
  (* As there is no C counterpart for the pipe server (no c_pipe_server),
     the question is how to ensure that the proxy descriptor is closed.
     For that reason we allocate an event (psrv_proxy_handle) and use
     this event as proxy descriptor. For events there is the possibility
     to let the C part of the module close the descriptor.
   *)

type w32_pipe_server = i_pipe_server * Unix.file_descr
    (* The descriptor is the proxy descriptor *)


type pipe_conn_state = Pipe_deaf | Pipe_listening | Pipe_connected | Pipe_down

type c_process
type i_process = c_process

type w32_process = i_process * Unix.file_descr
    (* The descriptor is the proxy descriptor *)

type i_input_thread =
  { ithr_descr : Unix.file_descr;
    (* One can send command to the thread by setting ithr_cmd, and signaling
       ithr_cmd_cond:
     *)
    ithr_cmd_cond : Netsys_oothr.condition;
    ithr_cmd_mutex : Netsys_oothr.mutex;
    mutable ithr_cmd : [ `Read | `Cancel ] option;
    mutable ithr_cancel_cmd : bool;   (* similar to ithr_cmd = Some `Cancel *)
    ithr_event : w32_event; (* The event is set when there is something to read *)
    ithr_buffer : Bytes.t;
    mutable ithr_buffer_start : int;
    mutable ithr_buffer_len : int;
    mutable ithr_buffer_cond : [ `Cancelled | `EOF | `Exception of exn | `Data ];
    mutable ithr_thread : int32;   (* The Win32 thread ID *)
    ithr_read_mutex : Netsys_oothr.mutex;  (* to serialize user read accesses *)
    mutable ithr_running : bool;
    ithr_proxy_handle : c_event;  (* the proxy - same pattern as in pipe servers *)
  }

type w32_input_thread = i_input_thread * Unix.file_descr * < >
    (* The descriptor is the proxy descriptor *)


type i_output_thread =
  { othr_descr : Unix.file_descr;
    othr_cmd_cond : Netsys_oothr.condition;
    othr_cmd_mutex : Netsys_oothr.mutex;
    mutable othr_cmd : [ `Write | `Close | `Cancel ] option;
    mutable othr_cancel_cmd : bool; 
    othr_event : w32_event;
    othr_buffer : Bytes.t;
    mutable othr_buffer_len : int;
    mutable othr_write_cond : [ `Cancelled | `Exception of exn ] option;
    mutable othr_thread : int32;   (* The Win32 thread ID *)
    othr_write_mutex : Netsys_oothr.mutex;
    mutable othr_running : bool;
    othr_proxy_handle : c_event;
  }

type w32_output_thread = i_output_thread * Unix.file_descr * < >
    (* The descriptor is the proxy descriptor *)


type i_object =
  | I_event of i_event
  | I_pipe of i_pipe
  | I_pipe_server of i_pipe_server
  | I_process of i_process
  | I_input_thread of i_input_thread * < >
  | I_output_thread of i_output_thread * < >

type w32_object =
  | W32_event of w32_event
  | W32_pipe of w32_pipe
  | W32_pipe_server of w32_pipe_server
  | W32_process of w32_process
  | W32_input_thread of w32_input_thread
  | W32_output_thread of w32_output_thread
      

type create_process_option =
  | CP_change_directory of string
  | CP_set_env of string
  | CP_std_handles of Unix.file_descr * Unix.file_descr * Unix.file_descr
  | CP_create_console
  | CP_detach_from_console
  | CP_inherit_console
  | CP_inherit_or_create_console
  | CP_unicode_environment
  | CP_ansi_environment
  | CP_new_process_group
  | CP_inherit_process_group



module Int64Map = Map.Make(Int64)


external int64_of_file_descr : Unix.file_descr -> int64
  = "netsys_int64_of_file_descr"
  (* Also occurs in netsys.ml! *)

external netsys_win32_set_debug : bool -> unit
  = "netsys_win32_set_debug"

module Debug = struct
  let enable = ref false

  let debug_c_wrapper = netsys_win32_set_debug
end

let dlog = Netlog.Debug.mk_dlog "Netsys_win32" Debug.enable
let dlogr = Netlog.Debug.mk_dlogr "Netsys_win32" Debug.enable

let () =
  Netlog.Debug.register_module "Netsys_win32" Debug.enable


module FD = struct
  type t = int64
  let equal (fd1) (fd2) =
    fd1=fd2
  let hash fd =
    Hashtbl.hash fd
end

module H = Hashtbl.Make(FD)
  (* Hash table mapping
     proxy file descriptors to the w32_object referenced by the descriptors.
     The keys are the handle values contained in the fd values. As we allow
     that proxies are [Unix.close]d it can happen that several fd's exist
     that have the same handle values. In this case, the address of the
     fd itself is used to distinguish between these same-looking fd's.
   *)

let proxies = ref (H.create 41)
let proxies_mutex = !Netsys_oothr.provider # create_mutex()
let proxies_gc_flag = ref false
let proxies_unreg_count = ref 0


let mk_weak x =
  let w = Weak.create 1 in
  Weak.set w 0 (Some x);
  w

let get_weak w =
  Weak.get w 0


let finalise_proxy cell _ =
  (* the GC finaliser *)
  proxies_unreg_count := !proxies_unreg_count + 1;
  cell := None


let gc_proxy() =
  (* Walk through the table and check. We have to take care that the
     order of the bindings for the same key is preserved, i.e. the most
     recent use of a descriptor number needs to be the visible binding
     in the table.
   *)
  let proxies' = H.create 41 in
  let n_old = ref 0 in
  let n_new = ref 0 in
  H.iter
    (fun fd_num entries ->
       let m = ref [] in
       List.iter
	 (fun entry ->
	    incr n_old;
	    let (_, cell) = entry in
	    if !cell <> None then (
	      incr n_new;
	      m := entry :: !m
	    )
	 )
	 entries;
       H.add proxies' fd_num (List.rev !m)
    )
    !proxies;
  proxies := proxies';
  proxies_unreg_count := 0;
  proxies_gc_flag := false;
  dlogr
    (fun () ->
       sprintf "register_proxy: keeping %d/%d entries in proxy tbl"
	 !n_new !n_old;
    );
  dlogr
    (fun () ->
       let b = Buffer.create 500 in
       Buffer.add_string b "\n";
       H.iter
	 (fun fd_num entries ->
	    List.iter
	      (fun (_,cell) ->
		 bprintf b " - proxy tbl %Ld -> %s\n"
		   fd_num
		   ( match !cell with
		       | None -> "<free>"
		       | Some(I_event _) -> "I_event"
		       | Some(I_pipe _) -> "I_pipe"
		       | Some(I_pipe_server _) -> "I_pipe_server"
		       | Some(I_process _) -> "I_process"
		       | Some(I_input_thread _) -> "I_input_thread"
		  | Some(I_output_thread _) -> "I_output_thread"
		   )
	      )
	      entries
	 )
	 proxies';
       Buffer.contents b
    )
	 

let register_proxy fd i_obj =
  let fd_num = int64_of_file_descr fd in
  (* Note that it is possible that we register several i_obj for the same
     fd_num. This can happen when fd is first closed, and then collected
     by the GC. So after the close the OS can reuse the fd_num for something
     else, but the old fd_num is still in the table.
   *)
  Netsys_oothr.serialize
    proxies_mutex
    (fun () ->
       if (!proxies_gc_flag && 
	     2 * !proxies_unreg_count > H.length !proxies)
       then (  (* do a GC pass *)
	 gc_proxy()
       );
       let cell = ref (Some i_obj) in
       let fd_weak = mk_weak fd in
       let l = try H.find !proxies fd_num with Not_found -> [] in
       H.replace !proxies fd_num ((fd_weak, cell) :: l);
       Gc.finalise (finalise_proxy cell) fd
    )
    ()


let unregister fd =
  (* called from user code *)
  let fd_num = int64_of_file_descr fd in
  Netsys_oothr.serialize
    proxies_mutex
    (fun () ->
       let l = try H.find !proxies fd_num with Not_found -> [] in
       let l' = 
	 List.filter
	   (fun (fd'_weak,cell) ->
	      match get_weak fd'_weak with
		| None -> false
		| Some fd' ->
		    !cell <> None && fd != fd'  (* phys. cmp! *)
	   )
	   l in
       H.replace !proxies fd_num l'
    )
    ()

let _ =
  Gc.create_alarm
    (fun () ->
       proxies_gc_flag := true
    )


let lookup fd =
  let fd_num = int64_of_file_descr fd in
  Netsys_oothr.serialize
    proxies_mutex
    (fun () ->
       let l = H.find !proxies fd_num in
       let (_, cell_opt) =
	 List.find
	   (fun (fd'_weak,cell) ->
	      match get_weak fd'_weak with
		| None -> false
		| Some fd' -> 
		    !cell <> None && fd == fd'  (* phys. cmp! *)
	   )
	   l in
       match !cell_opt with
	 | None ->
	     assert false
	 | Some i_obj ->
	     ( match i_obj with
		 | I_event i_ev -> 
		     W32_event(i_ev, fd)
		 | I_pipe i_pipe -> 
		     W32_pipe(i_pipe, fd)
		 | I_pipe_server i_psrv ->
		     W32_pipe_server(i_psrv, fd)
		 | I_process i_proc ->
		     W32_process(i_proc, fd)
		 | I_input_thread(i_thr, keep_alive) ->
		     W32_input_thread(i_thr, fd, keep_alive)
		 | I_output_thread(o_thr, keep_alive) ->
		     W32_output_thread(o_thr, fd, keep_alive)
	     )
    )
    ()


let lookup_pipe fd =
  try
    match lookup fd with
      | W32_pipe ph -> ph
      | _ -> raise Not_found
  with Not_found ->
    failwith "Netsys_win32.lookup_pipe: not found"

let lookup_pipe_server fd =
  try
    match lookup fd with
      | W32_pipe_server ph -> ph
      | _ -> raise Not_found
  with Not_found ->
    failwith "Netsys_win32.lookup_pipe_server: not found"

let lookup_event fd =
  try
    match lookup fd with
      | W32_event e -> e
      | _ -> raise Not_found
  with Not_found ->
    failwith "Netsys_win32.lookup_event: not found"

let lookup_process fd =
  try
    match lookup fd with
      | W32_process e -> e
      | _ -> raise Not_found
  with Not_found ->
    failwith "Netsys_win32.lookup_process: not found"

let lookup_input_thread fd =
  try
    match lookup fd with
      | W32_input_thread e -> e
      | _ -> raise Not_found
  with Not_found ->
    failwith "Netsys_win32.lookup_input_thread: not found"

let lookup_output_thread fd =
  try
    match lookup fd with
      | W32_output_thread e -> e
      | _ -> raise Not_found
  with Not_found ->
    failwith "Netsys_win32.lookup_output_thread: not found"


external get_active_code_page : unit -> int 
  = "netsys_getacp"

external netsys_real_select : 
         Unix.file_descr list -> 
         Unix.file_descr list -> 
         Unix.file_descr list -> 
         float ->
           (Unix.file_descr list * Unix.file_descr list * Unix.file_descr list)
  = "netsys_real_select"

let real_select = netsys_real_select


external netsys_test_close_on_exec : Unix.file_descr -> bool
  = "netsys_test_close_on_exec"

let test_close_on_exec = netsys_test_close_on_exec

external netsys_modify_close_on_exec : Unix.file_descr -> bool -> unit
  = "netsys_modify_close_on_exec"

let modify_close_on_exec = netsys_modify_close_on_exec

external netsys_is_crt_fd : Unix.file_descr -> int -> bool
  = "netsys_is_crt_fd"

let is_crt_fd = netsys_is_crt_fd



external netsys_create_event : unit -> c_event 
  = "netsys_create_event"

external netsys_event_descr : c_event -> Unix.file_descr
  = "netsys_event_descr"

external netsys_close_event : c_event -> unit
  = "netsys_close_event"

external netsys_set_auto_close_event_proxy : c_event -> bool -> unit
  = "netsys_set_auto_close_event_proxy"

let decorate_event e =
  let e_proxy = netsys_event_descr e in
  let ev = (e, e_proxy) in
  Gc.finalise netsys_close_event e;
  register_proxy e_proxy (I_event e);
  ev

let create_event() =
  let ev = decorate_event(netsys_create_event()) in
  dlogr (fun () -> 
	   sprintf "create_event: descr=%Ld" 
	     (int64_of_file_descr (snd ev)));
  ev


let event_descr (e,e_proxy) = 
  netsys_set_auto_close_event_proxy e false;
  e_proxy

external netsys_set_event : c_event -> unit
  = "netsys_set_event"

external netsys_reset_event : c_event -> unit
  = "netsys_reset_event"

external netsys_test_event : c_event -> bool
  = "netsys_test_event"

external netsys_event_wait : c_event -> int -> bool
  = "netsys_event_wait"

let set_event (e,e_proxy)   = 
  dlogr (fun () -> 
	   sprintf "set_event: descr=%Ld" 
	     (int64_of_file_descr e_proxy));
  netsys_set_event e

let reset_event (e,e_proxy) = 
  dlogr (fun () -> 
	   sprintf "reset_event: descr=%Ld" 
	     (int64_of_file_descr e_proxy));
  netsys_reset_event e

let test_event (e,_)  = netsys_test_event e

let event_wait (e,e_proxy) tmo =
  dlogr (fun () -> 
	   sprintf "event_wait: descr=%Ld tmo=%f" 
	     (int64_of_file_descr e_proxy) tmo);
  let flag =
    Netsys_impl_util.slice_time_ms
      (fun tmo_ms ->
	 if netsys_event_wait e tmo_ms then Some () else None
      )
      tmo
    <> None in
  dlogr (fun () -> 
	   sprintf "event_wait: descr=%Ld returning %b" 
	     (int64_of_file_descr e_proxy) flag);
  flag



external netsys_wsa_event_select :  
  c_event -> Unix.file_descr -> Netsys_posix.poll_req_events -> unit
  = "netsys_wsa_event_select"

external wsa_maximum_wait_events : unit -> int
  = "netsys_wsa_maximum_wait_events"

external netsys_wsa_wait_for_multiple_events : 
  c_event array -> int -> int option
  = "netsys_wsa_wait_for_multiple_events"

external netsys_wsa_enum_network_events : 
  Unix.file_descr -> c_event -> Netsys_posix.poll_act_events
  = "netsys_wsa_enum_network_events"

let wsa_event_select (e,e_proxy) fd pie =
  dlogr (fun () -> 
	   sprintf "wsa_event_select: evdescr=%Ld sockdescr=%Ld bits=%d" 
	     (int64_of_file_descr e_proxy) 
	     (int64_of_file_descr fd)
	     (Netsys_posix.int_of_req_events pie)
	);
  netsys_wsa_event_select e fd pie

let wsa_wait_for_multiple_events ea n =
  dlogr (fun () ->
	   sprintf "wsa_wait_for_multiple_events: descrs=%s tmo=%d"
	     (String.concat ","
		(Array.to_list
		   (Array.map
		      (fun (_,e_proxy) -> 
			 Int64.to_string(int64_of_file_descr e_proxy)) 
		      ea)))
	     n
	);
  let r =
    netsys_wsa_wait_for_multiple_events (Array.map fst ea) n in
  dlogr (fun () ->
	   sprintf "wsa_wait_for_multiple_events: returning %s"
	     (match r with
		| None -> "None"
		| Some k ->
		    let e_proxy = snd(ea.(k)) in
		    sprintf "Some %d (descr %Ld)"
		      k (int64_of_file_descr e_proxy)
	     ));
  r

let wsa_enum_network_events fd (e,e_proxy) =
  let r = netsys_wsa_enum_network_events fd e in
  dlogr (fun () ->
	   sprintf "wsa_enum_network_events: sockdescr=%Ld evdescr=%Ld bits=%d"
	     (int64_of_file_descr fd)
	     (int64_of_file_descr e_proxy)
	     (Netsys_posix.int_of_act_events r)
	);
  r



external netsys_pipe_free : 
  c_pipe_helper -> unit
  = "netsys_pipe_free"

external netsys_create_local_named_pipe :
  string -> pipe_mode -> int -> c_event -> bool -> c_pipe_helper
  = "netsys_create_local_named_pipe"

external netsys_pipe_listen :
  c_pipe_helper -> unit
  = "netsys_pipe_listen"

external netsys_pipe_deafen :
  c_pipe_helper -> unit
  = "netsys_pipe_deafen"

external netsys_pipe_connect :
  string -> pipe_mode -> c_pipe_helper
  = "netsys_pipe_connect"

external netsys_pipe_read :
  c_pipe_helper -> Bytes.t -> int -> int -> int
  = "netsys_pipe_read"

external netsys_pipe_write :
  c_pipe_helper -> Bytes.t -> int -> int -> int
  = "netsys_pipe_write"

external netsys_pipe_shutdown :
  c_pipe_helper -> unit
  = "netsys_pipe_shutdown"

external netsys_pipe_rd_event :
  c_pipe_helper -> c_event
  = "netsys_pipe_rd_event"

external netsys_pipe_wr_event :
  c_pipe_helper -> c_event
  = "netsys_pipe_wr_event"

external netsys_pipe_descr :
   c_pipe_helper -> Unix.file_descr
  = "netsys_pipe_descr"

external netsys_pipe_conn_state : 
  c_pipe_helper -> pipe_conn_state
  = "netsys_pipe_conn_state"

external netsys_pipe_signal :
  c_pipe_helper -> c_event -> unit
  = "netsys_pipe_signal"

external netsys_set_auto_close_pipe_proxy : c_pipe_helper -> bool -> unit
  = "netsys_set_auto_close_pipe_proxy"


let rev_mode =
  function
    | Pipe_in -> Pipe_out
    | Pipe_out -> Pipe_in
    | Pipe_duplex -> Pipe_duplex

let create_local_pipe_server name mode n =
  let cn_event = create_event() in
  let p_event = netsys_create_event() in
  let proxy = netsys_event_descr p_event in
  let psrv =
    { psrv_name = name;
      psrv_mode = mode;
      psrv_max = n;
      psrv_first = true;
      psrv_queue = [];
      psrv_listen = 0;
      psrv_ready = Queue.create();
      psrv_cn_event = cn_event;
      psrv_proxy_handle = p_event;
      psrv_mutex = !Netsys_oothr.provider # create_mutex();
    } in
  Gc.finalise netsys_close_event p_event;
  register_proxy proxy (I_pipe_server psrv);
  dlogr (fun () ->
	   sprintf "create_local_pipe_server: \
                    name=%s proxydescr=%Ld cnevdescr=%Ld"
	     name 
	     (int64_of_file_descr proxy) 
	     (int64_of_file_descr (snd cn_event))
	);
  (psrv, proxy)



let decorate_pipe_nogc ph name mode =
  let fd = netsys_pipe_descr ph in
  let pipe =
    { pipe_name = name;
      pipe_mode = mode;
      pipe_helper = ph;
      (* pipe_signal = None; *)
      pipe_rd_event = decorate_event(netsys_pipe_rd_event ph);
      pipe_wr_event = decorate_event(netsys_pipe_wr_event ph);
    } in
  register_proxy fd (I_pipe pipe);
  (pipe, fd)


let decorate_pipe ph name mode =
  Gc.finalise netsys_pipe_free ph;
  decorate_pipe_nogc ph name mode

let prefix = "\\\\.\\pipe\\"
let prefix_len = String.length prefix

let pipe_connect name mode =
  (* Check that name starts with the right prefix, to prevent security
     vulnerabilities:
   *)
  if String.length name < prefix_len ||
     (String.sub name 0 prefix_len <> prefix)
  then
    raise(Unix.Unix_error(Unix.EPERM, "pipe_connect", name));

  dlogr (fun () -> sprintf "pipe_connect: name=%s" name);
  let pipe = decorate_pipe(netsys_pipe_connect name mode) name mode in
  dlogr (fun () -> sprintf "pipe_connect: name=%s returning %Ld" 
	   name (int64_of_file_descr (snd pipe)));
  pipe

let pipe_server_descr (psrv, psrv_proxy) = 
  netsys_set_auto_close_event_proxy psrv.psrv_proxy_handle false;
  psrv_proxy

let pipe_descr (pipe, pipe_proxy) = 
  netsys_set_auto_close_pipe_proxy pipe.pipe_helper false;
  pipe_proxy

let pipe_server_endpoint psrv =
  let ph = 
    netsys_create_local_named_pipe
      psrv.psrv_name psrv.psrv_mode psrv.psrv_max 
      (fst psrv.psrv_cn_event) psrv.psrv_first in
  Gc.finalise netsys_pipe_free ph;
  netsys_pipe_listen ph;
  psrv.psrv_first <- false;
  ph

let pipe_listen_lck psrv n =
  if psrv.psrv_listen < n then (
    let d = n - psrv.psrv_listen in
    for k = 1 to d do
      let ph = pipe_server_endpoint psrv in
      psrv.psrv_queue <- ph :: psrv.psrv_queue
    done
  );
  (* else: we do nothing. You may consider this as a bug, but it is simply
     too risky to shut down server pipes because of race conditions
   *)
  psrv.psrv_listen <- n


let pipe_listen (psrv, psrv_proxy) n =
  dlogr (fun () -> sprintf "pipe_listen: name=%s proxydescr=%Ld n=%d" 
	   psrv.psrv_name (int64_of_file_descr psrv_proxy) n);
  Netsys_oothr.serialize
    psrv.psrv_mutex
    (fun () -> pipe_listen_lck psrv n)
    ()


let check_for_connections psrv =
  let rec find_delete l =
    match l with
      | [] -> 
	  []
      | ph :: l' ->
	  let s = netsys_pipe_conn_state ph in
	  if s = Pipe_connected then (
	    Queue.push ph psrv.psrv_ready;
	    find_delete l'
	  )
	  else
	    ph :: find_delete l'
  in

  let queue' = find_delete psrv.psrv_queue in
  let old_listen = psrv.psrv_listen in
  psrv.psrv_listen <- List.length queue';
  psrv.psrv_queue <- queue';
  pipe_listen_lck psrv old_listen
    
(* In rare cases it may happen that cn_event is reset for a short
   period of time, and then set again.
 *)

let empty_buf = Bytes.create 0

let rec pipe_accept_1 psrv =
  match Queue.length psrv.psrv_ready with
    | 0 ->
	ignore(event_wait psrv.psrv_cn_event (-1.0));
	reset_event psrv.psrv_cn_event;
	check_for_connections psrv;
	if not(Queue.is_empty psrv.psrv_ready) then
	  set_event psrv.psrv_cn_event;
	pipe_accept_1 psrv
    | 1 ->
	let ph = Queue.take psrv.psrv_ready in
	reset_event psrv.psrv_cn_event;
	check_for_connections psrv;
	if not(Queue.is_empty psrv.psrv_ready) then
	  set_event psrv.psrv_cn_event;
	ignore(netsys_pipe_read ph empty_buf 0 0);     (* check for errors *)
	decorate_pipe_nogc ph psrv.psrv_name psrv.psrv_mode
    | _ ->
	let ph = Queue.take psrv.psrv_ready in
	ignore(netsys_pipe_read ph empty_buf 0 0);     (* check for errors *)
	decorate_pipe_nogc ph psrv.psrv_name psrv.psrv_mode


let pipe_accept (psrv, psrv_proxy) =
  dlogr (fun () -> sprintf "pipe_accept: name=%s proxydescr=%Ld" 
	   psrv.psrv_name (int64_of_file_descr psrv_proxy));
  let pipe =
    Netsys_oothr.serialize
      psrv.psrv_mutex
      (fun () -> pipe_accept_1 psrv)
      () in
  dlogr (fun () -> sprintf "pipe_accept: name=%s proxydescr=%Ld returning %Ld" 
	   psrv.psrv_name (int64_of_file_descr psrv_proxy)
	   (int64_of_file_descr (snd pipe))
	);
  pipe
  

let pipe_rd_event (pipe,_) =
  pipe.pipe_rd_event

let pipe_wr_event (pipe,_) =
  pipe.pipe_wr_event

let pipe_connect_event (psrv,_) =
  psrv.psrv_cn_event


let pipe_read (pipe,pipe_proxy) s pos len =
  if pos < 0 || len < 0 || pos > Bytes.length s - len then
    invalid_arg "Netsys_win32.pipe_read";
  dlogr (fun () -> sprintf "pipe_read: name=%s proxydescr=%Ld len=%d" 
	   pipe.pipe_name (int64_of_file_descr pipe_proxy) len);
  try
    let n = netsys_pipe_read pipe.pipe_helper s pos len in
    dlogr (fun () -> sprintf "pipe_read: name=%s proxydescr=%Ld returning %d" 
	     pipe.pipe_name (int64_of_file_descr pipe_proxy) n);
    n
  with
    | error when !Debug.enable ->
	dlogr (fun () -> 
		 sprintf "pipe_read: name=%s proxydescr=%Ld exception %s" 
		   pipe.pipe_name (int64_of_file_descr pipe_proxy) 
		   (Netexn.to_string error)
	      );
	raise error


let pipe_write (pipe,pipe_proxy) s pos len =
  if pos < 0 || len < 0 || pos > Bytes.length s - len then
    invalid_arg "Netsys_win32.pipe_write";
  dlogr (fun () -> sprintf "pipe_write: name=%s proxydescr=%Ld len=%d" 
	   pipe.pipe_name (int64_of_file_descr pipe_proxy) len);
  try
    let n = netsys_pipe_write pipe.pipe_helper s pos len in
    dlogr (fun () -> sprintf "pipe_write: name=%s proxydescr=%Ld returning %d" 
	     pipe.pipe_name (int64_of_file_descr pipe_proxy) n);
    n
  with
    | error when !Debug.enable ->
	dlogr (fun () -> 
		 sprintf "pipe_write: name=%s proxydescr=%Ld exception %s" 
		   pipe.pipe_name (int64_of_file_descr pipe_proxy) 
		   (Netexn.to_string error)
	      );
	raise error

let pipe_write_string (pipe,pipe_proxy) s pos len =
  pipe_write (pipe,pipe_proxy) (Bytes.unsafe_of_string s) pos len


let pipe_shutdown (pipe,pipe_proxy) = 
  dlogr (fun () -> sprintf "pipe_shutdown: name=%s proxydescr=%Ld" 
	   pipe.pipe_name (int64_of_file_descr pipe_proxy));
  netsys_pipe_shutdown pipe.pipe_helper

let pipe_shutdown_server (psrv,psrv_proxy) =
  dlogr (fun () -> sprintf "pipe_shutdown_server: name=%s proxydescr=%Ld" 
	   psrv.psrv_name (int64_of_file_descr psrv_proxy));
  Netsys_oothr.serialize
    psrv.psrv_mutex
    (fun () ->
       List.iter
	 (fun ph ->
	    netsys_pipe_shutdown ph
	 )
	 psrv.psrv_queue;
       psrv.psrv_queue <- [];
       psrv.psrv_listen <- 0
    )
    ()


let pipe_wait_rd (pipe,pipe_proxy) tmo =
  dlogr (fun () -> sprintf "pipe_wait_rd: name=%s proxydescr=%Ld" 
	   pipe.pipe_name (int64_of_file_descr pipe_proxy));
  event_wait pipe.pipe_rd_event tmo

let pipe_wait_wr (pipe,pipe_proxy) tmo =
  dlogr (fun () -> sprintf "pipe_wait_wr: name=%s proxydescr=%Ld" 
	   pipe.pipe_name (int64_of_file_descr pipe_proxy));
  event_wait pipe.pipe_wr_event tmo

let pipe_wait_connect (psrv,psrv_proxy) tmo =
  dlogr (fun () -> sprintf "pipe_wait_connect: name=%s proxydescr=%Ld" 
	   psrv.psrv_name (int64_of_file_descr psrv_proxy));
  event_wait psrv.psrv_cn_event tmo

let pipe_name (pipe,_) =
  pipe.pipe_name

let pipe_server_name (psrv,_) =
  psrv.psrv_name

let pipe_mode (pipe,_) =
  pipe.pipe_mode

let pipe_server_mode (psrv,_) =
  psrv.psrv_mode


let counter = ref 0
let counter_mutex = !Netsys_oothr.provider # create_mutex()

let unpredictable_pipe_name() =
  let n = (
    counter_mutex # lock();
    let n = !counter in
    incr counter;
    counter_mutex # unlock();
    n
  ) in
  let random = Bytes.make 16 ' ' in
  fill_random random;
  let name =
    "\\\\.\\pipe\\ocamlnet" ^ 
      string_of_int (Unix.getpid()) ^ "_" ^ string_of_int n ^ "_" ^ 
      Digest.to_hex(Bytes.to_string random) in
  name

let pipe_pair mode =
  (* FIXME: If somebody guesses the pipe name (which is hard),
     it is possible to connect from the outside to lph. We detect
     this problem, and give up on the pipe pair, but external code can 
     make our programs unreliable.
   *)
  dlog "pipe_pair";
  let mode' =
    match mode with
      | Pipe_in -> Pipe_out
      | Pipe_out -> Pipe_in
      | Pipe_duplex -> Pipe_duplex in
  let name = unpredictable_pipe_name() in
  let psrv = create_local_pipe_server name mode 1 in
  pipe_listen psrv 1;
  let rph = pipe_connect name mode' in
  ( try
      pipe_listen psrv 0;
      let lph = pipe_accept psrv in
      ( try
	  let s = Bytes.create 0 in
	  ignore(pipe_write lph s 0 0);
	  dlogr 
	    (fun () -> 
	       sprintf "pipe_pair: returning \
                        name=%s proxydescr1=%Ld proxydescr2=%Ld" 
		 name
		 (int64_of_file_descr (snd lph)) 
		 (int64_of_file_descr (snd rph)) 
	    );
	  (lph, rph)
	with e -> 
	  pipe_shutdown lph; 
	  raise e
      )
    with e -> 
      pipe_shutdown rph; 
      raise e
  )


external netsys_create_process : string -> string -> 
  create_process_option list -> c_process 
  = "netsys_create_process"

external netsys_close_process : c_process -> unit
  = "netsys_close_process"

external netsys_get_process_status : c_process -> int
  = "netsys_get_process_status"

external netsys_as_process_event : c_process -> c_event
  = "netsys_as_process_event"

external netsys_emulated_pid : c_process -> int
  = "netsys_emulated_pid"

external netsys_win_pid : c_process -> int
  = "netsys_win_pid"

external netsys_process_free : c_process -> unit
  = "netsys_process_free"

external netsys_process_descr : c_process -> Unix.file_descr
  = "netsys_process_descr"

external netsys_set_auto_close_process_proxy : c_process -> bool -> unit
  = "netsys_set_auto_close_process_proxy"

external netsys_terminate_process : c_process -> unit
  = "netsys_terminate_process"

let close_process (c_proc, _) =
  netsys_process_free c_proc

let get_process_status (c_proc,_) =
  try
    let code = netsys_get_process_status c_proc in
    Some(Unix.WEXITED code)
  with
    | Not_found -> None

let default_opts =
  [ CP_inherit_or_create_console;
    CP_ansi_environment;
    CP_inherit_process_group
  ]

let create_process cmd cmdline opts =
  let opts = (* prepend defaults: *)
    default_opts @ opts in
  let c_proc = netsys_create_process cmd cmdline opts in
  let proxy = netsys_process_descr c_proc in
  register_proxy proxy (I_process c_proc);
  Gc.finalise netsys_process_free c_proc;
  ignore(get_process_status (c_proc,proxy));
  (* The new process seems to remain suspended until the caller waits
     for the process handle. So we do this here.
   *)
  (c_proc, proxy)

let terminate_process (c_proc,_) =
  netsys_terminate_process c_proc

let as_process_event (c_proc,_) =
  let ev = netsys_as_process_event c_proc in
  decorate_event ev

let emulated_pid (c_proc,_) =
  netsys_emulated_pid c_proc

let win_pid (c_proc, _) =
  netsys_win_pid c_proc

let process_descr (c_proc, fd) =
  netsys_set_auto_close_process_proxy c_proc false;
  fd

let cp_set_env env =
  CP_set_env(String.concat "\000" (Array.to_list env) ^ "\000")
    (* another null byte is implicitly added by the ocaml runtime! *)

external search_path : string option -> string -> string option -> string
  = "netsys_search_path"


type w32_console_attr =
    { mutable cursor_x : int;
      mutable cursor_y : int;
      mutable cursor_size : int;
      mutable cursor_visible : bool;
      mutable text_attr : int;
    }

type w32_console_info =
    {
      mutable width : int;
      mutable height : int;
    }

type w32_console_mode = 
    { mutable enable_echo_input : bool;
      mutable enable_insert_mode : bool;
      mutable enable_line_input : bool;
      mutable enable_processed_input : bool;
      mutable enable_quick_edit_mode : bool;
      mutable enable_processed_output : bool;
      mutable enable_wrap_at_eol_output : bool;
    }

external has_console : unit -> bool
  = "netsys_has_console"

external is_console : Unix.file_descr -> bool
  = "netsys_is_console"

external alloc_console : unit -> unit
  = "netsys_alloc_console"

let get_console_input() =
  if not(has_console()) then
    alloc_console();
  Unix.openfile "CONIN$" [Unix.O_RDWR] 0
    (* O_RDONLY is insufficient for certain console ops *)


let get_console_output() =
  if not(has_console()) then
    alloc_console();
  Unix.openfile "CONOUT$" [Unix.O_RDWR] 0
    (* O_WRONLY is insufficient for certain console ops *)

external get_console_attr : unit -> w32_console_attr
  = "netsys_get_console_attr"

external set_console_attr : w32_console_attr -> unit
  = "netsys_set_console_attr"

external get_console_info : unit -> w32_console_info
  = "netsys_get_console_info"

let fg_blue = 1
let fg_green = 2
let fg_red = 4
let fg_intensity = 8
let bg_blue = 16
let bg_green = 32
let bg_red = 64
let bg_intensity = 128

external get_console_mode : unit -> w32_console_mode
  = "netsys_get_console_mode"

external set_console_mode : w32_console_mode -> unit
  = "netsys_set_console_mode"

external init_console_codepage : unit -> unit
  = "netsys_init_console_codepage"

type clear_mode =
  | EOL | EOS | All

external clear_console : clear_mode -> unit
  = "netsys_clear_console"

let clear_until_end_of_line() = clear_console EOL

let clear_until_end_of_screen() = clear_console EOS

let clear_console() = clear_console All


external get_current_thread_id : unit -> int32
  = "netsys_get_current_thread_id"

external cancel_synchronous_io : int32 -> unit
  = "netsys_cancel_synchronous_io"
  (* Only implemented on Vista (and newer). *)



module InputThread = struct
  let rec thread_body (ithr : i_input_thread) =
    (* Check for new commands: *)
    dlogr (fun () ->
	     sprintf "input_thread_body: descr=%Ld waiting"
	       (int64_of_file_descr ithr.ithr_descr));
    ithr.ithr_cmd_mutex # lock();
    while ithr.ithr_cmd = None && not ithr.ithr_cancel_cmd do
      ithr.ithr_cmd_cond # wait ithr.ithr_cmd_mutex
    done;
    let next_cmd =
      if ithr.ithr_cancel_cmd then
	`Cancel
      else
	match ithr.ithr_cmd with
          | None -> 
              assert false
          | Some c -> 
              ithr.ithr_cmd <- None;
              c in
    let continue =
      match next_cmd with
	| `Cancel ->
	    dlogr (fun () ->
		     sprintf "input_thread_body: descr=%Ld got `Cancel"
		       (int64_of_file_descr ithr.ithr_descr));
	    ithr.ithr_buffer_cond <- `Cancelled;
	    false
	| `Read ->
	    dlogr (fun () ->
		     sprintf "input_thread_body: descr=%Ld got `Read"
		       (int64_of_file_descr ithr.ithr_descr));
	    ( try
		let n = 
		  Unix.read 
		    ithr.ithr_descr
		    ithr.ithr_buffer
		    0
		    (Bytes.length ithr.ithr_buffer) in
		if n = 0 then (
		  ithr.ithr_buffer_cond <- `EOF;
		  ithr.ithr_buffer_start <- 0;
		  ithr.ithr_buffer_len <- 0;
		  false
		) 
		else (
		  ithr.ithr_buffer_cond <- `Data;
		  ithr.ithr_buffer_start <- 0;
		  ithr.ithr_buffer_len <- n;
		  true
		)
	      with
		| Unix.Unix_error(Unix.EPIPE,_,_) ->  (* same as EOF *)
		    ithr.ithr_buffer_cond <- `EOF;
		    ithr.ithr_buffer_start <- 0;
		    ithr.ithr_buffer_len <- 0;
		    false
		| error ->
		    ithr.ithr_buffer_cond <- `Exception error;
		    ithr.ithr_buffer_start <- 0;
		    ithr.ithr_buffer_len <- 0;
		    false
	    )
    in
    dlogr (fun () ->
	     sprintf "input_thread_body: descr=%Ld unblocking"
	       (int64_of_file_descr ithr.ithr_descr));
    set_event ithr.ithr_event;
    ithr.ithr_cmd_mutex # unlock();
    if continue then 
      thread_body ithr
    else (
      (* clean-up: *)
      dlogr (fun () ->
	       sprintf "input_thread_body: descr=%Ld terminating"
		 (int64_of_file_descr ithr.ithr_descr));
      Unix.close ithr.ithr_descr;
      ithr.ithr_running <- false
    )
      

  let i_cancel_input_thread ithr =
    dlogr (fun () ->
	     sprintf "cancel_input_thread: descr=%Ld"
	       (int64_of_file_descr ithr.ithr_descr));
    ithr.ithr_cancel_cmd <- true;  (* don't mess with locks here *)
    ithr.ithr_cmd_cond # signal();
    (* This is clearly a race condition... The thread may terminate
       right now, and cancel_io_thread is called with an invalid thread
       ID.
     *)
    if ithr.ithr_running then (
      try
	cancel_synchronous_io ithr.ithr_thread
      with _ -> ()
    )

  let f_cancel_input_thread ithr _ =
    i_cancel_input_thread ithr

  let cancel_input_thread (ithr,_,_) =
    i_cancel_input_thread ithr 

  let create_input_thread fd =
    let oothr = !Netsys_oothr.provider in
    let init_cond = oothr#create_condition() in
    let init_mutex = oothr#create_mutex() in
    let p_event = netsys_create_event() in
    let proxy = netsys_event_descr p_event in
    let ithr =
      { ithr_descr = fd;
	ithr_cmd_cond = oothr#create_condition();
	ithr_cmd_mutex = oothr#create_mutex();
	ithr_cmd = Some `Read;
	ithr_cancel_cmd = false;
	ithr_event = create_event();
	ithr_buffer = Bytes.create 4096;
	ithr_buffer_start = 0;
	ithr_buffer_len = 0;
	ithr_buffer_cond = `Data;
	ithr_thread = 0l;  (* initialized below *)
	ithr_read_mutex = oothr#create_mutex();
	ithr_running = true;
	ithr_proxy_handle = p_event;
      } in
    let _ =
      oothr # create_thread
	(fun () ->
	   ithr.ithr_thread <- get_current_thread_id();
	   init_cond # signal();
	   thread_body ithr
	)
	() in
    init_cond # wait init_mutex;
    let f = f_cancel_input_thread ithr in
    let keep_alive = (object end) in
    Gc.finalise f keep_alive;
    Gc.finalise netsys_close_event p_event;
    register_proxy proxy (I_input_thread(ithr, keep_alive));
    (ithr, proxy, keep_alive)
    

  let input_thread_read (ithr,_,_) s pos len =
    if pos < 0 || len < 0 || pos > Bytes.length s - len then
      invalid_arg "Netsys_win32.input_thread_read";
    
    if len = 0 then
      0
    else (
      Netsys_oothr.serialize
	ithr.ithr_read_mutex   (* only one reader at a time *)
	(fun () ->
	   let b = test_event ithr.ithr_event in
	   if b then (
	     ithr.ithr_cmd_mutex # lock();
	   (* Look at what we have: *)
	   match ithr.ithr_buffer_cond with
	     | `EOF ->
		 ithr.ithr_cmd_mutex # unlock();
		 0
	     | `Exception e ->
		 ithr.ithr_cmd_mutex # unlock();
		 raise e
	     | `Cancelled ->
		 ithr.ithr_cmd_mutex # unlock();
		 raise(Unix.Unix_error(Unix.EPERM, 
				       "Netsys_win32.input_thread_read", ""))
	     | `Data ->
		 let n = min len ithr.ithr_buffer_len in
		 Bytes.blit 
		   ithr.ithr_buffer ithr.ithr_buffer_start
		   s pos
		   n;
		 ithr.ithr_buffer_start <- ithr.ithr_buffer_start + n;
		 ithr.ithr_buffer_len <- ithr.ithr_buffer_len - n;
		 if ithr.ithr_buffer_len = 0 then (
		   ithr.ithr_cmd <- Some `Read;
		   ithr.ithr_cmd_cond # signal();
		   reset_event ithr.ithr_event;
		 );
		 ithr.ithr_cmd_mutex # unlock();
		 n
	   )
	   else
	     raise(Unix.Unix_error(Unix.EAGAIN, 
				   "Netsys_win32.input_thread_read", ""))
	)
	()
    )

  let input_thread_event (ithr,_,_) =
    ithr.ithr_event

  let input_thread_proxy_descr (ithr, proxy, _) = 
    netsys_set_auto_close_event_proxy ithr.ithr_proxy_handle false;
    proxy

end

let create_input_thread = InputThread.create_input_thread
let input_thread_event = InputThread.input_thread_event
let input_thread_read = InputThread.input_thread_read
let cancel_input_thread = InputThread.cancel_input_thread
let input_thread_proxy_descr = InputThread.input_thread_proxy_descr
let input_thread_descr (ithr,_,_) = ithr.ithr_descr


module OutputThread = struct
  let rec write_string othr pos len =
    if len = 0 || othr.othr_cancel_cmd then
      ()
    else
      let n = Unix.single_write othr.othr_descr othr.othr_buffer pos len in
      write_string othr (pos+n) (len-n)


  let rec thread_body (othr : i_output_thread) =
    (* Check for new commands: *)
    dlogr (fun () ->
	     sprintf "output_thread_body: descr=%Ld waiting"
	       (int64_of_file_descr othr.othr_descr));
    othr.othr_cmd_mutex # lock();
    while othr.othr_cmd = None && not othr.othr_cancel_cmd do
      othr.othr_cmd_cond # wait othr.othr_cmd_mutex
    done;
    let next_cmd =
      if othr.othr_cancel_cmd then
	`Cancel
      else
	match othr.othr_cmd with
          | None -> 
              assert false
          | Some c -> 
              othr.othr_cmd <- None;
              c in
    let continue =
      match next_cmd with
	| `Cancel ->
	    dlogr (fun () ->
		     sprintf "output_thread_body: descr=%Ld got `Cancel"
		       (int64_of_file_descr othr.othr_descr));
	    othr.othr_buffer_len <- 0;
	    othr.othr_write_cond <- Some `Cancelled;
	    false
	| `Close ->
	    dlogr (fun () ->
		     sprintf "output_thread_body: descr=%Ld got `Close"
		       (int64_of_file_descr othr.othr_descr));
	    othr.othr_write_cond <- Some `Cancelled;
	    false
	| `Write ->
	    dlogr (fun () ->
		     sprintf "output_thread_body: descr=%Ld got `Write"
		       (int64_of_file_descr othr.othr_descr));
	    ( try
		write_string othr 0 othr.othr_buffer_len;
		othr.othr_buffer_len <- 0;
		true
	      with
		| error ->
		    othr.othr_write_cond <- Some (`Exception error);
		    false
	    )
    in
    dlogr (fun () ->
	     sprintf "output_thread_body: descr=%Ld unblocking"
	       (int64_of_file_descr othr.othr_descr));
    set_event othr.othr_event;
    othr.othr_cmd_mutex # unlock();
    if continue then 
      thread_body othr
    else (
      (* clean-up: *)
      dlogr (fun () ->
	       sprintf "output_thread_body: descr=%Ld terminating"
		 (int64_of_file_descr othr.othr_descr));
      Unix.close othr.othr_descr;
      othr.othr_running <- false
    )


  let i_cancel_output_thread othr =
    dlogr (fun () ->
	     sprintf "cancel_output_thread: descr=%Ld"
	       (int64_of_file_descr othr.othr_descr));
    othr.othr_cancel_cmd <- true;  (* don't mess with locks here *)
    othr.othr_cmd_cond # signal();
    (* This is clearly a race condition... The thread may terminate
       right now, and cancel_io_thread is called with an invalid thread
       ID.
     *)
    if othr.othr_running then (
      try
	cancel_synchronous_io othr.othr_thread
      with _ -> ()
    )

  let f_cancel_output_thread othr _ =
    i_cancel_output_thread othr

  let cancel_output_thread (othr,_,_) =
    i_cancel_output_thread othr 

  let create_output_thread fd =
    let oothr = !Netsys_oothr.provider in
    let init_cond = oothr#create_condition() in
    let init_mutex = oothr#create_mutex() in
    let p_event = netsys_create_event() in
    let proxy = netsys_event_descr p_event in
    let othr =
      { othr_descr = fd;
	othr_cmd_cond = oothr#create_condition();
	othr_cmd_mutex = oothr#create_mutex();
	othr_cmd = None;
	othr_cancel_cmd = false;
	othr_event = create_event();
	othr_buffer = Bytes.create 4096;
	othr_buffer_len = 0;
	othr_write_cond = None;
	othr_thread = 0l;  (* initialized below *)
	othr_write_mutex = oothr#create_mutex();
	othr_running = true;
	othr_proxy_handle = p_event;
      } in
    set_event othr.othr_event;
    let _ =
      oothr # create_thread
	(fun () ->
	   othr.othr_thread <- get_current_thread_id();
	   init_cond # signal();
	   thread_body othr
	)
	() in
    init_cond # wait init_mutex;
    let f = f_cancel_output_thread othr in
    let keep_alive = (object end) in
    Gc.finalise f keep_alive;
    Gc.finalise netsys_close_event p_event;
    register_proxy proxy (I_output_thread(othr, keep_alive));
    (othr, proxy, keep_alive)
    

  let output_thread_write (othr,_,_) s pos len =
    if pos < 0 || len < 0 || pos > Bytes.length s - len then
      invalid_arg "Netsys_win32.output_thread_write";
    
    if len = 0 then
      0
    else (
      Netsys_oothr.serialize
	othr.othr_write_mutex   (* only one writer at a time *)
	(fun () ->
	   let b = test_event othr.othr_event in
	   if b then (
	     othr.othr_cmd_mutex # lock();
	     (* Look at what we have: *)
	     match othr.othr_write_cond with
	       | Some(`Exception e) ->
		   othr.othr_cmd_mutex # unlock();
		   raise e
	       | Some `Cancelled ->
		   othr.othr_cmd_mutex # unlock();
		   raise(Unix.Unix_error(Unix.EPERM, 
					 "Netsys_win32.output_thread_write",
					 ""))
	       | None ->
		   assert(othr.othr_buffer_len = 0);
		   let n = min len (Bytes.length othr.othr_buffer) in
		   Bytes.blit 
		     s pos
		     othr.othr_buffer 0
		     n;
		   othr.othr_buffer_len <- n;
		   othr.othr_cmd <- Some `Write;
		   othr.othr_cmd_cond # signal();
		   reset_event othr.othr_event;
		   othr.othr_cmd_mutex # unlock();
		   n
	   )
	   else
	     raise(Unix.Unix_error(Unix.EAGAIN, 
				   "Netsys_win32.output_thread_write", ""))
	)
	()
    )

  let close_output_thread (othr,_,_) =
    Netsys_oothr.serialize
      othr.othr_write_mutex   (* only one writer at a time *)
      (fun () ->
	 let b = test_event othr.othr_event in
	 if b then (
	   othr.othr_cmd_mutex # lock();
	   (* Look at what we have: *)
	   match othr.othr_write_cond with
	     | Some(`Exception e) ->
		 othr.othr_cmd_mutex # unlock();
		 raise e
	     | Some `Cancelled ->
		 othr.othr_cmd_mutex # unlock();
		 raise(Unix.Unix_error(Unix.EPERM, 
				       "Netsys_win32.close_output_thread",
				       ""))
	     | None ->
		 assert(othr.othr_buffer_len = 0);
		 othr.othr_cmd <- Some `Close;
		 othr.othr_cmd_cond # signal();
		 reset_event othr.othr_event;
		 othr.othr_cmd_mutex # unlock();
	 )
	 else
	   raise(Unix.Unix_error(Unix.EAGAIN, 
				 "Netsys_win32.close_output_thread", ""))
      )
      ()



  let output_thread_event (othr,_,_) =
    othr.othr_event

  let output_thread_proxy_descr (othr, proxy, _) = 
    netsys_set_auto_close_event_proxy othr.othr_proxy_handle false;
    proxy

end

let create_output_thread = OutputThread.create_output_thread
let output_thread_event = OutputThread.output_thread_event
let output_thread_write = OutputThread.output_thread_write
let cancel_output_thread = OutputThread.cancel_output_thread
let close_output_thread = OutputThread.close_output_thread
let output_thread_proxy_descr = OutputThread.output_thread_proxy_descr
let output_thread_descr (othr,_,_) = othr.othr_descr

let output_thread_write_string out s pos len =
  output_thread_write out (Bytes.unsafe_of_string s) pos len

OCaml

Innovation. Community. Security.