wxdragon 0.9.15

Safe Rust bindings for wxWidgets via the wxDragon C wrapper
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
//! IPC (Inter-Process Communication) module for wxDragon.
//!
//! This module provides safe wrappers around wxWidgets' generic IPC classes (wxServer,
//! wxClient, wxConnection). The underlying transport is platform-dependent:
//! - **Windows**: Uses DDE (Dynamic Data Exchange), which is OS-native and does not
//!   trigger firewall prompts.
//! - **Unix/macOS**: Uses TCP sockets. Unix domain sockets are also supported when a
//!   file path is passed as the service name instead of a port number.
//!
//! # Overview
//!
//! wxIPC provides a simple client-server model:
//! - A **Server** listens on a service (port) and accepts connections
//! - A **Client** connects to a server on a host/service/topic
//! - A **Connection** is established between client and server for data exchange
//!
//! # Example
//!
//! ```rust,no_run
//! use wxdragon::ipc::{IPCServer, IPCClient, IPCConnection, IPCFormat};
//!
//! // Server side
//! let server = IPCServer::new(|topic| {
//!     println!("Client connecting to topic: {}", topic);
//!     Some(IPCConnection::builder()
//!         .on_execute(|_topic, data, _format| {
//!             println!("Received execute: {:?}", data);
//!             true
//!         })
//!         .build())
//! });
//! server.create("4242"); // Listen on port 4242
//!
//! // Client side
//! let client = IPCClient::new();
//! if let Some(conn) = client.make_connection("localhost", "4242", "test") {
//!     conn.execute_string("Hello, server!");
//! }
//! ```

use std::ffi::{CStr, CString};
use std::os::raw::c_void;
use std::ptr;
use wxdragon_sys as ffi;

/// IPC data format for Execute, Request, Poke, and Advise operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum IPCFormat {
    /// Text format (CF_TEXT)
    Text = 1,
    /// Bitmap format (CF_BITMAP)
    Bitmap = 2,
    /// Metafile format (CF_METAFILEPICT)
    Metafile = 3,
    /// Unicode text format (CF_UNICODETEXT)
    UnicodeText = 13,
    /// UTF-8 text format
    Utf8Text = 14,
    /// Private/binary data format
    Private = 20,
}

impl From<ffi::wxd_IPCFormat> for IPCFormat {
    fn from(format: ffi::wxd_IPCFormat) -> Self {
        match format {
            ffi::wxd_IPCFormat_WXD_IPC_TEXT => IPCFormat::Text,
            ffi::wxd_IPCFormat_WXD_IPC_BITMAP => IPCFormat::Bitmap,
            ffi::wxd_IPCFormat_WXD_IPC_METAFILE => IPCFormat::Metafile,
            ffi::wxd_IPCFormat_WXD_IPC_UNICODETEXT => IPCFormat::UnicodeText,
            ffi::wxd_IPCFormat_WXD_IPC_UTF8TEXT => IPCFormat::Utf8Text,
            ffi::wxd_IPCFormat_WXD_IPC_PRIVATE => IPCFormat::Private,
            _ => IPCFormat::Text,
        }
    }
}

impl From<IPCFormat> for ffi::wxd_IPCFormat {
    fn from(format: IPCFormat) -> Self {
        match format {
            IPCFormat::Text => ffi::wxd_IPCFormat_WXD_IPC_TEXT,
            IPCFormat::Bitmap => ffi::wxd_IPCFormat_WXD_IPC_BITMAP,
            IPCFormat::Metafile => ffi::wxd_IPCFormat_WXD_IPC_METAFILE,
            IPCFormat::UnicodeText => ffi::wxd_IPCFormat_WXD_IPC_UNICODETEXT,
            IPCFormat::Utf8Text => ffi::wxd_IPCFormat_WXD_IPC_UTF8TEXT,
            IPCFormat::Private => ffi::wxd_IPCFormat_WXD_IPC_PRIVATE,
        }
    }
}

// =============================================================================
// Connection Callbacks - stored in a Box and passed to C++
// =============================================================================

