ipc-channel 0.21.0

A multiprocess drop-in replacement for Rust channels
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
// Copyright 2015 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::ipc::IpcMessage;
use crossbeam_channel::{self, Receiver, RecvTimeoutError, Select, Sender, TryRecvError};
use std::cell::{Ref, RefCell};
use std::cmp::PartialEq;
use std::collections::hash_map::HashMap;
use std::fmt::{self, Debug, Formatter};
use std::io;
use std::ops::{Deref, RangeFrom};
use std::slice;
use std::sync::{Arc, LazyLock, Mutex};
use std::time::Duration;
use thiserror::Error;
use uuid::Uuid;

#[derive(Debug, Error)]
pub enum OsTrySelectError {
    #[error("Error in IO: {0}.")]
    IoError(#[from] ChannelError),
    #[error("No messages were received and no disconnections occurred.")]
    Empty,
}

#[derive(Clone)]
struct ServerRecord {
    sender: OsIpcSender,
    conn_sender: Sender<bool>,
    conn_receiver: Receiver<bool>,
}

impl ServerRecord {
    fn new(sender: OsIpcSender) -> ServerRecord {
        let (tx, rx) = crossbeam_channel::unbounded::<bool>();
        ServerRecord {
            sender,
            conn_sender: tx,
            conn_receiver: rx,
        }
    }

    fn accept(&self) {
        self.conn_receiver.recv().unwrap();
    }

    fn connect(&self) {
        self.conn_sender.send(true).unwrap();
    }
}

static ONE_SHOT_SERVERS: LazyLock<Mutex<HashMap<String, ServerRecord>>> =
    LazyLock::new(|| Mutex::new(HashMap::new()));

struct ChannelMessage(IpcMessage);

pub fn channel() -> Result<(OsIpcSender, OsIpcReceiver), ChannelError> {
    let (base_sender, base_receiver) = crossbeam_channel::unbounded::<ChannelMessage>();
    Ok((
        OsIpcSender::new(base_sender),
        OsIpcReceiver::new(base_receiver),
    ))
}

#[derive(Debug)]
pub struct OsIpcReceiver {
    receiver: RefCell<Option<crossbeam_channel::Receiver<ChannelMessage>>>,
}

impl OsIpcReceiver {
    fn new(receiver: Receiver<ChannelMessage>) -> OsIpcReceiver {
        OsIpcReceiver {
            receiver: RefCell::new(Some(receiver)),
        }
    }

    pub fn consume(&self) -> OsIpcReceiver {
        OsIpcReceiver {
            receiver: RefCell::new(self.receiver.borrow_mut().take()),
        }
    }

    pub fn recv(&self) -> Result<IpcMessage, ChannelError> {
        let r = self.receiver.borrow();
        let r = r.as_ref().unwrap();
        match r.recv() {
            Ok(ChannelMessage(ipc_message)) => Ok(ipc_message),
            Err(_) => Err(ChannelError::ChannelClosedError),
        }
    }

    pub fn try_recv(&self) -> Result<IpcMessage, ChannelError> {
        let r = self.receiver.borrow();
        let r = r.as_ref().unwrap();
        match r.try_recv() {
            Ok(ChannelMessage(ipc_message)) => Ok(ipc_message),
            Err(e) => match e {
                TryRecvError::Empty => Err(ChannelError::ChannelEmpty),
                TryRecvError::Disconnected => Err(ChannelError::ChannelClosedError),
            },
        }
    }

    pub fn try_recv_timeout(&self, duration: Duration) -> Result<IpcMessage, ChannelError> {
        let r = self.receiver.borrow();
        let r = r.as_ref().unwrap();
        match r.recv_timeout(duration) {
            Ok(ChannelMessage(ipc_message)) => Ok(ipc_message),
            Err(e) => match e {
                RecvTimeoutError::Timeout => Err(ChannelError::ChannelEmpty),
                RecvTimeoutError::Disconnected => Err(ChannelError::ChannelClosedError),
            },
        }
    }
}

#[derive(Clone, Debug)]
pub struct OsIpcSender {
    sender: Sender<ChannelMessage>,
}

impl OsIpcSender {
    fn new(sender: Sender<ChannelMessage>) -> OsIpcSender {
        OsIpcSender { sender }
    }

