Binder C + + class

Source: Internet
Author: User

Binder

Binder as a mechanism for IPC in Android, is used everywhere in Android. Contains three layers (Java, c++/c, driver).
The purpose of binder is to implement communication between multiple processes, with data passed between the basic data type, binder type, and this wrapper in parcel.
In the C + + tier, use multiple classes to encapsulate:

The main classes are:

    1. RefBaseAbout reference counting
    2. ParcelThis is the data container for the transfer and also supports the ##### #IBinder对象 ##### #的引用.
    3. IBinderBinder object interface with transact pure virtual function
    4. BpBinderBinder Reference Object, present BpRefBase in, generated by binder driver
    5. BBinderBinder's Entity Object
    6. IInterfaceInterface
    7. BnInterfaceTemplate class, inherited by Ixx and Bbinder
    8. BpInterfaceTemplate class, inherited by Ixx and Bprefbase
    9. IPCThreadState
    10. ProcessStateAfter the class is generated, the binder driver opens.

In C + + to write binder applications, to have a client code and server-side code, one of my applications, the main code is:

Client
  
 
  1. int main(int argc, char** argv)
  2. {
  3. LOGI("binderclient");
  4. printf("binderclient --\n");
  5. int sum = 0;
  6. sp mTestBinserService;
  7. if (mTestBinserService.get() == 0) {
  8. sp sm = defaultServiceManager();
  9. sp binder;
  10. do {
  11. binder = sm->getService(String16("my.test.binder"));
  12. if (binder != 0)
  13. break;
  14. LOGI("getService fail");
  15. usleep(500000); // 0.5 s
  16. } while (true);
  17. mTestBinserService = interface_cast (binder);
  18. LOGE_IF(mTestBinserService == 0, "no ITestBinserService!?");
  19. }
  20. sum = mTestBinserService->add(3, 4);
  21. LOGI("sum = %d", sum);
  22. return 0;
  23. }
Service side
  
 
  1. int main(int argc, char** argv)
  2. {
  3. sp proc(ProcessState::self());
  4. sp sm = defaultServiceManager();
  5. LOGI("TestBinderService before");
  6. //TestBinderService::instantiate();
  7. sm->addService(String16("my.test.binder", new TestBinderService());//TestBinderService继承于B你Interface, 也就是继承于ITestBinderService和BBinder.
  8. LOGI("TestBinderService End");
  9. ProcessState::self()->startThreadPool();
  10. IPCThreadState::self()->joinThreadPool();
  11. LOGI("TestBinderService End");
  12. return 0;
  13. }
Inter-process callbacks

Using binders allows you to implement callbacks between processes. The IBinder interface is available as a function parameter.



From for notes (Wiz)

Binder C + + class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.