温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-01-12 18:02:08 来源:亿速云 阅读:215 作者:iii 栏目:互联网科技

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

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

@Camera.cpp

sp<Camera> Camera::connect()

{

    LOGV("connect");

    sp<Camera> c = new Camera();

    const sp<ICameraService>& cs = getCameraService();

    if (cs != 0) {

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

    }

    return c;

}

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

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

virtual sp<ICamera> 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<BBinder> 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<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
            sp<ICamera> 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通信”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI