codemirror-lang-liquidsoap 0.3.5 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (427) hide show
  1. package/dist/cli.js +8 -8
  2. package/dist/index.js +7 -7
  3. package/package.json +1 -1
  4. package/src/liquidsoap.grammar +10 -4
  5. package/src/parser.js +6 -6
  6. package/tests/liquidsoap/.codespellignore +1 -0
  7. package/tests/liquidsoap/.github/ISSUE_TEMPLATE/bug_report.yml +16 -0
  8. package/tests/liquidsoap/.github/docker/alpine.dockerfile +20 -0
  9. package/tests/liquidsoap/.github/docker/debian.dockerfile +55 -0
  10. package/tests/liquidsoap/.github/opam/{liquidsoap-core-windows.opam → liquidsoap-windows.opam} +8 -5
  11. package/tests/liquidsoap/.github/renovate.json +23 -0
  12. package/tests/liquidsoap/.github/scripts/add-local-opam-packages.sh +5 -3
  13. package/tests/liquidsoap/.github/scripts/build-apk.sh +10 -19
  14. package/tests/liquidsoap/.github/scripts/build-deb.sh +6 -15
  15. package/tests/liquidsoap/.github/scripts/build-details.sh +15 -4
  16. package/tests/liquidsoap/.github/scripts/build-posix.sh +11 -20
  17. package/tests/liquidsoap/.github/scripts/build-website.sh +1 -1
  18. package/tests/liquidsoap/.github/scripts/build-win32.sh +6 -2
  19. package/tests/liquidsoap/.github/scripts/push-docker.sh +13 -15
  20. package/tests/liquidsoap/.github/workflows/ci.yml +291 -185
  21. package/tests/liquidsoap/.ocamlformat +1 -1
  22. package/tests/liquidsoap/.pre-commit-config.yaml +2 -2
  23. package/tests/liquidsoap/CHANGES.md +112 -3
  24. package/tests/liquidsoap/README.md +10 -9
  25. package/tests/liquidsoap/ROADMAP.md +14 -8
  26. package/tests/liquidsoap/bin/liquidsoap-macos-instruments +7 -0
  27. package/tests/liquidsoap/doc/content/build.md +0 -1
  28. package/tests/liquidsoap/doc/content/faq.md +2 -4
  29. package/tests/liquidsoap/doc/content/icy_metadata.md +2 -2
  30. package/tests/liquidsoap/doc/content/install.md +13 -2
  31. package/tests/liquidsoap/doc/content/json.md +1 -1
  32. package/tests/liquidsoap/doc/content/language.md +4 -4
  33. package/tests/liquidsoap/doc/content/liq/beets-protocol.liq +1 -1
  34. package/tests/liquidsoap/doc/content/liq/crossfade.liq +1 -1
  35. package/tests/liquidsoap/doc/content/liq/harbor-simple.liq +1 -1
  36. package/tests/liquidsoap/doc/content/liq/radiopi.liq +10 -9
  37. package/tests/liquidsoap/doc/content/liq/request.dynamic.liq +1 -1
  38. package/tests/liquidsoap/doc/content/migrating.md +25 -3
  39. package/tests/liquidsoap/doc/content/playlist_parsers.md +46 -1
  40. package/tests/liquidsoap/doc/content/script_loading.md +3 -0
  41. package/tests/liquidsoap/doc/content/xml.md +160 -0
  42. package/tests/liquidsoap/doc/dune.inc +129 -0
  43. package/tests/liquidsoap/dune-project +29 -49
  44. package/tests/liquidsoap/liquidsoap +1 -1
  45. package/tests/liquidsoap/{liquidsoap-js.opam → opam/liquidsoap-js.opam} +3 -2
  46. package/tests/liquidsoap/{liquidsoap-lang.opam → opam/liquidsoap-lang.opam} +4 -3
  47. package/tests/liquidsoap/{liquidsoap-mode.opam → opam/liquidsoap-mode.opam} +3 -2
  48. package/tests/liquidsoap/{liquidsoap-core.opam → opam/liquidsoap.opam} +46 -17
  49. package/tests/liquidsoap/{prometheus-liquidsoap.opam → opam/prometheus-liquidsoap.opam} +2 -1
  50. package/tests/liquidsoap/{sdl-liquidsoap.opam → opam/sdl-liquidsoap.opam} +2 -1
  51. package/tests/liquidsoap/{tls-liquidsoap.opam → opam/tls-liquidsoap.opam} +2 -1
  52. package/tests/liquidsoap/src/bin/dune +1 -1
  53. package/tests/liquidsoap/src/config/xmlplaylist_option.disabled.ml +2 -0
  54. package/tests/liquidsoap/src/core/builtins/builtins_ffmpeg_decoder.ml +22 -22
  55. package/tests/liquidsoap/src/core/builtins/builtins_ffmpeg_encoder.ml +4 -4
  56. package/tests/liquidsoap/src/core/builtins/builtins_ffmpeg_filters.ml +37 -41
  57. package/tests/liquidsoap/src/core/builtins/builtins_files.ml +3 -1
  58. package/tests/liquidsoap/src/core/builtins/builtins_optionals.ml +0 -1
  59. package/tests/liquidsoap/src/core/builtins/builtins_process.ml +15 -7
  60. package/tests/liquidsoap/src/core/builtins/builtins_resolvers.ml +16 -0
  61. package/tests/liquidsoap/src/core/builtins/builtins_socket.ml +7 -5
  62. package/tests/liquidsoap/src/core/builtins/builtins_source.ml +3 -3
  63. package/tests/liquidsoap/src/core/builtins/builtins_srt.ml +84 -8
  64. package/tests/liquidsoap/src/core/builtins/builtins_string_extra.ml +13 -0
  65. package/tests/liquidsoap/src/core/builtins/builtins_sys.ml +6 -6
  66. package/tests/liquidsoap/src/core/builtins/builtins_thread.ml +2 -2
  67. package/tests/liquidsoap/src/core/builtins/builtins_time.ml +5 -3
  68. package/tests/liquidsoap/src/core/builtins/builtins_tls.ml +1 -1
  69. package/tests/liquidsoap/src/core/clock.ml +105 -38
  70. package/tests/liquidsoap/src/core/clock.mli +1 -0
  71. package/tests/liquidsoap/src/core/clock_base.ml +2 -2
  72. package/tests/liquidsoap/src/core/conversions/stereo.ml +6 -8
  73. package/tests/liquidsoap/src/core/converters/audio_converter.mli +5 -5
  74. package/tests/liquidsoap/src/core/converters/video_converter.ml +2 -2
  75. package/tests/liquidsoap/src/core/converters/video_converter.mli +3 -3
  76. package/tests/liquidsoap/src/core/decoder/decoder.ml +14 -17
  77. package/tests/liquidsoap/src/core/decoder/decoder.mli +11 -11
  78. package/tests/liquidsoap/src/core/decoder/decoder_utils.ml +12 -13
  79. package/tests/liquidsoap/src/core/decoder/decoder_utils.mli +2 -3
  80. package/tests/liquidsoap/src/core/decoder/external_decoder.ml +9 -13
  81. package/tests/liquidsoap/src/core/decoder/ffmpeg_decoder.ml +57 -53
  82. package/tests/liquidsoap/src/core/decoder/ffmpeg_raw_decoder.ml +28 -9
  83. package/tests/liquidsoap/src/core/decoder/flac_metadata_plug.ml +15 -2
  84. package/tests/liquidsoap/src/core/decoder/image_decoder.ml +33 -19
  85. package/tests/liquidsoap/src/core/decoder/liq_flac_decoder.ml +22 -23
  86. package/tests/liquidsoap/src/core/decoder/liq_ogg_decoder.ml +5 -5
  87. package/tests/liquidsoap/src/core/decoder/mad_decoder.ml +2 -2
  88. package/tests/liquidsoap/src/core/decoder/midi_decoder.ml +1 -2
  89. package/tests/liquidsoap/src/core/decoder/ogg_flac_duration.ml +3 -11
  90. package/tests/liquidsoap/src/core/decoder/ogg_metadata_plug.ml +15 -2
  91. package/tests/liquidsoap/src/core/decoder/raw_audio_decoder.ml +8 -7
  92. package/tests/liquidsoap/src/core/decoder/wav_aiff_decoder.ml +3 -3
  93. package/tests/liquidsoap/src/core/dune +2 -15
  94. package/tests/liquidsoap/src/core/encoder/encoder.ml +18 -23
  95. package/tests/liquidsoap/src/core/encoder/encoder.mli +12 -17
  96. package/tests/liquidsoap/src/core/encoder/encoders/ffmpeg_encoder.ml +13 -27
  97. package/tests/liquidsoap/src/core/encoder/encoders/ffmpeg_encoder_common.ml +1 -1
  98. package/tests/liquidsoap/src/core/encoder/encoders/flac_encoder.ml +3 -4
  99. package/tests/liquidsoap/src/core/encoder/encoders/ndi_encoder.ml +2 -1
  100. package/tests/liquidsoap/src/core/encoder/encoders/ogg_encoder.ml +1 -2
  101. package/tests/liquidsoap/src/core/encoder/lang/lang_external_encoder.ml +2 -2
  102. package/tests/liquidsoap/src/core/encoder/lang/lang_fdkaac.ml +1 -1
  103. package/tests/liquidsoap/src/core/encoder/lang/lang_ffmpeg.ml +46 -39
  104. package/tests/liquidsoap/src/core/encoder/lang/lang_flac.ml +1 -1
  105. package/tests/liquidsoap/src/core/encoder/lang/lang_shine.ml +2 -2
  106. package/tests/liquidsoap/src/core/encoder/lang/lang_vorbis.ml +1 -1
  107. package/tests/liquidsoap/src/core/encoder/lang/lang_wav.ml +1 -1
  108. package/tests/liquidsoap/src/core/file_watcher.inotify.ml +8 -13
  109. package/tests/liquidsoap/src/core/harbor/harbor.ml +22 -24
  110. package/tests/liquidsoap/src/core/io/ffmpeg_filter_io.ml +4 -3
  111. package/tests/liquidsoap/src/core/io/oss_io.ml +3 -4
  112. package/tests/liquidsoap/src/core/io/portaudio_io.ml +1 -1
  113. package/tests/liquidsoap/src/core/io/pulseaudio_io.ml +3 -4
  114. package/tests/liquidsoap/src/core/io/srt_io.ml +316 -120
  115. package/tests/liquidsoap/src/core/io/udp_io.ml +14 -12
  116. package/tests/liquidsoap/src/core/lang.mli +10 -12
  117. package/tests/liquidsoap/src/core/lang_source.ml +25 -39
  118. package/tests/liquidsoap/src/core/lang_string.ml +30 -0
  119. package/tests/liquidsoap/src/core/lang_string.mli +3 -0
  120. package/tests/liquidsoap/src/core/ogg_formats/ogg_flac_encoder.ml +6 -6
  121. package/tests/liquidsoap/src/core/ogg_formats/ogg_muxer.ml +17 -27
  122. package/tests/liquidsoap/src/core/ogg_formats/ogg_muxer.mli +44 -53
  123. package/tests/liquidsoap/src/core/ogg_formats/opus_encoder.ml +1 -1
  124. package/tests/liquidsoap/src/core/ogg_formats/vorbis_encoder.ml +1 -2
  125. package/tests/liquidsoap/src/core/operators/add.ml +2 -2
  126. package/tests/liquidsoap/src/core/operators/cross.ml +2 -2
  127. package/tests/liquidsoap/src/core/operators/delay.ml +23 -6
  128. package/tests/liquidsoap/src/core/operators/dtmf.ml +3 -2
  129. package/tests/liquidsoap/src/core/operators/dyn_op.ml +2 -2
  130. package/tests/liquidsoap/src/core/operators/fir_filter.ml +2 -1
  131. package/tests/liquidsoap/src/core/operators/iir_filter.ml +1 -1
  132. package/tests/liquidsoap/src/core/operators/insert_metadata.ml +1 -1
  133. package/tests/liquidsoap/src/core/operators/lilv_op.ml +1 -1
  134. package/tests/liquidsoap/src/core/operators/lufs.ml +136 -89
  135. package/tests/liquidsoap/src/core/operators/lufs_c.c +105 -0
  136. package/tests/liquidsoap/src/core/operators/map_metadata.ml +33 -9
  137. package/tests/liquidsoap/src/core/operators/max_duration.ml +3 -4
  138. package/tests/liquidsoap/src/core/operators/muxer.ml +2 -1
  139. package/tests/liquidsoap/src/core/operators/noblank.ml +6 -6
  140. package/tests/liquidsoap/src/core/operators/sequence.ml +5 -5
  141. package/tests/liquidsoap/src/core/operators/switch.ml +10 -11
  142. package/tests/liquidsoap/src/core/operators/time_warp.ml +14 -11
  143. package/tests/liquidsoap/src/core/operators/video_fade.ml +2 -3
  144. package/tests/liquidsoap/src/core/outputs/ao_out.ml +1 -1
  145. package/tests/liquidsoap/src/core/outputs/bjack_out.ml +1 -1
  146. package/tests/liquidsoap/src/core/outputs/graphics_out.ml +6 -2
  147. package/tests/liquidsoap/src/core/outputs/harbor_output.ml +12 -10
  148. package/tests/liquidsoap/src/core/outputs/hls_output.ml +19 -14
  149. package/tests/liquidsoap/src/core/outputs/icecast2.ml +85 -52
  150. package/tests/liquidsoap/src/core/outputs/ndi_out.ml +1 -1
  151. package/tests/liquidsoap/src/core/outputs/output.ml +8 -9
  152. package/tests/liquidsoap/src/core/outputs/output.mli +61 -61
  153. package/tests/liquidsoap/src/core/outputs/pipe_output.ml +8 -6
  154. package/tests/liquidsoap/src/core/outputs/sdl_out.ml +5 -2
  155. package/tests/liquidsoap/src/core/playlist_parser.ml +6 -7
  156. package/tests/liquidsoap/src/core/playlists/playlist_basic.ml +0 -180
  157. package/tests/liquidsoap/src/core/protocols/annotate.ml +3 -3
  158. package/tests/liquidsoap/src/core/request.ml +16 -17
  159. package/tests/liquidsoap/src/core/request.mli +27 -28
  160. package/tests/liquidsoap/src/core/shebang.ml +3 -2
  161. package/tests/liquidsoap/src/core/source.ml +104 -63
  162. package/tests/liquidsoap/src/core/source.mli +249 -247
  163. package/tests/liquidsoap/src/core/sources/generated.ml +2 -2
  164. package/tests/liquidsoap/src/core/sources/request_dynamic.ml +2 -2
  165. package/tests/liquidsoap/src/core/sources/synthesized.ml +2 -2
  166. package/tests/liquidsoap/src/core/stream/aFrame.mli +7 -9
  167. package/tests/liquidsoap/src/core/stream/content.mli +4 -13
  168. package/tests/liquidsoap/src/core/stream/content_base.ml +2 -19
  169. package/tests/liquidsoap/src/core/stream/content_video.ml +9 -10
  170. package/tests/liquidsoap/src/core/stream/ffmpeg_copy_content.ml +19 -15
  171. package/tests/liquidsoap/src/core/stream/ffmpeg_raw_content.ml +26 -9
  172. package/tests/liquidsoap/src/core/stream/frame.ml +4 -4
  173. package/tests/liquidsoap/src/core/stream/frame.mli +13 -15
  174. package/tests/liquidsoap/src/core/stream/frame_base.ml +1 -2
  175. package/tests/liquidsoap/src/core/stream/frame_settings.ml +14 -14
  176. package/tests/liquidsoap/src/core/stream/generator.mli +14 -17
  177. package/tests/liquidsoap/src/core/stream/mFrame.mli +2 -2
  178. package/tests/liquidsoap/src/core/tools/charset.ml +28 -18
  179. package/tests/liquidsoap/src/core/tools/charset_base.ml +5 -0
  180. package/tests/liquidsoap/src/core/tools/child_support.ml +2 -2
  181. package/tests/liquidsoap/src/core/tools/ffmpeg_avfilter_utils.ml +139 -0
  182. package/tests/liquidsoap/src/core/tools/ffmpeg_avfilter_utils.mli +22 -0
  183. package/tests/liquidsoap/src/core/tools/ffmpeg_utils.ml +2 -1
  184. package/tests/liquidsoap/src/core/tools/lifecycle.mli +4 -4
  185. package/tests/liquidsoap/src/core/tools/liq_http.ml +44 -19
  186. package/tests/liquidsoap/src/core/tools/liq_http.mli +1 -1
  187. package/tests/liquidsoap/src/core/tools/log.ml +3 -8
  188. package/tests/liquidsoap/src/core/tools/log.mli +1 -1
  189. package/tests/liquidsoap/src/core/tools/mutex_utils.ml +1 -1
  190. package/tests/liquidsoap/src/core/tools/pool.ml +2 -2
  191. package/tests/liquidsoap/src/core/tools/process_handler.mli +30 -27
  192. package/tests/liquidsoap/src/core/tools/producer_consumer.ml +10 -9
  193. package/tests/liquidsoap/src/{lang → core/tools}/queues.ml +58 -64
  194. package/tests/liquidsoap/src/{lang → core/tools}/queues.mli +10 -0
  195. package/tests/liquidsoap/src/core/tools/sdl_utils.ml +2 -0
  196. package/tests/liquidsoap/src/core/tools/server.mli +8 -8
  197. package/tests/liquidsoap/src/core/tools/sha1.ml +2 -1
  198. package/tests/liquidsoap/src/core/tools/start_stop.ml +2 -1
  199. package/tests/liquidsoap/src/core/tools/strings.mli +5 -4
  200. package/tests/liquidsoap/src/core/tools/tutils.ml +7 -7
  201. package/tests/liquidsoap/src/core/tools/tutils.mli +14 -18
  202. package/tests/liquidsoap/src/core/tools/unix_c.c +16 -5
  203. package/tests/liquidsoap/src/core/tools/utils.ml +83 -25
  204. package/tests/liquidsoap/src/core/tools/wav_aiff.ml +3 -3
  205. package/tests/liquidsoap/src/core/tools/wav_aiff.mli +7 -7
  206. package/tests/liquidsoap/src/core/types/format_type.ml +6 -6
  207. package/tests/liquidsoap/src/js/index.html +1 -154
  208. package/tests/liquidsoap/src/js/interactive_js.ml +7 -3
  209. package/tests/liquidsoap/src/js/stdlib_js.liq +6 -0
  210. package/tests/liquidsoap/src/lang/builtins_lang.ml +8 -0
  211. package/tests/liquidsoap/src/lang/builtins_null.ml +1 -1
  212. package/tests/liquidsoap/src/lang/builtins_string.ml +2 -11
  213. package/tests/liquidsoap/src/lang/builtins_xml.ml +204 -0
  214. package/tests/liquidsoap/src/lang/cache.ml +7 -2
  215. package/tests/liquidsoap/src/lang/dune +5 -2
  216. package/tests/liquidsoap/src/lang/flags.ml +2 -2
  217. package/tests/liquidsoap/src/lang/lang.mli +10 -12
  218. package/tests/liquidsoap/src/lang/lang_core.ml +24 -24
  219. package/tests/liquidsoap/src/lang/lang_eval.ml +1 -1
  220. package/tests/liquidsoap/src/lang/lang_string.ml +7 -13
  221. package/tests/liquidsoap/src/lang/lang_string.mli +0 -1
  222. package/tests/liquidsoap/src/lang/lexer.ml +3 -0
  223. package/tests/liquidsoap/src/lang/parser.mly +7 -4
  224. package/tests/liquidsoap/src/lang/parser_helper.ml +9 -77
  225. package/tests/liquidsoap/src/lang/parser_helper.mli +1 -1
  226. package/tests/liquidsoap/src/lang/pos.ml +4 -4
  227. package/tests/liquidsoap/src/lang/pos.mli +55 -0
  228. package/tests/liquidsoap/src/lang/preprocessor.ml +12 -5
  229. package/tests/liquidsoap/src/lang/repr.ml +6 -6
  230. package/tests/liquidsoap/src/lang/runtime.ml +55 -48
  231. package/tests/liquidsoap/src/lang/runtime.mli +8 -3
  232. package/tests/liquidsoap/src/lang/term/parsed_term.ml +33 -33
  233. package/tests/liquidsoap/src/lang/term/term_base.ml +14 -10
  234. package/tests/liquidsoap/src/lang/term/term_preprocessor.ml +28 -1
  235. package/tests/liquidsoap/src/lang/term/term_reducer.ml +244 -132
  236. package/tests/liquidsoap/src/lang/term/term_reducer.mli +10 -2
  237. package/tests/liquidsoap/src/lang/term/term_stdlib.ml +14 -13
  238. package/tests/liquidsoap/src/lang/term.mli +3 -1
  239. package/tests/liquidsoap/src/lang/typechecking.ml +16 -16
  240. package/tests/liquidsoap/src/lang/typechecking.mli +5 -1
  241. package/tests/liquidsoap/src/lang/types/type_base.ml +16 -17
  242. package/tests/liquidsoap/src/lang/typing.ml +13 -12
  243. package/tests/liquidsoap/src/lang/typing.mli +2 -1
  244. package/tests/liquidsoap/src/lang/value.ml +20 -14
  245. package/tests/liquidsoap/src/libs/audio.liq +53 -18
  246. package/tests/liquidsoap/src/libs/autocue.liq +8 -8
  247. package/tests/liquidsoap/src/libs/clock.liq +3 -3
  248. package/tests/liquidsoap/src/libs/dune +2 -2
  249. package/tests/liquidsoap/src/libs/extra/audio.liq +4 -4
  250. package/tests/liquidsoap/src/libs/extra/audioscrobbler.liq +487 -0
  251. package/tests/liquidsoap/src/libs/extra/deprecations.liq +11 -11
  252. package/tests/liquidsoap/src/libs/extra/externals.liq +9 -9
  253. package/tests/liquidsoap/src/libs/extra/fades.liq +2 -2
  254. package/tests/liquidsoap/src/libs/extra/file.liq +1 -1
  255. package/tests/liquidsoap/src/libs/extra/http.liq +4 -4
  256. package/tests/liquidsoap/src/libs/extra/interactive.liq +1 -1
  257. package/tests/liquidsoap/src/libs/extra/metadata.liq +3 -3
  258. package/tests/liquidsoap/src/libs/extra/native.liq +6 -6
  259. package/tests/liquidsoap/src/libs/extra/server.liq +2 -2
  260. package/tests/liquidsoap/src/libs/extra/source.liq +10 -10
  261. package/tests/liquidsoap/src/libs/extra/spinitron.liq +11 -11
  262. package/tests/liquidsoap/src/libs/extra/telnet.liq +51 -0
  263. package/tests/liquidsoap/src/libs/extra/video.liq +2 -2
  264. package/tests/liquidsoap/src/libs/fades.liq +70 -57
  265. package/tests/liquidsoap/src/libs/ffmpeg.liq +50 -33
  266. package/tests/liquidsoap/src/libs/file.liq +8 -8
  267. package/tests/liquidsoap/src/libs/hls.liq +5 -5
  268. package/tests/liquidsoap/src/libs/http.liq +64 -37
  269. package/tests/liquidsoap/src/libs/icecast.liq +1 -1
  270. package/tests/liquidsoap/src/libs/io.liq +6 -6
  271. package/tests/liquidsoap/src/libs/medialib.liq +46 -45
  272. package/tests/liquidsoap/src/libs/metadata.liq +4 -4
  273. package/tests/liquidsoap/src/libs/null.liq +3 -3
  274. package/tests/liquidsoap/src/libs/playlist.liq +547 -22
  275. package/tests/liquidsoap/src/libs/protocols.liq +14 -14
  276. package/tests/liquidsoap/src/libs/replaygain.liq +11 -11
  277. package/tests/liquidsoap/src/libs/request.liq +27 -29
  278. package/tests/liquidsoap/src/libs/runtime.liq +2 -0
  279. package/tests/liquidsoap/src/libs/source.liq +15 -15
  280. package/tests/liquidsoap/src/libs/sqlite.liq +1 -1
  281. package/tests/liquidsoap/src/libs/stdlib.liq +1 -1
  282. package/tests/liquidsoap/src/libs/string.liq +6 -6
  283. package/tests/liquidsoap/src/libs/switches.liq +3 -3
  284. package/tests/liquidsoap/src/libs/thread.liq +3 -9
  285. package/tests/liquidsoap/src/libs/tracks.liq +23 -9
  286. package/tests/liquidsoap/src/libs/video.liq +32 -32
  287. package/tests/liquidsoap/src/modules/dtools/README.md +58 -0
  288. package/tests/liquidsoap/src/modules/dtools/dtools.ml +19 -0
  289. package/tests/liquidsoap/src/modules/dtools/dtools.mli +284 -0
  290. package/tests/liquidsoap/src/modules/dtools/dtools_impl.ml +860 -0
  291. package/tests/liquidsoap/src/modules/dtools/dtools_syslog.impl.ml +45 -0
  292. package/tests/liquidsoap/src/modules/dtools/dtools_syslog.noop.ml +1 -0
  293. package/tests/liquidsoap/src/modules/dtools/dune +12 -0
  294. package/tests/liquidsoap/src/modules/duppy/.merlin +4 -0
  295. package/tests/liquidsoap/src/modules/duppy/README.md +47 -0
  296. package/tests/liquidsoap/src/modules/duppy/dune +7 -0
  297. package/tests/liquidsoap/src/modules/duppy/duppy.ml +1097 -0
  298. package/tests/liquidsoap/src/modules/duppy/duppy.mli +494 -0
  299. package/tests/liquidsoap/src/modules/duppy/duppy_stubs.c +176 -0
  300. package/tests/liquidsoap/src/modules/duppy/examples/dune +9 -0
  301. package/tests/liquidsoap/src/modules/duppy/examples/http.ml +560 -0
  302. package/tests/liquidsoap/src/modules/duppy/examples/index.html +1 -0
  303. package/tests/liquidsoap/src/modules/duppy/examples/telnet.ml +150 -0
  304. package/tests/liquidsoap/src/modules/xmlplaylist/dune +5 -0
  305. package/tests/liquidsoap/src/modules/xmlplaylist/xmlplaylist.ml +185 -0
  306. package/tests/liquidsoap/src/modules/xmlplaylist/xmlplaylist.mli +56 -0
  307. package/tests/liquidsoap/src/ndi/ndi.mli +2 -3
  308. package/tests/liquidsoap/src/runtime/build_config.ml +0 -1
  309. package/tests/liquidsoap/src/runtime/main.ml +4 -23
  310. package/tests/liquidsoap/src/stereotool/stereotool.mli +3 -3
  311. package/tests/liquidsoap/src/tooling/parsed_json.ml +6 -4
  312. package/tests/liquidsoap/tests/core/content_test.ml +2 -39
  313. package/tests/liquidsoap/tests/core/dune +13 -0
  314. package/tests/liquidsoap/tests/core/dune.inc +1 -1
  315. package/tests/liquidsoap/tests/core/gen_dune.ml +4 -1
  316. package/tests/liquidsoap/tests/core/json_test.ml +10 -0
  317. package/tests/liquidsoap/tests/core/more_types.ml +32 -28
  318. package/tests/liquidsoap/tests/core/output_encoded_test.ml +2 -0
  319. package/tests/liquidsoap/tests/core/string_test.ml +4 -0
  320. package/tests/liquidsoap/tests/core/types.ml +1 -1
  321. package/tests/liquidsoap/tests/harbor/http.liq +1 -1
  322. package/tests/liquidsoap/tests/harbor/http2.liq +1 -1
  323. package/tests/liquidsoap/tests/language/cue_test.liq +3993 -0
  324. package/tests/liquidsoap/tests/language/dune.inc +24 -0
  325. package/tests/liquidsoap/tests/language/error.liq +1 -1
  326. package/tests/liquidsoap/tests/language/json.liq +11 -11
  327. package/tests/liquidsoap/tests/language/list.liq +1 -1
  328. package/tests/liquidsoap/tests/language/metadata.liq +1 -1
  329. package/tests/liquidsoap/tests/language/null.liq +9 -9
  330. package/tests/liquidsoap/tests/language/record.liq +23 -23
  331. package/tests/liquidsoap/tests/language/replaygain.liq +8 -8
  332. package/tests/liquidsoap/tests/language/sqlite.liq +1 -1
  333. package/tests/liquidsoap/tests/language/string.liq +1 -1
  334. package/tests/liquidsoap/tests/language/type_errors.liq +2 -2
  335. package/tests/liquidsoap/tests/language/typing.liq +2 -2
  336. package/tests/liquidsoap/tests/language/various.liq +1 -0
  337. package/tests/liquidsoap/tests/language/xml_test.liq +132 -0
  338. package/tests/liquidsoap/tests/media/dune +74 -0
  339. package/tests/liquidsoap/tests/media/dune.inc +63 -0
  340. package/tests/liquidsoap/tests/media/ffmpeg_add_text.liq +42 -56
  341. package/tests/liquidsoap/tests/media/ffmpeg_copy_concat.liq +5 -2
  342. package/tests/liquidsoap/tests/media/ffmpeg_distributed_hls.liq +2 -4
  343. package/tests/liquidsoap/tests/media/ffmpeg_filter_changing_rate.liq +1 -1
  344. package/tests/liquidsoap/tests/media/ffmpeg_inline_encode_decode_audio.liq +1 -5
  345. package/tests/liquidsoap/tests/media/ffmpeg_inline_encode_decode_video.liq +1 -3
  346. package/tests/liquidsoap/tests/media/ffmpeg_raw_and_encode_decoder.liq +1 -3
  347. package/tests/liquidsoap/tests/media/ffmpeg_raw_hls.liq +2 -4
  348. package/tests/liquidsoap/tests/media/ffmpeg_raw_implicit_conversion.liq +63 -0
  349. package/tests/liquidsoap/tests/media/gen_dune.ml +1 -0
  350. package/tests/liquidsoap/tests/media/image_decoder_duration.liq +15 -0
  351. package/tests/liquidsoap/tests/media/lufs_integrated.liq +17 -0
  352. package/tests/liquidsoap/tests/regression/AC5109.liq +2 -2
  353. package/tests/liquidsoap/tests/regression/GH1129.liq +3 -3
  354. package/tests/liquidsoap/tests/regression/GH1279.liq +2 -2
  355. package/tests/liquidsoap/tests/regression/GH1327.liq +3 -3
  356. package/tests/liquidsoap/tests/regression/GH2585.liq +0 -2
  357. package/tests/liquidsoap/tests/regression/GH2756-2.liq +3 -3
  358. package/tests/liquidsoap/tests/regression/GH2756.liq +2 -2
  359. package/tests/liquidsoap/tests/regression/GH3134.liq +2 -2
  360. package/tests/liquidsoap/tests/regression/GH3303.liq +2 -2
  361. package/tests/liquidsoap/tests/regression/GH4163.liq +14 -3
  362. package/tests/liquidsoap/tests/regression/GH4281-2.liq +29 -0
  363. package/tests/liquidsoap/tests/regression/GH4281.liq +29 -0
  364. package/tests/liquidsoap/tests/regression/GH4395.liq +27 -0
  365. package/tests/liquidsoap/tests/regression/LS354-1.liq +1 -1
  366. package/tests/liquidsoap/tests/regression/LS354-2.liq +1 -1
  367. package/tests/liquidsoap/tests/regression/append-merge.liq +3 -1
  368. package/tests/liquidsoap/tests/regression/dune.inc +64 -0
  369. package/tests/liquidsoap/tests/regression/fallible_ogg.liq +9 -0
  370. package/tests/liquidsoap/tests/regression/init-error.liq +2 -0
  371. package/tests/liquidsoap/tests/regression/seek_track_map.liq +1 -1
  372. package/tests/liquidsoap/tests/regression/source_dynamic.liq +2 -2
  373. package/tests/liquidsoap/tests/run_test.ml +53 -60
  374. package/tests/liquidsoap/tests/streams/cross-override-end.liq +9 -2
  375. package/tests/liquidsoap/tests/streams/cross-override-start.liq +9 -2
  376. package/tests/liquidsoap/tests/streams/cross-override.liq +9 -2
  377. package/tests/liquidsoap/tests/streams/cross-persist-end-override.liq +9 -2
  378. package/tests/liquidsoap/tests/streams/cross-persist-override.liq +9 -2
  379. package/tests/liquidsoap/tests/streams/cross-persist-start-override.liq +9 -2
  380. package/tests/liquidsoap/tests/streams/cross.liq +8 -2
  381. package/tests/liquidsoap/tests/streams/crossfade-plot.liq +11 -8
  382. package/tests/liquidsoap/tests/streams/crossfade-plot.new.txt +148 -248
  383. package/tests/liquidsoap/tests/streams/crossfade-plot.old.txt +50 -150
  384. package/tests/liquidsoap/tests/streams/crossfade.liq +8 -2
  385. package/tests/liquidsoap/tests/streams/ctype2.liq +1 -1
  386. package/tests/liquidsoap/tests/streams/dune.inc +31 -0
  387. package/tests/liquidsoap/tests/streams/fades-overrides.liq +9 -2
  388. package/tests/liquidsoap/tests/streams/fades-persistent-override.liq +9 -2
  389. package/tests/liquidsoap/tests/streams/harbor_metadata_3.liq +1 -1
  390. package/tests/liquidsoap/tests/streams/hls_id3v2.liq +11 -7
  391. package/tests/liquidsoap/tests/streams/icecast_last_meta.liq +47 -0
  392. package/tests/liquidsoap/tests/streams/icecast_ssl.liq +3 -0
  393. package/tests/liquidsoap/tests/streams/icecast_ssl_tls.liq +3 -3
  394. package/tests/liquidsoap/tests/streams/icecast_tls.liq +1 -0
  395. package/tests/liquidsoap/tests/streams/icecast_tls_ssl.liq +5 -0
  396. package/tests/liquidsoap/tests/streams/no-cue-cut.liq +2 -2
  397. package/tests/liquidsoap/tests/streams/random.liq +2 -2
  398. package/tests/liquidsoap/tests/streams/replaygain.liq +10 -10
  399. package/tests/liquidsoap/tests/streams/rotate.liq +6 -1
  400. package/tests/liquidsoap/tests/streams/say.liq +2 -2
  401. package/tests/liquidsoap/tests/streams/srt_listen_callback.liq +1 -0
  402. package/tests/liquidsoap/tests/streams/srt_multiple_outputs.liq +1 -0
  403. package/tests/liquidsoap/tests/streams/srt_passphrase.liq +1 -0
  404. package/tests/liquidsoap/tests/streams/srt_raw_pcm.liq +1 -0
  405. package/tests/liquidsoap/tests/test.liq +4 -5
  406. package/tests/liquidsoap/.github/docker/Dockerfile.production +0 -37
  407. package/tests/liquidsoap/.github/docker/Dockerfile.production-alpine +0 -18
  408. package/tests/liquidsoap/.github/opam/packages/metadata/metadata.0.3.0/opam +0 -33
  409. package/tests/liquidsoap/.github/scripts/build-docker-alpine.sh +0 -31
  410. package/tests/liquidsoap/.github/scripts/build-docker.sh +0 -35
  411. package/tests/liquidsoap/liquidsoap-libs-extra.opam +0 -32
  412. package/tests/liquidsoap/liquidsoap-libs.opam +0 -31
  413. package/tests/liquidsoap/liquidsoap.opam +0 -63
  414. package/tests/liquidsoap/src/core/builtins/builtins_lastfm.ml +0 -110
  415. package/tests/liquidsoap/src/core/tools/liqfm.ml +0 -244
  416. package/tests/liquidsoap/src/libs/extra/lastfm.liq +0 -185
  417. /package/tests/liquidsoap/.github/docker/{Dockerfile.website → website.dockerfile} +0 -0
  418. /package/tests/liquidsoap/.github/opam/packages/ffmpeg-av-windows/{ffmpeg-av-windows.1.2.0 → ffmpeg-av-windows.1.2.5}/opam +0 -0
  419. /package/tests/liquidsoap/.github/opam/packages/ffmpeg-avcodec-windows/{ffmpeg-avcodec-windows.1.2.0 → ffmpeg-avcodec-windows.1.2.5}/opam +0 -0
  420. /package/tests/liquidsoap/.github/opam/packages/ffmpeg-avdevice-windows/{ffmpeg-avdevice-windows.1.2.0 → ffmpeg-avdevice-windows.1.2.5}/opam +0 -0
  421. /package/tests/liquidsoap/.github/opam/packages/ffmpeg-avfilter-windows/{ffmpeg-avfilter-windows.1.2.0 → ffmpeg-avfilter-windows.1.2.5}/opam +0 -0
  422. /package/tests/liquidsoap/.github/opam/packages/ffmpeg-avutil-windows/{ffmpeg-avutil-windows.1.2.0 → ffmpeg-avutil-windows.1.2.5}/opam +0 -0
  423. /package/tests/liquidsoap/.github/opam/packages/ffmpeg-swresample-windows/{ffmpeg-swresample-windows.1.2.0 → ffmpeg-swresample-windows.1.2.5}/opam +0 -0
  424. /package/tests/liquidsoap/.github/opam/packages/ffmpeg-swscale-windows/{ffmpeg-swscale-windows.1.2.0 → ffmpeg-swscale-windows.1.2.5}/opam +0 -0
  425. /package/tests/liquidsoap/.github/opam/packages/ffmpeg-windows/{ffmpeg-windows.1.2.0 → ffmpeg-windows.1.2.5}/opam +0 -0
  426. /package/tests/liquidsoap/{liquidsoap-mode.opam.template → opam/liquidsoap-mode.opam.template} +0 -0
  427. /package/tests/liquidsoap/{liquidsoap.opam.template → opam/liquidsoap.opam.template} +0 -0
