============================================================================
在学习Android Binder之前,需要对如下几个类、一个模板函数和两个关键宏有所了解:
1)IBinder、BBinder、BpRefBase 、BpBinder、IInterface、模板类BnInterface、模板类BpInterface
2)模板函数interface_cast
3)两个关键宏:DECLARE_META_INTERFACE(INTERFACE)和IMPLEMENT_META_INTERFACE(INTERFACE, NAME)
============================================================================
// IBinder.h
// 类IBinder是Binder通信协议的接口,为client和server端通信的基础。
// 类BBinder和类BpBinder均为IBinder的子类:BBinder用于server端,BpBinder用于client端。
/**
* Base class and low-level protocol for a remotable object.
* You can derive from this class to create an object for which other
* processes can hold references to it. Communication between processes
* (method calls, property get and set) is down through a low-level
* protocol implemented on top of the transact() API.
*/
class IBinder : public virtual RefBase
{
public:
/**
* Return the canonical name of the interface provided by this IBinder
* object.
*/
virtual const String16& getInterfaceDescriptor() const = 0;
virtual status_t transact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0) = 0;
virtual BBinder* localBinder();
virtual BpBinder* remoteBinder();
protected:
virtual ~IBinder();
};
// Binder.h
/*
类BBinder继承自IBinder,用于server端。各个服务端的service(比如ServiceManager、MediaPlayerService等等)需要创建一个继承BBinder并实现onTransact()函数的class,比如BnXXXService。BBinder通过transact()接收处理client端发过来的请求命令,并调用onTransact()这个虚函数,即调用BnXXXService的onTransact()函数。
*/
/*
类BpRefBase 的作用:client 端通过查询ServiceManager 获取到所需的的 BpBinder 后, BpRefBase 负责管理当前获得的 BpBinder 实例,并保存至成员变量IBinder* const mRemote;
*/
class BBinder : public IBinder
{
public:
BBinder();
virtual const String16& getInterfaceDescriptor() const;
virtual bool isBinderAlive() const;
virtual status_t transact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0);
virtual BBinder* localBinder();
protected:
virtual ~BBinder();
virtual status_t onTransact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0);
private:
BBinder(const BBinder& o);
BBinder& operator=(const BBinder& o);
class Extras;
Extras* mExtras;
void* mReserved0;
};
class BpRefBase : public virtual RefBase
{
protected:
BpRefBase(const sp<IBinder>& o);
virtual ~BpRefBase();
inline IBinder* remote() { return mRemote; }
inline IBinder* remote() const { return mRemote; }
private:
BpRefBase(const BpRefBase& o);
BpRefBase& operator=(const BpRefBase& o);
IBinder* const mRemote;
};
// BpBinder.h
/*
类BpBinder继承自IBinder,负责将client端的请求发送至服务端的BBinder,为服务端在client端所提供的服务代理。客户端必须通过服务代理BpBinder才可以享用服务端所提供的具体服务。
*/
class BpBinder : public IBinder
{
public:
BpBinder(int32_t handle);
inline int32_t handle() const { return mHandle; }
virtual const String16& getInterfaceDescriptor() const;
virtual bool isBinderAlive() const;
virtual status_t transact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0);
virtual BpBinder* remoteBinder();
private:
const int32_t mHandle;
};
// IInterface.h
/*
类IInterface为所有的server端的共同基类,它的子类,比如IServiceManager、IMediaPlayerService等等提供了该服务端所具备的所有功能(一些列API)。
*/
/*
模板函数:
template<typename INTERFACE> inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
return INTERFACE::asInterface(obj);
}
用法:
sp<IMediaPlayerService> sMediaPlayerService;
sp<IServiceManager> sm = defaultServiceManager();
sp<IBinder> binder;
binder = sm->getService(String16("media.player"));
sMediaPlayerService = interface_cast<IMediaPlayerService>(binder);
*/
/*
模板类BnInterface(template<typename INTERFACE> class BnInterface : public INTERFACE, public BBinder)继承自传入的类INTERFACE和BBinder;
模板类BpInterface(template<typename INTERFACE> class BpInterface : public INTERFACE, public BpRefBase)继承自传入的类INTERFACE和BpRefBase。
*/
// ----------------------------------------------------------------------
class IInterface : public virtual RefBase
{
public:
IInterface();
sp<IBinder> asBinder();
sp<const IBinder> asBinder() const;
protected:
virtual ~IInterface();
virtual IBinder* onAsBinder() = 0;
};
// ----------------------------------------------------------------------
template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
return INTERFACE::asInterface(obj);
}
// ----------------------------------------------------------------------
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>
class BpInterface : public INTERFACE, public BpRefBase
{
public:
BpInterface(const sp<IBinder>& remote);
protected:
virtual IBinder* onAsBinder();
};
// ----------------------------------------------------------------------
#define DECLARE_META_INTERFACE(INTERFACE) \
static const android::String16 descriptor; \
static android::sp<I##INTERFACE> asInterface( \
const android::sp<android::IBinder>& obj); \
virtual const android::String16& getInterfaceDescriptor() const; \
I##INTERFACE(); \
virtual ~I##INTERFACE(); \
#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
const android::String16 I##INTERFACE::descriptor(NAME); \
const android::String16& \
I##INTERFACE::getInterfaceDescriptor() const { \
return I##INTERFACE::descriptor; \
} \
android::sp<I##INTERFACE> I##INTERFACE::asInterface( \
const android::sp<android::IBinder>& obj) \
{ \
android::sp<I##INTERFACE> intr; \
if (obj != NULL) { \
intr = static_cast<I##INTERFACE*>( \
obj->queryLocalInterface( \
I##INTERFACE::descriptor).get()); \
if (intr == NULL) { \
intr = new Bp##INTERFACE(obj); \
} \
} \
return intr; \
} \
I##INTERFACE::I##INTERFACE() { } \
I##INTERFACE::~I##INTERFACE() { } \
// ----------------------------------------------------------------------
关于DECLARE_META_INTERFACE(INTERFACE)和IMPLEMENT_META_INTERFACE(INTERFACE, NAME)这两个宏,以MediaPlayerClient为例说明如下:
// ----------------------------------------------------------------------
#define DECLARE_META_INTERFACE(MediaPlayerClient)// 展开如下:
static const android::String16 descriptor;
static android::sp<IMediaPlayerClient> asInterface(
const android::sp<android::IBinder>& obj);
virtual const android::String16& getInterfaceDescriptor() const;
IMediaPlayerClient();
virtual ~IMediaPlayerClient();
#define IMPLEMENT_META_INTERFACE(MediaPlayerClient, "android.media.IMediaPlayerClient")// 展开如下:
const android::String16 IMediaPlayerClient::descriptor("android.media.IMediaPlayerClient");
const android::String16&
IMediaPlayerClient::getInterfaceDescriptor() const {
return IMediaPlayerClient::descriptor;
}
android::sp<IMediaPlayerClient> IMediaPlayerClient::asInterface(
const android::sp<android::IBinder>& obj)
{
android::sp<IMediaPlayerClient> intr;
if (obj != NULL) {
intr = static_cast<IMediaPlayerClient*>(
obj->queryLocalInterface(
IMediaPlayerClient::descriptor).get());
if (intr == NULL) {
intr = new BpMediaPlayerClient(obj);
}
}
return intr;
}
IMediaPlayerClient::IMediaPlayerClient() { }
IMediaPlayerClient::~IMediaPlayerClient() { }
// ----------------------------------------------------------------------