Binder-从Java层说起

发布时间:2017-7-9 7:18:26编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Binder-从Java层说起 ",主要涉及到Binder-从Java层说起 方面的内容,对于Binder-从Java层说起 感兴趣的同学可以参考一下。

  Java层的binder是对C++层的Native Binder的封装,提供给其他Java code方便使用;关于与Native Binder的监听与交互,是交给Java Binder的jni部分完成的

这里涉及的code如下

framework/base/core/java/android/os/      - IInterface.java    - IBinder.java    - Parcel.java    - IServiceManager.java    - ServiceManager.java    - ServiceManagerNative.java    - Binder.java  framework/base/core/jni/        - android_os_Parcel.cpp    - AndroidRuntime.cpp    - android_util_Binder.cpp (核心类)

总体的架构图如下

 整个Binder流程里涉及到的类如下

先看Java层的code

其中IBinder是用来做通信的接口,IInterface是程序直接逻辑方法的接口

各个类之间的关系如下图

 首先看这个类图里的IBinder接口

  这个接口里定义的是一些常量flag和IPC协议必须实现的接口

  这个接口的核心方法是transact(对应服务端有响应的onTransact方法),发送本地方法给远程服务端。方法的参数通过Parcel对象传递过去

先是定义操作码的范围

1 85public interface IBinder {2 86    /**3 87     * The first transaction code available for user commands.4 88     */5 89    int FIRST_CALL_TRANSACTION  = 0x00000001;6 90    /**7 91     * The last transaction code available for user commands.8 92     */9 93    int LAST_CALL_TRANSACTION   = 0x00ffffff;

然后是这pingBinder,dump,shell command,interface,tweet,like,sysprops几个函数的操作码

 1 95    /** 2 96     * IBinder protocol transaction code: pingBinder(). 3 97     */ 4 98    int PING_TRANSACTION        = ('_'<<24)|('P'<<16)|('N'<<8)|'G'; 5 99 6 100    /** 7 101     * IBinder protocol transaction code: dump internal state. 8 102     */ 9 103    int DUMP_TRANSACTION        = ('_'<<24)|('D'<<16)|('M'<<8)|'P';10 10411 105    /**12 106     * IBinder protocol transaction code: execute a shell command.13 107     * @hide14 108     */15 109    int SHELL_COMMAND_TRANSACTION = ('_'<<24)|('C'<<16)|('M'<<8)|'D';16 11017 111    /**18 112     * IBinder protocol transaction code: interrogate the recipient side19 113     * of the transaction for its canonical interface descriptor.20 114     */21 115    int INTERFACE_TRANSACTION   = ('_'<<24)|('N'<<16)|('T'<<8)|'F';22 11623 117    /**24 118     * IBinder protocol transaction code: send a tweet to the target25 119     * object.  The data in the parcel is intended to be delivered to26 120     * a shared messaging service associated with the object; it can be27 121     * anything, as long as it is not more than 130 UTF-8 characters to28 122     * conservatively fit within common messaging services.  As part of29 123     * {@link Build.VERSION_CODES#HONEYCOMB_MR2}, all Binder objects are30 124     * expected to support this protocol for fully integrated tweeting31 125     * across the platform.  To support older code, the default implementation32 126     * logs the tweet to the main log as a simple emulation of broadcasting33 127     * it publicly over the Internet.34 128     *35 129     * <p>Also, upon completing the dispatch, the object must make a cup36 130     * of tea, return it to the caller, and exclaim "jolly good message37 131     * old boy!".38 132     */39 133    int TWEET_TRANSACTION   = ('_'<<24)|('T'<<16)|('W'<<8)|'T';40 13441 135    /**42 136     * IBinder protocol transaction code: tell an app asynchronously that the43 137     * caller likes it.  The app is responsible for incrementing and maintaining44 138     * its own like counter, and may display this value to the user to indicate the45 139     * quality of the app.  This is an optional command that applications do not46 140     * need to handle, so the default implementation is to do nothing.47 141     *48 142     * <p>There is no response returned and nothing about the49 143     * system will be functionally affected by it, but it will improve the50 144     * app's self-esteem.51 145     */52 146    int LIKE_TRANSACTION   = ('_'<<24)|('L'<<16)|('I'<<8)|'K';53 14754 148    /** @hide */55 149    int SYSPROPS_TRANSACTION = ('_'<<24)|('S'<<16)|('P'<<8)|'R';

 然后是发给transact的flag

1 151    /**2 152     * Flag to {@link #transact}: this is a one-way call, meaning that the3 153     * caller returns immediately, without waiting for a result from the4 154     * callee. Applies only if the caller and callee are in different5 155     * processes.6 156     */7 157    int FLAG_ONEWAY             = 0x00000001;

说明这个IPC调用马上就可以返回,不需要等待结果

然后是buffer大小限制,64K

1 159    /**2 160     * Limit that should be placed on IPC sizes to keep them safely under the3 161     * transaction buffer limit.4 162     * @hide5 163     */6 164    public static final int MAX_IPC_SIZE = 64 * 1024;

下面这几个方法是binder通信时需要实现的,可以探知binder的状态,通过transact做通信

 1 166    /** 2 167     * Get the canonical name of the interface supported by this binder. 3 168     */ 4 169    public String getInterfaceDescriptor() throws RemoteException; 5 170 6 171    /** 7 172     * Check to see if the object still exists. 8 173     * 9 174     * @return Returns false if the10 175     * hosting process is gone, otherwise the result (always by default11 176     * true) returned by the pingBinder() implementation on the other12 177     * side.13 178     */14 179    public boolean pingBinder();15 18016 181    /**17 182     * Check to see if the process that the binder is in is still alive.18 183     *19 184     * @return false if the process is not alive.  Note that if it returns20 185     * true, the process may have died while the call is returning.21 186     */22 187    public boolean isBinderAlive();23 18824 189    /**25 190     * Attempt to retrieve a local implementation of an interface26 191     * for this Binder object.  If null is returned, you will need27 192     * to instantiate a proxy class to marshall calls through28 193     * the transact() method.29 194     */30 195    public IInterface queryLocalInterface(String descriptor);31 19632 197    /**33 198     * Print the object's state into the given stream.34 199     *35 200     * @param fd The raw file descriptor that the dump is being sent to.36 201     * @param args additional arguments to the dump request.37 202     */38 203    public void dump(FileDescriptor fd, String[] args) throws RemoteException;39 20440 205    /**41 206     * Like {@link #dump(FileDescriptor, String[])} but always executes42 207     * asynchronously.  If the object is local, a new thread is created43 208     * to perform the dump.44 209     *45 210     * @param fd The raw file descriptor that the dump is being sent to.46 211     * @param args additional arguments to the dump request.47 212     */48 213    public void dumpAsync(FileDescriptor fd, String[] args) throws RemoteException;49 21450 215    /**51 216     * Execute a shell command on this object.  This may be performed asynchrously from the caller;52 217     * the implementation must always call resultReceiver when finished.53 218     *54 219     * @param in The raw file descriptor that an input data stream can be read from.55 220     * @param out The raw file descriptor that normal command messages should be written to.56 221     * @param err The raw file descriptor that command error messages should be written to.57 222     * @param args Command-line arguments.58 223     * @param resultReceiver Called when the command has finished executing, with the result code.59 224     * @hide60 225     */61 226    public void shellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,62 227            String[] args, ResultReceiver resultReceiver) throws RemoteException;63 228