// Type aliases for callback types to reduce complexity warnings
type ExecuteCallback = Box<dyn FnMut(&str, &[u8], IPCFormat) -> bool>;
type RequestCallback = Box<dyn FnMut(&str, &str, IPCFormat) -> Option<Vec<u8>>>;
type PokeCallback = Box<dyn FnMut(&str, &str, &[u8], IPCFormat) -> bool>;
type AdviseTopicCallback = Box<dyn FnMut(&str, &str) -> bool>;
type AdviseDataCallback = Box<dyn FnMut(&str, &str, &[u8], IPCFormat) -> bool>;
type DisconnectCallback = Box<dyn FnMut() -> bool>;
type AcceptConnectionCallback = Box<dyn FnMut(&str) -> Option<IPCConnection>>;

/// Internal structure holding all connection callbacks.
struct ConnectionCallbacks {
    on_execute: Option<ExecuteCallback>,
    on_request: Option<RequestCallback>,
    on_poke: Option<PokeCallback>,
    on_start_advise: Option<AdviseTopicCallback>,
    on_stop_advise: Option<AdviseTopicCallback>,
    on_advise: Option<AdviseDataCallback>,
    on_disconnect: Option<DisconnectCallback>,
    /// Buffer for OnRequest response data (must outlive the callback return)
    request_buffer: Vec<u8>,
}

impl ConnectionCallbacks {
    fn new() -> Self {
        Self {
            on_execute: None,
            on_request: None,
            on_poke: None,
            on_start_advise: None,
            on_stop_advise: None,
            on_advise: None,
            on_disconnect: None,
            request_buffer: Vec::new(),
        }
    }
}

// =============================================================================
// C Callback Trampolines
// =============================================================================

