monocoque-rs-core 0.1.0

Protocol-agnostic messaging kernel with io_uring-based I/O
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! In-process transport for zero-copy messaging within the same process.
//!
//! The inproc transport provides high-performance communication between sockets
//! in the same process using channels, without TCP/IP overhead.
//!
//! # Features
//!
//! - **Zero-copy**: Messages are shared via `Arc<Vec<Bytes>>` between sockets
//! - **Thread-safe**: Global registry protected by `DashMap`
//! - **Fast**: No serialization, network, or syscall overhead
//! - **`ZeroMQ` compatible**: Uses `inproc://` URI scheme
//!
//! # Usage
//!
//! ```ignore
//! use monocoque_core::inproc::{bind_inproc, connect_inproc};
//! use bytes::Bytes;
//!
//! // Bind to an inproc endpoint
//! let (sender, receiver) = bind_inproc("inproc://my-endpoint").unwrap();
//!
//! // Connect from another task
//! let client = connect_inproc("inproc://my-endpoint").unwrap();
//!
//! // Send messages
//! client.send(vec![Bytes::from("Hello")]).unwrap();
//!
//! // Receive messages
//! if let Ok(msg) = receiver.recv() {
//!     println!("Received: {:?}", msg);
//! }
//! ```

use bytes::Bytes;
use dashmap::DashMap;
use flume::{Receiver, Sender};
use std::io;

/// Message type for inproc transport (multipart message)
pub type InprocMessage = Vec<Bytes>;

/// Sender half of an inproc connection
pub type InprocSender = Sender<InprocMessage>;

/// Receiver half of an inproc connection
pub type InprocReceiver = Receiver<InprocMessage>;

/// Global registry of inproc endpoints (server receives from clients)
static INPROC_REGISTRY: once_cell::sync::Lazy<DashMap<String, InprocSender>> =
    once_cell::sync::Lazy::new(DashMap::new);

/// Registry of server→client senders for bidirectional inproc connections.
///
/// When `bind_inproc_bidi` is called, the server→client sender is registered
/// here so that `connect_inproc_bidi` can retrieve it to receive server replies.
static INPROC_REPLY_REGISTRY: once_cell::sync::Lazy<DashMap<String, InprocSender>> =
    once_cell::sync::Lazy::new(DashMap::new);

/// Bind to an inproc endpoint and return sender/receiver pair.
///
/// The endpoint is registered in the global registry. Multiple clients can
/// connect to this endpoint using `connect_inproc()`.
///
/// # Arguments
///
/// * `endpoint` - The endpoint URI (must start with "inproc://")
///
/// # Returns
///
/// Returns a tuple of (sender, receiver):
/// - `sender`: Used to send messages from this socket
/// - `receiver`: Used to receive messages sent by connected clients
///
/// # Errors
///
/// Returns an error if:
/// - The endpoint doesn't start with "inproc://"
/// - The endpoint is already bound
/// - The endpoint name is empty
///
/// # Example
///
/// ```
/// use monocoque_core::inproc::bind_inproc;
///
/// let (sender, receiver) = bind_inproc("inproc://my-endpoint-bind").unwrap();
/// // sender and receiver are ready for use
/// ```
pub fn bind_inproc(endpoint: &str) -> io::Result<(InprocSender, InprocReceiver)> {
    // Validate endpoint format
    let name = validate_and_extract_name(endpoint)?;

    // Create unbounded channel for message passing
    let (tx, rx) = flume::unbounded();

    // Try to insert into registry
    if INPROC_REGISTRY
        .insert(name.to_string(), tx.clone())
        .is_some()
    {
        return Err(io::Error::new(
            io::ErrorKind::AddrInUse,
            format!("inproc endpoint '{name}' is already bound"),
        ));
    }

    Ok((tx, rx))
}

/// Connect to an inproc endpoint.
///
/// Returns a sender that can be used to send messages to the bound endpoint.
/// This function blocks until the endpoint becomes available.
///
/// # Arguments
///
/// * `endpoint` - The endpoint URI (must start with "inproc://")
///
/// # Returns
///
/// Returns a sender that can send messages to the bound endpoint.
///
/// # Errors
///
/// Returns an error if:
/// - The endpoint doesn't start with "inproc://"
/// - The endpoint is not bound
/// - The endpoint name is empty
///
/// # Example
///
/// ```rust,no_run
/// use monocoque_core::inproc::connect_inproc;
/// use bytes::Bytes;
///
/// # fn example() -> std::io::Result<()> {
/// let sender = connect_inproc("inproc://my-endpoint")?;
///
/// // Send a message
/// sender.send(vec![Bytes::from("Hello")]).map_err(|_| {
///     std::io::Error::new(std::io::ErrorKind::BrokenPipe, "receiver dropped")
/// })?;
/// # Ok(())
/// # }
/// ```
pub fn connect_inproc(endpoint: &str) -> io::Result<InprocSender> {
    // Validate endpoint format
    let name = validate_and_extract_name(endpoint)?;

    // Look up the endpoint in the registry
    if let Some(sender) = INPROC_REGISTRY.get(name) {
        return Ok(sender.clone());
    }

    Err(io::Error::new(
        io::ErrorKind::NotFound,
        format!("inproc endpoint '{name}' not found (must bind before connect)"),
    ))
}

