Skip to main content

rayforce_sys/
lib.rs

1//! Raw FFI bindings to the RayforceDB v2 core (`librayforce`).
2//!
3//! Generated by `bindgen` from the core's own headers: the public
4//! `rayforce.h`, plus the private ones declaring the handful of internal
5//! symbols the safe crate needs (`INTERNAL_FNS` in `build.rs` is the full
6//! list). This crate is `unsafe` and 1:1 with the C ABI; use the safe
7//! `rayforce` crate instead.
8//!
9//! The header's function-like macros (`ray_type`, `ray_len`, `ray_data`,
10//! `RAY_IS_ERR`, `RAY_IS_NULL`, …) are not emitted by bindgen — the safe crate
11//! reimplements them in Rust against the generated [`ray_t`] layout.
12#![allow(non_upper_case_globals)]
13#![allow(non_camel_case_types)]
14#![allow(non_snake_case)]
15#![allow(dead_code)]
16#![allow(clippy::all)]
17
18include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
19
20// Q IPC wire-format client from the shared `rayforce-q` repo (q.c/q.h). Not
21// part of the engine bindings; hand-declared here to match `q.h`.
22extern "C" {
23    /// Open a TCP connection to a Q server and perform the login handshake.
24    /// Returns a connection fd (>= 0), or a negative `Q_ERR_*` code.
25    pub fn q_connect(
26        host: *const ::std::os::raw::c_char,
27        port: ::std::os::raw::c_int,
28        user: *const ::std::os::raw::c_char,
29        password: *const ::std::os::raw::c_char,
30        timeout_ms: ::std::os::raw::c_int,
31    ) -> ::std::os::raw::c_int;
32    /// Close a connection fd. Returns 0 on success, -1 on an invalid fd.
33    pub fn q_close(fd: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
34    /// Encode + exchange + decode: send `msg` and return the decoded response
35    /// (possibly a `RAY_ERROR`), or null on a transport/serialization failure
36    /// with a short reason written to `err`.
37    pub fn q_send(
38        fd: ::std::os::raw::c_int,
39        msg: *mut ray_t,
40        err: *mut ::std::os::raw::c_char,
41        errlen: usize,
42    ) -> *mut ray_t;
43    /// Decompress (if needed) and deserialize a response *body* (wire header
44    /// already stripped) into a freshly-owned object — possibly a `RAY_ERROR`
45    /// carrying a Q server-side error. Touches the symbol table: engine thread
46    /// only. Returns null on a decode failure with a short reason in `err`.
47    pub fn q_decode(
48        resp: *mut u8,
49        resp_len: i64,
50        compressed: ::std::os::raw::c_int,
51        err: *mut ::std::os::raw::c_char,
52        errlen: usize,
53    ) -> *mut ray_t;
54    /// Serialize `msg` into a complete Q wire request (8-byte header + body).
55    /// The buffer is malloc'd by C and must be released with `libc::free`.
56    /// `q_encode` always stamps the header's message-type byte as SYNC; a
57    /// publisher retypes `req[1]` in place to send ASYNC or RESPONSE.
58    /// Touches the symbol table: engine thread only.
59    pub fn q_encode(
60        msg: *mut ray_t,
61        req: *mut *mut u8,
62        req_len: *mut i64,
63        err: *mut ::std::os::raw::c_char,
64        errlen: usize,
65    ) -> ::std::os::raw::c_int;
66    /// Send a pre-encoded request on `fd` and read the raw response *body*.
67    /// Pure socket I/O — the half of `q_send` that a runtime with a global lock
68    /// can perform with that lock released.
69    pub fn q_exchange(
70        fd: ::std::os::raw::c_int,
71        req: *const u8,
72        req_len: i64,
73        resp: *mut *mut u8,
74        resp_len: *mut i64,
75        compressed: *mut ::std::os::raw::c_int,
76        err: *mut ::std::os::raw::c_char,
77        errlen: usize,
78    ) -> ::std::os::raw::c_int;
79}
80
81// Q wire-format *server* from `rayforce-q` (q_server.c/q_server.h), available
82// since rayforce-q 2.1.0. Where the client above lets you call a Q server, this
83// lets you receive from one.
84//
85// `q_send` is request/response over a blocking socket, so a frame the peer
86// pushes unsolicited would be read as the answer to the next call — which makes
87// a subscription impossible with the client alone. Attaching the fd to a
88// rayforce poll fixes that: the event loop owns the reads and routes each frame
89// by message type, waking a parked `q_conn_send` on a RESPONSE and dispatching
90// anything else to the function the publisher named.
91extern "C" {
92    /// Register a Q-protocol *listener* on `poll`, bound to TCP `port`.
93    /// Returns the poll selector id (>= 0), or -1 on failure.
94    pub fn q_serve(poll: *mut ray_poll_t, port: ::std::os::raw::c_int) -> i64;
95    /// Put an already-connected, already-handshaken `q_connect` fd under
96    /// `poll`'s rx machine. Returns a poll selector id (>= 0) — the handle for
97    /// the two calls below — or -1. **The fd is owned by the poll from here on;
98    /// do not `q_close` it.**
99    pub fn q_conn_attach(poll: *mut ray_poll_t, fd: ::std::os::raw::c_int) -> i64;
100    /// Sync round-trip on an attached connection: write a SYNC frame, then pump
101    /// this connection until its RESPONSE arrives — dispatching, not swallowing,
102    /// any frame that arrives in between. So a bound handler can fire before
103    /// this returns. Returns a freshly-owned object, possibly a `RAY_ERROR`.
104    /// Refuses a nested call from inside a handler.
105    pub fn q_conn_send(poll: *mut ray_poll_t, id: i64, msg: *mut ray_t) -> *mut ray_t;
106    /// Close an attached connection and release its state. Illegal once the rx
107    /// machine has already deregistered the selector — check `ray_poll_get`.
108    pub fn q_conn_close(poll: *mut ray_poll_t, id: i64);
109}
110
111/// `q_connect` failure codes (mirrors `q.h`).
112pub const Q_ERR_SOCKET: ::std::os::raw::c_int = -1;
113pub const Q_ERR_HANDSHAKE: ::std::os::raw::c_int = -2;
114pub const Q_ERR_TIMEOUT: ::std::os::raw::c_int = -3;
115
116/// `ray_ipc_connect` failure codes.
117///
118/// The public header declares the function with no contract at all
119/// (`include/rayforce.h`); this mirrors the comment on the private
120/// `src/core/ipc.h` declaration and, for [`RAY_IPC_ERR_OS`], the
121/// `connect_fail_code()` classifier in `src/core/ipc.c` that the header's
122/// comment predates. Hand-maintained against a core bump, like `Q_ERR_*`.
123///
124/// [`RAY_IPC_ERR_REFUSED`] is the documented name for -1, but the core also
125/// returns it for a missing poll, a credential buffer overflow and a failed
126/// poll registration — treat it as the catch-all it is.
127pub const RAY_IPC_ERR_REFUSED: i64 = -1;
128/// The server demands credentials and the caller supplied no password.
129pub const RAY_IPC_ERR_AUTH_REQUIRED: i64 = -2;
130/// The server rejected the credentials.
131pub const RAY_IPC_ERR_AUTH_FAILED: i64 = -3;
132/// The peer speaks a different serialization wire version.
133pub const RAY_IPC_ERR_WIRE_VERSION: i64 = -4;
134/// No answer within the connect/handshake budget. The core folds `EAGAIN` and
135/// `EWOULDBLOCK` in here too, so this also covers a live server that is busy
136/// inside a long evaluation.
137pub const RAY_IPC_ERR_TIMEOUT: i64 = -5;
138/// Some other OS error; the core leaves `errno` holding it.
139pub const RAY_IPC_ERR_OS: i64 = -6;
140
141/// Function attribute flags for `ray_fn_unary` / `ray_fn_vary`, from the core's
142/// `src/lang/eval.h:33-40`. They are `#define`s in a header `wrapper.h` does
143/// not include, so bindgen never sees them.
144///
145/// `RAY_FN_NONE` is what a plain callback wants: no auto-mapping over vectors,
146/// no aggregation, args evaluated normally.
147pub const RAY_FN_NONE: u8 = 0x00;
148pub const RAY_FN_LEFT_ATOMIC: u8 = 0x01;
149pub const RAY_FN_RIGHT_ATOMIC: u8 = 0x02;
150pub const RAY_FN_ATOMIC: u8 = 0x04;
151pub const RAY_FN_AGGR: u8 = 0x08;
152pub const RAY_FN_SPECIAL_FORM: u8 = 0x10;
153pub const RAY_FN_RESTRICTED: u8 = 0x20;
154pub const RAY_FN_LAZY_AWARE: u8 = 0x80;