// Allow unsafe operations in unsafe fns for cleaner trampoline code
#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn on_execute_trampoline(
    user_data: *mut c_void,
    topic: *const i8,
    data: *const c_void,
    size: usize,
    format: ffi::wxd_IPCFormat,
) -> bool {
    if user_data.is_null() {
        return false;
    }
    let callbacks = &mut *(user_data as *mut ConnectionCallbacks);
    if let Some(ref mut cb) = callbacks.on_execute {
        let topic_str = if topic.is_null() {
            ""
        } else {
            CStr::from_ptr(topic).to_str().unwrap_or("")
        };
        let data_slice = if data.is_null() || size == 0 {
            &[]
        } else {
            std::slice::from_raw_parts(data as *const u8, size)
        };
        return cb(topic_str, data_slice, IPCFormat::from(format));
    }
    false
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn on_request_trampoline(
    user_data: *mut c_void,
    topic: *const i8,
    item: *const i8,
    out_size: *mut usize,
    format: ffi::wxd_IPCFormat,
) -> *const c_void {
    if user_data.is_null() {
        return ptr::null();
    }
    let callbacks = &mut *(user_data as *mut ConnectionCallbacks);
    if let Some(ref mut cb) = callbacks.on_request {
        let topic_str = if topic.is_null() {
            ""
        } else {
            CStr::from_ptr(topic).to_str().unwrap_or("")
        };
        let item_str = if item.is_null() {
            ""
        } else {
            CStr::from_ptr(item).to_str().unwrap_or("")
        };
        if let Some(data) = cb(topic_str, item_str, IPCFormat::from(format)) {
            // Store in buffer so it outlives this function
            callbacks.request_buffer = data;
            if !out_size.is_null() {
                *out_size = callbacks.request_buffer.len();
            }
            return callbacks.request_buffer.as_ptr() as *const c_void;
        }
    }
    if !out_size.is_null() {
        *out_size = 0;
    }
    ptr::null()
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn on_poke_trampoline(
    user_data: *mut c_void,
    topic: *const i8,
    item: *const i8,
    data: *const c_void,
    size: usize,
    format: ffi::wxd_IPCFormat,
) -> bool {
    if user_data.is_null() {
        return false;
    }
    let callbacks = &mut *(user_data as *mut ConnectionCallbacks);
    if let Some(ref mut cb) = callbacks.on_poke {
        let topic_str = if topic.is_null() {
            ""
        } else {
            CStr::from_ptr(topic).to_str().unwrap_or("")
        };
        let item_str = if item.is_null() {
            ""
        } else {
            CStr::from_ptr(item).to_str().unwrap_or("")
        };
        let data_slice = if data.is_null() || size == 0 {
            &[]
        } else {
            std::slice::from_raw_parts(data as *const u8, size)
        };
        return cb(topic_str, item_str, data_slice, IPCFormat::from(format));
    }
    false
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn on_start_advise_trampoline(user_data: *mut c_void, topic: *const i8, item: *const i8) -> bool {
    if user_data.is_null() {
        return false;
    }
    let callbacks = &mut *(user_data as *mut ConnectionCallbacks);
    if let Some(ref mut cb) = callbacks.on_start_advise {
        let topic_str = if topic.is_null() {
            ""
        } else {
            CStr::from_ptr(topic).to_str().unwrap_or("")
        };
        let item_str = if item.is_null() {
            ""
        } else {
            CStr::from_ptr(item).to_str().unwrap_or("")
        };
        return cb(topic_str, item_str);
    }
    false
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn on_stop_advise_trampoline(user_data: *mut c_void, topic: *const i8, item: *const i8) -> bool {
    if user_data.is_null() {
        return false;
    }
    let callbacks = &mut *(user_data as *mut ConnectionCallbacks);
    if let Some(ref mut cb) = callbacks.on_stop_advise {
        let topic_str = if topic.is_null() {
            ""
        } else {
            CStr::from_ptr(topic).to_str().unwrap_or("")
        };
        let item_str = if item.is_null() {
            ""
        } else {
            CStr::from_ptr(item).to_str().unwrap_or("")
        };
        return cb(topic_str, item_str);
    }
    false
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn on_advise_trampoline(
    user_data: *mut c_void,
    topic: *const i8,
    item: *const i8,
    data: *const c_void,
    size: usize,
    format: ffi::wxd_IPCFormat,
) -> bool {
    if user_data.is_null() {
        return false;
    }
    let callbacks = &mut *(user_data as *mut ConnectionCallbacks);
    if let Some(ref mut cb) = callbacks.on_advise {
        let topic_str = if topic.is_null() {
            ""
        } else {
            CStr::from_ptr(topic).to_str().unwrap_or("")
        };
        let item_str = if item.is_null() {
            ""
        } else {
            CStr::from_ptr(item).to_str().unwrap_or("")
        };
        let data_slice = if data.is_null() || size == 0 {
            &[]
        } else {
            std::slice::from_raw_parts(data as *const u8, size)
        };
        return cb(topic_str, item_str, data_slice, IPCFormat::from(format));
    }
    false
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn on_disconnect_trampoline(user_data: *mut c_void) -> bool {
    if user_data.is_null() {
        return true;
    }
    let callbacks = &mut *(user_data as *mut ConnectionCallbacks);
    if let Some(ref mut cb) = callbacks.on_disconnect {
        return cb();
    }
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn free_connection_callbacks(user_data: *mut c_void) {
    if !user_data.is_null() {
        let _ = Box::from_raw(user_data as *mut ConnectionCallbacks);
    }
}

// =============================================================================
// IPCConnection
// =============================================================================

/// A connection between an IPC client and server.
///
/// Connections are used to exchange data using Execute, Request, Poke, and Advise
/// operations. The connection can be created by the server (in OnAcceptConnection)
/// or returned from a client's MakeConnection call.
pub struct IPCConnection {
    ptr: *mut ffi::wxd_IPCConnection_t,
    /// Whether we own the pointer and should destroy it
    owned: bool,
}

impl IPCConnection {
    /// Create a new connection builder.
    pub fn builder() -> IPCConnectionBuilder {
        IPCConnectionBuilder::new()
    }

    /// Create a connection from a raw pointer (used internally).
    ///
    /// # Safety
    /// The pointer must be valid and the caller transfers ownership.
    #[allow(dead_code)]
    pub(crate) unsafe fn from_ptr(ptr: *mut ffi::wxd_IPCConnection_t) -> Option<Self> {
        if ptr.is_null() {
            None
        } else {
            Some(Self { ptr, owned: false })
        }
    }

    /// Get the raw pointer (for internal use).
    #[allow(dead_code)]
    pub(crate) fn as_ptr(&self) -> *mut ffi::wxd_IPCConnection_t {
        self.ptr
    }

    /// Execute a command on the remote side.
    ///
    /// On the server side, this triggers the client's OnExecute callback.
    /// On the client side, this triggers the server's OnExecute callback.
    pub fn execute(&self, data: &[u8], format: IPCFormat) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_IPCConnection_Execute(self.ptr, data.as_ptr() as *const c_void, data.len(), format.into()) }
    }

    /// Execute a string command (convenience method for text data).
    pub fn execute_string(&self, data: &str) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        let c_str = match CString::new(data) {
            Ok(s) => s,
            Err(_) => return false,
        };
        unsafe { ffi::wxd_IPCConnection_ExecuteString(self.ptr, c_str.as_ptr()) }
    }

    /// Request data from the remote side.
    ///
    /// Returns the data if the request was successful, None otherwise.
    pub fn request(&self, item: &str, format: IPCFormat) -> Option<Vec<u8>> {
        if self.ptr.is_null() {
            return None;
        }
        let c_item = CString::new(item).ok()?;
        let mut size: usize = 0;
        let data_ptr = unsafe { ffi::wxd_IPCConnection_Request(self.ptr, c_item.as_ptr(), &mut size, format.into()) };
        if data_ptr.is_null() || size == 0 {
            return None;
        }
        let data_slice = unsafe { std::slice::from_raw_parts(data_ptr as *const u8, size) };
        Some(data_slice.to_vec())
    }

    /// Poke data to the remote side.
    pub fn poke(&self, item: &str, data: &[u8], format: IPCFormat) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        let c_item = match CString::new(item) {
            Ok(s) => s,
            Err(_) => return false,
        };
        unsafe {
            ffi::wxd_IPCConnection_Poke(
                self.ptr,
                c_item.as_ptr(),
                data.as_ptr() as *const c_void,
                data.len(),
                format.into(),
            )
        }
    }

    /// Start an advise loop for the given item.
    ///
    /// The server will send updates via the OnAdvise callback when the item changes.
    pub fn start_advise(&self, item: &str) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        let c_item = match CString::new(item) {
            Ok(s) => s,
            Err(_) => return false,
        };
        unsafe { ffi::wxd_IPCConnection_StartAdvise(self.ptr, c_item.as_ptr()) }
    }

    /// Stop an advise loop for the given item.
    pub fn stop_advise(&self, item: &str) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        let c_item = match CString::new(item) {
            Ok(s) => s,
            Err(_) => return false,
        };
        unsafe { ffi::wxd_IPCConnection_StopAdvise(self.ptr, c_item.as_ptr()) }
    }

    /// Send advised data to the client (server-side only).
    pub fn advise(&self, item: &str, data: &[u8], format: IPCFormat) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        let c_item = match CString::new(item) {
            Ok(s) => s,
            Err(_) => return false,
        };
        unsafe {
            ffi::wxd_IPCConnection_Advise(
                self.ptr,
                c_item.as_ptr(),
                data.as_ptr() as *const c_void,
                data.len(),
                format.into(),
            )
        }
    }

    /// Disconnect the connection.
    pub fn disconnect(&self) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_IPCConnection_Disconnect(self.ptr) }
    }

    /// Check if the connection is still connected.
    pub fn is_connected(&self) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_IPCConnection_IsConnected(self.ptr) }
    }
}

