AIDL 服务器
最后,我们可以创建一个暴露服务的服务器:
birthday_service/src/server.rs:
1. //! Birthday service.
2. use birthdayservice::BirthdayService;
3. use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::BnBirthdayService;
4. use com_example_birthdayservice::binder;
6. const SERVICE_IDENTIFIER: &str = "birthdayservice";
8. /// Entry point for birthday service.
9. fn main() {
10. let birthday_service = BirthdayService;
11. let birthday_service_binder = BnBirthdayService::new_binder(
12. birthday_service,
13. binder::BinderFeatures::default(),
14. );
15. binder::add_service(SERVICE_IDENTIFIER, birthday_service_binder.as_binder())
16. .expect("Failed to register service");
17. binder::ProcessState::join_thread_pool()
18. }
birthday_service/Android.bp:
1. rust_binary {
2. name: "birthday_server",
3. crate_name: "birthday_server",
4. srcs: ["src/server.rs"],
5. rustlibs: [
6. "com.example.birthdayservice-rust",
7. "libbinder_rs",
8. "libbirthdayservice",
9. ],
10. prefer_rlib: true, // To avoid dynamic link error.
11. }
The process for taking a user-defined service implementation (in this case the BirthdayService type, which implements the IBirthdayService) and starting it as a Binder service has multiple steps, and may appear more complicated than students are used to if they’ve used Binder from C++ or another language. Explain to students why each step is necessary.
- Create an instance of your service type (
BirthdayService). - Wrap the service object in corresponding
Bn*type (BnBirthdayServicein this case). This type is generated by Binder and provides the common Binder functionality that would be provided by theBnBinderbase class in C++. We don’t have inheritance in Rust, so instead we use composition, putting ourBirthdayServicewithin the generatedBnBinderService. - Call
add_service, giving it a service identifier and your service object (theBnBirthdayServiceobject in the example). - Call
join_thread_poolto add the current thread to Binder’s thread pool and start listening for connections.
