《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描述.
- 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
- 模板类BnInterface继承了BBinder,BBinder中定义了Binder本地对象进程间通信的接口.
- 当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;
...
};