​如何连接建立后的client和Service通信

今天小编给大家分享一下如何连接建立后的client和Service通信的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

创新互联公司是少有的网站建设、成都网站建设、营销型企业网站、微信平台小程序开发、手机APP,开发、制作、设计、卖链接、推广优化一站式服务网络公司,从2013年创立,坚持透明化,价格低,无套路经营理念。让网页惊喜每一位访客多年来深受用户好评

这里以CameraService::connect()为例进行说明。

@Camera.cpp

sp Camera::connect()

{

    LOGV("connect");

    sp c = new Camera();

    const sp& cs = getCameraService();

    if (cs != 0) {

        c->mCamera = cs->connect(c);//这条语句将进入BpCameraService::connect()

    }

    return c;

}

@ICameraService.cpp
virtual sp connect(const sp& cameraClient)
    {
        Parcel data, reply;
        data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
        data.writeStrongBinder(cameraClient->asBinder());
        remote()->transact(BnCameraService::CONNECT, data, &reply);
        return interface_cast(reply.readStrongBinder());
    }
 

这里remote是我们的CameraService映射的一个BpBinder对象

virtual sp ICameraService:: connect()会调用到BpBinder::transact()

à IPCThreadState::self()->transact(),并写入binder driver中,

Binder driver最终会唤醒media_server进程中的在IPCThreadState::joinThreadPool()中运行的读线程。

void IPCThreadState::joinThreadPool(bool isMain)  

{   …

     do {  

      … 

           result = talkWithDriver();  

            size_t IN = mIn.dataAvail();  

            if (IN < sizeof(int32_t)) continue;  

            cmd = mIn.readInt32();  

            result = executeCommand(cmd);  

        } while (result != -ECONNREFUSED && result != -EBADF);  

    …

这一次,talkWithDriver()函数会返回BpCameraService:: connect ()生成的数据包,并调用executeCommand()函数执行命令。在本例中,命令为BR_TRANSACTION。

status_t IPCThreadState::executeCommand(int32_t cmd)  

{  

    ......  

    switch(cmd){  

    ......  

    case BR_TRANSACTION:  

        {  

            binder_transaction_data tr;  

            ......  

            Parcel reply;  

            ......  

            if (tr.target.ptr) {  

             sp b((BBinder*)tr.cookie);  

             const status_t error = b->transact(tr.code, buffer, &reply, 0);  

             }

             … 

            if ((tr.flags & TF_ONE_WAY) == 0) {  

                LOG_ONEWAY("Sending reply to %d!", mCallingPid);  

                sendReply(reply, 0);  

            }

            ...  

        }  

        break;  

    ......  

    } // end of switch  

    ...

if(tr.target.ptr)为真的分支部分是最重要的。它从binder内核驱动中获取到一个地址并转换为BBinder类型的指针(该指针在执行IServiceManager::addService()函数时放入binder内核驱动)。记住,CameraService继承自BBinder。该指针实际上与CameraService实例是同一个指针。于是接下来的transact()函数将调用到CaermaService::onTransact(),再调用到BnCameraService::onTransact()虚函数。

@CameraService.cpp

status_t CameraService::onTransact(

    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)

{

        switch (code) {

        case BnCameraService::CONNECT:

            IPCThreadState* ipc = IPCThreadState::self();

            const int pid = ipc->getCallingPid();

            const int self_pid = getpid();

            if (pid != self_pid) {

                // we're called from a different process, do the real check

                if (!checkCallingPermission(

                        String16("android.permission.CAMERA")))

                {

                    const int uid = ipc->getCallingUid();

                    LOGE("Permission Denial: "

                            "can't use the camera pid=%d, uid=%d", pid, uid);

                    return PERMISSION_DENIED;

                }

            }

            break;

    }

    status_t err = BnCameraService::onTransact(code, data, reply, flags);

    return err;

}

@ICameraService.cpp

status_t BnCameraService::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    switch(code) {
        case CONNECT: {
            CHECK_INTERFACE(ICameraService, data, reply);
            sp cameraClient = interface_cast(data.readStrongBinder());
            sp camera = connect(cameraClient); //真正的处理函数
            reply->writeStrongBinder(camera->asBinder());
            return NO_ERROR;
        } break;
        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}
至此完成了一次从client到service的函数调用过程。

注意在这个函数中,系统将调用CameraService::connect()

以上就是“如何连接建立后的client和Service通信”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注创新互联行业资讯频道。


当前文章:​如何连接建立后的client和Service通信
分享链接:http://myzitong.com/article/pgsepo.html