module Uwt_io:sig
..end
Buffered byte channels
A channel is a high-level object for performing input/output (IO). It allows to read/write from/to the outside world in an efficient way, by minimising the number of system calls.
An output channel is used to send data and an input channel is used to receive data.
If you are familiar with buffered channels you may be familiar too with the flush operation. Note that byte channels of this module are automatically flushed when there is nothing else to do (i.e. before the program becomes idle), so this means that you no longer have to write:
eprintf "log message\n";
flush stderr;
to have your messages displayed.
Note about errors: input functions of this module raise
End_of_file
when the end-of-file is reached (i.e. when the read
function returns 0
). Other exceptions are ones caused by the
backend read/write functions, such as Unix.Unix_error
.
exception Channel_closed of string
Exception raised when a channel is closed. The parameter is a description of the channel.
type 'mode
channel
Type of buffered byte channels
type
input
Input mode
type
output
Output mode
type 'a
mode =
| |
Input : |
| |
Output : |
Channel mode
val input : input mode
input
input mode representation
val output : output mode
output
output mode representation
typeinput_channel =
input channel
Type of input channels
typeoutput_channel =
output channel
Type of output channels
val mode : 'a channel -> 'a mode
mode ch
returns the mode of a channel
val stdin : input_channel
The standard input, it reads data from Uwt.stdin
val stdout : output_channel
The standard output, it writes data to Uwt.stdout
val stderr : output_channel
The standard output for error messages, it writes data to
Uwt.stderr
val zero : input_channel
Inputs which returns always '\x00'
val null : output_channel
Output which drops everything
val pipe : ?cloexec:bool ->
?in_buffer:Uwt_bytes.t ->
?out_buffer:Uwt_bytes.t ->
unit -> input_channel * output_channel
pipe ?cloexec ?in_buffer ?out_buffer ()
creates a pipe using
Uwt.Unix.pipe
and makes two channels from the two returned file
descriptors
val make : ?buffer:Uwt_bytes.t ->
?close:(unit -> unit Lwt.t) ->
?seek:(int64 -> Unix.seek_command -> int64 Lwt.t) ->
mode:'mode mode ->
(Uwt_bytes.t -> int -> int -> int Lwt.t) -> 'mode channel
make ?buffer ?close ~mode perform_io
is the
main function for creating new channels.
buffer
: user-supplied buffer. When this argument is
present, its value will be used as the buffer for created
channel. Size of buffer must conform to limitations described
in Uwt_io.set_default_buffer_size
.
When this argument is not present, a new internal buffer of default
size will be allocated for this channel.
Warning: do not use the same buffer for simultaneous work with
more than one channel.
There are other functions in this module that take buffer
argument, their semantics agrees with the described above.close
: close function of the channel. It defaults to
Lwt.return
seek
: same meaning as Unix.lseek
val of_bytes : mode:'mode mode -> Uwt_bytes.t -> 'mode channel
Create a channel from a byte array. Reading/writing is done directly on the provided array.
val of_file : ?buffer:Uwt_bytes.t ->
?close:(unit -> unit Lwt.t) ->
mode:'m mode -> Uwt.file -> 'm channel
of_file ?buffer ?close ~mode fd
creates a channel from a
file descriptor.
close
: defaults to closing the file descriptor.val of_stream : ?buffer:Uwt_bytes.t ->
?close:(unit -> unit Lwt.t) ->
mode:'m mode -> Uwt.Stream.t -> 'm channel
val of_pipe : ?buffer:Uwt_bytes.t ->
?close:(unit -> unit Lwt.t) ->
mode:'m mode -> Uwt.Pipe.t -> 'm channel
val of_tcp : ?buffer:Uwt_bytes.t ->
?close:(unit -> unit Lwt.t) ->
mode:'m mode -> Uwt.Tcp.t -> 'm channel
val close : 'a channel -> unit Lwt.t
close ch
closes the given channel. If ch
is an output
channel, it performs all pending actions, flushes it and closes
it. If ch
is an input channel, it just closes it immediately.
close
returns the result of the close function of the
channel. Multiple calls to close
will return exactly the same
value.
Note: you cannot use close
on channels obtained with
Uwt_io.atomic
.
val abort : 'a channel -> unit Lwt.t
abort ch
abort current operations and close the channel
immediately.
val atomic : ('a channel -> 'b Lwt.t) -> 'a channel -> 'b Lwt.t
atomic f
transforms a sequence of io operations into one
single atomic io operation.
Note:
f
is invalid after f
terminatesatomic
can be called inside another atomic
val file_length : string -> int64 Lwt.t
Retrieves the length of the file at the given path. If the path refers to a
directory, the returned promise is rejected with
Unix.(Unix_error (EISDIR, _, _))
.
val buffered : 'a channel -> int
buffered oc
returns the number of bytes in the buffer
val flush : output_channel -> unit Lwt.t
flush oc
performs all pending writes on oc
val flush_all : unit -> unit Lwt.t
flush_all ()
flushes all open output channels
val buffer_size : 'a channel -> int
Returns the size of the internal buffer.
val resize_buffer : 'a channel -> int -> unit Lwt.t
Resize the internal buffer to the given size
val is_busy : 'a channel -> bool
is_busy channel
returns whether the given channel is currently
busy. A channel is busy when there is at least one job using it
that has not yet terminated.
val position : 'a channel -> int64
position ch
Returns the current position in the channel.
val set_position : 'a channel -> int64 -> unit Lwt.t
set_position ch pos
Sets the position in the output channel. This
does not work if the channel does not support random access.
val length : 'a channel -> int64 Lwt.t
Returns the length of the channel in bytes
Note: except for functions dealing with streams (Uwt_io.read_chars
and
Uwt_io.read_lines
) all functions are atomic.
val read_char : input_channel -> char Lwt.t
read_char ic
reads the next character of ic
.
End_of_file
if the end of the file is reachedval read_char_opt : input_channel -> char option Lwt.t
Same as read_byte
but does not raise End_of_file
on end of
input
val read_chars : input_channel -> char Lwt_stream.t
read_chars ic
returns a stream holding all characters of
ic
val read_line : input_channel -> string Lwt.t
read_line ic
reads one complete line from ic
and returns it
without the end of line. End of line is either "\n"
or
"\r\n"
.
If the end of line is reached before reading any character,
End_of_file
is raised. If it is reached before reading an end
of line but characters have already been read, they are
returned.
val read_line_opt : input_channel -> string option Lwt.t
Same as Uwt_io.read_line
but do not raise End_of_file
on end of
input.
val read_lines : input_channel -> string Lwt_stream.t
read_lines ic
returns a stream holding all lines of ic
val read : ?count:int -> input_channel -> string Lwt.t
read ?count ic
reads at most count
characters from ic
. It
returns ""
if the end of input is reached. If count
is not
specified, it reads all bytes until the end of input.
val read_into : input_channel -> Bytes.t -> int -> int -> int Lwt.t
read_into ic buffer offset length
reads up to length
bytes,
stores them in buffer
at offset offset
, and returns the
number of bytes read.
Note: read_into
does not raise End_of_file
, it returns a
length of 0
instead.
val read_into_exactly : input_channel -> Bytes.t -> int -> int -> unit Lwt.t
read_into_exactly ic buffer offset length
reads exactly
length
bytes and stores them in buffer
at offset offset
.
End_of_file
on end of inputval read_value : input_channel -> 'a Lwt.t
read_value channel
reads a marshaled value from channel
; it corresponds
to the standard library's
Marshal.from_channel
.
The corresponding writing function is Uwt_io.write_value
.
Note that reading marshaled values is not, in general, type-safe. See
the warning in the description of module
Marshal
for details. The short version is: if you read a value of one
type, such as string
, when a value of another type, such as int
has
actually been marshaled to channel
, you may get arbitrary behavior,
including segmentation faults, access violations, security bugs, etc.
Note: as for reading functions, all functions except
Uwt_io.write_chars
and Uwt_io.write_lines
are atomic.
For example if you use Uwt_io.write_line
in two different threads, the
two operations will be serialized, and lines cannot be mixed.
val write_char : output_channel -> char -> unit Lwt.t
write_char oc char
writes char
on oc
val write_chars : output_channel -> char Lwt_stream.t -> unit Lwt.t
write_chars oc chars
writes all characters of chars
on
oc
val write : output_channel -> string -> unit Lwt.t
write oc str
writes all characters of str
on oc
val write_line : output_channel -> string -> unit Lwt.t
write_line oc str
writes str
on oc
followed by a
new-line.
val write_lines : output_channel -> string Lwt_stream.t -> unit Lwt.t
write_lines oc lines
writes all lines of lines
to oc
val write_from : output_channel -> Bytes.t -> int -> int -> int Lwt.t
write_from oc buffer offset length
writes up to length
bytes
to oc
, from buffer
at offset offset
and returns the number
of bytes actually written
val write_from_string : output_channel -> string -> int -> int -> int Lwt.t
See Uwt_io.write
.
val write_from_exactly : output_channel -> Bytes.t -> int -> int -> unit Lwt.t
write_from_exactly oc buffer offset length
writes all length
bytes from buffer
at offset offset
to oc
val write_from_string_exactly : output_channel -> string -> int -> int -> unit Lwt.t
val write_value : output_channel -> ?flags:Marshal.extern_flags list -> 'a -> unit Lwt.t
write_value oc ?flags x
marshals the value x
to oc
These functions are basically helpers. Also you may prefer
using the name Uwt_io.printl
rather than Uwt_io.write_line
because it is
shorter.
The general name of a printing function is <prefix>print<suffixes>
,
where <prefix>
is one of:
'f'
, which means that the function takes as argument a channelUwt_io.stdout
'e'
, which means that the function prints on Uwt_io.stderr
and <suffixes>
is a combination of:
'l'
which means that a new-line character is printed after the message'f'
which means that the function takes as argument a format instead
of a stringval fprint : output_channel -> string -> unit Lwt.t
val fprintl : output_channel -> string -> unit Lwt.t
val fprintf : output_channel ->
('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'a
%!
does nothing here. To flush the channel, use Uwt_io.flush channel
.
val fprintlf : output_channel ->
('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'a
%!
does nothing here. To flush the channel, use Uwt_io.flush channel
.
val print : string -> unit Lwt.t
val printl : string -> unit Lwt.t
val printf : ('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'a
%!
does nothing here. To flush the channel, use Uwt_io.(flush stdout)
.
val printlf : ('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'a
%!
does nothing here. To flush the channel, use Uwt_io.flush channel
.
val eprint : string -> unit Lwt.t
val eprintl : string -> unit Lwt.t
val eprintf : ('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'a
%!
does nothing here. To flush the channel, use Uwt_io.(flush stderr)
.
val eprintlf : ('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'a
%!
does nothing here. To flush the channel, use Uwt_io.(flush stderr)
.
val hexdump_stream : output_channel -> char Lwt_stream.t -> unit Lwt.t
hexdump_stream oc byte_stream
produces the same output as the
command hexdump -C
.
val hexdump : output_channel -> string -> unit Lwt.t
hexdump oc str = hexdump_stream oc (Lwt_stream.of_string str)
typefile_name =
string
Type of file names
val open_file : ?buffer:Uwt_bytes.t ->
?flags:Uwt.Fs.uv_open_flag list ->
?perm:Unix.file_perm ->
mode:'a mode -> file_name -> 'a channel Lwt.t
open_file ?buffer ?flags ?perm ~mode filename
opens the file with name
filename
, and returns a channel for either reading or writing it.
Note: if opening for writing (~mode:Output
), and the file already exists,
open_file
truncates (clears) the file by default. If you would like to
keep the pre-existing contents of the file, use the ?flags
parameter to
pass a custom flags list that does not include Unix.O_TRUNC
.
Unix.Unix_error
on error.val with_file : ?buffer:Uwt_bytes.t ->
?flags:Uwt.Fs.uv_open_flag list ->
?perm:Unix.file_perm ->
mode:'a mode ->
file_name -> ('a channel -> 'b Lwt.t) -> 'b Lwt.t
with_file ?buffer ?flags ?perm ~mode filename f
opens a file and passes
the channel to f
. It is ensured that the channel is closed when f ch
resolves (even if it is rejected, or if f
raises an exception).
Note: if opening for writing (~mode:Output
), and the file already exists,
with_file
truncates (clears) the file by default. If you would like to
keep the pre-existing contents of the file, use the ?flags
parameter to
pass a custom flags list that does not include Unix.O_TRUNC
.
val open_temp_file : ?buffer:Uwt_bytes.t ->
?flags:Uwt.Fs.uv_open_flag list ->
?perm:Unix.file_perm ->
?temp_dir:string ->
?prefix:string -> unit -> (string * output_channel) Lwt.t
open_temp_file ()
starts creating a new temporary file, and evaluates to a
promise for the pair of the file's name, and an output channel for writing
to the file.
The caller should take care to delete the file later. Alternatively, see
Uwt_io.with_temp_file
.
The ?buffer
and ?perm
arguments are passed directly to an internal call
to Uwt.io.open_file
.
If not specified, ?flags
defaults to
[O_CREAT; O_EXCL; O_WRONLY]
. If specified, the specified flags
are used exactly. Note that these should typically contain at least
O_CREAT
and O_EXCL
, otherwise open_temp_file
may open an existing
file.
?temp_dir
can be used to choose the directory in which the file is
created. For the current directory, use
Filename.current_dir_name
. If not specified, the directory is taken from
Filename.get_temp_dir_name
, which is typically set to your system
temporary file directory.
?prefix
helps determine the name of the file. It will be the prefix
concatenated with a random sequence of characters. If not specified,
open_temp_file
uses some default prefix.
val with_temp_file : ?buffer:Uwt_bytes.t ->
?flags:Uwt.Fs.uv_open_flag list ->
?perm:Unix.file_perm ->
?temp_dir:string ->
?prefix:string -> (string * output_channel -> 'b Lwt.t) -> 'b Lwt.t
with_temp_file f
calls Uwt_io.open_temp_file
()
, passing all optional
arguments directly to it. It then attaches f
to run after the file is
created, passing the filename and output channel to f
. When the promise
returned by f
is resolved, with_temp_file
closes the channel and deletes
the temporary file by calling Uwt.Fs.unlink
.
val open_connection : ?in_buffer:Uwt_bytes.t ->
?out_buffer:Uwt_bytes.t ->
Unix.sockaddr -> (input_channel * output_channel) Lwt.t
open_connection ?in_buffer ?out_buffer addr
opens a connection to the
given address and returns two channels for using it.
The connection is completly closed when you close both channels.
Unix.Unix_error
on error.val with_connection : ?in_buffer:Uwt_bytes.t ->
?out_buffer:Uwt_bytes.t ->
Unix.sockaddr ->
(input_channel * output_channel -> 'a Lwt.t) -> 'a Lwt.t
with_connection ?fd ?in_buffer ?out_buffer addr f
opens a connection to
the given address and passes the channels to f
type
server
Type of a server
val establish_server_with_client_address : ?buffer_size:int ->
?backlog:int ->
?no_close:bool ->
Unix.sockaddr ->
(Unix.sockaddr -> input_channel * output_channel -> unit Lwt.t) ->
server Lwt.t
establish_server_with_client_address listen_address f
creates a server
which listens for incoming connections on listen_address
. When a client
makes a new connection, it is passed to f
: more precisely, the server
calls
f client_address (in_channel, out_channel)
where client_address
is the address (peer name) of the new client, and
in_channel
and out_channel
are two channels wrapping the socket for
communicating with that client.
The server does not block waiting for f
to complete: it concurrently tries
to accept more client connections while f
is handling the client.
When the promise returned by f
completes (i.e., f
is done handling the
client), establish_server_with_client_address
automatically closes
in_channel
and out_channel
. This is a default behavior that is useful
for simple cases, but for a robust application you should explicitly close
these channels yourself, and handle any exceptions. If the channels are
still open when f
completes, and their automatic closing raises an
exception, establish_server_with_client_address
treats it as an unhandled
exception reaching the top level of the application: it passes that
exception to Lwt.async_exception_hook
, the default behavior of which is
to print the exception and terminate your process.
Automatic closing can be completely disabled by passing ~no_close:true
.
Similarly, if f
raises an exception (or the promise it returns fails with
an exception), establish_server_with_client_address
can do nothing with
that exception, except pass it to Lwt.async_exception_hook
.
~backlog
is the argument passed to Lwt_unix.listen
.
The returned promise (a server Lwt.t
) resolves when the server has just
started listening on listen_address
: right after the internal call to
listen
, and right before the first internal call to accept
.
val establish_server : ?buffer_size:int ->
?backlog:int ->
?no_close:bool ->
Unix.sockaddr ->
(input_channel * output_channel -> unit Lwt.t) ->
server Lwt.t
Like establish_server_with_client_address
, but does not pass the client
address to the callback f
.
val shutdown_server : server -> unit Lwt.t
Shutdown the given server
val lines_of_file : file_name -> string Lwt_stream.t
lines_of_file name
returns a stream of all lines of the file
with name name
. The file is automatically closed when all
lines have been read.
val lines_to_file : file_name -> string Lwt_stream.t -> unit Lwt.t
lines_to_file name lines
writes all lines of lines
to
file with name name
.
val chars_of_file : file_name -> char Lwt_stream.t
chars_of_file name
returns a stream of all characters of the
file with name name
. As for Uwt_io.lines_of_file
the file is
closed when all characters have been read.
val chars_to_file : file_name -> char Lwt_stream.t -> unit Lwt.t
chars_to_file name chars
writes all characters of chars
to
name
module type NumberIO =sig
..end
Common interface for reading/writing integers in binary
module LE:NumberIO
Reading/writing of numbers in little-endian
module BE:NumberIO
Reading/writing of numbers in big-endian
include Uwt_io.NumberIO
Reading/writing of numbers in the system endianness.
type
byte_order =
| |
Little_endian |
|||
| |
Big_endian |
(* | Type of byte order | *) |
val system_byte_order : byte_order
val block : 'a channel -> int -> (Uwt_bytes.t -> int -> 'b Lwt.t) -> 'b Lwt.t
block ch size f
pass to f
the internal buffer and an
offset. The buffer contains size
chars at offset
. f
may
read or write these chars. size
must satisfy 0 <= size <=
16
type
direct_access = {
|
da_buffer : |
(* | The internal buffer | *) |
|
mutable da_ptr : |
(* | The pointer to:
| *) |
|
mutable da_max : |
(* | The maximum offset | *) |
|
da_perform : |
(* | - for input channels: refills the buffer and returns how many bytes have been read
| *) |
Information for directly accessing the internal buffer of a channel
val direct_access : 'a channel -> (direct_access -> 'b Lwt.t) -> 'b Lwt.t
direct_access ch f
passes to f
a Uwt_io.direct_access
structure. f
must use it and update da_ptr
to reflect how
many bytes have been read/written.
val default_buffer_size : unit -> int
Return the default size for buffers. Channels that are created without a specific buffer use new buffer of this size.
val set_default_buffer_size : int -> unit
Change the default buffer size.
Invalid_argument
if the given size is smaller than 16
or greater than Sys.max_string_length