@@ -0,0 +1,1097 @@
1
+ (*****************************************************************************
2
+
3
+ Duppy, a task scheduler for OCaml.
4
+ Copyright 2003-2010 Savonet team
5
+
6
+ This program is free software; you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation; either version 2 of the License, or
9
+ (at your option) any later version.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details, fully stated in the COPYING
15
+ file at the root of the liquidsoap distribution.
16
+
17
+ You should have received a copy of the GNU General Public License
18
+ along with this program; if not, write to the Free Software
19
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
+
21
+ *****************************************************************************)
22
+
23
+ module Pcre = Re.Pcre
24
+
25
+ type fd = Unix.file_descr
26
+
27
+ external poll :
28
+ Unix.file_descr array ->
29
+ Unix.file_descr array ->
30
+ Unix.file_descr array ->
31
+ float ->
32
+ Unix.file_descr array * Unix.file_descr array * Unix.file_descr array
33
+ = "caml_poll"
34
+
35
+ let poll r w e timeout =
36
+ let r = Array.of_list r in
37
+ let w = Array.of_list w in
38
+ let e = Array.of_list e in
39
+ let r, w, e = poll r w e timeout in
40
+ (Array.to_list r, Array.to_list w, Array.to_list e)
41
+
42
+ let select, select_fname =
43
+ match Sys.os_type with
44
+ | "Unix" -> (poll, "poll")
45
+ | _ -> (Unix.select, "select")
46
+
47
+ (** [remove f l] is like [List.find f l] but also returns the result of removing
48
+ * the found element from the original list. *)
49
+ let remove f l =
50
+ let rec aux acc = function
51
+ | [] -> raise Not_found
52
+ | x :: l -> if f x then (x, List.rev_append acc l) else aux (x :: acc) l
53
+ in
54
+ aux [] l
55
+
56
+ (** Events and tasks from the implementation point-of-view: * we have to hide
57
+ the 'a parameter. *)
58
+
59
+ type e = { r : fd list; w : fd list; x : fd list; t : float }
60
+
61
+ type 'a t = {
62
+ prio : 'a;
63
+ enrich : e -> e;
64
+ is_ready : e -> (unit -> 'a t list) option;
65
+ }
66
+
67
+ type 'a scheduler = {
68
+ on_error : exn -> Printexc.raw_backtrace -> unit;
69
+ out_pipe : Unix.file_descr;
70
+ in_pipe : Unix.file_descr;
71
+ compare : 'a -> 'a -> int;
72
+ select_m : Mutex.t;
73
+ mutable tasks : 'a t list;
74
+ tasks_m : Mutex.t;
75
+ mutable ready : ('a * (unit -> 'a t list)) list;
76
+ ready_m : Mutex.t;
77
+ mutable queues : Condition.t list;
78
+ queues_m : Mutex.t;
79
+ mutable stop : bool;
80
+ stop_m : Mutex.t;
81
+ queue_stopped_c : Condition.t;
82
+ }
83
+
84
+ let clear_tasks s =
85
+ Mutex.lock s.tasks_m;
86
+ s.tasks <- [];
87
+ Mutex.unlock s.tasks_m
88
+
89
+ let create ?(on_error = Printexc.raise_with_backtrace) ?(compare = compare) () =
90
+ let out_pipe, in_pipe = Unix.pipe () in
91
+ if not Sys.win32 then Unix.set_nonblock in_pipe;
92
+ {
93
+ on_error;
94
+ out_pipe;
95
+ in_pipe;
96
+ compare;
97
+ select_m = Mutex.create ();
98
+ tasks = [];
99
+ tasks_m = Mutex.create ();
100
+ ready = [];
101
+ ready_m = Mutex.create ();
102
+ queues = [];
103
+ queues_m = Mutex.create ();
104
+ stop = false;
105
+ stop_m = Mutex.create ();
106
+ queue_stopped_c = Condition.create ();
107
+ }
108
+
109
+ let wake_up s =
110
+ try ignore (Unix.write s.in_pipe (Bytes.of_string "x") 0 1)
111
+ with
112
+ | Unix.Unix_error (Unix.EAGAIN, _, _)
113
+ | Unix.Unix_error (Unix.EWOULDBLOCK, _, _)
114
+ ->
115
+ ()
116
+
117
+ module Task = struct
118
+ (** Events and tasks from the user's point-of-view. *)
119
+
120
+ type event =
121
+ [ `Delay of float | `Write of fd | `Read of fd | `Exception of fd ]
122
+
123
+ type ('a, 'b) task = {
124
+ priority : 'a;
125
+ events : 'b list;
126
+ handler : 'b list -> ('a, 'b) task list;
127
+ }
128
+
129
+ let time () = Unix.gettimeofday ()
130
+
131
+ let rec t_of_task (task : ('a, [< event ]) task) =
132
+ let t0 = time () in
133
+ {
134
+ prio = task.priority;
135
+ enrich =
136
+ (fun e ->
137
+ List.fold_left
138
+ (fun e -> function
139
+ | `Delay s -> { e with t = min e.t (t0 +. s) }
140
+ | `Read s -> { e with r = s :: e.r }
141
+ | `Write s -> { e with w = s :: e.w }
142
+ | `Exception s -> { e with x = s :: e.x })
143
+ e task.events);
144
+ is_ready =
145
+ (fun e ->
146
+ let l =
147
+ List.filter
148
+ (fun evt ->
149
+ match (evt :> event) with
150
+ | `Delay s when time () > t0 +. s -> true
151
+ | `Read s when List.mem s e.r -> true
152
+ | `Write s when List.mem s e.w -> true
153
+ | `Exception s when List.mem s e.x -> true
154
+ | _ -> false)
155
+ task.events
156
+ in
157
+ if l = [] then None
158
+ else Some (fun () -> List.map t_of_task (task.handler l)));
159
+ }
160
+
161
+ let add_t s items =
162
+ let f item =
163
+ match item.is_ready { r = []; w = []; x = []; t = 0. } with
164
+ | Some f ->
165
+ Mutex.lock s.ready_m;
166
+ s.ready <- (item.prio, f) :: s.ready;
167
+ Mutex.unlock s.ready_m
168
+ | None ->
169
+ Mutex.lock s.tasks_m;
170
+ s.tasks <- item :: s.tasks;
171
+ Mutex.unlock s.tasks_m
172
+ in
173
+ List.iter f items;
174
+ wake_up s
175
+
176
+ let add s t = add_t s [t_of_task t]
177
+ end
178
+
179
+ open Task
180
+
181
+ let stop s =
182
+ clear_tasks s;
183
+ Mutex.lock s.stop_m;
184
+ s.stop <- true;
185
+ Mutex.unlock s.stop_m;
186
+ Mutex.lock s.queues_m;
187
+ while List.length s.queues > 0 do
188
+ wake_up s;
189
+ Mutex.lock s.ready_m;
190
+ List.iter Condition.signal s.queues;
191
+ Mutex.unlock s.ready_m;
192
+ Condition.wait s.queue_stopped_c s.queues_m
193
+ done;
194
+ Mutex.unlock s.queues_m
195
+
196
+ let tmp = Bytes.create 1024
197
+
198
+ (** There should be only one call of #process at a time. * Process waits for
199
+ tasks to become ready, and moves ready tasks * to the ready queue. *)
200
+ let process s log =
201
+ (* Compute the union of all events. *)
202
+ let e =
203
+ List.fold_left
204
+ (fun e t -> t.enrich e)
205
+ { r = [s.out_pipe]; w = []; x = []; t = infinity }
206
+ s.tasks
207
+ in
208
+ (* Poll for an event. *)
209
+ let r, w, x =
210
+ let rec f () =
211
+ try
212
+ let timeout = if e.t = infinity then -1. else max 0. (e.t -. time ()) in
213
+ log
214
+ (Printf.sprintf "Enter %s at %f, timeout %f (%d/%d/%d)." select_fname
215
+ (time ()) timeout (List.length e.r) (List.length e.w)
216
+ (List.length e.x));
217
+ let r, w, x = select e.r e.w e.x timeout in
218
+ log
219
+ (Printf.sprintf "Left %s at %f (%d/%d/%d)." select_fname (time ())
220
+ (List.length r) (List.length w) (List.length x));
221
+ (r, w, x)
222
+ with
223
+ | Unix.Unix_error (Unix.EINTR, _, _) ->
224
+ (* [EINTR] means that select was interrupted by
225
+ * a signal before any of the selected events
226
+ * occurred and before the timeout interval expired.
227
+ * We catch it and restart.. *)
228
+ log (Printf.sprintf "Select interrupted at %f." (time ()));
229
+ f ()
230
+ | e ->
231
+ (* Uncaught exception:
232
+ * 1) Discards all tasks currently in the loop (we do not know which
233
+ * socket caused an error).
234
+ * 2) Re-Raise e *)
235
+ clear_tasks s;
236
+ raise e
237
+ in
238
+ f ()
239
+ in
240
+ (* Empty the wake_up pipe if needed. *)
241
+ let () =
242
+ if List.mem s.out_pipe r then
243
+ (* For safety, we may absorb more than
244
+ * one write. This avoids bad situation
245
+ * when exceesive wake_up may fill up the
246
+ * pipe's write buffer, causing a wake_up
247
+ * to become blocking.. *)
248
+ ignore (Unix.read s.out_pipe tmp 0 1024)
249
+ in
250
+ (* Move ready tasks to the ready list. *)
251
+ let e = { r; w; x; t = 0. } in
252
+ Mutex.lock s.tasks_m;
253
+ (* Split [tasks] into [r]eady and still [w]aiting. *)
254
+ let r, w =
255
+ List.fold_left
256
+ (fun (r, w) t ->
257
+ match t.is_ready e with
258
+ | Some f -> ((t.prio, f) :: r, w)
259
+ | None -> (r, t :: w))
260
+ ([], []) s.tasks
261
+ in
262
+ s.tasks <- w;
263
+ Mutex.unlock s.tasks_m;
264
+ Mutex.lock s.ready_m;
265
+ s.ready <-
266
+ List.stable_sort (fun (p, _) (p', _) -> s.compare p p') (s.ready @ r);
267
+ Mutex.unlock s.ready_m
268
+
269
+ (** Code for a queue to process ready tasks. * Returns true a task was found
270
+ (and hence processed). * * s.ready_m *must* be locked before calling * this
271
+ function, and is freed *only* * if some task was processed. *)
272
+ let exec s (priorities : 'a -> bool) =
273
+ (* This assertion does not work on
274
+ * win32 because a thread can double-lock
275
+ * the same mutex.. *)
276
+ if Sys.os_type <> "Win32" then assert (not (Mutex.try_lock s.ready_m));
277
+ match remove (fun (p, _) -> priorities p) s.ready with
278
+ | (_, task), remaining ->
279
+ s.ready <- remaining;
280
+ Mutex.unlock s.ready_m;
281
+ let tasks =
282
+ match task () with
283
+ | exception exn ->
284
+ let bt = Printexc.get_raw_backtrace () in
285
+ s.on_error exn bt;
286
+ []
287
+ | v -> v
288
+ in
289
+ add_t s tasks;
290
+ true
291
+ | exception Not_found -> false
292
+
293
+ exception Queue_stopped
294
+ exception Queue_processed
295
+
296
+ (** Main loop for queues. *)
297
+ let queue ?log ?(priorities = fun _ -> true) s name =
298
+ let log =
299
+ match log with Some e -> e | None -> Printf.printf "queue %s: %s\n" name
300
+ in
301
+ let c =
302
+ let c = Condition.create () in
303
+ Mutex.lock s.queues_m;
304
+ s.queues <- c :: s.queues;
305
+ Mutex.unlock s.queues_m;
306
+ log (Printf.sprintf "Queue #%d starting..." (List.length s.queues));
307
+ c
308
+ in
309
+ (* Try to process ready tasks, otherwise try to become the master,
310
+ * or be a slave and wait for the master to get some more ready tasks. *)
311
+ let run () =
312
+ Mutex.lock s.stop_m;
313
+ let stop = s.stop in
314
+ Mutex.unlock s.stop_m;
315
+ if stop then raise Queue_stopped;
316
+ (* Lock the ready tasks until the queue has a task to proceed,
317
+ * *or* is really ready to restart on its condition, see the
318
+ * Condition.wait call below for the atomic unlock and wait. *)
319
+ Mutex.lock s.ready_m;
320
+ log (Printf.sprintf "There are %d ready tasks." (List.length s.ready));
321
+ if exec s priorities then raise Queue_processed;
322
+ let wake () =
323
+ let is_ready =
324
+ Mutex.lock s.ready_m;
325
+ let is_ready = s.ready <> [] in
326
+ Mutex.unlock s.ready_m;
327
+ is_ready
328
+ in
329
+ (* Wake up other queues if there are remaining tasks *)
330
+ if is_ready then begin
331
+ Mutex.lock s.queues_m;
332
+ List.iter (fun x -> if x <> c then Condition.signal x) s.queues;
333
+ Mutex.unlock s.queues_m
334
+ end
335
+ in
336
+ if Mutex.try_lock s.select_m then begin
337
+ (* Processing finished for me
338
+ * I can unlock ready_m now.. *)
339
+ Mutex.unlock s.ready_m;
340
+ process s log;
341
+ Mutex.unlock s.select_m;
342
+ wake ()
343
+ end
344
+ else begin
345
+ (* We use s.ready_m mutex here.
346
+ * Hence, we avoid race conditions
347
+ * with any other queue being processing
348
+ * a task that would create a new task:
349
+ * without this mutex, the new task may not be
350
+ * notified to this queue if it is going to sleep
351
+ * in concurrency..
352
+ * It also avoid race conditions when restarting
353
+ * queues since s.ready_m is locked until all
354
+ * queues have been signaled. *)
355
+ Condition.wait c s.ready_m;
356
+ Mutex.unlock s.ready_m
357
+ end
358
+ in
359
+ let rec f () =
360
+ begin
361
+ try run () with Queue_processed -> ()
362
+ end;
363
+ (f [@tailcall]) ()
364
+ in
365
+ let on_done () =
366
+ Mutex.lock s.queues_m;
367
+ s.queues <- List.filter (fun q -> q <> c) s.queues;
368
+ Condition.signal s.queue_stopped_c;
369
+ Mutex.unlock s.queues_m
370
+ in
371
+ (try f () with
372
+ | Queue_stopped -> ()
373
+ | exn ->
374
+ let bt = Printexc.get_raw_backtrace () in
375
+ (try on_done () with _ -> ());
376
+ Printexc.raise_with_backtrace exn bt);
377
+ on_done ()
378
+
379
+ module Async = struct
380
+ (* m is used to make sure that
381
+ * calls to [wake_up] and [stop]
382
+ * are thread-safe. *)
383
+ type t = { stop : bool ref; mutable fd : fd option; m : Mutex.t }
384
+
385
+ exception Stopped
386
+
387
+ let add ~priority (scheduler : 'a scheduler) f =
388
+ (* A pipe to wake up the task *)
389
+ let out_pipe, in_pipe = Unix.pipe () in
390
+ if not Sys.win32 then Unix.set_nonblock in_pipe;
391
+ let stop = ref false in
392
+ let tmp = Bytes.create 1024 in
393
+ let rec task l =
394
+ if List.exists (( = ) (`Read out_pipe)) l then
395
+ (* Consume data from the pipe *)
396
+ ignore (Unix.read out_pipe tmp 0 1024);
397
+ if !stop then begin
398
+ begin
399
+ try
400
+ (* This interface is purely asynchronous
401
+ * so we close both sides of the pipe here. *)
402
+ Unix.close in_pipe;
403
+ Unix.close out_pipe
404
+ with _ -> ()
405
+ end;
406
+ []
407
+ end
408
+ else begin
409
+ let delay = f () in
410
+ let event = if delay >= 0. then [`Delay delay] else [] in
411
+ [{ priority; events = `Read out_pipe :: event; handler = task }]
412
+ end
413
+ in
414
+ let task = { priority; events = [`Read out_pipe]; handler = task } in
415
+ add scheduler task;
416
+ { stop; fd = Some in_pipe; m = Mutex.create () }
417
+
418
+ let wake_up t =
419
+ Mutex.lock t.m;
420
+ try
421
+ begin
422
+ match t.fd with
423
+ | Some t -> (
424
+ try ignore (Unix.write t (Bytes.of_string " ") 0 1)
425
+ with
426
+ | Unix.Unix_error (Unix.EAGAIN, _, _)
427
+ | Unix.Unix_error (Unix.EWOULDBLOCK, _, _)
428
+ ->
429
+ ())
430
+ | None -> raise Stopped
431
+ end;
432
+ Mutex.unlock t.m
433
+ with e ->
434
+ Mutex.unlock t.m;
435
+ raise e
436
+
437
+ let stop t =
438
+ Mutex.lock t.m;
439
+ try
440
+ begin
441
+ match t.fd with
442
+ | Some c ->
443
+ t.stop := true;
444
+ ignore (Unix.write c (Bytes.of_string " ") 0 1)
445
+ | None -> raise Stopped
446
+ end;
447
+ t.fd <- None;
448
+ Mutex.unlock t.m
449
+ with e ->
450
+ Mutex.unlock t.m;
451
+ raise e
452
+ end
453
+
454
+ module type Transport_t = sig
455
+ type t
456
+
457
+ type bigarray =
458
+ (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
459
+
460
+ val sock : t -> Unix.file_descr
461
+ val read : t -> Bytes.t -> int -> int -> int
462
+ val write : t -> Bytes.t -> int -> int -> int
463
+ val ba_write : t -> bigarray -> int -> int -> int
464
+ end
465
+
466
+ module Unix_transport : Transport_t with type t = Unix.file_descr = struct
467
+ type t = Unix.file_descr
468
+
469
+ type bigarray =
470
+ (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
471
+
472
+ let sock s = s
473
+ let read = Unix.read
474
+ let write = Unix.write
475
+
476
+ external ba_write : t -> bigarray -> int -> int -> int
477
+ = "ocaml_duppy_write_ba"
478
+ end
479
+
480
+ module type Io_t = sig
481
+ type socket
482
+ type marker = Length of int | Split of string
483
+
484
+ type bigarray =
485
+ (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
486
+
487
+ type failure =
488
+ | Io_error
489
+ | Unix of Unix.error * string * string
490
+ | Unknown of exn
491
+ | Timeout
492
+
493
+ val read :
494
+ ?recursive:bool ->
495
+ ?init:string ->
496
+ ?on_error:(string * failure -> unit) ->
497
+ ?timeout:float ->
498
+ priority:'a ->
499
+ 'a scheduler ->
500
+ socket ->
501
+ marker ->
502
+ (string * string option -> unit) ->
503
+ unit
504
+
505
+ val write :
506
+ ?exec:(unit -> unit) ->
507
+ ?on_error:(failure -> unit) ->
508
+ ?bigarray:bigarray ->
509
+ ?offset:int ->
510
+ ?length:int ->
511
+ ?string:Bytes.t ->
512
+ ?timeout:float ->
513
+ priority:'a ->
514
+ 'a scheduler ->
515
+ socket ->
516
+ unit
517
+ end
518
+
519
+ module MakeIo (Transport : Transport_t) : Io_t with type socket = Transport.t =
520
+ struct
521
+ type socket = Transport.t
522
+ type marker = Length of int | Split of string
523
+
524
+ type failure =
525
+ | Io_error
526
+ | Unix of Unix.error * string * string
527
+ | Unknown of exn
528
+ | Timeout
529
+
530
+ exception Io
531
+ exception Timeout_exc
532
+
533
+ type bigarray =
534
+ (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
535
+
536
+ let read ?(recursive = false) ?(init = "") ?(on_error = fun _ -> ()) ?timeout
537
+ ~priority (scheduler : 'a scheduler) socket marker exec =
538
+ let length = 1024 in
539
+ let b = Buffer.create length in
540
+ let buf = Bytes.make length ' ' in
541
+ Buffer.add_string b init;
542
+ let unix_socket = Transport.sock socket in
543
+ let events, check_timeout =
544
+ match timeout with
545
+ | None -> ([`Read unix_socket], fun _ -> false)
546
+ | Some f -> ([`Read unix_socket; `Delay f], List.mem (`Delay f))
547
+ in
548
+ let rec f l =
549
+ if check_timeout l then raise Timeout_exc;
550
+ if List.mem (`Read unix_socket) l then begin
551
+ let input = Transport.read socket buf 0 length in
552
+ if input <= 0 then raise Io;
553
+ Buffer.add_subbytes b buf 0 input
554
+ end;
555
+ let ret =
556
+ match marker with
557
+ | Split r ->
558
+ let rex = Pcre.regexp r in
559
+ let acc = Buffer.contents b in
560
+ let ret = Pcre.full_split ~max:2 ~rex acc in
561
+ let rec p l =
562
+ match l with
563
+ | Pcre.Text x :: Pcre.Delim _ :: l ->
564
+ let f b x =
565
+ match x with
566
+ | Pcre.Text s | Pcre.Delim s -> Buffer.add_string b s
567
+ | _ -> ()
568
+ in
569
+ if recursive then begin
570
+ Buffer.reset b;
571
+ List.iter (f b) l;
572
+ Some (x, None)
573
+ end
574
+ else begin
575
+ let b = Buffer.create 10 in
576
+ List.iter (f b) l;
577
+ Some (x, Some (Buffer.contents b))
578
+ end
579
+ | _ :: l' -> p l'
580
+ | [] -> None
581
+ in
582
+ p ret
583
+ | Length n when n <= Buffer.length b ->
584
+ let s = Buffer.sub b 0 n in
585
+ let rem = Buffer.sub b n (Buffer.length b - n) in
586
+ if recursive then begin
587
+ Buffer.reset b;
588
+ Buffer.add_string b rem;
589
+ Some (s, None)
590
+ end
591
+ else Some (s, Some rem)
592
+ | _ -> None
593
+ in
594
+ (* Catch all exceptions.. *)
595
+ let f x =
596
+ try f x with
597
+ | Io ->
598
+ on_error (Buffer.contents b, Io_error);
599
+ []
600
+ | Timeout_exc ->
601
+ on_error (Buffer.contents b, Timeout);
602
+ []
603
+ | Unix.Unix_error (x, y, z) ->
604
+ on_error (Buffer.contents b, Unix (x, y, z));
605
+ []
606
+ | e ->
607
+ on_error (Buffer.contents b, Unknown e);
608
+ []
609
+ in
610
+ match ret with
611
+ | Some x -> (
612
+ match x with
613
+ | s, Some _ when recursive ->
614
+ exec (s, None);
615
+ [{ priority; events; handler = f }]
616
+ | _ ->
617
+ exec x;
618
+ [])
619
+ | None -> [{ priority; events; handler = f }]
620
+ in
621
+ (* Catch all exceptions.. *)
622
+ let f x =
623
+ try f x with
624
+ | Io ->
625
+ on_error (Buffer.contents b, Io_error);
626
+ []
627
+ | Timeout_exc ->
628
+ on_error (Buffer.contents b, Timeout);
629
+ []
630
+ | Unix.Unix_error (x, y, z) ->
631
+ on_error (Buffer.contents b, Unix (x, y, z));
632
+ []
633
+ | e ->
634
+ on_error (Buffer.contents b, Unknown e);
635
+ []
636
+ in
637
+ (* First one is without read,
638
+ * in case init contains the wanted match.
639
+ * Unless the user sets timeout to 0., this
640
+ * should not interfere with user-defined timeout.. *)
641
+ let task =
642
+ { priority; events = [`Delay 0.; `Read unix_socket]; handler = f }
643
+ in
644
+ add scheduler task
645
+
646
+ let write ?(exec = fun () -> ()) ?(on_error = fun _ -> ()) ?bigarray
647
+ ?(offset = 0) ?length ?string ?timeout ~priority
648
+ (scheduler : 'a scheduler) socket =
649
+ let length, write =
650
+ match (string, bigarray) with
651
+ | Some s, _ ->
652
+ let length =
653
+ match length with Some length -> length | None -> Bytes.length s
654
+ in
655
+ (length, Transport.write socket s)
656
+ | None, Some b ->
657
+ let length =
658
+ match length with
659
+ | Some length -> length
660
+ | None -> Bigarray.Array1.dim b
661
+ in
662
+ (length, Transport.ba_write socket b)
663
+ | _ -> (0, fun _ _ -> 0)
664
+ in
665
+ let unix_socket = Transport.sock (socket : Transport.t) in
666
+ let exec () =
667
+ if Sys.os_type = "Win32" then Unix.clear_nonblock unix_socket;
668
+ exec ()
669
+ in
670
+ let events, check_timeout =
671
+ match timeout with
672
+ | None -> ([`Write unix_socket], fun _ -> false)
673
+ | Some f -> ([`Write unix_socket; `Delay f], List.mem (`Delay f))
674
+ in
675
+ let rec f pos l =
676
+ try
677
+ if check_timeout l then raise Timeout_exc;
678
+ assert (List.exists (( = ) (`Write unix_socket)) l);
679
+ let len = length - pos in
680
+ let n = write pos len in
681
+ if n <= 0 then (
682
+ on_error Io_error;
683
+ [])
684
+ else if n < len then
685
+ [{ priority; events = [`Write unix_socket]; handler = f (pos + n) }]
686
+ else (
687
+ exec ();
688
+ [])
689
+ with
690
+ | Unix.Unix_error (Unix.EWOULDBLOCK, _, _) when Sys.os_type = "Win32" ->
691
+ [{ priority; events = [`Write unix_socket]; handler = f pos }]
692
+ | Timeout_exc ->
693
+ on_error Timeout;
694
+ []
695
+ | Unix.Unix_error (x, y, z) ->
696
+ on_error (Unix (x, y, z));
697
+ []
698
+ | e ->
699
+ on_error (Unknown e);
700
+ []
701
+ in
702
+ let task = { priority; events; handler = f offset } in
703
+ if length > 0 then
704
+ (* Win32 is particularly bad with writing on sockets. It is nearly impossible
705
+ * to write proper non-blocking code. send will block on blocking sockets if
706
+ * there isn't enough data available instead of returning a partial buffer
707
+ * and WSAEventSelect will not return if the socket still has available space.
708
+ * Thus, setting the socket to non-blocking and writing as much as we can. *)
709
+ if Sys.os_type = "Win32" then begin
710
+ Unix.set_nonblock unix_socket;
711
+ List.iter (add scheduler) (f offset [`Write unix_socket])
712
+ end
713
+ else add scheduler task
714
+ else exec ()
715
+ end
716
+
717
+ module Io : Io_t with type socket = Unix.file_descr = MakeIo (Unix_transport)
718
+
719
+ (** A monad for implicit continuations or responses *)
720
+ module Monad = struct
721
+ type ('a, 'b) handler = { return : 'a -> unit; raise : 'b -> unit }
722
+ type ('a, 'b) t = ('a, 'b) handler -> unit
723
+
724
+ let return x h = h.return x
725
+ let raise x h = h.raise x
726
+
727
+ let bind f g h =
728
+ let ret x =
729
+ let process = g x in
730
+ process h
731
+ in
732
+ f { return = ret; raise = h.raise }
733
+
734
+ let ( >>= ) = bind
735
+ let run ~return:ret ~raise f = f { return = ret; raise }
736
+
737
+ let catch f g h =
738
+ let raise x =
739
+ let process = g x in
740
+ process h
741
+ in
742
+ f { return = h.return; raise }
743
+
744
+ let ( =<< ) x y = catch y x
745
+
746
+ let rec fold_left f a = function
747
+ | [] -> a
748
+ | b :: l -> fold_left f (bind a (fun a -> f a b)) l
749
+
750
+ let fold_left f a l = fold_left f (return a) l
751
+ let iter f l = fold_left (fun () b -> f b) () l
752
+
753
+ module Mutex_o = Mutex
754
+
755
+ module Mutex = struct
756
+ module type Mutex_control = sig
757
+ type priority
758
+
759
+ val scheduler : priority scheduler
760
+ val priority : priority
761
+ end
762
+
763
+ module type Mutex_t = sig
764
+ (** Type for a mutex. *)
765
+ type mutex
766
+
767
+ module Control : Mutex_control
768
+
769
+ (** [create ()] creates a mutex. Implementation-wise, * a duppy task is
770
+ created that will be used to select a * waiting computation, lock the
771
+ mutex on it and resume it. * Thus, [priority] and [s] represents,
772
+ resp., the priority * and scheduler used when running calling process'
773
+ computation. *)
774
+ val create : unit -> mutex
775
+
776
+ (** A computation that locks a mutex * and returns [unit] afterwards.
777
+ Computation * will be blocked until the mutex is successfully locked.
778
+ *)
779
+ val lock : mutex -> (unit, 'a) t
780
+
781
+ (** A computation that tries to lock a mutex. * Returns immediately [true]
782
+ if the mutex was successfully locked * or [false] otherwise. *)
783
+ val try_lock : mutex -> (bool, 'a) t
784
+
785
+ (** A computation that unlocks a mutex. * Should return immediately. *)
786
+ val unlock : mutex -> (unit, 'a) t
787
+ end
788
+
789
+ module Factory (Control : Mutex_control) = struct
790
+ (* A mutex is either locked or not
791
+ * and has a list of tasks waiting to get
792
+ * it. *)
793
+ type mutex = {
794
+ mutable locked : bool;
795
+ mutable tasks : (unit -> unit) list;
796
+ }
797
+
798
+ module Control = Control
799
+
800
+ let tmp = Bytes.create 1024
801
+ let x, y = Unix.pipe ()
802
+ let stop = ref false
803
+ let wake_up () = ignore (Unix.write y (Bytes.of_string " ") 0 1)
804
+ let ctl_m = Mutex_o.create ()
805
+
806
+ let finalise _ =
807
+ stop := true;
808
+ wake_up ()
809
+
810
+ let mutexes = Queue.create ()
811
+ let () = Gc.finalise finalise mutexes
812
+
813
+ let register () =
814
+ let m = { locked = false; tasks = [] } in
815
+ Queue.push m mutexes;
816
+ m
817
+
818
+ let cleanup m =
819
+ Mutex_o.lock ctl_m;
820
+ let q = Queue.create () in
821
+ Queue.iter (fun m' -> if m <> m' then Queue.push m q) mutexes;
822
+ Queue.clear mutexes;
823
+ Queue.transfer q mutexes;
824
+ Mutex_o.unlock ctl_m
825
+
826
+ let task f =
827
+ {
828
+ Task.priority = Control.priority;
829
+ events = [`Delay 0.];
830
+ handler =
831
+ (fun _ ->
832
+ f ();
833
+ []);
834
+ }
835
+
836
+ (* This should only be called when [ctl_m] is locked. *)
837
+ let process_mutex tasks m =
838
+ if not m.locked then (
839
+ (* I don't think shuffling tasks
840
+ * matters here.. *)
841
+ match m.tasks with
842
+ | x :: l ->
843
+ m.tasks <- l;
844
+ m.locked <- true;
845
+ task x :: tasks
846
+ | _ -> tasks)
847
+ else tasks
848
+
849
+ let rec handler _ =
850
+ Mutex_o.lock ctl_m;
851
+ if not !stop then begin
852
+ let tasks = Queue.fold process_mutex [] mutexes in
853
+ Mutex_o.unlock ctl_m;
854
+ ignore (Unix.read x tmp 0 1024);
855
+ { Task.priority = Control.priority; events = [`Read x]; handler }
856
+ :: tasks
857
+ end
858
+ else begin
859
+ Mutex_o.unlock ctl_m;
860
+ try
861
+ Unix.close x;
862
+ Unix.close y;
863
+ []
864
+ with _ -> []
865
+ end
866
+
867
+ let () =
868
+ Task.add Control.scheduler
869
+ { Task.priority = Control.priority; events = [`Read x]; handler }
870
+
871
+ let create () =
872
+ Mutex_o.lock ctl_m;
873
+ let ret = register () in
874
+ Mutex_o.unlock ctl_m;
875
+ Gc.finalise cleanup ret;
876
+ ret
877
+
878
+ let lock m h' =
879
+ Mutex_o.lock ctl_m;
880
+ if not m.locked then begin
881
+ m.locked <- true;
882
+ Mutex_o.unlock ctl_m;
883
+ h'.return ()
884
+ end
885
+ else begin
886
+ m.tasks <- h'.return :: m.tasks;
887
+ Mutex_o.unlock ctl_m
888
+ end
889
+
890
+ let try_lock m h' =
891
+ Mutex_o.lock ctl_m;
892
+ if not m.locked then begin
893
+ m.locked <- true;
894
+ Mutex_o.unlock ctl_m;
895
+ h'.return true
896
+ end
897
+ else begin
898
+ Mutex_o.unlock ctl_m;
899
+ h'.return false
900
+ end
901
+
902
+ let unlock m h' =
903
+ Mutex_o.lock ctl_m;
904
+ (* Here we allow inter-thread
905
+ * and double unlock.. Double unlock
906
+ * is not necessarily a problem and
907
+ * inter-thread unlock well.. what is
908
+ * a thread here ?? :-) *)
909
+ m.locked <- false;
910
+ let wake = m.tasks <> [] in
911
+ Mutex_o.unlock ctl_m;
912
+ if wake then wake_up ();
913
+ h'.return ()
914
+ end
915
+ end
916
+
917
+ module Condition = struct
918
+ module Factory (Mutex : Mutex.Mutex_t) = struct
919
+ type condition = {
920
+ condition_m : Mutex_o.t;
921
+ waiting : (unit -> unit) Queue.t;
922
+ }
923
+
924
+ module Control = Mutex.Control
925
+
926
+ let create () =
927
+ { condition_m = Mutex_o.create (); waiting = Queue.create () }
928
+
929
+ (* Mutex.unlock m needs to happen _after_
930
+ * the task has been registered. *)
931
+ let wait c m h =
932
+ let proc () = Mutex.lock m h in
933
+ Mutex_o.lock c.condition_m;
934
+ Queue.push proc c.waiting;
935
+ Mutex_o.unlock c.condition_m;
936
+ (* Mutex.unlock does not raise exceptions (for now..) *)
937
+ let h' = { return = (fun () -> ()); raise = (fun _ -> assert false) } in
938
+ Mutex.unlock m h'
939
+
940
+ let wake_up h =
941
+ let handler _ =
942
+ h ();
943
+ []
944
+ in
945
+ Task.add Control.scheduler
946
+ { Task.priority = Control.priority; events = [`Delay 0.]; handler }
947
+
948
+ let signal c h =
949
+ Mutex_o.lock c.condition_m;
950
+ let h' = Queue.pop c.waiting in
951
+ Mutex_o.unlock c.condition_m;
952
+ wake_up h';
953
+ h.return ()
954
+
955
+ let broadcast c h =
956
+ let q = Queue.create () in
957
+ Mutex_o.lock c.condition_m;
958
+ Queue.transfer c.waiting q;
959
+ Mutex_o.unlock c.condition_m;
960
+ Queue.iter wake_up q;
961
+ h.return ()
962
+ end
963
+ end
964
+
965
+ module type Monad_io_t = sig
966
+ type socket
967
+
968
+ module Io : Io_t with type socket = socket
969
+
970
+ type ('a, 'b) handler = {
971
+ scheduler : 'a scheduler;
972
+ socket : Io.socket;
973
+ mutable data : string;
974
+ on_error : Io.failure -> 'b;
975
+ }
976
+
977
+ val exec :
978
+ ?delay:float ->
979
+ priority:'a ->
980
+ ('a, 'b) handler ->
981
+ ('c, 'b) t ->
982
+ ('c, 'b) t
983
+
984
+ val delay : priority:'a -> ('a, 'b) handler -> float -> (unit, 'b) t
985
+
986
+ val read :
987
+ ?timeout:float ->
988
+ priority:'a ->
989
+ marker:Io.marker ->
990
+ ('a, 'b) handler ->
991
+ (string, 'b) t
992
+
993
+ val read_all :
994
+ ?timeout:float ->
995
+ priority:'a ->
996
+ 'a scheduler ->
997
+ Io.socket ->
998
+ (string, string * Io.failure) t
999
+
1000
+ val write :
1001
+ ?timeout:float ->
1002
+ priority:'a ->
1003
+ ('a, 'b) handler ->
1004
+ ?offset:int ->
1005
+ ?length:int ->
1006
+ Bytes.t ->
1007
+ (unit, 'b) t
1008
+
1009
+ val write_bigarray :
1010
+ ?timeout:float ->
1011
+ priority:'a ->
1012
+ ('a, 'b) handler ->
1013
+ Io.bigarray ->
1014
+ (unit, 'b) t
1015
+ end
1016
+
1017
+ module MakeIo (Io : Io_t) = struct
1018
+ type socket = Io.socket
1019
+
1020
+ module Io = Io
1021
+
1022
+ type ('a, 'b) handler = {
1023
+ scheduler : 'a scheduler;
1024
+ socket : Io.socket;
1025
+ mutable data : string;
1026
+ on_error : Io.failure -> 'b;
1027
+ }
1028
+
1029
+ let exec ?(delay = 0.) ~priority h f h' =
1030
+ let handler _ =
1031
+ begin
1032
+ try f h' with e -> h'.raise (h.on_error (Io.Unknown e))
1033
+ end;
1034
+ []
1035
+ in
1036
+ Task.add h.scheduler { Task.priority; events = [`Delay delay]; handler }
1037
+
1038
+ let delay ~priority h delay = exec ~delay ~priority h (return ())
1039
+
1040
+ let read ?timeout ~priority ~marker h h' =
1041
+ let process x =
1042
+ let s =
1043
+ match x with
1044
+ | s, None ->
1045
+ h.data <- "";
1046
+ s
1047
+ | s, Some s' ->
1048
+ h.data <- s';
1049
+ s
1050
+ in
1051
+ h'.return s
1052
+ in
1053
+ let init = h.data in
1054
+ h.data <- "";
1055
+ let on_error (s, x) =
1056
+ h.data <- s;
1057
+ h'.raise (h.on_error x)
1058
+ in
1059
+ Io.read ?timeout ~priority ~init ~recursive:false ~on_error h.scheduler
1060
+ h.socket marker process
1061
+
1062
+ let read_all ?timeout ~priority s sock =
1063
+ let handler =
1064
+ { scheduler = s; socket = sock; data = ""; on_error = (fun e -> e) }
1065
+ in
1066
+ let buf = Buffer.create 1024 in
1067
+ let rec f () =
1068
+ let data = read ?timeout ~priority ~marker:(Io.Length 1024) handler in
1069
+ let process data =
1070
+ Buffer.add_string buf data;
1071
+ f ()
1072
+ in
1073
+ data >>= process
1074
+ in
1075
+ let catch_ret e =
1076
+ Buffer.add_string buf handler.data;
1077
+ match e with
1078
+ | Io.Io_error -> return (Buffer.contents buf)
1079
+ | e -> raise (Buffer.contents buf, e)
1080
+ in
1081
+ catch (f ()) catch_ret
1082
+
1083
+ let write ?timeout ~priority h ?offset ?length s h' =
1084
+ let on_error x = h'.raise (h.on_error x) in
1085
+ let exec () = h'.return () in
1086
+ Io.write ?timeout ~priority ~on_error ~exec ?offset ?length ~string:s
1087
+ h.scheduler h.socket
1088
+
1089
+ let write_bigarray ?timeout ~priority h ba h' =
1090
+ let on_error x = h'.raise (h.on_error x) in
1091
+ let exec () = h'.return () in
1092
+ Io.write ?timeout ~priority ~on_error ~exec ~bigarray:ba h.scheduler
1093
+ h.socket
1094
+ end
1095
+
1096
+ module Io = MakeIo (Io)
1097
+ end