ipckit 0.1.1

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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
//! Python bindings for ipckit
//!
//! This module provides Python bindings using PyO3.
//! All JSON serialization is done in Rust using serde_json for better performance.

use pyo3::prelude::*;
use pyo3::types::{PyBool, PyBytes, PyDict, PyFloat, PyInt, PyList, PyString};
use std::io::{Read, Write};
use std::time::Duration;

use crate::error::IpcError;
use crate::file_channel::{
    FileChannel as RustFileChannel, FileMessage as RustFileMessage, MessageType as RustMessageType,
};
use crate::graceful::{
    GracefulChannel, GracefulIpcChannel as RustGracefulIpcChannel,
    GracefulNamedPipe as RustGracefulNamedPipe,
};
use crate::pipe::{AnonymousPipe as RustAnonymousPipe, NamedPipe as RustNamedPipe};
use crate::shm::SharedMemory as RustSharedMemory;

// ============================================================================
// JSON Conversion Utilities (Rust-native, no Python json module dependency)
// ============================================================================

/// Convert a Python object to serde_json::Value using Rust
/// This is faster than using Python's json module
fn py_to_json_value(obj: &Bound<'_, PyAny>) -> PyResult<serde_json::Value> {
    // None
    if obj.is_none() {
        return Ok(serde_json::Value::Null);
    }

    // Bool (must check before int, as bool is subclass of int in Python)
    if let Ok(b) = obj.downcast::<PyBool>() {
        return Ok(serde_json::Value::Bool(b.is_true()));
    }

    // Int
    if let Ok(i) = obj.downcast::<PyInt>() {
        if let Ok(v) = i.extract::<i64>() {
            return Ok(serde_json::Value::Number(v.into()));
        }
        if let Ok(v) = i.extract::<u64>() {
            return Ok(serde_json::Value::Number(v.into()));
        }
        // Fall back to float for very large integers
        if let Ok(v) = i.extract::<f64>() {
            if let Some(n) = serde_json::Number::from_f64(v) {
                return Ok(serde_json::Value::Number(n));
            }
        }
        return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
            "Integer too large for JSON",
        ));
    }

    // Float
    if let Ok(f) = obj.downcast::<PyFloat>() {
        let v: f64 = f.extract()?;
        if let Some(n) = serde_json::Number::from_f64(v) {
            return Ok(serde_json::Value::Number(n));
        }
        // NaN and Infinity are not valid JSON
        return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
            "Float value is not valid JSON (NaN or Infinity)",
        ));
    }

    // String
    if let Ok(s) = obj.downcast::<PyString>() {
        let v: String = s.extract()?;
        return Ok(serde_json::Value::String(v));
    }

    // Bytes -> base64 string
    if let Ok(b) = obj.downcast::<PyBytes>() {
        let bytes: &[u8] = b.as_bytes();
        // Use base64 encoding for bytes
        use base64::Engine;
        let encoded = base64::engine::general_purpose::STANDARD.encode(bytes);
        return Ok(serde_json::Value::String(encoded));
    }

    // List
    if let Ok(list) = obj.downcast::<PyList>() {
        let mut arr = Vec::with_capacity(list.len());
        for item in list.iter() {
            arr.push(py_to_json_value(&item)?);
        }
        return Ok(serde_json::Value::Array(arr));
    }

    // Dict
    if let Ok(dict) = obj.downcast::<PyDict>() {
        let mut map = serde_json::Map::new();
        for (key, value) in dict.iter() {
            let key_str: String = key.extract().map_err(|_| {
                PyErr::new::<pyo3::exceptions::PyTypeError, _>("Dict keys must be strings")
            })?;
            map.insert(key_str, py_to_json_value(&value)?);
        }
        return Ok(serde_json::Value::Object(map));
    }

    // Try to convert other types via their __dict__ or repr
    Err(PyErr::new::<pyo3::exceptions::PyTypeError, _>(format!(
        "Cannot convert type to JSON: {:?}",
        obj.get_type().name()
    )))
}

