Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate connect() in favour of IpcConnector #380

Closed
wants to merge 3 commits into from
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
39 changes: 39 additions & 0 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,13 @@ where
{
/// Create an [IpcSender] connected to a previously defined [IpcOneShotServer].
///
/// This function should not be called more than once per [IpcOneShotServer],
/// otherwise the behaviour is unpredictable.
/// See [issue 378](https://github.com/servo/ipc-channel/issues/378) for details.
///
/// [IpcSender]: struct.IpcSender.html
/// [IpcOneShotServer]: struct.IpcOneShotServer.html
#[deprecated(since = "0.20.0", note = "please use `new_with_connector` instead")]
pub fn connect(name: String) -> Result<IpcSender<T>, io::Error> {
Ok(IpcSender {
os_sender: OsIpcSender::connect(name)?,
Expand Down Expand Up @@ -871,6 +876,7 @@ impl<T> IpcOneShotServer<T>
where
T: for<'de> Deserialize<'de> + Serialize,
{
#[deprecated(since = "0.20.0", note = "please use `new_with_connector` instead")]
pub fn new() -> Result<(IpcOneShotServer<T>, String), io::Error> {
let (os_server, name) = OsIpcOneShotServer::new()?;
Ok((
Expand All @@ -882,6 +888,20 @@ where
))
}

pub fn new_with_connector() -> Result<(IpcOneShotServer<T>, IpcConnector<T>), io::Error> {
let (os_server, name) = OsIpcOneShotServer::new()?;
Ok((
IpcOneShotServer {
os_server,
phantom: PhantomData,
},
IpcConnector {
name,
phantom: PhantomData,
},
))
}

pub fn accept(self) -> Result<(IpcReceiver<T>, T), bincode::Error> {
let (os_receiver, ipc_message) = self.os_server.accept()?;
Ok((
Expand All @@ -894,6 +914,25 @@ where
}
}

/// A means of connecting to an [IpcOneShotServer].
/// [IpcOneShotServer]: struct.IpcOneShotServer.html
pub struct IpcConnector<T> {
name: String,
phantom: PhantomData<T>,
}

impl<T> IpcConnector<T>
where
T: for<'de> Deserialize<'de> + Serialize,
{
pub fn connect(self) -> Result<IpcSender<T>, io::Error> {
Ok(IpcSender {
os_sender: OsIpcSender::connect(self.name)?,
phantom: PhantomData,
})
}
}

/// Receiving end of a channel that does not used serialized messages.
#[derive(Debug)]
pub struct IpcBytesReceiver {
Expand Down
28 changes: 28 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,34 @@ fn cross_process_embedded_senders_fork() {
assert_eq!(received_person, person);
}

#[cfg(not(any(
feature = "force-inprocess",
target_os = "windows",
target_os = "android",
target_os = "ios"
)))]
#[test]
fn cross_process_embedded_senders_fork_with_connector() {
let person = ("Patrick Walton".to_owned(), 29);
let (server0, server0_connector) = IpcOneShotServer::new_with_connector().unwrap();
let (server2, server2_connector) = IpcOneShotServer::new_with_connector().unwrap();
let child_pid = unsafe {
fork(|| {
let (tx1, rx1): (IpcSender<Person>, IpcReceiver<Person>) = ipc::channel().unwrap();
let tx0 = server0_connector.connect().unwrap();
tx0.send(tx1).unwrap();
rx1.recv().unwrap();
let tx2: IpcSender<Person> = server2_connector.connect().unwrap();
tx2.send(person.clone()).unwrap();
})
};
let (_, tx1): (_, IpcSender<Person>) = server0.accept().unwrap();
tx1.send(person.clone()).unwrap();
let (_, received_person): (_, Person) = server2.accept().unwrap();
child_pid.wait();
assert_eq!(received_person, person);
}

#[test]
fn router_simple_global() {
// Note: All ROUTER operation need to run in a single test,
Expand Down
Loading