59
59
* process data, but is in non-blocking mode, both methods now return the
60
60
* value 0. In previous releases of ocamlnet, the behaviour was not
63
* {b Ocamlnet-3.0} changed the behavior of [close_out]. Errors are no longer
64
* reported - instead, the exception is logged to {!Netlog}. For a stricter
65
* error handling, it is suggested to call [flush] first. Also, [close_in]
66
* and [close_out] no longer raise [Closed_channel] when the channel is
67
* already closed. Read more about this in the section
68
* {!Netchannels.rec_out_channel.close_error}.
64
71
exception Closed_channel
110
117
method close_in : unit -> unit
111
118
(** Closes the channel for input.
113
* When the channel is already closed, the exception [Closed_channel] will
114
* be raised if an ocamlnet implementation is used. For implementations
115
* of other libraries there is no standard for this case.
120
* When the channel is already closed, this is a no-op.
122
* Error policy: Exceptions are only raised in cases of serious
123
* corruption, e.g. if the underlying descriptor is invalid.
161
169
method flush : unit -> unit
162
170
(** If there is a write buffer, it will be flushed. Otherwise, nothing
165
173
method close_out : unit -> unit
166
174
(** Flushes the buffer, if any, and closes the channel for output.
168
* When the channel is already closed, the exception [Closed_channel] will
169
* be raised if an ocamlnet implementation is used. For implementations
170
* of other libraries there is no standard for this case.
176
* When the channel is already closed, this is a no-op.
179
(** {2:close_error How to close channels in case of errors}
181
The [close_out] method has actually two tasks: First, it writes out
182
all remaining data (like [flush]), and second, it releases OS
183
resources (e.g. closes file descriptors). There is the question
184
what has to happen when the write part fails - is the resource released
187
We choose here a pragmatic approach under the assumption that
188
an OS error at close time is usually unrecoverable, and it is
189
more important to release the OS resource. Also, we
190
assume that the user is wise enough to call [flush] first if
191
it is essential to know write errors at close time. Under these
194
- The [flush] method fully reports any errors when writing out
196
- When [flush] raises an error exception, it should discard
197
any data in the buffer. This is not obligatory, however,
198
but considered good practice, and is subject to discussion.
199
- The [close_out] method usually does not report errors by
200
raising exceptions, but only by logging them via {!Netlog}.
201
The OS resource is released in any case. As before, this
202
behavior is not obligatory, but considered as good practice,
203
and subject to discussion.
205
This ensures that the following code snippet reports all errors, but also
206
releases OS resources:
213
ch # close_out(); raise error
216
There are some cases where data can be first written when it is
217
known that the channel is closed. These data would not be written
218
by a preceding [flush]. In such cases:
220
- The best way to deal with it is to define another method,
221
e.g. called [write_eof], that marks the data as logically
222
being complete, so a following [flush] can do the complete
223
shutdown cycle of the channel.
224
- At least, however, one should allow then that a double
225
[close_out] releases the descriptor: the first [close_out]
226
will report the error condition as exception, but discard
227
all data in the channel. The second [close_out] finally
228
releases the OS resource.
230
In any way, hard errors indicating bugs of the program logic
231
(like invalid file descriptors) should always be immediately
174
236
(** Basic Unix-level class type for output channels as used by ocamlnet. In addition
424
499
* When [close_out] is invoked, the subprocess is [wait]ed for. If the
425
500
* process exits with code 0, the method returns normally. Otherwise,
426
* the exception [Command_failure] is raised.
501
* the exception [Command_failure] is raised. (The channel is closed
502
* even if this exception is raised.)
428
504
* @param onclose this function is called when the [close_out] method is
429
505
* invoked, just after the underlying descriptor has been closed.
762
844
* The [pos_in] and [pos_out] methods returns logical positions.
846
* This class supports sockets and Win32 named pipes. Note, however,
847
* that for Win32 named pipes it is not possible to shut down only one
848
* direction of the bidirectional data channel.
764
850
* @param blocking See {!input_descr} and {!output_descr}
765
851
* @param start_pos_in The position to which [pos_in] is initialized when
766
852
* the channel is created, by default 0
796
882
* the triple (name, inch, outch) containing the file [name],
797
883
* the file opened as in_channel [inch] and as out_channel [outch].
799
* @param tmp_directory By default the current directory
800
* @param tmp_prefix By default ["netstring"]. It is better to have a prefix
801
* that is likely to be unique, e.g. the process ID, or the current time.
885
* @param tmp_directory Defaults to {!Netsys_tmp.tmp_directory()}
886
* @param tmp_prefix By default ["netstring"]. This needs not to be
887
* unique, but just descriptive.
802
888
* @param mode The creation mask of the file; defaults to 0o600, i.e. the
803
889
* file is private for the current user
804
890
* @param limit Limits the number of trials to find the unique suffix.