Binder机制 在Android中的具体实现原理
1.实现步骤
Binder
机制在 Android
中的实现次要依附 Binder
类,其实现了IBinder
接口
- 实例阐明: 即:
Client
过程 须要调用Server
过程的加法函数(将整数a和b相加)
Client
过程 须要传两个整数给Server
过程Server
过程 须要把相加后的后果 返回给Client
过程
- 具体步骤 上面,我会依据
Binder
跨过程通信机制 模型的步骤进行剖析
✔步骤1:注册服务
- 过程形容
Server
过程 通过Binder
驱动 向Service Manager
过程注册服务 代码实现
Server
过程创立 一个Binder
对象Binder
实体是Server
过程 在Binder
驱动中的存在模式
- 该对象保留
Server
和Service Manager
的信息(保留在内核空间中)
- 该对象保留
Binder
驱动通过 内核空间的Binder
实体 找到用户空间的Server
对象
- 代码剖析
Binder binder = new Stub(); // 步骤1:创立Binder对象 ->>剖析1 // 步骤2:创立 IInterface 接口类 的匿名类 // 创立前,须要事后定义 继承了IInterface 接口的接口 -->剖析3 IInterface plus = new IPlus(){ // 确定Client过程须要调用的办法 public int add(int a,int b){ return a+b; } // 实现IInterface接口中惟一的办法 public IBinder asBinder(){ return null ; } }; // 步骤3 binder.attachInterface(plus,"add two int"); // 1. 将(add two int,plus)作为(key,value)对存入到Binder对象中的一个Map<String,IInterface>对象中 // 2. 之后,Binder对象 可依据add two int通过queryLocalIInterface()取得对应IInterface对象(即plus)的援用,可依附该援用实现对申请办法的调用 // 剖析结束,跳出 <-- 剖析1:Stub类 --> public class Stub extends Binder { // 继承自Binder类 ->>剖析2 // 复写onTransact() @Override boolean onTransact(int code, Parcel data, Parcel reply, int flags){ // 具体逻辑等到步骤3再具体解说,此处先跳过 switch (code) { case Stub.add: { data.enforceInterface("add two int"); int arg0 = data.readInt(); int arg1 = data.readInt(); int result = this.queryLocalIInterface("add two int") .add(arg0, arg1); reply.writeInt(result); return true; } } return super.onTransact(code, data, reply, flags); } // 回到下面的步骤1,持续看步骤2 <-- 剖析2:Binder 类 --> public class Binder implement IBinder{ // Binder机制在Android中的实现次要依附的是Binder类,其实现了IBinder接口// IBinder接口: //定义了近程操作对象的根本接口,代表了一种跨过程传输的能力 // 零碎会为每个实现了IBinder接口的对象提供跨过程传输能力 // 即Binder类对象具备了跨过程传输的能力 void attachInterface(IInterface plus, String descriptor); // 作用: // 1. 将(descriptor,plus)作为(key,value)对存入到Binder对象中的一个Map<String,IInterface>对象中 // 2. 之后,Binder对象 可依据descriptor通过queryLocalIInterface()取得对应IInterface对象(即plus)的援用, //可依附该援用实现对申请办法的调用 IInterface queryLocalInterface(Stringdescriptor) ; // 作用:依据 参数 descriptor 查找相应的IInterface对象(即plus援用) boolean onTransact(int code, Parcel data, Parcel reply, int flags); // 定义:继承自IBinder接口的 // 作用:执行Client过程所申请的指标办法(子类须要复写) // 参数阐明: // code:Client过程申请办法标识符。即Server过程依据该标识确定所申请的指标办法 // data:指标办法的参数。(Client过程传进来的,此处就是整数a和b) // reply:指标办法执行后的后果(返回给Client过程) // 注:运行在Server过程的Binder线程池中;当Client过程发动近程申请时,近程申请会要求零碎底层执行回调该办法 final class BinderProxy implements IBinder { // 即Server过程创立的Binder对象的代理对象类 // 该类属于Binder的外部类 } // 回到剖析1原处 } <-- 剖析3:IInterface接口实现类 --> public interface IPlus extends IInterface { // 继承自IInterface接口->>剖析4 // 定义须要实现的接口办法,即Client过程须要调用的办法 public int add(int a,int b); // 返回步骤2 } <-- 剖析4:IInterface接口类 --> // 过程间通信定义的通用接口 // 通过定义接口,而后再服务端实现接口、客户端调用接口,就可实现跨过程通信。 public interface IInterface{ // 只有一个办法:返回以后接口关联的 Binder 对象。 public IBinder asBinder(); } // 回到剖析3原处
注册服务后, Binder
驱动持有 Server
过程创立的 Binder
实体
✔步骤2:获取服务(Client)
Client
过程 应用 某个service
前(此处是 相加函数),须通过Binder
驱动向ServiceManager
过程 获取相应的Service
信息- 具体代码实现过程如下:
此时, Client
过程与Server
过程曾经建设了连贯
✔步骤3:应用服务(Client)
Client
过程 依据获取到的 service
信息( Binder
代理对象),通过Binder
驱动建设与该Service
所在Server
过程通信的链路,并开始应用服务
过程形容
Client
过程通信的链路,并开始应用服务 过程 将参数(整数a和b)发送到Server
过程Server
过程 依据Client
过程要求调用 指标办法(即加法函数)Server
过程 将指标办法的后果(即加法后的后果)返回给Client
过程
代码实现过程
3.1:
Client
过程 将参数(整数a和b)发送到Server
过程
// 1. Client过程 将须要传送的数据写入到Parcel对象中 // data = 数据 = 指标办法的参数(Client过程传进来的,此处就是整数a和b)+ IInterface接口对象的标识符descriptor android.os.Parcel data = android.os.Parcel.obtain(); data.writeInt(a); data.writeInt(b); data.writeInterfaceToken("add two int"); // 办法对象标识符让Server过程在Binder对象中依据"add two int"通过queryLocalIInterface() // 查找相应的IInterface对象(即Server创立的plus),Client过程须要调 用的相加办法就在该对象中 android.os.Parcel reply = android.os.Parcel.obtain(); // reply:指标办法执行后的后果(此处是相加后的后果) // 2. 通过 调用代理对象的transact() 将 上述数据发送到Binder驱动 binderproxy.transact(Stub.add, data, reply, 0) // 参数阐明: // 1. Stub.add:指标办法的标识符(Client过程 和 Server过程 本身约定,可为任意)// 2. data :上述的Parcel对象 // 3. reply:返回后果 // 0:可不论 // 注:在发送数据后,Client过程的该线程会临时被挂起 // 所以,若Server过程执行的耗时操作,请不要应用主线程,以避免ANR // 3. Binder驱动依据 代理对象 找到对应的真身Binder对象所在的Server 过程(零碎主动执行) // 4. Binder驱动把 数据 发送到Server 过程中,并告诉Server 过程执行解包(零碎主动执行) 123456789101112131415161718192021222324252627
3.2: Server
过程依据 Client
过程要求 调用 指标办法(即加法函数)
// 1. 收到Binder驱动告诉后,Server 过程通过回调Binder对象onTransact()进行数据解包&调用指标办法 public class Stub extends Binder { // 复写onTransact() @Override boolean onTransact(int code, Parcel data, Parcel reply, int flags){ // code即在transact()中约定的指标办法的标识符 switch (code) { case Stub.add: { // a. 解包Parcel中的数据 data.enforceInterface("add two int"); // a1. 解析指标办法对象的标识符 int arg0 = data.readInt(); int arg1 = data.readInt(); // a2. 取得指标办法的参数 // b. 依据"add two int"通过queryLocalIInterface()获取相应的IInterface对象 //(即Server创立的plus)的援用,通过该对象援用调用办法 int result = this.queryLocalIInterface("add two int") .add(arg0, arg1); // c. 将计算结果写入到reply reply.writeInt(result); return true; } } return super.onTransact(code, data, reply, flags); // 2. 将结算后果返回 到Binder驱动
3.3: 过程 将指标办法的后果(即加法后的后果)返回给过程
// 2. 将结算后果返回 到Binder驱动 // 1. Binder驱动依据 代理对象 沿原路 将后果返回 并告诉Client过程获取返回后果 // 2. 通过代理对象 接管后果(之前被挂起的线程被唤醒) binderproxy.transact(Stub.ADD, data, reply, 0); reply.readException();; result = reply.readInt(); }}12345678
2.原理图 & 流程图
- 总结 上面,我用一个原理图 & 流程图来总结步骤3的内容
3. 长处
比照 Linux
( Android
基于 Linux
)上的其余过程通信形式(管道、音讯队列、共享内存、信号量、 Socket
), Binder
机制的长处有:
4. 总结
本文次要具体解说 跨过程通信模型 Binder
机制 ,总结如下:
特地地,对于从模型构造组成的Binder驱动来说:
- 整个
Binder
模型的原理步骤 & 源码剖析