Android Binder Mechanism III (anonymous service)

Source: Internet
Author: User

What is an anonymous service? Any service that is not registered on the ServiceManager is anonymous service.

Or take an example of an article to see the code:

status_t mediaplayer::setdatasource (int fd, int64_t offset, int64_t length) {    status_t err = unknown_error;    Const sp<imediaplayerservice>& Service (Getmediaplayerservice ());    if (service! = 0) {        sp<imediaplayer> player (service->create (this, Maudiosessionid));        if ((No_error! = dosetretransmitendpoint (player)) | |            (No_error! = Player->setdatasource (fd, offset, length))) {            player.clear ();        }        Err = Attachnewplayer (player);    }    return err;}

In Bpmediaplayerservice, the implementation of Create is as follows:

Virtual sp<imediaplayer> Create (        const sp<imediaplayerclient>& Client, int audiosessionid) {    Parcel data, reply;    Data.writeinterfacetoken (Imediaplayerservice::getinterfacedescriptor ());    Data.writestrongbinder (Client->asbinder ());    Data.writeint32 (Audiosessionid);    Remote ()->transact (CREATE, data, &reply);    Return interface_cast<imediaplayer> (Reply.readstrongbinder ());}
Jump directly to the server Mediaplayerservice to see the real implementation of Create:

Sp<imediaplayer> mediaplayerservice::create (const sp<imediaplayerclient>& client,        int Audiosessionid) {    pid_t pid = ipcthreadstate::self ()->getcallingpid ();    int32_t Connid = Android_atomic_inc (&mnextconnid);    sp<client> C = new Client (this            , PID, Connid, Client, Audiosessionid,            ipcthreadstate::self () Getcallinguid ());    ALOGV ("Create new Client (%d) from PID%d, uid%d,", Connid, PID,         ipcthreadstate::self ()->getcallinguid ());    /* Add by Gary. Start {-----------------------------------*    /C->setscreen (mscreen);    /* Add by Gary. End   -----------------------------------}} *    /C->setsubgate (mglobalsubgate);  2012-03-12, add the global interfaces to control the subtitle gate    wp<client> w = c;    {        Mutex::autolock lock (mLock);        Mclients.add (w);    }    return c;}

From the code, we can see that service->create (this, Maudiosessionid) is a Bpmediaplayer object that returns a parameter of type client, Where Client is Mediaplayerservice's private inner class, its declaration is: Class Client:publicbnmediaplayer. In this way, the service side and the client of binder communication are set up. Client side Bpmediaplayer is used by MediaPlayer, and server-side bnmediaplayer is used by Mediaplayerservice.

How does Bpmediaplayer get the handle value of Bnmediaplayer? The answer is that when Mediaplayerservice returns the binder's reference, the binder driver holds the various data for the binder entity, creates the node, and looks at the following code:

status_t bnmediaplayerservice::ontransact (    uint32_t Code, const parcel& data, parcel* reply, uint32_t flags) { C1/>switch (code) {case        CREATE: {            check_interface (imediaplayerservice, data, reply);            sp<imediaplayerclient> client =                interface_cast<imediaplayerclient> (Data.readstrongbinder ());            int audiosessionid = Data.readint32 ();            Sp<imediaplayer> player = Create (client, Audiosessionid);            Reply->writestrongbinder (Player->asbinder ());            return no_error;        } Break, ...}

The answer is in this sentence: Reply->writestrongbinder (Player->asbinder ());

When this reply is written to the binder driver, the driver specifically handles this ibinder type of data, creating a handle for Bbinder.

Once the communication path is established, the communication can be made: Player->setdatasource (FD, offset, length)

The subsequent implementation is the same as the normal service in the previous article.

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.