/// Convert serde_json::Value to Python object
fn json_value_to_py(py: Python<'_>, value: &serde_json::Value) -> PyResult<PyObject> {
    match value {
        serde_json::Value::Null => Ok(py.None()),
        serde_json::Value::Bool(b) => Ok(b.into_pyobject(py)?.to_owned().into_any().unbind()),
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                Ok(i.into_pyobject(py)?.into_any().unbind())
            } else if let Some(u) = n.as_u64() {
                Ok(u.into_pyobject(py)?.into_any().unbind())
            } else if let Some(f) = n.as_f64() {
                Ok(f.into_pyobject(py)?.into_any().unbind())
            } else {
                Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
                    "Invalid JSON number",
                ))
            }
        }
        serde_json::Value::String(s) => Ok(s.into_pyobject(py)?.into_any().unbind()),
        serde_json::Value::Array(arr) => {
            let list = PyList::empty(py);
            for item in arr {
                list.append(json_value_to_py(py, item)?)?;
            }
            Ok(list.into())
        }
        serde_json::Value::Object(map) => {
            let dict = PyDict::new(py);
            for (key, value) in map {
                dict.set_item(key, json_value_to_py(py, value)?)?;
            }
            Ok(dict.into())
        }
    }
}

/// Serialize Python object to JSON bytes using Rust's serde_json
#[pyfunction]
fn json_dumps(obj: &Bound<'_, PyAny>) -> PyResult<String> {
    let value = py_to_json_value(obj)?;
    serde_json::to_string(&value)
        .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
}

/// Serialize Python object to pretty JSON string
#[pyfunction]
fn json_dumps_pretty(obj: &Bound<'_, PyAny>) -> PyResult<String> {
    let value = py_to_json_value(obj)?;
    serde_json::to_string_pretty(&value)
        .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
}

/// Deserialize JSON string to Python object using Rust's serde_json
#[pyfunction]
fn json_loads(py: Python<'_>, s: &str) -> PyResult<PyObject> {
    let value: serde_json::Value = serde_json::from_str(s)
        .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))?;
    json_value_to_py(py, &value)
}

// ============================================================================
// AnonymousPipe
// ============================================================================

/// Python wrapper for AnonymousPipe
/// Uses Mutex to allow concurrent access from multiple threads
#[pyclass(name = "AnonymousPipe")]
pub struct PyAnonymousPipe {
    reader: std::sync::Mutex<Option<crate::pipe::PipeReader>>,
    writer: std::sync::Mutex<Option<crate::pipe::PipeWriter>>,
}

#[pymethods]
impl PyAnonymousPipe {
    /// Create a new anonymous pipe pair
    #[new]
    fn new() -> PyResult<Self> {
        let pipe = RustAnonymousPipe::new()?;
        let (reader, writer) = pipe.split();
        Ok(Self {
            reader: std::sync::Mutex::new(Some(reader)),
            writer: std::sync::Mutex::new(Some(writer)),
        })
    }

    /// Read data from the pipe
    fn read(&self, py: Python<'_>, size: usize) -> PyResult<Py<PyBytes>> {
        let mut guard = self
            .reader
            .lock()
            .map_err(|_| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("Lock poisoned"))?;
        let reader = guard.as_mut().ok_or(IpcError::Closed)?;

        let mut buf = vec![0u8; size];
        let n = py.allow_threads(|| reader.read(&mut buf))?;
        buf.truncate(n);

        Ok(PyBytes::new(py, &buf).into())
    }

    /// Write data to the pipe
    fn write(&self, py: Python<'_>, data: &[u8]) -> PyResult<usize> {
        let mut guard = self
            .writer
            .lock()
            .map_err(|_| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("Lock poisoned"))?;
        let writer = guard.as_mut().ok_or(IpcError::Closed)?;
        let data = data.to_vec(); // Clone data before releasing GIL
        let n = py.allow_threads(|| writer.write(&data))?;
        Ok(n)
    }

    /// Get the reader file descriptor (Unix only)
    #[cfg(unix)]
    fn reader_fd(&self) -> PyResult<i32> {
        use std::os::unix::io::AsRawFd;
        let guard = self
            .reader
            .lock()
            .map_err(|_| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("Lock poisoned"))?;
        let reader = guard.as_ref().ok_or(IpcError::Closed)?;
        Ok(reader.as_raw_fd())
    }

    /// Get the writer file descriptor (Unix only)
    #[cfg(unix)]
    fn writer_fd(&self) -> PyResult<i32> {
        use std::os::unix::io::AsRawFd;
        let guard = self
            .writer
            .lock()
            .map_err(|_| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("Lock poisoned"))?;
        let writer = guard.as_ref().ok_or(IpcError::Closed)?;
        Ok(writer.as_raw_fd())
    }