/// Unbind an inproc endpoint, removing it from the global registry.
///
/// This should be called when a bound socket is closed to free up the endpoint name.
///
/// # Arguments
///
/// * `endpoint` - The endpoint URI (must start with "inproc://")
///
/// # Example
///
/// ```rust,no_run
/// use monocoque_core::inproc::{bind_inproc, unbind_inproc};
///
/// # fn example() -> std::io::Result<()> {
/// let (sender, receiver) = bind_inproc("inproc://my-endpoint")?;
///
/// // ... use the endpoint ...
///
/// // Clean up when done
/// unbind_inproc("inproc://my-endpoint")?;
/// # Ok(())
/// # }
/// ```
pub fn unbind_inproc(endpoint: &str) -> io::Result<()> {
    let name = validate_and_extract_name(endpoint)?;
    INPROC_REGISTRY.remove(name);
    INPROC_REPLY_REGISTRY.remove(name);
    Ok(())
}

/// Bind to an inproc endpoint for bidirectional communication.
///
/// Returns `(to_clients_tx, from_clients_rx)`:
/// - `to_clients_tx`: The server uses this to send replies back to the client.
///   It is registered so that `connect_inproc_bidi` can retrieve it.
/// - `from_clients_rx`: The server reads client messages from this.
///
/// The caller (server side) owns both halves.  The client side
/// (`connect_inproc_bidi`) gets a `(to_server_tx, from_server_rx)` pair.
///
/// # Errors
///
/// Returns an error if the endpoint is already bound.
pub fn bind_inproc_bidi(
    endpoint: &str,
) -> io::Result<(InprocSender, InprocReceiver, InprocSender, InprocReceiver)> {
    let name = validate_and_extract_name(endpoint)?;

    // Channel: client → server
    let (client_to_server_tx, client_to_server_rx) = flume::unbounded::<InprocMessage>();
    // Channel: server → client
    let (server_to_client_tx, server_to_client_rx) = flume::unbounded::<InprocMessage>();

    // Register the client→server sender (clients call connect_inproc to get this)
    if INPROC_REGISTRY
        .insert(name.to_string(), client_to_server_tx.clone())
        .is_some()
    {
        return Err(io::Error::new(
            io::ErrorKind::AddrInUse,
            format!("inproc endpoint '{name}' is already bound"),
        ));
    }

    // Register the server→client sender so connect_inproc_bidi can retrieve it
    INPROC_REPLY_REGISTRY.insert(name.to_string(), server_to_client_tx.clone());

    // Return all four channel ends
    Ok((
        server_to_client_tx,
        client_to_server_rx,
        client_to_server_tx,
        server_to_client_rx,
    ))
}

