Legend:
Library
Module
Module type
Parameter
Class
Class type
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.
exceptionChannel_closedof string
Exception raised when a channel is closed. The parameter is a description of the channel.
make ?buffer ?close ~mode perform_io is the main function for creating new channels.
parameterbuffer
user-supplied buffer. When this argument is present, its value will be used as the buffer for the created channel. The size of buffer must conform to the limitations described in 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 a buffer argument, sharing the same semantics.
parameterclose
close function of the channel. It defaults to Lwt.return
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 atomic.
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.
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.
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.
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 terminates (even if it fails).
open_connection ?fd ?in_buffer ?out_buffer addr opens a connection to the given address and returns two channels for using it. If fd is not specified, a fresh one will be used.
The connection is completly closed when you close both channels.
establish_server ?fd ?buffer_size ?backlog sockaddr f creates a server which listens for incoming connections. New connections are passed to f.
establish_server does not start separate threads for running f, nor close the connections passed to f. Thus, the skeleton of a practical server based on establish_server might look like this:
Closes the given server's listening socket. This function does not wait for the close operation to actually complete. It does not affect the sockets of connections that have already been accepted, i.e. passed to f by establish_server.
deprecated
Will be replaced by Versioned.shutdown_server_2, which evaluates to a thread that waits for close to complete.
chars_of_file name returns a stream of all characters of the file with name name. As for lines_of_file the file is closed when all characters have been read.
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