日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

android binder IPC 通信中 asInterface 与 asBinder

發(fā)布時間:2023/12/15 35 生活家
生活随笔 收集整理的這篇文章主要介紹了 android binder IPC 通信中 asInterface 与 asBinder 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Binder 用于通信,Interface用于功能調(diào)用。

其實asInterface 完成的是Binder到Interface的轉(zhuǎn)換,具體就是:

BBinder->BnInterface

BpBinder->BpInterface

而asBinder功能則相反,具體是:

BnInterface->BBinder

BpInterface->BpBinder

asInterface 與 asBinder 的返回值與輸入?yún)?shù)和調(diào)用對象有關(guān),如下:

template<typename INTERFACE>
IBinder* BnInterface<INTERFACE>::onAsBinder()
{
    return this;
}

template<typename INTERFACE>
inline IBinder* BpInterface<INTERFACE>::onAsBinder()
{
    return remote();
}

sp<IXxx>asInterface( const sp<IBinder>& obj)   
 {                                                           
        sp<IXxx> intr;                       
        if (obj != NULL) {                                             
            intr = static_cast<IXxx*>(obj->queryLocalInterface(descriptor).get());            
            if (intr == NULL) {                                       
                intr = new BpIXxx(obj);                       
            }                                                         
        }                                                             
        return intr;                                                  
}      

1、調(diào)用由 繼承自BnInterface的類 實例化的對象,返回對象本身,因為本來就該類本來就繼承自 BBinder。

2、調(diào)用由 繼承自BpInterface的類 實例化的對象,返回的是BpBinder。

3、調(diào)用asInterface 時,如果傳入的是繼承自BBinder的類 實例化的對象,返回的是BnInterface。

4、調(diào)用asInterface 時,如果傳入的是繼承自BpBinder的類 實例化的對象,返回的是BpInterface。

======================================================================================

使用情景之一:從 A 服務(wù)中獲取 Xxx 服務(wù),IXxx 繼承自IInterface。

A server 建立BnInterface,A client 獲得 BpInterface,而 BpBinder 和 BBinder 扮演信使的作用。

在 client 端使用某個功能時,一般用IXxx->Func() 的形式,那么如何得到IXxx呢?

1、通過 A 的 client 向 A 的 server 發(fā)送GET_XXX 命令

IA->remote()->transact(GET_XXX, data, &reply);

其中IA->remote() 返回的 A 的 BpBinder。

2、A 的 server 通過 A 的 BBinder收到GET_XXX 命令

調(diào)用 BnA 的onTransact

status_t BnA::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
     switch(code) {
        case GET_XXX: {
            CHECK_INTERFACE(IXxx, data, reply);
            sp<IXxx> x = createXxx();
            reply->writeStrongBinder(x->asBinder());
            return NO_ERROR;
        } break;
        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}

createXxx() 返回的是 new 出來的 Xxx 對象,而 Xxx 在此處代表一個BnInterface。

3、reply->writeStrongBinder(x->asBinder())

asBinder 在BnInterface 中實現(xiàn)

template<typename INTERFACE>
IBinder* BnInterface<INTERFACE>::onAsBinder()
{
    return this;
}

即返回的是自身,因為 BnInterface 同時是一個 BBinder。

reply->writeStrongBinder 調(diào)用 Binder 驅(qū)動,Binder 驅(qū)動將BBinder的引用返回給 client。

4、reply.readStrongBinder()

reply.readStrongBinder() 返回的是BBinder 的代理對象 BpBinder

return interface_cast<IXxx>(reply.readStrongBinder());

5、interface_cast<IXxx>

interface_cast 展開

template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
    return INTERFACE::asInterface(obj);
}

將 BpBinder 轉(zhuǎn)為IXxx,也就是 BpInterface。

至此得到了與 Xxx server 有關(guān)聯(lián)的 IXxx,當(dāng)然,實際上建立的 BpXxx 與 BnXxx 之間的通信。

總結(jié)

以上是生活随笔為你收集整理的android binder IPC 通信中 asInterface 与 asBinder的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。