AndroidBinder源码阅读

更新时间:2023-06-24 12:01:48 阅读: 评论:0

AndroidBinder源码阅读
作为Android 主要的IPC⽅式Binder, 其实现了IBinder接⼝,通过代理模式,parcelable序列化反序列操作,实现了RPC,即跨进程调⽤
1.IBinder接⼝
⾸先Binder 实现了IBinder接⼝,故先来看看IBinder接⼝,IBinder代码只有四⼗⾏功能⽐较清晰。
public interface IBinder {
//IBinder协议传输码,⽤来获得⽹络状态
int DUMP_TRANSACTION = 1598311760;
//第⼀次传输回调传输码
int FIRST_CALL_TRANSACTION = 1;
//IBinder协议传输码,⽤于 transact(int, Parcel, Parcel, int),oneway意味着caller返回很快,不会等待结果
int FLAG_ONEWAY = 1;
//Binder协议传输码,⽤于查询事务的接收⽅以获得其规范接⼝描述符。
int INTERFACE_TRANSACTION = 1598968902;取决
//上⼀次传输回调传输码
int LAST_CALL_TRANSACTION = 16777215;
//IBinder协议传输码,异步调⽤app
int LIKE_TRANSACTION = 1598835019;
//IBinder协议传输码,pingBinder()的传输码
int PING_TRANSACTION = 1599098439;
//IBinder协议传输码,向对象发送⼀个tweet
int TWEET_TRANSACTION = 1599362900;
@Nullable
//获取接⼝描述
String getInterfaceDescriptor() throws RemoteException;
//检查对象是否存在
boolean pingBinder();
//检查进程的Binder是否活着
boolean isBinderAlive();
//尝试检索⼀个Binder的本地接⼝
@Nullable
IInterface queryLocalInterface(@NonNull String var1);
//获得对象的状态
void dump(@NonNull FileDescriptor var1, @Nullable String[] var2) throws RemoteException;
//异步的获得对象的状态
void dumpAsync(@NonNull FileDescriptor var1, @Nullable String[] var2) throws RemoteException;
//对对象执⾏泛型操作
boolean transact(int var1, @NonNull Parcel var2, @Nullable Parcel var3, int var4) throws RemoteException;
taking over me//注册Binder死亡监听
void linkToDeath(@NonNull IBinder.DeathRecipient var1, int var2) throws RemoteException;
贯通日本语//移除Binder死亡监听
boolean unlinkToDeath(@NonNull IBinder.DeathRecipient var1, int var2);
//Binder死亡监听回调接⼝
public interface DeathRecipient {
void binderDied();
}
}
2.Binder源码分析
2.1 内部类 NoImagePreloadHolder
//使⽤Holder允许在引导映像中静态初始化绑定器,是为了避免⼀些初始化排序可能的问题。
private static class NoImagePreloadHolder {
public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
ClassLoader(), getNativeFinalizer(), NATIVE_ALLOCATION_SIZE);  }
2.2Binder内部变量
FIND_POTENTIAL_LEAKS ⽤于设置FLAG是否检查可能的内存泄露
CHECK_PARCEL_SIZE ⽤于设置FLAG是否检查Parcel ⼤⼩
LOG_RUNTIME_EXCEPTION ⽤于设置FLAG是否检查运⾏异常
sDumpDisabled 控制dump是否可被调⽤
sTransactionTracker 全局进程transaction tracker
2.3 主要的业务函数
pintBinder 默认返回true
isBinderAlive 默认返回true
onTransact
transact()的回调函数,对传输对象进⾏序列化操作
//data对象是序列化Parcel类携带数据,reply对象是序列化类返回给调⽤者的结果
//往返回值中写⼊接⼝描述字段
if (code == INTERFACE_TRANSACTION) {
reply.writeString(getInterfaceDescriptor());
return true;
} el if (code == DUMP_TRANSACTION) {
ParcelFileDescriptor fd = adFileDescriptor();
String[] args = adStringArray();
if (fd != null) {
try {
FileDescriptor(), args);
} finally {
IoUtils.cloQuietly(fd);
}
}
// Write the StrictMode header.
if (reply != null) {
日语考试培训班reply.writeNoException();
} el {
StrictMode.clearGatheredViolations();
}
return true;
} el if (code == SHELL_COMMAND_TRANSACTION) {rival
ParcelFileDescriptor in = adFileDescriptor();
ParcelFileDescriptor out = adFileDescriptor();
ParcelFileDescriptor err = adFileDescriptor();
String[] args = adStringArray();
ShellCallback shellCallback = ateFromParcel(data);
ResultReceiver resultReceiver = ateFromParcel(data);
try {
if (out != null) {
shellCommand(in != null ? in.getFileDescriptor() : null,
err != null ? FileDescriptor() : FileDescriptor(),
args, shellCallback, resultReceiver);
}
} finally {
IoUtils.cloQuietly(in);
IoUtils.cloQuietly(out);
IoUtils.cloQuietly(err);
// Write the StrictMode header.
if (reply != null) {
reply.writeNoException();
} el {
StrictMode.clearGatheredViolations();
}
}
return true;
}
return fal;
}
transact() 默认的实现传递parcel对象并调⽤onTransact。在客户端,transact会调⽤Binder 代理类实现IPC,跨进程调⽤
if (fal) Log.v("Binder", "Transact: " + code + " to " + this);
if (data != null) {
data.tDataPosition(0);机械制图培训
}
boolean r = onTransact(code, data, reply, flags);
伴娘我最大if (reply != null) {
reply.tDataPosition(0);
}
return r;
}
客户端中的BinderProxy的transact() 中调⽤native函数 transactNative进⾏传值
public boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {        Binder.checkParcel(this, code, data, "Unreasonably large binder buffer");
if (mWarnOnBlocking && ((flags & FLAG_ONEWAY) == 0)) {
// For now, avoid spamming the log by disabling after we've logged
// about this interface at least once
mWarnOnBlocking = fal;
Log.w(Binder.TAG, "Outgoing transactions from this process must be FLAG_ONEWAY",                    new Throwable());
}
final boolean tracingEnabled = Binder.isTracingEnabled();
respring
if (tracingEnabled) {
新奥尔良黄蜂
final Throwable tr = new Throwable();
StackTraceElement stackTraceElement = tr.getStackTrace()[1];
try {
return transactNative(code, data, reply, flags);
} finally {
if (tracingEnabled) {
}
pirate}
}

本文发布于:2023-06-24 12:01:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/155958.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:对象   是否   检查   传输码   序列化   返回   获得
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图