rpc与ipc
进程间通信(ipc)是跨进程调用(rpc)的基础。rpc的一个典型的使用场景是,进程client调用server的某一个函数,例如add。ipc要解决的问题是,不同进程之间的数据传递(parcel),同步(mutex)等问题,而rpc要更多,例如,调用某个函数(handle)的问题。
aidl
android中的进程,有的是有init.rc中配置,系统初始化的时候便启动进程,有的是通过ams调用fork来启动。service_manager是在init.rc中配置启动的。android中的大量自带的service,均在service_manager启动之后,向其注册。在这篇文章中,我们使用一个简单的例子,自定义一个service,手动启动。
在aidl用来定义进程之间的接口,由于android应用进程与他进程频繁通讯,所以有大量的aidl文件。通一个简单的例子,看清aidl的使用方式和调用过程,然后,分析一下AMS如何使用aidl的,之后,我们可以理解,为何使用aidl会让调用的方式变得简洁(开发层面,实质还是rpc没变)。
简单的例子来看看其结构:
// IText.aidl
package com.example.aidl;
// Declare any non-default types here with import statements
interface IText {
//仅定义一个方法
int add(int a, int b);
}
编译时生成java文件
public interface IText extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.example.aidl.IText
{
private static final java.lang.String DESCRIPTOR = "com.example.aidl.IText";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.aidl.IText interface,
* generating a proxy if needed.
*/
public static com.example.aidl.IText asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.aidl.IText))) {
return ((com.example.aidl.IText)iin);
}
return new com.example.aidl.IText.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
java.lang.String descriptor = DESCRIPTOR;
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(descriptor);
return true;
}
case TRANSACTION_add:
{
data.enforceInterface(descriptor);
int _arg0;
_arg0 = data.readInt();
int _arg1;
_arg1 = data.readInt();
int _result = this.add(_arg0, _arg1);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
default:
{
return super.onTransact(code, data, reply, flags);
}
}
}
private static class Proxy implements com.example.aidl.IText
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public int add(int a, int b) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(a);
_data.writeInt(b);
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public int add(int a, int b) throws android.os.RemoteException;
}
编译后,aidl文件将生成一个接口,它本身也继续自IInterface,IInterface,仅有一个返回IBinder的方法.
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.os;
/**
* Base class for Binder interfaces. When defining a new interface,
* you must derive it from IInterface.
*/
public interface IInterface
{
/**
* Retrieve the Binder object associated with this interface.
* You must use this instead of a plain cast, so that proxy objects
* can return the correct result.
*/
public IBinder asBinder();
}
IText.aidl文件生成IText.java文件之后,会生成一个Stub抽象嵌套类,而Stub包含一个Proxy的静态内部类,Stub和Proxy均继承IText,Proxy实现了在aidl中定义的add方法,实现的方式是通过远程调用。