mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-22 10:21:03 +02:00
Do the more complex unix socket setup so we can set the fd transport options
This commit is contained in:
parent
6f272c40ef
commit
088067566a
@ -18,6 +18,7 @@ rust_binary {
|
||||
rustlibs: [
|
||||
"com.example.birthdayservice-rust",
|
||||
"libbinder_rs",
|
||||
"librpcbinder_rs",
|
||||
"libbirthdayservice",
|
||||
],
|
||||
prefer_rlib: true, // To avoid dynamic link error.
|
||||
@ -32,6 +33,7 @@ rust_binary {
|
||||
rustlibs: [
|
||||
"com.example.birthdayservice-rust",
|
||||
"libbinder_rs",
|
||||
"librpcbinder_rs",
|
||||
],
|
||||
prefer_rlib: true, // To avoid dynamic link error.
|
||||
}
|
||||
|
@ -19,12 +19,15 @@ use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayI
|
||||
};
|
||||
use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::IBirthdayService;
|
||||
use com_example_birthdayservice::binder::{self, BinderFeatures, ParcelFileDescriptor};
|
||||
use rpcbinder::{FileDescriptorTransportMode, RpcSession};
|
||||
use std::error::Error;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::os::unix::net::UnixStream;
|
||||
use std::os::fd::AsRawFd as _;
|
||||
|
||||
// ANCHOR: main
|
||||
const SERVICE_IDENTIFIER: &str = "birthdayservice";
|
||||
const SERVICE_IDENTIFIER: &str = "/tmp/birthdayservice";
|
||||
|
||||
/// Call the birthday service.
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
@ -34,9 +37,19 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
.and_then(|arg| arg.parse::<i32>().ok())
|
||||
.unwrap_or(42);
|
||||
|
||||
binder::ProcessState::start_thread_pool();
|
||||
let service = binder::get_interface::<dyn IBirthdayService>(SERVICE_IDENTIFIER)
|
||||
.map_err(|_| "Failed to connect to BirthdayService")?;
|
||||
let stream = UnixStream::connect(SERVICE_IDENTIFIER).expect("Failed to connect UnixStream");
|
||||
println!("UnixStream connected successfully");
|
||||
|
||||
// binder::ProcessState::start_thread_pool();
|
||||
// let service = binder::get_interface::<dyn IBirthdayService>(SERVICE_IDENTIFIER)
|
||||
// .map_err(|_| "Failed to connect to BirthdayService")?;
|
||||
|
||||
let session = RpcSession::new();
|
||||
session.set_file_descriptor_transport_mode(FileDescriptorTransportMode::Unix);
|
||||
let service = session
|
||||
// .setup_unix_domain_client::<dyn IBirthdayService>(SERVICE_IDENTIFIER)
|
||||
.setup_preconnected_client::<dyn IBirthdayService>(|| Some(stream.as_raw_fd()))
|
||||
.map_err(|e| format!("Failed to connect to BirthdayService: {e}"))?;
|
||||
|
||||
// Call the service.
|
||||
let msg = service.wishHappyBirthday(&name, years)?;
|
||||
|
@ -17,8 +17,10 @@
|
||||
use birthdayservice::BirthdayService;
|
||||
use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::BnBirthdayService;
|
||||
use com_example_birthdayservice::binder;
|
||||
use rpcbinder::{FileDescriptorTransportMode, RpcServer};
|
||||
use std::os::unix::net::UnixStream;
|
||||
|
||||
const SERVICE_IDENTIFIER: &str = "birthdayservice";
|
||||
const SERVICE_IDENTIFIER: &str = "/tmp/birthdayservice";
|
||||
|
||||
/// Entry point for birthday service.
|
||||
fn main() {
|
||||
@ -27,7 +29,14 @@ fn main() {
|
||||
birthday_service,
|
||||
binder::BinderFeatures::default(),
|
||||
);
|
||||
binder::add_service(SERVICE_IDENTIFIER, birthday_service_binder.as_binder())
|
||||
.expect("Failed to register service");
|
||||
binder::ProcessState::join_thread_pool();
|
||||
// binder::add_service(SERVICE_IDENTIFIER, birthday_service_binder.as_binder())
|
||||
// .expect("Failed to register service");
|
||||
// binder::ProcessState::join_thread_pool();
|
||||
|
||||
std::fs::write(SERVICE_IDENTIFIER, "").expect("Failed to create socket file");
|
||||
let socket = UnixStream::connect(SERVICE_IDENTIFIER).expect("Failed to create socket");
|
||||
// let socket = UnixListener::bind(SERVICE_IDENTIFIER).expect("Failed to create socket");
|
||||
let server = RpcServer::new_bound_socket(birthday_service_binder.as_binder(), socket.into()).expect("Failed to create server");
|
||||
server.set_supported_file_descriptor_transport_modes(&[FileDescriptorTransportMode::Unix]);
|
||||
server.join();
|
||||
}
|
||||
|
@ -12,6 +12,6 @@ java_binary {
|
||||
name: "helloworld_jni",
|
||||
srcs: ["HelloWorld.java"],
|
||||
main_class: "HelloWorld",
|
||||
required: ["libhello_jni"],
|
||||
jni_libs: ["libhello_jni"],
|
||||
}
|
||||
// ANCHOR_END: helloworld_jni
|
||||
|
Loading…
x
Reference in New Issue
Block a user