1
0
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:
Nicole LeGare 2025-02-13 16:24:01 -08:00
parent 6f272c40ef
commit 088067566a
4 changed files with 33 additions and 9 deletions

View File

@ -18,6 +18,7 @@ rust_binary {
rustlibs: [ rustlibs: [
"com.example.birthdayservice-rust", "com.example.birthdayservice-rust",
"libbinder_rs", "libbinder_rs",
"librpcbinder_rs",
"libbirthdayservice", "libbirthdayservice",
], ],
prefer_rlib: true, // To avoid dynamic link error. prefer_rlib: true, // To avoid dynamic link error.
@ -32,6 +33,7 @@ rust_binary {
rustlibs: [ rustlibs: [
"com.example.birthdayservice-rust", "com.example.birthdayservice-rust",
"libbinder_rs", "libbinder_rs",
"librpcbinder_rs",
], ],
prefer_rlib: true, // To avoid dynamic link error. prefer_rlib: true, // To avoid dynamic link error.
} }

View File

@ -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::aidl::com::example::birthdayservice::IBirthdayService::IBirthdayService;
use com_example_birthdayservice::binder::{self, BinderFeatures, ParcelFileDescriptor}; use com_example_birthdayservice::binder::{self, BinderFeatures, ParcelFileDescriptor};
use rpcbinder::{FileDescriptorTransportMode, RpcSession};
use std::error::Error; use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::os::unix::net::UnixStream;
use std::os::fd::AsRawFd as _;
// ANCHOR: main // ANCHOR: main
const SERVICE_IDENTIFIER: &str = "birthdayservice"; const SERVICE_IDENTIFIER: &str = "/tmp/birthdayservice";
/// Call the birthday service. /// Call the birthday service.
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
@ -34,9 +37,19 @@ fn main() -> Result<(), Box<dyn Error>> {
.and_then(|arg| arg.parse::<i32>().ok()) .and_then(|arg| arg.parse::<i32>().ok())
.unwrap_or(42); .unwrap_or(42);
binder::ProcessState::start_thread_pool(); let stream = UnixStream::connect(SERVICE_IDENTIFIER).expect("Failed to connect UnixStream");
let service = binder::get_interface::<dyn IBirthdayService>(SERVICE_IDENTIFIER) println!("UnixStream connected successfully");
.map_err(|_| "Failed to connect to BirthdayService")?;
// 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. // Call the service.
let msg = service.wishHappyBirthday(&name, years)?; let msg = service.wishHappyBirthday(&name, years)?;

View File

@ -17,8 +17,10 @@
use birthdayservice::BirthdayService; use birthdayservice::BirthdayService;
use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::BnBirthdayService; use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::BnBirthdayService;
use com_example_birthdayservice::binder; 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. /// Entry point for birthday service.
fn main() { fn main() {
@ -27,7 +29,14 @@ fn main() {
birthday_service, birthday_service,
binder::BinderFeatures::default(), binder::BinderFeatures::default(),
); );
binder::add_service(SERVICE_IDENTIFIER, birthday_service_binder.as_binder()) // binder::add_service(SERVICE_IDENTIFIER, birthday_service_binder.as_binder())
.expect("Failed to register service"); // .expect("Failed to register service");
binder::ProcessState::join_thread_pool(); // 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();
} }

View File

@ -12,6 +12,6 @@ java_binary {
name: "helloworld_jni", name: "helloworld_jni",
srcs: ["HelloWorld.java"], srcs: ["HelloWorld.java"],
main_class: "HelloWorld", main_class: "HelloWorld",
required: ["libhello_jni"], jni_libs: ["libhello_jni"],
} }
// ANCHOR_END: helloworld_jni // ANCHOR_END: helloworld_jni