ipckit 0.1.6

A cross-platform IPC (Inter-Process Communication) library for Rust and Python
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! Local Socket implementation for IPC
//!
//! This module provides a cross-platform local socket abstraction for IPC.
//! When the `backend-interprocess` feature is enabled, it uses the `interprocess` crate
//! for a more robust implementation. Otherwise, it falls back to the native implementation.
//!
//! # Features
//! - Unix Domain Sockets on Unix systems
//! - Named Pipes on Windows
//! - Server/Client architecture
//! - Async support (with `async` feature)

use crate::error::Result;
use std::io::{Read, Write};

// ============================================================================
// Backend: interprocess
// ============================================================================

#[cfg(feature = "backend-interprocess")]
mod interprocess_backend {
    use super::*;
    use crate::error::IpcError;
    use interprocess::local_socket::{
        prelude::*, GenericFilePath, GenericNamespaced, ListenerOptions, Stream, ToFsName, ToNsName,
    };

    /// A local socket listener that accepts incoming connections.
    pub struct LocalSocketListener {
        listener: interprocess::local_socket::Listener,
        name: String,
    }

    /// A local socket stream for bidirectional communication.
    pub struct LocalSocketStream {
        inner: Stream,
        name: String,
    }

    impl LocalSocketListener {
        /// Create a new local socket listener bound to the given name.
        pub fn bind(name: &str) -> Result<Self> {
            let socket_name = get_socket_name(name)?;

            let listener = ListenerOptions::new()
                .name(socket_name)
                .create_sync()
                .map_err(|e| IpcError::Io(std::io::Error::other(e)))?;

            Ok(Self {
                listener,
                name: name.to_string(),
            })
        }

        /// Accept a new incoming connection.
        pub fn accept(&self) -> Result<LocalSocketStream> {
            let stream = self
                .listener
                .accept()
                .map_err(|e| IpcError::Io(std::io::Error::other(e)))?;

            Ok(LocalSocketStream {
                inner: stream,
                name: self.name.clone(),
            })
        }

        /// Get the name of this listener.
        pub fn name(&self) -> &str {
            &self.name
        }

        /// Returns an iterator over incoming connections.
        pub fn incoming(&self) -> impl Iterator<Item = Result<LocalSocketStream>> + '_ {
            std::iter::from_fn(move || Some(self.accept()))
        }
    }

    impl LocalSocketStream {
        /// Connect to a local socket server.
        pub fn connect(name: &str) -> Result<Self> {
            let socket_name = get_socket_name(name)?;

            let stream =
                Stream::connect(socket_name).map_err(|e| IpcError::Io(std::io::Error::other(e)))?;

            Ok(Self {
                inner: stream,
                name: name.to_string(),
            })
        }

        /// Get the name of this stream.
        pub fn name(&self) -> &str {
            &self.name
        }
    }

    impl Read for LocalSocketStream {
        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
            self.inner.read(buf)
        }
    }

    impl Write for LocalSocketStream {
        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
            self.inner.write(buf)
        }

        fn flush(&mut self) -> std::io::Result<()> {
            self.inner.flush()
        }
    }

    /// Get the appropriate socket name for the current platform.
    fn get_socket_name(name: &str) -> Result<interprocess::local_socket::Name<'static>> {
        // Try namespaced name first (works on Linux with abstract sockets and Windows)
        if let Ok(ns_name) = name.to_string().to_ns_name::<GenericNamespaced>() {
            return Ok(ns_name);
        }

        // Fall back to filesystem path
        let path = if cfg!(unix) {
            if name.starts_with('/') {
                name.to_string()
            } else {
                format!("/tmp/{}.sock", name)
            }
        } else {
            // Windows named pipe
            if name.starts_with(r"\\.\pipe\") {
                name.to_string()
            } else {
                format!(r"\\.\pipe\{}", name)
            }
        };

        path.to_fs_name::<GenericFilePath>()
            .map_err(|e| IpcError::Io(std::io::Error::other(e)))
    }
}

#[cfg(feature = "backend-interprocess")]
pub use interprocess_backend::{LocalSocketListener, LocalSocketStream};

// ============================================================================
// Backend: Native (fallback)
// ============================================================================

#[cfg(not(feature = "backend-interprocess"))]
mod native_backend {
    use super::*;
    #[cfg(unix)]
    use crate::error::IpcError;

    #[cfg(unix)]
    use std::os::unix::net::{UnixListener, UnixStream};

    /// A local socket listener that accepts incoming connections.
    pub struct LocalSocketListener {
        #[cfg(unix)]
        listener: UnixListener,
        #[cfg(unix)]
        path: String,
        #[cfg(windows)]
        pipe_name: String,
        name: String,
    }

