1# IPC与RPC通信开发指导 2 3 4## 场景介绍 5 6IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,包括Proxy和Stub运行在不同设备的情况。 7 8 9## 接口说明 10 11**表1** Native侧IPC接口 12 13| 类/接口 | 方法 | 功能说明 | 14| -------- | -------- | -------- | 15| IRemoteBroker | sptr<IRemoteObject> AsObject() | 返回通信对象。派生类需要实现,Stub端返回RemoteObject对象本身,Proxy端返回代理对象。 | 16| IRemoteStub | virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) | 请求处理方法,派生类需要重写,处理Proxy的请求并返回结果。 | 17| IRemoteProxy | | 业务Proxy类派生自IRemoteProxy类。 | 18 19 20## 开发步骤 21 22**Native侧开发步骤** 23 241. 定义IPC接口ITestAbility 25 SA接口继承IPC基类接口IRemoteBroker,接口里定义描述符、业务函数和消息码,其中业务函数在Proxy端和Stub端都需要实现。 26 27 ``` 28 class ITestAbility : public IRemoteBroker { 29 public: 30 // DECLARE_INTERFACE_DESCRIPTOR是必须的,入参需使用std::u16string; 31 DECLARE_INTERFACE_DESCRIPTOR(u"test.ITestAbility"); 32 int TRANS_ID_PING_ABILITY = 1; // 定义消息码 33 virtual int TestPingAbility(const std::u16string &dummy) = 0; // 定义业务函数 34 }; 35 ``` 36 372. 定义和实现服务端TestAbilityStub 38 该类是和IPC框架相关的实现,需要继承 IRemoteStub<ITestAbility>。Stub端作为接收请求的一端,需重写OnRemoteRequest方法用于接收客户端调用。 39 40 ``` 41 class TestAbilityStub : public IRemoteStub<ITestAbility> { 42 public: 43 virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 44 int TestPingAbility(const std::u16string &dummy) override; 45 }; 46 47 int TestServiceStub::OnRemoteRequest(uint32_t code, 48 MessageParcel &data, MessageParcel &reply, MessageOption &option) 49 { 50 switch (code) { 51 case TRANS_ID_PING_ABILITY: { 52 std::u16string dummy = data.ReadString16(); 53 int result = TestPingAbility(dummy); 54 reply.WriteInt32(result); 55 return 0; 56 } 57 default: 58 return IPCObjectStub::OnRemoteRequest(code, data, reply, option); 59 } 60 } 61 ``` 62 633. 定义服务端业务函数具体实现类TestAbility 64 ``` 65 class TestAbility : public TestAbilityStub { 66 public: 67 int TestPingAbility(const std::u16string &dummy); 68 } 69 70 int TestAbility::TestPingAbility(const std::u16string &dummy) { 71 return 0; 72 } 73 ``` 74 754. 定义和实现客户端 TestAbilityProxy 76 该类是Proxy端实现,继承IRemoteProxy<ITestAbility>,调用SendRequest接口向Stub端发送请求,对外暴露服务端提供的能力。 77 78 ``` 79 class TestAbilityProxy : public IRemoteProxy<ITestAbility> { 80 public: 81 explicit TestAbilityProxy(const sptr<IRemoteObject> &impl); 82 int TestPingService(const std::u16string &dummy) override; 83 private: 84 static inline BrokerDelegator<TestAbilityProxy> delegator_; // 方便后续使用iface_cast宏 85 } 86 87 TestAbilityProxy::TestAbilityProxy(const sptr<IRemoteObject> &impl) 88 : IRemoteProxy<ITestAbility>(impl) 89 { 90 } 91 92 int TestAbilityProxy::TestPingService(const std::u16string &dummy){ 93 MessageOption option; 94 MessageParcel dataParcel, replyParcel; 95 dataParcel.WriteString16(dummy); 96 int error = Remote()->SendRequest(TRANS_ID_PING_ABILITY, dataParcel, replyParcel, option); 97 int result = (error == ERR_NONE) ? replyParcel.ReadInt32() : -1; 98 return result; 99 } 100 ``` 101 1025. SA 注册与启动 103 SA 需要将自己的 TestAbilityStub实例通过 AddSystemAbility接口注册到 SystemAbilityManager,设备内与分布式的注册参数不同。 104 105 ``` 106 // 注册到本设备内 107 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 108 samgr->AddSystemAbility(said, new TestAbility()); 109 110 // 在组网场景下,会被同步到其他设备上 111 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 112 ISystemAbilityManager::SAExtraProp saExtra; 113 saExtra.isDistributed = true; // 设置为分布式SA 114 int result = samgr->AddSystemAbility(said, new TestAbility(), saExtra); 115 ``` 116 1176. SA 获取与调用 118 通过SystemAbilityManager的GetSystemAbility方法可获取到对应SA的代理IRemoteObject,然后构造TestAbilityProxy即可。 119 120 ``` 121 // 获取本设备内注册的SA的proxy 122 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 123 sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(said); 124 sptr<ITestAbility> testAbility = iface_cast<ITestAbility>(remoteObject); // 使用iface_cast宏转换成具体类型 125 126 // 获取其他设备注册的SA的Proxy 127 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 128 sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(sdid, deviceId); // deviceId是指定设备的标识符 129 sptr<TestAbilityProxy> proxy(new TestAbilityProxy(remoteObject)); // 直接构造具体Proxy 130 ``` 131