未验证 提交 e52505dc 编写于 作者: O openharmony_ci 提交者: Gitee

!9114 rpc 新增onRemoteReuqstEx接口说明文档

Merge pull request !9114 from chenyuyan/apidocs
......@@ -3998,9 +3998,12 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me
```
### onRemoteRequest
### onRemoteRequest<sup>8+(deprecated)</sup>
onRemoteRequest(code : number, data : MessageParcel, reply: MessageParcel, options : MessageOption): boolean
> **说明:**
> 从 API Version 9 开始废弃,建议使用[onRemoteRequestEx<sup>9+</sup>](#onremoterequestex)替代。
sendRequestAsync请求的响应处理函数,服务端在该函数里处理请求,回复结果。
......@@ -4024,7 +4027,7 @@ sendRequestAsync请求的响应处理函数,服务端在该函数里处理请
**示例:**
```
```ets
class MyDeathRecipient {
onRemoteDied() {
console.log("server died");
......@@ -4055,8 +4058,191 @@ sendRequestAsync请求的响应处理函数,服务端在该函数里处理请
}
}
```
### onRemoteRequestEx<sup>9+</sup>
onRemoteRequestEx(code : number, data : MessageParcel, reply: MessageParcel, options : MessageOption): boolean | Promise <boolean>
> **说明:**
>- 开发者应优先选择重载onRemoteRequestEx方法,其中可以自由实现同步和异步的消息处理。
>- 开发者同时重载onRemoteRequest和onRemoteRequestEx方法时,仅onRemoteRequestEx方法生效。
sendRequestAsync请求的响应处理函数,服务端在该函数里同步或异步地处理请求,回复结果。
**系统能力**:SystemCapability.Communication.IPC.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| code | number | 是 | 对端发送的服务请求码。 |
| data | [MessageParcel](#messageparcel) | 是 | 携带客户端调用参数的MessageParcel对象。 |
| reply | [MessageParcel](#messageparcel) | 是 | 写入结果的MessageParcel对象。 |
| option | [MessageOption](#messageoption) | 是 | 指示操作是同步还是异步。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| boolean | 若在onRemoteRequestEx中同步地处理请求,则返回一个布尔值:操作成功,则返回true;否则返回false。 |
| Promise <boolean> | 若在onRemoteRequestEx中异步地处理请求,则返回一个Promise对象。 |
**重载onRemoteRequestEx方法同步处理请求示例:**
```ets
class MyDeathRecipient {
onRemoteDied() {
console.log("server died");
}
}
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor) {
super(descriptor);
}
addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
return true;
}
removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
return true;
}
isObjectDead(): boolean {
return false;
}
onRemoteRequestEx(code, data, reply, option) {
if (code === 1) {
console.log("RpcServer: sync onRemoteRequestEx is called");
return true;
} else {
console.log("RpcServer: unknown code: " + code);
return false;
}
}
}
```
**重载onRemoteRequestEx方法异步处理请求示例:**
```ets
class MyDeathRecipient {
onRemoteDied() {
console.log("server died");
}
}
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor) {
super(descriptor);
}
addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
return true;
}
removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
return true;
}
isObjectDead(): boolean {
return false;
}
async onRemoteRequestEx(code, data, reply, option) {
if (code === 1) {
console.log("RpcServer: async onRemoteRequestEx is called");
} else {
console.log("RpcServer: unknown code: " + code);
return false;
}
await new Promise((resolve) => {
setTimeout(resolve, 100);
})
return true;
}
}
```
**同时重载onRemoteRequestEx和onRemoteRequest方法同步处理请求示例:**
```ets
class MyDeathRecipient {
onRemoteDied() {
console.log("server died");
}
}
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor) {
super(descriptor);
}
addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
return true;
}
removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
return true;
}
isObjectDead(): boolean {
return false;
}
onRemoteRequest(code, data, reply, option) {
if (code === 1) {
console.log("RpcServer: sync onRemoteRequestEx is called");
return true;
} else {
console.log("RpcServer: unknown code: " + code);
return false;
}
}
// 同时调用仅会执行onRemoteRequestEx
onRemoteRequestEx(code, data, reply, option) {
if (code === 1) {
console.log("RpcServer: async onRemoteRequestEx is called");
} else {
console.log("RpcServer: unknown code: " + code);
return false;
}
return true;
}
}
```
**同时重载onRemoteRequestEx和onRemoteRequest方法异步处理请求示例:**
```ets
int index = 0;
class MyDeathRecipient {
onRemoteDied() {
console.log("server died");
}
}
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor) {
super(descriptor);
}
addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
return true;
}
removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
return true;
}
isObjectDead(): boolean {
return false;
}
onRemoteRequest(code, data, reply, option) {
if (code === 1) {
console.log("RpcServer: sync onRemoteRequestEx is called");
return true;
} else {
console.log("RpcServer: unknown code: " + code);
return false;
}
}
// 同时调用仅会执行onRemoteRequestEx
async onRemoteRequestEx(code, data, reply, option) {
if (code === 1) {
console.log("RpcServer: async onRemoteRequestEx is called");
} else {
console.log("RpcServer: unknown code: " + code);
return false;
}
await new Promise((resolve) => {
setTimeout(resolve, 100);
})
return true;
}
}
```
### getCallingUid
getCallingUid(): number
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册