9.13 Binder System _java Implementation _ internal mechanism _server end

Source: Internet
Author: User

Logcat testserver:* testclient:* helloservice:* *:s &
Classpath=/mnt/android_fs/testserver.jar App_process/testserver &
Classpath=/mnt/android_fs/testclient.jar app_process/testclient Hello
Classpath=/mnt/android_fs/testclient.jar app_process/testclient Hello Weidongshan


App_process:frameworks\base\cmds\app_process\app_main.cpp

6.1 Server reads data
uses app_process to start the server process,
It creates child threads first:
appruntime::onstarted ()
Proc-> Startthreadpool ();
Spawnpooledthread (TRUE);
Sp<thread> t = new Poolthread (ismain);
T->run (name.string ());           
//It creates a child thread and executes Threadloop
Ipcthreadstate::self ()->jointhreadpool (mismain);
              {
Do {
result = Getandexecutecommand ();
result = Talkwithdriver ();
result = ExecuteCommand (cmd);              
for br_transaction data,
sp<bbinder> B ((bbinder*) tr.cookie);
Error = B->transact (tr.code, buffer, &reply, tr.flags);
} while (...)
 }


How the 6.2 server calls the Ontransact function of the service RPC layer after it reads the data
A. Set when AddService. Ptr/.cookie
Servicemanager.addservice ("Hello", New HelloService ());
Analysis:
A.1 New HelloService () is a Java object
A.2 Convert. Cookies to Bbinder objects when processing data, which is a C + + object
Therefore: AddService will certainly convert the Java object into a Bbinder derived class object, which exists in the. cookie

Conclusion:
A.1 AddService will invoke C + + functions via JNI:
Create a Bbinder derived class Javabbinder object,
It's. Mobject point to Java object: New HelloService ()
It contains the Ontransact function
Put this object into a. Cookie (final Binder_node.cookie of the service in Binder driver)

The A.2 server process reads data from the drive, which contains a. cookie
Convert it to a Bbinder object,
Call it's Transact function
It calls into the Ontransact function defined in the derived class Javabbinder

A.3 Javabbinder defined in the Ontransact function (c + +)
It calls Java Binder's Exectransact method through JNI,
Then call the Ontransact function (JAVA) defined in binder derived class Ihelloservice.stub

The Ontransact function (JAVA) defined in A.4 ihelloservice.stub:
Analyze data
Call Sayhello/sayhello_to

SOURCE reading:
A.1 servicemanager.addservice ("Hello", New HelloService ());
Servicemanagerproxy.addservice:
Parcel.java
Data.writestrongbinder (service);
Nativewritestrongbinder (Mnativeptr, Val); val = Service = new HelloService ()
It is a JNI call, corresponding to Android_os_parcel_writestrongbinder (C + +)

A.2 Android_os_parcel_writestrongbinder (c + +)
It constructs a Javabbinder object (c + +), A. Mobject=new helloservice () Java object
Then let the. Cookie=javabbinder object (c + +)
Convert Java Parcel to C + + Parcel
parcel* Parcel = reinterpret_cast<parcel*> (nativeptr);

. cookie = Ibinderforjavaobject (env, object) get a Javabbinder object
Parcel->writestrongbinder (Ibinderforjavaobject (env, object))

A.3 Ibinderforjavaobject (env, object)//object = new HelloService ()
Convert a Java object (new HelloService ()) to a C + + IBinder object

javabbinderholder* JBH = (javabbinderholder*) env->getlongfield (obj, gbinderoffsets.mobject);
return JBH! = NULL? Jbh->get (env, obj): NULL;
b = new Javabbinder (env, obj); obj = new HelloService ()
Mobject = new HelloService ()//executed in the constructor of Javabbinder


A.4 from the driver. A cookie, which is a Javabbinder object
Call its Transact function, which causes the ontransact of the Javabbinder object to be called

Javabbinder::ontransact (call a function in Java)
Mobject point to HelloService Object
Gbinderoffsets.mexectransact point: Exectransact method in Java Binder class
Call the Exectransact method in the HelloService (derived from Binder) object
Jboolean res = Env->callbooleanmethod (Mobject, Gbinderoffsets.mexectransact,code, reinterpret_cast<jlong> ( &data), reinterpret_cast<jlong> (reply), flags);

A.5 Java Binder's Exectransact method:
res = ontransact (code, data, reply, flags);
Call the Ontransact method in HelloService (from Ihelloservice.stube)
Resolving data
Call Sayhello/sayhello_to

B. The data read contains the. Ptr/.cookie

It converts the cookie into a Bbinder object.

Call it's Transact function

Jointhreadpool ()

{

do{

result = Getandexecutecommand (); //

result = Talkwithdriver ();//Read data

result = ExecuteCommand (cmd);//Analysis data

For br_transaction data

Sp<bbinder> B ((bbinder*) tr.cookie);//Convert Cookies to Bbinder objects

Error = B->transact (tr.code,buffer,&reply,tr.flags)

}

}

9.13 Binder System _java Implementation _ internal mechanism _server end

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.