- 论坛徽章:
- 0
|
// pthread_setspecific。
IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
if (st) return st;
return new IPCThreadState;//new一个对象,
}
if (gShutdown) return NULL;
pthread_mutex_lock(&gTLSMutex);
if (!gHaveTLS) {
if (pthread_key_create(&gTLS, threadDestructor) != 0) {
pthread_mutex_unlock(&gTLSMutex);
return NULL;
}
gHaveTLS = true;
}
pthread_mutex_unlock(&gTLSMutex);
goto restart; //我FT,其实goto没有我们说得那样卑鄙,汇编代码很多跳转语句的。
//关键是要用好。
}
//这里是构造函数,在构造函数里边pthread_setspecific
IPCThreadState::IPCThreadState()
: mProcess(ProcessState::self()), mMyThreadId(androidGetTid())
{
pthread_setspecific(gTLS, this);
clearCaller();
mIn.setDataCapacity(256);
//mIn,mOut是两个Parcel,干嘛用的啊?把它看成是命令的buffer吧。再深入解释,又会大脑停摆的。
mOut.setDataCapacity(256);
}
出来了,终于出来了....,恩,回到BpBinder那。
BpBinder::BpBinder(int32_t handle)
: mHandle(handle) //注意,接上述内容,这里调用的时候传入的是0
, mAlive(1)
, mObitsSent(0)
, mObituaries(NULL)
{
......
IPCThreadState::self()->incWeakHandle(handle);
什么incWeakHandle,不讲了..
}
喔,new BpBinder就算完了。到这里,我们创建了些什么呢?
ProcessState有了。
IPCThreadState有了,而且是主线程的。
BpBinder有了,内部handle值为0
gDefaultServiceManager = interface_cast<IServiceManager>(new BpBinder(0));
终于回到原点了,大家是不是快疯掉了?
interface_cast,我第一次接触的时候,把它看做类似的static_cast一样的东西,然后死活也搞不明白 BpBinder*指针怎么能强转为IServiceManager*,花了n多时间查看BpBinder是否和IServiceManager继承还是咋的....。
终于,我用ctrl+鼠标(source insight)跟踪进入了interface_cast
IInterface.h位于framework/base/include/binder/IInterface.h
template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
return INTERFACE::asInterface(obj);
}
所以,上面等价于:
inline sp<IServiceManager> interface_cast(const sp<IBinder>& obj)
{
return IServiceManager::asInterface(obj);
}
看来,只能跟到IServiceManager了。
IServiceManager.h---》framework/base/include/binder/IServiceManager.h
看看它是如何定义的:
2.4 IserviceManager
class IServiceManager : public IInterface
{
//ServiceManager,字面上理解就是Service管理类,管理什么?增加服务,查询服务等
//这里仅列出增加服务addService函数
public:
DECLARE_META_INTERFACE(ServiceManager);
virtual status_t addService( const String16& name,
const sp<IBinder>& service) = 0;
};
DECLARE_META_INTERFACE(ServiceManager)??
怎么和MFC这么类似?微软的影响很大啊!知道MFC的,有DELCARE肯定有IMPLEMENT
果然,这两个宏DECLARE_META_INTERFACE和IMPLEMENT_META_INTERFACE(INTERFACE, NAME)都在
刚才的IInterface.h中定义。我们先看看DECLARE_META_INTERFACE这个宏往IServiceManager加了什么?
下面是DECLARE宏
#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();
我们把它兑现到IServiceManager就是:
static const android::String16 descriptor; -->喔,增加一个描述字符串
static android::sp< IServiceManager > asInterface(const android::sp<android::IBinder>&
obj) ---》增加一个asInterface函数
virtual const android::String16& getInterfaceDescriptor() const; ---》增加一个get函数
估计其返回值就是descriptor这个字符串
IServiceManager (); \
virtual ~IServiceManager();增加构造和虚析购函数...
那IMPLEMENT宏在哪定义的呢?
见IServiceManager.cpp。位于framework/base/libs/binder/IServiceManager.cpp
IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
下面是这个宏的定义
#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() { } \
很麻烦吧?尤其是宏看着头疼。赶紧兑现下吧。
const
android::String16 IServiceManager::descriptor(“android.os.IServiceManager”);
const android::String16& IServiceManager::getInterfaceDescriptor() const
{ return IServiceManager::descriptor;//返回上面那个android.os.IServiceManager
}
android::sp<IServiceManager> IServiceManager::asInterface(
const android::sp<android::IBinder>& obj)
{
android::sp<IServiceManager> intr;
if (obj != NULL) {
intr = static_cast<IServiceManager *>(
obj->queryLocalInterface(IServiceManager::descriptor).get());
if (intr == NULL) {
intr = new BpServiceManager(obj);
}
}
return intr;
}
IServiceManager::IServiceManager () { }
IServiceManager::~ IServiceManager() { }
哇塞,asInterface是这么搞的啊,赶紧分析下吧,还是不知道interface_cast怎么把BpBinder*转成了IServiceManager
我们刚才解析过的interface_cast<IServiceManager>(new BpBinder(0)),
原来就是调用asInterface(new BpBinder(0))
android::sp<IServiceManager> IServiceManager::asInterface(
const android::sp<android::IBinder>& obj)
{
android::sp<IServiceManager> intr;
if (obj != NULL) {
....
intr = new BpServiceManager(obj);
//神呐,终于看到和IServiceManager相关的东西了,看来
//实际返回的是BpServiceManager(new BpBinder(0));
}
}
return intr;
}
BpServiceManager是个什么玩意儿?p是什么个意思?
2.5 BpServiceManager
终于可以讲解点架构上的东西了。p是proxy即代理的意思,Bp就是BinderProxy,BpServiceManager,就是SM的Binder代理。既然是代理,那肯定希望对用户是透明的,那就是说头文件里边不会有这个Bp的定义。是吗?
果然,BpServiceManager就在刚才的IServiceManager.cpp中定义。
class BpServiceManager : public BpInterface<IServiceManager>
//这种继承方式,表示同时继承BpInterface和IServiceManager,这样IServiceManger的
addService必然在这个类中实现
{
public:
//注意构造函数参数的命名 impl,难道这里使用了Bridge模式?真正完成操作的是impl对象?
//这里传入的impl就是new BpBinder(0)
BpServiceManager(const sp<IBinder>& impl)
: BpInterface<IServiceManager>(impl)
{
}
virtual status_t addService(const String16& name, const sp<IBinder>& service)
{
待会再说..
}
基类BpInterface的构造函数(经过兑现后)
//这里的参数又叫remote,唉,真是害人不浅啊。
inline BpInterface< IServiceManager >::BpInterface(const sp<IBinder>& remote)
: BpRefBase(remote)
{
}
BpRefBase::BpRefBase(const sp<IBinder>& o)
: mRemote(o.get()), mRefs(NULL), mState(0)
//o.get(),这个是sp类的获取实际数据指针的一个方法,你只要知道
//它返回的是sp<xxxx>中xxx* 指针就行
{
//mRemote就是刚才的BpBinder(0)
...
}
好了,到这里,我们知道了:
sp<IServiceManager> sm = defaultServiceManager(); 返回的实际是BpServiceManager,它的remote对象是BpBinder,传入的那个handle参数是0。
现在重新回到MediaService。
int main(int argc, char** argv)
{
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
//上面的讲解已经完了
MediaPlayerService::instantiate();//实例化MediaPlayerservice
//看来这里有名堂!
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
}
到这里,我们把binder设备打开了,得到一个BpServiceManager对象,这表明我们可以和SM打交道了,但是好像没干什么有意义的事情吧?
2.6 MediaPlayerService
那下面我们看看后续又*什么?以MediaPlayerService为例。它位于framework\base\media\libmediaplayerservice\libMediaPlayerService.cpp
void MediaPlayerService::instantiate() {
defaultServiceManager()->addService(
//传进去服务的名字,传进去new出来的对象
String16("media.player"), new MediaPlayerService());
}
MediaPlayerService::MediaPlayerService()
{
LOGV("MediaPlayerService created");//太简单了
mNextConnId = 1;
}
defaultServiceManager返回的是刚才创建的BpServiceManager
调用它的addService函数。
MediaPlayerService从BnMediaPlayerService派生
class MediaPlayerService : public BnMediaPlayerService
FT,MediaPlayerService从BnMediaPlayerService派生,BnXXX,BpXXX,快晕了。
Bn 是Binder Native的含义,是和Bp相对的,Bp的p是proxy代理的意思,那么另一端一定有一个和代理打交道的东西,这个就是Bn。
讲到这里会有点乱喔。先分析下,到目前为止都构造出来了什么。
BpServiceManager
BnMediaPlayerService
这两个东西不是相对的两端,从BnXXX就可以判断,BpServiceManager对应的应该是BnServiceManager,BnMediaPlayerService对应的应该是BpMediaPlayerService。
我们现在在哪里?对了,我们现在是创建了BnMediaPlayerService,想把它加入到系统的中去。
喔,明白了。我创建一个新的Service—BnMediaPlayerService,想把它告诉ServiceManager。
那我怎么和ServiceManager通讯呢?恩,利用BpServiceManager。所以嘛,我调用了BpServiceManager的addService函数!
为什么要搞个ServiceManager来呢?这个和Android机制有关系。所有Service都需要加入到ServiceManager来管理。同时也方便了Client来查询系统存在哪些Service,没看见我们传入了字符串吗?这样就可以通过Human Readable的字符串来查找Service了。
---》感觉没说清楚...饶恕我吧。
2.7 addService
addService是调用的BpServiceManager的函数。前面略去没讲,现在我们看看。
virtual status_t addService(const String16& name, const sp<IBinder>& service)
{
Parcel data, reply;
//data是发送到BnServiceManager的命令包
//看见没?先把Interface名字写进去,也就是什么android.os.IServiceManager
data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
//再把新service的名字写进去 叫media.player
data.writeString16(name);
//把新服务service—>就是MediaPlayerService写到命令中
data.writeStrongBinder(service);
//调用remote的transact函数
status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
return err == NO_ERROR ? reply.readInt32() : err;
}
我的天,remote()返回的是什么?
remote(){ return mRemote; }-->啊?找不到对应的实际对象了???
还记得我们刚才初始化时候说的:
“这里的参数又叫remote,唉,真是害人不浅啊“
原来,这里的mRemote就是最初创建的BpBinder..
好吧,到那里去看看:
BpBinder的位置在framework\base\libs\binder\BpBinder.cpp
status_t BpBinder::transact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
//又绕回去了,调用IPCThreadState的transact。
//注意啊,这里的mHandle为0,code是ADD_SERVICE_TRANSACTION,data是命令包
//reply是回复包,flags=0
status_t status = IPCThreadState::self()->transact(
mHandle, code, data, reply, flags);
if (status == DEAD_OBJECT) mAlive = 0;
return status;
}
...
}
再看看IPCThreadState的transact函数把
status_t IPCThreadState::transact(int32_t handle,
uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags)
{
status_t err = data.errorCheck();
flags |= TF_ACCEPT_FDS;
if (err == NO_ERROR) {
//调用writeTransactionData 发送数据
err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL);
}
if ((flags & TF_ONE_WAY) == 0) {
if (reply) {
err = waitForResponse(reply);
} else {
Parcel fakeReply;
err = waitForResponse(&fakeReply);
}
....等回复
err = waitForResponse(NULL, NULL);
....
return err;
}
再进一步,瞧瞧这个...
status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
{
binder_transaction_data tr;
tr.target.handle = handle;
tr.code = code;
tr.flags = binderFlags;
const status_t err = data.errorCheck();
if (err == NO_ERROR) {
tr.data_size = data.ipcDataSize();
tr.data.ptr.buffer = data.ipcData();
tr.offsets_size = data.ipcObjectsCount()*sizeof(size_t);
tr.data.ptr.offsets = data.ipcObjects();
}
....
上面把命令数据封装成binder_transaction_data,然后
写到mOut中,mOut是命令的缓冲区,也是一个Parcel
mOut.writeInt32(cmd);
mOut.write(&tr, sizeof(tr));
//仅仅写到了Parcel中,Parcel好像没和/dev/binder设备有什么关联啊?
恩,那只能在另外一个地方写到binder设备中去了。难道是在?
return NO_ERROR;
}
//说对了,就是在waitForResponse中
status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
{
int32_t cmd;
int32_t err;
while (1) {
//talkWithDriver,哈哈,应该是这里了
if ((err=talkWithDriver()) < NO_ERROR) break;
err = mIn.errorCheck();
if (err < NO_ERROR) break;
if (mIn.dataAvail() == 0) continue;
//看见没?这里开始操作mIn了,看来talkWithDriver中
//把mOut发出去,然后从driver中读到数据放到mIn中了。
cmd = mIn.readInt32();
switch (cmd) {
case BR_TRANSACTION_COMPLETE:
if (!reply && !acquireResult) goto finish;
break;
.....
return err;
}
status_t IPCThreadState::talkWithDriver(bool doReceive)
{
binder_write_read bwr;
//中间东西太复杂了,不就是把mOut数据和mIn接收数据的处理后赋值给bwr吗?
status_t err;
do {
//用ioctl来读写
if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
err = NO_ERROR;
else
err = -errno;
} while (err == -EINTR);
//到这里,回复数据就在bwr中了,bmr接收回复数据的buffer就是mIn提供的 |
|