日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

android binder IPC 通信中 asInterface 与 asBinder

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

Binder 用于通信,Interface用于功能調用。

其實asInterface 完成的是Binder到Interface的轉換,具體就是:

BBinder->BnInterface

BpBinder->BpInterface

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

BnInterface->BBinder

BpInterface->BpBinder

asInterface 與 asBinder 的返回值與輸入參數和調用對象有關,如下:

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、調用由 繼承自BnInterface的類 實例化的對象,返回對象本身,因為本來就該類本來就繼承自 BBinder。

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

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

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

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

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

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

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

1、通過 A 的 client 向 A 的 server 發送GET_XXX 命令

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

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

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

調用 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 中實現

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

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

reply->writeStrongBinder 調用 Binder 驅動,Binder 驅動將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 轉為IXxx,也就是 BpInterface。

至此得到了與 Xxx server 有關聯的 IXxx,當然,實際上建立的 BpXxx 與 BnXxx 之間的通信。

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。