File: netchannels.ml

package info (click to toggle)
ocamlnet 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 51,764 kB
  • ctags: 16,446
  • sloc: ml: 148,419; ansic: 10,989; sh: 1,885; makefile: 1,355
file content (2169 lines) | stat: -rw-r--r-- 55,655 bytes parent folder | download | duplicates (6)
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
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
(* $Id$
 * ----------------------------------------------------------------------
 *
 *)

open Netsys_types
open Netstring_tstring

exception Closed_channel
exception Buffer_underrun
exception Command_failure of Unix.process_status

let () =
  Netexn.register_printer
    (Command_failure(Unix.WEXITED 0))
    (fun e ->
       match e with
	 | Command_failure ps ->
	     let ps_str =
	       match ps with
		 | Unix.WEXITED n -> "WEXITED " ^ string_of_int n
		 | Unix.WSIGNALED n -> "WSIGNALED " ^ string_of_int n
		 | Unix.WSTOPPED n -> "WSTOPPED " ^ string_of_int n
	     in
	     "Netchannels.Command_failure(" ^ ps_str ^ ")"
	 | _ ->
	     assert false
    )

let () =
  Netsys_signal.init()


class type rec_in_channel = object
  method input : Bytes.t -> int -> int -> int
  method close_in : unit -> unit
end

class type raw_in_channel = object
  inherit rec_in_channel
  method pos_in : int             (* number of read characters *)
end

type input_result =
    [ `Data of int
    | `Separator of string
    ]

class type enhanced_raw_in_channel =
object 
  inherit raw_in_channel
  method private enhanced_input_line : unit -> string
  method private enhanced_input : Bytes.t -> int -> int -> input_result
end

class type rec_out_channel = object
  method output : Bytes.t -> int -> int -> int
  method close_out : unit -> unit
  method flush : unit -> unit
end

class type raw_out_channel = object
  inherit rec_out_channel
  method pos_out : int             (* number of written characters *)
end

class type raw_io_channel = object
  inherit raw_in_channel
  inherit raw_out_channel
end

class type compl_in_channel = object
  (* Classic operations: *)
  method really_input : Bytes.t -> int -> int -> unit
  method really_input_string : int -> string
  method input_char : unit -> char
  method input_line : unit -> string
  method input_byte : unit -> int
end

class type in_obj_channel = object
  inherit raw_in_channel
  inherit compl_in_channel
end


class type compl_out_channel = object
  (* Classic operations: *)
  method really_output : Bytes.t -> int -> int -> unit
  method really_output_string : string -> int -> int -> unit
  method output_char : char -> unit
  method output_bytes : Bytes.t -> unit
  method output_string : string -> unit
  method output_byte : int -> unit
  method output_buffer : Buffer.t -> unit
  method output_channel : ?len:int -> in_obj_channel -> unit
      (* ~len: optionally limit the number of bytes *)
end


class type out_obj_channel = object
  inherit raw_out_channel
  inherit compl_out_channel
end

class type io_obj_channel = object
  inherit in_obj_channel
  inherit out_obj_channel
end

class type trans_out_obj_channel = object
  inherit out_obj_channel

  method commit_work : unit -> unit
  method rollback_work : unit -> unit
end
;;


(* error_behavior: currently not used. This was a proposal to control
 * error handling, but it is not clear whether it is really 
 * useful or not.
 * I do not delete these types because they remind us of this
 * possibility. Maybe we find an outstanding example for them, and
 * want to have them back.
 *)

