module Lwt_io:sig..end
An output channel is a channel that can be used to send data and an input channel is a channel that can used to receive data.
If you are familiar with buffered channels you may be familiar too with the flush operation. Note that byte channles of this modules are automatically flushed when there is nothing else to do (i.e. before the program goes into idle), so this means that you no longer have to write:
eprintf "log message\n";
flush stderr;
to have you 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
type 'mode channel
type input
type output
type 'a mode =
| |
Input : |
| |
Output : |
val input : input modeinput input mode representationval output : output modeoutput output mode representationtypeinput_channel =input channel
typeoutput_channel =output channel
val mode : 'a channel -> 'a modemode ch returns the mode of a channelval stdin : input_channelLwt_unix.stdinval stdout : output_channelLwt_unix.stdoutval stderr : output_channelLwt_unix.stderrval zero : input_channel'\x00'val null : output_channelval pipe : ?buffer_size:int -> unit -> input_channel * output_channelpipe ?buffer_size () creates a pipe using Lwt_unix.pipe and
makes two channels from the two returned file descriptorsval make : ?buffer_size:int ->
?close:(unit -> unit Lwt.t) ->
?seek:(int64 -> Unix.seek_command -> int64 Lwt.t) ->
mode:'mode mode ->
(Lwt_bytes.t -> int -> int -> int Lwt.t) -> 'mode channelmake ?buffer_size ?close ~mode perform_io is the
main function for creating new channels.buffer_size : size of the internal buffer. It must be
between 16 and Sys.max_string_lengthclose : close function of the channel. It defaults to
Lwt.returnseek : same meaning as Unix.lseekval of_bytes : mode:'mode mode -> Lwt_bytes.t -> 'mode channelval of_fd : ?buffer_size:int ->
?close:(unit -> unit Lwt.t) ->
mode:'mode mode -> Lwt_unix.file_descr -> 'mode channelof_fd ?buffer_size ?close ~mode fd creates a channel from a
file descriptor.close : defaults to closing the file descriptor.val of_unix_fd : ?buffer_size:int ->
?close:(unit -> unit Lwt.t) ->
mode:'mode mode -> Unix.file_descr -> 'mode channelof_unix_fd ?buffer_size ?close ~mode fd is a short-hand for:
of_fd ?buffer_size ?close (Lwt_unix.of_unix_file_descr fd)
val close : 'a channel -> unit Lwt.tclose ch closes the given channel. If ch is an output
channel, it performs all pending actions, flush it and close
it. If ch is an input channel, it just close it immediatly.
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 channel obtained with an
Lwt_io.atomic.
val abort : 'a channel -> unit Lwt.tabort ch abort current operations and close the channel
immediatly.val atomic : ('a channel -> 'b Lwt.t) -> 'a channel -> 'b Lwt.tatomic 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 atomicval file_length : string -> int64 Lwt.tval buffered : 'a channel -> intbuffered oc returns the number of bytes in the bufferval flush : output_channel -> unit Lwt.tflush oc performs all pending writes on ocval flush_all : unit -> unit Lwt.tflush_all () flushes all open output channelsval buffer_size : 'a channel -> intval resize_buffer : 'a channel -> int -> unit Lwt.tval is_busy : 'a channel -> boolis_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 -> int64position ch Returns the current position in the channel.val set_position : 'a channel -> int64 -> unit Lwt.tset_position ch pos Sets the position in the output channel. This
does not work if the channel do not support random access.val length : 'a channel -> int64 Lwt.tLwt_io.read_chars and
Lwt_io.read_lines) all functions are atomic.val read_char : input_channel -> char Lwt.tread_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.tread_byte but does not raises End_of_file on end of
inputval read_chars : input_channel -> char Lwt_stream.tread_chars ic returns a stream holding all characters of
icval read_line : input_channel -> string Lwt.tread_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
val read_lines : input_channel -> string Lwt_stream.tread_lines ic returns a stream holding all lines of icval read : ?count:int -> input_channel -> string Lwt.tread ?count ic reads at most len 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 -> string -> int -> int -> int Lwt.tread_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 -> string -> int -> int -> unit Lwt.tread_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.tread_value ic reads a marshaled value from icLwt_io.write_chars and Lwt_io.write_lines are atomic.
For example if you use Lwt_io.write_line in to different threads, the
two operations will be serialized, and lines cannot be mixed.
val write_char : output_channel -> char -> unit Lwt.twrite_char oc char writes char on ocval write_chars : output_channel -> char Lwt_stream.t -> unit Lwt.twrite_chars oc chars writes all characters of chars on
ocval write : output_channel -> string -> unit Lwt.twrite oc str writes all characters of str on ocval write_line : output_channel -> string -> unit Lwt.twrite_line oc str writes str on oc followed by a
new-line.val write_lines : output_channel -> string Lwt_stream.t -> unit Lwt.twrite_lines oc lines writes all lines of lines to ocval write_from : output_channel -> string -> int -> int -> int Lwt.twrite_from oc buffer offset length writes up to length bytes
to oc, from buffer at offset offset and returns the number
of bytes actually writtenval write_from_exactly : output_channel -> string -> int -> int -> unit Lwt.twrite_from_exactly oc buffer offset length writes all length
bytes from buffer at offset offset to ocval write_value : output_channel -> ?flags:Marshal.extern_flags list -> 'a -> unit Lwt.twrite_value oc ?flags x marshals the value x to ocLwt_io.printl rather than Lwt_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 channelLwt_io.stdout'e', which means that the function prints on Lwt_io.stderr<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.tval fprintl : output_channel -> string -> unit Lwt.tval fprintf : output_channel ->
('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'aval fprintlf : output_channel ->
('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'aval print : string -> unit Lwt.tval printl : string -> unit Lwt.tval printf : ('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'aval printlf : ('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'aval eprint : string -> unit Lwt.tval eprintl : string -> unit Lwt.tval eprintf : ('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'aval eprintlf : ('a, unit, string, unit Lwt.t) Pervasives.format4 -> 'aval hexdump_stream : output_channel -> char Lwt_stream.t -> unit Lwt.thexdump_stream oc byte_stream produces the same output as the
command hexdump -C.val hexdump : output_channel -> string -> unit Lwt.thexdump oc str = hexdump_stream oc (Lwt_stream.of_string str)typefile_name =string
val open_file : ?buffer_size:int ->
?flags:Unix.open_flag list ->
?perm:Unix.file_perm ->
mode:'a mode -> file_name -> 'a channel Lwt.topen_file ?buffer_size ?flags ?perm ~mode filename open the
file with name filename and returns a channel for
reading/writing it.Unix.Unix_error on error.val with_file : ?buffer_size:int ->
?flags:Unix.open_flag list ->
?perm:Unix.file_perm ->
mode:'a mode ->
file_name -> ('a channel -> 'b Lwt.t) -> 'b Lwt.twith_file ?buffer_size ?flags ?perm ~mode filename f open a
file and passes the channel to f. It is ensured that the
channel is closed when f ch terminates (even if it fails).val open_connection : ?buffer_size:int ->
Unix.sockaddr -> (input_channel * output_channel) Lwt.topen_connection ?buffer_size ~mode addr open a connection to
the given address and returns two channels for using it.
The connection is completly closed when you close both
channels.
Raises Unix.Unix_error on error.
val with_connection : ?buffer_size:int ->
Unix.sockaddr ->
(input_channel * output_channel -> 'a Lwt.t) -> 'a Lwt.twith_connection ?buffer_size ~mode addr f open a connection to
the given address and passes the channels to ftype server
val establish_server : ?buffer_size:int ->
?backlog:int ->
Unix.sockaddr ->
(input_channel * output_channel -> unit) -> serverestablich_server ?buffer_size ?backlog sockaddr f creates a
server which will listen for incomming connections. New
connections are passed to f. Note that f must not raise any
exception.
backlog is the argument passed to Lwt_unix.listen
val shutdown_server : server -> unitval lines_of_file : file_name -> string Lwt_stream.tlines_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.tlines_to_file name lines writes all lines of lines to
filesval chars_of_file : file_name -> char Lwt_stream.tchars_of_file name returns a stream of all characters of the
file with name name. As for Lwt_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.tchars_to_file name chars writes all characters of chars to
namemodule type NumberIO =sig..end
module LE:NumberIO
module BE:NumberIO
include Lwt_io.NumberIO
typebyte_order =Lwt_sys.byte_order=
| |
Little_endian |
|||
| |
Big_endian |
(* | Type of byte order | *) |
val system_byte_order : byte_orderLwt_sys.byte_order.val block : 'a channel -> int -> (Lwt_bytes.t -> int -> 'b Lwt.t) -> 'b Lwt.tblock ch size f pass to f the internal buffer and an
offset. The buffer contains size chars at offset. f may
reads or writes these chars. size must verify 0 <= size <=
16type direct_access = {
|
da_buffer : |
(* | The internal buffer | *) |
|
mutable da_ptr : |
(* | The pointer to:
| *) |
|
mutable da_max : |
(* | The maximum offset | *) |
|
da_perform : |
(* | - for input channels:
refill the buffer and returns how many bytes have been read
| *) |
val direct_access : 'a channel -> (direct_access -> 'b Lwt.t) -> 'b Lwt.tdirect_access ch f pass to f a Lwt_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 -> intval set_default_buffer_size : int -> unitInvalid_argument if the given size is smaller than 16
or greater than Sys.max_string_length