    /// Take the reader end (for passing to child process)
    fn take_reader(&self) -> PyResult<()> {
        let mut guard = self
            .reader
            .lock()
            .map_err(|_| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("Lock poisoned"))?;
        guard.take();
        Ok(())
    }

    /// Take the writer end (for passing to child process)
    fn take_writer(&self) -> PyResult<()> {
        let mut guard = self
            .writer
            .lock()
            .map_err(|_| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("Lock poisoned"))?;
        guard.take();
        Ok(())
    }
}

// ============================================================================
// NamedPipe
// ============================================================================

/// Python wrapper for NamedPipe
#[pyclass(name = "NamedPipe")]
pub struct PyNamedPipe {
    inner: RustNamedPipe,
}

#[pymethods]
impl PyNamedPipe {
    /// Create a new named pipe server
    #[staticmethod]
    fn create(name: &str) -> PyResult<Self> {
        let inner = RustNamedPipe::create(name)?;
        Ok(Self { inner })
    }

    /// Connect to an existing named pipe
    #[staticmethod]
    fn connect(name: &str) -> PyResult<Self> {
        let inner = RustNamedPipe::connect(name)?;
        Ok(Self { inner })
    }

    /// Get the pipe name
    #[getter]
    fn name(&self) -> &str {
        self.inner.name()
    }

    /// Check if this is the server end
    #[getter]
    fn is_server(&self) -> bool {
        self.inner.is_server()
    }

    /// Wait for a client to connect (server only)
    fn wait_for_client(&mut self, py: Python<'_>) -> PyResult<()> {
        // Release GIL to allow other Python threads to run
        py.allow_threads(|| self.inner.wait_for_client())?;
        Ok(())
    }

    /// Read data from the pipe
    fn read(&mut self, py: Python<'_>, size: usize) -> PyResult<Py<PyBytes>> {
        let mut buf = vec![0u8; size];
        // Release GIL during blocking read
        let n = py.allow_threads(|| self.inner.read(&mut buf))?;
        buf.truncate(n);
        Ok(PyBytes::new(py, &buf).into())
    }

    /// Write data to the pipe
    fn write(&mut self, py: Python<'_>, data: Vec<u8>) -> PyResult<usize> {
        // Release GIL during write
        let n = py.allow_threads(|| self.inner.write(&data))?;
        Ok(n)
    }

    /// Read exact number of bytes
    fn read_exact(&mut self, py: Python<'_>, size: usize) -> PyResult<Py<PyBytes>> {
        let mut buf = vec![0u8; size];
        // Release GIL during blocking read
        py.allow_threads(|| self.inner.read_exact(&mut buf))?;
        Ok(PyBytes::new(py, &buf).into())
    }

    /// Write all data
    fn write_all(&mut self, py: Python<'_>, data: Vec<u8>) -> PyResult<()> {
        // Release GIL during write
        py.allow_threads(|| self.inner.write_all(&data))?;
        Ok(())
    }
}

// ============================================================================
// SharedMemory
// ============================================================================

/// Python wrapper for SharedMemory
#[pyclass(name = "SharedMemory")]
pub struct PySharedMemory {
    inner: RustSharedMemory,
}

#[pymethods]
impl PySharedMemory {
    /// Create a new shared memory region
    #[staticmethod]
    fn create(name: &str, size: usize) -> PyResult<Self> {
        let inner = RustSharedMemory::create(name, size)?;
        Ok(Self { inner })
    }

    /// Open an existing shared memory region
    #[staticmethod]
    fn open(name: &str) -> PyResult<Self> {
        let inner = RustSharedMemory::open(name)?;
        Ok(Self { inner })
    }

    /// Get the shared memory name
    #[getter]
    fn name(&self) -> &str {
        self.inner.name()
    }

    /// Get the shared memory size
    #[getter]
    fn size(&self) -> usize {
        self.inner.size()
    }

    /// Check if this instance is the owner
    #[getter]
    fn is_owner(&self) -> bool {
        self.inner.is_owner()
    }

    /// Write data to shared memory at offset
    fn write(&mut self, offset: usize, data: &[u8]) -> PyResult<()> {
        self.inner.write(offset, data)?;
        Ok(())
    }

