美文网首页
IPC(四)--AIDL整体代码流程

IPC(四)--AIDL整体代码流程

作者: azu_test | 来源:发表于2019-02-21 17:01 被阅读0次

移步Android跨进程通信IPC

流程图

如果不清楚点击图片查看原图

AIDL使用流程图

AIDL的整体流程基本上就是图上的内容,下面根据代码的具体执行再走一遍代码(其实已无必要)。

第一步 创建aidl实例的Binder对象,并实现aidl内的接口方法

AIDLService#mBinder
private final IService.Stub mBinder = new IService.Stub() {
        @Override
        public String hello(String name) throws RemoteException {
            return "Server处理后的: "+name;
        }

        @Override
        public Book addBook() throws RemoteException {
            return new Book(123,"艺术探索");
        }
    };
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
  1. 因为IService.Stub实现了接口aidl而且继承了Binder,所有Stub对象既是一个Binder对象也是一个aidl实例对象。
  2. 这里实现了aidl内的接口方法,也是最终实现位置,在服务端需要做的操作都在这些方法内执行。
  3. 在被客户端通过bindService()绑定此服务时,会通过onBind()方法将IService.Stub返回返回给客户端,进而让客户端可以持有Binder对象。

第二步 客户端Client绑定服务端Service,并通过回调参数给的Binder获取IService实例

    private IService mRemoteService;
    //创建Service绑定时使用的ServiceConnection实例,并在回调内通过回调的参数获取IService对象
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder serverBinder) {
            //根据serverBinder获取IService对象
            mRemoteService = IService.Stub.asInterface(serverBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
    private void initServiceForAIDL(){
        Intent intent = new Intent(mContext, AIDLService.class);
        //绑定service
        mContext.bindService(intent,mConnection, Context.BIND_AUTO_CREATE);
    }

通过Binder生成IService时调用的方法如下

        //通过此静态方法可获取到aidl实体Stub,或者Stub的代理类Proxy
        public static com.wuyazhou.learn.IPC.aidl.IService asInterface(android.os.IBinder obj)
        {
            if ((obj==null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            //判断客户端和服务端是不是在一个进程内,如果是则直接返回Stub实体对象。如果不是则返回Stub的代理Proxy类。
            if (((iin!=null)&&(iin instanceof com.wuyazhou.learn.IPC.aidl.IService))) {
                return ((com.wuyazhou.learn.IPC.aidl.IService)iin);
            }
            return new com.wuyazhou.learn.IPC.aidl.IService.Stub.Proxy(obj);
        }
  1. 通过代码可知客户端和服务端在同一进程时返回的其实就是Stub实体对象,调用hello方法时也是直接调用Service内实现的接口,并没有通过Binder转译传递,就是简单的对象调用接口。
  2. 客户端和服务端不在同一进程时,返回的是代理对象Proxy。

第三步 调用aidl内的接口

客户端和服务端在同一进程时为简单的对象调用,此处就忽略分析了,主要分析在不同进程时是怎么执行的。

    String string = "AIDL test";
    string = mRemoteService.hello(string);

通过上面的分析可知mRemoteService为IService.Stub.Proxy对象,往下代码为

            Iservice.Stub.Proxy#hello
            //客户端方法对应转化,将调用交付给binder处理
            @Override 
            public java.lang.String hello(java.lang.String name) 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);
                    _data.writeString(name);
                    //将请求参数和请求方法标志位flag和返回数据的存储器交付给Binder处理
                    mRemote.transact(Stub.TRANSACTION_hello, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readString();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }

将参数封装在容器Parcel里面做binder传递,将结果容器也传入Binder内,等待结果输出。
在操作mRemote.transact()方法时,客户端内的线程将会挂起。

    Binder#transact
    public final boolean transact(int code, @NonNull Parcel data, @Nullable 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;
    }

再往后的操作就到了Binder内部,能力有限(手动大哭),先不做分析了,我们知道最终会进入到服务端的IService.Stub#onTransact()方法内即可。

        IService.Stub#onTransact
        //客户端请求通过系统底层封装后交由此方法执行,此方法运行在服务端的Binder线程池中,将会根据请求标志位flag调用服务端Server的方法。
        public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
            java.lang.String descriptor = DESCRIPTOR;
            //根据标志位flag调用服务端方法
            switch (code) {
                case INTERFACE_TRANSACTION: {
                    reply.writeString(descriptor);
                    return true;
                }

                case TRANSACTION_hello: {
                    data.enforceInterface(descriptor);
                    java.lang.String _arg0;
                    _arg0 = data.readString();
                    java.lang.String _result = this.hello(_arg0);
                    reply.writeNoException();
                    reply.writeString(_result);
                    return true;
                }
                default: {
                    return super.onTransact(code, data, reply, flags);
                }
            }

此处将会根据transact传递进来的方法标志位INTERFACE_TRANSACTION来定位调用的方法为hello()。然后通过Parcel容器读取传递进来的参数数据,直接调用客户端Server内实现的hello()方法。

    Service#mBinder
    private final IService.Stub mBinder = new IService.Stub() {
        @Override
        public String hello(String name) throws RemoteException {
            return "Server处理后的: "+name;
        }
    };

hello()方法执行完以后会将数据写入到Parcel容器内,然后通过Binder再返回给客户端内的Proxy,Proxy在做相应的读取操作,然后return,hello()方法请求就结束了。

相关文章

网友评论

      本文标题:IPC(四)--AIDL整体代码流程

      本文链接:https://www.haomeiwen.com/subject/xlfryqtx.html