Handles are closed automatically, if they are not longer referenced from the OCaml heap. Nevertheless, you should nearly always close them with
- if they wrap a file descriptor, you will sooner or later run out of file descriptors. The OCaml garbage collector doesn't give any guarantee, when orphaned memory blocks are removed.
- you might have registered some repeatedly called action (e.g. timeout, read_start,...), that prevent that all references get removed from the OCaml heap.
However, it's safe to write code in this manner:
let s = Uwt.Tcp.init () in let c = Uwt.Tcp.init () in Uwt.Tcp.nodelay s false; Uwt.Tcp.simultaneous_accepts true; if foobar () then (* no file descriptor yet assigned, no need to worry about exceptions inside foobar,... *) Lwt.return_unit (* no need to close *) else ...
If you want - for whatever reason - keep a file descriptor open for the whole lifetime of your process, remember to keep a reference to its handle.
val close_noerr : t -> unit
close_wait is only useful, if you intend to wait until all concurrent write and read threads related to this handle are canceled.
val is_active : t -> bool
Returns non-zero if the handle is active, zero if it's inactive. What "active" means depends on the type of handle:
Async.thandle is always active and cannot be deactivated, except by closing it with uv_close().
Udp.t, etc. handle - basically any handle that deals with i/o - is active when it is doing something that involves i/o, like reading, writing, connecting, accepting new connections, etc.
Rule of thumb: if a handle of type
Uwt.Foo.t has a uv_foo_start() function, then it's active from the moment that function is called. Likewise, uv_foo_stop() deactivates the handle again.
val ref' : t -> unit
Reference the given handle. References are idempotent, that is, if a handle is already referenced calling this function again will have no effect.
val unref : t -> unit
Un-reference the given handle. References are idempotent, that is, if a handle is not referenced calling this function again will have no effect.
val has_ref : t -> bool
Returns non-zero if the handle is referenced, zero otherwise.
val is_readable : t -> bool
val is_writable : t -> bool
Read data from an incoming stream. The
~cb will be made several times until there is no more data to read or
read_stop is called.
val read_stop_exn : t -> unit
There is currently no
uv_read function in libuv, just
uv_read_stop. This is a wrapper for your convenience. It calls read_stop internally, if you don't continue with reading immediately. Zero result indicates EOF.
In future libuv versions, there might be
uv_try_read functions (it was discussed several times). If these changes got merged,
Stream.read will wrap them - even if there will be small semantic differences.
It is currently not possible to start several read threads in parallel, you must serialize the requests manually. In the following example
t2 will fail with EBUSY:
let t1 = Uwt.Stream.read t ~buf:buf1 in let t2 = Uwt.Stream.read t ~buf:buf2 in (* ... *)
Calling the function with
~len:0 has a dubious, system dependent semantic.
val write_queue_size : t -> int
Returns the amount of queued bytes waiting to be sent
Write data to stream, but won't queue a write request if it can't be completed immediately.
write is eager - like the counterparts inside
Lwt_unix. It first calls
try_write internally to check if it can return immediately (without the overhead of creating a sleeping thread and waking it up later). If it can't write everything instantly, it will call
write_raw is exposed here mainly in order to write unit tests for it. But you can also use it, if you your
~buf is very large or you know for another reason, that try_write will fail.
Windows doesn't support writing multiple buffers with a single syscall for some HANDLEs (e.g. it's supported for tcp handles, but not pipes). uwt then writes the buffers one by one
If the number of buffers is greater than IOV_MAX, libuv already contains the necessary workarounds
See comment to
try_writev! This function will fail with
Unix.EOPNOTSUPP on Windows for e.g. pipe handles
writev, but if passing several buffers at once is not supported by the OS, the buffers will be written one by one. Please note that as a consequence you should not start several
writev_emul threads in parallel. The writing order would be surprising in this case. If you don't use windows, this function is identic to
Start listening for incoming connections.
~max indicates the number of connections the kernel might queue, same as
listen(2). When a new incoming connection is received
~cb is called.
Shutdown the outgoing (write) side of a duplex stream. It waits for pending write requests to complete.