Android Binder機(jī)制 基礎(chǔ)(一)
前言
Linus Benedict Torvalds : RTFSC – Read The Fucking Source Code
Dianne Hackborn 記住這家伙擂错,這系統(tǒng)他做的。
Binder 簡(jiǎn)要
Binder:用來(lái)實(shí)現(xiàn)不用進(jìn)程間通信袱讹。Binder屬于一個(gè)驅(qū)動(dòng)山孔,工作在linux層糠馆,運(yùn)行在kernel娄涩。服務(wù)端嘲驾,客戶(hù)端處在用戶(hù)空間,binder驅(qū)動(dòng)處在內(nèi)核空間相恃。
Binder機(jī)制有兩個(gè)重要的類(lèi):IBinder和Binder辜纲。通過(guò)分析這兩個(gè)主要的類(lèi)來(lái)淺析Binder的使用。
1拦耐、IBinder
Base interface for a remotable object, the core part of a lightweight remote procedure call mechanism designed for high performance when performing in-process and cross-process calls. This interface describes the abstract protocol for interacting with a remotable object. Do not implement this interface directly, instead extend from Binder.
翻譯:
一個(gè)遠(yuǎn)程對(duì)象的基本接口耕腾,一個(gè)輕量級(jí)的遠(yuǎn)程程序的調(diào)用機(jī)制的核心部分專(zhuān)為高性能設(shè)計(jì)在執(zhí)行程序和跨進(jìn)程調(diào)用。該接口描述了與遠(yuǎn)程對(duì)象交互的抽象協(xié)議杀糯。注意:不要直接實(shí)現(xiàn)這個(gè)接口扫俺,而不是從“Binder”中擴(kuò)展。
IBinder的核心API是transact transact()匹配在Binder類(lèi)的onTransact Binder.onTransact()函數(shù)中固翰。這方法允許你發(fā)送一個(gè)調(diào)用到一個(gè)IBinder對(duì)象和從IBinder對(duì)象中收到一個(gè)調(diào)用狼纬。這個(gè)API是同步的。
通過(guò)transact()傳輸?shù)臄?shù)據(jù)是一個(gè)Parcel骂际,一個(gè)通用的緩存數(shù)據(jù)疗琉,同時(shí)也有一些元數(shù)據(jù)。在buffer中元數(shù)據(jù)使用來(lái)管理IBinder對(duì)象標(biāo)記的歉铝,這樣的引用可以在buffer穿越過(guò)程中得以保留盈简。這種機(jī)制確保IBinder被寫(xiě)入Parcel發(fā)送到另一個(gè)進(jìn)程,如果其他進(jìn)程發(fā)送一個(gè)標(biāo)記到同一個(gè)IBinder回到原來(lái)的過(guò)程太示,那么原始的過(guò)程中會(huì)接收到同一個(gè)IBinder對(duì)象回來(lái)柠贤。這些語(yǔ)義允許IBinder/Binder對(duì)象作為一個(gè)獨(dú)特的身份(作為一個(gè)記號(hào)或作其他用途),可以在過(guò)程管理类缤。
The system maintains a pool of transaction threads in each process that it runs in. These threads are used to dispatch all IPCs coming in from other processes.
The Binder system also supports recursion across processes.
1.1臼勉、transact()方法
- IBinder核心接口方法:
- 允許你可以分別發(fā)送一個(gè)消息到一個(gè)Binder對(duì)象和接收一個(gè)從Binder對(duì)象發(fā)出的消息。
/*
Perform a generic operation with the object.
@param code The action to perform. This should be a number between FIRST_CALL_TRANSACTION and LAST_CALL_TRANSACTION.
@param data Marshalled data to send to the target. Must not be null. If you are not sending any data, you must create an empty Parcel that is given here.
@param reply Marshalled data to be received from the target. May be null if you are not interested in the return value.
@param flags Additional operation flags. Either 0 for a normal RPC, or FLAG_ONEWAY for a one-way RPC.
*/
public boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException;
- 確認(rèn)遠(yuǎn)程對(duì)象是否有效有三個(gè)方法:
- The transact(); 如果對(duì)象被銷(xiāo)毀而你嘗試去調(diào)用這個(gè)函數(shù)餐弱,它會(huì)拋出一個(gè)異常宴霸。
- The pingBinder(); 如果對(duì)象被銷(xiāo)毀,它會(huì)返回false膏蚓。
- The linkToDeath(); 用來(lái)注冊(cè)一個(gè)DeathRecipient猖败,當(dāng)被銷(xiāo)毀的時(shí)候?qū)?huì)監(jiān)聽(tīng)到。
2降允、Binder
Base class for a remotable object, the core part of a lightweight remote procedure call mechanism defined by IBinder. This class is an implementation of IBinder that provides standard local implementation of such an object.
翻譯:
基類(lèi)可遠(yuǎn)程對(duì)象恩闻,由定義的IBinder一個(gè)輕量級(jí)的遠(yuǎn)程過(guò)程調(diào)用機(jī)制的核心組成部分。這個(gè)類(lèi)是的IBinder的實(shí)現(xiàn)剧董,提供了標(biāo)準(zhǔn)的地方實(shí)現(xiàn)這樣一個(gè)目標(biāo)的幢尚。
大多數(shù)開(kāi)發(fā)者不應(yīng)該直接實(shí)現(xiàn)這個(gè)類(lèi),作為代替應(yīng)該使用AIDl工具去描述你想要的接口翅楼,產(chǎn)生合適的Binder子類(lèi)尉剩。但是,你可以直接使用Binder來(lái)實(shí)現(xiàn)自定義的RPC協(xié)議或簡(jiǎn)單地實(shí)例化一個(gè)Binder的對(duì)象來(lái)直接使用作為一個(gè)記號(hào)可以共享的過(guò)程毅臊。
2.1理茎、transact()方法
/**Default implementation rewinds the parcels and calls onTransact. On the remote side, transact calls into the binder to do the IPC.*/
public final boolean transact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
if (false) Log.v("Binder", "Transact: " + code + " to " + this);
if (data != null) {
data.setDataPosition(0);
}
boolean r = onTransact(code, data, reply, flags);
if (reply != null) {
reply.setDataPosition(0);
}
return r;
}
2.2、onTransact()方法
/*Default implementation is a stub that returns false. You will want to override this to do the appropriate unmarshalling of transactions. If you want to call this, call transact().*/
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
if (code == INTERFACE_TRANSACTION) {
...
return true;
} else if (code == DUMP_TRANSACTION) {
...
return true;
}
return false;
}
2.3、execTransact()方法
// Entry point from android_util_Binder.cpp's onTransact
private boolean execTransact(int code, long dataObj, long replyObj,
int flags) {
Parcel data = Parcel.obtain(dataObj);
Parcel reply = Parcel.obtain(replyObj);
// theoretically, we should call transact, which will call onTransact,
// but all that does is rewind it, and we just got these from an IPC,
// so we'll just call it directly.
boolean res;
// Log any exceptions as warnings, don't silently suppress them.
// If the call was FLAG_ONEWAY then these exceptions disappear into the ether.
try {
res = onTransact(code, data, reply, flags);
} catch (RemoteException e) {
if ((flags & FLAG_ONEWAY) != 0) {
} else {
reply.setDataPosition(0);
reply.writeException(e);
}
res = true;
} catch (RuntimeException e) {
if ((flags & FLAG_ONEWAY) != 0) {
} else {
reply.setDataPosition(0);
reply.writeException(e);
}
res = true;
} catch (OutOfMemoryError e) {
// Unconditionally log this, since this is generally unrecoverable.
RuntimeException re = new RuntimeException("Out of memory", e);
reply.setDataPosition(0);
reply.writeException(re);
res = true;
}
checkParcel(this, code, reply, "Unreasonably large binder reply buffer");
reply.recycle();
data.recycle();
// Just in case -- we are done with the IPC, so there should be no more strict
// mode violations that have gathered for this thread. Either they have been
// parceled and are now in transport off to the caller, or we are returning back
// to the main transaction loop to wait for another incoming transaction. Either
// way, strict mode begone!
StrictMode.clearGatheredViolations();
return res;
}