You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use capnproto to write code, but I'm having trouble exchanging messages between multiple processes using capnproto.
In this case, is it correct to create the client of the concat server with bootstrap in the input function? Or is it better to create the client in the main function and pass it to the ServerImpl?
api.capnp
interface API {
struct Request {
first @0 :Text;
second @1 :Text;
}
struct Reply {
message @0 :Text;
}
input@0 (request: Request) -> (reply: Reply);
}
pub mod api_capnp {
include!(concat!(env!("OUT_DIR"), "/api_capnp.rs"));
}
pub mod concat_capnp {
include!(concat!(env!("OUT_DIR"), "/concat_capnp.rs"));
}
// ==============================
use crate::api_capnp::a_p_i;
use crate::concat_capnp::concat;
use capnp::capability::Promise;
use capnp_rpc::{RpcSystem, pry, rpc_twoparty_capnp, twoparty};
use capnp_rpc::new_client;
use capnp_rpc::rpc_twoparty_capnp::Side;
use futures::AsyncReadExt;
use std::path::Path;
use std::sync::Arc;
use tokio::fs;
use tokio::net::UnixListener;
use tokio::net::UnixStream;
// ==============================
struct ServerImpl {}
impl a_p_i::Server for ServerImpl {
fn input(
&mut self,
params: a_p_i::InputParams,
mut results: a_p_i::InputResults,
) -> Promise<(), ::capnp::Error> {
//
let request = pry!(pry!(params.get()).get_request());
let first = pry!(pry!(request.get_first()).to_str());
let first = first.to_string();
let second = pry!(pry!(request.get_second()).to_str());
let second = second.to_string();
// I want to pass first and second to the concat.capnp server and get the combined string.
let socket_path = "/tmp/concat.sock";
let s = ~~~~~
results.get().set_result(&s);
Promise::<(), ::capnp::Error>::from_future(async move { Ok(()) })
}
}
pub async fn server_main() -> Result<(), Box<dyn std::error::Error>> {
let socket_path = "/tmp/api.sock";
// 既にソケットが存在していたら削除
if Path::new(socket_path).exists() {
fs::remove_file(socket_path).await?;
}
let listener = UnixListener::bind(socket_path)?;
let api_client: a_p_i::Client = capnp_rpc::new_client(ServerImpl {});
tokio::task::LocalSet::new()
.run_until(async move {
loop {
println!("Listening on {}", socket_path);
let (stream, _) = listener.accept().await?;
let (reader, writer) =
tokio_util::compat::TokioAsyncReadCompatExt::compat(stream).split();
let network = twoparty::VatNetwork::new(
futures::io::BufReader::new(reader),
futures::io::BufWriter::new(writer),
rpc_twoparty_capnp::Side::Server,
Default::default(),
);
let rpc_system = RpcSystem::new(Box::new(network), Some(api_client.clone().client));
tokio::task::spawn_local(rpc_system);
}
})
.await
}
// ==============================
#[tokio::main]
async fn main() {
println!("Hello, world!");
server_main().await.expect("REASON")
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I want to use capnproto to write code, but I'm having trouble exchanging messages between multiple processes using capnproto.
In this case, is it correct to create the client of the concat server with bootstrap in the input function? Or is it better to create the client in the main function and pass it to the ServerImpl?
Beta Was this translation helpful? Give feedback.
All reactions