    pub fn connect(name: String) -> Result<OsIpcSender, ChannelError> {
        let record = ONE_SHOT_SERVERS.lock().unwrap().get(&name).unwrap().clone();
        record.connect();
        Ok(record.sender)
    }

    pub fn get_max_fragment_size() -> usize {
        usize::MAX
    }

    pub fn send(
        &self,
        data: &[u8],
        ports: Vec<OsIpcChannel>,
        shared_memory_regions: Vec<OsIpcSharedMemory>,
    ) -> Result<(), ChannelError> {
        let os_ipc_channels = ports.into_iter().map(OsOpaqueIpcChannel::new).collect();
        let ipc_message = IpcMessage::new(data.to_vec(), os_ipc_channels, shared_memory_regions);
        self.sender
            .send(ChannelMessage(ipc_message))
            .map_err(|_| ChannelError::BrokenPipeError)
    }
}

pub struct OsIpcReceiverSet {
    incrementor: RangeFrom<u64>,
    receiver_ids: Vec<u64>,
    receivers: Vec<OsIpcReceiver>,
}

impl OsIpcReceiverSet {
    pub fn new() -> Result<OsIpcReceiverSet, ChannelError> {
        Ok(OsIpcReceiverSet {
            incrementor: 0..,
            receiver_ids: vec![],
            receivers: vec![],
        })
    }

    pub fn add(&mut self, receiver: OsIpcReceiver) -> Result<u64, ChannelError> {
        let last_index = self.incrementor.next().unwrap();
        self.receiver_ids.push(last_index);
        self.receivers.push(receiver.consume());
        Ok(last_index)
    }

    pub fn select(&mut self) -> Result<Vec<OsIpcSelectionResult>, ChannelError> {
        if self.receivers.is_empty() {
            return Err(ChannelError::UnknownError);
        }

        struct Remove(usize, u64);

        // FIXME: Remove early returns and explicitly drop `borrows` when lifetimes are non-lexical
        let Remove(r_index, r_id) = {
            let borrows: Vec<_> = self
                .receivers
                .iter()
                .map(|r| Ref::map(r.receiver.borrow(), |o| o.as_ref().unwrap()))
                .collect();

            let mut select = Select::new();
            for r in &borrows {
                select.recv(r);
            }
            let res = select.select();
            let receiver_index = res.index();
            let receiver_id = self.receiver_ids[receiver_index];
            if let Ok(ChannelMessage(ipc_message)) = res.recv(&borrows[receiver_index]) {
                return Ok(vec![OsIpcSelectionResult::DataReceived(
                    receiver_id,
                    ipc_message,
                )]);
            } else {
                Remove(receiver_index, receiver_id)
            }
        };
        self.receivers.remove(r_index);
        self.receiver_ids.remove(r_index);
        Ok(vec![OsIpcSelectionResult::ChannelClosed(r_id)])
    }

    pub fn try_select(&mut self) -> Result<Vec<OsIpcSelectionResult>, OsTrySelectError> {
        if self.receivers.is_empty() {
            return Err(OsTrySelectError::Empty);
        }

        struct Remove(usize, u64);

        // FIXME: Remove early returns and explicitly drop `borrows` when lifetimes are non-lexical
        let Remove(r_index, r_id) = {
            let borrows: Vec<_> = self
                .receivers
                .iter()
                .map(|r| Ref::map(r.receiver.borrow(), |o| o.as_ref().unwrap()))
                .collect();

            let mut select = Select::new();
            for r in &borrows {
                select.recv(r);
            }
            let res = select.try_select();
            if res.is_err() {
                return Err(OsTrySelectError::Empty);
            }
            let res = res.unwrap();
            let receiver_index = res.index();
            let receiver_id = self.receiver_ids[receiver_index];
            if let Ok(ChannelMessage(ipc_message)) = res.recv(&borrows[receiver_index]) {
                return Ok(vec![OsIpcSelectionResult::DataReceived(
                    receiver_id,
                    ipc_message,
                )]);
            } else {
                Remove(receiver_index, receiver_id)
            }
        };
        self.receivers.remove(r_index);
        self.receiver_ids.remove(r_index);
        Ok(vec![OsIpcSelectionResult::ChannelClosed(r_id)])
    }