    /// Read data from shared memory at offset
    fn read(&self, py: Python<'_>, offset: usize, size: usize) -> PyResult<Py<PyBytes>> {
        let data = self.inner.read(offset, size)?;
        Ok(PyBytes::new(py, &data).into())
    }

    /// Read all data from shared memory
    fn read_all(&self, py: Python<'_>) -> PyResult<Py<PyBytes>> {
        let data = self.inner.read(0, self.inner.size())?;
        Ok(PyBytes::new(py, &data).into())
    }
}

// ============================================================================
// IpcChannel
// ============================================================================

/// Python wrapper for IpcChannel
#[pyclass(name = "IpcChannel")]
pub struct PyIpcChannel {
    inner: crate::channel::IpcChannel<Vec<u8>>,
}

#[pymethods]
impl PyIpcChannel {
    /// Create a new IPC channel server
    #[staticmethod]
    fn create(name: &str) -> PyResult<Self> {
        let inner = crate::channel::IpcChannel::create(name)?;
        Ok(Self { inner })
    }

    /// Connect to an existing IPC channel
    #[staticmethod]
    fn connect(name: &str) -> PyResult<Self> {
        let inner = crate::channel::IpcChannel::connect(name)?;
        Ok(Self { inner })
    }

    /// Get the channel name
    #[getter]
    fn name(&self) -> &str {
        self.inner.name()
    }

    /// Check if this is the server end
    #[getter]
    fn is_server(&self) -> bool {
        self.inner.is_server()
    }

    /// Wait for a client to connect (server only)
    fn wait_for_client(&mut self, py: Python<'_>) -> PyResult<()> {
        // Release GIL to allow other Python threads to run
        py.allow_threads(|| self.inner.wait_for_client())?;
        Ok(())
    }

    /// Send bytes through the channel
    fn send(&mut self, py: Python<'_>, data: Vec<u8>) -> PyResult<()> {
        py.allow_threads(|| self.inner.send_bytes(&data))?;
        Ok(())
    }

    /// Receive bytes from the channel
    fn recv(&mut self, py: Python<'_>) -> PyResult<Py<PyBytes>> {
        let data = py.allow_threads(|| self.inner.recv_bytes())?;
        Ok(PyBytes::new(py, &data).into())
    }

    /// Send a JSON-serializable object (uses Rust serde_json)
    fn send_json(&mut self, py: Python<'_>, obj: &Bound<'_, PyAny>) -> PyResult<()> {
        let value = py_to_json_value(obj)?;
        let json_bytes = serde_json::to_vec(&value)
            .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))?;
        py.allow_threads(|| self.inner.send_bytes(&json_bytes))?;
        Ok(())
    }

    /// Receive a JSON object (uses Rust serde_json)
    fn recv_json(&mut self, py: Python<'_>) -> PyResult<PyObject> {
        let data = py.allow_threads(|| self.inner.recv_bytes())?;
        let value: serde_json::Value =
            serde_json::from_slice(&data).map_err(|e| IpcError::deserialization(e.to_string()))?;
        json_value_to_py(py, &value)
    }
}

// ============================================================================
// FileChannel
// ============================================================================

/// Python wrapper for FileChannel - File-based IPC for frontend-backend communication
///
/// All JSON serialization is done in Rust for better performance.
#[pyclass(name = "FileChannel")]
pub struct PyFileChannel {
    inner: RustFileChannel,
}

#[pymethods]
impl PyFileChannel {
    /// Create a backend-side file channel
    #[staticmethod]
    fn backend(dir: &str) -> PyResult<Self> {
        let inner = RustFileChannel::backend(dir)?;
        Ok(Self { inner })
    }

    /// Create a frontend-side file channel
    #[staticmethod]
    fn frontend(dir: &str) -> PyResult<Self> {
        let inner = RustFileChannel::frontend(dir)?;
        Ok(Self { inner })
    }

    /// Get the channel directory path
    #[getter]
    fn dir(&self) -> String {
        self.inner.dir().to_string_lossy().to_string()
    }

    /// Send a request message (JSON serialization done in Rust)
    fn send_request(&self, method: &str, params: &Bound<'_, PyAny>) -> PyResult<String> {
        let json_value = py_to_json_value(params)?;
        let id = self.inner.send_request(method, json_value)?;
        Ok(id)
    }

