AIDL & Binder机制和原理

新的Android 通信规范中,C++层使用AIDL替代HIDL实现Framework和Vendor层之间调用解耦。

我们知道,Binder之间通信,需要一个服务端和一个客户端.

【Binder】

Binder架构分成四层,应用层,Framework层,Native层和内核层

应用层:Java应用层通过调用IActivityManager.bindService,经过层层调用到AMS.bindService;

Framework层:Jave IPC Binder通信采用C/S架构,在Framework层实现BinderProxy和Binder;

Native层:Native IPC,在Native层的C/S架构,实现了BpBinder和BBinder(JavaBBinder);

Kernel层:Binder驱动,运行在内核空间,可共享。其它三层是在用户空间,不可共享。

Binder  IPC原理:

 Binder Service流程:

在这里插入图片描述

 创建Proxy:

   private static class Proxy implements android.app.IActivityManager {
        private android.os.IBinder mRemote;
         Proxy(android.os.IBinder remote) {
                mRemote = remote;
         } 
         ...
           @Override
     public int bindService(android.app.IApplicationThread caller, android.os.IBinder token, android.content.Intent service, String resolvedType, android.app.IServiceConnection connection, int flags, String callingPackage, int userId) throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain(); //创建数据Parcel实例
                android.os.Parcel _reply = android.os.Parcel.obtain();//创建返回Parcel实例
                int _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    //将ApplicationThread对象传递给systemserver,caller在binder前初始化创建?
                    _data.writeStrongBinder((((caller != null)) ? (caller.asBinder()) : (null)));
                    _data.writeStrongBinder(token);
                    if ((service != null)) {
                        _data.writeInt(1);
                        service.writeToParcel(_data, 0);
                    } else {
                        _data.writeInt(0);
                    }
                    _data.writeString(resolvedType);
                    //将InnerConnection对象传递给systemserver
                    _data.writeStrongBinder((((connection != null)) ? (connection.asBinder()) : (null)));
                    _data.writeInt(flags);
                    _data.writeString(callingPackage);
                    _data.writeInt(userId);
                    //通过bind调用,进入到systemserver
                    mRemote.transact(Stub.TRANSACTION_bindService, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readInt();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
        }
        ...
    }

【AIDL】

是一个基于Binder通信的接口封装工具(直通式例外,另说),通过定义AIDL接口文件确定Binder客户端和服务通信接口。在软件编译时,AIDL工具根据AIDL接口文件自动生成Interface,客户端Proxy和服务端Stub代码, 模块间调用关系如图:

 

Interface: 服务端接口 + binder功能接口(asBinder)

Proxy: 服务端对应用端提供的客户端代理类(= Binder 客户端 + Inteface transact实现)。服务对外提供接口不直接提供创建的服务实例本身,而是会通过底层Binder服务创建的一个代理服务(伪服务)实例返回给应用端调用。因此伪服务除了继承Interface外,还实现了如何远程调用真服务的接口(基于binder的transact)

private static class Proxy implements com.test.IAIService
    {
 @Override public java.lang.String getServerState() throws android.os.RemoteException
      {
        android.os.Parcel _data = android.os.Parcel.obtain();
        android.os.Parcel _reply = android.os.Parcel.obtain();
        java.lang.String _result;
        try {
          _data.writeInterfaceToken(DESCRIPTOR);
          boolean _status = mRemote.transact(Stub.TRANSACTION_getServerState, _data, _reply, 0);
          if (!_status) {
            if (getDefaultImpl() != null) {
              return getDefaultImpl().getServerState();
            }
          }
          _reply.readException();
          _result = _reply.readString();
        }
        finally {
          _reply.recycle();
          _data.recycle();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值