impl Drop for IPCConnection {
    fn drop(&mut self) {
        if self.owned && !self.ptr.is_null() {
            unsafe { ffi::wxd_IPCConnection_Destroy(self.ptr) };
        }
    }
}

/// Builder for creating an IPCConnection with callbacks.
pub struct IPCConnectionBuilder {
    callbacks: ConnectionCallbacks,
}

impl IPCConnectionBuilder {
    /// Create a new connection builder.
    pub fn new() -> Self {
        Self {
            callbacks: ConnectionCallbacks::new(),
        }
    }

    /// Set the OnExecute callback (server-side: called when client executes a command).
    pub fn on_execute<F>(mut self, callback: F) -> Self
    where
        F: FnMut(&str, &[u8], IPCFormat) -> bool + 'static,
    {
        self.callbacks.on_execute = Some(Box::new(callback));
        self
    }

    /// Set the OnRequest callback (server-side: called when client requests data).
    pub fn on_request<F>(mut self, callback: F) -> Self
    where
        F: FnMut(&str, &str, IPCFormat) -> Option<Vec<u8>> + 'static,
    {
        self.callbacks.on_request = Some(Box::new(callback));
        self
    }

    /// Set the OnPoke callback (server-side: called when client pokes data).
    pub fn on_poke<F>(mut self, callback: F) -> Self
    where
        F: FnMut(&str, &str, &[u8], IPCFormat) -> bool + 'static,
    {
        self.callbacks.on_poke = Some(Box::new(callback));
        self
    }

