今天小編給大家分享一下如何連接建立后的client和Service通信的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。
創(chuàng)新互聯(lián)公司是少有的網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、營(yíng)銷(xiāo)型企業(yè)網(wǎng)站、微信平臺(tái)小程序開(kāi)發(fā)、手機(jī)APP,開(kāi)發(fā)、制作、設(shè)計(jì)、賣(mài)鏈接、推廣優(yōu)化一站式服務(wù)網(wǎng)絡(luò)公司,從2013年創(chuàng)立,堅(jiān)持透明化,價(jià)格低,無(wú)套路經(jīng)營(yíng)理念。讓網(wǎng)頁(yè)驚喜每一位訪客多年來(lái)深受用戶好評(píng)
這里以CameraService::connect()為例進(jìn)行說(shuō)明。
@Camera.cpp
sp<Camera> Camera::connect()
{
LOGV("connect");
sp<Camera> c = new Camera();
const sp<ICameraService>& cs = getCameraService();
if (cs != 0) {
c->mCamera = cs->connect(c);//這條語(yǔ)句將進(jìn)入BpCameraService::connect()
}
return c;
}
@ICameraService.cpp
virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient)
{
Parcel data, reply;
data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
data.writeStrongBinder(cameraClient->asBinder());
remote()->transact(BnCameraService::CONNECT, data, &reply);
return interface_cast<ICamera>(reply.readStrongBinder());
}
這里remote是我們的CameraService映射的一個(gè)BpBinder對(duì)象
virtual sp<ICamera> ICameraService:: connect()會(huì)調(diào)用到BpBinder::transact()
à IPCThreadState::self()->transact(),并寫(xiě)入binder driver中,
Binder driver最終會(huì)喚醒media_server進(jìn)程中的在IPCThreadState::joinThreadPool()中運(yùn)行的讀線程。
void IPCThreadState::joinThreadPool(bool isMain)
{ …
do {
…
result = talkWithDriver();
size_t IN = mIn.dataAvail();
if (IN < sizeof(int32_t)) continue;
cmd = mIn.readInt32();
result = executeCommand(cmd);
} while (result != -ECONNREFUSED && result != -EBADF);
…
}
這一次,talkWithDriver()函數(shù)會(huì)返回BpCameraService:: connect ()生成的數(shù)據(jù)包,并調(diào)用executeCommand()函數(shù)執(zhí)行命令。在本例中,命令為BR_TRANSACTION。
status_t IPCThreadState::executeCommand(int32_t cmd)
{
......
switch(cmd){
......
case BR_TRANSACTION:
{
binder_transaction_data tr;
......
Parcel reply;
......
if (tr.target.ptr) {
sp<BBinder> b((BBinder*)tr.cookie);
const status_t error = b->transact(tr.code, buffer, &reply, 0);
}
…
if ((tr.flags & TF_ONE_WAY) == 0) {
LOG_ONEWAY("Sending reply to %d!", mCallingPid);
sendReply(reply, 0);
}
...
}
break;
......
} // end of switch
...
}
if(tr.target.ptr)為真的分支部分是最重要的。它從binder內(nèi)核驅(qū)動(dòng)中獲取到一個(gè)地址并轉(zhuǎn)換為BBinder類(lèi)型的指針(該指針在執(zhí)行IServiceManager::addService()函數(shù)時(shí)放入binder內(nèi)核驅(qū)動(dòng))。記住,CameraService繼承自BBinder。該指針實(shí)際上與CameraService實(shí)例是同一個(gè)指針。于是接下來(lái)的transact()函數(shù)將調(diào)用到CaermaService::onTransact(),再調(diào)用到BnCameraService::onTransact()虛函數(shù)。
@CameraService.cpp
status_t CameraService::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch (code) {
case BnCameraService::CONNECT:
IPCThreadState* ipc = IPCThreadState::self();
const int pid = ipc->getCallingPid();
const int self_pid = getpid();
if (pid != self_pid) {
// we're called from a different process, do the real check
if (!checkCallingPermission(
String16("android.permission.CAMERA")))
{
const int uid = ipc->getCallingUid();
LOGE("Permission Denial: "
"can't use the camera pid=%d, uid=%d", pid, uid);
return PERMISSION_DENIED;
}
}
break;
}
status_t err = BnCameraService::onTransact(code, data, reply, flags);
return err;
}
@ICameraService.cpp
status_t BnCameraService::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch(code) {
case CONNECT: {
CHECK_INTERFACE(ICameraService, data, reply);
sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
sp<ICamera> camera = connect(cameraClient); //真正的處理函數(shù)
reply->writeStrongBinder(camera->asBinder());
return NO_ERROR;
} break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
}
至此完成了一次從client到service的函數(shù)調(diào)用過(guò)程。
注意在這個(gè)函數(shù)中,系統(tǒng)將調(diào)用CameraService::connect()
以上就是“如何連接建立后的client和Service通信”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標(biāo)題:?如何連接建立后的client和Service通信
網(wǎng)頁(yè)URL:http://www.rwnh.cn/article30/pgsepo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站策劃、網(wǎng)站營(yíng)銷(xiāo)、電子商務(wù)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)