    /// Send a response to a request
    fn send_response(&self, request_id: &str, result: &Bound<'_, PyAny>) -> PyResult<()> {
        let json_value = py_to_json_value(result)?;
        self.inner.send_response(request_id, json_value)?;
        Ok(())
    }

    /// Send an error response
    fn send_error(&self, request_id: &str, error: &str) -> PyResult<()> {
        self.inner.send_error(request_id, error)?;
        Ok(())
    }

    /// Send an event (fire-and-forget, no response expected)
    fn send_event(&self, name: &str, payload: &Bound<'_, PyAny>) -> PyResult<()> {
        let json_value = py_to_json_value(payload)?;
        self.inner.send_event(name, json_value)?;
        Ok(())
    }

    /// Receive all new messages
    fn recv(&mut self, py: Python<'_>) -> PyResult<PyObject> {
        let messages = self.inner.recv()?;
        let list = PyList::empty(py);
        for msg in messages {
            list.append(file_message_to_py(py, msg)?)?;
        }
        Ok(list.into())
    }

    /// Receive a single new message (non-blocking)
    fn recv_one(&mut self, py: Python<'_>) -> PyResult<PyObject> {
        match self.inner.recv_one()? {
            Some(msg) => file_message_to_py(py, msg),
            None => Ok(py.None()),
        }
    }

    /// Wait for a response to a specific request
    fn wait_response(
        &mut self,
        py: Python<'_>,
        request_id: &str,
        timeout_ms: u64,
    ) -> PyResult<PyObject> {
        let timeout = Duration::from_millis(timeout_ms);
        let msg = self.inner.wait_response(request_id, timeout)?;
        file_message_to_py(py, msg)
    }

    /// Clear all messages in both inbox and outbox
    fn clear(&self) -> PyResult<()> {
        self.inner.clear()?;
        Ok(())
    }
}

/// Convert FileMessage to Python dict (all in Rust, no Python json module)
fn file_message_to_py(py: Python<'_>, msg: RustFileMessage) -> PyResult<PyObject> {
    let dict = PyDict::new(py);

    dict.set_item("id", &msg.id)?;
    dict.set_item("timestamp", msg.timestamp)?;

    let msg_type = match msg.msg_type {
        RustMessageType::Request => "request",
        RustMessageType::Response => "response",
        RustMessageType::Event => "event",
    };
    dict.set_item("type", msg_type)?;

    if let Some(method) = msg.method {
        dict.set_item("method", method)?;
    }

    if let Some(reply_to) = msg.reply_to {
        dict.set_item("reply_to", reply_to)?;
    }

    if let Some(error) = msg.error {
        dict.set_item("error", error)?;
    }

    // Convert payload using Rust JSON conversion
    let payload = json_value_to_py(py, &msg.payload)?;
    dict.set_item("payload", payload)?;

    Ok(dict.into())
}

// ============================================================================
// GracefulNamedPipe
// ============================================================================

/// Python wrapper for GracefulNamedPipe - Named pipe with graceful shutdown support
///
/// This class wraps a NamedPipe with graceful shutdown capabilities,
/// preventing errors when background threads continue sending messages
/// after the main event loop has closed.
#[pyclass(name = "GracefulNamedPipe")]
pub struct PyGracefulNamedPipe {
    inner: RustGracefulNamedPipe,
}

#[pymethods]
impl PyGracefulNamedPipe {
    /// Create a new named pipe server with graceful shutdown
    #[staticmethod]
    fn create(name: &str) -> PyResult<Self> {
        let inner = RustGracefulNamedPipe::create(name)?;
        Ok(Self { inner })
    }

    /// Connect to an existing named pipe with graceful shutdown
    #[staticmethod]
    fn connect(name: &str) -> PyResult<Self> {
        let inner = RustGracefulNamedPipe::connect(name)?;
        Ok(Self { inner })
    }

    /// Get the pipe name
    #[getter]
    fn name(&self) -> &str {
        self.inner.name()
    }

    /// Check if this is the server end
    #[getter]
    fn is_server(&self) -> bool {
        self.inner.is_server()
    }

    /// Check if the channel has been shutdown
    #[getter]
    fn is_shutdown(&self) -> bool {
        self.inner.is_shutdown()
    }

