Binder技术篇(一)
网上主要参考了两篇资料,都是从Native层的IBinder开始的。
说明一点的是,这个类是被定义在命名空间为android里面的,命名空间的定义最后是可以加上分号也可以不加,但是不怎么使用过命名空间中定义类的使用
在IBinder.h
class IBinder : public virtual RefBase// 至少包含一个纯虚函数的类为纯虚类,实际上RefBase是纯虚类,IBinder也属于是纯虚类 { public: ... virtual sp<IInterface> queryLocalInterface(const String16& descriptor); //返回一个IInterface对象(指针) ... virtual const String16& getInterfaceDescriptor() const = 0; //纯虚函数,需要派生类来实现 virtual bool isBinderAlive() const = 0; virtual status_t pingBinder() = 0; virtual status_t dump(int fd, const Vector<String16>& args) = 0; virtual status_t transact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0) = 0; virtual status_t linkToDeath(const sp<DeathRecipient>& recipient, void* cookie = NULL, uint32_t flags = 0) = 0; virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient, void* cookie = NULL, uint32_t flags = 0, wp<DeathRecipient>* outRecipient = NULL) = 0; ... virtual BBinder* localBinder(); //返回一个BBinder对象(指针) virtual BpBinder* remoteBinder(); //返回一个BpBinder对象(指针) };
接着来看看IBinder的两个子类:BBinder以及BpBinder
BpBinder.h中申明了BpBinder实际为public继承了IBinder类。
class BpBinder : public IBinder { public: BpBinder(int32_t handle); 。。。。。。。。 };
Binder.h中也申明了BBinder实际为public继承了IBinder类。
class BBinder : public IBinder { public: BBinder(); 。。。。。。。 };
IBinder和BBinder的实现主要是在Binder.cpp文件中、BpBinder的实现主要是在BpBinder.cpp中。
接口 | BBinder | BpBinder |
queryLocalInterface()//用来查询服务 | 没有实现, 默认实现 IBinder 默认{reutrn NULL}; | 没有实现 IBinder 默认实现 {return NULL} |
getInterfaceDescriptor() | {return sEmptyDescriptor;} | (this)->transact(INTERFACE_TRANSACTION, send, &reply); ... mDescriptorCache = res; |
isBinderAlive() | {return true;} | {return mAlive != 0;} |
pingBinder() | {return NoError;} | {transact(PING_TRANSACTION, send, &reply); |
linkToDeath()//服务器挂载的时候通知客户端 | {return INVALID_OPERATION;} | {self->requestDeathNotification(mHandle, this);} |
unlinkToDeath() | {return INVALID_OPERATION;} | {self->clearDeathNotification(mHandle, this);} |
localBinder() | {return this;} | 没有实现, IBinder默认实现 {return NULL}; |
remoteBinder()//返回远程的本地代理 | 没有实现,IBinder默认实现 {return NULL;} | {return this}; |
transact()//用于发送请求 | {err = onTransact(code, data, reply, flags);} | IPCThreadState::self()->transact(mHandle, code, data, reply, flags); |
onTransact()//实际上这个函数在IBinder中也没有这个接口啊//用来响应请求 |
switch (code) { |
没有实现 |
首先给出native服务器端的家族图谱
1、queryLocalInterface查询服务
该函数是虚函数,在IBinder中直接返回了NULL,在BBinder和BpBinder中都没有实现。
sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/) { return NULL; }
难道是不提供任何服务吗?好像不是的,只能继续往他的派生类中查找,派生类是BnInterface,这个类中的定义如下:IIterface.h中
template<typename INTERFACE> class BnInterface : public INTERFACE, public BBinder { public: virtual sp<IInterface> queryLocalInterface(const String16& _descriptor); virtual const String16& getInterfaceDescriptor() const; protected: virtual IBinder* onAsBinder(); };
查看模板类函数的实现:
// 这个实际上在模板类外申明函数的方法 template<typename INTERFACE> inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface( const String16& _descriptor) { if (_descriptor == INTERFACE::descriptor) return this; return NULL; }
实际上是返回了this,也就是返回了他本身,BnInterface对象。他是如何和IIterface挂钩的呢?BBinder不可能是IIterface的子类了,那只能是传入的模板INTERFACE来实现BnInterface为IIterface的派生类了。
class IMediaDrmService: public IInterface { public: DECLARE_META_INTERFACE(MediaDrmService); virtual sp<ICrypto> makeCrypto() = 0; virtual sp<IDrm> makeDrm() = 0; };
中转类是IMediaDrmService,这个类实际上也就是传入的INTERFACE
class BnMediaDrmService: public BnInterface<IMediaDrmService> { public: virtual status_t onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0); };
在Android里面应该是很多类似这样的应用,先记录下来,自己的分析过程。
下图是在服务器端的继承关系梳理,operation只是列举了其中的一部分:
其中关注一下IIterface这个类的实现:关注里面的asBinder以及onAsBinder函数
class IInterface : public virtual RefBase { public: IInterface(); static sp<IBinder> asBinder(const IInterface*); static sp<IBinder> asBinder(const sp<IInterface>&); protected: virtual ~IInterface(); virtual IBinder* onAsBinder() = 0; };
首先来客户端的家族图谱
看着好像是和BpBinder没有啥子关系,实际不然:
posted on 2017-08-01 14:06 feifeiyuan 阅读(609) 评论(0) 编辑 收藏 举报