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 fd

See 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 -> connection

Create a new connection from a file descriptor

val is_open : connection -> bool

Check if a connection is open

val close : connection -> unit

Close the connection

Low-Level IO Operations

val read_exact : Unix.file_descr -> int -> bytes

Read 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 -> unit

Write all bytes to a file descriptor.

val read_one_byte : Unix.file_descr -> int

Read a single byte

val write_one_byte : Unix.file_descr -> int -> unit

Write a single byte

val read_int32_be_fd : Unix.file_descr -> int

Read a 4-byte big-endian integer

val write_int32_be_fd : Unix.file_descr -> int -> unit

Write a 4-byte big-endian integer

Effect Handlers

val run_with_fd : Unix.file_descr -> (unit -> 'a) -> 'a

Run 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) -> 'a

Run 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_descr

Create a listening TCP socket on the given port

val accept_conn : Unix.file_descr -> Unix.file_descr

Accept a connection on a listening socket

val connect_tcp : string -> int -> Unix.file_descr

Connect to a TCP server

val run_tcp_server : port:int -> handler:(Unix.file_descr -> unit) -> 'a

Run 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) -> 'a

Read a file using effects

val write_file : string -> (unit -> 'a) -> 'a

Write a file using effects

Pipe Operations

val create_pipe : unit -> Unix.file_descr * Unix.file_descr

Create a pipe and return (read_fd, write_fd)

val run_with_pipe : (((unit -> 'a) -> 'a) -> ((unit -> 'b) -> 'b) -> 'c) -> 'c

Run a function with a pipe pair for testing. The function receives (reader, writer) functions that run with the respective file descriptors.