    pub fn try_select_timeout(
        &mut self,
        duration: Duration,
    ) -> Result<Vec<OsIpcSelectionResult>, OsTrySelectError> {
        if self.receivers.is_empty() {
            std::thread::sleep(duration);
            // The set is still empty since the current method is &mut self amd receivers
            // does not have interior mutability.
            return Err(OsTrySelectError::Empty);
        }

        struct Remove(usize, u64);

        // FIXME: Remove early returns and explicitly drop `borrows` when lifetimes are non-lexical
        let Remove(r_index, r_id) = {
            let borrows: Vec<_> = self
                .receivers
                .iter()
                .map(|r| Ref::map(r.receiver.borrow(), |o| o.as_ref().unwrap()))
                .collect();

            let mut select = Select::new();
            for r in &borrows {
                select.recv(r);
            }
            let res = select.select_timeout(duration);
            if res.is_err() {
                return Err(OsTrySelectError::Empty);
            }
            let res = res.unwrap();
            let receiver_index = res.index();
            let receiver_id = self.receiver_ids[receiver_index];
            if let Ok(ChannelMessage(ipc_message)) = res.recv(&borrows[receiver_index]) {
                return Ok(vec![OsIpcSelectionResult::DataReceived(
                    receiver_id,
                    ipc_message,
                )]);
            } else {
                Remove(receiver_index, receiver_id)
            }
        };
        self.receivers.remove(r_index);
        self.receiver_ids.remove(r_index);
        Ok(vec![OsIpcSelectionResult::ChannelClosed(r_id)])
    }
}

pub enum OsIpcSelectionResult {
    DataReceived(u64, IpcMessage),
    ChannelClosed(u64),
}

impl OsIpcSelectionResult {
    pub fn unwrap(self) -> (u64, IpcMessage) {
        match self {
            OsIpcSelectionResult::DataReceived(id, ipc_message) => (id, ipc_message),
            OsIpcSelectionResult::ChannelClosed(id) => {
                panic!("OsIpcSelectionResult::unwrap(): receiver ID {id} was closed!")
            },
        }
    }
}

pub struct OsIpcOneShotServer {
    receiver: OsIpcReceiver,
    name: String,
}

impl OsIpcOneShotServer {
    pub fn new() -> Result<(OsIpcOneShotServer, String), ChannelError> {
        let (sender, receiver) = channel()?;

        let name = Uuid::new_v4().to_string();
        let record = ServerRecord::new(sender);
        ONE_SHOT_SERVERS
            .lock()
            .unwrap()
            .insert(name.clone(), record);
        Ok((
            OsIpcOneShotServer {
                receiver,
                name: name.clone(),
            },
            name.clone(),
        ))
    }

    pub fn accept(self) -> Result<(OsIpcReceiver, IpcMessage), ChannelError> {
        let record = ONE_SHOT_SERVERS
            .lock()
            .unwrap()
            .get(&self.name)
            .unwrap()
            .clone();
        record.accept();
        ONE_SHOT_SERVERS.lock().unwrap().remove(&self.name).unwrap();
        let ipc_message = self.receiver.recv()?;
        Ok((self.receiver, ipc_message))
    }
}

#[derive(Debug)]
pub enum OsIpcChannel {
    Sender(OsIpcSender),
    Receiver(OsIpcReceiver),
}

#[derive(Debug)]
pub struct OsOpaqueIpcChannel {
    channel: RefCell<Option<OsIpcChannel>>,
}

impl OsOpaqueIpcChannel {
    fn new(channel: OsIpcChannel) -> OsOpaqueIpcChannel {
        OsOpaqueIpcChannel {
            channel: RefCell::new(Some(channel)),
        }
    }

    pub fn to_receiver(&self) -> OsIpcReceiver {
        match self.channel.borrow_mut().take().unwrap() {
            OsIpcChannel::Sender(_) => panic!("Opaque channel is not a receiver!"),
            OsIpcChannel::Receiver(r) => r,
        }
    }

