9.7 Binder System _c++ Implementation _ internal mechanism 1

Source: Internet
Author: User
Tags mremote

1. Internal mechanism review Binder framework key points

Binder Process Communication Process scenario examples:

Test_server through AddService to Service_manager registration service

Test_client access to Service_manager through GetService

Then test_client use the service and Test_server communication

The access to the driver involved in the above communication process:

(1) Registration service process Add_service

A, test_server constructs a flat_binder_object structure for each service, different binders and cookies for different service structures

B. Call the IOCTL to send data (data includes: flat_binder_object, service name, who is sent to (handle = 0, for Service_manager))

C, the driver for each flat_binder_object for the test_server process new Binder_node node, the node's PTR and cookie parameter values to Flat_binder_ Binder and cookie for object struct

D, driver according to Handle=0, find Service_manager, send data to it, and create BINDER_REF structure for Service_manager process, and according to Binder_ The order in which ref is created sets its desc parameter and node points to the corresponding Binder_node

E, Service_manager, the name of the record service and the DESC value, in the Service_manager application has a svclist linked list, each node of the linked list holds the name of the service and the handle value (whose value is equal to DESC)

(2) Get the service process Get_service

A, test_client construction data (data: Service name, purpose (handle = 0))

B. Call the IOCTL to send data

C, driver according to Handle=0, find Service_manager, send the data to it

D, Service_manager from the svclist according to the name of the service to find the corresponding item, take out the corresponding node handle

E, Service_manager return data via IOCTL (format of data: Flat_binder_object (type is reference, handle, cookie))

F, Driver discovery Data contains flat_binder_object structure, and type represents a reference, from the binder_ref of the Service_manager process to find the DESC equals handle an item, find according to Binder_ Node finds the corresponding service, finally establishes binder_ref for the test_client process, sets the test_client process according to the order in which test_client gets the service, and you binder_ref the DESC value, and set its node to perform the corresponding service Binder_node

(3) Test_client How to use HelloService

A, construction data (data contains: code (and the service-side contract of the value of the representative function), parameters, purpose (based on the service to determine the handle value))

B. Using the IOCTL to send data

C, the driver from the data out of the handle, according to handle find test_client process binder_ref, according to find Binder_node, based on Binder_node inside proc Find service process, the data to Test_ Server, and set PTR and cookie in the data, which equals binder_node ptr and cookie

D, Test_server according to PTR and cookie know which service you want to invoke, which function to call based on code in the data


When the server registers the service, it provides different ptr/cookie for each service.
The driver constructs a binder_node for each service, and it also contains Ptr/cookie

Client must first GetService before using the service: it constructs a binder_ref in the driver,
BINDER_REF contains DESC, node member, DESC is an integer, node points to the binder_node of the corresponding service

When using the service, the client constructs the data and invokes the IOCTL: the data contains handle

The driver finds Binder_ref (Desc==handle) based on handle, finds Binder_node, and then finds the server,
Remove Ptr/cookie from Binder_node and send the data to the server

Server according to Ptr/cookie know which service to invoke, ....

The core function: IOCTL
The core data of the client is: handle
The core data for the server is: Ptr/cookie

4. Internal mechanism _ proxy class analysis: Bpservicemanager and Bphelloservice

Communication between Test_client and Test_server is implemented through the proxy class Bphelloservice in C + + (this time test_client is Client,test_server is server)

Test_server the process of adding services to Service_manager is done through the proxy class Bpservicemanager (this time test_server is Client,service_manager is the server)

Test_client the process of getting a service to Service_manager is done through the proxy class Bpservicemanager (this time test_client is Client,service_manager is the server)

According to the above section: the core data of the client is: handle

So for class Bpservicemanager: Handle = 0 passed to it;

So for class Bphelloservice: The handle value returned by handle = Bpservicemanager->getservice ("Hello") passed to it


4.1 The process of obtaining a Bpservicemanager object:
Defaultservicemanager constructs a Bpservicemanager object,
where it's mremote = new Bpbinder (0); Mremote->mhandle=0