    /// Set the OnStartAdvise callback (server-side: called when client starts advise).
    pub fn on_start_advise<F>(mut self, callback: F) -> Self
    where
        F: FnMut(&str, &str) -> bool + 'static,
    {
        self.callbacks.on_start_advise = Some(Box::new(callback));
        self
    }

    /// Set the OnStopAdvise callback (server-side: called when client stops advise).
    pub fn on_stop_advise<F>(mut self, callback: F) -> Self
    where
        F: FnMut(&str, &str) -> bool + 'static,
    {
        self.callbacks.on_stop_advise = Some(Box::new(callback));
        self
    }

    /// Set the OnAdvise callback (client-side: called when server sends advised data).
    pub fn on_advise<F>(mut self, callback: F) -> Self
    where
        F: FnMut(&str, &str, &[u8], IPCFormat) -> bool + 'static,
    {
        self.callbacks.on_advise = Some(Box::new(callback));
        self
    }

    /// Set the OnDisconnect callback (both sides: called when connection is terminated).
    pub fn on_disconnect<F>(mut self, callback: F) -> Self
    where
        F: FnMut() -> bool + 'static,
    {
        self.callbacks.on_disconnect = Some(Box::new(callback));
        self
    }

    /// Build the connection.
    pub fn build(self) -> IPCConnection {
        let callbacks_box = Box::new(self.callbacks);
        let user_data = Box::into_raw(callbacks_box) as *mut c_void;

        let ptr = unsafe {
            ffi::wxd_IPCConnection_Create(
                user_data,
                Some(on_execute_trampoline),
                Some(on_request_trampoline),
                Some(on_poke_trampoline),
                Some(on_start_advise_trampoline),
                Some(on_stop_advise_trampoline),
                Some(on_advise_trampoline),
                Some(on_disconnect_trampoline),
                Some(free_connection_callbacks),
            )
        };

        IPCConnection { ptr, owned: true }
    }
}

impl Default for IPCConnectionBuilder {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// IPCServer
// =============================================================================

/// Callback data for the server's OnAcceptConnection.
struct ServerCallbacks {
    on_accept: AcceptConnectionCallback,
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn on_accept_connection_trampoline(user_data: *mut c_void, topic: *const i8) -> *mut ffi::wxd_IPCConnection_t {
    if user_data.is_null() {
        return ptr::null_mut();
    }
    let callbacks = &mut *(user_data as *mut ServerCallbacks);
    let topic_str = if topic.is_null() {
        ""
    } else {
        CStr::from_ptr(topic).to_str().unwrap_or("")
    };
    if let Some(conn) = (callbacks.on_accept)(topic_str) {
        // Transfer ownership to C++ - it will manage the connection
        let ptr = conn.ptr;
        std::mem::forget(conn);
        ptr
    } else {
        ptr::null_mut()
    }
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn free_server_callbacks(user_data: *mut c_void) {
    if !user_data.is_null() {
        let _ = Box::from_raw(user_data as *mut ServerCallbacks);
    }
}

/// An IPC server that listens for client connections.
///
/// The server listens on a service and accepts connections from clients.
/// On Windows, the service name identifies a DDE service. On Unix/macOS, the service
/// can be a port number (TCP) or a file path (Unix domain socket).
/// When a client connects, the OnAcceptConnection callback is called, which should
/// return a new IPCConnection to handle the client.
///
/// # Example
///
/// ```rust,no_run
/// use wxdragon::ipc::{IPCServer, IPCConnection, IPCFormat};
///
/// let server = IPCServer::new(|topic| {
///     println!("Client connecting to topic: {}", topic);
///     Some(IPCConnection::builder()
///         .on_execute(|_topic, data, _format| {
///             println!("Received: {:?}", String::from_utf8_lossy(data));
///             true
///         })
///         .build())
/// });
///
/// if server.create("4242") {
///     println!("Server listening on port 4242");
/// }
/// ```
pub struct IPCServer {
    ptr: *mut ffi::wxd_IPCServer_t,
}

impl IPCServer {
    /// Create a new IPC server with the given OnAcceptConnection callback.
    ///
    /// The callback receives the topic string and should return Some(IPCConnection)
    /// to accept the connection, or None to reject it.
    pub fn new<F>(on_accept_connection: F) -> Self
    where
        F: FnMut(&str) -> Option<IPCConnection> + 'static,
    {
        let callbacks = ServerCallbacks {
            on_accept: Box::new(on_accept_connection),
        };
        let user_data = Box::into_raw(Box::new(callbacks)) as *mut c_void;

        let ptr =
            unsafe { ffi::wxd_IPCServer_Create(user_data, Some(on_accept_connection_trampoline), Some(free_server_callbacks)) };

        Self { ptr }
    }

    /// Start the server listening on the given service.
    ///
    /// The service can be a port number (e.g., "4242") or a Unix socket path.
    /// Returns true if the server started successfully.
    pub fn create(&self, service: &str) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        let c_service = match CString::new(service) {
            Ok(s) => s,
            Err(_) => return false,
        };
        unsafe { ffi::wxd_IPCServer_Create_Service(self.ptr, c_service.as_ptr()) }
    }
}

impl Drop for IPCServer {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::wxd_IPCServer_Destroy(self.ptr) };
        }
    }
}