    pub fn to_sender(&mut self) -> OsIpcSender {
        match self.channel.borrow_mut().take().unwrap() {
            OsIpcChannel::Sender(s) => s,
            OsIpcChannel::Receiver(_) => panic!("Opaque channel is not a sender!"),
        }
    }
}

pub struct OsIpcSharedMemory {
    ptr: *mut u8,
    length: usize,
    data: Arc<Vec<u8>>,
}

unsafe impl Send for OsIpcSharedMemory {}
unsafe impl Sync for OsIpcSharedMemory {}

impl Clone for OsIpcSharedMemory {
    fn clone(&self) -> OsIpcSharedMemory {
        OsIpcSharedMemory {
            ptr: self.ptr,
            length: self.length,
            data: self.data.clone(),
        }
    }
}

impl PartialEq for OsIpcSharedMemory {
    fn eq(&self, other: &OsIpcSharedMemory) -> bool {
        **self == **other
    }
}

impl Debug for OsIpcSharedMemory {
    fn fmt(&self, formatter: &mut Formatter) -> Result<(), fmt::Error> {
        (**self).fmt(formatter)
    }
}

impl Deref for OsIpcSharedMemory {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &[u8] {
        if self.ptr.is_null() {
            panic!("attempted to access a consumed `OsIpcSharedMemory`")
        }
        unsafe { slice::from_raw_parts(self.ptr, self.length) }
    }
}

impl OsIpcSharedMemory {
    /// # Safety
    ///
    /// This is safe if there is only one reader/writer on the data.
    /// User can achieve this by not cloning [`IpcSharedMemory`]
    /// and serializing/deserializing only once.
    #[inline]
    pub unsafe fn deref_mut(&mut self) -> &mut [u8] {
        if self.ptr.is_null() {
            panic!("attempted to access a consumed `OsIpcSharedMemory`")
        }
        unsafe { slice::from_raw_parts_mut(self.ptr, self.length) }
    }

    pub fn take(self) -> Option<Vec<u8>> {
        Arc::into_inner(self.data)
    }
}

impl OsIpcSharedMemory {
    pub fn from_byte(byte: u8, length: usize) -> OsIpcSharedMemory {
        let mut v = Arc::new(vec![byte; length]);
        OsIpcSharedMemory {
            ptr: Arc::get_mut(&mut v).unwrap().as_mut_ptr(),
            length,
            data: v,
        }
    }

    pub fn from_bytes(bytes: &[u8]) -> OsIpcSharedMemory {
        let mut v = Arc::new(bytes.to_vec());
        OsIpcSharedMemory {
            ptr: Arc::get_mut(&mut v).unwrap().as_mut_ptr(),
            length: v.len(),
            data: v,
        }
    }
}

#[derive(Debug, PartialEq, Error)]
pub enum ChannelError {
    #[error("Channel Closed.")]
    ChannelClosedError,
    #[error("Broken Pipe.")]
    BrokenPipeError,
    #[error("Channel Empty.")]
    ChannelEmpty,
    #[error("Unknown Error.")]
    UnknownError,
}

impl ChannelError {
    #[allow(dead_code)]
    pub fn channel_is_closed(&self) -> bool {
        *self == ChannelError::ChannelClosedError
    }
}

impl From<ChannelError> for crate::IpcError {
    fn from(error: ChannelError) -> Self {
        match error {
            ChannelError::ChannelClosedError => crate::IpcError::Disconnected,
            e => crate::IpcError::Io(io::Error::from(e)),
        }
    }
}

impl From<ChannelError> for crate::TryRecvError {
    fn from(error: ChannelError) -> Self {
        match error {
            ChannelError::ChannelClosedError => {
                crate::TryRecvError::IpcError(crate::IpcError::Disconnected)
            },
            ChannelError::ChannelEmpty => crate::TryRecvError::Empty,
            e => crate::TryRecvError::IpcError(crate::IpcError::Io(io::Error::from(e))),
        }
    }
}

impl From<ChannelError> for io::Error {
    fn from(crossbeam_error: ChannelError) -> io::Error {
        match crossbeam_error {
            ChannelError::ChannelClosedError => io::Error::new(
                io::ErrorKind::ConnectionReset,
                "crossbeam-channel sender closed",
            ),
            ChannelError::ChannelEmpty => io::Error::new(
                io::ErrorKind::ConnectionReset,
                "crossbeam-channel receiver has no received messages",
            ),
            ChannelError::BrokenPipeError => io::Error::new(
                io::ErrorKind::BrokenPipe,
                "crossbeam-channel receiver closed",
            ),
            ChannelError::UnknownError => io::Error::other("Other crossbeam-channel error"),
        }
    }
}