type error_behavior = 
    [ `Close | `Fun of (unit -> unit) | `None ]

type extended_error_behavior =
    [ `Close | `Rollback | `Fun of (unit -> unit) | `None ]


type close_mode = [ `Commit | `Rollback ];;


(* Delegation *)

class rec_in_channel_delegation ?(close=true) (ch:rec_in_channel) =
object(self)
  method input = ch#input
  method close_in() = if close then ch#close_in()
end


class raw_in_channel_delegation ?(close=true) (ch:raw_in_channel) =
object(self)
  method input = ch#input
  method close_in() = if close then ch#close_in()
  method pos_in = ch#pos_in
end


class in_obj_channel_delegation ?(close=true) (ch:in_obj_channel) =
object(self)
  method input = ch#input
  method close_in() = if close then ch#close_in()
  method pos_in = ch#pos_in
  method really_input = ch#really_input
  method really_input_string = ch#really_input_string
  method input_char = ch#input_char
  method input_line = ch#input_line
  method input_byte = ch#input_byte
end


class rec_out_channel_delegation ?(close=true) (ch:rec_out_channel) =
object(self)
  method output = ch#output
  method close_out() = if close then ch#close_out()
  method flush = ch#flush
end


class raw_out_channel_delegation ?(close=true) (ch:raw_out_channel) =
object(self)
  method output = ch#output
  method close_out() = if close then ch#close_out()
  method flush = ch#flush
  method pos_out = ch#pos_out
end


class out_obj_channel_delegation ?(close=true) (ch:out_obj_channel) =
object(self)
  method output = ch#output
  method close_out() = if close then ch#close_out()
  method flush = ch#flush
  method pos_out = ch#pos_out
  method really_output = ch#really_output
  method really_output_string = ch#really_output_string
  method output_char = ch#output_char
  method output_string = ch#output_string
  method output_bytes = ch#output_bytes
  method output_byte = ch#output_byte
  method output_buffer = ch#output_buffer
  method output_channel = ch#output_channel
end


(****************************** input ******************************)

class input_channel ?(onclose=fun () -> ()) ch (* : in_obj_channel *) =
object (self)
  val ch = ch
  val mutable closed = false

  method private complain_closed() =
    raise Closed_channel

  method input buf pos len = 
    if closed then self # complain_closed();
    try
      if len=0 then raise Sys_blocked_io;
      let n = Pervasives.input ch buf pos len in
      if n=0 then raise End_of_file else n
    with
	Sys_blocked_io -> 0

  method really_input buf pos len = 
    if closed then self # complain_closed();
    Pervasives.really_input ch buf pos len 

  method really_input_string len = 
    if closed then self # complain_closed();
    #ifdef HAVE_BYTES
      Pervasives.really_input_string ch len 
    #else
      let buf = String.create len in
      Pervasives.really_input ch buf 0 len;
      buf
    #endif

  method input_char () =
    if closed then self # complain_closed();
    Pervasives.input_char ch 

  method input_line () =
    if closed then self # complain_closed();
    Pervasives.input_line ch 

  method input_byte () =
    if closed then self # complain_closed();
    Pervasives.input_byte ch 

  method close_in () =
    if not closed then (
      Pervasives.close_in ch; closed <- true; onclose()
    )

  method pos_in =
    if closed then self # complain_closed();
    Pervasives.pos_in ch 
end
;;

let input_channel = new input_channel


class input_command cmd =
let ch = Unix.open_process_in cmd in
object (self)
  inherit input_channel ch as super

  method close_in() =
    if not closed then (
      let p = Unix.close_process_in ch in
      closed <- true;
      if p <> Unix.WEXITED 0 then
	raise (Command_failure p);
    )
end
;;

let input_command = new input_command


class ['t] input_generic name ops ?(pos = 0) ?len (s:'t) : in_obj_channel =
object (self)
  val mutable str = s
  val mutable str_len = 
    match len with 
	None   -> ops.length s 
      | Some l -> pos + l

  val mutable str_pos = pos
  val mutable closed = false

  initializer
    if str_pos < 0 || str_pos > ops.length str || 
       str_len < 0 || str_len > ops.length s 
    then
      invalid_arg ("new Netchannels." ^ name)
	  

  method private complain_closed() =
    raise Closed_channel

  method input buf pos len =
    if closed then self # complain_closed();
    if pos < 0 || len < 0 || pos+len > Bytes.length buf then
      invalid_arg "input";

    let n = min len (str_len - str_pos) in
    ops.blit_to_bytes str str_pos buf pos n;
    
    str_pos <- str_pos + n;

    if n=0 && len>0 then raise End_of_file else n


  method really_input buf pos len =
    if closed then self # complain_closed();
    if pos < 0 || len < 0 || pos+len > Bytes.length buf then
      invalid_arg "really_input";

    let n = self # input buf pos len in
    if n <> len then raise End_of_file;
    ()


  method really_input_string len =
    if closed then self # complain_closed();
    if len < 0 then
      invalid_arg "really_input_string";

    let buf = Bytes.create len in
    let n = self # input buf 0 len in
    if n <> len then raise End_of_file;
    Bytes.to_string buf


  method input_char() =
    if closed then self # complain_closed();
    if str_pos >= str_len then raise End_of_file;
    let c = ops.get str str_pos in
    str_pos <- str_pos + 1;
    c


  method input_line() =
    if closed then self # complain_closed();
    try
      let k = ops.index_from str str_pos '\n' in
      (* CHECK: Are the different end of line conventions important here? *)
      let line = ops.substring str str_pos (k - str_pos) in
      str_pos <- k+1;
      line
    with
	Not_found ->
	  if str_pos >= str_len then raise End_of_file;
	  (* Implicitly add linefeed at the end of the file: *)
	  let line = ops.substring str str_pos (str_len - str_pos) in
	  str_pos <- str_len;
	  line


  method input_byte() =
    Char.code (self # input_char())


  method close_in() =
    (* str <- ""; *)
    closed <- true;


  method pos_in = 
    if closed then self # complain_closed();
    str_pos

end
;;


class input_string =
  [string] input_generic "input_string" Netstring_tstring.string_ops

let input_string = new input_string

class input_bytes =
  [Bytes.t] input_generic "input_bytes" Netstring_tstring.bytes_ops

let input_bytes = new input_bytes

class input_memory =
  [memory] input_generic "input_memory" Netstring_tstring.memory_ops

let input_memory = new input_memory

let input_tstring ?pos ?len ts =
  match ts with
    | `String s -> input_string ?pos ?len s
    | `Bytes s -> input_bytes ?pos ?len s
    | `Memory s -> input_memory ?pos ?len s


class type nb_in_obj_channel =
object
  inherit in_obj_channel
  method shutdown : unit -> unit
end


class input_netbuffer ?(keep_data=false) b : nb_in_obj_channel =
object (self)
  val mutable b = b
  val mutable offset = 0
  val mutable eof = false
  val mutable closed = false
  val mutable ch_pos = 0

  method private complain_closed() =
    raise Closed_channel

  method private input_into : type t . (int -> int -> t) -> int -> t =
    fun f len ->
      let n = min len (Netbuffer.length b - offset) in
      if n = 0 && len>0 then begin
        if eof then raise End_of_file else raise Buffer_underrun
      end
      else begin
        let result = f offset n in
        if keep_data then
          offset <- offset + n
        else
          Netbuffer.delete b 0 n;
        ch_pos <- ch_pos + n;
        result
      end

  method input buf pos len =
    if closed then self # complain_closed();
    if pos < 0 || len < 0 || pos > Bytes.length buf - len then
      invalid_arg "input";

    self # input_into
      (fun b_offs n ->
        Netbuffer.blit b b_offs buf pos n;
        n
      )
      len

  method really_input buf pos len =
    if closed then self # complain_closed();
    if pos < 0 || len < 0 || pos+len > Bytes.length buf then
      invalid_arg "really_input";

    let n = self # input buf pos len in
    if n <> len then raise End_of_file;
    ()


  method really_input_string len =
    if closed then self # complain_closed();
    if len < 0 then
      invalid_arg "really_input_string";

    self # input_into
      (fun b_offs n ->
         if n <> len then raise End_of_file;
         Netbuffer.sub b b_offs n
      )
      len

  method input_char() =
    if closed then self # complain_closed();
    let s = Bytes.create 1 in
    match self # input s 0 1 with
      | 1 -> Bytes.get s 0
      | _ -> assert false


  method input_line() =
    if closed then self # complain_closed();
    try
      let k = Netbuffer.index_from b offset '\n' in
      (* CHECK: Are the different end of line conventions important here? *)
      let line = Netbuffer.sub b offset (k - offset) in
      if keep_data then
        offset <- offset + k + 1
      else
        Netbuffer.delete b 0 (k+1);
      ch_pos <- ch_pos + k + 1;
      line
    with
	Not_found ->
	  if eof then begin
            let n = Netbuffer.length b - offset in
	    if n=0 then raise End_of_file;
	    (* Implicitly add linefeed at the end of the file: *)
	    let line = Netbuffer.sub b offset n in
            if keep_data then
              offset <- offset + n
            else
	      Netbuffer.clear b;
	    ch_pos <- ch_pos + n;
	    line
	  end
	  else raise Buffer_underrun


  method input_byte() =
    Char.code (self # input_char())


  method close_in() =
    closed <- true;


  method pos_in = 
    if closed then self # complain_closed();
    ch_pos


  method shutdown() = eof <- true
end
;;


let create_input_netbuffer ?keep_data b =
  let ch = new input_netbuffer ?keep_data b in
  (ch :> in_obj_channel), (ch # shutdown)
;;


let lexbuf_of_in_obj_channel (objch : in_obj_channel) : Lexing.lexbuf =
  let fill_buffer buf len =
    try
      let n = objch # input buf 0 len in
      if n=0 then failwith "Netchannels.lexbuf_of_in_obj_channel: No data (non-blocking I/O?)";
      n
    with
	End_of_file -> 0
  in
  Lexing.from_function fill_buffer
;;


let bytes_of_in_obj_channel (objch : in_obj_channel) : Bytes.t =
  (* There are similarities to copy_channel below. *)
  (* The following algorithm uses only up to 2 * N memory, not 3 * N
   * as with the Buffer module.
   *)
  let slen = 1024 in
  let l = ref [] in
  let k = ref 0 in
  try
    while true do
      let s = Bytes.create slen in
      let n = objch # input s 0 slen in
      if n = 0 then 
	failwith "Netchannels.bytes_of_in_obj_channel: No data (non-blocking I/O?)";
      k := !k + n;
      if n < slen then
	l := (Bytes.sub s 0 n) :: !l
      else
	l := s :: !l;
    done;
    assert false
  with
      End_of_file -> 
	let s = Bytes.create !k in
	while !l <> [] do
	  match !l with
	      u :: l' ->
		let n = Bytes.length u in
		k := !k - n;
		Bytes.blit u 0 s !k n;
		l := l'
	    | [] -> assert false
	done;
	assert (!k = 0);
	s
;;

let string_of_in_obj_channel objch =
  Bytes.unsafe_to_string (bytes_of_in_obj_channel objch)


let lines_of_in_obj_channel ch =
  let acc = ref [] in
  try
    while true do
      acc := ch#input_line() :: !acc
    done;
    assert false
  with
    | End_of_file -> List.rev !acc
;;


let with_in_obj_channel ch f =
  try
    let result = f ch in
    ( try ch # close_in() with Closed_channel -> ());
    result
  with
      e ->
	( try ch # close_in() with Closed_channel -> ());
	raise e
;;


class virtual augment_raw_in_channel =
object (self)
  method virtual input : Bytes.t -> int -> int -> int
  method virtual close_in : unit -> unit
  method virtual pos_in : int

  method really_input s pos len =
    let rec read_rest n =
      if n < len then
	let m = self # input s (pos+n) (len-n) in
	if m = 0 then raise Sys_blocked_io;
	read_rest (n+m)
      else
	()
    in
    read_rest 0

  method really_input_string len =
    let b = Bytes.create len in
    self#really_input b 0 len;
    Bytes.unsafe_to_string b

  method input_char () =
    let s = Bytes.create 1 in
    self # really_input s 0 1;
    Bytes.get s 0

  method input_byte () =
    let s = Bytes.create 1 in
    self # really_input s 0 1;
    Char.code (Bytes.get s 0)

  method input_line () =
    let s = Bytes.create 1 in
    let b = Buffer.create 80 in
    let m = self # input s 0 1 in
    if m = 0 then raise Sys_blocked_io;
    while Bytes.get s 0 <> '\n' do
      Buffer.add_char b (Bytes.get s 0);
      try
	let m = self # input s 0 1 in
	if m = 0 then raise Sys_blocked_io;
      with
	  End_of_file ->
            Bytes.set s 0 '\n'
    done;
    Buffer.contents b

end
;;


class lift_raw_in_channel r =
object(self)
  inherit augment_raw_in_channel

  method input s p l =
    r # input s p l

  method close_in () =
    r # close_in()

  method pos_in =
    r # pos_in

end;;


class lift_rec_in_channel ?(start_pos_in = 0) (r : rec_in_channel) =
object(self)
  inherit augment_raw_in_channel

  val mutable closed = false
  val mutable pos_in = start_pos_in

  method input s p l =
    if closed then raise Closed_channel;
    let n = r # input s p l in
    pos_in <- pos_in + n;
    n

  method close_in () =
    if not closed then (
      closed <- true;
      r # close_in()
    )

  method pos_in =
    if closed then raise Closed_channel;
    pos_in

end;;


type eol_status =
    EOL_not_found
  | EOL_partially_found of int (* Position *)
  | EOL_found of int * int     (* Position, length *)


exception Pass_through

class buffered_raw_in_channel
      ?(eol = [ "\n" ])
      ?(buffer_size = 4096)
      ?(pass_through = max_int)
      (ch : raw_in_channel) : enhanced_raw_in_channel =
object (self)
  val out = ch
  val bufsize = buffer_size
  val buf = Bytes.create buffer_size
  val mutable bufpos = 0
  val mutable buflen = 0
  val mutable eof = false
  val mutable closed = false

  initializer
    if List.exists(fun s -> s = "") eol then
      invalid_arg "Netchannels.buffered_raw_in_channel";
    if List.exists(fun s -> String.length s > buffer_size) eol then
      invalid_arg "Netchannels.buffered_raw_in_channel";

  method input s pos len =
    if closed then raise Closed_channel;
    try
      if len > 0 then (
	if bufpos = buflen then (
	  if len >= pass_through then
	    raise Pass_through
	  else
	    self # refill();
	);
	let n = min len (buflen - bufpos) in
	Bytes.blit buf bufpos s pos n;
	bufpos <- bufpos + n;
	n
      )
      else 0
    with Pass_through ->
      ch # input s pos len


  method private refill() =
    let d = bufpos in
    if d > 0 && d < buflen then (
      Bytes.blit buf d buf 0 (buflen-d)
    );
    bufpos <- 0;
    buflen <- buflen - d;
    try
      assert(bufsize > buflen);  (* otherwise problems... *)
      let n = ch # input buf buflen (bufsize-buflen) in  (* or End_of_file *)
      if n = 0 then raise Sys_blocked_io;
      buflen <- buflen+n;
    with
	End_of_file as exn ->
	  eof <- true;
	  raise exn


  method close_in () =
    if not closed then (
      ch # close_in();
      closed <- true
    )

  method pos_in =
    (ch # pos_in) - (buflen - bufpos)


  method private find_eol() =
    (* Try all strings from [eol] in turn. For every string we may
     * have three results:
     * - Not found
     * - Partially found
     * - Found
     * The eol delimiter is only found if there are no partial
     * results, and at least one positive result. The longest
     * string is taken.
     *)

    let find_this_eol eol =
      (* Try to find the eol string [eol] in [buf] starting at
       * [bufpos] up to [buflen]. Return [eol_status].
       *)
      let eol0 = eol.[0] in
      try
	let k = Bytes.index_from buf bufpos eol0 in (* or Not_found *)
	if k>=buflen then raise Not_found;
	let k' = min buflen (k+String.length eol) in
	let s = Bytes.sub_string buf k (k' - k) in
	if s = eol then
	  EOL_found(k, String.length eol)
	else
	  if not eof && String.sub eol 0 (String.length s) = s then
	    EOL_partially_found k
	  else
	    EOL_not_found
      with
	    Not_found -> EOL_not_found
    in  

    let rec find_best_eol best eol_result =
      match eol_result with
	  EOL_not_found :: eol_result' ->
	    find_best_eol best eol_result'
	| EOL_partially_found pos as r :: eol_result' ->
	    ( match best with
		  EOL_partially_found pos' ->
		    if pos < pos' then
		      find_best_eol r eol_result'
		    else
		      find_best_eol best eol_result'
		| _ ->
		    find_best_eol r eol_result'
	    )
	| EOL_found(pos,len) as r :: eol_result' ->
	    ( match best with
		  EOL_found(pos',len') ->
		    if pos < pos' || (pos = pos' && len > len') then
		      find_best_eol r eol_result'
		    else
		      find_best_eol best eol_result'
		| EOL_partially_found _ ->
		    find_best_eol best eol_result'
		| EOL_not_found ->
		    find_best_eol r eol_result'
	    )
	| [] ->
	    best
    in
    let eol_results = List.map find_this_eol eol in
    find_best_eol EOL_not_found eol_results

  method private enhanced_input s pos len : input_result =
    if closed then raise Closed_channel;
    if len > 0 then (
      if bufpos = buflen then (
	self # refill(); (* may raise End_of_file *)
      );
      let result = ref None in
      while !result = None do
	let best = self # find_eol() in
	match best with
	    EOL_not_found -> 
	      let n = min len (buflen - bufpos) in
	      Bytes.blit buf bufpos s pos n;
	      bufpos <- bufpos + n;
	      result := Some(`Data n)
	  | EOL_found(p,l) ->
	      if p = bufpos then (
		bufpos <- bufpos + l;
		result := Some(`Separator(Bytes.sub_string buf p l))
	      )
	      else (
		let n = min len (p - bufpos) in
		Bytes.blit buf bufpos s pos n;
		bufpos <- bufpos + n;
		result := Some(`Data n)
	      )
	  | EOL_partially_found p ->
	      if p = bufpos then (
		try self # refill()
		with End_of_file -> ()
		(* ... and continue! *)
	      )
	      else (
		let n = min len (p - bufpos) in
		Bytes.blit buf bufpos s pos n;
		bufpos <- bufpos + n;
		result := Some(`Data n)
	      )
      done;
      match !result with
	  None -> assert false
	| Some r -> r
    )
    else `Data 0

  method private enhanced_input_line() =
    if closed then raise Closed_channel;
    let b = Netbuffer.create 80 in
    let eol_found = ref false in
    if bufpos = buflen then (
      self # refill();  (* may raise End_of_file *)
    );
    while not !eol_found do
      let best = self # find_eol() in
      try
	match best with
	    EOL_not_found ->
	      Netbuffer.add_subbytes b buf bufpos (buflen-bufpos);
	      bufpos <- buflen;
	      self # refill();     (* may raise End_of_file *)
	  | EOL_partially_found pos ->
	      Netbuffer.add_subbytes b buf bufpos (pos-bufpos);
	      bufpos <- pos;
	      self # refill();     (* may raise End_of_file *)
	  | EOL_found(pos,len) ->
	      Netbuffer.add_subbytes b buf bufpos (pos-bufpos);
	      bufpos <- pos+len;
	      eol_found := true
	with
	    End_of_file -> 
	      bufpos <- 0;
	      buflen <- 0;
	      eof <- true;
	      eol_found := true
    done;
    Netbuffer.contents b
end
;;


class lift_raw_in_channel_buf ?eol ?buffer_size ?pass_through r =
object(self)
  inherit buffered_raw_in_channel ?eol ?buffer_size ?pass_through r
  inherit augment_raw_in_channel

  method input_line () =
    self # enhanced_input_line()
end;;


type lift_in_arg = [ `Rec of rec_in_channel | `Raw of raw_in_channel ]

let lift_in ?(eol = ["\n"]) ?(buffered=true) ?buffer_size ?pass_through
            (x : lift_in_arg) =
  match x with
      `Rec r when not buffered ->
	if eol <> ["\n"] then invalid_arg "Netchannels.lift_in";
	new lift_rec_in_channel r
    | `Rec r when buffered ->
	let r' = new lift_rec_in_channel r in
	new lift_raw_in_channel_buf
	  ~eol ?buffer_size ?pass_through 
	  (r' :> raw_in_channel)
    | `Raw r when not buffered ->
	if eol <> ["\n"] then invalid_arg "Netchannels.lift_in";
	new lift_raw_in_channel r
    | `Raw r when buffered ->
	new lift_raw_in_channel_buf ~eol ?buffer_size ?pass_through r
;;


(****************************** output ******************************)

exception No_end_of_file

let copy_channel 
      ?(buf = Bytes.create 1024)
      ?len (src_ch : in_obj_channel) (dest_ch : out_obj_channel) =
  (* Copies contents from src_ch to dest_ch. Returns [true] if at EOF.
   *)
  let slen = Bytes.length buf in
  let k = ref 0 in
  try
    while true do
      let m = min slen (match len with Some x -> x - !k | None -> max_int) in
      if m <= 0 then raise No_end_of_file;
      let n = src_ch # input buf 0 m in
      if n = 0 then raise Sys_blocked_io;
      dest_ch # really_output buf 0 n;
      k := !k + n
    done;
    assert false
  with
      End_of_file ->
	true
    | No_end_of_file ->
	false
;;


class output_channel ?(onclose = fun () -> ()) ch 
		     (* : out_obj_channel *) =
  let errflag = ref false in
  let monitored f arg =
    try 
      let r = f arg in
      errflag := false;
      r
    with
      | error ->
	  errflag := true;
	  raise error in

object (self)
  val ch = ch
  val onclose = onclose
  val mutable closed = false

  method private complain_closed() =
    raise Closed_channel

  method output buf pos len =
    if closed then self # complain_closed();
    (* Pervasives.output does not support non-blocking I/O directly.
     * Work around it:
     *)
    let p0 = Pervasives.pos_out ch in
    try 
      Pervasives.output ch buf pos len;
      errflag := false;
      len
    with
      | Sys_blocked_io ->
	  let p1 = Pervasives.pos_out ch in
	  errflag := false;
	  p1 - p0
      | error ->
	  errflag := true;
	  raise error

  method really_output buf pos len =
    if closed then self # complain_closed();
    monitored (Pervasives.output ch buf pos) len

  method really_output_string buf pos len =
    if closed then self # complain_closed();
    #ifdef HAVE_BYTES
      monitored (Pervasives.output_substring ch buf pos) len
    #else
      monitored (Pervasives.output ch buf pos) len
    #endif

  method output_char c =
    if closed then self # complain_closed();
    monitored (Pervasives.output_char ch) c

  method output_string s =
    if closed then self # complain_closed();
    monitored (Pervasives.output_string ch) s

  method output_bytes s =
    if closed then self # complain_closed();
    #ifdef HAVE_BYTES
      monitored (Pervasives.output_bytes ch) s
    #else
      monitored (Pervasives.output_string ch) s
    #endif

  method output_byte b =
    if closed then self # complain_closed();
    monitored (Pervasives.output_byte ch) b

  method output_buffer b =
    if closed then self # complain_closed();
    monitored(Buffer.output_buffer ch) b

  method output_channel ?len ch =
    if closed then self # complain_closed();
    ignore
      (monitored
	 (copy_channel ?len ch)
	 (self : #out_obj_channel :> out_obj_channel))

  method flush() =
    if closed then self # complain_closed();
    monitored Pervasives.flush ch

  method close_out() =
    if not closed then (
      ( try
	  (* if !errflag is set, we know that the immediately preceding
	     operation raised an exception, and we are now likely in the
	     exception handler
	   *)
	  if !errflag then
	    Pervasives.close_out_noerr ch
	  else
	    Pervasives.close_out ch; 
	  closed <- true; 
	with
	  | error ->
	      let bt = Printexc.get_backtrace() in
	      Netlog.logf `Err
		"Netchannels.output_channel: \
                   Suppressed error in close_out: %s - backtrace: %s"
		(Netexn.to_string error) bt;
	      Pervasives.close_out_noerr ch;
	      closed <- true; 
      );
      onclose()
    )

  method pos_out = 
    if closed then self # complain_closed();
    Pervasives.pos_out ch

end
;;


class output_command ?onclose cmd =
  let ch = Unix.open_process_out cmd in
object (self)
  inherit output_channel ?onclose ch as super

  method close_out() =
    if not closed then (
      let p = Unix.close_process_out ch in
      closed <- true;
      onclose();
      if p <> Unix.WEXITED 0 then
	raise (Command_failure p);  (* Keep this *)
    )
end
;;


class output_buffer ?(onclose = fun () -> ()) buffer : out_obj_channel =
object(self)
  val buffer = buffer
  val onclose = onclose
  val mutable closed = false

  method private complain_closed() =
    raise Closed_channel

  method output buf pos len =
    if closed then self # complain_closed();
    #ifdef HAVE_BYTES
      Buffer.add_subbytes buffer buf pos len;
    #else
      Buffer.add_substring buffer buf pos len;
    #endif
    len

  method really_output buf pos len =
    if closed then self # complain_closed();
    #ifdef HAVE_BYTES
      Buffer.add_subbytes buffer buf pos len;
    #else
      Buffer.add_substring buffer buf pos len;
    #endif

  method really_output_string buf pos len =
    if closed then self # complain_closed();
    Buffer.add_substring buffer buf pos len;

  method output_char c =
    if closed then self # complain_closed();
    Buffer.add_char buffer c

  method output_string s =
    if closed then self # complain_closed();
    Buffer.add_string buffer s

  method output_bytes s =
    if closed then self # complain_closed();
    #ifdef HAVE_BYTES
      Buffer.add_bytes buffer s
    #else
      Buffer.add_string buffer s
    #endif

  method output_byte b =
    if closed then self # complain_closed();
    Buffer.add_char buffer (Char.chr b)

  method output_buffer b =
    if closed then self # complain_closed();
    Buffer.add_buffer buffer b

  method output_channel ?len ch =
    if closed then self # complain_closed();
    ignore(copy_channel ?len ch (self : #out_obj_channel :> out_obj_channel))

  method flush() = 
    if closed then self # complain_closed();
    ()

  method close_out() =
    if not closed then (
      closed <- true;
      onclose()
    )

  method pos_out = 
    if closed then self # complain_closed();
    Buffer.length buffer

end
;;


class output_netbuffer ?(onclose = fun () -> ()) buffer : out_obj_channel =
object(self)
  val buffer = buffer
  val onclose = onclose
  val mutable closed = false
  val mutable ch_pos = 0

  method private complain_closed() =
    raise Closed_channel

  method output buf pos len =
    if closed then self # complain_closed();
    Netbuffer.add_subbytes buffer buf pos len;
    ch_pos <- ch_pos + len;
    len

  method really_output buf pos len =
    if closed then self # complain_closed();
    Netbuffer.add_subbytes buffer buf pos len;
    ch_pos <- ch_pos + len;

  method really_output_string buf pos len =
    if closed then self # complain_closed();
    Netbuffer.add_substring buffer buf pos len;
    ch_pos <- ch_pos + len;

  method output_char c =
    if closed then self # complain_closed();
    Netbuffer.add_string buffer (String.make 1 c);
    ch_pos <- ch_pos + 1;

  method output_string s =
    if closed then self # complain_closed();
    Netbuffer.add_string buffer s;
    ch_pos <- ch_pos + String.length s

  method output_bytes s =
    if closed then self # complain_closed();
    Netbuffer.add_bytes buffer s;
    ch_pos <- ch_pos + Bytes.length s

  method output_byte b =
    if closed then self # complain_closed();
    Netbuffer.add_string buffer (String.make 1 (Char.chr b));
    ch_pos <- ch_pos + 1;

  method output_buffer b =
    if closed then self # complain_closed();
    Netbuffer.add_string buffer (Buffer.contents b);
    ch_pos <- ch_pos + Buffer.length b;

  method output_channel ?len ch =
    if closed then self # complain_closed();
    ignore(copy_channel ?len ch (self : #out_obj_channel :> out_obj_channel))

  method flush() = 
    if closed then self # complain_closed();
    ()

  method close_out() =
    if not closed then (
      closed <- true;
      onclose()
    )

  method pos_out = 
    if closed then self # complain_closed();
    ch_pos
    (* We cannot return Netbuffer.length b as [pos_out] (like in the class
     * [output_buffer]) because the user of this class is allowed to delete
     * data from the netbuffer. So we manually count how many bytes are
     * ever appended to the netbuffer.
     * This behavior is especially needed by [pipe_channel] below.
     *)
end
;;


class output_null ?(onclose = fun () -> ()) () : out_obj_channel =
object(self)
  val mutable closed = false
  val mutable pos = 0
  method private complain_closed() =
    raise Closed_channel
  method output s start len =
    if closed then self # complain_closed();
    pos <- pos + len; len
  method really_output s start len =
    if closed then self # complain_closed();
    pos <- pos + len
  method really_output_string s start len =
    if closed then self # complain_closed();
    pos <- pos + len
  method output_char _ =
    if closed then self # complain_closed();
    pos <- pos + 1
  method output_string s =
    if closed then self # complain_closed();
    pos <- pos + String.length s
  method output_bytes s =
    if closed then self # complain_closed();
    pos <- pos + Bytes.length s
  method output_byte _ =
    if closed then self # complain_closed();
    pos <- pos + 1
  method output_buffer b =
    if closed then self # complain_closed();
    pos <- pos + Buffer.length b
  method output_channel ?len ch =
    if closed then self # complain_closed();
    ignore(copy_channel ?len ch (self : #out_obj_channel :> out_obj_channel))
  method flush() = 
    if closed then self # complain_closed();
  method close_out() =
    closed <- true
  method pos_out =
    if closed then self # complain_closed();
    pos
end ;;


let with_out_obj_channel ch f =
  try
    let result = f ch in
    (* we _have_ to flush here because close_out often does no longer
       report exceptions
     *)
    ( try ch # flush() with Closed_channel -> ());
    ( try ch # close_out() with Closed_channel -> ());
    result
  with
      e ->
	( try ch # close_out() with Closed_channel -> ());
	raise e
;;


class virtual augment_raw_out_channel =
object (self)
  method virtual output : Bytes.t -> int -> int -> int
  method virtual close_out : unit -> unit
  method virtual flush : unit -> unit
  method virtual pos_out : int

  method really_output s pos len =
    let rec print_rest n =
      if n < len then
	let m = self # output s (pos+n) (len-n) in
	if m=0 then raise Sys_blocked_io;
	print_rest (n+m)
      else
	()
    in
    print_rest 0

  method really_output_string s pos len =
    self # really_output (Bytes.unsafe_of_string s) pos len

  method output_char c =
    ignore(self # output (Bytes.make 1 c) 0 1)

  method output_byte n =
    ignore(self # output (Bytes.make 1 (Char.chr n)) 0 1)

  method output_string s =
    self # really_output_string s 0 (String.length s)

  method output_bytes s =
    self # really_output s 0 (Bytes.length s)

  method output_buffer b =
    self # output_string (Buffer.contents b)

  method output_channel ?len ch =
    ignore(copy_channel ?len ch (self : #out_obj_channel :> out_obj_channel))

end
;;


class lift_raw_out_channel (r : raw_out_channel) =
object(self)
  inherit augment_raw_out_channel

  method output s p l =
    r # output s p l

  method flush () =
    r # flush()

  method close_out () =
    r # close_out()

  method pos_out =
    r # pos_out

end;;


class lift_rec_out_channel ?(start_pos_out = 0) (r : rec_out_channel) =
object(self)
  inherit augment_raw_out_channel

  val mutable closed = false
  val mutable pos_out = start_pos_out

  method output s p l =
    if closed then raise Closed_channel;
    let n = r # output s p l in
    pos_out <- pos_out + n;
    n

  method flush() =
    if closed then raise Closed_channel;
    r # flush();

  method close_out () =
    if not closed then (
      closed <- true;
      r # close_out()
    )

  method pos_out =
    if closed then raise Closed_channel;
    pos_out

end;;


class buffered_raw_out_channel 
      ?(buffer_size = 4096)
      ?(pass_through = max_int)
      (ch : raw_out_channel) : raw_out_channel =
object (self)
  val out = ch
  val bufsize = buffer_size
  val buf = Bytes.create buffer_size
  val mutable bufpos = 0
  val mutable closed = false

  method output s pos len =
    if closed then raise Closed_channel;
    if bufpos=0 && len >= pass_through then
      ch # output s pos len
    else
      let n = min len (bufsize - bufpos) in
      Bytes.blit s pos buf bufpos n;
      bufpos <- bufpos + n;
      if bufpos = bufsize then
	self # flush();
      n

  method flush() =
    let k = ref 0 in
    while !k < bufpos do
      k := !k + (ch # output buf !k (bufpos - !k))
    done;
    bufpos <- 0;
    ch # flush()

  method close_out() = 
    if not closed then (
      ( try
	  self # flush()
	with
	  | error ->
	      let bt = Printexc.get_backtrace() in
	      Netlog.logf `Err
		"Netchannels.buffered_raw_out_channel: \
                  Suppressed error in close_out: %s - backtrace: %s"
		(Netexn.to_string error) bt;
      );
      ch # close_out();
      closed <- true
    )

  method pos_out =
    (ch # pos_out) + bufpos
end
;;


type lift_out_arg = [ `Rec of rec_out_channel | `Raw of raw_out_channel ]

let lift_out ?(buffered=true) ?buffer_size ?pass_through (x : lift_out_arg) =
  match x with
      `Rec r when not buffered ->
	new lift_rec_out_channel r
    | `Rec r when buffered ->
	let r' = new lift_rec_out_channel r in
	let r'' = 
	  new buffered_raw_out_channel
	    ?buffer_size ?pass_through (r' :> raw_out_channel) in
	new lift_raw_out_channel r''
    | `Raw r when not buffered ->
	new lift_raw_out_channel r
    | `Raw r when buffered ->
	let r' = new buffered_raw_out_channel ?buffer_size ?pass_through r in
	new lift_raw_out_channel r'
;;


(************************* raw channels *******************************)


let norestart _ _ _ f arg =
  try
    f arg
  with
    | Unix.Unix_error(Unix.EAGAIN,_,_)
    | Unix.Unix_error(Unix.EWOULDBLOCK,_,_)
    | Netsys_types.EAGAIN_RD
    | Netsys_types.EAGAIN_WR ->
         0


let shutdown_fd mode fd_style fd =
  try
    ignore
      (Netsys.restart_wait mode fd_style fd
         (fun () ->
            Netsys.gshutdown fd_style fd Unix.SHUTDOWN_ALL; 0
         )
         ()
      )
  with
    | Netsys.Shutdown_not_supported -> ()
    | Unix.Unix_error(Unix.EPERM, _, _) -> ()


class input_descr_prelim ?(blocking=true) ?(start_pos_in = 0) ?fd_style fd =
  let fd_style =
    match fd_style with
      | None -> Netsys.get_fd_style fd
      | Some st -> st in
  let wrapper =
    if blocking then Netsys.restart_wait else norestart in
object (self)
  val fd_in = fd
  val mutable pos_in = start_pos_in
  val mutable closed_in = false

  method private complain_closed() =
    raise Closed_channel

  method input buf pos len =
    if closed_in then self # complain_closed();
    wrapper `R fd_style fd
      (fun () ->
         let n = Netsys.gread fd_style fd_in buf pos len in
         pos_in <- pos_in + n;
         if n=0 && len>0 then raise End_of_file;
         n
      )
      ()
	  
  
  method close_in () =
    if not closed_in then (
      (* The gshutdown call only exists because of TLS: *)
      shutdown_fd `R fd_style fd;
      Netsys.gclose fd_style fd_in; 
      closed_in <- true
    )

  method pos_in =
    if closed_in then self # complain_closed();
    pos_in
end
;;


class input_descr ?blocking ?start_pos_in ?fd_style fd : raw_in_channel = 
  input_descr_prelim ?blocking ?start_pos_in ?fd_style fd
;;


class output_descr_prelim ?(blocking=true) ?(start_pos_out = 0) ?fd_style fd =
  let fd_style =
    match fd_style with
      | None -> Netsys.get_fd_style fd
      | Some st -> st in
  let wrapper =
    if blocking then Netsys.restart_wait else norestart in
object (self)
  val fd_out = fd
  val mutable pos_out = start_pos_out
  val mutable closed_out = false

  method private complain_closed() =
    raise Closed_channel

  method output buf pos len =
    if closed_out then self # complain_closed();
    wrapper `W fd_style fd
      (fun () ->
         let n = Netsys.gwrite fd_style fd_out buf pos len in
         pos_out <- pos_out + n;
         n
      )
      ()
  
  method close_out () =
    if not closed_out then (
      (* FIXME. We block here even when non-blocking semantics
         is requested. We do this because most programmers would
         be surprised to get EAGAIN when closing a channel.
         Actually, this only affects Win32 output threads and TLS.
       *)
      shutdown_fd `W fd_style fd;
      Netsys.gclose fd_style fd_out; 
      closed_out <- true
    )

  method pos_out =
    if closed_out then self # complain_closed();
    pos_out

  method flush () =
    if closed_out then self # complain_closed()
end
;;


class output_descr ?blocking ?start_pos_out ?fd_style fd : raw_out_channel =
  output_descr_prelim ?blocking ?start_pos_out ?fd_style fd 
;;


class socket_descr ?blocking ?(start_pos_in = 0) ?(start_pos_out = 0) 
                   ?fd_style fd 
      : raw_io_channel =
  let fd_style =
    match fd_style with
      | None -> Netsys.get_fd_style fd
      | Some st -> st in
  let () =
    match fd_style with
      | `Recv_send _
      | `Recv_send_implied
      | `W32_pipe
      | `TLS _ -> ()
      | _ ->
	  failwith "Netchannels.socket_descr: This type of descriptor is \
                    unsupported"
  in
object (self)
  inherit input_descr_prelim ?blocking ~start_pos_in ~fd_style fd
  inherit output_descr_prelim ?blocking ~start_pos_out ~fd_style fd 
  
  method private gen_close cmd =
    shutdown_fd `W fd_style fd;
    if cmd = Unix.SHUTDOWN_ALL then
      Netsys.gclose fd_style fd


  method close_in () =
    if not closed_in then (
      closed_in <- true;
      if closed_out then
	self # gen_close Unix.SHUTDOWN_ALL
      else
	self # gen_close Unix.SHUTDOWN_RECEIVE
    )

  method close_out () =
    if not closed_out then (
      closed_out <- true;
      if closed_in then
	self # gen_close Unix.SHUTDOWN_ALL
      else
	self # gen_close Unix.SHUTDOWN_SEND
    )
end
;;


(************************** transactional *****************************)

class buffered_trans_channel ?(close_mode = (`Commit : close_mode)) 
			      (ch : out_obj_channel)
                              : trans_out_obj_channel =
  let closed = ref false in
  let transbuf = ref(Buffer.create 50) in
  let trans = ref(new output_buffer !transbuf) in
  let reset() =
    transbuf := Buffer.create 50;
    trans := new output_buffer !transbuf in
object (self)
  val out        = ch
  val close_mode = close_mode

  method output         = !trans # output
  method really_output  = !trans # really_output
  method really_output_string = !trans # really_output_string
  method output_char    = !trans # output_char
  method output_string  = !trans # output_string
  method output_bytes   = !trans # output_bytes
  method output_byte    = !trans # output_byte
  method output_buffer  = !trans # output_buffer
  method output_channel = !trans # output_channel
  method flush          = !trans # flush

  method close_out() =
    if not !closed then (
      ( try
	  ( match close_mode with
		`Commit   -> self # commit_work()
	      | `Rollback -> self # rollback_work()
	  )
	with
	  | error ->
	      let bt = Printexc.get_backtrace() in
	      Netlog.logf `Err
		"Netchannels.buffered_trans_channel: \
                   Suppressed error in close_out: %s - backtrace: %s"
		(Netexn.to_string error) bt;
      );
      !trans # close_out();
      out # close_out();
      closed := true
    )


  method pos_out =
    out # pos_out + !trans # pos_out

  method commit_work() =
    try
      (* in any way avoid that the contents of transbuf are printed twice *)
      let b = !transbuf in
      reset();
      out # output_buffer b;
      out # flush();
    with
	err ->
	  self # rollback_work();   (* reset anyway *)
	  raise err

  method rollback_work() =
    reset()

end
;;


let make_temporary_file 
  ?(mode = 0o600) 
  ?(limit = 1000) 
  ?(tmp_directory = Netsys_tmp.tmp_directory() ) 
  ?(tmp_prefix = "netstring")
  () =
  (* Returns (filename, in_channel, out_channel). *)
  let rec try_creation n =
    try
      let fn =
        Filename.concat
          tmp_directory
          (Netsys_tmp.tmp_prefix tmp_prefix ^ "-" ^ (string_of_int n))
      in
      let fd_in =
	Unix.openfile fn [ Unix.O_RDWR; Unix.O_CREAT; Unix.O_EXCL ] mode in
      let fd_out =
	Unix.openfile fn [ Unix.O_RDWR ] mode in
      (* For security reasons check that fd_in and fd_out are the same file: *)
      let stat_in = Unix.fstat fd_in in
      let stat_out = Unix.fstat fd_out in
      if stat_in.Unix.st_dev <> stat_out.Unix.st_dev ||
	 stat_in.Unix.st_rdev <> stat_out.Unix.st_rdev ||
	 stat_in.Unix.st_ino <> stat_out.Unix.st_ino 
      then
	raise(Sys_error("File has been replaced (security alert)"));
      let ch_in  = Unix.in_channel_of_descr fd_in in
      let ch_out = Unix.out_channel_of_descr fd_out in
      fn, ch_in, ch_out
    with
        Unix.Unix_error(Unix.EEXIST,_,_) ->
          (* This does not look very intelligent, but it is the only chance
           * to limit the number of trials.
	   * Note that we get EACCES if the directory is not writeable.
           *)
          if n > limit then
            failwith ("Netchannels: Cannot create temporary file - too many files in this temp directory: " ^ tmp_directory);
          try_creation (n+1)
      | Unix.Unix_error(e,_,_) ->
	  raise (Sys_error("Cannot create a temporary file in the directory " ^
			   tmp_directory ^ ": " ^ Unix.error_message e))
	  
  in
  try_creation 0
;;


class tempfile_trans_channel ?(close_mode = (`Commit : close_mode)) 
                              ?tmp_directory
			      ?tmp_prefix
			      (ch : out_obj_channel)
			      : trans_out_obj_channel =
  let _transname, _transch_in, _transch_out = 
    make_temporary_file ?tmp_directory ?tmp_prefix () in
  let closed = ref false in
object (self)
  val transch_out        = _transch_out
  val mutable transch_in = _transch_in
  val trans              = new output_channel _transch_out
  val mutable out        = ch
  val close_mode         = close_mode
  val mutable need_clear = false

  initializer
    try
      Sys.remove _transname;
      (* Remove the file immediately. This requires "Unix semantics" of the
       * underlying file system, because we don't remove the file but only
       * the entry in the directory. So we can read and write the file and
       * allocate disk space, but the file is private from now on. (It's
       * not fully private, because another process can obtain a descriptor
       * between creation of the file and removal of the entry. We should
       * keep that in mind if privacy really matters.)
       * The disk space will be freed when the descriptor is closed.
       *)
    with
	err -> 
	  close_in  _transch_in;
	  close_out _transch_out;
	  raise err

  method output         = if need_clear then self#clear(); trans # output
  method really_output  = if need_clear then self#clear(); trans # really_output
  method really_output_string =
    if need_clear then self#clear(); trans # really_output_string
  method output_char    = if need_clear then self#clear(); trans # output_char
  method output_string  = if need_clear then self#clear(); trans # output_string
  method output_bytes   = if need_clear then self#clear(); trans # output_bytes
  method output_byte    = if need_clear then self#clear(); trans # output_byte
  method output_buffer  = if need_clear then self#clear(); trans # output_buffer
  method output_channel = if need_clear then self#clear(); trans #output_channel
  method flush          = if need_clear then self#clear(); trans # flush

  method close_out() =
    if not !closed then (
      if need_clear then self#clear(); 
      ( try
	  ( match close_mode with
		`Commit   -> self # commit_work()
	      | `Rollback -> self # rollback_work()
	  )
	with
	  | error ->
	      let bt = Printexc.get_backtrace() in
	      Netlog.logf `Err
		"Netchannels.tempfile_trans_channel: \
                  Suppressed error in close_out: %s - backtrace: %s"
		(Netexn.to_string error) bt;
      );
      Pervasives.close_in transch_in;
      trans # close_out();      (* closes transch_out *)
      out # close_out();
      closed := true
    )


  method pos_out =
    if need_clear then self#clear(); 
    out # pos_out + trans # pos_out

  method commit_work() =
    need_clear <- true;
    let len = trans # pos_out in
    trans # flush();
    Pervasives.seek_in transch_in 0;
    let trans' = new input_channel transch_in in
    ( try 
        out # output_channel ~len trans';
        out # flush();
      with
	  err -> 
	    self # rollback_work();
	    raise err
    );
    self # clear()

  method rollback_work() = self # clear()

  method private clear() =
    (* delete the contents of the file *)
    (* First empty the file and reset the output channel: *)
    Pervasives.seek_out transch_out 0;
    Unix.ftruncate (Unix.descr_of_out_channel transch_out) 0;
    (* Renew the input channel. We create a new channel to avoid problems
     * with the internal buffer of the channel.
     * (Problem: transch_in has an internal buffer, and the buffer contains
     * old data now. So we drop the channel and create a new channel for the
     * same file descriptor. Note that we cannot set the file offset with
     * seek_in because neither the old nor the new channel is properly 
     * synchronized with the file. So we fall back to lseek.)
     *) 
    let fd = Unix.descr_of_in_channel transch_in in
    ignore(Unix.lseek fd 0 Unix.SEEK_END);                  (* set the offset *)
    transch_in <- Unix.in_channel_of_descr fd;               (* renew channel *)
    (* Now check that everything worked: *)
    assert(pos_in transch_in = 0);
    assert(in_channel_length transch_in = 0);
    (* Note: the old transch_in will be automatically finalized, but the
     * underlying file descriptor will not be closed in this case
     *)
    need_clear <- false

end
;;


let id_conv incoming incoming_eof outgoing =
  (* Copies everything from [incoming] to [outgoing] *)
  let len = Netbuffer.length incoming in
  ignore
    (Netbuffer.add_inplace ~len outgoing 
       (fun s_outgoing pos len' ->
	  assert (len = len');
	  Netbuffer.blit incoming 0 s_outgoing pos len';
	  Netbuffer.clear incoming;
	  len'
       ))
;;


let call_input refill f arg =
  (* Try to satisfy the request: *)
  try f arg
  with
      Buffer_underrun ->
	(* Not enough data in the outgoing buffer. *)
	refill();
	f arg
;;


class pipe ?(conv = id_conv) ?(buffer_size = 1024) () : io_obj_channel =
  let _incoming = Netbuffer.create buffer_size in
  let _outgoing = Netbuffer.create buffer_size in
object(self)
  (* The properties as "incoming buffer" [output_super] are simply inherited
   * from [output_netbuffer]. The "outgoing buffer" [input_super] invocations
   * are delegated to [input_netbuffer]. Inheritance does not work because
   * there is no way to make the public method [shutdown] private again.
   *)

  inherit output_netbuffer _incoming as output_super
    
  val conv = conv
  val incoming = _incoming
  val outgoing = _outgoing
  val input_super = new input_netbuffer _outgoing

  val mutable incoming_eof = false
  val mutable pos_in = 0
    (* We must count positions ourselves. Can't use input_super#pos_in
     * because conv may manipulate the buffer.
     *)

  val mutable output_closed = false

  (* Input methods: *)

  method private refill() =
    conv incoming incoming_eof outgoing;
    if incoming_eof then input_super # shutdown()


  method input str pos len =
    let n = call_input self#refill (input_super#input str pos) len in
    pos_in <- pos_in + n;
    n

  method input_line() =
    let p = input_super # pos_in in
    let line = call_input self#refill (input_super#input_line) () in
    let p' = input_super # pos_in in
    pos_in <- pos_in + (p' - p);
    line

  method really_input str pos len =
    call_input self#refill (input_super#really_input str pos) len;
    pos_in <- pos_in + len

  method really_input_string len =
    let buf = Bytes.create len in
    call_input self#refill (input_super#really_input buf 0) len;
    pos_in <- pos_in + len;
    Bytes.unsafe_to_string buf

  method input_char() =
    let c = call_input self#refill (input_super#input_char) () in
    pos_in <- pos_in + 1;
    c

  method input_byte() =
    let b = call_input self#refill (input_super#input_byte) () in
    pos_in <- pos_in + 1;
    b

  method close_in() =
    (* [close_in] implies [close_out]: *)
    if not output_closed then (
      output_super # close_out();
      output_closed <- true;
    );
    input_super # close_in()

  method pos_in = pos_in

  (* [close_out] also shuts down the input side of the pipe. *)

  method close_out () =
    if not output_closed then (
      output_super # close_out();
      output_closed <- true;
    );
    incoming_eof <- true

end


class output_filter 
      (p : io_obj_channel) (out : out_obj_channel) : out_obj_channel =
object(self)
  val p = p
  val mutable p_closed = false  (* output side of p is closed *)
  val out = out
  val buf = Bytes.create 1024  (* for copy_channel *)

  method output s pos len =
    if p_closed then raise Closed_channel;
    let n = p # output s pos len in
    self # transfer();
    n

  method really_output s pos len =
    if p_closed then raise Closed_channel;
    p # really_output s pos len;
    self # transfer();

  method really_output_string s pos len =
    if p_closed then raise Closed_channel;
    p # really_output_string s pos len;
    self # transfer();

  method output_char c =
    if p_closed then raise Closed_channel;
    p # output_char c;
    self # transfer();

  method output_string s =
    if p_closed then raise Closed_channel;
    p # output_string s;
    self # transfer();

  method output_bytes s =
    if p_closed then raise Closed_channel;
    p # output_bytes s;
    self # transfer();

  method output_byte b =
    if p_closed then raise Closed_channel;
    p # output_byte b;
    self # transfer();

  method output_buffer b =
    if p_closed then raise Closed_channel;
    p # output_buffer b;
    self # transfer();

  method output_channel ?len ch =
    (* To avoid large intermediate buffers, the channel is copied
     * chunk by chunk 
     *)
    if p_closed then raise Closed_channel;
    let len_to_do = ref (match len with None -> -1 | Some l -> max 0 l) in
    let buf = buf in
    while !len_to_do <> 0 do
      let n = if !len_to_do < 0 then 1024 else min !len_to_do 1024 in
      if copy_channel ~buf ~len:n ch (p :> out_obj_channel) then
	(* EOF *)
	len_to_do := 0
      else
	if !len_to_do >= 0 then
	  (len_to_do := !len_to_do - n; assert(!len_to_do >= 0));
      self # transfer();
    done

  method flush() =
    p # flush();
    self # transfer();
    out # flush()

  method close_out() =
    if not p_closed then (
      p # close_out();
      p_closed <- true;
      ( try
	  self # transfer()
	with
	  | error ->
	      (* We report the error. However, we prevent that another,
		 immediately following [close_out] reports the same
		 error again. This is done by setting p_closed.
	       *)
	      raise error
      )
    )

  method pos_out = p # pos_out

  method private transfer() =
    (* Copy as much as possible from [p] to [out] *)
    try
      (* Call [copy_channel] directly (and not the method [output_channel])
       * because we can pass the copy buffer ~buf
       *)
      ignore(copy_channel ~buf (p :> in_obj_channel) out);
      out # flush();
    with
	Buffer_underrun -> ()

end


let rec filter_input refill f arg =
  (* Try to satisfy the request: *)
  try f arg
  with
      Buffer_underrun ->
	(* Not enough data in the outgoing buffer. *)
	refill();
	filter_input refill f arg
;;


class input_filter 
      (inp : in_obj_channel) (p : io_obj_channel) : in_obj_channel =
object(self)
  val inp = inp
  val p = p
  val buf = Bytes.create 1024  (* for copy_channel *)

  method private refill() =
    (* Copy some data from [inp] to [p] *)
    (* Call [copy_channel] directly (and not the method [output_channel])
     * because we can pass the copy buffer ~buf
     *)
    let eof = 
      copy_channel ~len:(Bytes.length buf) ~buf inp (p :> out_obj_channel) in
    if eof then p # close_out();

  method input str pos =
    filter_input self#refill (p#input str pos)

  method input_line =
    filter_input self#refill (p#input_line)

  method really_input str pos =
    filter_input self#refill (p#really_input str pos)

  method really_input_string =
    filter_input self#refill p#really_input_string

  method input_char =
    filter_input self#refill (p#input_char)

  method input_byte =
    filter_input self#refill (p#input_byte)

  method close_in() =
    p#close_in();

  method pos_in = p#pos_in

end