From bb076cd6bec7ee629e896b05182d5db49f8e9578 Mon Sep 17 00:00:00 2001 From: wangqilong2 Date: Sat, 17 Dec 2022 18:51:31 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=9112-17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangqilong2 --- .../ipc-rpc-development-guideline.md | 1 - .../connectivity/subscribe-remote-state.md | 55 ++++++++++++++++++- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/zh-cn/application-dev/connectivity/ipc-rpc-development-guideline.md b/zh-cn/application-dev/connectivity/ipc-rpc-development-guideline.md index 4bfaf67a20..d6328d5580 100755 --- a/zh-cn/application-dev/connectivity/ipc-rpc-development-guideline.md +++ b/zh-cn/application-dev/connectivity/ipc-rpc-development-guideline.md @@ -161,7 +161,6 @@ include_dirs = [ ``` import rpc from "@ohos.rpc" import featureAbility from "@ohos.ability.featureAbility" -] ``` 2. 客户端构造变量want,指定要绑定的Ability所在应用的包名、组件名,如果是跨设备的场景,还需要目标设备NetworkId。构造变量connect,指定绑定成功、绑定失败、断开连接时的回调函数。使用featureAbility提供的接口绑定Ability。 diff --git a/zh-cn/application-dev/connectivity/subscribe-remote-state.md b/zh-cn/application-dev/connectivity/subscribe-remote-state.md index 427227ddd3..76b010f000 100755 --- a/zh-cn/application-dev/connectivity/subscribe-remote-state.md +++ b/zh-cn/application-dev/connectivity/subscribe-remote-state.md @@ -15,20 +15,69 @@ IPC/RPC提供对远端Stub对象状态的订阅机制, 在远端Stub对象死 | RemoveDeathRecipient(const sptr\ &recipient); | bool | 取消订阅远端Stub对象状态。 | | OnRemoteDied(const wptr\ &object); | void | 当远端Stub对象死亡时回调。 | - ### 参考代码 +```C++ +//定义消息码 +enum { + TRANS_ID_PING_ABILITY = 5, + TRANS_ID_REVERSED_MONITOR +}; + +class ITestService : public IRemoteBroker { +public: + // DECLARE_INTERFACE_DESCRIPTOR是必需的,入参需使用std::u16string; + DECLARE_INTERFACE_DESCRIPTOR(to_utf16(DESCRIPTOR)); + virtual int TestPingAbility(const std::u16string &dummy) = 0; // 定义业务函数 +}; + +class TestServiceProxy : public IRemoteProxy { +public: + explicit TestAbilityProxy(const sptr &impl); + virtual int TestPingAbility(const std::u16string &dummy) override; + int TestAnonymousStub(); +private: + static inline BrokerDelegator delegator_; // 方便后续使用iface_cast宏 +}; + +TestServiceProxy::TestServiceProxy(const sptr &impl) + : IRemoteProxy(impl) +{ +} + +int TestServiceProxy::TestPingAbility(const std::u16string &dummy){ + MessageOption option; + MessageParcel dataParcel, replyParcel; + dataParcel.WriteString16(dummy); + int error = PeerHolder::Remote()->SendRequest(TRANS_ID_PING_ABILITY, dataParcel, replyParcel, option); + int result = (error == ERR_NONE) ? replyParcel.ReadInt32() : -1; + return result; +} +``` + + + ``` class TestDeathRecipient : public IRemoteObject::DeathRecipient { public: virtual void OnRemoteDied(const wptr& remoteObject); } + +void TestDeathRecipient::OnRemoteDied(const wptr& remoteObject) +{ +} +``` + +```c++ +sptr object = new IPCObjectProxy(1, to_utf16(DESCRIPTOR)); sptr deathRecipient (new TestDeathRecipient());// 构造一个死亡通知对象 -bool result = proxy->AddDeathRecipient(deathRecipient); // 注册死亡通知 -result = proxy->RemoveDeathRecipient(deathRecipient); // 移除死亡通知 +bool result = object->AddDeathRecipient(deathRecipient); // 注册死亡通知 +result = object->RemoveDeathRecipient(deathRecipient); // 移除死亡通知 ``` + + ## Stub感知Proxy死亡(匿名Stub的使用) 正向的死亡通知是Proxy感知Stub的状态,若想达到反向的死亡通知,即Stub感知Proxy的状态,可以巧妙的利用正向死亡通知。如两个进程A(原Stub所在进程)和B(原Proxy所在进程),进程B在获取到进程A的Proxy对象后,在B进程新建一个匿名Stub对象(匿名指未向SAMgr注册),可称之为回调Stub,再通过SendRequest接口将回调Stub传给进程A的原Stub。这样一来,进程A便获取到了进程B的回调Proxy。当进程B死亡或B所在设备离开组网时,回调Stub会死亡,回调Proxy会感知,进而通知给原Stub,便实现了反向死亡通知。 -- GitLab