Crdt.Io_unix
crdt · API reference
Unix blocking IO effect handler
Unix blocking IO effect handler.
This module provides a simple blocking effect handler using Unix file descriptors. It's suitable for CLI tools, simple scripts, and situations where async IO is not needed.
Usage:
let fd = Unix.(openfile "data.bin" [O_RDONLY] 0o644) in
let result = Io_unix.run_with_fd fd (fun () ->
let data = Io_intf.read_bytes 100 in
(* ... process data ... *)
) in
Unix.close fdSee Io_intf for the effect definitions.
Types
type connection = {
mutable fd : Unix.file_descr option;
mutable closed : bool;
mutable bytes_read : int;
mutable bytes_written : int;
}Connection state for Unix IO
val create : Unix.file_descr -> connectionCreate a new connection from a file descriptor
val is_open : connection -> boolCheck if a connection is open
val close : connection -> unitClose the connection
Low-Level IO Operations
val read_exact : Unix.file_descr -> int -> bytesRead exactly n bytes from a file descriptor. Raises End_of_file if EOF is reached before all bytes are read.
val write_all : Unix.file_descr -> bytes -> unitWrite all bytes to a file descriptor.
val read_one_byte : Unix.file_descr -> intRead a single byte
val write_one_byte : Unix.file_descr -> int -> unitWrite a single byte
val read_int32_be_fd : Unix.file_descr -> intRead a 4-byte big-endian integer
val write_int32_be_fd : Unix.file_descr -> int -> unitWrite a 4-byte big-endian integer
Effect Handlers
val run_with_fd : Unix.file_descr -> (unit -> 'a) -> 'aRun a function with blocking Unix IO using a file descriptor.
This handler intercepts IO effects and translates them to Unix system calls on the given file descriptor.
val run_stdio : (unit -> 'a) -> 'aRun a function with stdin/stdout for IO.
Reads come from stdin, writes go to stdout. Useful for CLI tools that process data from pipes.
Socket Operations
val listen_tcp : int -> Unix.file_descrCreate a listening TCP socket on the given port
val accept_conn : Unix.file_descr -> Unix.file_descrAccept a connection on a listening socket
val connect_tcp : string -> int -> Unix.file_descrConnect to a TCP server
val run_tcp_server : port:int -> handler:(Unix.file_descr -> unit) -> 'aRun a simple TCP server that handles one connection at a time.
parameter port The port to listen on parameter handler Function to handle each connection (receives fd) Example:
Io_unix.run_tcp_server ~port:8080 (fun fd ->
Io_unix.run_with_fd fd (fun () ->
let request = Io_intf.read_framed_message () in
let response = process request in
Io_intf.write_framed_message response))File Operations
val read_file : string -> (unit -> 'a) -> 'aRead a file using effects
val write_file : string -> (unit -> 'a) -> 'aWrite a file using effects
Pipe Operations
val create_pipe : unit -> Unix.file_descr * Unix.file_descrCreate a pipe and return (read_fd, write_fd)
val run_with_pipe : (((unit -> 'a) -> 'a) -> ((unit -> 'b) -> 'b) -> 'c) -> 'cRun a function with a pipe pair for testing. The function receives (reader, writer) functions that run with the respective file descriptors.