android binder IPC 通信中 asInterface 与 asBinder

Binder 用于通信,Interface用于功能调用。

 

其实asInterface 完成的是Binder到Interface的转换,具体就是:

BBinder->BnInterface

BpBinder->BpInterface

 

而asBinder功能则相反,具体是:

BnInterface->BBinder

BpInterface->BpBinder

 

asInterface 与 asBinder 的返回值与输入参数和调用对象有关,如下:

template<typename INTERFACE>
IBinder* BnInterface<INTERFACE>::onAsBinder()
{
    return this;
}

template<typename INTERFACE>
inline IBinder* BpInterface<INTERFACE>::onAsBinder()
{
    return remote();
}

sp<IXxx>asInterface( const sp<IBinder>& obj)   
 {                                                           
        sp<IXxx> intr;                       
        if (obj != NULL) {                                             
            intr = static_cast<IXxx*>(obj->queryLocalInterface(descriptor).get());            
            if (intr == NULL) {                                       
                intr = new BpIXxx(obj);                       
            }                                                         
        }                                                             
        return intr;                                                  
}      

1、调用由 继承自BnInterface的类 实例化的对象,返回对象本身,因为本来就该类本来就继承自 BBinder。

2、调用由 继承自BpInterface的类 实例化的对象,返回的是BpBinder。

3、调用 asInterface 时,如果传入的是 继承自BBinder的类 实例化的对象,返回的是BnInterface。

4、调用 asInterface 时,如果传入的是 继承自BpBinder的类 实例化的对象,返回的是BpInterface。

======================================================================================

使用情景之一:从 A 服务中获取 Xxx 服务,IXxx 继承自 IInterface。

A server 建立 BnInterface,A client 获得 BpInterface,而 BpBinder 和 BBinder 扮演信使的作用。

 

在 client 端使用某个功能时,一般用 IXxx->Func() 的形式,那么如何得到 IXxx呢?

1、通过 A 的 client 向 A 的 server 发送 GET_XXX 命令
IA->remote()->transact(GET_XXX, data, &reply);

其中 IA->remote() 返回的 A 的 BpBinder。

2、A 的 server 通过 A 的 BBinder 收到 GET_XXX 命令

调用 BnA 的 onTransact

status_t BnA::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
     switch(code) {
        case GET_XXX: {
            CHECK_INTERFACE(IXxx, data, reply);
            sp<IXxx> x = createXxx();
            reply->writeStrongBinder(x->asBinder());
            return NO_ERROR;
        } break;
        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}

createXxx() 返回的是 new 出来的 Xxx 对象,而 Xxx 在此处代表一个BnInterface。

3、reply->writeStrongBinder(x->asBinder())

asBinder 在 BnInterface 中实现

template<typename INTERFACE>
IBinder* BnInterface<INTERFACE>::onAsBinder()
{
    return this;
}

即返回的是自身,因为 BnInterface 同时是一个 BBinder。

reply->writeStrongBinder 调用 Binder 驱动,Binder 驱动将BBinder的引用返回给 client。

4、reply.readStrongBinder()

reply.readStrongBinder() 返回的是 BBinder 的代理对象 BpBinder

return interface_cast<IXxx>(reply.readStrongBinder());
5、interface_cast<IXxx>

interface_cast 展开

template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
    return INTERFACE::asInterface(obj);
}

将 BpBinder 转为 IXxx,也就是 BpInterface。

至此得到了与 Xxx server 有关联的 IXxx,当然,实际上建立的 BpXxx 与 BnXxx 之间的通信。

 

 

 

转载于:https://www.cnblogs.com/JonnyLulu/p/3757452.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值