hidl之client/server端的双向死亡监听

我们在AIDL跨进程通信中知道,可以通过Binder的linkToDeath()实现client/server端的双向死亡监听。那么HIDL可以吗?本文以Android应用为client端,Hal Service为server端。一起看看吧~~

基础知识参考官方文档。https://source.android.com/docs/core/architecture/hidl(打不开链接的小伙伴,不可能是长得太帅)

1.Server端

TestManager继承了类 hidl_death_recipient并重新它的serviceDied方法,从而实现死亡监听。

TestManager.h中部分代码:

class TestManager : public hidl_death_recipient {

        ...
        void serviceDied(uint64_t cookie, const android::wp<::android::hidl::base::V1_0::IBase>& who) override;
        ...
};

TestManager.cpp中部分代码:

//register callback and return cookie id
int32_t TestManager::subscribe(const sp<ITestServerCallback>& callback) {
    ALOGI("TestManager subscribe, callback=%p\n", callback.get());
    lock_guard<mutex> lk(mMut);
    ALOGI("TestManager subscribe>>>mDeathCount =%lu\n", mDeathCount);
    int32_t cookie = 0;
    mCallbackMap.insert(std::pair<int, sp<ITestServerCallback>>(mDeathCount, callback));
    if (mChd != nullptr) {
        ALOGI("TestManager subscribe>>>linkToDeath\n");
        callback->linkToDeath(mChd, mDeathCount);
        cookie = mDeathCount;
        mDeathCount++;
    } else {
        ALOGI("TestManager mChd is null\n");
    }
    return cookie;
}

//unregister callback by cookie id
int32_t TestManager::unsubscribe(int32_t cookie) {
    ALOGI("TestManager unsubscribe, cookie=%d\n", cookie);
    int result = mCallbackMap.erase(cookie);
    ALOGI("TestManager unsubscribe result is %d", result);
    return cookie;
}

//listening service died
void TestManager::serviceDied(uint64_t cookie, const android::wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
    lock_guard<mutex> lk(mMut);
    ALOGI("TestManager serviceDied cookie is %lu, callbackMap size is %lu\n", cookie , mCallbackMap.size());
    int result = mCallbackMap.erase(cookie);
    ALOGI("TestManager erase result is %d", result);
}

2.Client端

2.1.获取service实例

private IHwBinder.DeathRecipient mDeathDecipient = new IHwBinder.DeathRecipient() {
   @Override public void serviceDied(long cookie) {
     try {
       Log.d(TAG, "mService serviceDied cookie = " + cookie);
       mService.asBinder().unlinkToDeath(this);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 };
 
private void initHalService() {
  try {
    mService = ITestServer.getService();
    mService.asBinder().linkToDeath(mDeathDecipient, 42);
  } catch (Exception e) {
    Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
  }
}

2.2.Callback的注册、注销

if (v.getId() == R.id.subscribe) {
  try {
    if (mCallback == null) {
      mCallback = new MyCallback();
    }
    Log.d(TAG, "subscribe mCallback = " + mCallback);
    int cookie = mService.subscribe(mCallback);
    cookieList.add(cookie);
  } catch (Exception e) {
    e.printStackTrace();
  }
} else if (v.getId() == R.id.unsubscribe) {
  try {
    Iterator<Integer> iterator = cookieList.iterator();
    while (iterator.hasNext()) {
      int cookie = iterator.next();
      Log.d(TAG, "unsubscribe cookie = " + cookie);
      if (cookie != 0) {
        mService.unsubscribe(cookie);
        iterator.remove();
      }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}

3.运行结果

3.1.正常注册、注销

log如下:

09-23 19:22:11.374 7974 7974 D TestManager: subscribe mCallback = vendor.test.testserver@1.0::ITestServerCallback@Stub
09-23 19:22:11.390 8005 8007 I : TestManager subscribe, callback=0x70bf25b380
09-23 19:22:11.390 8005 8007 I : TestManager subscribe>>>mDeathCount =1
09-23 19:22:11.390 8005 8007 I : TestManager subscribe>>>linkToDeath
09-23 19:22:14.321 7974 7974 D TestManager: unsubscribe cookie = 1
09-23 19:22:14.322 8005 8007 I : TestManager unsubscribe, cookie=1
09-23 19:22:14.326 8005 8007 I : TestManager unsubscribe result is 1

3.2.Client异常退出

log如下:

09-23 19:23:23.669 7974 7974 D TestManager: subscribe mCallback = vendor.test.testserver@1.0::ITestServerCallback@Stub
09-23 19:23:23.670 8005 8007 I : TestManager subscribe, callback=0x70bf25b380
09-23 19:23:23.670 8005 8007 I : TestManager subscribe>>>mDeathCount =2
09-23 19:23:23.670 8005 8007 I : TestManager subscribe>>>linkToDeath
09-23 19:23:30.171 8005 8007 I : TestManager serviceDied cookie is 2, callbackMap size is 1
09-23 19:23:30.171 8005 8007 I : TestManager erase result is 1

3.3.Server异常退出

log如下:

09-23 19:24:24.457 8011 8011 D TestManager: subscribe mCallback = vendor.test.testserver@1.0::ITestServerCallback@Stub
09-23 19:24:24.483 8005 8007 I : TestManager subscribe, callback=0x70bf25b380
09-23 19:24:24.483 8005 8007 I : TestManager subscribe>>>mDeathCount =3
09-23 19:24:24.483 8005 8007 I : TestManager subscribe>>>linkToDeath
09-23 19:24:28.544 8011 8036 D TestManager: mService serviceDied cookie = 42

4.完整代码

https://gitee.com/lxy0502/hidl-usage

参考链接

https://blog.csdn.net/u014787262/article/details/122792112#comments_23277356

如有问题,欢迎留言~~

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值