Android深入浅出之Binder机制
2012-06-30 10:47:32 来源:WEB开发网核心提示:} else {Parcel fakeReply;err =waitForResponse(&fakeReply);}....等回复err = waitForResponse(NULL, NULL);.... return err;}再进一步,瞧瞧这个...status_tIPCThreadState::write
} else {
Parcel fakeReply;
err =waitForResponse(&fakeReply);
}
....等回复
err = waitForResponse(NULL, NULL);
....
return err;
}
再进一步,瞧瞧这个...
status_tIPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
int32_t handle, uint32_t code, constParcel& 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_tIPCThreadState::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) {
caseBR_TRANSACTION_COMPLETE:
if (!reply &&!acquireResult) goto finish;
break;
.....
return err;
}
status_tIPCThreadState::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提供的
if (bwr.read_consumed > 0) {
mIn.setDataSize(bwr.read_consumed);
mIn.setDataPosition(0);
}
returnNO_ERROR;
}
好了,到这里,我们发送addService的流程就彻底走完了。
BpServiceManager发送了一个addService命令到BnServiceManager,然后收到回复。
先继续我们的main函数。
int main(int argc,char** argv)
{
sp<ProcessState>proc(ProcessState::self());
sp<IServiceManager> sm =defaultServiceManager();
MediaPlayerService::instantiate();
---》该函数内部调用addService,把MediaPlayerService信息 add到ServiceManager中
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
}
这里有个容易搞晕的地方:
MediaPlayerService是一个BnMediaPlayerService,那么它是不是应该等着
BpMediaPlayerService来和他交互呢?但是我们没看见MediaPlayerService有打开binder设备的操作啊!
Parcel fakeReply;
err =waitForResponse(&fakeReply);
}
....等回复
err = waitForResponse(NULL, NULL);
....
return err;
}
再进一步,瞧瞧这个...
status_tIPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
int32_t handle, uint32_t code, constParcel& 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_tIPCThreadState::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) {
caseBR_TRANSACTION_COMPLETE:
if (!reply &&!acquireResult) goto finish;
break;
.....
return err;
}
status_tIPCThreadState::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提供的
if (bwr.read_consumed > 0) {
mIn.setDataSize(bwr.read_consumed);
mIn.setDataPosition(0);
}
returnNO_ERROR;
}
好了,到这里,我们发送addService的流程就彻底走完了。
BpServiceManager发送了一个addService命令到BnServiceManager,然后收到回复。
先继续我们的main函数。
int main(int argc,char** argv)
{
sp<ProcessState>proc(ProcessState::self());
sp<IServiceManager> sm =defaultServiceManager();
MediaPlayerService::instantiate();
---》该函数内部调用addService,把MediaPlayerService信息 add到ServiceManager中
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
}
这里有个容易搞晕的地方:
MediaPlayerService是一个BnMediaPlayerService,那么它是不是应该等着
BpMediaPlayerService来和他交互呢?但是我们没看见MediaPlayerService有打开binder设备的操作啊!
更多精彩
赞助商链接