    /// Wait for a client to connect (server only)
    fn wait_for_client(&mut self, py: Python<'_>) -> PyResult<()> {
        // Release GIL to allow other Python threads to run
        py.allow_threads(|| self.inner.wait_for_client())?;
        Ok(())
    }

    /// Signal the channel to shutdown
    ///
    /// After calling this method:
    /// - New send/receive operations will raise ConnectionError
    /// - Pending operations may still complete
    /// - Use drain() to wait for pending operations
    fn shutdown(&self) {
        self.inner.shutdown();
    }

    /// Wait for all pending operations to complete
    fn drain(&self, py: Python<'_>) -> PyResult<()> {
        py.allow_threads(|| self.inner.drain())?;
        Ok(())
    }

    /// Shutdown with a timeout (in milliseconds)
    ///
    /// Combines shutdown() and drain() with a timeout.
    /// Raises TimeoutError if the drain doesn't complete within the timeout.
    fn shutdown_timeout(&self, py: Python<'_>, timeout_ms: u64) -> PyResult<()> {
        let timeout = Duration::from_millis(timeout_ms);
        py.allow_threads(|| self.inner.shutdown_timeout(timeout))?;
        Ok(())
    }

    /// Read data from the pipe
    fn read(&mut self, py: Python<'_>, size: usize) -> PyResult<Py<PyBytes>> {
        let mut buf = vec![0u8; size];
        // Release GIL during blocking read
        let n = py.allow_threads(|| self.inner.read(&mut buf))?;
        buf.truncate(n);
        Ok(PyBytes::new(py, &buf).into())
    }

    /// Write data to the pipe
    fn write(&mut self, py: Python<'_>, data: Vec<u8>) -> PyResult<usize> {
        // Release GIL during write
        let n = py.allow_threads(|| self.inner.write(&data))?;
        Ok(n)
    }

    /// Read exact number of bytes
    fn read_exact(&mut self, py: Python<'_>, size: usize) -> PyResult<Py<PyBytes>> {
        let mut buf = vec![0u8; size];
        // Release GIL during blocking read
        py.allow_threads(|| self.inner.read_exact(&mut buf))?;
        Ok(PyBytes::new(py, &buf).into())
    }

    /// Write all data
    fn write_all(&mut self, py: Python<'_>, data: Vec<u8>) -> PyResult<()> {
        // Release GIL during write
        py.allow_threads(|| self.inner.write_all(&data))?;
        Ok(())
    }
}

// ============================================================================
// GracefulIpcChannel
// ============================================================================

/// Python wrapper for GracefulIpcChannel - IPC channel with graceful shutdown support
///
/// This class wraps an IpcChannel with graceful shutdown capabilities,
/// preventing errors when background threads continue sending messages
/// after the main event loop has closed.
#[pyclass(name = "GracefulIpcChannel")]
pub struct PyGracefulIpcChannel {
    inner: RustGracefulIpcChannel<Vec<u8>>,
}

#[pymethods]
impl PyGracefulIpcChannel {
    /// Create a new IPC channel server with graceful shutdown
    #[staticmethod]
    fn create(name: &str) -> PyResult<Self> {
        let inner = RustGracefulIpcChannel::create(name)?;
        Ok(Self { inner })
    }

    /// Connect to an existing IPC channel with graceful shutdown
    #[staticmethod]
    fn connect(name: &str) -> PyResult<Self> {
        let inner = RustGracefulIpcChannel::connect(name)?;
        Ok(Self { inner })
    }

    /// Get the channel name
    #[getter]
    fn name(&self) -> &str {
        self.inner.name()
    }

    /// Check if this is the server end
    #[getter]
    fn is_server(&self) -> bool {
        self.inner.is_server()
    }

    /// Check if the channel has been shutdown
    #[getter]
    fn is_shutdown(&self) -> bool {
        self.inner.is_shutdown()
    }

    /// Wait for a client to connect (server only)
    fn wait_for_client(&mut self, py: Python<'_>) -> PyResult<()> {
        // Release GIL to allow other Python threads to run
        py.allow_threads(|| self.inner.wait_for_client())?;
        Ok(())
    }

    /// Signal the channel to shutdown
    ///
    /// After calling this method:
    /// - New send/receive operations will raise ConnectionError
    /// - Pending operations may still complete
    /// - Use drain() to wait for pending operations
    fn shutdown(&self) {
        self.inner.shutdown();
    }

