ipc_channel_adapter

Crates.ioipc_channel_adapter
lib.rsipc_channel_adapter
version0.1.2
sourcesrc
created_at2024-05-11 08:28:47.80786
updated_at2024-05-12 08:47:40.146145
descriptionProvides a utility for fast cross process communication
homepage
repository
max_upload_size
id1236736
size29,072
David Alsh (alshdavid)

documentation

README

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);
}
Commit count: 0

cargo fmt