Android 如何理解BpBinder,BnBinder等概念

《Android系统源代码情景分析》
了解BnBinder,BpBinder等概念有助于理解Binder通信机制

总结
概念作用
Binder代理对象类型为BpBinder,在用户空间创建,且执行在Client进程中.会被Client进程中的其他对象引用,另外会引用Binder驱动程序中的Binder引用对象.
Binder引用对象类型为binder_ref,在Binder驱动程序中创建,被Binder代理对象引用.
Binder实体对象类型为binder_node,在Binder驱动程序中创建,被Binder引用对象所引用
Binder本地对象类型为BBinder,在用户空间中创建,且执行在Server进程中.会被Server进程中其他对象引用,还会被Binder实体对象引用.
Binder本地对象与Binder代理对象

Binder本地对象在Service组件中,使用模板类BnInterface描述.

  1. Binder本地对象在Binder驱动中对应Binder实体对象.
// IInterface.h
template<typename INTERFACE>
// 该模板类继承子BBinder.
// BBinder为Binder本地对象提供了抽象的进程间接口.
class BnInterface : public INTERFACE, public BBinder
{
public:
    virtual sp<IInterface>      queryLocalInterface(const String16& _descriptor);
    virtual const String16&     getInterfaceDescriptor() const;

protected:
    virtual IBinder*            onAsBinder();
};

Binder代理对象在Client组件中,使用模板类BpInterface描述.

  • Binder代理对象在Binder驱动中对应Binder引用对象.
// IInterface.h
template<typename INTERFACE>
class BpInterface : public INTERFACE, public BpRefBase
{
public:
                                BpInterface(const sp<IBinder>& remote);
protected:
    virtual IBinder*            onAsBinder();
};
BBinder
  1. 模板类BnInterface继承了BBinder,BBinder中定义了Binder本地对象进程间通信的接口.
  2. 当Binder代理对象通过Binder驱动向Binder本地对象发送进程间通信请求时,Binder本地对象onTransact()方法将被调用.
// Binder.h
class BBinder : public IBinder
{
public:
                        BBinder();
...
protected:
    virtual             ~BBinder();

    virtual status_t    onTransact( uint32_t code,
                                    const Parcel& data,
                                    Parcel* reply,
                                    uint32_t flags = 0);
...
};
BpBinder

BpInterface继承自BpRefBase.

  • BpRefBase为Binder代理对象提供了进程间通信的接口
    BpRefBase类中有个成员变量mRemote,它属于BpBinder类型.
class BpRefBase : public virtual RefBase
{
    ...
    IBinder* const          mRemote;//BpBinder类型
    RefBase::weakref_type*  mRefs;
    volatile int32_t        mState;
};
  • BpBinder中的成员变量mHandler表示Binder引用对象的句柄值.
class BpBinder : public IBinder
{
public:
                        BpBinder(int32_t handle);
    ...
    // Binder代理对象通过该方法向Binder本地对象发送消息,
    // 会将mHandle和通信数据发送给Binder驱动,驱动就能根据mHandle找到Binder引用对象->Binder实体对象->Binder本地对象
    virtual status_t    transact(   uint32_t code,
                                    const Parcel& data,
                                    Parcel* reply,
                                    uint32_t flags = 0);
    ...
private:
    const   int32_t             mHandle;
    ...
};
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值