    /// A local socket stream for bidirectional communication.
    pub struct LocalSocketStream {
        #[cfg(unix)]
        stream: UnixStream,
        #[cfg(windows)]
        handle: crate::windows::PipeHandle,
        name: String,
    }

    impl LocalSocketListener {
        /// Create a new local socket listener bound to the given name.
        pub fn bind(name: &str) -> Result<Self> {
            #[cfg(unix)]
            {
                let path = if name.starts_with('/') {
                    name.to_string()
                } else {
                    format!("/tmp/{}.sock", name)
                };

                // Remove existing socket if any
                let _ = std::fs::remove_file(&path);

                let listener = UnixListener::bind(&path).map_err(|e| match e.kind() {
                    std::io::ErrorKind::PermissionDenied => {
                        IpcError::PermissionDenied(path.clone())
                    }
                    _ => IpcError::Io(e),
                })?;

                Ok(Self {
                    listener,
                    path,
                    name: name.to_string(),
                })
            }

            #[cfg(windows)]
            {
                let pipe_name = if name.starts_with(r"\\.\pipe\") {
                    name.to_string()
                } else {
                    format!(r"\\.\pipe\{}", name)
                };

                Ok(Self {
                    pipe_name,
                    name: name.to_string(),
                })
            }
        }

        /// Accept a new incoming connection.
        pub fn accept(&self) -> Result<LocalSocketStream> {
            #[cfg(unix)]
            {
                let (stream, _) = self.listener.accept()?;
                Ok(LocalSocketStream {
                    stream,
                    name: self.name.clone(),
                })
            }

            #[cfg(windows)]
            {
                use crate::windows;
                let handle = windows::create_named_pipe_for_server(&self.pipe_name)?;
                windows::wait_for_client_handle(&handle)?;
                Ok(LocalSocketStream {
                    handle,
                    name: self.name.clone(),
                })
            }
        }

        /// Get the name of this listener.
        pub fn name(&self) -> &str {
            &self.name
        }

        /// Returns an iterator over incoming connections.
        pub fn incoming(&self) -> impl Iterator<Item = Result<LocalSocketStream>> + '_ {
            std::iter::from_fn(move || Some(self.accept()))
        }
    }

    #[cfg(unix)]
    impl Drop for LocalSocketListener {
        fn drop(&mut self) {
            let _ = std::fs::remove_file(&self.path);
        }
    }

    impl LocalSocketStream {
        /// Connect to a local socket server.
        pub fn connect(name: &str) -> Result<Self> {
            #[cfg(unix)]
            {
                let path = if name.starts_with('/') {
                    name.to_string()
                } else {
                    format!("/tmp/{}.sock", name)
                };

                let stream = UnixStream::connect(&path).map_err(|e| match e.kind() {
                    std::io::ErrorKind::NotFound => IpcError::NotFound(path.clone()),
                    std::io::ErrorKind::PermissionDenied => {
                        IpcError::PermissionDenied(path.clone())
                    }
                    std::io::ErrorKind::ConnectionRefused => {
                        IpcError::NotFound(format!("Connection refused: {}", path))
                    }
                    _ => IpcError::Io(e),
                })?;

                Ok(Self {
                    stream,
                    name: name.to_string(),
                })
            }

            #[cfg(windows)]
            {
                use crate::windows;
                let pipe_name = if name.starts_with(r"\\.\pipe\") {
                    name.to_string()
                } else {
                    format!(r"\\.\pipe\{}", name)
                };

                let handle = windows::connect_to_named_pipe(&pipe_name)?;
                Ok(Self {
                    handle,
                    name: name.to_string(),
                })
            }
        }

        /// Get the name of this stream.
        pub fn name(&self) -> &str {
            &self.name
        }
    }

    impl Read for LocalSocketStream {
        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
            #[cfg(unix)]
            {
                self.stream.read(buf)
            }
            #[cfg(windows)]
            {
                crate::windows::read_pipe(&self.handle, buf)
            }
        }
    }

    impl Write for LocalSocketStream {
        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
            #[cfg(unix)]
            {
                self.stream.write(buf)
            }
            #[cfg(windows)]
            {
                crate::windows::write_pipe(&self.handle, buf)
            }
        }

        fn flush(&mut self) -> std::io::Result<()> {
            #[cfg(unix)]
            {
                self.stream.flush()
            }
            #[cfg(windows)]
            {
                Ok(())
            }
        }
    }
}

#[cfg(not(feature = "backend-interprocess"))]
pub use native_backend::{LocalSocketListener, LocalSocketStream};

// ============================================================================
// Async support
// ============================================================================

#[cfg(all(feature = "async", feature = "backend-interprocess"))]
pub mod async_socket {
    //! Async local socket support using tokio.

    use super::*;
    use crate::error::IpcError;
    use interprocess::local_socket::{
        tokio::prelude::*, GenericFilePath, GenericNamespaced, ListenerOptions, ToFsName, ToNsName,
    };
    use tokio::io::{AsyncRead, AsyncWrite};

