Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ipc-channel"
version = "0.20.1"
version = "0.21.0"
description = "A multiprocess drop-in replacement for Rust channels"
authors = ["The Servo Project Developers"]
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -31,13 +31,14 @@ win32-trace = []
enable-slow-tests = []

[dependencies]
bincode = "1"
bitcode = { version = "0.6.6", features = ["serde"] }
crossbeam-channel = "0.5"
fnv = "1.0.3"
futures-channel = { version = "0.3.31", optional = true }
futures-core = { version = "0.3.31", optional = true }
libc = "0.2.162"
serde = { version = "1.0", features = ["rc"] }
thiserror = "2.0.12"
uuid = { version = "1", features = ["v4"] }

[target.'cfg(any(target_os = "linux", target_os = "openbsd", target_os = "freebsd", target_os = "illumos"))'.dependencies]
Expand Down
3 changes: 2 additions & 1 deletion src/asynch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use crate::ipc::{
self, IpcMessage, IpcReceiver, IpcReceiverSet, IpcSelectionResult, IpcSender, OpaqueIpcReceiver,
};
use crate::IpcError;
use futures_channel::mpsc::UnboundedReceiver;
use futures_channel::mpsc::UnboundedSender;
use futures_core::stream::FusedStream;
Expand Down Expand Up @@ -96,7 +97,7 @@ impl<T> Stream for IpcStream<T>
where
T: for<'de> Deserialize<'de> + Serialize,
{
type Item = Result<T, bincode::Error>;
type Item = Result<T, IpcError>;

fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> {
let recv = Pin::new(&mut self.0);
Expand Down
32 changes: 32 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::fmt::Display;
use std::io;

use thiserror::Error;

#[derive(Debug, Error)]
/// An error that occurs for serialization or deserialization
pub struct SerializationError(#[from] pub(crate) bitcode::Error);

impl Display for SerializationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Serialization error")
}
}

#[derive(Debug, Error)]
pub enum IpcError {
#[error("Error in decoding or encoding: {0}")]
SerializationError(#[from] SerializationError),
#[error("Error in IO: {0}")]
Io(#[from] io::Error),
#[error("Ipc Disconnected")]
Disconnected,
}

#[derive(Debug, Error)]
pub enum TryRecvError {
#[error("IPC error")]
IpcError(#[from] IpcError),
#[error("Channel empty")]
Empty,
}
76 changes: 15 additions & 61 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::error::SerializationError;
use crate::platform::{self, OsIpcChannel, OsIpcReceiver, OsIpcReceiverSet, OsIpcSender};
use crate::platform::{
OsIpcOneShotServer, OsIpcSelectionResult, OsIpcSharedMemory, OsOpaqueIpcChannel,
};
use crate::{IpcError, TryRecvError};

use bincode;
use bitcode;
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
use std::cell::RefCell;
use std::cmp::min;
use std::error::Error as StdError;
use std::fmt::{self, Debug, Formatter};
use std::io;
use std::marker::PhantomData;
Expand All @@ -37,57 +38,6 @@ thread_local! {
const { RefCell::new(Vec::new()) }
}

#[derive(Debug)]
pub enum IpcError {
Bincode(bincode::Error),
Io(io::Error),
Disconnected,
}

impl fmt::Display for IpcError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
IpcError::Bincode(ref err) => write!(fmt, "bincode error: {err}"),
IpcError::Io(ref err) => write!(fmt, "io error: {err}"),
IpcError::Disconnected => write!(fmt, "disconnected"),
}
}
}

impl StdError for IpcError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match *self {
IpcError::Bincode(ref err) => Some(err),
IpcError::Io(ref err) => Some(err),
IpcError::Disconnected => None,
}
}
}

#[derive(Debug)]
pub enum TryRecvError {
IpcError(IpcError),
Empty,
}

impl fmt::Display for TryRecvError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
TryRecvError::IpcError(ref err) => write!(fmt, "ipc error: {err}"),
TryRecvError::Empty => write!(fmt, "empty"),
}
}
}

impl StdError for TryRecvError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match *self {
TryRecvError::IpcError(ref err) => Some(err),
TryRecvError::Empty => None,
}
}
}