    /// Wait for all pending operations to complete
    fn drain(&self, py: Python<'_>) -> PyResult<()> {
        py.allow_threads(|| self.inner.drain())?;
        Ok(())
    }

    /// Shutdown with a timeout (in milliseconds)
    ///
    /// Combines shutdown() and drain() with a timeout.
    /// Raises TimeoutError if the drain doesn't complete within the timeout.
    fn shutdown_timeout(&self, py: Python<'_>, timeout_ms: u64) -> PyResult<()> {
        let timeout = Duration::from_millis(timeout_ms);
        py.allow_threads(|| self.inner.shutdown_timeout(timeout))?;
        Ok(())
    }

    /// Send bytes through the channel
    fn send(&mut self, py: Python<'_>, data: Vec<u8>) -> PyResult<()> {
        py.allow_threads(|| self.inner.send_bytes(&data))?;
        Ok(())
    }

    /// Receive bytes from the channel
    fn recv(&mut self, py: Python<'_>) -> PyResult<Py<PyBytes>> {
        let data = py.allow_threads(|| self.inner.recv_bytes())?;
        Ok(PyBytes::new(py, &data).into())
    }

    /// Send a JSON-serializable object (uses Rust serde_json)
    fn send_json(&mut self, py: Python<'_>, obj: &Bound<'_, PyAny>) -> PyResult<()> {
        let value = py_to_json_value(obj)?;
        let json_bytes = serde_json::to_vec(&value)
            .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))?;
        py.allow_threads(|| self.inner.send_bytes(&json_bytes))?;
        Ok(())
    }

    /// Receive a JSON object (uses Rust serde_json)
    fn recv_json(&mut self, py: Python<'_>) -> PyResult<PyObject> {
        let data = py.allow_threads(|| self.inner.recv_bytes())?;
        let value: serde_json::Value =
            serde_json::from_slice(&data).map_err(|e| IpcError::deserialization(e.to_string()))?;
        json_value_to_py(py, &value)
    }
}

// ============================================================================
// Python Module
// ============================================================================

/// Create the Python module
#[pymodule]
#[pyo3(name = "ipckit")]
pub fn ipckit_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
    // IPC classes
    m.add_class::<PyAnonymousPipe>()?;
    m.add_class::<PyNamedPipe>()?;
    m.add_class::<PySharedMemory>()?;
    m.add_class::<PyIpcChannel>()?;
    m.add_class::<PyFileChannel>()?;

    // Graceful shutdown classes
    m.add_class::<PyGracefulNamedPipe>()?;
    m.add_class::<PyGracefulIpcChannel>()?;

    // JSON utilities (Rust-native, faster than Python's json module)
    m.add_function(wrap_pyfunction!(json_dumps, m)?)?;
    m.add_function(wrap_pyfunction!(json_dumps_pretty, m)?)?;
    m.add_function(wrap_pyfunction!(json_loads, m)?)?;

    // Version info
    m.add("__version__", env!("CARGO_PKG_VERSION"))?;

    // Docstring
    m.add(
        "__doc__",
        "ipckit - A cross-platform IPC library for Python

This library provides various IPC mechanisms:
- AnonymousPipe: For parent-child process communication
- NamedPipe: For communication between unrelated processes
- SharedMemory: For fast data sharing between processes
- IpcChannel: High-level message passing interface
- FileChannel: File-based IPC for frontend-backend communication

Graceful shutdown support:
- GracefulNamedPipe: Named pipe with graceful shutdown
- GracefulIpcChannel: IPC channel with graceful shutdown

JSON utilities (faster than Python's json module):
- json_dumps(obj): Serialize Python object to JSON string
- json_dumps_pretty(obj): Serialize with pretty formatting
- json_loads(s): Deserialize JSON string to Python object

Example:
    import ipckit

    # Using graceful shutdown
    channel = ipckit.GracefulIpcChannel.create('my_channel')
    channel.wait_for_client()

    # ... use channel ...

    # Graceful shutdown
    channel.shutdown()
    channel.drain()  # Wait for pending operations

    # Or with timeout (in milliseconds)
    channel.shutdown_timeout(5000)
",
    )?;

    Ok(())
}