Defaultservicemanager (singleton mode, only one in one process)//IServiceManager.cpp
Convert Bpbinder (mhandle=0) object to Iservicemanager interface (Bpservicemanager)
Gdefaultservicemanager = interface_cast<iservicemanager> (Processstate::self ()->getContextObject (NULL));
Analysis:
Processstate::self ()->getcontextobject (NULL)
Getstrongproxyforhandle (0);
b = new Bpbinder (handle); Mhandle=handle=0


Interface_cast<iservicemanager> (new Bpbinder (0))//IInterface.h
Iservicemanager::asinterface (obj);
return new Bpservicemanager (obj); Mremote=obj=new bpbinder (0);


4.2 The process of obtaining a Bphelloservice object:
Call Bpservicemanager's GetService function to get a flat_binder_object,
Remove the handle from it, create a bpbinder (handle),
Then use Interface_cast to create a Bphelloservice object using this Bpbinder

Binders are Bpbinder objects that contain helloservice handle.
sp<ibinder> Binder = Sm->getservice (String16 ("Hello")); IServiceManager.cpp
Construct data: The data must contain "Hello"
Send data: To handle 0, which is the Service_manager process
Remove HelloService's handle from the received reply
return Reply.readstrongbinder ();
Unflatten_binder (Processstate::self (), *this, &val);
*out = Proc->getstrongproxyforhandle (Flat->handle);
New Bpbinder (handle);

Convert Binder to Ihelloservice interface (Bphelloservice object)
Binders are Bpbinder objects that contain helloservice handle.
sp<ihelloservice> service = interface_cast<ihelloservice> (binder);


4.3 How the proxy class sends data: IOCTL, data containing handle, other structural parameters
After constructing the data, call:
Remote ()->transact (...)
Ipcthreadstate::self ()->transact (Mhandle, code, data, reply, flags);

5. Internal mechanism _ Data transmission: Processstate and Ipcthreadstate

Test_server main process Open, mmap operation through sp<processstate> proc (processstate::self ()) to achieve

The class object is Processstate, which is a singleton pattern, with only one processstate per process, and processstate::self to get the object gprocess

Test_server creates multiple threads depending on the situation, each thread has a ipcthreadstate, which is also a singleton pattern, which is ipcthreadstate::self to get the object

Processstate:

gprocess = new Processstate when open and MMAP,FD exist MDRIVERFD

Each thread has its own Ipcthreadstate object, the object is a line thread some, each different, it should exist in the thread of the local space, in the process through the pthread_key_create () create a key, there is a key has value, key value pair. The same key can be set to a different value for a thread

Through the pthread_setspecific (K,V1) setting in thread 1, the process is obtained through pthread_getspecific (k) V1

Ipcthreadstate

Processstate::self ()->startthreadpool ();//Creating Child threads

Ipcthreadstate::self ()->jointhreadpool ();//Main thread

5.1 AddService
for different services, constructs the flat_binder_object structure, inside the. Binder/.cookie for different services It's not the same value

Sm->addservice ( String16 ("Hello"), new Bnhelloservice ());
Data.writestrongbinder (service);//service = new Bnhelloservice ();
Flatten_binder (Processstate::self (), Val, this);//val = Service = new Bnhelloservice ();
Flat_binder_object obj;//Parameter binder = val = Service = new Bnhelloservice ();
IBinder *local = Binder->localbinder ();//=this = new Bnhelloservice ();
Obj.type = Binder_type_binder;
Obj.binder = reinterpret_cast<uintptr_t> (Local->getweakrefs ());
Obj.cookie = reinterpret_cast<uintptr_t> (local);//New Bnhelloservice ();

5.2 Server How to tell which service the client wants to use
The server receives the data containing the flat_binder_object structure,
It can be based on the. Binder/.cookie analysis of which service the client wants to use

Convert the. Cookie to a Bnxxxx object, and then call its function:

A bbinder pointer is constructed based on a cookie, which actually points to a Bnxxx object
Sp<bbinder> B ((bbinder*) tr.cookie);

And then call its Transact function.
Error = B->transact (tr.code, buffer, &reply, tr.flags);
Err = ontransact (code, data, reply, flags); It will be called to the Ontransact implemented in Bnxxx.
It will call different functions according to the code value.

5.3 How to invoke the function provided by HelloService

9.7 Binder System _c++ Implementation _ internal mechanism 1

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.