lio 0.4.1

A platform-independent async I/O library with native support for io_uring (Linux), IOCP (Windows), and kqueue (macOS)
Documentation
#ifndef LIO_H
#define LIO_H

/* THIS FILE IS AUTO-GENERATED BY cbindgen — DO NOT EDIT MANUALLY */

#include <stddef.h>
#include <stdint.h>
#include <sys/socket.h>
#ifndef __cplusplus
typedef struct sockaddr sockaddr;
typedef struct sockaddr_storage sockaddr_storage;
#endif

/**
 * Opaque lio driver handle.  Create with [`lio_create`], destroy with
 * [`lio_destroy`].  Not thread-safe; use one handle per thread.
 */
typedef struct lio_handle_t lio_handle_t;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

/**
 * Create a new lio driver with the given operation capacity.
 *
 * Returns a non-null opaque pointer on success, or null on failure.
 * The caller owns the returned handle and must pass it to [`lio_destroy`]
 * when done.
 */
struct lio_handle_t *lio_create(unsigned int capacity);

/**
 * Destroy a lio handle created by [`lio_create`].
 *
 * After this call `lio` is invalid.
 *
 * # Safety
 * `lio` must have been returned by [`lio_create`] and must not be used
 * after this call.
 */
void lio_destroy(struct lio_handle_t *lio);

/**
 * Drive the event loop once (non-blocking).
 *
 * Returns the number of operations that completed, or -1 on error.
 *
 * # Safety
 * `lio` must be a valid handle.
 */
int lio_tick(struct lio_handle_t *lio);

/**
 * Shut down part of a full-duplex connection.
 *
 * - `fd`: Socket file descriptor
 * - `how`: `SHUT_RD`=0, `SHUT_WR`=1, `SHUT_RDWR`=2
 * - `callback(result)`: 0 on success, negative errno on error
 *
 * # Safety
 * `lio` must be a valid handle and `fd` a valid socket.
 */
void lio_shutdown(struct lio_handle_t *lio, intptr_t fd, int how, void (*callback)(int));

/**
 * Synchronize a file's in-core state with the storage device.
 *
 * - `callback(result)`: 0 on success, negative errno on error
 *
 * # Safety
 * `lio` must be a valid handle and `fd` a valid file descriptor.
 */
void lio_fsync(struct lio_handle_t *lio, intptr_t fd, void (*callback)(int));

/**
 * Truncate a file to `len` bytes.
 *
 * - `callback(result)`: 0 on success, negative errno on error
 *
 * # Safety
 * `lio` must be a valid handle and `fd` a valid file descriptor.
 */
void lio_truncate(struct lio_handle_t *lio, intptr_t fd, uint64_t len, void (*callback)(int));

/**
 * Write data to `fd` at `offset`.  Pass `offset = -1` for current position.
 *
 * Ownership of `buf` (which must have been `malloc`'d with at least `buf_len`
 * bytes) transfers to lio.  The callback receives the original pointer so the
 * caller can `free` it.
 *
 * - `callback(result, buf, len)`: bytes written (or negative errno), buffer
 *
 * # Safety
 * `lio` must be valid; `buf` must point to at least `buf_len` bytes allocated
 * with `malloc`.
 */
void lio_write_at(struct lio_handle_t *lio,
                  intptr_t fd,
                  uint8_t *buf,
                  uintptr_t buf_len,
                  int64_t offset,
                  void (*callback)(int, uint8_t*, uintptr_t));

/**
 * Read from `fd` at `offset` into `buf`.  Pass `offset = -1` for current
 * position.
 *
 * Ownership of `buf` transfers to lio (see [`lio_write_at`]).
 *
 * - `callback(result, buf, len)`: bytes read (or negative errno), buffer
 *
 * # Safety
 * Same requirements as [`lio_write_at`].
 */
void lio_read_at(struct lio_handle_t *lio,
                 intptr_t fd,
                 uint8_t *buf,
                 uintptr_t buf_len,
                 int64_t offset,
                 void (*callback)(int, uint8_t*, uintptr_t));

/**
 * Create a socket.
 *
 * - `callback(result)`: new socket fd (`intptr_t`) on success, negative errno
 *   on error
 *
 * # Safety
 * `lio` must be a valid handle.
 */
void lio_socket(struct lio_handle_t *lio,
                int domain,
                int ty,
                int proto,
                void (*callback)(intptr_t));

/**
 * Bind a socket to an address.
 *
 * - `callback(result)`: 0 on success, negative errno on error
 *
 * # Safety
 * `lio` must be valid; `sock` must point to `sock_len` bytes of a valid
 * `sockaddr`.
 */
void lio_bind(struct lio_handle_t *lio,
              intptr_t fd,
              const sockaddr *sock,
              socklen_t sock_len,
              void (*callback)(int));

/**
 * Accept a connection.
 *
 * - `callback(result, addr)`: new socket fd on success (negative errno on
 *   error); `addr` is heap-allocated `sockaddr_storage` — **caller must free
 *   it** — or null on error.
 *
 * # Safety
 * `lio` must be valid; `fd` must be a listening socket.
 */
void lio_accept(struct lio_handle_t *lio, intptr_t fd, void (*callback)(intptr_t,
                                                                        const sockaddr_storage*));

/**
 * Listen for connections on a socket.
 *
 * - `callback(result)`: 0 on success, negative errno on error
 *
 * # Safety
 * `lio` and `fd` must be valid.
 */
void lio_listen(struct lio_handle_t *lio, intptr_t fd, int backlog, void (*callback)(int));

/**
 * Send data on a connected socket.
 *
 * Ownership of `buf` transfers to lio (see [`lio_write_at`]).
 *
 * - `callback(result, buf, len)`: bytes sent (or negative errno), buffer
 *
 * # Safety
 * `lio` must be valid; `buf` must be at least `buf_len` bytes allocated with
 * `malloc`.
 */
void lio_send(struct lio_handle_t *lio,
              intptr_t fd,
              uint8_t *buf,
              uintptr_t buf_len,
              int flags,
              void (*callback)(int, uint8_t*, uintptr_t));

/**
 * Receive data from a socket.
 *
 * Ownership of `buf` transfers to lio (see [`lio_write_at`]).
 *
 * - `callback(result, buf, len)`: bytes received (or negative errno), buffer
 *
 * # Safety
 * `lio` must be valid; `buf` must be at least `buf_len` bytes allocated with
 * `malloc`.
 */
void lio_recv(struct lio_handle_t *lio,
              intptr_t fd,
              uint8_t *buf,
              uintptr_t buf_len,
              int flags,
              void (*callback)(int, uint8_t*, uintptr_t));

/**
 * Close a file descriptor.
 *
 * The fd must be a C-owned fd, not a [`Resource`] managed by Rust.
 *
 * - `callback(result)`: 0 on success, negative errno on error
 *
 * # Safety
 * `lio` must be valid; `fd` must be a valid open file descriptor.
 */
void lio_close(struct lio_handle_t *lio, intptr_t fd, void (*callback)(int));

/**
 * Wait for `millis` milliseconds.
 *
 * - `callback(result)`: 0 on success
 *
 * # Safety
 * `lio` must be a valid handle.
 */
void lio_timeout(struct lio_handle_t *lio, unsigned int millis, void (*callback)(int));

#ifdef __cplusplus
}  // extern "C"
#endif  // __cplusplus

#endif  /* LIO_H */