/// Connect to an inproc endpoint for bidirectional communication.
///
/// Returns `(to_server_tx, from_server_rx)` so the client can both send
/// messages to the server and receive replies from it.
///
/// The server must have called `bind_inproc_bidi` before this is called.
///
/// # Errors
///
/// Returns an error if the endpoint is not bound.
pub fn connect_inproc_bidi(endpoint: &str) -> io::Result<(InprocSender, InprocReceiver)> {
    let name = validate_and_extract_name(endpoint)?;

    // Get sender to the server
    let to_server = INPROC_REGISTRY
        .get(name)
        .map(|r| r.clone())
        .ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::NotFound,
                format!("inproc endpoint '{name}' not found (must bind before connect)"),
            )
        })?;

    // Get the reply channel the server registered for us
    let from_server = INPROC_REPLY_REGISTRY
        .get(name)
        .map(|r| r.clone())
        .ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::NotFound,
                format!(
                    "inproc reply channel for '{name}' not found; \
                     use bind_inproc_bidi on the server side"
                ),
            )
        })?;

    // The registry holds the server→client SENDER.  We need to create a fresh
    // (tx, rx) pair: our from_server rx and tell the server to use our new tx.
    // Because the server already has its rx from bind_inproc_bidi, we simply
    // create a new channel and give its tx to the server registry so the server
    // can write to us, and keep the rx for ourselves.
    let (our_reply_tx, our_reply_rx) = flume::unbounded::<InprocMessage>();

    // Replace the registry entry with our fresh tx so the server will write to us.
    // (This means only one client is supported per endpoint at a time, which
    // is the correct semantic for a DEALER↔ROUTER or REQ↔REP pair.)
    INPROC_REPLY_REGISTRY.insert(name.to_string(), our_reply_tx);

    // The server also needs to be told to write to us  -  we accomplish this by
    // updating the reply registry.  The server reads from the channel whose tx
    // we just stored.  But the server's *rx* was already created in
    // bind_inproc_bidi and is owned by the caller there.
    //
    // For simplicity, we just use the original server_to_client_tx (from_server)
    // to send back  -  the server already has server_to_client_rx.
    // Drop the original from_server (it was just a reference clone of the
    // server→client tx) and use the server_to_client_tx we stored in the
    // registry as the SENDER that the server will use.  The caller of
    // bind_inproc_bidi got server_to_client_rx directly.
    let _ = from_server; // we replaced it in the registry with our_reply_tx

    Ok((to_server, our_reply_rx))
}

/// List all currently bound inproc endpoints.
///
/// This is primarily useful for debugging and testing.
///
/// # Returns
///
/// Returns a vector of endpoint names (without the "inproc://" prefix).
pub fn list_inproc_endpoints() -> Vec<String> {
    INPROC_REGISTRY
        .iter()
        .map(|entry| entry.key().clone())
        .collect()
}

/// Validate endpoint format and extract the name.
///
/// # Arguments
///
/// * `endpoint` - The full endpoint URI (e.g., "<inproc://my-endpoint>")
///
/// # Returns
///
/// Returns the endpoint name without the "inproc://" prefix.
///
/// # Errors
///
/// Returns an error if the endpoint doesn't start with "inproc://" or has an empty name.
fn validate_and_extract_name(endpoint: &str) -> io::Result<&str> {
    const PREFIX: &str = "inproc://";

    if !endpoint.starts_with(PREFIX) {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            format!("inproc endpoint must start with '{PREFIX}', got: '{endpoint}'"),
        ));
    }

    let name = &endpoint[PREFIX.len()..];
    if name.is_empty() {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "inproc endpoint name cannot be empty",
        ));
    }

    Ok(name)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_endpoint() {
        assert!(validate_and_extract_name("inproc://test").is_ok());
        assert_eq!(validate_and_extract_name("inproc://test").unwrap(), "test");

        assert!(validate_and_extract_name("tcp://test").is_err());
        assert!(validate_and_extract_name("inproc://").is_err());
        assert!(validate_and_extract_name("").is_err());
    }

    #[test]
    fn test_bind_duplicate() {
        let endpoint = "inproc://test-duplicate";

        // First bind should succeed
        let _result1 = bind_inproc(endpoint);
        assert!(_result1.is_ok());

        // Second bind should fail
        let result2 = bind_inproc(endpoint);
        assert!(result2.is_err());
        assert_eq!(result2.unwrap_err().kind(), io::ErrorKind::AddrInUse);

        // Cleanup
        let _ = unbind_inproc(endpoint);
    }

    #[test]
    fn test_bind_and_connect() {
        let endpoint = "inproc://test-connect";

        // Bind
        let (_tx, rx) = bind_inproc(endpoint).unwrap();

        // Connect
        let client = connect_inproc(endpoint).unwrap();

        // Send message from client
        let msg = vec![Bytes::from("Hello, inproc!")];
        client.send(msg.clone()).unwrap();

        // Receive on bound socket (non-blocking recv_timeout)
        let received = rx
            .recv_timeout(std::time::Duration::from_millis(100))
            .unwrap();
        assert_eq!(received, msg);

        // Cleanup
        unbind_inproc(endpoint).unwrap();
    }

    #[test]
    fn test_list_endpoints() {
        let ep1 = "inproc://test-list-1";
        let ep2 = "inproc://test-list-2";

        let _bind1 = bind_inproc(ep1).unwrap();
        let _bind2 = bind_inproc(ep2).unwrap();

        let endpoints = list_inproc_endpoints();
        assert!(endpoints.contains(&"test-list-1".to_string()));
        assert!(endpoints.contains(&"test-list-2".to_string()));

        // Cleanup
        unbind_inproc(ep1).unwrap();
        unbind_inproc(ep2).unwrap();
    }
}