然后是类DeathRecipient,用来监听binder进程死亡消息,其中的binderDied为回调函数,通知死亡

1 246    /**2 247     * Interface for receiving a callback when the process hosting an IBinder3 248     * has gone away.4 249     *5 250     * @see #linkToDeath6 251     */7 252    public interface DeathRecipient {8 253        public void binderDied();9 254    }

然后是注册和取消注册死亡通知对象方法,仅对远程有限,对本地无效

 1 256    /** 2 257     * Register the recipient for a notification if this binder 3 258     * goes away.  If this binder object unexpectedly goes away 4 259     * (typically because its hosting process has been killed), 5 260     * then the given {@link DeathRecipient}'s 6 261     * {@link DeathRecipient#binderDied DeathRecipient.binderDied()} method 7 262     * will be called. 8 263     * 9 264     * <p>You will only receive death notifications for remote binders,10 265     * as local binders by definition can't die without you dying as well.11 266     *12 267     * @throws RemoteException if the target IBinder's13 268     * process has already died.14 269     *15 270     * @see #unlinkToDeath16 271     */17 272    public void linkToDeath(DeathRecipient recipient, int flags)18 273            throws RemoteException;19 27420 275    /**21 276     * Remove a previously registered death notification.22 277     * The recipient will no longer be called if this object23 278     * dies.24 279     *25 280     * @return {@code true} if the <var>recipient</var> is successfully26 281     * unlinked, assuring you that its27 282     * {@link DeathRecipient#binderDied DeathRecipient.binderDied()} method28 283     * will not be called;  {@code false} if the target IBinder has already29 284     * died, meaning the method has been (or soon will be) called.30 285     *31 286     * @throws java.util.NoSuchElementException if the given32 287     * <var>recipient</var> has not been registered with the IBinder, and33 288     * the IBinder is still alive.  Note that if the <var>recipient</var>34 289     * was never registered, but the IBinder has already died, then this35 290     * exception will <em>not</em> be thrown, and you will receive a false36 291     * return value instead.37 292     */38 293    public boolean unlinkToDeath(DeathRecipient recipient, int flags);

 然后是IInterface接口,该接口比较简单,仅仅一个asBinder方法,返回IBinder通讯对象。所有要支持binder的类必须实现这个接口,只有这样,才能有能力做binder通信。AIDL就是实现这个接口,外加自己的逻辑功能接口

 1 17package android.os; 2 18 3 19/** 4 20 * Base class for Binder interfaces.  When defining a new interface, 5 21 * you must derive it from IInterface. 6 22 */ 7 23public interface IInterface 8 24{ 9 25    /**10 26     * Retrieve the Binder object associated with this interface.11 27     * You must use this instead of a plain cast, so that proxy objects12 28     * can return the correct result.13 29     */14 30    public IBinder asBinder();15 31}16 32


上一篇:WebService 获取当前URL
下一篇:详解 Webpack+Babel+React 开发环境的搭建

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款