// =============================================================================
// IPCClient
// =============================================================================

/// An IPC client that connects to servers.
///
/// The client connects to a server on a given host, service, and topic.
/// On Windows, DDE is used (host is ignored for local DDE connections). On Unix/macOS,
/// TCP sockets or Unix domain sockets are used depending on the service format.
/// If the connection is successful, it returns an IPCConnection that can be
/// used to exchange data with the server.
///
/// # Example
///
/// ```rust,no_run
/// use wxdragon::ipc::{IPCClient, IPCFormat};
///
/// let client = IPCClient::new();
///
/// // Connect to a server
/// if let Some(conn) = client.make_connection("localhost", "4242", "test") {
///     // Send a command
///     conn.execute_string("Hello, server!");
///
///     // Request data
///     if let Some(data) = conn.request("status", IPCFormat::Text) {
///         println!("Server status: {}", String::from_utf8_lossy(&data));
///     }
///
///     // Disconnect when done
///     conn.disconnect();
/// }
/// ```
pub struct IPCClient {
    ptr: *mut ffi::wxd_IPCClient_t,
}

impl IPCClient {
    /// Create a new IPC client.
    pub fn new() -> Self {
        let ptr = unsafe { ffi::wxd_IPCClient_Create() };
        Self { ptr }
    }

    /// Connect to a server.
    ///
    /// # Arguments
    ///
    /// * `host` - The hostname or IP address of the server
    /// * `service` - The service (port number or socket path)
    /// * `topic` - The topic to connect to
    ///
    /// Returns Some(IPCConnection) if the connection was successful, None otherwise.
    pub fn make_connection(&self, host: &str, service: &str, topic: &str) -> Option<IPCConnection> {
        self.make_connection_with_callbacks(host, service, topic, IPCConnectionBuilder::new())
    }

    /// Connect to a server with custom callbacks.
    ///
    /// This allows you to specify callbacks for the connection (e.g., OnAdvise
    /// for receiving server-pushed updates).
    pub fn make_connection_with_callbacks(
        &self,
        host: &str,
        service: &str,
        topic: &str,
        builder: IPCConnectionBuilder,
    ) -> Option<IPCConnection> {
        if self.ptr.is_null() {
            return None;
        }

        let c_host = CString::new(host).ok()?;
        let c_service = CString::new(service).ok()?;
        let c_topic = CString::new(topic).ok()?;

        let callbacks_box = Box::new(builder.callbacks);
        let user_data = Box::into_raw(callbacks_box) as *mut c_void;

        let conn_ptr = unsafe {
            ffi::wxd_IPCClient_MakeConnection(
                self.ptr,
                c_host.as_ptr(),
                c_service.as_ptr(),
                c_topic.as_ptr(),
                user_data,
                Some(on_execute_trampoline),
                Some(on_request_trampoline),
                Some(on_poke_trampoline),
                Some(on_start_advise_trampoline),
                Some(on_stop_advise_trampoline),
                Some(on_advise_trampoline),
                Some(on_disconnect_trampoline),
                Some(free_connection_callbacks),
            )
        };

        if conn_ptr.is_null() {
            None
        } else {
            Some(IPCConnection {
                ptr: conn_ptr,
                owned: false, // Owned by the wxWidgets system
            })
        }
    }
}

impl Default for IPCClient {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for IPCClient {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::wxd_IPCClient_Destroy(self.ptr) };
        }
    }
}