#ipc #communication #request-response #process #cross #channel #tokio

ipc_channel_adapter

Provides a utility for fast cross process communication

3 releases

0.1.2 May 12, 2024
0.1.1 May 11, 2024
0.1.0 May 11, 2024

#15 in #cross

Download history 1/week @ 2024-07-29 78/week @ 2024-08-05 7/week @ 2024-08-26 42/week @ 2024-09-23 5/week @ 2024-09-30 1/week @ 2024-10-14 85/week @ 2024-11-11

85 downloads per month
Used in mach_bundler_core

MIT license

30KB
742 lines

IPC Channel Adapter

This utility wraps ipc-channel to handle the handshake and offers with standard channels.

This utility also offers a request/response wrapper for messages

It supports Tokio for non-blocking operation

Usage

Parent Process

fn main() {
  // Send requests to child
  //   Request to send to child is u32 
  //   Response coming back from child u64 
  let child_sender = ChildSender::<u32, u64>::new().unwrap();

  // Receive requests from child
  //   Request coming from child is u32 
  //   Response to send back to child is u64 
  let (child_receiver, child_rx) = ChildReceiver::<u32, u64>::new().unwrap();

  // Spawn child process and give it the names of the IPC channels
  let child_process = spawn_child_process("your_child_process")
    .env("IPC_RECEIVER_NAME", child_receiver.server_name)
    .env("IPC_SENDER_NAME", child_sender.server_name)
    .spawn();

  thread::spawn(move || {
    while let Ok((v, reply)) = child_rx.recv() {
      println!("[Host] Received: {}", v);
      reply.send(v).unwrap()
    }
  });

  let response = child_sender.send_blocking(42);
  println!("[Host] Response: {}", response);
}

Child Process

fn main() {
  // Get name of IPC servers
  let host_receiver_server = env::var("IPC_RECEIVER_NAME").unwrap();
  let host_sender_server = env::var("IPC_SENDER_NAME").unwrap();
  
  // Send requests to host
  //   Request to send to host is u32 
  //   Response coming back from host u64 
  let host_sender = HostSender::<u32, u64>::new(&host_receiver_server).unwrap();

  // Receive requests from host
  //   Request coming from host is u32 
  //   Response to send back to host is u64 
  let (_host_receiver, host_receiver_rx) = HostReceiver::<u32, u64>::new(&host_sender_server).unwrap();

  thread::spawn(move || {
    while let Ok((v, reply)) = host_receiver_rx.recv() {
      println!("[Child] Received: {}", v);
      reply.send(v).unwrap()
    }
  });

  let response = host_sender.send_blocking(43);
  println!("[Child] Response: {}", response);
}

Dependencies

~3–31MB
~507K SLoC