    /// Async local socket listener.
    pub struct AsyncLocalSocketListener {
        inner: interprocess::local_socket::tokio::Listener,
        name: String,
    }

    /// Async local socket stream.
    pub struct AsyncLocalSocketStream {
        inner: interprocess::local_socket::tokio::Stream,
        name: String,
    }

    impl AsyncLocalSocketListener {
        /// Create a new async local socket listener.
        pub async fn bind(name: &str) -> Result<Self> {
            let socket_name = get_async_socket_name(name)?;

            let listener = ListenerOptions::new()
                .name(socket_name)
                .create_tokio()
                .map_err(|e| IpcError::Io(std::io::Error::other(e)))?;

            Ok(Self {
                inner: listener,
                name: name.to_string(),
            })
        }

        /// Accept a new incoming connection asynchronously.
        pub async fn accept(&self) -> Result<AsyncLocalSocketStream> {
            let stream = self
                .inner
                .accept()
                .await
                .map_err(|e| IpcError::Io(std::io::Error::other(e)))?;

            Ok(AsyncLocalSocketStream {
                inner: stream,
                name: self.name.clone(),
            })
        }

        /// Get the name of this listener.
        pub fn name(&self) -> &str {
            &self.name
        }
    }

    impl AsyncLocalSocketStream {
        /// Connect to a local socket server asynchronously.
        pub async fn connect(name: &str) -> Result<Self> {
            let socket_name = get_async_socket_name(name)?;

            let stream = interprocess::local_socket::tokio::Stream::connect(socket_name)
                .await
                .map_err(|e| IpcError::Io(std::io::Error::other(e)))?;

            Ok(Self {
                inner: stream,
                name: name.to_string(),
            })
        }

        /// Get the name of this stream.
        pub fn name(&self) -> &str {
            &self.name
        }

        /// Split into read and write halves.
        pub fn into_split(
            self,
        ) -> (
            tokio::io::ReadHalf<interprocess::local_socket::tokio::Stream>,
            tokio::io::WriteHalf<interprocess::local_socket::tokio::Stream>,
        ) {
            tokio::io::split(self.inner)
        }
    }

    impl AsyncRead for AsyncLocalSocketStream {
        fn poll_read(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &mut tokio::io::ReadBuf<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            std::pin::Pin::new(&mut self.inner).poll_read(cx, buf)
        }
    }

    impl AsyncWrite for AsyncLocalSocketStream {
        fn poll_write(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &[u8],
        ) -> std::task::Poll<std::io::Result<usize>> {
            std::pin::Pin::new(&mut self.inner).poll_write(cx, buf)
        }

        fn poll_flush(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            std::pin::Pin::new(&mut self.inner).poll_flush(cx)
        }

        fn poll_shutdown(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            std::pin::Pin::new(&mut self.inner).poll_shutdown(cx)
        }
    }

    fn get_async_socket_name(name: &str) -> Result<interprocess::local_socket::Name<'static>> {
        if let Ok(ns_name) = name.to_string().to_ns_name::<GenericNamespaced>() {
            return Ok(ns_name);
        }

        let path = if cfg!(unix) {
            if name.starts_with('/') {
                name.to_string()
            } else {
                format!("/tmp/{}.sock", name)
            }
        } else if name.starts_with(r"\\.\pipe\") {
            name.to_string()
        } else {
            format!(r"\\.\pipe\{}", name)
        };

        path.to_fs_name::<GenericFilePath>()
            .map_err(|e| IpcError::Io(std::io::Error::other(e)))
    }
}

#[cfg(all(feature = "async", feature = "backend-interprocess"))]
pub use async_socket::{AsyncLocalSocketListener, AsyncLocalSocketStream};

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

    #[test]
    fn test_local_socket_communication() {
        let server_name = format!("test_socket_{}", std::process::id());

        // Create server in a separate thread
        let server_name_clone = server_name.clone();
        let server_thread = thread::spawn(move || {
            let listener = LocalSocketListener::bind(&server_name_clone).unwrap();
            let mut stream = listener.accept().unwrap();

            let mut buf = [0u8; 32];
            let n = stream.read(&mut buf).unwrap();
            assert_eq!(&buf[..n], b"Hello, Server!");

            stream.write_all(b"Hello, Client!").unwrap();
        });

        // Give server time to start
        thread::sleep(std::time::Duration::from_millis(100));

        // Connect as client
        let mut client = LocalSocketStream::connect(&server_name).unwrap();
        client.write_all(b"Hello, Server!").unwrap();

        let mut buf = [0u8; 32];
        let n = client.read(&mut buf).unwrap();
        assert_eq!(&buf[..n], b"Hello, Client!");

        server_thread.join().unwrap();
    }
}