/// Create a connected [IpcSender] and [IpcReceiver] that
/// transfer messages of a given type provided by type `T`
/// or inferred by the types of messages sent by the sender.
Expand Down Expand Up @@ -248,15 +198,18 @@ where
{
/// Blocking receive.
pub fn recv(&self) -> Result<T, IpcError> {
self.os_receiver.recv()?.to().map_err(IpcError::Bincode)
self.os_receiver
.recv()?
.to()
.map_err(IpcError::SerializationError)
}

/// Non-blocking receive
pub fn try_recv(&self) -> Result<T, TryRecvError> {
self.os_receiver
.try_recv()?
.to()
.map_err(IpcError::Bincode)
.map_err(IpcError::SerializationError)
.map_err(TryRecvError::IpcError)
}

Expand All @@ -270,7 +223,7 @@ where
self.os_receiver
.try_recv_timeout(duration)?
.to()
.map_err(IpcError::Bincode)
.map_err(IpcError::SerializationError)
.map_err(TryRecvError::IpcError)
}

Expand Down Expand Up @@ -364,7 +317,7 @@ where
}

/// Send data across the channel to the receiver.
pub fn send(&self, data: T) -> Result<(), bincode::Error> {
pub fn send(&self, data: T) -> Result<(), IpcError> {
let mut bytes = Vec::with_capacity(4096);
OS_IPC_CHANNELS_FOR_SERIALIZATION.with(|os_ipc_channels_for_serialization| {
OS_IPC_SHARED_MEMORY_REGIONS_FOR_SERIALIZATION.with(
Expand All @@ -377,7 +330,8 @@ where
let os_ipc_shared_memory_regions;
let os_ipc_channels;
{
bincode::serialize_into(&mut bytes, &data)?;
bytes = bitcode::serialize(&data).map_err(SerializationError)?;

os_ipc_channels = mem::replace(
&mut *os_ipc_channels_for_serialization.borrow_mut(),
old_os_ipc_channels,
Expand Down Expand Up @@ -726,7 +680,7 @@ impl IpcMessage {
}

/// Deserialize the raw data in the contained message into the inferred type.
pub fn to<T>(mut self) -> Result<T, bincode::Error>
pub fn to<T>(mut self) -> Result<T, SerializationError>
where
T: for<'de> Deserialize<'de> + Serialize,
{
Expand All @@ -744,7 +698,7 @@ impl IpcMessage {
.map(Some)
.collect(),
);
let result = bincode::deserialize(&self.data[..]);
let result = bitcode::deserialize(&self.data[..]).map_err(|e| e.into());
*os_ipc_shared_memory_regions_for_deserialization.borrow_mut() =
old_ipc_shared_memory_regions_for_deserialization;
mem::swap(
Expand Down Expand Up @@ -886,7 +840,7 @@ where
))
}

pub fn accept(self) -> Result<(IpcReceiver<T>, T), bincode::Error> {
pub fn accept(self) -> Result<(IpcReceiver<T>, T), IpcError> {
let (os_receiver, ipc_message) = self.os_server.accept()?;
Ok((
IpcReceiver {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ pub mod asynch;
#[cfg(all(not(feature = "force-inprocess"), target_os = "windows"))]
extern crate windows;

mod error;
pub mod ipc;
pub mod platform;
pub mod router;

#[cfg(test)]
mod test;

pub use bincode::{Error, ErrorKind};
pub use error::{IpcError, TryRecvError};
44 changes: 14 additions & 30 deletions src/platform/inprocess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::ipc::{self, IpcMessage};
use bincode;
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::error::Error as StdError;
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 std::usize;
use thiserror::Error;
use uuid::Uuid;

#[derive(Clone)]
Expand Down Expand Up @@ -387,11 +386,15 @@ impl OsIpcSharedMemory {
}
}

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

Expand All @@ -402,42 +405,23 @@ impl ChannelError {
}
}

impl fmt::Display for ChannelError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
ChannelError::ChannelClosedError => write!(fmt, "channel closed"),
ChannelError::BrokenPipeError => write!(fmt, "broken pipe"),
ChannelError::ChannelEmpty => write!(fmt, "channel empty"),
ChannelError::UnknownError => write!(fmt, "unknown error"),
}
}
}

impl StdError for ChannelError {}

impl From<ChannelError> for bincode::Error {
fn from(crossbeam_error: ChannelError) -> Self {
io::Error::from(crossbeam_error).into()
}
}

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

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