js-apis-rpc.md 269.8 KB
Newer Older
1
# @ohos.rpc (RPC通信)
Z
zengyawen 已提交
2

3
本模块提供进程间通信能力,包括设备内的进程间通信(IPC)和设备间的进程间通信(RPC),前者基于Binder驱动,后者基于软总线驱动。
Z
zengyawen 已提交
4

5 6
> **说明:**
>
7
> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
>
9
> - 本模块从API version 9开始支持异常返回功能。
W
wanghaoxu 已提交
10 11 12 13 14 15 16

## 导入模块

```
import rpc from '@ohos.rpc';
```

C
crazy_hu 已提交
17 18 19 20 21 22 23 24
## ErrorCode<sup>9+</sup>

从API version 9起,IPC支持异常返回功能。错误码对应数值及含义如下。

**系统能力**:SystemCapability.Communication.IPC.Core

  | 名称                                  | 值      | 说明                                          |
  | ------------------------------------- | ------- | --------------------------------------------- |
C
crazy_hu 已提交
25
  | CHECK_PARAM_ERROR                     | 401     | 检查参数失败。                                |
C
crazy_hu 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  | OS_MMAP_ERROR                         | 1900001 | 执行系统调用mmap失败。                        |
  | OS_IOCTL_ERROR                        | 1900002 | 在共享内存文件描述符上执行系统调用ioctl失败。 |
  | WRITE_TO_ASHMEM_ERROR                 | 1900003 | 向共享内存写数据失败。                        |
  | READ_FROM_ASHMEM_ERROR                | 1900004 | 从共享内存读数据失败。                        |
  | ONLY_PROXY_OBJECT_PERMITTED_ERROR     | 1900005 | 只有proxy对象允许该操作。                     |
  | ONLY_REMOTE_OBJECT_PERMITTED_ERROR    | 1900006 | 只有remote对象允许该操作。                    |
  | COMMUNICATION_ERROR                   | 1900007 | 和远端对象进行进程间通信失败。                |
  | PROXY_OR_REMOTE_OBJECT_INVALID_ERROR  | 1900008 | 非法的代理对象或者远端对象。                  |
  | WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR  | 1900009 | 向MessageSequence写数据失败。                 |
  | READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR | 1900010 | 读取MessageSequence数据失败。                 |
  | PARCEL_MEMORY_ALLOC_ERROR             | 1900011 | 序列化过程中内存分配失败。                    |
  | CALL_JS_METHOD_ERROR                  | 1900012 | 执行JS回调方法失败。                          |
  | OS_DUP_ERROR                          | 1900013 | 执行系统调用dup失败。                         |


W
wanghaoxu 已提交
41
## MessageSequence<sup>9+</sup>
Z
zengyawen 已提交
42

43
  在RPC或IPC过程中,发送方可以使用MessageSequence提供的写方法,将待发送的数据以特定格式写入该对象。接收方可以使用MessageSequence提供的读方法从该对象中读取特定格式的数据。数据格式包括:基础类型及数组、IPC对象、接口描述符和自定义序列化对象。
Z
zengyawen 已提交
44 45 46

### create

47 48
  static create(): MessageSequence

W
wanghaoxu 已提交
49
  静态方法,创建MessageSequence对象。
Z
zengyawen 已提交
50

Y
yangguangzhao 已提交
51
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
52

Y
yangguangzhao 已提交
53
**返回值:**
W
wanghaoxu 已提交
54

W
wanghaoxu 已提交
55 56 57
  | 类型            | 说明                            |
  | --------------- | ------------------------------- |
  | MessageSequence | 返回创建的MessageSequence对象。 |
Z
zengyawen 已提交
58

Y
yangguangzhao 已提交
59
**示例:**
Y
yangguangzhao 已提交
60

Y
Yippo 已提交
61
  ```ts
W
wanghaoxu 已提交
62
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
63 64 65 66 67 68 69
  console.log("RpcClient: data is " + data);
  ```

### reclaim

reclaim(): void

W
wanghaoxu 已提交
70
释放不再使用的MessageSequence对象。
Z
zengyawen 已提交
71

Y
yangguangzhao 已提交
72
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
73

Y
yangguangzhao 已提交
74
**示例:**
Y
yangguangzhao 已提交
75

Y
Yippo 已提交
76
  ```ts
W
wanghaoxu 已提交
77
  let reply = rpc.MessageSequence.create();
Z
zengyawen 已提交
78 79 80 81 82
  reply.reclaim();
  ```

### writeRemoteObject

W
wanghaoxu 已提交
83
writeRemoteObject(object: [IRemoteObject](#iremoteobject)): void
Y
yangguangzhao 已提交
84

W
wanghaoxu 已提交
85
序列化远程对象并将其写入MessageSequence对象。
W
wanghaoxu 已提交
86

Y
yangguangzhao 已提交
87
**系统能力**:SystemCapability.Communication.IPC.Core
W
wanghaoxu 已提交
88

W
wanghaoxu 已提交
89
**参数:**
W
wanghaoxu 已提交
90

W
wanghaoxu 已提交
91 92 93
  | 参数名 | 类型                            | 必填 | 说明                                      |
  | ------ | ------------------------------- | ---- | ----------------------------------------- |
  | object | [IRemoteObject](#iremoteobject) | 是   | 要序列化并写入MessageSequence的远程对象。 |
Y
yangguangzhao 已提交
94

W
wanghaoxu 已提交
95 96
**错误码:**

C
crazy_hu 已提交
97
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
Z
zengyawen 已提交
98

W
wanghaoxu 已提交
99
  | 错误码ID | 错误信息 |
W
wanghaoxu 已提交
100 101 102
  | -------- | ------- |
  | 1900008 | proxy or remote object is invalid |
  | 1900009 | write data to message sequence failed |
W
wanghaoxu 已提交
103

Y
yangguangzhao 已提交
104
**示例:**
Y
yangguangzhao 已提交
105

Y
Yippo 已提交
106
  ```ts
Z
zengyawen 已提交
107 108 109 110 111
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
W
wanghaoxu 已提交
112
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
113
  let testRemoteObject = new TestRemoteObject("testObject");
W
wanghaoxu 已提交
114 115 116 117 118 119
  try {
      data.writeRemoteObject(testRemoteObject);
  } catch(error) {
      console.info("Rpc write remote object fail, errorCode " + error.code);
      console.info("Rpc write remote object fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
120 121 122 123 124 125
  ```

### readRemoteObject

readRemoteObject(): IRemoteObject

W
wanghaoxu 已提交
126
从MessageSequence读取远程对象。此方法用于反序列化MessageSequence对象以生成IRemoteObject。远程对象按写入MessageSequence的顺序读取。
W
wanghaoxu 已提交
127

Y
yangguangzhao 已提交
128
**系统能力**:SystemCapability.Communication.IPC.Core
W
wanghaoxu 已提交
129

Y
yangguangzhao 已提交
130
**返回值:**
W
wanghaoxu 已提交
131

W
wanghaoxu 已提交
132 133
  | 类型                            | 说明               |
  | ------------------------------- | ------------------ |
Y
yangguangzhao 已提交
134
  | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 |
Z
zengyawen 已提交
135

W
wanghaoxu 已提交
136 137
**错误码:**

C
crazy_hu 已提交
138
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
139

W
wanghaoxu 已提交
140 141 142 143
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900008 | proxy or remote object is invalid |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
144

Y
yangguangzhao 已提交
145
**示例:**
Y
yangguangzhao 已提交
146

Y
Yippo 已提交
147
  ```ts
Z
zengyawen 已提交
148 149 150 151 152
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
W
wanghaoxu 已提交
153
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
154
  let testRemoteObject = new TestRemoteObject("testObject");
W
wanghaoxu 已提交
155 156 157 158 159 160 161
  try {
      data.writeRemoteObject(testRemoteObject);
      let proxy = data.readRemoteObject();
  } catch(error) {
      console.info("Rpc write remote object fail, errorCode " + error.code);
      console.info("Rpc write remote object fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
162 163 164 165
  ```

### writeInterfaceToken

W
wanghaoxu 已提交
166
writeInterfaceToken(token: string): void
Z
zengyawen 已提交
167

W
wanghaoxu 已提交
168
将接口描述符写入MessageSequence对象,远端对象可使用该信息校验本次通信。
W
wanghaoxu 已提交
169

Y
yangguangzhao 已提交
170
**系统能力**:SystemCapability.Communication.IPC.Core
W
wanghaoxu 已提交
171

W
wanghaoxu 已提交
172
**参数:**
W
wanghaoxu 已提交
173

W
wanghaoxu 已提交
174 175 176
  | 参数名 | 类型   | 必填 | 说明               |
  | ------ | ------ | ---- | ------------------ |
  | token  | string | 是   | 字符串类型描述符。 |
Z
zengyawen 已提交
177

W
wanghaoxu 已提交
178 179
**错误码:**

C
crazy_hu 已提交
180
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
Z
zengyawen 已提交
181

W
wanghaoxu 已提交
182 183 184
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
W
wanghaoxu 已提交
185

Y
yangguangzhao 已提交
186
**示例:**
Y
yangguangzhao 已提交
187

Y
Yippo 已提交
188
  ```ts
W
wanghaoxu 已提交
189 190 191 192 193 194 195
  let data = rpc.MessageSequence.create();
  try {
      data.writeInterfaceToken("aaa");
  } catch(error) {
      console.info("rpc write interface fail, errorCode " + error.code);
      console.info("rpc write interface fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
196 197 198 199 200 201
  ```

### readInterfaceToken

readInterfaceToken(): string

202
从MessageSequence对象中读取接口描述符,接口描述符按写入MessageSequence的顺序读取,本地对象可使用该信息检验本次通信。
W
wanghaoxu 已提交
203

Y
yangguangzhao 已提交
204
**系统能力**:SystemCapability.Communication.IPC.Core
W
wanghaoxu 已提交
205

Y
yangguangzhao 已提交
206
**返回值:**
W
wanghaoxu 已提交
207

W
wanghaoxu 已提交
208 209
  | 类型   | 说明                     |
  | ------ | ------------------------ |
Y
yangguangzhao 已提交
210
  | string | 返回读取到的接口描述符。 |
Z
zengyawen 已提交
211

W
wanghaoxu 已提交
212 213
**错误码:**

C
crazy_hu 已提交
214
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
215

W
wanghaoxu 已提交
216 217 218
  | 错误码ID | 错误信息 |
  | ------- | ----- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
219

Y
yangguangzhao 已提交
220
**示例:**
Y
yangguangzhao 已提交
221

Y
Yippo 已提交
222
```ts
223 224 225 226 227 228 229 230 231 232 233 234
class Stub extends rpc.RemoteObject {
    onRemoteRequest(code, data, reply, option) {
        try {
            let interfaceToken = data.readInterfaceToken();
            console.log("RpcServer: interfaceToken is " + interfaceToken);
        } catch(error) {
            console.info("RpcServer: read interfaceToken failed, errorCode " + error.code);
            console.info("RpcServer: read interfaceToken failed, errorMessage " + error.message);
        }
        return true;
    }
}
235
```
Z
zengyawen 已提交
236 237 238 239 240

### getSize

getSize(): number

241
获取当前创建的MessageSequence对象的数据大小。
W
wanghaoxu 已提交
242

Y
yangguangzhao 已提交
243
**系统能力**:SystemCapability.Communication.IPC.Core
W
wanghaoxu 已提交
244

Y
yangguangzhao 已提交
245
**返回值:**
W
wanghaoxu 已提交
246

W
wanghaoxu 已提交
247 248
  | 类型   | 说明                                            |
  | ------ | ----------------------------------------------- |
249
  | number | 获取的MessageSequence实例的数据大小。以字节为单位。 |
W
wanghaoxu 已提交
250

Y
yangguangzhao 已提交
251
**示例:**
Y
yangguangzhao 已提交
252

Y
Yippo 已提交
253
  ```ts
W
wanghaoxu 已提交
254
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
255 256 257 258 259 260 261 262
  let size = data.getSize();
  console.log("RpcClient: size is " + size);
  ```

### getCapacity

getCapacity(): number

263
获取当前MessageSequence对象的容量大小。
W
wanghaoxu 已提交
264

Y
yangguangzhao 已提交
265
**系统能力**:SystemCapability.Communication.IPC.Core
W
wanghaoxu 已提交
266

Y
yangguangzhao 已提交
267
**返回值:**
W
wanghaoxu 已提交
268

W
wanghaoxu 已提交
269 270
  | 类型   | 说明 |
  | ------ | ----- |
271
  | number | 获取的MessageSequence实例的容量大小。以字节为单位。 |
W
wanghaoxu 已提交
272

Y
yangguangzhao 已提交
273
**示例:**
Y
yangguangzhao 已提交
274

Y
Yippo 已提交
275
  ```ts
W
wanghaoxu 已提交
276
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
277 278 279 280 281 282
  let result = data.getCapacity();
  console.log("RpcClient: capacity is " + result);
  ```

### setSize

W
wanghaoxu 已提交
283
setSize(size: number): void
Z
zengyawen 已提交
284

285
设置MessageSequence对象中包含的数据大小。
W
wanghaoxu 已提交
286

Y
yangguangzhao 已提交
287
**系统能力**:SystemCapability.Communication.IPC.Core
W
wanghaoxu 已提交
288

W
wanghaoxu 已提交
289
**参数:**
W
wanghaoxu 已提交
290

W
wanghaoxu 已提交
291 292
  | 参数名 | 类型   | 必填 | 说明 |
  | ------ | ------ | ---- | ------ |
W
wanghaoxu 已提交
293
  | size   | number | 是   | MessageSequence实例的数据大小。以字节为单位。 |
Z
zengyawen 已提交
294

Y
yangguangzhao 已提交
295
**示例:**
Y
yangguangzhao 已提交
296

Y
Yippo 已提交
297
  ```ts
W
wanghaoxu 已提交
298 299 300
  let data = rpc.MessageSequence.create();
  try {
      data.setSize(16);
C
crazy_hu 已提交
301
      console.log("RpcClient: setSize is " + data.getSize());
W
wanghaoxu 已提交
302 303 304 305
  } catch(error) {
      console.info("rpc set size of MessageSequence fail, errorCode " + error.code);
      console.info("rpc set size of MessageSequence fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
306 307 308 309
  ```

### setCapacity

W
wanghaoxu 已提交
310
setCapacity(size: number): void
Z
zengyawen 已提交
311

312
设置MessageSequence对象的存储容量。
Z
zengyawen 已提交
313

Y
yangguangzhao 已提交
314
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
315

W
wanghaoxu 已提交
316
**参数:**
W
wangqinxiao 已提交
317

W
wanghaoxu 已提交
318 319 320
  | 参数名 | 类型   | 必填 | 说明                                          |
  | ------ | ------ | ---- | --------------------------------------------- |
  | size   | number | 是   | MessageSequence实例的存储容量。以字节为单位。 |
Z
zengyawen 已提交
321

W
wanghaoxu 已提交
322 323
**错误码:**

C
crazy_hu 已提交
324
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
325

W
wanghaoxu 已提交
326 327 328
  | 错误码ID | 错误信息 |
  | -------- | ------ |
  | 1900011 | parcel memory alloc failed |
Z
zengyawen 已提交
329

Y
yangguangzhao 已提交
330
**示例:**
Y
yangguangzhao 已提交
331

Y
Yippo 已提交
332
  ```ts
W
wanghaoxu 已提交
333 334 335
  let data = rpc.MessageSequence.create();
  try {
      data.setCapacity(100);
C
crazy_hu 已提交
336
      console.log("RpcClient: setCapacity is " + data.getCapacity());
W
wanghaoxu 已提交
337 338 339 340
  } catch(error) {
      console.info("rpc memory alloc fail, errorCode " + error.code);
      console.info("rpc memory alloc fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
341 342 343 344 345 346
  ```

### getWritableBytes

getWritableBytes(): number

347
获取MessageSequence的可写字节空间大小。
Z
zengyawen 已提交
348

Y
yangguangzhao 已提交
349
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
350

Y
yangguangzhao 已提交
351
**返回值:**
W
wangqinxiao 已提交
352

W
wanghaoxu 已提交
353 354
  | 类型 | 说明 |
  | ------ | ------ |
355
  | number | 获取到的MessageSequence实例的可写字节空间。以字节为单位。 |
Z
zengyawen 已提交
356

Y
yangguangzhao 已提交
357
**示例:**
Y
yangguangzhao 已提交
358

Y
Yippo 已提交
359
```ts
360 361 362 363 364 365 366
class Stub extends rpc.RemoteObject {
    onRemoteRequest(code, data, reply, option) {
        let getWritableBytes = data.getWritableBytes();
        console.log("RpcServer: getWritableBytes is " + getWritableBytes);
        return true;
    }
}
367
```
Z
zengyawen 已提交
368 369 370 371 372

### getReadableBytes

getReadableBytes(): number

W
wanghaoxu 已提交
373
获取MessageSequence的可读字节空间。
Z
zengyawen 已提交
374

Y
yangguangzhao 已提交
375
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
376

Y
yangguangzhao 已提交
377
**返回值:**
W
wangqinxiao 已提交
378

W
wanghaoxu 已提交
379 380
  | 类型 | 说明 |
  | ------ | ------- |
381
  | number | 获取到的MessageSequence实例的可读字节空间。以字节为单位。 |
Z
zengyawen 已提交
382

Y
yangguangzhao 已提交
383
**示例:**
Y
yangguangzhao 已提交
384

Y
Yippo 已提交
385
  ```ts
386 387 388 389 390 391 392
class Stub extends rpc.RemoteObject {
    onRemoteRequest(code, data, reply, option) {
        let result = data.getReadableBytes();
        console.log("RpcServer: getReadableBytes is " + result);
        return true;
    }
}
Z
zengyawen 已提交
393 394 395 396 397 398
  ```

### getReadPosition

getReadPosition(): number

W
wanghaoxu 已提交
399
获取MessageSequence的读位置。
Z
zengyawen 已提交
400

Y
yangguangzhao 已提交
401
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
402

Y
yangguangzhao 已提交
403
**返回值:**
W
wanghaoxu 已提交
404

W
wanghaoxu 已提交
405 406
  | 类型 | 说明 |
  | ------ | ------ |
W
wanghaoxu 已提交
407
  | number | 返回MessageSequence实例中的当前读取位置。 |
Z
zengyawen 已提交
408

Y
yangguangzhao 已提交
409
**示例:**
Y
yangguangzhao 已提交
410

Y
Yippo 已提交
411
  ```ts
W
wanghaoxu 已提交
412
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
413 414 415 416 417 418 419 420
  let readPos = data.getReadPosition();
  console.log("RpcClient: readPos is " + readPos);
  ```

### getWritePosition

getWritePosition(): number

W
wanghaoxu 已提交
421
获取MessageSequence的写位置。
Z
zengyawen 已提交
422

Y
yangguangzhao 已提交
423
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
424

Y
yangguangzhao 已提交
425
**返回值:**
W
wangqinxiao 已提交
426

W
wanghaoxu 已提交
427 428
  | 类型 | 说明 |
  | ------ | ----- |
W
wanghaoxu 已提交
429
  | number | 返回MessageSequence实例中的当前写入位置。 |
Z
zengyawen 已提交
430

Y
yangguangzhao 已提交
431
**示例:**
Y
yangguangzhao 已提交
432

Y
Yippo 已提交
433
  ```ts
W
wanghaoxu 已提交
434
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
435 436 437 438 439 440 441
  data.writeInt(10);
  let bwPos = data.getWritePosition();
  console.log("RpcClient: bwPos is " + bwPos);
  ```

### rewindRead

W
wanghaoxu 已提交
442
rewindRead(pos: number): void
Z
zengyawen 已提交
443 444 445

重新偏移读取位置到指定的位置。

Y
yangguangzhao 已提交
446
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
447

W
wanghaoxu 已提交
448
**参数:**
W
wangqinxiao 已提交
449

W
wanghaoxu 已提交
450 451
  | 参数名 | 类型 | 必填 | 说明 |
  | ------ | ------ | ---- | ------- |
W
wanghaoxu 已提交
452
  | pos    | number | 是   | 开始读取数据的目标位置。 |
Z
zengyawen 已提交
453

Y
yangguangzhao 已提交
454
**示例:**
Y
yangguangzhao 已提交
455

Y
Yippo 已提交
456
  ```ts
W
wanghaoxu 已提交
457
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
458
  data.writeInt(12);
W
wanghaoxu 已提交
459
  data.writeString("sequence");
Z
zengyawen 已提交
460 461
  let number = data.readInt();
  console.log("RpcClient: number is " + number);
W
wanghaoxu 已提交
462 463 464 465 466 467
  try {
      data.rewindRead(0);
  } catch(error) {
      console.info("rpc rewind read data fail, errorCode " + error.code);
      console.info("rpc rewind read data fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
468 469 470 471 472 473
  let number2 = data.readInt();
  console.log("RpcClient: rewindRead is " + number2);
  ```

### rewindWrite

W
wanghaoxu 已提交
474
rewindWrite(pos: number): void
Z
zengyawen 已提交
475 476 477

重新偏移写位置到指定的位置。

Y
yangguangzhao 已提交
478
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
479

W
wanghaoxu 已提交
480
**参数:**
W
wangqinxiao 已提交
481

W
wanghaoxu 已提交
482 483
  | 参数名 | 类型 | 必填 | 说明 |
  | ------ | ------ | ---- | ----- |
W
wanghaoxu 已提交
484
  | pos    | number | 是   | 开始写入数据的目标位置。 |
Z
zengyawen 已提交
485

Y
yangguangzhao 已提交
486
**示例:**
Y
yangguangzhao 已提交
487

Y
Yippo 已提交
488
  ```ts
W
wanghaoxu 已提交
489
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
490
  data.writeInt(4);
W
wanghaoxu 已提交
491 492 493 494 495 496
  try {
      data.rewindWrite(0);
  } catch(error) {
      console.info("rpc rewind read data fail, errorCode " + error.code);
      console.info("rpc rewind read data fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
497 498 499 500 501 502 503
  data.writeInt(5);
  let number = data.readInt();
  console.log("RpcClient: rewindWrite is: " + number);
  ```

### writeByte

W
wanghaoxu 已提交
504
writeByte(val: number): void
Z
zengyawen 已提交
505

W
wanghaoxu 已提交
506
将字节值写入MessageSequence实例。
Z
zengyawen 已提交
507

Y
yangguangzhao 已提交
508
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
509

W
wanghaoxu 已提交
510
**参数:**
W
wangqinxiao 已提交
511

W
wanghaoxu 已提交
512 513 514
  | 参数名 | 类型   | 必填 | 说明 |
  | ----- | ------ | ---- | ----- |
  | val | number | 是 | 要写入的字节值。 |
Z
zengyawen 已提交
515

W
wanghaoxu 已提交
516 517
**错误码:**

C
crazy_hu 已提交
518
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
519

W
wanghaoxu 已提交
520 521 522
  | 错误码ID | 错误信息 |
  | -------- | ------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
523

Y
yangguangzhao 已提交
524
**示例:**
Y
yangguangzhao 已提交
525

Y
Yippo 已提交
526
  ```ts
W
wanghaoxu 已提交
527 528 529 530 531 532 533
  let data = rpc.MessageSequence.create();
  try {
      data.writeByte(2);
  } catch(error) {
    console.info("rpc write byte fail, errorCode " + error.code);
    console.info("rpc write byte fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
534 535 536 537 538 539
  ```

### readByte

readByte(): number

W
wanghaoxu 已提交
540
从MessageSequence实例读取字节值。
Z
zengyawen 已提交
541

Y
yangguangzhao 已提交
542
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
543

Y
yangguangzhao 已提交
544
**返回值:**
W
wangqinxiao 已提交
545

W
wanghaoxu 已提交
546 547
  | 类型   | 说明 |
  | ------ | ----- |
Y
yangguangzhao 已提交
548
  | number | 返回字节值。 |
Z
zengyawen 已提交
549

W
wanghaoxu 已提交
550 551
**错误码:**

C
crazy_hu 已提交
552
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
553

W
wanghaoxu 已提交
554 555 556
  | 错误码ID | 错误信息 |
  | ------- | --------  |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
557

Y
yangguangzhao 已提交
558
**示例:**
Y
yangguangzhao 已提交
559

Y
Yippo 已提交
560
  ```ts
W
wanghaoxu 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574
  let data = rpc.MessageSequence.create();
  try {
      data.writeByte(2);
  } catch(error) {
    console.info("rpc write byte fail, errorCode " + error.code);
    console.info("rpc write byte fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readByte();
      console.log("RpcClient: readByte is: " + ret);
  } catch(error) {
    console.info("rpc write byte fail, errorCode " + error.code);
    console.info("rpc write byte fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
575 576 577 578
  ```

### writeShort

W
wanghaoxu 已提交
579
writeShort(val: number): void
Z
zengyawen 已提交
580

W
wanghaoxu 已提交
581
将短整数值写入MessageSequence实例。
Z
zengyawen 已提交
582

Y
yangguangzhao 已提交
583
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
584

W
wanghaoxu 已提交
585
**参数:**
W
wangqinxiao 已提交
586

W
wanghaoxu 已提交
587 588 589
  | 参数名 | 类型   | 必填 | 说明 |
  | ------ | ------ | --- | --- |
  | val | number | 是 | 要写入的短整数值。 |
Z
zengyawen 已提交
590

W
wanghaoxu 已提交
591 592
**错误码:**

C
crazy_hu 已提交
593
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
594

W
wanghaoxu 已提交
595 596 597
  | 错误码ID | 错误信息 |
  | ------- | ------ |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
598

Y
yangguangzhao 已提交
599
**示例:**
Y
yangguangzhao 已提交
600

Y
Yippo 已提交
601
  ```ts
W
wanghaoxu 已提交
602 603 604 605 606 607 608
  let data = rpc.MessageSequence.create();
  try {
      data.writeShort(8);
  } catch(error) {
      console.info("rpc write short fail, errorCode " + error.code);
      console.info("rpc write short fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
609 610 611 612 613 614
  ```

### readShort

readShort(): number

W
wanghaoxu 已提交
615
从MessageSequence实例读取短整数值。
Z
zengyawen 已提交
616

Y
yangguangzhao 已提交
617
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
618

Y
yangguangzhao 已提交
619
**返回值:**
W
wangqinxiao 已提交
620

W
wanghaoxu 已提交
621 622
  | 类型   | 说明           |
  | ------ | -------------- |
Y
yangguangzhao 已提交
623
  | number | 返回短整数值。 |
Z
zengyawen 已提交
624

W
wanghaoxu 已提交
625 626
**错误码:**

C
crazy_hu 已提交
627
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
628

W
wanghaoxu 已提交
629 630 631
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
632

Y
yangguangzhao 已提交
633
**示例:**
Y
yangguangzhao 已提交
634

Y
Yippo 已提交
635
  ```ts
W
wanghaoxu 已提交
636 637 638 639 640 641 642 643
  let data = rpc.MessageSequence.create();
  try {
      data.writeShort(8);
  } catch(error) {
      console.info("rpc write short fail, errorCode " + error.code);
      console.info("rpc write short fail, errorMessage" + error.message);
  }
  try {
644
      let ret = data.readShort();
W
wanghaoxu 已提交
645 646 647 648 649
  } catch(error) {
      console.info("rpc read short fail, errorCode " + error.code);
      console.info("rpc read short fail, errorMessage" + error.message);
  }
  console.log("RpcClient: readByte is: " + ret);
Z
zengyawen 已提交
650 651 652 653
  ```

### writeInt

W
wanghaoxu 已提交
654
writeInt(val: number): void
Z
zengyawen 已提交
655

W
wanghaoxu 已提交
656
将整数值写入MessageSequence实例。
Z
zengyawen 已提交
657

Y
yangguangzhao 已提交
658
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
659

W
wanghaoxu 已提交
660
**参数:**
W
wangqinxiao 已提交
661

W
wanghaoxu 已提交
662 663 664
  | 参数名 | 类型   | 必填 | 说明             |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | 是   | 要写入的整数值。 |
Z
zengyawen 已提交
665

W
wanghaoxu 已提交
666 667
**错误码:**

C
crazy_hu 已提交
668
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
669

W
wanghaoxu 已提交
670 671 672
  | 错误码ID | 错误信息 |
  | -------- | ------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
673

Y
yangguangzhao 已提交
674
**示例:**
Y
yangguangzhao 已提交
675

Y
Yippo 已提交
676
  ```ts
W
wanghaoxu 已提交
677 678 679 680 681 682 683
  let data = rpc.MessageSequence.create();
  try {
      data.writeInt(10);
  } catch(error) {
      console.info("rpc write int fail, errorCode " + error.code);
      console.info("rpc write int fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
684 685 686 687 688 689
  ```

### readInt

readInt(): number

W
wanghaoxu 已提交
690
从MessageSequence实例读取整数值。
Z
zengyawen 已提交
691

Y
yangguangzhao 已提交
692
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
693

Y
yangguangzhao 已提交
694
**返回值:**
W
wangqinxiao 已提交
695

W
wanghaoxu 已提交
696 697
  | 类型   | 说明         |
  | ------ | ------------ |
Y
yangguangzhao 已提交
698
  | number | 返回整数值。 |
Z
zengyawen 已提交
699

W
wanghaoxu 已提交
700 701
**错误码:**

C
crazy_hu 已提交
702
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
703

W
wanghaoxu 已提交
704 705 706
  | 错误码ID | 错误信息 |
  | ------- | ------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
707

Y
yangguangzhao 已提交
708
**示例:**
Y
yangguangzhao 已提交
709

Y
Yippo 已提交
710
  ```ts
W
wanghaoxu 已提交
711 712 713 714 715 716 717 718 719
  let data = rpc.MessageSequence.create();
  try {
      data.writeInt(10);
  } catch(error) {
      console.info("rpc write int fail, errorCode " + error.code);
      console.info("rpc write int fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readInt();
C
crazy_hu 已提交
720
      console.log("RpcClient: readInt is " + ret);
W
wanghaoxu 已提交
721 722 723 724
  } catch(error) {
      console.info("rpc read int fail, errorCode " + error.code);
      console.info("rpc read int fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
725 726 727 728
  ```

### writeLong

W
wanghaoxu 已提交
729
writeLong(val: number): void
Z
zengyawen 已提交
730

W
wanghaoxu 已提交
731
将长整数值写入MessageSequence实例。
Z
zengyawen 已提交
732

Y
yangguangzhao 已提交
733
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
734

W
wanghaoxu 已提交
735
**参数:**
W
wangqinxiao 已提交
736

W
wanghaoxu 已提交
737 738 739
  | 参数名 | 类型   | 必填 | 说明             |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | 是   | 要写入的长整数值 |
Z
zengyawen 已提交
740

W
wanghaoxu 已提交
741 742
**错误码:**

C
crazy_hu 已提交
743
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
744

W
wanghaoxu 已提交
745 746 747
  | 错误码ID | 错误信息 |
  | ------- | ------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
748

Y
yangguangzhao 已提交
749
**示例:**
Y
yangguangzhao 已提交
750

Y
Yippo 已提交
751
  ```ts
W
wanghaoxu 已提交
752 753 754 755 756 757 758
  let data = rpc.MessageSequence.create();
  try {
      data.writeLong(10000);
  } catch(error) {
      console.info("rpc write long fail, errorCode " + error.code);
      console.info("rpc write long fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
759 760 761 762 763 764
  ```

### readLong

readLong(): number

W
wanghaoxu 已提交
765
从MessageSequence实例中读取长整数值。
Z
zengyawen 已提交
766

Y
yangguangzhao 已提交
767
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
768

Y
yangguangzhao 已提交
769
**返回值:**
W
wangqinxiao 已提交
770

W
wanghaoxu 已提交
771 772
  | 类型   | 说明           |
  | ------ | -------------- |
Y
yangguangzhao 已提交
773
  | number | 返回长整数值。 |
Z
zengyawen 已提交
774

W
wanghaoxu 已提交
775 776
**错误码:**

C
crazy_hu 已提交
777
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
778

W
wanghaoxu 已提交
779 780 781
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
782

Y
yangguangzhao 已提交
783
**示例:**
Y
yangguangzhao 已提交
784

Y
Yippo 已提交
785
  ```ts
W
wanghaoxu 已提交
786 787 788 789 790 791 792 793 794
  let data = rpc.MessageSequence.create();
  try {
      data.writeLong(10000);
  } catch(error) {
      console.info("rpc write long fail, errorCode " + error.code);
      console.info("rpc write long fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readLong();
C
crazy_hu 已提交
795
      console.log("RpcClient: readLong is " + ret);
W
wanghaoxu 已提交
796 797 798 799
  } catch(error) {
      console.info("rpc read long fail, errorCode " + error.code);
      console.info("rpc read long fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
800 801 802 803
  ```

### writeFloat

W
wanghaoxu 已提交
804
writeFloat(val: number): void
Z
zengyawen 已提交
805

W
wanghaoxu 已提交
806
将浮点值写入MessageSequence实例。
Z
zengyawen 已提交
807

Y
yangguangzhao 已提交
808
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
809

W
wanghaoxu 已提交
810
**参数:**
W
wangqinxiao 已提交
811

W
wanghaoxu 已提交
812 813 814
  | 参数名 | 类型 | 必填 | 说明 |
  | ----- | ---- | ---- | ----- |
  | val | number | 是 | 要写入的浮点值。 |
Z
zengyawen 已提交
815

W
wanghaoxu 已提交
816 817
**错误码:**

C
crazy_hu 已提交
818
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
819

W
wanghaoxu 已提交
820 821
  | 错误码ID | 错误信息 |
  | ------- | ------- |
C
crazy_hu 已提交
822
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
823

Y
yangguangzhao 已提交
824
**示例:**
Y
yangguangzhao 已提交
825

Y
Yippo 已提交
826
  ```ts
W
wanghaoxu 已提交
827 828 829 830 831 832 833
  let data = rpc.MessageSequence.create();
  try {
      data.writeFloat(1.2);
  } catch(error) {
      console.info("rpc write float fail, errorCode " + error.code);
      console.info("rpc write float fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
834 835 836 837 838 839
  ```

### readFloat

readFloat(): number

W
wanghaoxu 已提交
840
从MessageSequence实例中读取浮点值。
Z
zengyawen 已提交
841

Y
yangguangzhao 已提交
842
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
843

Y
yangguangzhao 已提交
844
**返回值:**
W
wangqinxiao 已提交
845

W
wanghaoxu 已提交
846 847
  | 类型   | 说明         |
  | ------ | ------------ |
Y
yangguangzhao 已提交
848
  | number | 返回浮点值。 |
Z
zengyawen 已提交
849

W
wanghaoxu 已提交
850 851
**错误码:**

C
crazy_hu 已提交
852
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
853

W
wanghaoxu 已提交
854 855 856
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
857

Y
yangguangzhao 已提交
858
**示例:**
Y
yangguangzhao 已提交
859

Y
Yippo 已提交
860
  ```ts
W
wanghaoxu 已提交
861 862 863 864 865 866 867 868 869
  let data = rpc.MessageSequence.create();
  try {
      data.writeFloat(1.2);
  } catch(error) {
      console.info("rpc write float fail, errorCode " + error.code);
      console.info("rpc write float fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readFloat();
C
crazy_hu 已提交
870
      console.log("RpcClient: readFloat is " + ret);
W
wanghaoxu 已提交
871 872 873 874
  } catch(error) {
      console.info("rpc read float fail, errorCode " + error.code);
      console.info("rpc read float fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
875 876 877 878
  ```

### writeDouble

W
wanghaoxu 已提交
879
writeDouble(val: number): void
Z
zengyawen 已提交
880

W
wanghaoxu 已提交
881
将双精度浮点值写入MessageSequence实例。
Z
zengyawen 已提交
882

Y
yangguangzhao 已提交
883
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
884

W
wanghaoxu 已提交
885
**参数:**
W
wangqinxiao 已提交
886

887 888 889
  | 参数名 | 类型   | 必填 | 说明                   |
  | ------ | ------ | ---- | ---------------------- |
  | val    | number | 是   | 要写入的双精度浮点值。 |
Z
zengyawen 已提交
890

W
wanghaoxu 已提交
891 892
**错误码:**

C
crazy_hu 已提交
893
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
894

W
wanghaoxu 已提交
895 896 897
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
898

Y
yangguangzhao 已提交
899
**示例:**
Y
yangguangzhao 已提交
900

Y
Yippo 已提交
901
  ```ts
W
wanghaoxu 已提交
902 903 904 905 906 907 908
  let data = rpc.MessageSequence.create();
  try {
      data.writeDouble(10.2);
  } catch(error) {
      console.info("rpc read float fail, errorCode " + error.code);
      console.info("rpc read float fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
909 910 911 912 913 914
  ```

### readDouble

readDouble(): number

W
wanghaoxu 已提交
915
从MessageSequence实例读取双精度浮点值。
Z
zengyawen 已提交
916

Y
yangguangzhao 已提交
917
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
918

Y
yangguangzhao 已提交
919
**返回值:**
W
wangqinxiao 已提交
920

W
wanghaoxu 已提交
921 922
  | 类型   | 说明               |
  | ------ | ------------------ |
Y
yangguangzhao 已提交
923
  | number | 返回双精度浮点值。 |
Z
zengyawen 已提交
924

W
wanghaoxu 已提交
925 926
**错误码:**

C
crazy_hu 已提交
927
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
928

W
wanghaoxu 已提交
929 930 931
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
932

Y
yangguangzhao 已提交
933
**示例:**
Y
yangguangzhao 已提交
934

Y
Yippo 已提交
935
  ```ts
W
wanghaoxu 已提交
936 937 938 939 940 941 942 943 944
  let data = rpc.MessageSequence.create();
  try {
      data.writeDouble(10.2);
  } catch(error) {
      console.info("rpc write double fail, errorCode " + error.code);
      console.info("rpc write double fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readDouble();
C
crazy_hu 已提交
945
      console.log("RpcClient: readDouble is " + ret);
W
wanghaoxu 已提交
946 947 948 949
  } catch(error) {
      console.info("rpc read double fail, errorCode " + error.code);
      console.info("rpc read double fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
950 951 952 953
  ```

### writeBoolean

W
wanghaoxu 已提交
954
writeBoolean(val: boolean): void
Z
zengyawen 已提交
955

W
wanghaoxu 已提交
956
将布尔值写入MessageSequence实例。
Z
zengyawen 已提交
957

Y
yangguangzhao 已提交
958
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
959

W
wanghaoxu 已提交
960
**参数:**
W
wangqinxiao 已提交
961

W
wanghaoxu 已提交
962 963 964
  | 参数名 | 类型    | 必填 | 说明             |
  | ------ | ------- | ---- | ---------------- |
  | val    | boolean | 是   | 要写入的布尔值。 |
Z
zengyawen 已提交
965

W
wanghaoxu 已提交
966 967
**错误码:**

C
crazy_hu 已提交
968
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
969

W
wanghaoxu 已提交
970 971 972
  | 错误码ID | 错误信息 |
  | ------- | ------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
973

Y
yangguangzhao 已提交
974
**示例:**
Y
yangguangzhao 已提交
975

Y
Yippo 已提交
976
  ```ts
W
wanghaoxu 已提交
977 978 979 980 981 982 983
  let data = rpc.MessageSequence.create();
  try {
      data.writeBoolean(false);
  } catch(error) {
      console.info("rpc write boolean fail, errorCode " + error.code);
      console.info("rpc write boolean fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
984 985 986 987 988 989
  ```

### readBoolean

readBoolean(): boolean

W
wanghaoxu 已提交
990
从MessageSequence实例读取布尔值。
Z
zengyawen 已提交
991

Y
yangguangzhao 已提交
992
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
993

Y
yangguangzhao 已提交
994
**返回值:**
W
wangqinxiao 已提交
995

W
wanghaoxu 已提交
996 997
  | 类型    | 说明                 |
  | ------- | -------------------- |
Y
yangguangzhao 已提交
998
  | boolean | 返回读取到的布尔值。 |
Z
zengyawen 已提交
999

W
wanghaoxu 已提交
1000 1001
**错误码:**

C
crazy_hu 已提交
1002
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1003

W
wanghaoxu 已提交
1004 1005 1006
  | 错误码ID | 错误信息 |
  | -------- | ------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
1007

Y
yangguangzhao 已提交
1008
**示例:**
Y
yangguangzhao 已提交
1009

Y
Yippo 已提交
1010
  ```ts
W
wanghaoxu 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019
  let data = rpc.MessageSequence.create();
  try {
      data.writeBoolean(false);
  } catch(error) {
      console.info("rpc write boolean fail, errorCode " + error.code);
      console.info("rpc write boolean fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readBoolean();
C
crazy_hu 已提交
1020
      console.log("RpcClient: readBoolean is " + ret);
W
wanghaoxu 已提交
1021 1022 1023 1024
  } catch(error) {
      console.info("rpc read boolean fail, errorCode " + error.code);
      console.info("rpc read boolean fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1025 1026 1027 1028
  ```

### writeChar

W
wanghaoxu 已提交
1029
writeChar(val: number): void
Z
zengyawen 已提交
1030

W
wanghaoxu 已提交
1031
将单个字符值写入MessageSequence实例。
Z
zengyawen 已提交
1032

Y
yangguangzhao 已提交
1033
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1034

W
wanghaoxu 已提交
1035
**参数:**
W
wangqinxiao 已提交
1036

W
wanghaoxu 已提交
1037 1038 1039
  | 参数名 | 类型   | 必填 | 说明                 |
  | ------ | ------ | ---- | -------------------- |
  | val    | number | 是   | 要写入的单个字符值。 |
Z
zengyawen 已提交
1040

W
wanghaoxu 已提交
1041 1042
**错误码:**

C
crazy_hu 已提交
1043
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
1044

W
wanghaoxu 已提交
1045 1046 1047
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
1048

Y
yangguangzhao 已提交
1049
**示例:**
Y
yangguangzhao 已提交
1050

Y
Yippo 已提交
1051
  ```ts
W
wanghaoxu 已提交
1052 1053 1054 1055 1056 1057 1058
  let data = rpc.MessageSequence.create();
  try {
      data.writeChar(97);
  } catch(error) {
      console.info("rpc write char fail, errorCode " + error.code);
      console.info("rpc write char fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1059 1060 1061 1062 1063 1064
  ```

### readChar

readChar(): number

W
wanghaoxu 已提交
1065
从MessageSequence实例中读取单个字符值。
Z
zengyawen 已提交
1066

Y
yangguangzhao 已提交
1067
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1068

Y
yangguangzhao 已提交
1069
**返回值:**
W
wangqinxiao 已提交
1070

W
wanghaoxu 已提交
1071 1072
  | 类型   | 说明 |
  | ------ | ---- |
Y
yangguangzhao 已提交
1073
  | number | 返回单个字符值。 |
Z
zengyawen 已提交
1074

W
wanghaoxu 已提交
1075 1076
**错误码:**

C
crazy_hu 已提交
1077
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1078

W
wanghaoxu 已提交
1079 1080 1081
  | 错误码ID | 错误信息 |
  | ------ | --------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
1082

Y
yangguangzhao 已提交
1083
**示例:**
Y
yangguangzhao 已提交
1084

Y
Yippo 已提交
1085
  ```ts
W
wanghaoxu 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094
  let data = rpc.MessageSequence.create();
  try {
      data.writeChar(97);
  } catch(error) {
      console.info("rpc write char fail, errorCode " + error.code);
      console.info("rpc write char fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readChar();
C
crazy_hu 已提交
1095
      console.log("RpcClient: readChar is " + ret);
W
wanghaoxu 已提交
1096 1097 1098 1099
  } catch(error) {
      console.info("rpc read char fail, errorCode " + error.code);
      console.info("rpc read char fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1100 1101 1102 1103
  ```

### writeString

W
wanghaoxu 已提交
1104
writeString(val: string): void
Z
zengyawen 已提交
1105

W
wanghaoxu 已提交
1106
将字符串值写入MessageSequence实例。
Z
zengyawen 已提交
1107

Y
yangguangzhao 已提交
1108
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1109

W
wanghaoxu 已提交
1110
**参数:**
W
wangqinxiao 已提交
1111

W
wanghaoxu 已提交
1112 1113 1114
  | 参数名 | 类型   | 必填 | 说明                                      |
  | ------ | ------ | ---- | ----------------------------------------- |
  | val    | string | 是   | 要写入的字符串值,其长度应小于40960字节。 |
Z
zengyawen 已提交
1115

W
wanghaoxu 已提交
1116 1117
**错误码:**

C
crazy_hu 已提交
1118
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
1119

W
wanghaoxu 已提交
1120 1121 1122
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
1123

Y
yangguangzhao 已提交
1124
**示例:**
Y
yangguangzhao 已提交
1125

Y
Yippo 已提交
1126
  ```ts
W
wanghaoxu 已提交
1127 1128 1129 1130 1131 1132 1133
  let data = rpc.MessageSequence.create();
  try {
      data.writeString('abc');
  } catch(error) {
      console.info("rpc write string fail, errorCode " + error.code);
      console.info("rpc write string fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1134 1135 1136 1137 1138 1139
  ```

### readString

readString(): string

W
wanghaoxu 已提交
1140
从MessageSequence实例读取字符串值。
Z
zengyawen 已提交
1141

Y
yangguangzhao 已提交
1142
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1143

Y
yangguangzhao 已提交
1144
**返回值:**
W
wangqinxiao 已提交
1145

W
wanghaoxu 已提交
1146 1147
  | 类型   | 说明           |
  | ------ | -------------- |
Y
yangguangzhao 已提交
1148
  | string | 返回字符串值。 |
Z
zengyawen 已提交
1149

W
wanghaoxu 已提交
1150 1151
**错误码:**

C
crazy_hu 已提交
1152
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1153

W
wanghaoxu 已提交
1154 1155 1156
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
1157

Y
yangguangzhao 已提交
1158
**示例:**
Y
yangguangzhao 已提交
1159

Y
Yippo 已提交
1160
  ```ts
W
wanghaoxu 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169
  let data = rpc.MessageSequence.create();
  try {
      data.writeString('abc');
  } catch(error) {
      console.info("rpc write string fail, errorCode " + error.code);
      console.info("rpc write string fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readString();
C
crazy_hu 已提交
1170
      console.log("RpcClient: readString is " + ret);
W
wanghaoxu 已提交
1171 1172 1173 1174
  } catch(error) {
      console.info("rpc read string fail, errorCode " + error.code);
      console.info("rpc read string fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1175 1176
  ```

W
wanghaoxu 已提交
1177
### writeParcelable
Z
zengyawen 已提交
1178

W
wanghaoxu 已提交
1179
writeParcelable(val: Parcelable): void
Z
zengyawen 已提交
1180

W
wanghaoxu 已提交
1181
将自定义序列化对象写入MessageSequence实例。
Z
zengyawen 已提交
1182

Y
yangguangzhao 已提交
1183
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1184

W
wanghaoxu 已提交
1185
**参数:**
W
wangqinxiao 已提交
1186

W
wanghaoxu 已提交
1187 1188
  | 参数名 | 类型 | 必填 | 说明 |
  | ------ | --------- | ---- | ------ |
W
wanghaoxu 已提交
1189
  | val    | Parcelable | 是   | 要写入的可序列对象。 |
Z
zengyawen 已提交
1190

W
wanghaoxu 已提交
1191 1192
**错误码:**

C
crazy_hu 已提交
1193
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
1194

W
wanghaoxu 已提交
1195 1196 1197
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
1198

Y
yangguangzhao 已提交
1199
**示例:**
Y
yangguangzhao 已提交
1200

Y
Yippo 已提交
1201
  ```ts
Z
zengyawen 已提交
1202
  class MySequenceable {
Y
yangguangzhao 已提交
1203 1204 1205
      num: number;
      str: string;
      constructor(num, str) {
Z
zengyawen 已提交
1206
          this.num = num;
Y
yangguangzhao 已提交
1207
          this.str = str;
Z
zengyawen 已提交
1208
      }
W
wanghaoxu 已提交
1209 1210 1211
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
Z
zengyawen 已提交
1212 1213
          return true;
      }
W
wanghaoxu 已提交
1214 1215 1216
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
Z
zengyawen 已提交
1217 1218 1219
          return true;
      }
  }
C
crazy_hu 已提交
1220
  let parcelable = new MySequenceable(1, "aaa");
W
wanghaoxu 已提交
1221 1222 1223 1224 1225 1226 1227
  let data = rpc.MessageSequence.create();
  try {
      data.writeParcelable(parcelable);
  } catch(error) {
      console.info("rpc write parcelable fail, errorCode " + error.code);
      console.info("rpc write parcelable fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1228 1229
  ```

W
wanghaoxu 已提交
1230
### readParcelable
Z
zengyawen 已提交
1231

C
crazy_hu 已提交
1232
readParcelable(dataIn: Parcelable): void
Z
zengyawen 已提交
1233

W
wanghaoxu 已提交
1234
从MessageSequence实例中读取成员变量到指定的对象(dataIn)。
Z
zengyawen 已提交
1235

Y
yangguangzhao 已提交
1236
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1237

W
wanghaoxu 已提交
1238
**参数:**
W
wangqinxiao 已提交
1239

W
wanghaoxu 已提交
1240 1241
  | 参数名 | 类型                      | 必填 | 说明                                      |
  | ------ | ------------------------- | ---- | ----------------------------------------- |
W
wanghaoxu 已提交
1242
  | dataIn | Parcelable | 是   | 需要从MessageSequence读取成员变量的对象。 |
Z
zengyawen 已提交
1243

W
wanghaoxu 已提交
1244 1245
**错误码:**

C
crazy_hu 已提交
1246
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
1247

W
wanghaoxu 已提交
1248 1249 1250 1251
  | 错误码ID | 错误信息 |
  | -------- | ------- |
  | 1900010 | read data from message sequence failed |
  | 1900012 | call js callback function failed |
Z
zengyawen 已提交
1252

Y
yangguangzhao 已提交
1253
**示例:**
Y
yangguangzhao 已提交
1254

Y
Yippo 已提交
1255
  ```ts
Z
zengyawen 已提交
1256
  class MySequenceable {
Y
yangguangzhao 已提交
1257 1258 1259
      num: number;
      str: string;
      constructor(num, str) {
Z
zengyawen 已提交
1260
          this.num = num;
Y
yangguangzhao 已提交
1261
          this.str = str;
Z
zengyawen 已提交
1262
      }
W
wanghaoxu 已提交
1263 1264 1265
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
Z
zengyawen 已提交
1266 1267
          return true;
      }
W
wanghaoxu 已提交
1268 1269 1270
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
Z
zengyawen 已提交
1271 1272 1273
          return true;
      }
  }
C
crazy_hu 已提交
1274
  let parcelable = new MySequenceable(1, "aaa");
W
wanghaoxu 已提交
1275 1276
  let data = rpc.MessageSequence.create();
  data.writeParcelable(parcelable);
C
crazy_hu 已提交
1277
  let ret = new MySequenceable(0, "");
W
wanghaoxu 已提交
1278 1279
  try {
      data.readParcelable(ret);
C
crazy_hu 已提交
1280
  }catch(error) {
W
wanghaoxu 已提交
1281 1282 1283
      console.info("rpc read parcelable fail, errorCode " + error.code);
      console.info("rpc read parcelable fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1284 1285 1286 1287
  ```

### writeByteArray

W
wanghaoxu 已提交
1288
writeByteArray(byteArray: number[]): void
Z
zengyawen 已提交
1289

W
wanghaoxu 已提交
1290
将字节数组写入MessageSequence实例。
Z
zengyawen 已提交
1291

Y
yangguangzhao 已提交
1292
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1293

W
wanghaoxu 已提交
1294
**参数:**
W
wangqinxiao 已提交
1295

W
wanghaoxu 已提交
1296 1297 1298
  | 参数名    | 类型     | 必填 | 说明               |
  | --------- | -------- | ---- | ------------------ |
  | byteArray | number[] | 是   | 要写入的字节数组。 |
Z
zengyawen 已提交
1299

W
wanghaoxu 已提交
1300 1301
**错误码:**

C
crazy_hu 已提交
1302
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
1303

W
wanghaoxu 已提交
1304 1305 1306
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
1307

Y
yangguangzhao 已提交
1308
**示例:**
Y
yangguangzhao 已提交
1309

Y
Yippo 已提交
1310
  ```ts
W
wanghaoxu 已提交
1311
  let data = rpc.MessageSequence.create();
Y
yangguangzhao 已提交
1312
  let ByteArrayVar = [1, 2, 3, 4, 5];
W
wanghaoxu 已提交
1313 1314 1315 1316 1317 1318
  try {
      data.writeByteArray(ByteArrayVar);
  } catch(error) {
      console.info("rpc write byteArray fail, errorCode " + error.code);
      console.info("rpc write byteArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1319 1320 1321 1322
  ```

### readByteArray

C
crazy_hu 已提交
1323
readByteArray(dataIn: number[]): void
Z
zengyawen 已提交
1324

W
wanghaoxu 已提交
1325
从MessageSequence实例读取字节数组。
Z
zengyawen 已提交
1326

Y
yangguangzhao 已提交
1327
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1328

W
wanghaoxu 已提交
1329
**参数:**
W
wangqinxiao 已提交
1330

W
wanghaoxu 已提交
1331 1332 1333 1334
  | 参数名 | 类型     | 必填 | 说明               |
  | ------ | -------- | ---- | ------------------ |
  | dataIn | number[] | 是   | 要读取的字节数组。 |

W
wanghaoxu 已提交
1335 1336
**错误码:**

C
crazy_hu 已提交
1337
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1338

W
wanghaoxu 已提交
1339 1340 1341
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
Z
zengyawen 已提交
1342

Y
yangguangzhao 已提交
1343
**示例:**
Y
yangguangzhao 已提交
1344

Y
Yippo 已提交
1345
  ```ts
W
wanghaoxu 已提交
1346
  let data = rpc.MessageSequence.create();
Y
yangguangzhao 已提交
1347
  let ByteArrayVar = [1, 2, 3, 4, 5];
W
wanghaoxu 已提交
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
  try {
      data.writeByteArray(ByteArrayVar);
  } catch(error) {
      console.info("rpc write byteArray fail, errorCode " + error.code);
      console.info("rpc write byteArray fail, errorMessage" + error.message);
  }
  try {
      let array = new Array(5);
      data.readByteArray(array);
  } catch(error) {
      console.info("rpc write byteArray fail, errorCode " + error.code);
      console.info("rpc write byteArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1361 1362 1363 1364 1365 1366
  ```

### readByteArray

readByteArray(): number[]

W
wanghaoxu 已提交
1367
从MessageSequence实例中读取字节数组。
Z
zengyawen 已提交
1368

Y
yangguangzhao 已提交
1369
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1370

Y
yangguangzhao 已提交
1371
**返回值:**
W
wangqinxiao 已提交
1372

W
wanghaoxu 已提交
1373 1374
  | 类型     | 说明           |
  | -------- | -------------- |
Y
yangguangzhao 已提交
1375
  | number[] | 返回字节数组。 |
Z
zengyawen 已提交
1376

W
wanghaoxu 已提交
1377 1378
**错误码:**

C
crazy_hu 已提交
1379
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1380

W
wanghaoxu 已提交
1381 1382 1383
  | 错误码ID | 错误信息 |
  | -------- | ------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
1384

Y
yangguangzhao 已提交
1385
**示例:**
Y
yangguangzhao 已提交
1386

Y
Yippo 已提交
1387
  ```ts
W
wanghaoxu 已提交
1388
  let data = rpc.MessageSequence.create();
C
crazy_hu 已提交
1389
  let byteArrayVar = [1, 2, 3, 4, 5];
W
wanghaoxu 已提交
1390
  try {
C
crazy_hu 已提交
1391
      data.writeByteArray(byteArrayVar);
W
wanghaoxu 已提交
1392 1393 1394 1395 1396 1397
  } catch(error) {
      console.info("rpc write byteArray fail, errorCode " + error.code);
      console.info("rpc write byteArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readByteArray();
C
crazy_hu 已提交
1398
      console.log("RpcClient: readByteArray is " + array);
W
wanghaoxu 已提交
1399 1400 1401 1402
  } catch(error) {
      console.info("rpc read byteArray fail, errorCode " + error.code);
      console.info("rpc read byteArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1403 1404 1405 1406
  ```

### writeShortArray

W
wanghaoxu 已提交
1407
writeShortArray(shortArray: number[]): void
Z
zengyawen 已提交
1408

W
wanghaoxu 已提交
1409
将短整数数组写入MessageSequence实例。
Z
zengyawen 已提交
1410

Y
yangguangzhao 已提交
1411
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1412

W
wanghaoxu 已提交
1413
**参数:**
W
wangqinxiao 已提交
1414

W
wanghaoxu 已提交
1415 1416 1417
  | 参数名     | 类型     | 必填 | 说明                 |
  | ---------- | -------- | ---- | -------------------- |
  | shortArray | number[] | 是   | 要写入的短整数数组。 |
Z
zengyawen 已提交
1418

W
wanghaoxu 已提交
1419 1420
**错误码:**

C
crazy_hu 已提交
1421
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
1422

W
wanghaoxu 已提交
1423 1424 1425
  | 错误码ID | 错误信息 |
  | ----- | ----- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
1426

Y
yangguangzhao 已提交
1427
**示例:**
Y
yangguangzhao 已提交
1428

Y
Yippo 已提交
1429
  ```ts
W
wanghaoxu 已提交
1430 1431 1432 1433 1434 1435 1436
  let data = rpc.MessageSequence.create();
  try {
      data.writeShortArray([11, 12, 13]);
  } catch(error) {
      console.info("rpc read byteArray fail, errorCode " + error.code);
      console.info("rpc read byteArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1437 1438 1439 1440
  ```

### readShortArray

C
crazy_hu 已提交
1441
readShortArray(dataIn: number[]): void
Z
zengyawen 已提交
1442

W
wanghaoxu 已提交
1443
从MessageSequence实例中读取短整数数组。
Z
zengyawen 已提交
1444

Y
yangguangzhao 已提交
1445
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1446

W
wanghaoxu 已提交
1447
**参数:**
W
wangqinxiao 已提交
1448

W
wanghaoxu 已提交
1449 1450 1451 1452
  | 参数名 | 类型     | 必填 | 说明                 |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | number[] | 是   | 要读取的短整数数组。 |

W
wanghaoxu 已提交
1453 1454
**错误码:**

C
crazy_hu 已提交
1455
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1456

W
wanghaoxu 已提交
1457 1458 1459
  | 错误码ID | 错误信息 |
  | ------ | ------- |
  | 1900010 | read data from message sequence failed |
Z
zengyawen 已提交
1460

Y
yangguangzhao 已提交
1461
**示例:**
Y
yangguangzhao 已提交
1462

Y
Yippo 已提交
1463
  ```ts
W
wanghaoxu 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
  let data = rpc.MessageSequence.create();
  try {
      data.writeShortArray([11, 12, 13]);
  } catch(error) {
      console.info("rpc write shortArray fail, errorCode " + error.code);
      console.info("rpc write shortArray fail, errorMessage" + error.message);
  }
  try {
      let array = new Array(3);
      data.readShortArray(array);
  } catch(error) {
      console.info("rpc read shortArray fail, errorCode " + error.code);
      console.info("rpc read shortArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1478 1479 1480 1481 1482 1483
  ```

### readShortArray

readShortArray(): number[]

W
wanghaoxu 已提交
1484
从MessageSequence实例中读取短整数数组。
Z
zengyawen 已提交
1485

Y
yangguangzhao 已提交
1486
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1487

Y
yangguangzhao 已提交
1488
**返回值:**
W
wangqinxiao 已提交
1489

W
wanghaoxu 已提交
1490 1491
  | 类型     | 说明             |
  | -------- | ---------------- |
Y
yangguangzhao 已提交
1492
  | number[] | 返回短整数数组。 |
Z
zengyawen 已提交
1493

W
wanghaoxu 已提交
1494 1495
**错误码:**

C
crazy_hu 已提交
1496
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1497

W
wanghaoxu 已提交
1498 1499 1500
  | 错误码ID | 错误信息 |
  | -------- | ------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
1501

Y
yangguangzhao 已提交
1502
**示例:**
Y
yangguangzhao 已提交
1503

Y
Yippo 已提交
1504
  ```ts
W
wanghaoxu 已提交
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
  let data = rpc.MessageSequence.create();
  try {
      data.writeShortArray([11, 12, 13]);
  } catch(error) {
      console.info("rpc write shortArray fail, errorCode " + error.code);
      console.info("rpc write shortArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readShortArray();
      console.log("RpcClient: readShortArray is " + array);
  } catch(error) {
      console.info("rpc read shortArray fail, errorCode " + error.code);
      console.info("rpc read shortArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1519 1520 1521 1522
  ```

### writeIntArray

W
wanghaoxu 已提交
1523
writeIntArray(intArray: number[]): void
Z
zengyawen 已提交
1524

W
wanghaoxu 已提交
1525
将整数数组写入MessageSequence实例。
Z
zengyawen 已提交
1526

Y
yangguangzhao 已提交
1527
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1528

W
wanghaoxu 已提交
1529
**参数:**
W
wangqinxiao 已提交
1530

W
wanghaoxu 已提交
1531 1532 1533
  | 参数名   | 类型     | 必填 | 说明               |
  | -------- | -------- | ---- | ------------------ |
  | intArray | number[] | 是   | 要写入的整数数组。 |
Z
zengyawen 已提交
1534

W
wanghaoxu 已提交
1535 1536
**错误码:**

C
crazy_hu 已提交
1537
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
1538

W
wanghaoxu 已提交
1539 1540 1541
  | 错误码ID | 错误信息 |
  | ----- | --------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
1542

Y
yangguangzhao 已提交
1543
**示例:**
Y
yangguangzhao 已提交
1544

Y
Yippo 已提交
1545
  ```ts
W
wanghaoxu 已提交
1546 1547 1548 1549 1550 1551 1552
  let data = rpc.MessageSequence.create();
  try {
      data.writeIntArray([100, 111, 112]);
  } catch(error) {
      console.info("rpc write intArray fail, errorCode " + error.code);
      console.info("rpc write intArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1553 1554 1555 1556
  ```

### readIntArray

C
crazy_hu 已提交
1557
readIntArray(dataIn: number[]): void
Z
zengyawen 已提交
1558

W
wanghaoxu 已提交
1559
从MessageSequence实例中读取整数数组。
Z
zengyawen 已提交
1560

Y
yangguangzhao 已提交
1561
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1562

W
wanghaoxu 已提交
1563
**参数:**
W
wangqinxiao 已提交
1564

W
wanghaoxu 已提交
1565 1566 1567 1568
  | 参数名 | 类型     | 必填 | 说明               |
  | ------ | -------- | ---- | ------------------ |
  | dataIn | number[] | 是   | 要读取的整数数组。 |

W
wanghaoxu 已提交
1569 1570
**错误码:**

C
crazy_hu 已提交
1571
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1572

W
wanghaoxu 已提交
1573 1574 1575
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
Z
zengyawen 已提交
1576

Y
yangguangzhao 已提交
1577
**示例:**
Y
yangguangzhao 已提交
1578

Y
Yippo 已提交
1579
  ```ts
W
wanghaoxu 已提交
1580 1581 1582 1583 1584 1585 1586
  let data = rpc.MessageSequence.create();
  try {
      data.writeIntArray([100, 111, 112]);
  } catch(error) {
      console.info("rpc write intArray fail, errorCode " + error.code);
      console.info("rpc write intArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1587
  let array = new Array(3);
W
wanghaoxu 已提交
1588 1589 1590 1591 1592 1593
  try {
      data.readIntArray(array);
  } catch(error) {
      console.info("rpc read intArray fail, errorCode " + error.code);
      console.info("rpc read intArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1594 1595 1596 1597 1598 1599
  ```

### readIntArray

readIntArray(): number[]

W
wanghaoxu 已提交
1600
从MessageSequence实例中读取整数数组。
Z
zengyawen 已提交
1601

Y
yangguangzhao 已提交
1602
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1603

Y
yangguangzhao 已提交
1604
**返回值:**
W
wangqinxiao 已提交
1605

W
wanghaoxu 已提交
1606 1607
  | 类型     | 说明           |
  | -------- | -------------- |
Y
yangguangzhao 已提交
1608
  | number[] | 返回整数数组。 |
Z
zengyawen 已提交
1609

W
wanghaoxu 已提交
1610 1611
**错误码:**

C
crazy_hu 已提交
1612
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1613

W
wanghaoxu 已提交
1614 1615 1616
  | 错误码ID | 错误信息 |
  | ----- | ------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
1617

Y
yangguangzhao 已提交
1618
**示例:**
Y
yangguangzhao 已提交
1619

Y
Yippo 已提交
1620
  ```ts
W
wanghaoxu 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
  let data = rpc.MessageSequence.create();
  try {
      data.writeIntArray([100, 111, 112]);
  } catch(error) {
      console.info("rpc write intArray fail, errorCode " + error.code);
      console.info("rpc write intArray fail, errorMessage" + error.message);
  }
  try {
    let array = data.readIntArray();
    console.log("RpcClient: readIntArray is " + array);
  } catch(error) {
      console.info("rpc read intArray fail, errorCode " + error.code);
      console.info("rpc read intArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1635 1636 1637 1638
  ```

### writeLongArray

W
wanghaoxu 已提交
1639
writeLongArray(longArray: number[]): void
Z
zengyawen 已提交
1640

W
wanghaoxu 已提交
1641
将长整数数组写入MessageSequence实例。
Z
zengyawen 已提交
1642

Y
yangguangzhao 已提交
1643
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1644

W
wanghaoxu 已提交
1645
**参数:**
W
wangqinxiao 已提交
1646

W
wanghaoxu 已提交
1647 1648 1649
  | 参数名    | 类型     | 必填 | 说明                 |
  | --------- | -------- | ---- | -------------------- |
  | longArray | number[] | 是   | 要写入的长整数数组。 |
Z
zengyawen 已提交
1650

W
wanghaoxu 已提交
1651 1652
**错误码:**

C
crazy_hu 已提交
1653
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
1654

W
wanghaoxu 已提交
1655 1656 1657
  | 错误码ID | 错误信息 |
  | -------  | ----- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
1658

Y
yangguangzhao 已提交
1659
**示例:**
Y
yangguangzhao 已提交
1660

Y
Yippo 已提交
1661
  ```ts
W
wanghaoxu 已提交
1662 1663 1664
  let data = rpc.MessageSequence.create();
  try {
      data.writeLongArray([1111, 1112, 1113]);
C
crazy_hu 已提交
1665
  }catch(error){
W
wanghaoxu 已提交
1666 1667 1668
      console.info("rpc write longArray fail, errorCode " + error.code);
      console.info("rpc write longArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1669 1670 1671 1672
  ```

### readLongArray

C
crazy_hu 已提交
1673
readLongArray(dataIn: number[]): void
Z
zengyawen 已提交
1674

Y
Yippo 已提交
1675
从MessageSequence实例读取的长整数数组。
Z
zengyawen 已提交
1676

Y
yangguangzhao 已提交
1677
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1678

W
wanghaoxu 已提交
1679
**参数:**
W
wangqinxiao 已提交
1680

W
wanghaoxu 已提交
1681 1682 1683 1684
  | 参数名 | 类型     | 必填 | 说明                 |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | number[] | 是   | 要读取的长整数数组。 |

W
wanghaoxu 已提交
1685 1686
**错误码:**

C
crazy_hu 已提交
1687
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1688

W
wanghaoxu 已提交
1689 1690 1691
  | 错误码ID | 错误信息 |
  | ------- | ------ |
  | 1900010 | read data from message sequence failed |
Z
zengyawen 已提交
1692

Y
yangguangzhao 已提交
1693
**示例:**
Y
yangguangzhao 已提交
1694

Y
Yippo 已提交
1695
  ```ts
W
wanghaoxu 已提交
1696 1697 1698 1699 1700 1701 1702
  let data = rpc.MessageSequence.create();
  try {
      data.writeLongArray([1111, 1112, 1113]);
  } catch(error) {
      console.info("rpc write longArray fail, errorCode " + error.code);
      console.info("rpc write longArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1703
  let array = new Array(3);
W
wanghaoxu 已提交
1704 1705 1706 1707 1708 1709
  try {
      data.readLongArray(array);
  } catch(error) {
      console.info("rpc read longArray fail, errorCode " + error.code);
      console.info("rpc read longArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1710 1711 1712 1713 1714 1715
  ```

### readLongArray

readLongArray(): number[]

1716
从MessageSequence实例中读取所有的长整数数组。
Z
zengyawen 已提交
1717

Y
yangguangzhao 已提交
1718
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1719

Y
yangguangzhao 已提交
1720
**返回值:**
W
wangqinxiao 已提交
1721

W
wanghaoxu 已提交
1722 1723
  | 类型     | 说明             |
  | -------- | ---------------- |
Y
yangguangzhao 已提交
1724
  | number[] | 返回长整数数组。 |
Z
zengyawen 已提交
1725

W
wanghaoxu 已提交
1726 1727
**错误码:**

C
crazy_hu 已提交
1728
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1729

W
wanghaoxu 已提交
1730 1731 1732
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
1733

Y
yangguangzhao 已提交
1734
**示例:**
Y
yangguangzhao 已提交
1735

Y
Yippo 已提交
1736
  ```ts
W
wanghaoxu 已提交
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
  let data = rpc.MessageSequence.create();
  try {
      data.writeLongArray([1111, 1112, 1113]);
  } catch(error) {
      console.info("rpc write longArray fail, errorCode " + error.code);
      console.info("rpc write longArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readLongArray();
      console.log("RpcClient: readLongArray is " + array);
  } catch(error) {
      console.info("rpc read longArray fail, errorCode " + error.code);
      console.info("rpc read longArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1751 1752 1753 1754
  ```

### writeFloatArray

W
wanghaoxu 已提交
1755
writeFloatArray(floatArray: number[]): void
Z
zengyawen 已提交
1756

W
wanghaoxu 已提交
1757
将浮点数组写入MessageSequence实例。
Z
zengyawen 已提交
1758

Y
yangguangzhao 已提交
1759
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1760

W
wanghaoxu 已提交
1761
**参数:**
W
wangqinxiao 已提交
1762

W
wanghaoxu 已提交
1763 1764 1765
  | 参数名     | 类型     | 必填 | 说明                                                                                                                    |
  | ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- |
  | floatArray | number[] | 是   | 要写入的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 |
Z
zengyawen 已提交
1766

W
wanghaoxu 已提交
1767 1768
**错误码:**

C
crazy_hu 已提交
1769
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
1770

W
wanghaoxu 已提交
1771 1772 1773
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
1774

Y
yangguangzhao 已提交
1775
**示例:**
Y
yangguangzhao 已提交
1776

Y
Yippo 已提交
1777
  ```ts
W
wanghaoxu 已提交
1778 1779 1780 1781 1782 1783 1784
  let data = rpc.MessageSequence.create();
  try {
      data.writeFloatArray([1.2, 1.3, 1.4]);
  } catch(error) {
      console.info("rpc write floatArray fail, errorCode " + error.code);
      console.info("rpc write floatArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1785 1786 1787 1788
  ```

### readFloatArray

C
crazy_hu 已提交
1789
readFloatArray(dataIn: number[]): void
Z
zengyawen 已提交
1790

W
wanghaoxu 已提交
1791
从MessageSequence实例中读取浮点数组。
Z
zengyawen 已提交
1792

Y
yangguangzhao 已提交
1793 1794
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
1795
**参数:**
W
wangqinxiao 已提交
1796

W
wanghaoxu 已提交
1797 1798 1799 1800
  | 参数名 | 类型     | 必填 | 说明                                                                                                                    |
  | ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- |
  | dataIn | number[] | 是   | 要读取的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 |

W
wanghaoxu 已提交
1801 1802
**错误码:**

C
crazy_hu 已提交
1803
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
Z
zengyawen 已提交
1804

W
wanghaoxu 已提交
1805 1806 1807
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
Z
zengyawen 已提交
1808

Y
yangguangzhao 已提交
1809
**示例:**
Y
yangguangzhao 已提交
1810

Y
Yippo 已提交
1811
  ```ts
W
wanghaoxu 已提交
1812 1813 1814
  let data = rpc.MessageSequence.create();
  try {
      data.writeFloatArray([1.2, 1.3, 1.4]);
C
crazy_hu 已提交
1815
  }catch(error){
W
wanghaoxu 已提交
1816 1817 1818
      console.info("rpc write floatArray fail, errorCode " + error.code);
      console.info("rpc write floatArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1819
  let array = new Array(3);
W
wanghaoxu 已提交
1820 1821 1822 1823 1824 1825
  try {
      data.readFloatArray(array);
  } catch(error) {
      console.info("rpc read floatArray fail, errorCode " + error.code);
      console.info("rpc read floatArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1826 1827 1828 1829 1830 1831
  ```

### readFloatArray

readFloatArray(): number[]

W
wanghaoxu 已提交
1832
从MessageSequence实例中读取浮点数组。
Z
zengyawen 已提交
1833

Y
yangguangzhao 已提交
1834
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1835

Y
yangguangzhao 已提交
1836
**返回值:**
W
wangqinxiao 已提交
1837

W
wanghaoxu 已提交
1838 1839
  | 类型     | 说明           |
  | -------- | -------------- |
Y
yangguangzhao 已提交
1840
  | number[] | 返回浮点数组。 |
Z
zengyawen 已提交
1841

W
wanghaoxu 已提交
1842 1843
**错误码:**

C
crazy_hu 已提交
1844
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1845

W
wanghaoxu 已提交
1846 1847 1848
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
1849

Y
yangguangzhao 已提交
1850
**示例:**
Y
yangguangzhao 已提交
1851

Y
Yippo 已提交
1852
  ```ts
W
wanghaoxu 已提交
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
  let data = rpc.MessageSequence.create();
  try {
      data.writeFloatArray([1.2, 1.3, 1.4]);
  } catch(error) {
      console.info("rpc write floatArray fail, errorCode " + error.code);
      console.info("rpc write floatArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readFloatArray();
      console.log("RpcClient: readFloatArray is " + array);
  } catch(error) {
      console.info("rpc read floatArray fail, errorCode " + error.code);
      console.info("rpc read floatArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1867 1868 1869 1870
  ```

### writeDoubleArray

W
wanghaoxu 已提交
1871
writeDoubleArray(doubleArray: number[]): void
Z
zengyawen 已提交
1872

W
wanghaoxu 已提交
1873
将双精度浮点数组写入MessageSequence实例。
Z
zengyawen 已提交
1874

Y
yangguangzhao 已提交
1875
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1876

W
wanghaoxu 已提交
1877
**参数:**
W
wangqinxiao 已提交
1878

W
wanghaoxu 已提交
1879 1880 1881
  | 参数名      | 类型     | 必填 | 说明                     |
  | ----------- | -------- | ---- | ------------------------ |
  | doubleArray | number[] | 是   | 要写入的双精度浮点数组。 |
Z
zengyawen 已提交
1882

W
wanghaoxu 已提交
1883 1884
**错误码:**

C
crazy_hu 已提交
1885
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
1886

W
wanghaoxu 已提交
1887 1888 1889
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
1890

Y
yangguangzhao 已提交
1891
**示例:**
Y
yangguangzhao 已提交
1892

Y
Yippo 已提交
1893
  ```ts
W
wanghaoxu 已提交
1894 1895 1896 1897 1898 1899 1900
  let data = rpc.MessageSequence.create();
  try {
      data.writeDoubleArray([11.1, 12.2, 13.3]);
  } catch(error) {
      console.info("rpc write doubleArray fail, errorCode " + error.code);
      console.info("rpc write doubleArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1901 1902 1903 1904
  ```

### readDoubleArray

C
crazy_hu 已提交
1905
readDoubleArray(dataIn: number[]): void
Z
zengyawen 已提交
1906

Y
Yippo 已提交
1907
从MessageSequence实例中读取双精度浮点数组。
Z
zengyawen 已提交
1908

Y
yangguangzhao 已提交
1909
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1910

W
wanghaoxu 已提交
1911
**参数:**
W
wangqinxiao 已提交
1912

W
wanghaoxu 已提交
1913 1914 1915 1916
  | 参数名 | 类型     | 必填 | 说明                     |
  | ------ | -------- | ---- | ------------------------ |
  | dataIn | number[] | 是   | 要读取的双精度浮点数组。 |

W
wanghaoxu 已提交
1917 1918
**错误码:**

C
crazy_hu 已提交
1919
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1920

W
wanghaoxu 已提交
1921 1922 1923
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
Z
zengyawen 已提交
1924

Y
yangguangzhao 已提交
1925
**示例:**
Y
yangguangzhao 已提交
1926

Y
Yippo 已提交
1927
  ```ts
W
wanghaoxu 已提交
1928 1929 1930 1931 1932 1933 1934
  let data = rpc.MessageSequence.create();
  try {
      data.writeDoubleArray([11.1, 12.2, 13.3]);
  } catch(error) {
      console.info("rpc write doubleArray fail, errorCode " + error.code);
      console.info("rpc write doubleArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1935
  let array = new Array(3);
W
wanghaoxu 已提交
1936 1937 1938 1939 1940 1941
  try {
      data.readDoubleArray(array);
  } catch(error) {
      console.info("rpc read doubleArray fail, errorCode " + error.code);
      console.info("rpc read doubleArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1942 1943 1944 1945 1946 1947
  ```

### readDoubleArray

readDoubleArray(): number[]

1948
从MessageSequence实例读取所有双精度浮点数组。
Z
zengyawen 已提交
1949

Y
yangguangzhao 已提交
1950
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1951

Y
yangguangzhao 已提交
1952
**返回值:**
W
wangqinxiao 已提交
1953

W
wanghaoxu 已提交
1954 1955
  | 类型     | 说明                 |
  | -------- | -------------------- |
Y
yangguangzhao 已提交
1956
  | number[] | 返回双精度浮点数组。 |
Z
zengyawen 已提交
1957

W
wanghaoxu 已提交
1958 1959
**错误码:**

C
crazy_hu 已提交
1960
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
1961

W
wanghaoxu 已提交
1962 1963 1964
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
1965

Y
yangguangzhao 已提交
1966
**示例:**
Y
yangguangzhao 已提交
1967

Y
Yippo 已提交
1968
  ```ts
W
wanghaoxu 已提交
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
  let data = rpc.MessageSequence.create();
  try {
      data.writeDoubleArray([11.1, 12.2, 13.3]);
  } catch(error) {
      console.info("rpc write doubleArray fail, errorCode " + error.code);
      console.info("rpc write doubleArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readDoubleArray();
      console.log("RpcClient: readDoubleArray is " + array);
  } catch(error) {
      console.info("rpc read doubleArray fail, errorCode " + error.code);
      console.info("rpc read doubleArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1983 1984 1985 1986
  ```

### writeBooleanArray

W
wanghaoxu 已提交
1987
writeBooleanArray(booleanArray: boolean[]): void
Z
zengyawen 已提交
1988

W
wanghaoxu 已提交
1989
将布尔数组写入MessageSequence实例。
Z
zengyawen 已提交
1990

Y
yangguangzhao 已提交
1991
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1992

W
wanghaoxu 已提交
1993
**参数:**
W
wangqinxiao 已提交
1994

W
wanghaoxu 已提交
1995 1996 1997
  | 参数名       | 类型      | 必填 | 说明               |
  | ------------ | --------- | ---- | ------------------ |
  | booleanArray | boolean[] | 是   | 要写入的布尔数组。 |
Z
zengyawen 已提交
1998

W
wanghaoxu 已提交
1999 2000
**错误码:**

C
crazy_hu 已提交
2001
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
2002

W
wanghaoxu 已提交
2003 2004 2005
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
2006

Y
yangguangzhao 已提交
2007
**示例:**
Y
yangguangzhao 已提交
2008

Y
Yippo 已提交
2009
  ```ts
W
wanghaoxu 已提交
2010 2011 2012 2013 2014 2015 2016
  let data = rpc.MessageSequence.create();
  try {
      data.writeBooleanArray([false, true, false]);
  } catch(error) {
      console.info("rpc write booleanArray fail, errorCode " + error.code);
      console.info("rpc write booleanArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2017 2018 2019 2020
  ```

### readBooleanArray

C
crazy_hu 已提交
2021
readBooleanArray(dataIn: boolean[]): void
Z
zengyawen 已提交
2022

Y
Yippo 已提交
2023
从MessageSequence实例中读取布尔数组。
Z
zengyawen 已提交
2024

Y
yangguangzhao 已提交
2025
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2026

W
wanghaoxu 已提交
2027
**参数:**
W
wangqinxiao 已提交
2028

W
wanghaoxu 已提交
2029 2030 2031 2032
  | 参数名 | 类型      | 必填 | 说明               |
  | ------ | --------- | ---- | ------------------ |
  | dataIn | boolean[] | 是   | 要读取的布尔数组。 |

W
wanghaoxu 已提交
2033 2034
**错误码:**

C
crazy_hu 已提交
2035
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2036

W
wanghaoxu 已提交
2037 2038 2039
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
Z
zengyawen 已提交
2040

Y
yangguangzhao 已提交
2041
**示例:**
Y
yangguangzhao 已提交
2042

Y
Yippo 已提交
2043
  ```ts
W
wanghaoxu 已提交
2044 2045 2046 2047 2048 2049 2050
  let data = rpc.MessageSequence.create();
  try {
      data.writeBooleanArray([false, true, false]);
  } catch(error) {
      console.info("rpc write booleanArray fail, errorCode " + error.code);
      console.info("rpc write booleanArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2051
  let array = new Array(3);
W
wanghaoxu 已提交
2052 2053 2054 2055 2056 2057
  try {
      data.readBooleanArray(array);
  } catch(error) {
      console.info("rpc read booleanArray fail, errorCode " + error.code);
      console.info("rpc read booleanArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2058 2059 2060 2061 2062 2063
  ```

### readBooleanArray

readBooleanArray(): boolean[]

2064
从MessageSequence实例中读取所有布尔数组。
Z
zengyawen 已提交
2065

Y
yangguangzhao 已提交
2066
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2067

Y
yangguangzhao 已提交
2068
**返回值:**
W
wangqinxiao 已提交
2069

W
wanghaoxu 已提交
2070 2071
  | 类型      | 说明           |
  | --------- | -------------- |
Y
yangguangzhao 已提交
2072 2073
  | boolean[] | 返回布尔数组。 |

W
wanghaoxu 已提交
2074 2075
**错误码:**

C
crazy_hu 已提交
2076
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2077

W
wanghaoxu 已提交
2078 2079 2080
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
2081 2082

**示例:**
Z
zengyawen 已提交
2083

Y
Yippo 已提交
2084
  ```ts
W
wanghaoxu 已提交
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
  let data = rpc.MessageSequence.create();
  try {
      data.writeBooleanArray([false, true, false]);
  } catch(error) {
      console.info("rpc write booleanArray fail, errorCode " + error.code);
      console.info("rpc write booleanArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readBooleanArray();
      console.log("RpcClient: readBooleanArray is " + array);
  } catch(error) {
      console.info("rpc read booleanArray fail, errorCode " + error.code);
      console.info("rpc read booleanArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2099 2100 2101 2102
  ```

### writeCharArray

W
wanghaoxu 已提交
2103
writeCharArray(charArray: number[]): void
Z
zengyawen 已提交
2104

W
wanghaoxu 已提交
2105
将单个字符数组写入MessageSequence实例。
Z
zengyawen 已提交
2106

Y
yangguangzhao 已提交
2107
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2108

W
wanghaoxu 已提交
2109
**参数:**
W
wangqinxiao 已提交
2110

W
wanghaoxu 已提交
2111 2112 2113
  | 参数名    | 类型     | 必填 | 说明                   |
  | --------- | -------- | ---- | ---------------------- |
  | charArray | number[] | 是   | 要写入的单个字符数组。 |
Z
zengyawen 已提交
2114

W
wanghaoxu 已提交
2115 2116
**错误码:**

C
crazy_hu 已提交
2117
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
2118

W
wanghaoxu 已提交
2119 2120 2121
  | 错误码ID | 错误信息 |
  | -------- | ------ |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
2122

Y
yangguangzhao 已提交
2123
**示例:**
Y
yangguangzhao 已提交
2124

Y
Yippo 已提交
2125
  ```ts
W
wanghaoxu 已提交
2126 2127 2128 2129 2130 2131 2132
  let data = rpc.MessageSequence.create();
  try {
      data.writeCharArray([97, 98, 88]);
  } catch(error) {
      console.info("rpc write charArray fail, errorCode " + error.code);
      console.info("rpc write charArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2133 2134 2135 2136
  ```

### readCharArray

C
crazy_hu 已提交
2137
readCharArray(dataIn: number[]): void
Z
zengyawen 已提交
2138

W
wanghaoxu 已提交
2139
从MessageSequence实例中读取单个字符数组。
Z
zengyawen 已提交
2140

Y
yangguangzhao 已提交
2141
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2142

W
wanghaoxu 已提交
2143
**参数:**
W
wangqinxiao 已提交
2144

W
wanghaoxu 已提交
2145 2146 2147 2148
  | 参数名 | 类型     | 必填 | 说明                   |
  | ------ | -------- | ---- | ---------------------- |
  | dataIn | number[] | 是   | 要读取的单个字符数组。 |

W
wanghaoxu 已提交
2149 2150
**错误码:**

C
crazy_hu 已提交
2151
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2152

W
wanghaoxu 已提交
2153 2154 2155
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
Z
zengyawen 已提交
2156

Y
yangguangzhao 已提交
2157
**示例:**
Y
yangguangzhao 已提交
2158

Y
Yippo 已提交
2159
  ```ts
W
wanghaoxu 已提交
2160 2161 2162 2163 2164 2165 2166
  let data = rpc.MessageSequence.create();
  try {
      data.writeCharArray([97, 98, 88]);
  } catch(error) {
      console.info("rpc write charArray fail, errorCode " + error.code);
      console.info("rpc write charArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2167
  let array = new Array(3);
W
wanghaoxu 已提交
2168 2169 2170 2171 2172 2173
  try {
      data.readCharArray(array);
  } catch(error) {
      console.info("rpc read charArray fail, errorCode " + error.code);
      console.info("rpc read charArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2174 2175 2176 2177
  ```

### readCharArray

Y
yangguangzhao 已提交
2178
readCharArray(): number[]
Z
zengyawen 已提交
2179

W
wanghaoxu 已提交
2180
从MessageSequence实例读取单个字符数组。
Z
zengyawen 已提交
2181

Y
yangguangzhao 已提交
2182
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2183

Y
yangguangzhao 已提交
2184
**返回值:**
W
wangqinxiao 已提交
2185

W
wanghaoxu 已提交
2186 2187
  | 类型     | 说明               |
  | -------- | ------------------ |
Y
yangguangzhao 已提交
2188
  | number[] | 返回单个字符数组。 |
Z
zengyawen 已提交
2189

W
wanghaoxu 已提交
2190 2191
**错误码:**

C
crazy_hu 已提交
2192
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2193

W
wanghaoxu 已提交
2194 2195 2196
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
2197

Y
yangguangzhao 已提交
2198
**示例:**
Y
yangguangzhao 已提交
2199

Y
Yippo 已提交
2200
  ```ts
W
wanghaoxu 已提交
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
  let data = rpc.MessageSequence.create();
  try {
      data.writeCharArray([97, 98, 88]);
  } catch(error) {
      console.info("rpc write charArray fail, errorCode " + error.code);
      console.info("rpc write charArray fail, errorMessage" + error.message);
  }
  let array = new Array(3);
  try {
      let array = data.readCharArray();
      console.log("RpcClient: readCharArray is " + array);
  } catch(error) {
      console.info("rpc read charArray fail, errorCode " + error.code);
      console.info("rpc read charArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2216 2217 2218 2219
  ```

### writeStringArray

W
wanghaoxu 已提交
2220
writeStringArray(stringArray: string[]): void
Z
zengyawen 已提交
2221

W
wanghaoxu 已提交
2222
将字符串数组写入MessageSequence实例。
Z
zengyawen 已提交
2223

Y
yangguangzhao 已提交
2224
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2225

W
wanghaoxu 已提交
2226
**参数:**
W
wangqinxiao 已提交
2227

W
wanghaoxu 已提交
2228 2229 2230
  | 参数名      | 类型     | 必填 | 说明                                                    |
  | ----------- | -------- | ---- | ------------------------------------------------------- |
  | stringArray | string[] | 是   | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 |
Z
zengyawen 已提交
2231

W
wanghaoxu 已提交
2232 2233
**错误码:**

C
crazy_hu 已提交
2234
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
2235

W
wanghaoxu 已提交
2236 2237 2238
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
2239

Y
yangguangzhao 已提交
2240
**示例:**
Y
yangguangzhao 已提交
2241

Y
Yippo 已提交
2242
  ```ts
W
wanghaoxu 已提交
2243 2244 2245 2246 2247 2248 2249
  let data = rpc.MessageSequence.create();
  try {
      data.writeStringArray(["abc", "def"]);
  } catch(error) {
      console.info("rpc write stringArray fail, errorCode " + error.code);
      console.info("rpc write stringArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2250 2251 2252 2253
  ```

### readStringArray

C
crazy_hu 已提交
2254
readStringArray(dataIn: string[]): void
Z
zengyawen 已提交
2255

W
wanghaoxu 已提交
2256
从MessageSequence实例读取字符串数组。
Z
zengyawen 已提交
2257

Y
yangguangzhao 已提交
2258
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2259

W
wanghaoxu 已提交
2260
**参数:**
W
wangqinxiao 已提交
2261

W
wanghaoxu 已提交
2262 2263 2264 2265
  | 参数名 | 类型     | 必填 | 说明                 |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | string[] | 是   | 要读取的字符串数组。 |

W
wanghaoxu 已提交
2266 2267
**错误码:**

C
crazy_hu 已提交
2268
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2269

W
wanghaoxu 已提交
2270 2271 2272
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
Z
zengyawen 已提交
2273

Y
yangguangzhao 已提交
2274
**示例:**
Y
yangguangzhao 已提交
2275

Y
Yippo 已提交
2276
  ```ts
W
wanghaoxu 已提交
2277 2278 2279 2280 2281 2282 2283
  let data = rpc.MessageSequence.create();
  try {
      data.writeStringArray(["abc", "def"]);
  } catch(error) {
      console.info("rpc write stringArray fail, errorCode " + error.code);
      console.info("rpc write stringArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2284
  let array = new Array(2);
W
wanghaoxu 已提交
2285 2286 2287 2288 2289 2290
  try {
      data.readStringArray(array);
  } catch(error) {
      console.info("rpc read stringArray fail, errorCode " + error.code);
      console.info("rpc read stringArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2291 2292 2293 2294 2295 2296
  ```

### readStringArray

readStringArray(): string[]

W
wanghaoxu 已提交
2297
从MessageSequence实例读取字符串数组。
Z
zengyawen 已提交
2298

Y
yangguangzhao 已提交
2299
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2300

Y
yangguangzhao 已提交
2301
**返回值:**
W
wangqinxiao 已提交
2302

W
wanghaoxu 已提交
2303 2304
  | 类型     | 说明             |
  | -------- | ---------------- |
Y
yangguangzhao 已提交
2305
  | string[] | 返回字符串数组。 |
Z
zengyawen 已提交
2306

W
wanghaoxu 已提交
2307 2308
**错误码:**

C
crazy_hu 已提交
2309
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2310

W
wanghaoxu 已提交
2311 2312 2313
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
2314

Y
yangguangzhao 已提交
2315
**示例:**
Y
yangguangzhao 已提交
2316

Y
Yippo 已提交
2317
  ```ts
W
wanghaoxu 已提交
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
  let data = rpc.MessageSequence.create();
  try {
      data.writeStringArray(["abc", "def"]);
  } catch(error) {
      console.info("rpc write stringArray fail, errorCode " + error.code);
      console.info("rpc write stringArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readStringArray();
      console.log("RpcClient: readStringArray is " + array);
  } catch(error) {
      console.info("rpc read stringArray fail, errorCode " + error.code);
      console.info("rpc read stringArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2332 2333
  ```

W
wanghaoxu 已提交
2334
### writeNoException
Z
zengyawen 已提交
2335 2336 2337

writeNoException(): void

W
wanghaoxu 已提交
2338
向MessageSequence写入“指示未发生异常”的信息。
Z
zengyawen 已提交
2339

Y
yangguangzhao 已提交
2340
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2341

W
wanghaoxu 已提交
2342 2343
**错误码:**

C
crazy_hu 已提交
2344
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2345

W
wanghaoxu 已提交
2346 2347 2348
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
W
wanghaoxu 已提交
2349

Y
yangguangzhao 已提交
2350
**示例:**
Y
yangguangzhao 已提交
2351

Y
Yippo 已提交
2352
  ```ts
Z
zengyawen 已提交
2353 2354 2355 2356
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
C
crazy_hu 已提交
2357

Z
zengyawen 已提交
2358 2359 2360
      onRemoteRequest(code, data, reply, option) {
          if (code === 1) {
              console.log("RpcServer: onRemoteRequest called");
W
wanghaoxu 已提交
2361 2362 2363 2364 2365 2366
              try {
                  reply.writeNoException();
              } catch(error) {
                  console.info("rpc write no exception fail, errorCode " + error.code);
                  console.info("rpc write no exception fail, errorMessage" + error.message);
              }
Z
zengyawen 已提交
2367 2368 2369 2370 2371 2372 2373 2374 2375
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
  }
  ```

W
wanghaoxu 已提交
2376
### readException
Z
zengyawen 已提交
2377 2378 2379

readException(): void

W
wanghaoxu 已提交
2380
从MessageSequence中读取异常。
Z
zengyawen 已提交
2381

Y
yangguangzhao 已提交
2382
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2383

W
wanghaoxu 已提交
2384 2385
**错误码:**

C
crazy_hu 已提交
2386
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2387

W
wanghaoxu 已提交
2388 2389 2390
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
2391

Y
yangguangzhao 已提交
2392
**示例:**
Y
yangguangzhao 已提交
2393

2394 2395
  获取服务

Y
Yippo 已提交
2396
  ```ts
Z
zengyawen 已提交
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
Y
yangguangzhao 已提交
2412
      "bundleName": "com.ohos.server",
2413
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
2414 2415
  };
  FA.connectAbility(want, connect);
2416 2417
  ```

2418
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息
2419 2420

  ```ts
Z
zengyawen 已提交
2421
  let option = new rpc.MessageOption();
W
wanghaoxu 已提交
2422 2423
  let data = rpc.MessageSequence.create();
  let reply = rpc.MessageSequence.create();
Z
zengyawen 已提交
2424 2425
  data.writeInt(1);
  data.writeString("hello");
W
wanghaoxu 已提交
2426
  proxy.sendMessageRequest(1, data, reply, option)
Z
zengyawen 已提交
2427 2428
      .then(function(errCode) {
          if (errCode === 0) {
W
wanghaoxu 已提交
2429
              console.log("sendMessageRequest got result");
W
wanghaoxu 已提交
2430 2431 2432 2433 2434 2435
              try {
                  reply.readException();
              } catch(error) {
                  console.info("rpc read exception fail, errorCode " + error.code);
                  console.info("rpc read no exception fail, errorMessage" + error.message);
              }
Z
zengyawen 已提交
2436 2437 2438
              let msg = reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
W
wanghaoxu 已提交
2439
              console.log("RPCTest: sendMessageRequest failed, errCode: " + errCode);
Z
zengyawen 已提交
2440 2441
          }
      }).catch(function(e) {
W
wanghaoxu 已提交
2442
          console.log("RPCTest: sendMessageRequest got exception: " + e.message);
Z
zengyawen 已提交
2443
      }).finally (() => {
W
wanghaoxu 已提交
2444
          console.log("RPCTest: sendMessageRequest ends, reclaim parcel");
Z
zengyawen 已提交
2445 2446 2447 2448 2449
          data.reclaim();
          reply.reclaim();
      });
  ```

W
wanghaoxu 已提交
2450
### writeParcelableArray
Z
zengyawen 已提交
2451

W
wanghaoxu 已提交
2452
writeParcelableArray(parcelableArray: Parcelable[]): void
Z
zengyawen 已提交
2453

W
wanghaoxu 已提交
2454
将可序列化对象数组写入MessageSequence实例。
Z
zengyawen 已提交
2455

Y
yangguangzhao 已提交
2456
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2457

W
wanghaoxu 已提交
2458
**参数:**
W
wangqinxiao 已提交
2459

W
wanghaoxu 已提交
2460 2461 2462
  | 参数名          | 类型         | 必填 | 说明                       |
  | --------------- | ------------ | ---- | -------------------------- |
  | parcelableArray | Parcelable[] | 是   | 要写入的可序列化对象数组。 |
Z
zengyawen 已提交
2463

W
wanghaoxu 已提交
2464 2465
**错误码:**

C
crazy_hu 已提交
2466
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
2467

W
wanghaoxu 已提交
2468 2469 2470
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
2471

Y
yangguangzhao 已提交
2472
**示例:**
Y
yangguangzhao 已提交
2473

Y
Yippo 已提交
2474
  ```ts
W
wanghaoxu 已提交
2475
  class MyParcelable {
Y
yangguangzhao 已提交
2476 2477 2478
      num: number;
      str: string;
      constructor(num, str) {
Z
zengyawen 已提交
2479
          this.num = num;
Y
yangguangzhao 已提交
2480
          this.str = str;
Z
zengyawen 已提交
2481
      }
W
wanghaoxu 已提交
2482 2483 2484
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
Z
zengyawen 已提交
2485 2486
          return true;
      }
W
wanghaoxu 已提交
2487 2488 2489
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
Z
zengyawen 已提交
2490 2491 2492
          return true;
      }
  }
W
wanghaoxu 已提交
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
  let parcelable = new MyParcelable(1, "aaa");
  let parcelable2 = new MyParcelable(2, "bbb");
  let parcelable3 = new MyParcelable(3, "ccc");
  let a = [parcelable, parcelable2, parcelable3];
  let data = rpc.MessageSequence.create();
  try {
      data.writeParcelableArray(a);
  } catch(error) {
      console.info("rpc write parcelable array fail, errorCode " + error.code);
      console.info("rpc write parcelable array fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2504 2505
  ```

W
wanghaoxu 已提交
2506
### readParcelableArray
Z
zengyawen 已提交
2507

W
wanghaoxu 已提交
2508
readParcelableArray(parcelableArray: Parcelable[]): void
Z
zengyawen 已提交
2509

W
wanghaoxu 已提交
2510
从MessageSequence实例读取可序列化对象数组。
Z
zengyawen 已提交
2511

Y
yangguangzhao 已提交
2512
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2513

W
wanghaoxu 已提交
2514
**参数:**
W
wangqinxiao 已提交
2515

W
wanghaoxu 已提交
2516 2517 2518 2519
  | 参数名          | 类型         | 必填 | 说明                       |
  | --------------- | ------------ | ---- | -------------------------- |
  | parcelableArray | Parcelable[] | 是   | 要读取的可序列化对象数组。 |

W
wanghaoxu 已提交
2520 2521
**错误码:**

C
crazy_hu 已提交
2522
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2523

W
wanghaoxu 已提交
2524 2525 2526 2527
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
  | 1900012 | call js callback function failed |
Z
zengyawen 已提交
2528

Y
yangguangzhao 已提交
2529
**示例:**
Y
yangguangzhao 已提交
2530

Y
Yippo 已提交
2531
  ```ts
W
wanghaoxu 已提交
2532
  class MyParcelable {
Y
yangguangzhao 已提交
2533 2534 2535
      num: number;
      str: string;
      constructor(num, str) {
Z
zengyawen 已提交
2536
          this.num = num;
Y
yangguangzhao 已提交
2537
          this.str = str;
Z
zengyawen 已提交
2538
      }
W
wanghaoxu 已提交
2539 2540 2541
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
Z
zengyawen 已提交
2542 2543
          return true;
      }
W
wanghaoxu 已提交
2544 2545 2546
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
Z
zengyawen 已提交
2547 2548 2549
          return true;
      }
  }
W
wanghaoxu 已提交
2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
  let parcelable = new MyParcelable(1, "aaa");
  let parcelable2 = new MyParcelable(2, "bbb");
  let parcelable3 = new MyParcelable(3, "ccc");
  let a = [parcelable, parcelable2, parcelable3];
  let data = rpc.MessageSequence.create();
  let result = data.writeParcelableArray(a);
  console.log("RpcClient: writeParcelableArray is " + result);
  let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")];
  try {
      data.readParcelableArray(b);
  } catch(error) {
      console.info("rpc read parcelable array fail, errorCode " + error.code);
      console.info("rpc read parcelable array fail, errorMessage" + error.message);
  }
  data.readParcelableArray(b);
Z
zengyawen 已提交
2565 2566
  ```

W
wanghaoxu 已提交
2567
### writeRemoteObjectArray
Z
zengyawen 已提交
2568

W
wanghaoxu 已提交
2569
writeRemoteObjectArray(objectArray: IRemoteObject[]): void
Z
zengyawen 已提交
2570

W
wanghaoxu 已提交
2571
将IRemoteObject对象数组写入MessageSequence。
Z
zengyawen 已提交
2572

Y
yangguangzhao 已提交
2573
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2574

W
wanghaoxu 已提交
2575
**参数:**
C
crazy_hu 已提交
2576

W
wanghaoxu 已提交
2577 2578 2579
  | 参数名      | 类型            | 必填 | 说明                                           |
  | ----------- | --------------- | ---- | ---------------------------------------------- |
  | objectArray | IRemoteObject[] | 是   | 要写入MessageSequence的IRemoteObject对象数组。 |
Z
zengyawen 已提交
2580

W
wanghaoxu 已提交
2581 2582
**错误码:**

C
crazy_hu 已提交
2583
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
2584

W
wanghaoxu 已提交
2585 2586 2587
  | 错误码ID | 错误信息 |
  | ------- | ------- |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
2588

Y
yangguangzhao 已提交
2589
**示例:**
Y
yangguangzhao 已提交
2590

Y
Yippo 已提交
2591
  ```ts
Z
zengyawen 已提交
2592 2593 2594
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
W
wanghaoxu 已提交
2595
          this.modifyLocalInterface(this, descriptor);
Y
yangguangzhao 已提交
2596
      }
C
crazy_hu 已提交
2597

Y
yangguangzhao 已提交
2598 2599 2600
      asObject(): rpc.IRemoteObject {
          return this;
      }
Z
zengyawen 已提交
2601 2602
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
W
wanghaoxu 已提交
2603
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
2604
  let result = data.writeRemoteObjectArray(a);
W
wanghaoxu 已提交
2605 2606 2607 2608 2609 2610
  try {
      data.writeRemoteObjectArray(a);
  } catch(error) {
      console.info("rpc write remote object array fail, errorCode " + error.code);
      console.info("rpc write remote object array fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2611 2612 2613
  console.log("RpcClient: writeRemoteObjectArray is " + result);
  ```

W
wanghaoxu 已提交
2614
### readRemoteObjectArray
Z
zengyawen 已提交
2615 2616 2617

readRemoteObjectArray(objects: IRemoteObject[]): void

W
wanghaoxu 已提交
2618
从MessageSequence读取IRemoteObject对象数组。
Z
zengyawen 已提交
2619

Y
yangguangzhao 已提交
2620
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2621

W
wanghaoxu 已提交
2622
**参数:**
W
wangqinxiao 已提交
2623

W
wanghaoxu 已提交
2624 2625 2626 2627
  | 参数名  | 类型            | 必填 | 说明                                           |
  | ------- | --------------- | ---- | ---------------------------------------------- |
  | objects | IRemoteObject[] | 是   | 从MessageSequence读取的IRemoteObject对象数组。 |

W
wanghaoxu 已提交
2628 2629
**错误码:**

C
crazy_hu 已提交
2630
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2631

W
wanghaoxu 已提交
2632 2633 2634
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
Z
zengyawen 已提交
2635

Y
yangguangzhao 已提交
2636
**示例:**
Y
yangguangzhao 已提交
2637

Y
Yippo 已提交
2638
  ```ts
Y
yangguangzhao 已提交
2639 2640
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
2641
          console.log("server died");
Y
yangguangzhao 已提交
2642 2643
      }
  }
Z
zengyawen 已提交
2644 2645 2646
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
W
wanghaoxu 已提交
2647
          this.modifyLocalInterface(this, descriptor);
Y
yangguangzhao 已提交
2648
      }
C
crazy_hu 已提交
2649

Y
yangguangzhao 已提交
2650 2651 2652
      asObject(): rpc.IRemoteObject {
          return this;
      }
Z
zengyawen 已提交
2653 2654
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
W
wanghaoxu 已提交
2655 2656
  let data = rpc.MessageSequence.create();
  data.writeRemoteObjectArray(a);
Z
zengyawen 已提交
2657
  let b = new Array(3);
W
wanghaoxu 已提交
2658 2659 2660 2661 2662 2663
  try {
      data.readRemoteObjectArray(b);
  } catch(error) {
      console.info("rpc read remote object array fail, errorCode " + error.code);
      console.info("rpc read remote object array fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2664 2665 2666
  data.readRemoteObjectArray(b);
  ```

W
wanghaoxu 已提交
2667
### readRemoteObjectArray
Z
zengyawen 已提交
2668 2669 2670

readRemoteObjectArray(): IRemoteObject[]

W
wanghaoxu 已提交
2671
从MessageSequence读取IRemoteObject对象数组。
Z
zengyawen 已提交
2672

Y
yangguangzhao 已提交
2673
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2674

Y
yangguangzhao 已提交
2675
**返回值:**
W
wangqinxiao 已提交
2676

W
wanghaoxu 已提交
2677 2678
  | 类型            | 说明                        |
  | --------------- | --------------------------- |
Y
yangguangzhao 已提交
2679
  | IRemoteObject[] | 返回IRemoteObject对象数组。 |
Z
zengyawen 已提交
2680

W
wanghaoxu 已提交
2681 2682
**错误码:**

C
crazy_hu 已提交
2683
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2684

W
wanghaoxu 已提交
2685 2686 2687
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
2688

Y
yangguangzhao 已提交
2689
**示例:**
Y
yangguangzhao 已提交
2690

Y
Yippo 已提交
2691
  ```ts
Z
zengyawen 已提交
2692 2693 2694
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
W
wanghaoxu 已提交
2695
          this.modifyLocalInterface(this, descriptor);
Y
yangguangzhao 已提交
2696
      }
C
crazy_hu 已提交
2697
 
Y
yangguangzhao 已提交
2698 2699 2700
      asObject(): rpc.IRemoteObject {
          return this;
      }
Z
zengyawen 已提交
2701 2702
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
W
wanghaoxu 已提交
2703 2704 2705 2706
  let data = rpc.MessageSequence.create();
  data.writeRemoteObjectArray(a);
  try {
      let b = data.readRemoteObjectArray();
C
crazy_hu 已提交
2707
      console.log("RpcClient: readRemoteObjectArray is " + b);
W
wanghaoxu 已提交
2708 2709 2710 2711
  } catch(error) {
      console.info("rpc read remote object array fail, errorCode " + error.code);
      console.info("rpc read remote object array fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2712 2713 2714
  ```


C
crazy_hu 已提交
2715
### closeFileDescriptor<sup>9+</sup>
Z
zengyawen 已提交
2716 2717 2718

static closeFileDescriptor(fd: number): void

2719
静态方法,关闭给定的文件描述符。
Z
zengyawen 已提交
2720

Y
yangguangzhao 已提交
2721
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2722

W
wanghaoxu 已提交
2723
**参数:**
W
wangqinxiao 已提交
2724

W
wanghaoxu 已提交
2725 2726 2727 2728
  | 参数名 | 类型   | 必填 | 说明                 |
  | ------ | ------ | ---- | -------------------- |
  | fd     | number | 是   | 要关闭的文件描述符。 |

Y
yangguangzhao 已提交
2729
**示例:**
Y
yangguangzhao 已提交
2730

Y
Yippo 已提交
2731
  ```ts
Z
zengyawen 已提交
2732 2733 2734
  import fileio from '@ohos.fileio';
  let filePath = "path/to/file";
  let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
W
wanghaoxu 已提交
2735 2736 2737 2738 2739 2740
  try {
      rpc.MessageSequence.closeFileDescriptor(fd);
  } catch(error) {
      console.info("rpc close file descriptor fail, errorCode " + error.code);
      console.info("rpc close file descriptor fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2741 2742
  ```

W
wanghaoxu 已提交
2743
### dupFileDescriptor
Z
zengyawen 已提交
2744 2745 2746

static dupFileDescriptor(fd: number) :number

2747
静态方法,复制给定的文件描述符。
Z
zengyawen 已提交
2748

Y
yangguangzhao 已提交
2749
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2750

W
wanghaoxu 已提交
2751
**参数:**
W
wangqinxiao 已提交
2752

W
wanghaoxu 已提交
2753 2754 2755
  | 参数名 | 类型   | 必填 | 说明                     |
  | ------ | ------ | ---- | ------------------------ |
  | fd     | number | 是   | 表示已存在的文件描述符。 |
Z
zengyawen 已提交
2756

Y
yangguangzhao 已提交
2757
**返回值:**
W
wangqinxiao 已提交
2758

W
wanghaoxu 已提交
2759 2760
  | 类型   | 说明                 |
  | ------ | -------------------- |
Y
yangguangzhao 已提交
2761
  | number | 返回新的文件描述符。 |
Z
zengyawen 已提交
2762

W
wanghaoxu 已提交
2763 2764
**错误码:**

C
crazy_hu 已提交
2765
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2766

W
wanghaoxu 已提交
2767 2768 2769
  | 错误码ID | 错误信息 |
  | ------- | ------- |
  | 1900013 | call os dup function failed |
W
wanghaoxu 已提交
2770

Y
yangguangzhao 已提交
2771
**示例:**
Y
yangguangzhao 已提交
2772

Y
Yippo 已提交
2773
  ```ts
Z
zengyawen 已提交
2774 2775 2776
  import fileio from '@ohos.fileio';
  let filePath = "path/to/file";
  let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
W
wanghaoxu 已提交
2777 2778 2779 2780 2781 2782
  try {
      let newFd = rpc.MessageSequence.dupFileDescriptor(fd);
  } catch(error) {
      console.info("rpc dup file descriptor fail, errorCode " + error.code);
      console.info("rpc dup file descriptor fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2783 2784
  ```

W
wanghaoxu 已提交
2785
### containFileDescriptors
Z
zengyawen 已提交
2786 2787 2788

containFileDescriptors(): boolean

W
wanghaoxu 已提交
2789
检查此MessageSequence对象是否包含文件描述符。
Z
zengyawen 已提交
2790

Y
yangguangzhao 已提交
2791
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2792

Y
yangguangzhao 已提交
2793
**返回值:**
W
wangqinxiao 已提交
2794

W
wanghaoxu 已提交
2795 2796
  | 类型    | 说明                                                                 |
  | ------- | -------------------------------------------------------------------- |
2797
  | boolean | true:包含文件描述符,false:不包含文件描述符。|
Z
zengyawen 已提交
2798

Y
yangguangzhao 已提交
2799
**示例:**
Y
yangguangzhao 已提交
2800

W
wanghaoxu 已提交
2801

Y
Yippo 已提交
2802
  ```ts
Z
zengyawen 已提交
2803
  import fileio from '@ohos.fileio';
W
wanghaoxu 已提交
2804
  let sequence = new rpc.MessageSequence();
Z
zengyawen 已提交
2805
  let filePath = "path/to/file";
W
wanghaoxu 已提交
2806
  let r1 = sequence.containFileDescriptors();
Z
zengyawen 已提交
2807
  let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
W
wanghaoxu 已提交
2808 2809 2810 2811 2812 2813 2814 2815
  try {
      sequence.writeFileDescriptor(fd);
  } catch(error) {
      console.info("rpc write file descriptor fail, errorCode " + error.code);
      console.info("rpc write file descriptor fail, errorMessage" + error.message);
  }
  try {
      let containFD = sequence.containFileDescriptors();
C
crazy_hu 已提交
2816
      console.log("RpcTest: sequence after write fd containFd result is : " + containFD);
W
wanghaoxu 已提交
2817 2818 2819 2820
  } catch(error) {
      console.info("rpc contain file descriptor fail, errorCode " + error.code);
      console.info("rpc contain file descriptor fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2821 2822
  ```

W
wanghaoxu 已提交
2823
### writeFileDescriptor
Z
zengyawen 已提交
2824

W
wanghaoxu 已提交
2825
writeFileDescriptor(fd: number): void
Z
zengyawen 已提交
2826

W
wanghaoxu 已提交
2827
写入文件描述符到MessageSequence。
Z
zengyawen 已提交
2828

Y
yangguangzhao 已提交
2829
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2830

W
wanghaoxu 已提交
2831
**参数:**
W
wangqinxiao 已提交
2832

W
wanghaoxu 已提交
2833 2834 2835
  | 参数名 | 类型   | 必填 | 说明         |
  | ------ | ------ | ---- | ------------ |
  | fd     | number | 是   | 文件描述符。 |
Z
zengyawen 已提交
2836

W
wanghaoxu 已提交
2837 2838
**错误码:**

C
crazy_hu 已提交
2839
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
2840

W
wanghaoxu 已提交
2841 2842 2843
  | 错误码ID | 错误信息 |
  | -------- | ------ |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
2844

Y
yangguangzhao 已提交
2845
**示例:**
Y
yangguangzhao 已提交
2846

Y
Yippo 已提交
2847
  ```ts
Z
zengyawen 已提交
2848
  import fileio from '@ohos.fileio';
W
wanghaoxu 已提交
2849
  let sequence = new rpc.MessageSequence();
Z
zengyawen 已提交
2850 2851
  let filePath = "path/to/file";
  let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
W
wanghaoxu 已提交
2852 2853 2854 2855 2856 2857
  try {
      sequence.writeFileDescriptor(fd);
  } catch(error) {
      console.info("rpc write file descriptor fail, errorCode " + error.code);
      console.info("rpc write file descriptor fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2858 2859
  ```

W
wanghaoxu 已提交
2860
### readFileDescriptor
Z
zengyawen 已提交
2861 2862 2863

readFileDescriptor(): number

W
wanghaoxu 已提交
2864
从MessageSequence中读取文件描述符。
Z
zengyawen 已提交
2865

Y
yangguangzhao 已提交
2866
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2867

Y
yangguangzhao 已提交
2868
**返回值:**
W
wangqinxiao 已提交
2869

W
wanghaoxu 已提交
2870 2871
  | 类型   | 说明             |
  | ------ | ---------------- |
Y
yangguangzhao 已提交
2872
  | number | 返回文件描述符。 |
Z
zengyawen 已提交
2873

W
wanghaoxu 已提交
2874 2875
**错误码:**

C
crazy_hu 已提交
2876
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2877

W
wanghaoxu 已提交
2878 2879 2880
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
2881

Y
yangguangzhao 已提交
2882
**示例:**
Y
yangguangzhao 已提交
2883

Y
Yippo 已提交
2884
  ```ts
Z
zengyawen 已提交
2885
  import fileio from '@ohos.fileio';
W
wanghaoxu 已提交
2886
  let sequence = new rpc.MessageSequence();
Z
zengyawen 已提交
2887 2888
  let filePath = "path/to/file";
  let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
W
wanghaoxu 已提交
2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900
  try {
      sequence.writeFileDescriptor(fd);
  } catch(error) {
      console.info("rpc write file descriptor fail, errorCode " + error.code);
      console.info("rpc write file descriptor fail, errorMessage" + error.message);
  }
  try {
      let readFD = sequence.readFileDescriptor();
  } catch(error) {
      console.info("rpc read file descriptor fail, errorCode " + error.code);
      console.info("rpc read file descriptor fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2901 2902
  ```

W
wanghaoxu 已提交
2903
### writeAshmem
Z
zengyawen 已提交
2904

W
wanghaoxu 已提交
2905
writeAshmem(ashmem: Ashmem): void
Z
zengyawen 已提交
2906

W
wanghaoxu 已提交
2907
将指定的匿名共享对象写入此MessageSequence。
Z
zengyawen 已提交
2908

Y
yangguangzhao 已提交
2909
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2910

W
wanghaoxu 已提交
2911
**参数:**
W
wangqinxiao 已提交
2912

W
wanghaoxu 已提交
2913 2914 2915
  | 参数名 | 类型   | 必填 | 说明                                  |
  | ------ | ------ | ---- | ------------------------------------- |
  | ashmem | Ashmem | 是   | 要写入MessageSequence的匿名共享对象。 |
Z
zengyawen 已提交
2916

W
wanghaoxu 已提交
2917 2918
**错误码:**

C
crazy_hu 已提交
2919
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
2920

W
wanghaoxu 已提交
2921 2922
  | 错误码ID | 错误信息 |
  | ------- | ------- |
C
crazy_hu 已提交
2923
  | 1900003 | write to ashmem failed |
Z
zengyawen 已提交
2924

Y
yangguangzhao 已提交
2925
**示例:**
Y
yangguangzhao 已提交
2926

Y
Yippo 已提交
2927
  ```ts
W
wanghaoxu 已提交
2928
  let sequence = new rpc.MessageSequence();
C
crazy_hu 已提交
2929
  let ashmem;
W
wanghaoxu 已提交
2930
  try {
C
crazy_hu 已提交
2931
      ashmem = rpc.Ashmem.create("ashmem", 1024);
W
wanghaoxu 已提交
2932 2933 2934 2935 2936 2937 2938 2939 2940 2941
  } catch(error) {
      console.info("rpc create ashmem fail, errorCode " + error.code);
      console.info("rpc creat ashmem fail, errorMessage" + error.message);
  }
  try {
      sequence.writeAshmem(ashmem);
  } catch(error) {
      console.info("rpc write ashmem fail, errorCode " + error.code);
      console.info("rpc write ashmem fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2942 2943 2944
  ```


W
wanghaoxu 已提交
2945
### readAshmem
Z
zengyawen 已提交
2946 2947 2948

readAshmem(): Ashmem

W
wanghaoxu 已提交
2949
从MessageSequence读取匿名共享对象。
Z
zengyawen 已提交
2950

Y
yangguangzhao 已提交
2951
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2952

Y
yangguangzhao 已提交
2953
**返回值:**
W
wangqinxiao 已提交
2954

W
wanghaoxu 已提交
2955 2956
  | 类型   | 说明               |
  | ------ | ------------------ |
Y
yangguangzhao 已提交
2957
  | Ashmem | 返回匿名共享对象。 |
Z
zengyawen 已提交
2958

W
wanghaoxu 已提交
2959 2960
**错误码:**

C
crazy_hu 已提交
2961
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
2962

W
wanghaoxu 已提交
2963 2964
  | 错误码ID | 错误信息 |
  | ------- | -------- |
C
crazy_hu 已提交
2965
  | 1900004 | read from ashmem failed |
W
wanghaoxu 已提交
2966

Y
yangguangzhao 已提交
2967
**示例:**
Y
yangguangzhao 已提交
2968

Y
Yippo 已提交
2969
  ```ts
W
wanghaoxu 已提交
2970
  let sequence = new rpc.MessageSequence();
C
crazy_hu 已提交
2971
  let ashmem;
W
wanghaoxu 已提交
2972
  try {
C
crazy_hu 已提交
2973
      ashmem = rpc.Ashmem.create("ashmem", 1024);
W
wanghaoxu 已提交
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
  } catch(error) {
      console.info("rpc create ashmem fail, errorCode " + error.code);
      console.info("rpc creat ashmem fail, errorMessage" + error.message);
  }
  try {
      sequence.writeAshmem(ashmem);
  } catch(error) {
      console.info("rpc write ashmem fail, errorCode " + error.code);
      console.info("rpc write ashmem fail, errorMessage" + error.message);
  }
  try {
C
crazy_hu 已提交
2985 2986
      let readAshmem = sequence.readAshmem();
      console.log("RpcTest: read ashmem to result is : " + readAshmem);
W
wanghaoxu 已提交
2987 2988 2989 2990
  } catch(error) {
      console.info("rpc read ashmem fail, errorCode " + error.code);
      console.info("rpc read ashmem fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2991 2992
  ```

W
wanghaoxu 已提交
2993
### getRawDataCapacity
Z
zengyawen 已提交
2994 2995 2996

getRawDataCapacity(): number

W
wanghaoxu 已提交
2997
获取MessageSequence可以容纳的最大原始数据量。
Z
zengyawen 已提交
2998

Y
yangguangzhao 已提交
2999
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
3000

Y
yangguangzhao 已提交
3001
**返回值:**
W
wangqinxiao 已提交
3002

W
wanghaoxu 已提交
3003 3004 3005
  | 类型   | 说明                                                         |
  | ------ | ------------------------------------------------------------ |
  | number | 返回MessageSequence可以容纳的最大原始数据量,即128&nbsp;Mb。 |
Z
zengyawen 已提交
3006

Y
yangguangzhao 已提交
3007
**示例:**
Y
yangguangzhao 已提交
3008

Y
Yippo 已提交
3009
  ```ts
W
wanghaoxu 已提交
3010 3011 3012
  let sequence = new rpc.MessageSequence();
  let result = sequence.getRawDataCapacity();
  console.log("RpcTest: sequence get RawDataCapacity result is : " + result);
Z
zengyawen 已提交
3013 3014
  ```

W
wanghaoxu 已提交
3015
### writeRawData
Z
zengyawen 已提交
3016

W
wanghaoxu 已提交
3017
writeRawData(rawData: number[], size: number): void
Z
zengyawen 已提交
3018

W
wanghaoxu 已提交
3019
将原始数据写入MessageSequence对象。
Z
zengyawen 已提交
3020

Y
yangguangzhao 已提交
3021
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
3022

W
wanghaoxu 已提交
3023
**参数:**
W
wangqinxiao 已提交
3024

W
wanghaoxu 已提交
3025 3026 3027 3028
  | 参数名  | 类型     | 必填 | 说明                               |
  | ------- | -------- | ---- | ---------------------------------- |
  | rawData | number[] | 是   | 要写入的原始数据。                 |
  | size    | number   | 是   | 发送的原始数据大小,以字节为单位。 |
Z
zengyawen 已提交
3029

W
wanghaoxu 已提交
3030 3031
**错误码:**

C
crazy_hu 已提交
3032
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
3033

W
wanghaoxu 已提交
3034 3035 3036
  | 错误码ID | 错误信息 |
  | ------- | ------ |
  | 1900009 | write data to message sequence failed |
Z
zengyawen 已提交
3037

Y
yangguangzhao 已提交
3038
**示例:**
Y
yangguangzhao 已提交
3039

Y
Yippo 已提交
3040
  ```ts
W
wanghaoxu 已提交
3041
  let sequence = new rpc.MessageSequence();
Y
yangguangzhao 已提交
3042
  let arr = [1, 2, 3, 4, 5];
W
wanghaoxu 已提交
3043 3044 3045 3046 3047 3048
  try {
      sequence.writeRawData(arr, arr.length);
  } catch(error) {
      console.info("rpc write rawdata fail, errorCode " + error.code);
      console.info("rpc write rawdata fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
3049 3050
  ```

W
wanghaoxu 已提交
3051
### readRawData
Z
zengyawen 已提交
3052 3053 3054

readRawData(size: number): number[]

W
wanghaoxu 已提交
3055
从MessageSequence读取原始数据。
Z
zengyawen 已提交
3056

Y
yangguangzhao 已提交
3057
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
3058

W
wanghaoxu 已提交
3059
**参数:**
W
wangqinxiao 已提交
3060

W
wanghaoxu 已提交
3061 3062 3063
  | 参数名 | 类型   | 必填 | 说明                     |
  | ------ | ------ | ---- | ------------------------ |
  | size   | number | 是   | 要读取的原始数据的大小。 |
Z
zengyawen 已提交
3064

Y
yangguangzhao 已提交
3065
**返回值:**
W
wangqinxiao 已提交
3066

W
wanghaoxu 已提交
3067 3068
  | 类型     | 说明                           |
  | -------- | ------------------------------ |
Y
yangguangzhao 已提交
3069
  | number[] | 返回原始数据(以字节为单位)。 |
Z
zengyawen 已提交
3070

W
wanghaoxu 已提交
3071 3072
**错误码:**

C
crazy_hu 已提交
3073
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
3074

W
wanghaoxu 已提交
3075 3076 3077
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900010 | read data from message sequence failed |
W
wanghaoxu 已提交
3078

Y
yangguangzhao 已提交
3079
**示例:**
Y
yangguangzhao 已提交
3080

Y
Yippo 已提交
3081
  ```ts
W
wanghaoxu 已提交
3082
  let sequence = new rpc.MessageSequence();
Y
yangguangzhao 已提交
3083
  let arr = [1, 2, 3, 4, 5];
W
wanghaoxu 已提交
3084 3085 3086 3087 3088 3089 3090 3091
  try {
      sequence.writeRawData(arr, arr.length);
  } catch(error) {
      console.info("rpc write rawdata fail, errorCode " + error.code);
      console.info("rpc write rawdata fail, errorMessage" + error.message);
  }
  try {
      let result = sequence.readRawData(5);
C
crazy_hu 已提交
3092
      console.log("RpcTest: sequence read raw data result is : " + result);
W
wanghaoxu 已提交
3093 3094 3095 3096
  } catch(error) {
      console.info("rpc read rawdata fail, errorCode " + error.code);
      console.info("rpc read rawdata fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
3097 3098
  ```

3099
## MessageParcel<sup>(deprecated)</sup>
Z
zengyawen 已提交
3100

3101 3102 3103
>从API version 9 开始不再维护,建议使用[MessageSequence](#messagesequence9)类替代。

在RPC过程中,发送方可以使用MessageParcel提供的写方法,将待发送的数据以特定格式写入该对象。接收方可以使用MessageParcel提供的读方法从该对象中读取特定格式的数据。数据格式包括:基础类型及数组、IPC对象、接口描述符和自定义序列化对象。
Z
zengyawen 已提交
3104

W
wanghaoxu 已提交
3105
### create
Z
zengyawen 已提交
3106

3107 3108
static create(): MessageParcel

W
wanghaoxu 已提交
3109
静态方法,创建MessageParcel对象。
Z
zengyawen 已提交
3110

Y
yangguangzhao 已提交
3111 3112
**系统能力**:SystemCapability.Communication.IPC.Core

Y
yangguangzhao 已提交
3113
**返回值:**
W
wangqinxiao 已提交
3114

W
wanghaoxu 已提交
3115 3116 3117
  | 类型          | 说明                          |
  | ------------- | ----------------------------- |
  | MessageParcel | 返回创建的MessageParcel对象。 |
Z
zengyawen 已提交
3118

Y
yangguangzhao 已提交
3119
**示例:**
Y
yangguangzhao 已提交
3120

Y
Yippo 已提交
3121
  ```ts
Z
zengyawen 已提交
3122
  let data = rpc.MessageParcel.create();
W
wanghaoxu 已提交
3123
  console.log("RpcClient: data is " + data);
Z
zengyawen 已提交
3124 3125
  ```

W
wanghaoxu 已提交
3126
### reclaim
Z
zengyawen 已提交
3127

W
wanghaoxu 已提交
3128
reclaim(): void
Z
zengyawen 已提交
3129

W
wanghaoxu 已提交
3130
释放不再使用的MessageParcel对象。
Z
zengyawen 已提交
3131

W
wanghaoxu 已提交
3132 3133 3134 3135
**系统能力**:SystemCapability.Communication.IPC.Core

**示例:**

Y
Yippo 已提交
3136
  ```ts
W
wanghaoxu 已提交
3137 3138 3139 3140 3141 3142 3143 3144 3145
  let reply = rpc.MessageParcel.create();
  reply.reclaim();
  ```

### writeRemoteObject

writeRemoteObject(object: [IRemoteObject](#iremoteobject)): boolean

序列化远程对象并将其写入MessageParcel对象。
Z
zengyawen 已提交
3146

Y
yangguangzhao 已提交
3147 3148
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3149
**参数:**
W
wangqinxiao 已提交
3150

W
wanghaoxu 已提交
3151 3152 3153
  | 参数名 | 类型                            | 必填 | 说明                                    |
  | ------ | ------------------------------- | ---- | --------------------------------------- |
  | object | [IRemoteObject](#iremoteobject) | 是   | 要序列化并写入MessageParcel的远程对象。 |
Z
zengyawen 已提交
3154

Y
yangguangzhao 已提交
3155
**返回值:**
W
wangqinxiao 已提交
3156

W
wanghaoxu 已提交
3157 3158
  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
3159
  | boolean | true:操作成功,false:操作失败。|
Z
zengyawen 已提交
3160

Y
yangguangzhao 已提交
3161
**示例:**
Y
yangguangzhao 已提交
3162

Y
Yippo 已提交
3163
  ```ts
W
wanghaoxu 已提交
3164 3165 3166
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
Z
zengyawen 已提交
3167
      }
W
wanghaoxu 已提交
3168 3169 3170 3171 3172 3173
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
Z
zengyawen 已提交
3174 3175
          return true;
      }
W
wanghaoxu 已提交
3176
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
Z
zengyawen 已提交
3177 3178
          return true;
      }
W
wanghaoxu 已提交
3179 3180 3181
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
3182 3183
  }
  let data = rpc.MessageParcel.create();
W
wanghaoxu 已提交
3184 3185
  let testRemoteObject = new TestRemoteObject("testObject");
  data.writeRemoteObject(testRemoteObject);
Z
zengyawen 已提交
3186 3187
  ```

W
wanghaoxu 已提交
3188
### readRemoteObject
Z
zengyawen 已提交
3189

W
wanghaoxu 已提交
3190
readRemoteObject(): IRemoteObject
Z
zengyawen 已提交
3191

W
wanghaoxu 已提交
3192
从MessageParcel读取远程对象。此方法用于反序列化MessageParcel对象以生成IRemoteObject。远程对象按写入MessageParcel的顺序读取。
Z
zengyawen 已提交
3193

Y
yangguangzhao 已提交
3194 3195
**系统能力**:SystemCapability.Communication.IPC.Core

Y
yangguangzhao 已提交
3196
**返回值:**
W
wangqinxiao 已提交
3197

W
wanghaoxu 已提交
3198 3199 3200
  | 类型                            | 说明               |
  | ------------------------------- | ------------------ |
  | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 |
Z
zengyawen 已提交
3201

Y
yangguangzhao 已提交
3202
**示例:**
Y
yangguangzhao 已提交
3203

Y
Yippo 已提交
3204
  ```ts
W
wanghaoxu 已提交
3205 3206 3207
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
Z
zengyawen 已提交
3208 3209
      }
  }
W
wanghaoxu 已提交
3210 3211 3212
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
Z
zengyawen 已提交
3213
      }
W
wanghaoxu 已提交
3214 3215 3216 3217 3218 3219 3220 3221
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
Z
zengyawen 已提交
3222 3223
      }
  }
W
wanghaoxu 已提交
3224 3225 3226 3227
  let data = rpc.MessageParcel.create();
  let testRemoteObject = new TestRemoteObject("testObject");
  data.writeRemoteObject(testRemoteObject);
  let proxy = data.readRemoteObject();
Z
zengyawen 已提交
3228 3229
  ```

W
wanghaoxu 已提交
3230
### writeInterfaceToken
Z
zengyawen 已提交
3231

W
wanghaoxu 已提交
3232
writeInterfaceToken(token: string): boolean
Z
zengyawen 已提交
3233

W
wanghaoxu 已提交
3234
将接口描述符写入MessageParcel对象,远端对象可使用该信息校验本次通信。
Z
zengyawen 已提交
3235

W
wanghaoxu 已提交
3236
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
3237

W
wanghaoxu 已提交
3238
**参数:**
Z
zengyawen 已提交
3239

W
wanghaoxu 已提交
3240 3241 3242
  | 参数名 | 类型   | 必填 | 说明               |
  | ------ | ------ | ---- | ------------------ |
  | token  | string | 是   | 字符串类型描述符。 |
Z
zengyawen 已提交
3243

W
wanghaoxu 已提交
3244
**返回值:**
Z
zengyawen 已提交
3245

W
wanghaoxu 已提交
3246 3247
  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
3248
  | boolean | true:操作成功,false:操作失败。|
Y
yangguangzhao 已提交
3249

Y
yangguangzhao 已提交
3250
**示例:**
Y
yangguangzhao 已提交
3251

Y
Yippo 已提交
3252
  ```ts
W
wanghaoxu 已提交
3253 3254 3255
  let data = rpc.MessageParcel.create();
  let result = data.writeInterfaceToken("aaa");
  console.log("RpcServer: writeInterfaceToken is " + result);
Z
zengyawen 已提交
3256 3257 3258
  ```


W
wanghaoxu 已提交
3259
### readInterfaceToken
Z
zengyawen 已提交
3260

W
wanghaoxu 已提交
3261
readInterfaceToken(): string
Y
yangguangzhao 已提交
3262

W
wanghaoxu 已提交
3263
从MessageParcel中读取接口描述符,接口描述符按写入MessageParcel的顺序读取,本地对象可使用该信息检验本次通信。
Z
zengyawen 已提交
3264

W
wanghaoxu 已提交
3265
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
3266

W
wanghaoxu 已提交
3267
**返回值:**
Z
zengyawen 已提交
3268

W
wanghaoxu 已提交
3269 3270 3271
  | 类型   | 说明                     |
  | ------ | ------------------------ |
  | string | 返回读取到的接口描述符。 |
Z
zengyawen 已提交
3272

W
wanghaoxu 已提交
3273
**示例:**
Z
zengyawen 已提交
3274

Y
Yippo 已提交
3275
  ```ts
W
wanghaoxu 已提交
3276
  class Stub extends rpc.RemoteObject {
3277
      onRemoteMessageRequest(code, data, reply, option) {
W
wanghaoxu 已提交
3278 3279 3280 3281 3282 3283
          let interfaceToken = data.readInterfaceToken();
          console.log("RpcServer: interfaceToken is " + interfaceToken);
          return true;
      }
  }
  ```
Z
zengyawen 已提交
3284

W
wanghaoxu 已提交
3285
### getSize
Z
zengyawen 已提交
3286

W
wanghaoxu 已提交
3287
getSize(): number
Y
yangguangzhao 已提交
3288

W
wanghaoxu 已提交
3289
获取当前MessageParcel的数据大小。
W
wangqinxiao 已提交
3290

W
wanghaoxu 已提交
3291
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
3292

Y
yangguangzhao 已提交
3293
**返回值:**
W
wangqinxiao 已提交
3294

W
wanghaoxu 已提交
3295 3296 3297
  | 类型   | 说明                                          |
  | ------ | --------------------------------------------- |
  | number | 获取的MessageParcel的数据大小。以字节为单位。 |
Z
zengyawen 已提交
3298

W
wanghaoxu 已提交
3299
**示例:**
Z
zengyawen 已提交
3300

Y
Yippo 已提交
3301
  ```ts
W
wanghaoxu 已提交
3302 3303 3304 3305
  let data = rpc.MessageParcel.create();
  let size = data.getSize();
  console.log("RpcClient: size is " + size);
  ```
W
wangqinxiao 已提交
3306

W
wanghaoxu 已提交
3307
### getCapacity
Z
zengyawen 已提交
3308

W
wanghaoxu 已提交
3309
getCapacity(): number
Y
yangguangzhao 已提交
3310

W
wanghaoxu 已提交
3311
获取当前MessageParcel的容量。
W
wangqinxiao 已提交
3312

W
wanghaoxu 已提交
3313
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
3314

Y
yangguangzhao 已提交
3315
**返回值:**
W
wangqinxiao 已提交
3316

W
wanghaoxu 已提交
3317 3318 3319
  | 类型   | 说明                                          |
  | ------ | --------------------------------------------- |
  | number | 获取的MessageParcel的容量大小。以字节为单位。 |
Z
zengyawen 已提交
3320

W
wanghaoxu 已提交
3321
**示例:**
Z
zengyawen 已提交
3322

Y
Yippo 已提交
3323
  ```ts
W
wanghaoxu 已提交
3324 3325 3326 3327
  let data = rpc.MessageParcel.create();
  let result = data.getCapacity();
  console.log("RpcClient: capacity is " + result);
  ```
3328

W
wanghaoxu 已提交
3329
### setSize
Z
zengyawen 已提交
3330

W
wanghaoxu 已提交
3331 3332 3333
setSize(size: number): boolean

设置MessageParcel实例中包含的数据大小。
Y
yangguangzhao 已提交
3334 3335 3336

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3337
**参数:**
W
wangqinxiao 已提交
3338

W
wanghaoxu 已提交
3339 3340 3341
  | 参数名 | 类型   | 必填 | 说明                                        |
  | ------ | ------ | ---- | ------------------------------------------- |
  | size   | number | 是   | MessageParcel实例的数据大小。以字节为单位。 |
3342 3343

**返回值:**
W
wangqinxiao 已提交
3344

W
wanghaoxu 已提交
3345 3346
  | 类型    | 说明                              |
  | ------- | --------------------------------- |
3347
  | boolean | true:设置成功,false:设置失败。|
3348

W
wanghaoxu 已提交
3349
**示例:**
3350

Y
Yippo 已提交
3351
  ```ts
W
wanghaoxu 已提交
3352 3353 3354 3355
  let data = rpc.MessageParcel.create();
  let setSize = data.setSize(16);
  console.log("RpcClient: setSize is " + setSize);
  ```
3356

W
wanghaoxu 已提交
3357
### setCapacity
W
wangqinxiao 已提交
3358

W
wanghaoxu 已提交
3359
setCapacity(size: number): boolean
Y
yangguangzhao 已提交
3360

W
wanghaoxu 已提交
3361
设置MessageParcel实例的存储容量。
W
wangqinxiao 已提交
3362

W
wanghaoxu 已提交
3363
**系统能力**:SystemCapability.Communication.IPC.Core
Y
yangguangzhao 已提交
3364

W
wanghaoxu 已提交
3365
**参数:**
Y
yangguangzhao 已提交
3366

W
wanghaoxu 已提交
3367 3368 3369
  | 参数名 | 类型   | 必填 | 说明                                        |
  | ------ | ------ | ---- | ------------------------------------------- |
  | size   | number | 是   | MessageParcel实例的存储容量。以字节为单位。 |
Z
zengyawen 已提交
3370

W
wanghaoxu 已提交
3371
**返回值:**
Z
zengyawen 已提交
3372

W
wanghaoxu 已提交
3373 3374
  | 类型    | 说明                              |
  | ------- | --------------------------------- |
3375
  | boolean | true:设置成功,false:设置失败。|
Y
yangguangzhao 已提交
3376

W
wanghaoxu 已提交
3377
**示例:**
W
wangqinxiao 已提交
3378

Y
Yippo 已提交
3379
  ```ts
W
wanghaoxu 已提交
3380 3381 3382 3383
  let data = rpc.MessageParcel.create();
  let result = data.setCapacity(100);
  console.log("RpcClient: setCapacity is " + result);
  ```
Z
zengyawen 已提交
3384

W
wanghaoxu 已提交
3385
### getWritableBytes
Z
zengyawen 已提交
3386

W
wanghaoxu 已提交
3387
getWritableBytes(): number
Z
zengyawen 已提交
3388

W
wanghaoxu 已提交
3389
获取MessageParcel的可写字节空间。
Z
zengyawen 已提交
3390

Y
yangguangzhao 已提交
3391 3392
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3393
**返回值:**
W
wangqinxiao 已提交
3394

W
wanghaoxu 已提交
3395 3396 3397
  | 类型   | 说明                                                |
  | ------ | --------------------------------------------------- |
  | number | 获取到的MessageParcel的可写字节空间。以字节为单位。 |
Z
zengyawen 已提交
3398

W
wanghaoxu 已提交
3399
**示例:**
W
wangqinxiao 已提交
3400

Y
Yippo 已提交
3401
  ```ts
W
wanghaoxu 已提交
3402
  class Stub extends rpc.RemoteObject {
3403
      onRemoteMessageRequest(code, data, reply, option) {
W
wanghaoxu 已提交
3404 3405 3406 3407 3408 3409
          let getWritableBytes = data.getWritableBytes();
          console.log("RpcServer: getWritableBytes is " + getWritableBytes);
          return true;
      }
  }
  ```
Z
zengyawen 已提交
3410

W
wanghaoxu 已提交
3411
### getReadableBytes
Z
zengyawen 已提交
3412

W
wanghaoxu 已提交
3413
getReadableBytes(): number
Z
zengyawen 已提交
3414

W
wanghaoxu 已提交
3415
获取MessageParcel的可读字节空间。
Z
zengyawen 已提交
3416

Y
yangguangzhao 已提交
3417 3418
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3419
**返回值:**
W
wangqinxiao 已提交
3420

W
wanghaoxu 已提交
3421 3422 3423
  | 类型   | 说明                                                |
  | ------ | --------------------------------------------------- |
  | number | 获取到的MessageParcel的可读字节空间。以字节为单位。 |
Z
zengyawen 已提交
3424

W
wanghaoxu 已提交
3425
**示例:**
W
wangqinxiao 已提交
3426

Y
Yippo 已提交
3427
  ```ts
W
wanghaoxu 已提交
3428 3429 3430 3431 3432 3433 3434 3435
  class Stub extends rpc.RemoteObject {
      onRemoteRequest(code, data, reply, option) {
          let result = data.getReadableBytes();
          console.log("RpcServer: getReadableBytes is " + result);
          return true;
      }
  }
  ```
Z
zengyawen 已提交
3436

W
wanghaoxu 已提交
3437
### getReadPosition
Z
zengyawen 已提交
3438

W
wanghaoxu 已提交
3439
getReadPosition(): number
Z
zengyawen 已提交
3440

W
wanghaoxu 已提交
3441
获取MessageParcel的读位置。
Z
zengyawen 已提交
3442

Y
yangguangzhao 已提交
3443 3444
**系统能力**:SystemCapability.Communication.IPC.Core

Y
yangguangzhao 已提交
3445
**返回值:**
W
wangqinxiao 已提交
3446

W
wanghaoxu 已提交
3447 3448 3449
  | 类型   | 说明                                    |
  | ------ | --------------------------------------- |
  | number | 返回MessageParcel实例中的当前读取位置。 |
Z
zengyawen 已提交
3450

W
wanghaoxu 已提交
3451
**示例:**
Z
zengyawen 已提交
3452

Y
Yippo 已提交
3453
  ```ts
W
wanghaoxu 已提交
3454 3455 3456 3457
  let data = rpc.MessageParcel.create();
  let readPos = data.getReadPosition();
  console.log("RpcClient: readPos is " + readPos);
  ```
Z
zengyawen 已提交
3458

W
wanghaoxu 已提交
3459 3460 3461 3462 3463
### getWritePosition

getWritePosition(): number

获取MessageParcel的写位置。
Z
zengyawen 已提交
3464

Y
yangguangzhao 已提交
3465 3466
**系统能力**:SystemCapability.Communication.IPC.Core

Y
yangguangzhao 已提交
3467
**返回值:**
W
wangqinxiao 已提交
3468

W
wanghaoxu 已提交
3469 3470 3471
  | 类型   | 说明                                    |
  | ------ | --------------------------------------- |
  | number | 返回MessageParcel实例中的当前写入位置。 |
Z
zengyawen 已提交
3472

W
wanghaoxu 已提交
3473
**示例:**
Z
zengyawen 已提交
3474

Y
Yippo 已提交
3475
  ```ts
W
wanghaoxu 已提交
3476 3477 3478 3479 3480
  let data = rpc.MessageParcel.create();
  data.writeInt(10);
  let bwPos = data.getWritePosition();
  console.log("RpcClient: bwPos is " + bwPos);
  ```
Z
zengyawen 已提交
3481

W
wanghaoxu 已提交
3482
### rewindRead
Y
yangguangzhao 已提交
3483

W
wanghaoxu 已提交
3484
rewindRead(pos: number): boolean
Y
yangguangzhao 已提交
3485

W
wanghaoxu 已提交
3486
重新偏移读取位置到指定的位置。
Y
yangguangzhao 已提交
3487

W
wanghaoxu 已提交
3488
**系统能力**:SystemCapability.Communication.IPC.Core
Y
yangguangzhao 已提交
3489

W
wanghaoxu 已提交
3490
**参数:**
Z
zengyawen 已提交
3491

W
wanghaoxu 已提交
3492 3493 3494
  | 参数名 | 类型   | 必填 | 说明                     |
  | ------ | ------ | ---- | ------------------------ |
  | pos    | number | 是   | 开始读取数据的目标位置。 |
Z
zengyawen 已提交
3495

W
wanghaoxu 已提交
3496
**返回值:**
W
wangqinxiao 已提交
3497

W
wanghaoxu 已提交
3498 3499
  | 类型    | 说明                                              |
  | ------- | ------------------------------------------------- |
3500
  | boolean | true:读取位置发生更改,false:读取位置未发生更改。|
Z
zengyawen 已提交
3501

W
wanghaoxu 已提交
3502 3503
**示例:**

Y
Yippo 已提交
3504
  ```ts
W
wanghaoxu 已提交
3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522
  let data = rpc.MessageParcel.create();
  data.writeInt(12);
  data.writeString("parcel");
  let number = data.readInt();
  console.log("RpcClient: number is " + number);
  data.rewindRead(0);
  let number2 = data.readInt();
  console.log("RpcClient: rewindRead is " + number2);
  ```

### rewindWrite

rewindWrite(pos: number): boolean

重新偏移写位置到指定的位置。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3523
**参数:**
W
wanghaoxu 已提交
3524 3525 3526 3527 3528 3529 3530 3531 3532

  | 参数名 | 类型   | 必填 | 说明                     |
  | ------ | ------ | ---- | ------------------------ |
  | pos    | number | 是   | 开始写入数据的目标位置。 |

**返回值:**

  | 类型    | 说明                                          |
  | ------- | --------------------------------------------- |
3533
  | boolean | true:写入位置发生更改,false:写入位置未发生更改。|
W
wanghaoxu 已提交
3534 3535 3536

**示例:**

Y
Yippo 已提交
3537
  ```ts
W
wanghaoxu 已提交
3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553
  let data = rpc.MessageParcel.create();
  data.writeInt(4);
  data.rewindWrite(0);
  data.writeInt(5);
  let number = data.readInt();
  console.log("RpcClient: rewindWrite is: " + number);
  ```

### writeByte

writeByte(val: number): boolean

将字节值写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3554
**参数:**
W
wanghaoxu 已提交
3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567

  | 参数名 | 类型   | 必填 | 说明             |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | 是   | 要写入的字节值。 |

**返回值:**

  | 类型    | 说明                          |
  | ------- | ----------------------------- |
  | boolean | 写入返回成功,否则返回false。 |

**示例:**

Y
Yippo 已提交
3568
  ```ts
W
wanghaoxu 已提交
3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589
  let data = rpc.MessageParcel.create();
  let result = data.writeByte(2);
  console.log("RpcClient: writeByte is: " + result);
  ```

### readByte

readByte(): number

从MessageParcel实例读取字节值。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明         |
  | ------ | ------------ |
  | number | 返回字节值。 |

**示例:**

Y
Yippo 已提交
3590
  ```ts
W
wanghaoxu 已提交
3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605
  let data = rpc.MessageParcel.create();
  let result = data.writeByte(2);
  console.log("RpcClient: writeByte is: " + result);
  let ret = data.readByte();
  console.log("RpcClient: readByte is: " + ret);
  ```

### writeShort

writeShort(val: number): boolean

将短整数值写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3606
**参数:**
W
wanghaoxu 已提交
3607 3608 3609 3610 3611 3612 3613 3614 3615

  | 参数名 | 类型   | 必填 | 说明               |
  | ------ | ------ | ---- | ------------------ |
  | val    | number | 是   | 要写入的短整数值。 |

**返回值:**

  | 类型    | 说明                          |
  | ------- | ----------------------------- |
3616
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
3617 3618 3619

**示例:**

Y
Yippo 已提交
3620
  ```ts
W
wanghaoxu 已提交
3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641
  let data = rpc.MessageParcel.create();
  let result = data.writeShort(8);
  console.log("RpcClient: writeShort is: " + result);
  ```

### readShort

readShort(): number

从MessageParcel实例读取短整数值。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明           |
  | ------ | -------------- |
  | number | 返回短整数值。 |

**示例:**

Y
Yippo 已提交
3642
  ```ts
W
wanghaoxu 已提交
3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657
  let data = rpc.MessageParcel.create();
  let result = data.writeShort(8);
  console.log("RpcClient: writeShort is: " + result);
  let ret = data.readShort();
  console.log("RpcClient: readShort is: " + ret);
  ```

### writeInt

writeInt(val: number): boolean

将整数值写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3658
**参数:**
W
wanghaoxu 已提交
3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671

  | 参数名 | 类型   | 必填 | 说明             |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | 是   | 要写入的整数值。 |

**返回值:**

  | 类型    | 说明                          |
  | ------- | ----------------------------- |
  | boolean | 写入返回成功,否则返回false。 |

**示例:**

Y
Yippo 已提交
3672
  ```ts
W
wanghaoxu 已提交
3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693
  let data = rpc.MessageParcel.create();
  let result = data.writeInt(10);
  console.log("RpcClient: writeInt is " + result);
  ```

### readInt

readInt(): number

从MessageParcel实例读取整数值。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明         |
  | ------ | ------------ |
  | number | 返回整数值。 |

**示例:**

Y
Yippo 已提交
3694
  ```ts
W
wanghaoxu 已提交
3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709
  let data = rpc.MessageParcel.create();
  let result = data.writeInt(10);
  console.log("RpcClient: writeInt is " + result);
  let ret = data.readInt();
  console.log("RpcClient: readInt is " + ret);
  ```

### writeLong

writeLong(val: number): boolean

将长整数值写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3710
**参数:**
W
wanghaoxu 已提交
3711 3712 3713 3714 3715 3716 3717 3718 3719

  | 参数名 | 类型   | 必填 | 说明             |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | 是   | 要写入的长整数值 |

**返回值:**

  | 类型    | 说明                              |
  | ------- | --------------------------------- |
3720
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
3721 3722 3723

**示例:**

Y
Yippo 已提交
3724
  ```ts
W
wanghaoxu 已提交
3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745
  let data = rpc.MessageParcel.create();
  let result = data.writeLong(10000);
  console.log("RpcClient: writeLong is " + result);
  ```

### readLong

readLong(): number

从MessageParcel实例中读取长整数值。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明           |
  | ------ | -------------- |
  | number | 返回长整数值。 |

**示例:**

Y
Yippo 已提交
3746
  ```ts
W
wanghaoxu 已提交
3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761
  let data = rpc.MessageParcel.create();
  let result = data.writeLong(10000);
  console.log("RpcClient: writeLong is " + result);
  let ret = data.readLong();
  console.log("RpcClient: readLong is " + ret);
  ```

### writeFloat

writeFloat(val: number): boolean

将浮点值写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3762
**参数:**
W
wanghaoxu 已提交
3763 3764 3765 3766 3767 3768 3769 3770 3771

  | 参数名 | 类型   | 必填 | 说明             |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | 是   | 要写入的浮点值。 |

**返回值:**

  | 类型    | 说明                              |
  | ------- | --------------------------------- |
3772
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
3773 3774 3775

**示例:**

Y
Yippo 已提交
3776
  ```ts
W
wanghaoxu 已提交
3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797
  let data = rpc.MessageParcel.create();
  let result = data.writeFloat(1.2);
  console.log("RpcClient: writeFloat is " + result);
  ```

### readFloat

readFloat(): number

从MessageParcel实例中读取浮点值。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明         |
  | ------ | ------------ |
  | number | 返回浮点值。 |

**示例:**

Y
Yippo 已提交
3798
  ```ts
W
wanghaoxu 已提交
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813
  let data = rpc.MessageParcel.create();
  let result = data.writeFloat(1.2);
  console.log("RpcClient: writeFloat is " + result);
  let ret = data.readFloat();
  console.log("RpcClient: readFloat is " + ret);
  ```

### writeDouble

writeDouble(val: number): boolean

将双精度浮点值写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3814
**参数:**
W
wanghaoxu 已提交
3815 3816 3817 3818 3819 3820 3821 3822 3823

  | 参数名 | 类型   | 必填 | 说明                   |
  | ------ | ------ | ---- | ---------------------- |
  | val    | number | 是   | 要写入的双精度浮点值。 |

**返回值:**

  | 类型    | 说明                              |
  | ------- | --------------------------------- |
3824
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
3825 3826 3827

**示例:**

Y
Yippo 已提交
3828
  ```ts
W
wanghaoxu 已提交
3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849
  let data = rpc.MessageParcel.create();
  let result = data.writeDouble(10.2);
  console.log("RpcClient: writeDouble is " + result);
  ```

### readDouble

readDouble(): number

从MessageParcel实例读取双精度浮点值。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明               |
  | ------ | ------------------ |
  | number | 返回双精度浮点值。 |

**示例:**

Y
Yippo 已提交
3850
  ```ts
W
wanghaoxu 已提交
3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865
  let data = rpc.MessageParcel.create();
  let result = data.writeDouble(10.2);
  console.log("RpcClient: writeDouble is " + result);
  let ret = data.readDouble();
  console.log("RpcClient: readDouble is " + ret);
  ```

### writeBoolean

writeBoolean(val: boolean): boolean

将布尔值写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3866
**参数:**
W
wanghaoxu 已提交
3867 3868 3869 3870 3871 3872 3873 3874 3875

  | 参数名 | 类型    | 必填 | 说明             |
  | ------ | ------- | ---- | ---------------- |
  | val    | boolean | 是   | 要写入的布尔值。 |

**返回值:**

  | 类型    | 说明                              |
  | ------- | --------------------------------- |
3876
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
3877 3878 3879

**示例:**

Y
Yippo 已提交
3880
  ```ts
W
wanghaoxu 已提交
3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901
  let data = rpc.MessageParcel.create();
  let result = data.writeBoolean(false);
  console.log("RpcClient: writeBoolean is " + result);
  ```

### readBoolean

readBoolean(): boolean

从MessageParcel实例读取布尔值。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型    | 说明                 |
  | ------- | -------------------- |
  | boolean | 返回读取到的布尔值。 |

**示例:**

Y
Yippo 已提交
3902
  ```ts
W
wanghaoxu 已提交
3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917
  let data = rpc.MessageParcel.create();
  let result = data.writeBoolean(false);
  console.log("RpcClient: writeBoolean is " + result);
  let ret = data.readBoolean();
  console.log("RpcClient: readBoolean is " + ret);
  ```

### writeChar

writeChar(val: number): boolean

将单个字符值写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
3918
**参数:**
W
wanghaoxu 已提交
3919 3920 3921 3922 3923 3924 3925 3926 3927

  | 参数名 | 类型   | 必填 | 说明                 |
  | ------ | ------ | ---- | -------------------- |
  | val    | number | 是   | 要写入的单个字符值。 |

**返回值:**

  | 类型    | 说明                          |
  | ------- | ----------------------------- |
3928
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
3929 3930 3931

**示例:**

Y
Yippo 已提交
3932
  ```ts
W
wanghaoxu 已提交
3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953
  let data = rpc.MessageParcel.create();
  let result = data.writeChar(97);
  console.log("RpcClient: writeChar is " + result);
  ```

### readChar

readChar(): number

从MessageParcel实例中读取单个字符值。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明             |
  | ------ | ---------------- |
  | number | 返回单个字符值。 |

**示例:**

Y
Yippo 已提交
3954
  ```ts
W
wanghaoxu 已提交
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968
  let data = rpc.MessageParcel.create();
  let result = data.writeChar(97);
  console.log("RpcClient: writeChar is " + result);
  let ret = data.readChar();
  console.log("RpcClient: readChar is " + ret);
  ```

### writeString

writeString(val: string): boolean

将字符串值写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core
3969

W
wanghaoxu 已提交
3970
**参数:**
3971

W
wanghaoxu 已提交
3972 3973 3974 3975 3976 3977 3978 3979
  | 参数名 | 类型   | 必填 | 说明                                      |
  | ------ | ------ | ---- | ----------------------------------------- |
  | val    | string | 是   | 要写入的字符串值,其长度应小于40960字节。 |

**返回值:**

  | 类型    | 说明                              |
  | ------- | --------------------------------- |
3980
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
3981 3982 3983

**示例:**

Y
Yippo 已提交
3984
  ```ts
W
wanghaoxu 已提交
3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005
  let data = rpc.MessageParcel.create();
  let result = data.writeString('abc');
  console.log("RpcClient: writeString  is " + result);
  ```

### readString

readString(): string

从MessageParcel实例读取字符串值。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明           |
  | ------ | -------------- |
  | string | 返回字符串值。 |

**示例:**

Y
Yippo 已提交
4006
  ```ts
W
wanghaoxu 已提交
4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021
  let data = rpc.MessageParcel.create();
  let result = data.writeString('abc');
  console.log("RpcClient: writeString  is " + result);
  let ret = data.readString();
  console.log("RpcClient: readString is " + ret);
  ```

### writeSequenceable

writeSequenceable(val: Sequenceable): boolean

将自定义序列化对象写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4022
**参数:**
W
wanghaoxu 已提交
4023 4024 4025 4026 4027 4028 4029 4030 4031

  | 参数名 | 类型                          | 必填 | 说明                 |
  | ------ | ----------------------------- | ---- | -------------------- |
  | val    | [Sequenceable](#sequenceable) | 是   | 要写入的可序列对象。 |

**返回值:**

  | 类型    | 说明                              |
  | ------- | --------------------------------- |
4032
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
4033 4034 4035

**示例:**

Y
Yippo 已提交
4036
  ```ts
W
wanghaoxu 已提交
4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062
  class MySequenceable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  let sequenceable = new MySequenceable(1, "aaa");
  let data = rpc.MessageParcel.create();
  let result = data.writeSequenceable(sequenceable);
  console.log("RpcClient: writeSequenceable is " + result);
  ```

### readSequenceable

C
crazy_hu 已提交
4063
readSequenceable(dataIn: Sequenceable): boolean
W
wanghaoxu 已提交
4064 4065 4066 4067 4068

从MessageParcel实例中读取成员变量到指定的对象(dataIn)。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4069
**参数:**
W
wanghaoxu 已提交
4070 4071 4072

  | 参数名 | 类型                          | 必填 | 说明                                    |
  | ------ | ----------------------------- | ---- | --------------------------------------- |
4073
  | dataIn | [Sequenceable](#sequenceabledeprecated) | 是   | 需要从MessageParcel读取成员变量的对象。 |
W
wanghaoxu 已提交
4074 4075 4076 4077 4078

**返回值:**

  | 类型    | 说明                                        |
  | ------- | ------------------------------------------- |
4079
  | boolean | true:反序列化成功,false:反序列化失败。|
W
wanghaoxu 已提交
4080 4081 4082

**示例:**

Y
Yippo 已提交
4083
  ```ts
W
wanghaoxu 已提交
4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118
  class MySequenceable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  let sequenceable = new MySequenceable(1, "aaa");
  let data = rpc.MessageParcel.create();
  let result = data.writeSequenceable(sequenceable);
  console.log("RpcClient: writeSequenceable is " + result);
  let ret = new MySequenceable(0, "");
  let result2 = data.readSequenceable(ret);
  console.log("RpcClient: writeSequenceable is " + result2);
  ```

### writeByteArray

writeByteArray(byteArray: number[]): boolean

将字节数组写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4119
**参数:**
W
wanghaoxu 已提交
4120 4121 4122 4123 4124 4125 4126 4127 4128

  | 参数名    | 类型     | 必填 | 说明               |
  | --------- | -------- | ---- | ------------------ |
  | byteArray | number[] | 是   | 要写入的字节数组。 |

**返回值:**

  | 类型    | 说明                              |
  | ------- | --------------------------------- |
4129
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
4130 4131 4132

**示例:**

Y
Yippo 已提交
4133
  ```ts
W
wanghaoxu 已提交
4134 4135 4136 4137 4138 4139 4140 4141
  let data = rpc.MessageParcel.create();
  let ByteArrayVar = [1, 2, 3, 4, 5];
  let result = data.writeByteArray(ByteArrayVar);
  console.log("RpcClient: writeByteArray is " + result);
  ```

### readByteArray

C
crazy_hu 已提交
4142
readByteArray(dataIn: number[]): void
W
wanghaoxu 已提交
4143 4144 4145 4146 4147

从MessageParcel实例读取字节数组。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4148
**参数:**
W
wanghaoxu 已提交
4149 4150 4151 4152 4153 4154 4155

  | 参数名 | 类型     | 必填 | 说明               |
  | ------ | -------- | ---- | ------------------ |
  | dataIn | number[] | 是   | 要读取的字节数组。 |

**示例:**

Y
Yippo 已提交
4156
  ```ts
W
wanghaoxu 已提交
4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180
  let data = rpc.MessageParcel.create();
  let ByteArrayVar = [1, 2, 3, 4, 5];
  let result = data.writeByteArray(ByteArrayVar);
  console.log("RpcClient: writeByteArray is " + result);
  let array = new Array(5);
  data.readByteArray(array);
  ```

### readByteArray

readByteArray(): number[]

从MessageParcel实例中读取字节数组。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型     | 说明           |
  | -------- | -------------- |
  | number[] | 返回字节数组。 |

**示例:**

Y
Yippo 已提交
4181
  ```ts
W
wanghaoxu 已提交
4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197
  let data = rpc.MessageParcel.create();
  let ByteArrayVar = [1, 2, 3, 4, 5];
  let result = data.writeByteArray(ByteArrayVar);
  console.log("RpcClient: writeByteArray is " + result);
  let array = data.readByteArray();
  console.log("RpcClient: readByteArray is " + array);
  ```

### writeShortArray

writeShortArray(shortArray: number[]): boolean

将短整数数组写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4198
**参数:**
W
wanghaoxu 已提交
4199 4200 4201 4202 4203 4204 4205 4206 4207

  | 参数名     | 类型     | 必填 | 说明                 |
  | ---------- | -------- | ---- | -------------------- |
  | shortArray | number[] | 是   | 要写入的短整数数组。 |

**返回值:**

  | 类型    | 说明                          |
  | ------- | ----------------------------- |
4208
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
4209 4210 4211

**示例:**

Y
Yippo 已提交
4212
  ```ts
W
wanghaoxu 已提交
4213 4214 4215 4216 4217 4218 4219
  let data = rpc.MessageParcel.create();
  let result = data.writeShortArray([11, 12, 13]);
  console.log("RpcClient: writeShortArray is " + result);
  ```

### readShortArray

C
crazy_hu 已提交
4220
readShortArray(dataIn: number[]): void
W
wanghaoxu 已提交
4221 4222 4223 4224 4225

从MessageParcel实例中读取短整数数组。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4226
**参数:**
W
wanghaoxu 已提交
4227 4228 4229 4230 4231 4232 4233

  | 参数名 | 类型     | 必填 | 说明                 |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | number[] | 是   | 要读取的短整数数组。 |

**示例:**

Y
Yippo 已提交
4234
  ```ts
W
wanghaoxu 已提交
4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257
  let data = rpc.MessageParcel.create();
  let result = data.writeShortArray([11, 12, 13]);
  console.log("RpcClient: writeShortArray is " + result);
  let array = new Array(3);
  data.readShortArray(array);
  ```

### readShortArray

readShortArray(): number[]

从MessageParcel实例中读取短整数数组。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型     | 说明             |
  | -------- | ---------------- |
  | number[] | 返回短整数数组。 |

**示例:**

Y
Yippo 已提交
4258
  ```ts
W
wanghaoxu 已提交
4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273
  let data = rpc.MessageParcel.create();
  let result = data.writeShortArray([11, 12, 13]);
  console.log("RpcClient: writeShortArray is " + result);
  let array = data.readShortArray();
 console.log("RpcClient: readShortArray is " + array);
  ```

### writeIntArray

writeIntArray(intArray: number[]): boolean

将整数数组写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4274
**参数:**
W
wanghaoxu 已提交
4275 4276 4277 4278 4279 4280 4281 4282 4283

  | 参数名   | 类型     | 必填 | 说明               |
  | -------- | -------- | ---- | ------------------ |
  | intArray | number[] | 是   | 要写入的整数数组。 |

**返回值:**

  | 类型    | 说明                          |
  | ------- | ----------------------------- |
4284
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
4285 4286 4287

**示例:**

Y
Yippo 已提交
4288
  ```ts
W
wanghaoxu 已提交
4289 4290 4291 4292 4293 4294 4295
  let data = rpc.MessageParcel.create();
  let result = data.writeIntArray([100, 111, 112]);
  console.log("RpcClient: writeIntArray is " + result);
  ```

### readIntArray

C
crazy_hu 已提交
4296
readIntArray(dataIn: number[]): void
W
wanghaoxu 已提交
4297 4298 4299 4300 4301

从MessageParcel实例中读取整数数组。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4302
**参数:**
W
wanghaoxu 已提交
4303 4304 4305 4306 4307 4308 4309

  | 参数名 | 类型     | 必填 | 说明               |
  | ------ | -------- | ---- | ------------------ |
  | dataIn | number[] | 是   | 要读取的整数数组。 |

**示例:**

Y
Yippo 已提交
4310
  ```ts
W
wanghaoxu 已提交
4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333
  let data = rpc.MessageParcel.create();
  let result = data.writeIntArray([100, 111, 112]);
  console.log("RpcClient: writeIntArray is " + result);
  let array = new Array(3);
  data.readIntArray(array);
  ```

### readIntArray

readIntArray(): number[]

从MessageParcel实例中读取整数数组。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型     | 说明           |
  | -------- | -------------- |
  | number[] | 返回整数数组。 |

**示例:**

Y
Yippo 已提交
4334
  ```ts
W
wanghaoxu 已提交
4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349
  let data = rpc.MessageParcel.create();
  let result = data.writeIntArray([100, 111, 112]);
  console.log("RpcClient: writeIntArray is " + result);
  let array = data.readIntArray();
  console.log("RpcClient: readIntArray is " + array);
  ```

### writeLongArray

writeLongArray(longArray: number[]): boolean

将长整数数组写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4350
**参数:**
W
wanghaoxu 已提交
4351 4352 4353 4354 4355 4356 4357 4358 4359

  | 参数名    | 类型     | 必填 | 说明                 |
  | --------- | -------- | ---- | -------------------- |
  | longArray | number[] | 是   | 要写入的长整数数组。 |

**返回值:**

  | 类型    | 说明                          |
  | ------- | ----------------------------- |
4360
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
4361 4362 4363

**示例:**

Y
Yippo 已提交
4364
  ```ts
W
wanghaoxu 已提交
4365 4366 4367 4368 4369 4370 4371
  let data = rpc.MessageParcel.create();
  let result = data.writeLongArray([1111, 1112, 1113]);
  console.log("RpcClient: writeLongArray is " + result);
  ```

### readLongArray

C
crazy_hu 已提交
4372
readLongArray(dataIn: number[]): void
W
wanghaoxu 已提交
4373 4374 4375 4376 4377

从MessageParcel实例读取长整数数组。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4378
**参数:**
W
wanghaoxu 已提交
4379 4380 4381 4382 4383 4384 4385

  | 参数名 | 类型     | 必填 | 说明                 |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | number[] | 是   | 要读取的长整数数组。 |

**示例:**

Y
Yippo 已提交
4386
  ```ts
W
wanghaoxu 已提交
4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409
  let data = rpc.MessageParcel.create();
  let result = data.writeLongArray([1111, 1112, 1113]);
  console.log("RpcClient: writeLongArray is " + result);
  let array = new Array(3);
  data.readLongArray(array);
  ```

### readLongArray

readLongArray(): number[]

从MessageParcel实例中读取长整数数组。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

 | 类型     | 说明             |
 | -------- | ---------------- |
 | number[] | 返回长整数数组。 |

**示例:**

Y
Yippo 已提交
4410
  ```ts
W
wanghaoxu 已提交
4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425
  let data = rpc.MessageParcel.create();
  let result = data.writeLongArray([1111, 1112, 1113]);
  console.log("RpcClient: writeLongArray is " + result);
  let array = data.readLongArray();
  console.log("RpcClient: readLongArray is " + array);
  ```

### writeFloatArray

writeFloatArray(floatArray: number[]): boolean

将浮点数组写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4426
**参数:**
W
wanghaoxu 已提交
4427 4428 4429 4430 4431 4432 4433 4434 4435

  | 参数名 | 类型 | 必填 | 说明 |
  | ---------- | -------- | ---- | --- |
  | floatArray | number[] | 是   | 要写入的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 |

**返回值:**

  | 类型    | 说明                          |
  | ------- | ----------------------------- |
4436
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
4437 4438 4439

**示例:**

Y
Yippo 已提交
4440
  ```ts
W
wanghaoxu 已提交
4441 4442 4443 4444 4445 4446 4447
  let data = rpc.MessageParcel.create();
  let result = data.writeFloatArray([1.2, 1.3, 1.4]);
  console.log("RpcClient: writeFloatArray is " + result);
  ```

### readFloatArray

C
crazy_hu 已提交
4448
readFloatArray(dataIn: number[]): void
W
wanghaoxu 已提交
4449 4450 4451 4452 4453

从MessageParcel实例中读取浮点数组。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4454
**参数:**
W
wanghaoxu 已提交
4455 4456 4457 4458 4459 4460 4461

  | 参数名 | 类型 | 必填 | 说明 |
  | ------ | -------- | ---- | ------ |
  | dataIn | number[] | 是   | 要读取的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 |

**示例:**

Y
Yippo 已提交
4462
  ```ts
W
wanghaoxu 已提交
4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485
  let data = rpc.MessageParcel.create();
  let result = data.writeFloatArray([1.2, 1.3, 1.4]);
  console.log("RpcClient: writeFloatArray is " + result);
  let array = new Array(3);
  data.readFloatArray(array);
  ```

### readFloatArray

readFloatArray(): number[]

从MessageParcel实例中读取浮点数组。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型     | 说明           |
  | -------- | -------------- |
  | number[] | 返回浮点数组。 |

**示例:**

Y
Yippo 已提交
4486
  ```ts
W
wanghaoxu 已提交
4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501
  let data = rpc.MessageParcel.create();
  let result = data.writeFloatArray([1.2, 1.3, 1.4]);
  console.log("RpcClient: writeFloatArray is " + result);
  let array = data.readFloatArray();
  console.log("RpcClient: readFloatArray is " + array);
  ```

### writeDoubleArray

writeDoubleArray(doubleArray: number[]): boolean

将双精度浮点数组写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4502
**参数:**
W
wanghaoxu 已提交
4503 4504 4505 4506 4507 4508 4509 4510 4511

  | 参数名      | 类型     | 必填 | 说明                     |
  | ----------- | -------- | ---- | ------------------------ |
  | doubleArray | number[] | 是   | 要写入的双精度浮点数组。 |

**返回值:**

  | 类型    | 说明                          |
  | ------- | ----------------------------- |
4512
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
4513 4514 4515

**示例:**

Y
Yippo 已提交
4516
  ```ts
W
wanghaoxu 已提交
4517 4518 4519 4520 4521 4522 4523
  let data = rpc.MessageParcel.create();
  let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
  console.log("RpcClient: writeDoubleArray is " + result);
  ```

### readDoubleArray

C
crazy_hu 已提交
4524
readDoubleArray(dataIn: number[]): void
W
wanghaoxu 已提交
4525 4526 4527 4528 4529

从MessageParcel实例中读取双精度浮点数组。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4530
**参数:**
W
wanghaoxu 已提交
4531 4532 4533 4534 4535 4536 4537

  | 参数名 | 类型     | 必填 | 说明                     |
  | ------ | -------- | ---- | ------------------------ |
  | dataIn | number[] | 是   | 要读取的双精度浮点数组。 |

**示例:**

Y
Yippo 已提交
4538
  ```ts
W
wanghaoxu 已提交
4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561
  let data = rpc.MessageParcel.create();
  let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
  console.log("RpcClient: writeDoubleArray is " + result);
  let array = new Array(3);
  data.readDoubleArray(array);
  ```

### readDoubleArray

readDoubleArray(): number[]

从MessageParcel实例读取双精度浮点数组。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型     | 说明                 |
  | -------- | -------------------- |
  | number[] | 返回双精度浮点数组。 |

**示例:**

Y
Yippo 已提交
4562
  ```ts
W
wanghaoxu 已提交
4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577
  let data = rpc.MessageParcel.create();
  let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
  console.log("RpcClient: writeDoubleArray is " + result);
  let array = data.readDoubleArray();
  console.log("RpcClient: readDoubleArray is " + array);
  ```

### writeBooleanArray

writeBooleanArray(booleanArray: boolean[]): boolean

将布尔数组写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4578
**参数:**
W
wanghaoxu 已提交
4579 4580 4581 4582 4583 4584 4585 4586 4587

  | 参数名       | 类型      | 必填 | 说明               |
  | ------------ | --------- | ---- | ------------------ |
  | booleanArray | boolean[] | 是   | 要写入的布尔数组。 |

**返回值:**

  | 类型    | 说明                              |
  | ------- | --------------------------------- |
4588
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
4589 4590 4591

**示例:**

Y
Yippo 已提交
4592
  ```ts
W
wanghaoxu 已提交
4593 4594 4595 4596 4597 4598 4599
  let data = rpc.MessageParcel.create();
  let result = data.writeBooleanArray([false, true, false]);
  console.log("RpcClient: writeBooleanArray is " + result);
  ```

### readBooleanArray

C
crazy_hu 已提交
4600
readBooleanArray(dataIn: boolean[]): void
W
wanghaoxu 已提交
4601 4602 4603 4604 4605

从MessageParcel实例中读取布尔数组。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4606
**参数:**
W
wanghaoxu 已提交
4607 4608 4609 4610 4611 4612 4613

  | 参数名 | 类型      | 必填 | 说明               |
  | ------ | --------- | ---- | ------------------ |
  | dataIn | boolean[] | 是   | 要读取的布尔数组。 |

**示例:**

Y
Yippo 已提交
4614
  ```ts
W
wanghaoxu 已提交
4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637
  let data = rpc.MessageParcel.create();
  let result = data.writeBooleanArray([false, true, false]);
  console.log("RpcClient: writeBooleanArray is " + result);
  let array = new Array(3);
  data.readBooleanArray(array);
  ```

### readBooleanArray

readBooleanArray(): boolean[]

从MessageParcel实例中读取布尔数组。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型      | 说明           |
  | --------- | -------------- |
  | boolean[] | 返回布尔数组。 |

**示例:**

Y
Yippo 已提交
4638
  ```ts
W
wanghaoxu 已提交
4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653
  let data = rpc.MessageParcel.create();
  let result = data.writeBooleanArray([false, true, false]);
  console.log("RpcClient: writeBooleanArray is " + result);
  let array = data.readBooleanArray();
  console.log("RpcClient: readBooleanArray is " + array);
  ```

### writeCharArray

writeCharArray(charArray: number[]): boolean

将单个字符数组写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4654
**参数:**
W
wanghaoxu 已提交
4655 4656 4657 4658 4659 4660 4661 4662 4663

  | 参数名    | 类型     | 必填 | 说明                   |
  | --------- | -------- | ---- | ---------------------- |
  | charArray | number[] | 是   | 要写入的单个字符数组。 |

**返回值:**

  | 类型    | 说明                              |
  | ------- | --------------------------------- |
4664
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
4665 4666 4667

**示例:**

Y
Yippo 已提交
4668
  ```ts
W
wanghaoxu 已提交
4669 4670 4671 4672 4673 4674 4675
  let data = rpc.MessageParcel.create();
  let result = data.writeCharArray([97, 98, 88]);
  console.log("RpcClient: writeCharArray is " + result);
  ```

### readCharArray

C
crazy_hu 已提交
4676
readCharArray(dataIn: number[]): void
W
wanghaoxu 已提交
4677 4678 4679 4680 4681

从MessageParcel实例中读取单个字符数组。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4682
**参数:**
W
wanghaoxu 已提交
4683 4684 4685 4686 4687 4688 4689

  | 参数名 | 类型     | 必填 | 说明                   |
  | ------ | -------- | ---- | ---------------------- |
  | dataIn | number[] | 是   | 要读取的单个字符数组。 |

**示例:**

Y
Yippo 已提交
4690
  ```ts
W
wanghaoxu 已提交
4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713
  let data = rpc.MessageParcel.create();
  let result = data.writeCharArray([97, 98, 99]);
  console.log("RpcClient: writeCharArray is " + result);
  let array = new Array(3);
  data.readCharArray(array);
  ```

### readCharArray

readCharArray(): number[]

从MessageParcel实例读取单个字符数组。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型     | 说明               |
  | -------- | ------------------ |
  | number[] | 返回单个字符数组。 |

**示例:**

Y
Yippo 已提交
4714
  ```ts
W
wanghaoxu 已提交
4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729
  let data = rpc.MessageParcel.create();
  let result = data.writeCharArray([97, 98, 99]);
  console.log("RpcClient: writeCharArray is " + result);
  let array = data.readCharArray();
  console.log("RpcClient: readCharArray is " + array);
  ```

### writeStringArray

writeStringArray(stringArray: string[]): boolean

将字符串数组写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4730
**参数:**
W
wanghaoxu 已提交
4731 4732 4733 4734 4735 4736 4737 4738 4739

  | 参数名      | 类型     | 必填 | 说明 |
  | ----------- | -------- | ---- | ---------------- |
  | stringArray | string[] | 是   | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 |

**返回值:**

  | 类型    | 说明 |
  | ------- | --------------------------------- |
4740
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
4741 4742 4743

**示例:**

Y
Yippo 已提交
4744
  ```ts
W
wanghaoxu 已提交
4745 4746 4747 4748 4749 4750 4751
  let data = rpc.MessageParcel.create();
  let result = data.writeStringArray(["abc", "def"]);
  console.log("RpcClient: writeStringArray is " + result);
  ```

### readStringArray

C
crazy_hu 已提交
4752
readStringArray(dataIn: string[]): void
W
wanghaoxu 已提交
4753 4754 4755 4756 4757

从MessageParcel实例读取字符串数组。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4758
**参数:**
W
wanghaoxu 已提交
4759 4760 4761 4762 4763 4764 4765

  | 参数名 | 类型     | 必填 | 说明                 |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | string[] | 是   | 要读取的字符串数组。 |

**示例:**

Y
Yippo 已提交
4766
  ```ts
W
wanghaoxu 已提交
4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789
  let data = rpc.MessageParcel.create();
  let result = data.writeStringArray(["abc", "def"]);
  console.log("RpcClient: writeStringArray is " + result);
  let array = new Array(2);
  data.readStringArray(array);
  ```

### readStringArray

readStringArray(): string[]

从MessageParcel实例读取字符串数组。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型     | 说明             |
  | -------- | ---------------- |
  | string[] | 返回字符串数组。 |

**示例:**

Y
Yippo 已提交
4790
  ```ts
W
wanghaoxu 已提交
4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807
  let data = rpc.MessageParcel.create();
  let result = data.writeStringArray(["abc", "def"]);
  console.log("RpcClient: writeStringArray is " + result);
  let array = data.readStringArray();
  console.log("RpcClient: readStringArray is " + array);
  ```

### writeNoException<sup>8+</sup>

writeNoException(): void

向MessageParcel写入“指示未发生异常”的信息。

**系统能力**:SystemCapability.Communication.IPC.Core

**示例:**

Y
Yippo 已提交
4808
  ```ts
W
wanghaoxu 已提交
4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848
  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: onRemoteRequest called");
              reply.writeNoException();
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
  }
  ```

### readException<sup>8+</sup>

readException(): void

从MessageParcel中读取异常。

**系统能力**:SystemCapability.Communication.IPC.Core

**示例:**
4849 4850
 
  获取服务
W
wanghaoxu 已提交
4851

Y
Yippo 已提交
4852
  ```ts
W
wanghaoxu 已提交
4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
4869
      "abilityName": "com.ohos.server.EntryAbility",
W
wanghaoxu 已提交
4870 4871
  };
  FA.connectAbility(want, connect);
4872 4873
  ```

4874
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息
4875 4876

  ```ts
W
wanghaoxu 已提交
4877 4878 4879 4880 4881
  let option = new rpc.MessageOption();
  let data = rpc.MessageParcel.create();
  let reply = rpc.MessageParcel.create();
  data.writeInt(1);
  data.writeString("hello");
W
wanghaoxu 已提交
4882
  proxy.sendMessageRequest(1, data, reply, option)
W
wanghaoxu 已提交
4883 4884
      .then(function(errCode) {
          if (errCode === 0) {
W
wanghaoxu 已提交
4885
              console.log("sendMessageRequest got result");
W
wanghaoxu 已提交
4886 4887 4888 4889
              reply.readException();
              let msg = reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
W
wanghaoxu 已提交
4890
              console.log("RPCTest: sendMessageRequest failed, errCode: " + errCode);
W
wanghaoxu 已提交
4891 4892
          }
      }).catch(function(e) {
W
wanghaoxu 已提交
4893
          console.log("RPCTest: sendMessageRequest got exception: " + e.message);
W
wanghaoxu 已提交
4894
      }).finally (() => {
W
wanghaoxu 已提交
4895
          console.log("RPCTest: sendMessageRequest ends, reclaim parcel");
W
wanghaoxu 已提交
4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908
          data.reclaim();
          reply.reclaim();
      });
  ```

### writeSequenceableArray

writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean

将可序列化对象数组写入MessageParcel实例。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4909
**参数:**
W
wanghaoxu 已提交
4910 4911 4912 4913 4914 4915 4916 4917 4918

  | 参数名            | 类型           | 必填 | 说明                       |
  | ----------------- | -------------- | ---- | -------------------------- |
  | sequenceableArray | Sequenceable[] | 是   | 要写入的可序列化对象数组。 |

**返回值:**

  | 类型    | 说明                              |
  | ------- | --------------------------------- |
4919
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
4920 4921 4922

**示例:**

Y
Yippo 已提交
4923
  ```ts
W
wanghaoxu 已提交
4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958
  class MySequenceable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  let sequenceable = new MySequenceable(1, "aaa");
  let sequenceable2 = new MySequenceable(2, "bbb");
  let sequenceable3 = new MySequenceable(3, "ccc");
  let a = [sequenceable, sequenceable2, sequenceable3];
  let data = rpc.MessageParcel.create();
  let result = data.writeSequenceableArray(a);
  console.log("RpcClient: writeSequenceableArray is " + result);
  ```

### readSequenceableArray<sup>8+</sup>

readSequenceableArray(sequenceableArray: Sequenceable[]): void

从MessageParcel实例读取可序列化对象数组。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
4959
**参数:**
W
wanghaoxu 已提交
4960 4961 4962 4963 4964 4965 4966

  | 参数名            | 类型           | 必填 | 说明                       |
  | ----------------- | -------------- | ---- | -------------------------- |
  | sequenceableArray | Sequenceable[] | 是   | 要读取的可序列化对象数组。 |

**示例:**

Y
Yippo 已提交
4967
  ```ts
W
wanghaoxu 已提交
4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004
  class MySequenceable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  let sequenceable = new MySequenceable(1, "aaa");
  let sequenceable2 = new MySequenceable(2, "bbb");
  let sequenceable3 = new MySequenceable(3, "ccc");
  let a = [sequenceable, sequenceable2, sequenceable3];
  let data = rpc.MessageParcel.create();
  let result = data.writeSequenceableArray(a);
  console.log("RpcClient: writeSequenceableArray is " + result);
  let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")];
  data.readSequenceableArray(b);
  ```

### writeRemoteObjectArray<sup>8+</sup>

writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean

将IRemoteObject对象数组写入MessageParcel。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5005
**参数:**
W
wanghaoxu 已提交
5006 5007 5008 5009 5010 5011 5012 5013 5014

  | 参数名      | 类型            | 必填 | 说明 |
  | ----------- | --------------- | ---- | ----- |
  | objectArray | IRemoteObject[] | 是   | 要写入MessageParcel的IRemoteObject对象数组。 |

**返回值:**

  | 类型    | 说明                                                                                                                 |
  | ------- | -------------------------------------------------------------------------------------------------------------------- |
5015
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
5016 5017 5018

**示例:**

Y
Yippo 已提交
5019
  ```ts
W
wanghaoxu 已提交
5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
          this.attachLocalInterface(this, descriptor);
      }
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
      asObject(): rpc.IRemoteObject {
          return this;
      }
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
  let data = rpc.MessageParcel.create();
  let result = data.writeRemoteObjectArray(a);
  console.log("RpcClient: writeRemoteObjectArray is " + result);
  ```

### readRemoteObjectArray<sup>8+</sup>

readRemoteObjectArray(objects: IRemoteObject[]): void

从MessageParcel读取IRemoteObject对象数组。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5057
**参数:**
5058

W
wanghaoxu 已提交
5059 5060 5061 5062 5063 5064
  | 参数名  | 类型            | 必填 | 说明 |
  | ------- | --------------- | ---- | --------- |
  | objects | IRemoteObject[] | 是   | 从MessageParcel读取的IRemoteObject对象数组。 |

**示例:**

Y
Yippo 已提交
5065
  ```ts
W
wanghaoxu 已提交
5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
          this.attachLocalInterface(this, descriptor);
      }
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
         return false;
      }
      asObject(): rpc.IRemoteObject {
          return this;
      }
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
  let data = rpc.MessageParcel.create();
  let result = data.writeRemoteObjectArray(a);
  let b = new Array(3);
  data.readRemoteObjectArray(b);
  ```

### readRemoteObjectArray<sup>8+</sup>

readRemoteObjectArray(): IRemoteObject[]

从MessageParcel读取IRemoteObject对象数组。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型 | 说明 |
  | --------------- | -------- |
  | IRemoteObject[] | 返回IRemoteObject对象数组。 |

**示例:**

Y
Yippo 已提交
5112
  ```ts
W
wanghaoxu 已提交
5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
          this.attachLocalInterface(this, descriptor);
      }
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
      asObject(): rpc.IRemoteObject {
          return this;
      }
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
  let data = rpc.MessageParcel.create();
  let result = data.writeRemoteObjectArray(a);
  console.log("RpcClient: readRemoteObjectArray is " + result);
  let b = data.readRemoteObjectArray();
  console.log("RpcClient: readRemoteObjectArray is " + b);
  ```

### closeFileDescriptor<sup>8+</sup>

static closeFileDescriptor(fd: number): void

5148
静态方法,关闭给定的文件描述符。
W
wanghaoxu 已提交
5149 5150 5151

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5152
**参数:**
W
wanghaoxu 已提交
5153 5154 5155 5156 5157 5158 5159

  | 参数名 | 类型   | 必填 | 说明                 |
  | ------ | ------ | ---- | -------------------- |
  | fd     | number | 是   | 要关闭的文件描述符。 |

**示例:**

Y
Yippo 已提交
5160
  ```ts
W
wanghaoxu 已提交
5161 5162 5163 5164 5165 5166 5167 5168 5169 5170
  import fileio from '@ohos.fileio';
  let filePath = "path/to/file";
  let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
  rpc.MessageParcel.closeFileDescriptor(fd);
  ```

### dupFileDescriptor<sup>8+</sup>

static dupFileDescriptor(fd: number) :number

5171
静态方法,复制给定的文件描述符。
W
wanghaoxu 已提交
5172 5173 5174

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5175
**参数:**
W
wanghaoxu 已提交
5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188

  | 参数名 | 类型   | 必填 | 说明                     |
  | ------ | ------ | ---- | ------------------------ |
  | fd     | number | 是   | 表示已存在的文件描述符。 |

**返回值:**

  | 类型   | 说明                 |
  | ------ | -------------------- |
  | number | 返回新的文件描述符。 |

**示例:**

Y
Yippo 已提交
5189
  ```ts
W
wanghaoxu 已提交
5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207
  import fileio from '@ohos.fileio';
  let filePath = "path/to/file";
  let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
  let newFd = rpc.MessageParcel.dupFileDescriptor(fd);
  ```

### containFileDescriptors<sup>8+</sup>

containFileDescriptors(): boolean

检查此MessageParcel对象是否包含文件描述符。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型    | 说明                                                               |
  | ------- | ------------------------------------------------------------------ |
5208
  | boolean |true:包含文件描述符,false:未包含文件描述符。|
W
wanghaoxu 已提交
5209 5210 5211

**示例:**

Y
Yippo 已提交
5212
  ```ts
W
wanghaoxu 已提交
5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231
  import fileio from '@ohos.fileio';
  let parcel = new rpc.MessageParcel();
  let filePath = "path/to/file";
  let r1 = parcel.containFileDescriptors();
  let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
  let writeResult = parcel.writeFileDescriptor(fd);
  console.log("RpcTest: parcel writeFd result is : " + writeResult);
  let containFD = parcel.containFileDescriptors();
  console.log("RpcTest: parcel after write fd containFd result is : " + containFD);
  ```

### writeFileDescriptor<sup>8+</sup>

writeFileDescriptor(fd: number): boolean

写入文件描述符到MessageParcel。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5232
**参数:**
W
wanghaoxu 已提交
5233 5234 5235 5236 5237 5238 5239 5240 5241

  | 参数名 | 类型   | 必填 | 说明         |
  | ------ | ------ | ---- | ------------ |
  | fd     | number | 是   | 文件描述符。 |

**返回值:**

  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
5242
  | boolean | true:操作成功,false:操作失败。|
W
wanghaoxu 已提交
5243 5244 5245

**示例:**

Y
Yippo 已提交
5246
  ```ts
W
wanghaoxu 已提交
5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270
  import fileio from '@ohos.fileio';
  let parcel = new rpc.MessageParcel();
  let filePath = "path/to/file";
  let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
  let writeResult = parcel.writeFileDescriptor(fd);
  console.log("RpcTest: parcel writeFd result is : " + writeResult);
  ```

### readFileDescriptor<sup>8+</sup>

readFileDescriptor(): number

从MessageParcel中读取文件描述符。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明             |
  | ------ | ---------------- |
  | number | 返回文件描述符。 |

**示例:**

Y
Yippo 已提交
5271
  ```ts
W
wanghaoxu 已提交
5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288
  import fileio from '@ohos.fileio';
  let parcel = new rpc.MessageParcel();
  let filePath = "path/to/file";
  let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
  let writeResult = parcel.writeFileDescriptor(fd);
  let readFD = parcel.readFileDescriptor();
  console.log("RpcTest: parcel read fd is : " + readFD);
  ```

### writeAshmem<sup>8+</sup>

writeAshmem(ashmem: Ashmem): boolean

将指定的匿名共享对象写入此MessageParcel。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5289
**参数:**
W
wanghaoxu 已提交
5290 5291 5292 5293 5294 5295 5296 5297 5298

  | 参数名 | 类型   | 必填 | 说明                                |
  | ------ | ------ | ---- | ----------------------------------- |
  | ashmem | Ashmem | 是   | 要写入MessageParcel的匿名共享对象。 |

**返回值:**

  | 类型    | 说明                                                                 |
  | ------- | -------------------------------------------------------------------- |
5299
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
5300 5301 5302

**示例:**

Y
Yippo 已提交
5303
  ```ts
W
wanghaoxu 已提交
5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325
  let parcel = new rpc.MessageParcel();
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024);
  let isWriteSuccess = parcel.writeAshmem(ashmem);
  console.log("RpcTest: write ashmem to result is : " + isWriteSuccess);
  ```

### readAshmem<sup>8+</sup>

readAshmem(): Ashmem

从MessageParcel读取匿名共享对象。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明               |
  | ------ | ------------------ |
  | Ashmem | 返回匿名共享对象。 |

**示例:**

Y
Yippo 已提交
5326
  ```ts
W
wanghaoxu 已提交
5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350
  let parcel = new rpc.MessageParcel();
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024);
  let isWriteSuccess = parcel.writeAshmem(ashmem);
  console.log("RpcTest: write ashmem to result is : " + isWriteSuccess);
  let readAshmem = parcel.readAshmem();
  console.log("RpcTest: read ashmem to result is : " + readAshmem);
  ```

### getRawDataCapacity<sup>8+</sup>

getRawDataCapacity(): number

获取MessageParcel可以容纳的最大原始数据量。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明                                                       |
  | ------ | ---------------------------------------------------------- |
  | number | 返回MessageParcel可以容纳的最大原始数据量,即128&nbsp;Mb。 |

**示例:**

Y
Yippo 已提交
5351
  ```ts
W
wanghaoxu 已提交
5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364
  let parcel = new rpc.MessageParcel();
  let result = parcel.getRawDataCapacity();
  console.log("RpcTest: parcel get RawDataCapacity result is : " + result);
  ```

### writeRawData<sup>8+</sup>

writeRawData(rawData: number[], size: number): boolean

将原始数据写入MessageParcel对象。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5365
**参数:**
W
wanghaoxu 已提交
5366 5367 5368 5369 5370 5371 5372 5373 5374 5375

  | 参数名  | 类型     | 必填 | 说明                               |
  | ------- | -------- | ---- | ---------------------------------- |
  | rawData | number[] | 是   | 要写入的原始数据。                 |
  | size    | number   | 是   | 发送的原始数据大小,以字节为单位。 |

**返回值:**

  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
5376
  | boolean | true:写入成功,false:写入失败。|
W
wanghaoxu 已提交
5377 5378 5379

**示例:**

Y
Yippo 已提交
5380
  ```ts
W
wanghaoxu 已提交
5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394
  let parcel = new rpc.MessageParcel();
  let arr = [1, 2, 3, 4, 5];
  let isWriteSuccess = parcel.writeRawData(arr, arr.length);
  console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess);
  ```

### readRawData<sup>8+</sup>

readRawData(size: number): number[]

从MessageParcel读取原始数据。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5395
**参数:**
W
wanghaoxu 已提交
5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408

  | 参数名 | 类型   | 必填 | 说明                     |
  | ------ | ------ | ---- | ------------------------ |
  | size   | number | 是   | 要读取的原始数据的大小。 |

**返回值:**

  | 类型     | 说明                           |
  | -------- | ------------------------------ |
  | number[] | 返回原始数据(以字节为单位)。 |

**示例:**

Y
Yippo 已提交
5409
  ```ts
W
wanghaoxu 已提交
5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429
  let parcel = new rpc.MessageParcel();
  let arr = [1, 2, 3, 4, 5];
  let isWriteSuccess = parcel.writeRawData(arr, arr.length);
  console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess);
  let result = parcel.readRawData(5);
  console.log("RpcTest: parcel read raw data result is : " + result);
  ```

## Parcelable<sup>9+</sup>

在进程间通信(IPC)期间,将类的对象写入MessageSequence并从MessageSequence中恢复它们。

### marshalling

marshalling(dataOut: MessageSequence): boolean

将此可序列对象封送到MessageSequence中。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5430
**参数:**
W
wanghaoxu 已提交
5431 5432 5433 5434 5435 5436 5437 5438 5439

  | 参数名  | 类型            | 必填 | 说明                                        |
  | ------- | --------------- | ---- | ------------------------------------------- |
  | dataOut | MessageSequence | 是   | 可序列对象将被封送到的MessageSequence对象。 |

**返回值:**

  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
5440
  | boolean | true:封送成功,false:封送失败。
W
wanghaoxu 已提交
5441 5442
**示例:**

Y
Yippo 已提交
5443
  ```ts
W
wanghaoxu 已提交
5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472
  class MyParcelable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
          return true;
      }
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
          return true;
      }
  }
  let parcelable = new MyParcelable(1, "aaa");
  let data = rpc.MessageSequence.create();
  let result = data.writeParcelable(parcelable);
  console.log("RpcClient: writeParcelable is " + result);
  let ret = new MyParcelable(0, "");
  let result2 = data.readParcelable(ret);
  console.log("RpcClient: readParcelable is " + result2);
  ```

### unmarshalling

C
crazy_hu 已提交
5473
unmarshalling(dataIn: MessageSequence): boolean
W
wanghaoxu 已提交
5474 5475 5476 5477 5478

从MessageSequence中解封此可序列对象。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5479
**参数:**
W
wanghaoxu 已提交
5480 5481 5482 5483 5484 5485 5486 5487 5488

  | 参数名 | 类型            | 必填 | 说明                                            |
  | ------ | --------------- | ---- | ----------------------------------------------- |
  | dataIn | MessageSequence | 是   | 已将可序列对象封送到其中的MessageSequence对象。 |

**返回值:**

  | 类型    | 说明                                          |
  | ------- | --------------------------------------------- |
5489
  | boolean | true:反序列化成功,false:反序列化失败。|
W
wanghaoxu 已提交
5490 5491 5492

**示例:**

Y
Yippo 已提交
5493
  ```ts
W
wanghaoxu 已提交
5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520
  class MyParcelable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
          return true;
      }
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
          return true;
      }
  }
  let parcelable = new MyParcelable(1, "aaa");
  let data = rpc.MessageSequence.create();
  let result = data.writeParcelable(parcelable);
  console.log("RpcClient: writeParcelable is " + result);
  let ret = new MyParcelable(0, "");
  let result2 = data.readParcelable(ret);
  console.log("RpcClient: readParcelable is " + result2);
  ```

5521
## Sequenceable<sup>(deprecated)</sup>
W
wanghaoxu 已提交
5522

5523 5524 5525
>从API version 9 开始不再维护,建议使用[Parcelable](#parcelable9)类替代。

在进程间通信(IPC)期间,将类的对象写入MessageParcel并从MessageParcel中恢复它们。
W
wanghaoxu 已提交
5526 5527 5528 5529 5530 5531 5532 5533 5534

### marshalling

marshalling(dataOut: MessageParcel): boolean

将此可序列对象封送到MessageParcel中。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5535
**参数:**
W
wanghaoxu 已提交
5536 5537 5538

  | 参数名  | 类型                            | 必填 | 说明                                      |
  | ------- | ------------------------------- | ---- | ----------------------------------------- |
5539
  | dataOut | [MessageParcel](#messageparceldeprecated) | 是   | 可序列对象将被封送到的MessageParcel对象。 |
W
wanghaoxu 已提交
5540 5541 5542 5543 5544

**返回值:**

  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
5545
  | boolean | true:封送成功,false:封送失败。
W
wanghaoxu 已提交
5546 5547
**示例:**

Y
Yippo 已提交
5548
  ```ts
W
wanghaoxu 已提交
5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577
  class MySequenceable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  let sequenceable = new MySequenceable(1, "aaa");
  let data = rpc.MessageParcel.create();
  let result = data.writeSequenceable(sequenceable);
  console.log("RpcClient: writeSequenceable is " + result);
  let ret = new MySequenceable(0, "");
  let result2 = data.readSequenceable(ret);
  console.log("RpcClient: readSequenceable is " + result2);
  ```

### unmarshalling

C
crazy_hu 已提交
5578
unmarshalling(dataIn: MessageParcel): boolean
W
wanghaoxu 已提交
5579 5580 5581 5582 5583

从MessageParcel中解封此可序列对象。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5584
**参数:**
W
wanghaoxu 已提交
5585 5586 5587

  | 参数名 | 类型                            | 必填 | 说明                                          |
  | ------ | ------------------------------- | ---- | --------------------------------------------- |
5588
  | dataIn | [MessageParcel](#messageparceldeprecated) | 是   | 已将可序列对象封送到其中的MessageParcel对象。 |
W
wanghaoxu 已提交
5589 5590 5591 5592 5593

**返回值:**

  | 类型    | 说明                                          |
  | ------- | --------------------------------------------- |
5594
  | boolean | true:反序列化成功,false:反序列化失败。|
W
wanghaoxu 已提交
5595 5596 5597

**示例:**

Y
Yippo 已提交
5598
  ```ts
W
wanghaoxu 已提交
5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645
  class MySequenceable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  let sequenceable = new MySequenceable(1, "aaa");
  let data = rpc.MessageParcel.create();
  let result = data.writeSequenceable(sequenceable);
  console.log("RpcClient: writeSequenceable is " + result);
  let ret = new MySequenceable(0, "");
  let result2 = data.readSequenceable(ret);
  console.log("RpcClient: readSequenceable is " + result2);
  ```

## IRemoteBroker

远端对象的代理持有者。用于获取代理对象。

### asObject

asObject(): IRemoteObject

需派生类实现,获取代理或远端对象。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型  | 说明 |
  | ---- | ----- |
  | [IRemoteObject](#iremoteobject) | 如果调用者是RemoteObject对象,则直接返回本身;如果调用者是[RemoteProxy](#remoteproxy)对象,则返回它的持有者[IRemoteObject](#iremoteobject)。 |

**示例:**

Y
Yippo 已提交
5646
  ```ts
W
wanghaoxu 已提交
5647 5648 5649 5650 5651
  class TestAbility extends rpc.RemoteObject {
      asObject() {
          return this;
      }
  }
5652
  let remoteObject = new TestAbility().asObject();
W
wanghaoxu 已提交
5653 5654 5655 5656
  ```

**示例:**

5657 5658
  获取服务

Y
Yippo 已提交
5659
  ```ts
5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function (elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function (elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function () {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
5676
      "abilityName": "com.ohos.server.EntryAbility",
5677 5678
  };
  FA.connectAbility(want, connect);
5679 5680
  ```
  
5681
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的asObject接口方法获取代理或远端对象
5682 5683
  
  ```ts
W
wanghaoxu 已提交
5684 5685 5686 5687 5688 5689 5690 5691 5692
  class TestProxy {
      remote: rpc.RemoteObject;
      constructor(remote) {
          this.remote = remote;
      }
      asObject() {
          return this.remote;
      }
  }
5693 5694
  let iRemoteObject = new TestProxy(proxy).asObject();

W
wanghaoxu 已提交
5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710
  ```

## DeathRecipient

用于订阅远端对象的死亡通知。当被订阅该通知的远端对象死亡时,本端可收到消息,调用[onRemoteDied](#onremotedied)接口。远端对象死亡可以为远端对象所在进程死亡,远端对象所在设备关机或重启,当远端对象与本端对象属于不同设备时,也可为远端对象离开组网时。

### onRemoteDied

onRemoteDied(): void

在成功添加死亡通知订阅后,当远端对象死亡时,将自动调用本方法。

**系统能力**:SystemCapability.Communication.IPC.Core

**示例:**

Y
Yippo 已提交
5711
  ```ts
W
wanghaoxu 已提交
5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  ```

## RequestResult<sup>9+</sup>

发送请求的响应结果。

**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。

C
crazy_hu 已提交
5725 5726 5727 5728 5729 5730
  | 名称    | 类型            | 可读 | 可写 | 说明                                  |
  | ------- | --------------- | ---- | ---- |-------------------------------------- |
  | errCode | number          | 是   | 否   | 错误码。                              |
  | code    | number          | 是   | 否   | 消息代码。                            |
  | data    | MessageSequence | 是   | 否   | 发送给对端进程的MessageSequence对象。 |
  | reply   | MessageSequence | 是   | 否   | 对端进程返回的MessageSequence对象。   |
W
wanghaoxu 已提交
5731 5732 5733

## SendRequestResult<sup>8+(deprecated)</sup>

5734 5735 5736
>从API version 9 开始不再维护,建议使用[RequestResult](#requestresult9)类替代。

发送请求的响应结果。
W
wanghaoxu 已提交
5737 5738 5739

**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。

C
crazy_hu 已提交
5740 5741 5742 5743 5744 5745
  | 名称    | 类型          | 可读 | 可写 | 说明                                |
  | ------- | ------------- | ---- | ---- | ----------------------------------- |
  | errCode | number        | 是   | 否   | 错误码。                            |
  | code    | number        | 是   | 否   | 消息代码。                          |
  | data    | MessageParcel | 是   | 否   | 发送给对端进程的MessageParcel对象。 |
  | reply   | MessageParcel | 是   | 否   | 对端进程返回的MessageParcel对象。   |
W
wanghaoxu 已提交
5746 5747 5748 5749 5750 5751 5752 5753 5754

## IRemoteObject

该接口可用于查询或获取接口描述符、添加或删除死亡通知、转储对象状态到特定文件、发送消息。

### getLocalInterface<sup>9+</sup>

getLocalInterface(descriptor: string): IRemoteBroker

5755
查询接口描述符的字符串。
W
wanghaoxu 已提交
5756 5757 5758

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5759
**参数:**
W
wanghaoxu 已提交
5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770

  | 参数名     | 类型   | 必填 | 说明                 |
  | ---------- | ------ | ---- | -------------------- |
  | descriptor | string | 是   | 接口描述符的字符串。 |

**返回值:**

  | 类型          | 说明                                          |
  | ------------- | --------------------------------------------- |
  | IRemoteBroker | 返回绑定到指定接口描述符的IRemoteBroker对象。 |

5771
### queryLocalInterface<sup>(deprecated)</sup>
W
wanghaoxu 已提交
5772

5773 5774
>从API version 9 开始不再维护,建议使用[getLocalInterface](#getlocalinterface9)类替代。

W
wanghaoxu 已提交
5775 5776
queryLocalInterface(descriptor: string): IRemoteBroker

5777
查询接口描述符的字符串。
W
wanghaoxu 已提交
5778 5779 5780

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5781
**参数:**
W
wanghaoxu 已提交
5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794

  | 参数名     | 类型   | 必填 | 说明                 |
  | ---------- | ------ | ---- | -------------------- |
  | descriptor | string | 是   | 接口描述符的字符串。 |

**返回值:**

  | 类型          | 说明                                          |
  | ------------- | --------------------------------------------- |
  | IRemoteBroker | 返回绑定到指定接口描述符的IRemoteBroker对象。 |

### sendRequest<sup>(deprecated)</sup>

5795 5796
>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9)类替代。

C
crazy_hu 已提交
5797
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
W
wanghaoxu 已提交
5798

5799
以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。
W
wanghaoxu 已提交
5800 5801 5802

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5803
**参数:**
W
wanghaoxu 已提交
5804 5805 5806 5807

  | 参数名  | 类型 | 必填 | 说明  |
  | ------- | ------------------------------- | ---- | ---- |
  | code    | number                          | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
5808 5809
  | data    | [MessageParcel](#messageparceldeprecated) | 是   | 保存待发送数据的&nbsp;MessageParcel对象。                                              |
  | reply   | [MessageParcel](#messageparceldeprecated) | 是   | 接收应答数据的MessageParcel对象。                                                      |
W
wanghaoxu 已提交
5810 5811 5812 5813 5814 5815
  | options | [MessageOption](#messageoption) | 是   | 本次请求的同异步模式,默认同步调用。                                                   |

**返回值:**

  | 类型    | 说明                                          |
  | ------- | --------------------------------------------- |
5816
  | boolean | true:发送成功,false:发送失败。|
W
wanghaoxu 已提交
5817 5818 5819 5820


### sendRequest<sup>8+(deprecated)</sup>

5821 5822
>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9)类替代。

C
crazy_hu 已提交
5823
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise&lt;SendRequestResult&gt;
W
wanghaoxu 已提交
5824

5825
以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。
W
wanghaoxu 已提交
5826 5827 5828

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5829
**参数:**
W
wanghaoxu 已提交
5830 5831 5832 5833

  | 参数名  | 类型                            | 必填 | 说明                                                                                   |
  | ------- | ------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                          | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
5834 5835
  | data    | [MessageParcel](#messageparceldeprecated) | 是   | 保存待发送数据的&nbsp;MessageParcel对象。                                              |
  | reply   | [MessageParcel](#messageparceldeprecated) | 是   | 接收应答数据的MessageParcel对象。                                                      |
W
wanghaoxu 已提交
5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846
  | options | [MessageOption](#messageoption) | 是   | 本次请求的同异步模式,默认同步调用。                                                   |

**返回值:**

  | 类型                             | 说明                                          |
  | -------------------------------- | --------------------------------------------- |
  | Promise&lt;SendRequestResult&gt; | 返回一个期约,兑现值是sendRequestResult实例。 |


### sendMessageRequest<sup>9+</sup>

C
crazy_hu 已提交
5847
sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise&lt;RequestResult&gt;
W
wanghaoxu 已提交
5848

5849
以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。
W
wanghaoxu 已提交
5850 5851 5852

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5853
**参数:**
W
wanghaoxu 已提交
5854 5855 5856 5857

  | 参数名  | 类型                            | 必填 | 说明                                                                                   |
  | ------- | ------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                          | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
5858 5859
  | data    | [MessageSequence](#messagesequence9) | 是   | 保存待发送数据的&nbsp;MessageSequence对象。                                            |
  | reply   | [MessageSequence](#messagesequence9) | 是   | 接收应答数据的MessageSequence对象。                                                    |
W
wanghaoxu 已提交
5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872
  | options | [MessageOption](#messageoption) | 是   | 本次请求的同异步模式,默认同步调用。                                                   |

**返回值:**

  | 类型                         | 说明                                      |
  | ---------------------------- | ----------------------------------------- |
  | Promise&lt;RequestResult&gt; | 返回一个期约,兑现值是requestResult实例。 |


### sendMessageRequest<sup>9+</sup>

sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback&lt;RequestResult&gt;): void

5873
以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。
W
wanghaoxu 已提交
5874 5875 5876

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5877
**参数:**
W
wanghaoxu 已提交
5878 5879 5880 5881

  | 参数名   | 类型                               | 必填 | 说明                                                                                   |
  | -------- | ---------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code     | number                             | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
5882 5883
  | data     | [MessageSequence](#messagesequence9) | 是   | 保存待发送数据的&nbsp;MessageSequence对象。                                            |
  | reply    | [MessageSequence](#messagesequence9) | 是   | 接收应答数据的MessageSequence对象。                                                    |
W
wanghaoxu 已提交
5884 5885 5886 5887 5888
  | options  | [MessageOption](#messageoption)    | 是   | 本次请求的同异步模式,默认同步调用。                                                   |
  | callback | AsyncCallback&lt;RequestResult&gt; | 是   | 接收发送结果的回调。                                                                   |

### sendRequest<sup>8+(deprecated)</sup>

5889 5890
>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9)类替代。

W
wanghaoxu 已提交
5891 5892
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback&lt;SendRequestResult&gt;): void

5893
以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。
W
wanghaoxu 已提交
5894 5895 5896

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5897
**参数:**
W
wanghaoxu 已提交
5898 5899 5900 5901

  | 参数名   | 类型                                   | 必填 | 说明                                                                                   |
  | -------- | -------------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code     | number                                 | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
5902 5903
  | data     | [MessageParcel](#messageparceldeprecated)        | 是   | 保存待发送数据的&nbsp;MessageParcel对象。                                              |
  | reply    | [MessageParcel](#messageparceldeprecated)        | 是   | 接收应答数据的MessageParcel对象。                                                      |
W
wanghaoxu 已提交
5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914
  | options  | [MessageOption](#messageoption)        | 是   | 本次请求的同异步模式,默认同步调用。                                                   |
  | callback | AsyncCallback&lt;SendRequestResult&gt; | 是   | 接收发送结果的回调。                                                                   |

### registerDeathRecipient<sup>9+</sup>

registerDeathRecipient(recipient: DeathRecipient, flags: number): void

注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5915
**参数:**
W
wanghaoxu 已提交
5916 5917 5918 5919 5920 5921

  | 参数名    | 类型                              | 必填 | 说明           |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | 是   | 要注册的回调。 |
  | flags     | number                            | 是   | 死亡通知标志。 |

W
wanghaoxu 已提交
5922 5923
**错误码:**

C
crazy_hu 已提交
5924
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
5925

W
wanghaoxu 已提交
5926 5927 5928
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900008 | proxy or remote object is invalid |
W
wanghaoxu 已提交
5929

5930
### addDeathrecipient<sup>(deprecated)</sup>
W
wanghaoxu 已提交
5931

5932 5933
>从API version 9 开始不再维护,建议使用[registerDeathRecipient](#registerdeathrecipient9)类替代。

W
wanghaoxu 已提交
5934 5935
addDeathRecipient(recipient: DeathRecipient, flags: number): boolean

5936
注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。
W
wanghaoxu 已提交
5937 5938 5939

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5940
**参数:**
W
wanghaoxu 已提交
5941 5942 5943 5944 5945 5946 5947 5948 5949 5950

  | 参数名    | 类型                              | 必填 | 说明           |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | 是   | 要注册的回调。 |
  | flags     | number                            | 是   | 死亡通知标志。 |

**返回值:**

  | 类型    | 说明                                          |
  | ------- | --------------------------------------------- |
5951
  | boolean | true:回调注册成功,false:回调注册失败。|
W
wanghaoxu 已提交
5952 5953 5954 5955


### unregisterDeathRecipient<sup>9+</sup>

C
crazy_hu 已提交
5956
unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void
W
wanghaoxu 已提交
5957 5958 5959 5960 5961

注销用于接收远程对象死亡通知的回调。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5962
**参数:**
W
wanghaoxu 已提交
5963 5964 5965 5966 5967 5968

  | 参数名    | 类型                              | 必填 | 说明           |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | 是   | 要注销的回调。 |
  | flags     | number                            | 是   | 死亡通知标志。 |

W
wanghaoxu 已提交
5969 5970
**错误码:**

C
crazy_hu 已提交
5971
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
5972

W
wanghaoxu 已提交
5973 5974 5975
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900008 | proxy or remote object is invalid |
W
wanghaoxu 已提交
5976

5977
### removeDeathRecipient<sup>(deprecated)</sup>
W
wanghaoxu 已提交
5978

5979 5980
>从API version 9 开始不再维护,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9)类替代。

W
wanghaoxu 已提交
5981 5982
removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean

5983
注销用于接收远程对象死亡通知的回调。
W
wanghaoxu 已提交
5984 5985 5986

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
5987
**参数:**
W
wanghaoxu 已提交
5988 5989 5990 5991 5992 5993 5994 5995 5996 5997

  | 参数名    | 类型                              | 必填 | 说明           |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | 是   | 要注销的回调。 |
  | flags     | number                            | 是   | 死亡通知标志。 |

**返回值:**

  | 类型    | 说明                                          |
  | ------- | --------------------------------------------- |
5998
  | boolean | true:回调注销成功,false:回调注销失败。|
W
wanghaoxu 已提交
5999 6000 6001 6002 6003

### getDescriptor<sup>9+</sup>

getDescriptor(): string

6004
获取对象的接口描述符,接口描述符为字符串。
W
wanghaoxu 已提交
6005 6006 6007 6008 6009 6010 6011 6012 6013

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明             |
  | ------ | ---------------- |
  | string | 返回接口描述符。 |

W
wanghaoxu 已提交
6014 6015
**错误码:**

C
crazy_hu 已提交
6016
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
6017

W
wanghaoxu 已提交
6018 6019 6020
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900008 | proxy or remote object is invalid |
W
wanghaoxu 已提交
6021 6022


6023
### getInterfaceDescriptor<sup>(deprecated)</sup>
W
wanghaoxu 已提交
6024

6025 6026
>从API version 9 开始不再维护,建议使用[getDescriptor](#getdescriptor9)类替代。

W
wanghaoxu 已提交
6027 6028
getInterfaceDescriptor(): string

6029
获取对象的接口描述符,接口描述符为字符串。
W
wanghaoxu 已提交
6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明             |
  | ------ | ---------------- |
  | string | 返回接口描述符。 |


### isObjectDead

isObjectDead(): boolean

检查当前对象是否死亡。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型    | 说明                                        |
  | ------- | ------------------------------------------- |
6052
  | boolean | true:对象死亡,false:对象未死亡。|
W
wanghaoxu 已提交
6053 6054 6055 6056 6057 6058 6059 6060


## RemoteProxy

实现IRemoteObject代理对象。

**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。

6061
| 名称                  | 默认值                      | 说明                              |
W
wanghaoxu 已提交
6062 6063 6064 6065 6066 6067 6068 6069 6070
| --------------------- | ----------------------- | --------------------------------- |
| PING_TRANSACTION      | 1599098439 (0x5f504e47) | 内部指令码,用于测试IPC服务正常。 |
| DUMP_TRANSACTION      | 1598311760 (0x5f444d50) | 内部指令码,获取Binder内部状态。  |
| INTERFACE_TRANSACTION | 1598968902 (0x5f4e5446) | 内部指令码,获取对端接口描述符。  |
| MIN_TRANSACTION_ID    | 1 (0x00000001)          | 最小有效指令码。                  |
| MAX_TRANSACTION_ID    | 16777215 (0x00FFFFFF)   | 最大有效指令码。                  |

### sendRequest<sup>(deprecated)</sup>

6071 6072
>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9)类替代。

C
crazy_hu 已提交
6073
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
W
wanghaoxu 已提交
6074

6075
以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。
W
wanghaoxu 已提交
6076 6077 6078

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
6079
**参数:**
W
wanghaoxu 已提交
6080 6081 6082 6083

  | 参数名  | 类型                            | 必填 | 说明                                                                                   |
  | ------- | ------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                          | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
6084 6085
  | data    | [MessageParcel](#messageparceldeprecated) | 是   | 保存待发送数据的&nbsp;MessageParcel对象。                                              |
  | reply   | [MessageParcel](#messageparceldeprecated) | 是   | 接收应答数据的MessageParcel对象。                                                      |
W
wanghaoxu 已提交
6086 6087 6088 6089 6090 6091
  | options | [MessageOption](#messageoption) | 是   | 本次请求的同异步模式,默认同步调用。                                                   |

**返回值:**

  | 类型    | 说明                                          |
  | ------- | --------------------------------------------- |
6092
  | boolean | true:发送成功,false:发送失败。|
W
wanghaoxu 已提交
6093 6094 6095

**示例:**

6096 6097
  获取服务

Y
Yippo 已提交
6098
  ```ts
W
wanghaoxu 已提交
6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
6115
      "abilityName": "com.ohos.server.EntryAbility",
W
wanghaoxu 已提交
6116 6117
  };
  FA.connectAbility(want, connect);
6118 6119
  ```
  
6120
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息
6121 6122
  
  ```ts
W
wanghaoxu 已提交
6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142
  let option = new rpc.MessageOption();
  let data = rpc.MessageParcel.create();
  let reply = rpc.MessageParcel.create();
  data.writeInt(1);
  data.writeString("hello");
  let ret: boolean = proxy.sendRequest(1, data, reply, option);
  if (ret) {
      console.log("sendRequest got result");
      let msg = reply.readString();
      console.log("RPCTest: reply msg: " + msg);
  } else {
      console.log("RPCTest: sendRequest failed");
  }
  console.log("RPCTest: sendRequest ends, reclaim parcel");
  data.reclaim();
  reply.reclaim();
  ```

### sendMessageRequest<sup>9+</sup>

C
crazy_hu 已提交
6143 6144
sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise&lt;RequestResult&gt;

6145
以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。
W
wanghaoxu 已提交
6146 6147 6148

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
6149
**参数:**
W
wanghaoxu 已提交
6150 6151 6152 6153

  | 参数名  | 类型                            | 必填 | 说明                                                                                   |
  | ------- | ------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                          | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
6154 6155
  | data    | [MessageSequence](#messagesequence9) | 是   | 保存待发送数据的&nbsp;MessageSequence对象。                                            |
  | reply   | [MessageSequence](#messagesequence9)  | 是   | 接收应答数据的MessageSequence对象。                                                    |
W
wanghaoxu 已提交
6156 6157 6158 6159 6160 6161 6162 6163 6164 6165
  | options | [MessageOption](#messageoption) | 是   | 本次请求的同异步模式,默认同步调用。                                                   |

**返回值:**

  | 类型                         | 说明                                      |
  | ---------------------------- | ----------------------------------------- |
  | Promise&lt;RequestResult&gt; | 返回一个期约,兑现值是requestResult实例。 |

**示例:**

6166 6167
  获取服务

Y
Yippo 已提交
6168
  ```ts
W
wanghaoxu 已提交
6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
6185
      "abilityName": "com.ohos.server.EntryAbility",
W
wanghaoxu 已提交
6186 6187
  };
  FA.connectAbility(want, connect);
6188 6189
  ```
  
6190
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息
6191 6192

  ```ts
W
wanghaoxu 已提交
6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218
  let option = new rpc.MessageOption();
  let data = rpc.MessageSequence.create();
  let reply = rpc.MessageSequence.create();
  data.writeInt(1);
  data.writeString("hello");
  proxy.sendMessageRequest(1, data, reply, option)
      .then(function(result) {
          if (result.errCode === 0) {
              console.log("sendMessageRequest got result");
              result.reply.readException();
              let msg = result.reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
              console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode);
          }
      }).catch(function(e) {
          console.log("RPCTest: sendMessageRequest got exception: " + e.message);
      }).finally (() => {
          console.log("RPCTest: sendMessageRequest ends, reclaim parcel");
          data.reclaim();
          reply.reclaim();
      });
  ```

### sendRequest<sup>8+(deprecated)</sup>

6219 6220
>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9)类替代。

C
crazy_hu 已提交
6221
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise&lt;SendRequestResult&gt;
W
wanghaoxu 已提交
6222

6223
以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。
W
wanghaoxu 已提交
6224 6225 6226

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
6227
**参数:**
W
wanghaoxu 已提交
6228 6229 6230 6231

  | 参数名  | 类型                            | 必填 | 说明                                                                                   |
  | ------- | ------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                          | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
6232 6233
  | data    | [MessageParcel](#messageparceldeprecated) | 是   | 保存待发送数据的&nbsp;MessageParcel对象。                                              |
  | reply   | [MessageParcel](#messageparceldeprecated) | 是   | 接收应答数据的MessageParcel对象。                                                      |
W
wanghaoxu 已提交
6234 6235 6236 6237 6238 6239 6240 6241 6242 6243
  | options | [MessageOption](#messageoption) | 是   | 本次请求的同异步模式,默认同步调用。                                                   |

**返回值:**

  | 类型                             | 说明                                          |
  | -------------------------------- | --------------------------------------------- |
  | Promise&lt;SendRequestResult&gt; | 返回一个期约,兑现值是sendRequestResult实例。 |

**示例:**

6244 6245
  获取服务

Y
Yippo 已提交
6246
  ```ts
W
wanghaoxu 已提交
6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
6263
      "abilityName": "com.ohos.server.EntryAbility",
W
wanghaoxu 已提交
6264 6265
  };
  FA.connectAbility(want, connect);
6266 6267
  ```

6268
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息
6269 6270

  ```ts
W
wanghaoxu 已提交
6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298
  let option = new rpc.MessageOption();
  let data = rpc.MessageParcel.create();
  let reply = rpc.MessageParcel.create();
  data.writeInt(1);
  data.writeString("hello");
  proxy.sendRequest(1, data, reply, option)
      .then(function(result) {
          if (result.errCode === 0) {
              console.log("sendRequest got result");
              result.reply.readException();
              let msg = result.reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
              console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
          }
      }).catch(function(e) {
          console.log("RPCTest: sendRequest got exception: " + e.message);
      }).finally (() => {
          console.log("RPCTest: sendRequest ends, reclaim parcel");
          data.reclaim();
          reply.reclaim();
      });
  ```

### sendMessageRequest<sup>9+</sup>

sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback&lt;RequestResult&gt;): void

6299
以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendMessageRequest返回后的某个时机执行回调,回复内容在RequestResult的reply报文里。
W
wanghaoxu 已提交
6300 6301 6302

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
6303
**参数:**
W
wanghaoxu 已提交
6304 6305 6306 6307

  | 参数名   | 类型                               | 必填 | 说明                                                                                   |
  | -------- | ---------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code     | number                             | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
6308 6309
  | data     | [MessageSequence](#messagesequence9) | 是   | 保存待发送数据的&nbsp;MessageSequence对象。                                            |
  | reply    | [MessageSequence](#messagesequence9) | 是   | 接收应答数据的MessageSequence对象。                                                    |
W
wanghaoxu 已提交
6310 6311 6312 6313
  | options  | [MessageOption](#messageoption)    | 是   | 本次请求的同异步模式,默认同步调用。                                                   |
  | callback | AsyncCallback&lt;RequestResult&gt; | 是   | 接收发送结果的回调。                                                                   |

**示例:**
6314 6315
  
  获取服务
W
wanghaoxu 已提交
6316

Y
Yippo 已提交
6317
  ```ts
W
wanghaoxu 已提交
6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
6334
      "abilityName": "com.ohos.server.EntryAbility",
W
wanghaoxu 已提交
6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349
  };
  function sendRequestCallback(result) {
      if (result.errCode === 0) {
          console.log("sendRequest got result");
          result.reply.readException();
          let msg = result.reply.readString();
          console.log("RPCTest: reply msg: " + msg);
      } else {
          console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
      }
      console.log("RPCTest: sendRequest ends, reclaim parcel");
      result.data.reclaim();
      result.reply.reclaim();
  }
  FA.connectAbility(want, connect);
6350 6351
  ```

6352
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息
6353 6354

  ```ts
W
wanghaoxu 已提交
6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366
  let option = new rpc.MessageOption();
  let data = rpc.MessageSequence.create();
  let reply = rpc.MessageSequence.create();
  data.writeInt(1);
  data.writeString("hello");
  try {
      proxy.sendRequest(1, data, reply, option, sendRequestCallback);
  } catch(error) {
      console.info("rpc send sequence request fail, errorCode " + error.code);
      console.info("rpc send sequence request fail, errorMessage " + error.message);
  }
  ```
Z
zengyawen 已提交
6367

W
wanghaoxu 已提交
6368
### sendRequest<sup>8+(deprecated)</sup>
W
wangqinxiao 已提交
6369

6370 6371
>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9)类替代。

W
wanghaoxu 已提交
6372
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback&lt;SendRequestResult&gt;): void
C
crazy_hu 已提交
6373

6374
以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。
Z
zengyawen 已提交
6375

W
wanghaoxu 已提交
6376
**系统能力**:SystemCapability.Communication.IPC.Core
W
wangqinxiao 已提交
6377

W
wanghaoxu 已提交
6378
**参数:**
Z
zengyawen 已提交
6379

W
wanghaoxu 已提交
6380 6381 6382
  | 参数名   | 类型                                   | 必填 | 说明                                                                                   |
  | -------- | -------------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code     | number                                 | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
6383 6384
  | data     | [MessageParcel](#messageparceldeprecated)        | 是   | 保存待发送数据的&nbsp;MessageParcel对象。                                              |
  | reply    | [MessageParcel](#messageparceldeprecated)        | 是   | 接收应答数据的MessageParcel对象。                                                      |
W
wanghaoxu 已提交
6385 6386
  | options  | [MessageOption](#messageoption)        | 是   | 本次请求的同异步模式,默认同步调用。                                                   |
  | callback | AsyncCallback&lt;SendRequestResult&gt; | 是   | 接收发送结果的回调。                                                                   |
Y
yangguangzhao 已提交
6387 6388

**示例:**
Y
yangguangzhao 已提交
6389

6390 6391
  获取服务

Y
Yippo 已提交
6392
  ```ts
Z
zengyawen 已提交
6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
Y
yangguangzhao 已提交
6408
      "bundleName": "com.ohos.server",
6409
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6410
  };
W
wanghaoxu 已提交
6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423
  function sendRequestCallback(result) {
      if (result.errCode === 0) {
          console.log("sendRequest got result");
          result.reply.readException();
          let msg = result.reply.readString();
          console.log("RPCTest: reply msg: " + msg);
      } else {
          console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
      }
      console.log("RPCTest: sendRequest ends, reclaim parcel");
      result.data.reclaim();
      result.reply.reclaim();
  }
Z
zengyawen 已提交
6424
  FA.connectAbility(want, connect);
6425 6426
  ```

6427
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息
6428 6429

  ```ts
Z
zengyawen 已提交
6430 6431 6432 6433 6434
  let option = new rpc.MessageOption();
  let data = rpc.MessageParcel.create();
  let reply = rpc.MessageParcel.create();
  data.writeInt(1);
  data.writeString("hello");
W
wanghaoxu 已提交
6435
  proxy.sendRequest(1, data, reply, option, sendRequestCallback);
Z
zengyawen 已提交
6436 6437
  ```

W
wanghaoxu 已提交
6438
### getLocalInterface<sup>9+</sup>
W
wangqinxiao 已提交
6439

W
wanghaoxu 已提交
6440
getLocalInterface(interface: string): IRemoteBroker
Y
yangguangzhao 已提交
6441

W
wanghaoxu 已提交
6442
查询并获取当前接口描述符对应的本地接口对象。
Y
yangguangzhao 已提交
6443 6444 6445

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
6446
**参数:**
W
wangqinxiao 已提交
6447

W
wanghaoxu 已提交
6448 6449 6450
  | 参数名    | 类型   | 必填 | 说明                   |
  | --------- | ------ | ---- | ---------------------- |
  | interface | string | 是   | 需要查询的接口描述符。 |
Y
yangguangzhao 已提交
6451 6452

**返回值:**
W
wangqinxiao 已提交
6453

W
wanghaoxu 已提交
6454 6455 6456 6457
  | 类型          | 说明                                       |
  | ------------- | ------------------------------------------ |
  | IRemoteBroker | 默认返回Null,标识该接口是一个代理侧接口。 |

C
crazy_hu 已提交
6458 6459 6460 6461 6462 6463 6464
**错误码:**

以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)

  | 错误码ID | 错误信息 |
  | ------- | --------  |
  | 1900006 | only remote object permitted |
Y
yangguangzhao 已提交
6465 6466

**示例:**
Y
yangguangzhao 已提交
6467

6468 6469
  获取服务

Y
Yippo 已提交
6470
  ```ts
Z
zengyawen 已提交
6471 6472 6473 6474 6475 6476 6477
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
W
wanghaoxu 已提交
6478
      onDisconnect: function (elementName) {
Z
zengyawen 已提交
6479 6480 6481 6482 6483 6484 6485
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
W
wanghaoxu 已提交
6486
      "bundleName":"com.ohos.server",
6487
      "abilityName":"com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6488 6489
  };
  FA.connectAbility(want, connect);
6490 6491
  ```

6492
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getLocalInterface接口方法查询接口对象
6493 6494

  ```ts
W
wanghaoxu 已提交
6495 6496 6497 6498 6499 6500 6501
  try {
      let broker = proxy.getLocalInterface("testObject");
      console.log("RpcClient: getLocalInterface is " + broker);
  } catch(error) {
      console.info("rpc get local interface fail, errorCode " + error.code);
      console.info("rpc get local interface fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
6502 6503
  ```

6504
### queryLocalInterface<sup>(deprecated)</sup>
W
wanghaoxu 已提交
6505

6506 6507
>从API version 9 开始不再维护,建议使用[getLocalInterface](#getlocalinterface9)类替代。

W
wanghaoxu 已提交
6508
queryLocalInterface(interface: string): IRemoteBroker
6509

6510
查询并获取当前接口描述符对应的本地接口对象。
6511 6512 6513

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
6514
**参数:**
W
wangqinxiao 已提交
6515

W
wanghaoxu 已提交
6516 6517 6518
  | 参数名    | 类型   | 必填 | 说明                   |
  | --------- | ------ | ---- | ---------------------- |
  | interface | string | 是   | 需要查询的接口描述符。 |
6519 6520

**返回值:**
W
wangqinxiao 已提交
6521

W
wanghaoxu 已提交
6522 6523 6524
  | 类型          | 说明                                       |
  | ------------- | ------------------------------------------ |
  | IRemoteBroker | 默认返回Null,标识该接口是一个代理侧接口。 |
6525 6526 6527

**示例:**

6528 6529
  获取服务

Y
Yippo 已提交
6530
  ```ts
6531 6532 6533 6534 6535 6536 6537
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
W
wanghaoxu 已提交
6538
      onDisconnect: function (elementName) {
6539 6540 6541 6542 6543 6544 6545
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
W
wanghaoxu 已提交
6546
      "bundleName":"com.ohos.server",
6547
      "abilityName":"com.ohos.server.EntryAbility",
6548 6549
  };
  FA.connectAbility(want, connect);
6550 6551
  ```
  
6552
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的queryLocalInterface接口获取接口对象
6553 6554

  ```ts
W
wanghaoxu 已提交
6555 6556
  let broker = proxy.queryLocalInterface("testObject");
  console.log("RpcClient: queryLocalInterface is " + broker);
6557
  ```
Z
zengyawen 已提交
6558

W
wanghaoxu 已提交
6559 6560 6561
### registerDeathRecipient<sup>9+</sup>

registerDeathRecipient(recipient: DeathRecipient, flags: number): void
Z
zengyawen 已提交
6562

W
wanghaoxu 已提交
6563
注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。
Z
zengyawen 已提交
6564

Y
yangguangzhao 已提交
6565
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6566

W
wanghaoxu 已提交
6567
**参数:**
W
wangqinxiao 已提交
6568

W
wanghaoxu 已提交
6569 6570 6571 6572 6573
  | 参数名    | 类型                              | 必填 | 说明           |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | 是   | 要注册的回调。 |
  | flags     | number                            | 是   | 死亡通知标志。 |

W
wanghaoxu 已提交
6574 6575
**错误码:**

C
crazy_hu 已提交
6576
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
6577

W
wanghaoxu 已提交
6578 6579 6580
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900008 | proxy or remote object is invalid |
Z
zengyawen 已提交
6581

Y
yangguangzhao 已提交
6582
**示例:**
Y
yangguangzhao 已提交
6583

6584 6585
  获取服务

Y
Yippo 已提交
6586
  ```ts
Z
zengyawen 已提交
6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
Y
yangguangzhao 已提交
6602
      "bundleName": "com.ohos.server",
6603
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6604
  };
W
wanghaoxu 已提交
6605
  FA.connectAbility(want, connect);
6606 6607
  ```

6608
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的registerDeathRecipient接口注册死亡回调
6609 6610
  
  ```ts
W
wanghaoxu 已提交
6611 6612 6613
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
Z
zengyawen 已提交
6614 6615
      }
  }
W
wanghaoxu 已提交
6616 6617
  let deathRecipient = new MyDeathRecipient();
  try {
6618
      proxy.registerDeathRecipient(deathRecipient, 0);
W
wanghaoxu 已提交
6619
  } catch(error) {
C
crazy_hu 已提交
6620 6621
      console.info("proxy register deathRecipient fail, errorCode " + error.code);
      console.info("proxy register deathRecipient fail, errorMessage " + error.message);
W
wanghaoxu 已提交
6622
  }
Z
zengyawen 已提交
6623 6624
  ```

6625
### addDeathRecippient<sup>(deprecated)</sup>
Z
zengyawen 已提交
6626

6627 6628
>从API version 9 开始不再维护,建议使用[registerDeathRecipient](#registerdeathrecipient9)类替代。

C
crazy_hu 已提交
6629
addDeathRecipient(recipient: DeathRecipient, flags: number): boolean
Z
zengyawen 已提交
6630

6631
注册用于接收远程对象死亡通知的回调,增加proxy对象上的死亡通知。
Z
zengyawen 已提交
6632

Y
yangguangzhao 已提交
6633
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6634

W
wanghaoxu 已提交
6635
**参数:**
W
wangqinxiao 已提交
6636

W
wanghaoxu 已提交
6637 6638 6639 6640
  | 参数名    | 类型                              | 必填 | 说明                              |
  | --------- | --------------------------------- | ---- | --------------------------------- |
  | recipient | [DeathRecipient](#deathrecipient) | 是   | 收件人表示要注册的回调。          |
  | flags     | number                            | 是   | 死亡通知标志。保留参数。设置为0。 |
Z
zengyawen 已提交
6641

Y
yangguangzhao 已提交
6642
**返回值:**
W
wangqinxiao 已提交
6643

W
wanghaoxu 已提交
6644 6645
  | 类型    | 说明                                          |
  | ------- | --------------------------------------------- |
6646
  | boolean | true:回调注册成功,false:回调注册失败。|
Z
zengyawen 已提交
6647

Y
yangguangzhao 已提交
6648
**示例:**
Y
yangguangzhao 已提交
6649

6650 6651
  获取服务

Y
Yippo 已提交
6652
  ```ts
Z
zengyawen 已提交
6653 6654 6655 6656 6657 6658 6659
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
W
wanghaoxu 已提交
6660
      onDisconnect: function(elementName) {
Z
zengyawen 已提交
6661 6662 6663 6664 6665 6666 6667
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
W
wanghaoxu 已提交
6668
      "bundleName": "com.ohos.server",
6669
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6670 6671
  };
  FA.connectAbility(want, connect);
6672 6673
  ```
  
6674
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的addDeathRecippient接口方法新增死亡回调
6675 6676

  ```ts
W
wanghaoxu 已提交
6677 6678 6679 6680 6681 6682
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  let deathRecipient = new MyDeathRecipient();
6683
  proxy.addDeathRecipient(deathRecipient, 0);
Z
zengyawen 已提交
6684 6685
  ```

W
wanghaoxu 已提交
6686
### unregisterDeathRecipient<sup>9+</sup>
Z
zengyawen 已提交
6687

C
crazy_hu 已提交
6688
unregisterDeathRecipient(recipient: DeathRecipient, flags: number): boolean
Z
zengyawen 已提交
6689

W
wanghaoxu 已提交
6690
注销用于接收远程对象死亡通知的回调。
Z
zengyawen 已提交
6691

Y
yangguangzhao 已提交
6692
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6693

W
wanghaoxu 已提交
6694
**参数:**
W
wangqinxiao 已提交
6695

W
wanghaoxu 已提交
6696 6697 6698 6699
  | 参数名    | 类型                              | 必填 | 说明           |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | 是   | 要注销的回调。 |
  | flags     | number                            | 是   | 死亡通知标志。 |
Z
zengyawen 已提交
6700

W
wanghaoxu 已提交
6701 6702
**错误码:**

C
crazy_hu 已提交
6703
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wangqinxiao 已提交
6704

W
wanghaoxu 已提交
6705 6706 6707
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900008 | proxy or remote object is invalid |
Z
zengyawen 已提交
6708

Y
yangguangzhao 已提交
6709
**示例:**
Y
yangguangzhao 已提交
6710

6711 6712
  获取服务

Y
Yippo 已提交
6713
  ```ts
Z
zengyawen 已提交
6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
Y
yangguangzhao 已提交
6729
      "bundleName": "com.ohos.server",
6730
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6731 6732
  };
  FA.connectAbility(want, connect);
6733 6734
  ```
  
6735
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的unregisterDeathRecipient接口方法注销死亡回调
6736 6737

  ```ts
Z
zengyawen 已提交
6738 6739
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
6740
          console.log("server died");
Z
zengyawen 已提交
6741 6742 6743
      }
  }
  let deathRecipient = new MyDeathRecipient();
W
wanghaoxu 已提交
6744 6745 6746 6747
  try {
      proxy.registerDeathRecippient(deathRecipient, 0);
      proxy.unregisterDeathRecippient(deathRecipient, 0);
  } catch(error) {
C
crazy_hu 已提交
6748 6749
      console.info("proxy register deathRecipient fail, errorCode " + error.code);
      console.info("proxy register deathRecipient fail, errorMessage " + error.message);
W
wanghaoxu 已提交
6750
  }
Z
zengyawen 已提交
6751 6752
  ```

6753
### removeDeathRecipient<sup>(deprecated)</sup>
Z
zengyawen 已提交
6754

6755 6756
>从API version 9 开始不再维护,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9)类替代。

C
crazy_hu 已提交
6757
removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean
Z
zengyawen 已提交
6758

6759
注销用于接收远程对象死亡通知的回调。
Z
zengyawen 已提交
6760

Y
yangguangzhao 已提交
6761
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6762

W
wanghaoxu 已提交
6763
**参数:**
W
wangqinxiao 已提交
6764

W
wanghaoxu 已提交
6765 6766 6767 6768
  | 参数名    | 类型                              | 必填 | 说明                              |
  | --------- | --------------------------------- | ---- | --------------------------------- |
  | recipient | [DeathRecipient](#deathrecipient) | 是   | 要注销的死亡回调。                |
  | flags     | number                            | 是   | 死亡通知标志。保留参数。设置为0。 |
Z
zengyawen 已提交
6769

Y
yangguangzhao 已提交
6770
**返回值:**
W
wangqinxiao 已提交
6771

W
wanghaoxu 已提交
6772 6773
  | 类型    | 说明                                          |
  | ------- | --------------------------------------------- |
6774
  | boolean | true:回调注销成功,false:回调注销失败。|
Z
zengyawen 已提交
6775

Y
yangguangzhao 已提交
6776
**示例:**
Y
yangguangzhao 已提交
6777

6778 6779
  获取服务

Y
Yippo 已提交
6780
  ```ts
Z
zengyawen 已提交
6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
Y
yangguangzhao 已提交
6796
      "bundleName": "com.ohos.server",
6797
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6798 6799
  };
  FA.connectAbility(want, connect);
6800 6801
  ```
  
6802
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的removeDeathRecipient接口方法去注册死亡回调
6803 6804

  ```ts
Z
zengyawen 已提交
6805 6806
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
6807
          console.log("server died");
Z
zengyawen 已提交
6808 6809 6810 6811 6812 6813 6814
      }
  }
  let deathRecipient = new MyDeathRecipient();
  proxy.addDeathRecippient(deathRecipient, 0);
  proxy.removeDeathRecipient(deathRecipient, 0);
  ```

W
wanghaoxu 已提交
6815
### getDescriptor<sup>9+</sup>
Z
zengyawen 已提交
6816

C
crazy_hu 已提交
6817
getDescriptor(): string
Z
zengyawen 已提交
6818

6819
获取对象的接口描述符,接口描述符为字符串。
Z
zengyawen 已提交
6820

Y
yangguangzhao 已提交
6821
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6822

Y
yangguangzhao 已提交
6823
**返回值:**
W
wangqinxiao 已提交
6824

W
wanghaoxu 已提交
6825 6826 6827 6828
  | 类型   | 说明             |
  | ------ | ---------------- |
  | string | 返回接口描述符。 |

W
wanghaoxu 已提交
6829 6830
**错误码:**

C
crazy_hu 已提交
6831
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
6832

W
wanghaoxu 已提交
6833 6834 6835
  | 错误码ID | 错误信息 |
  | -------- | ------- |
  | 1900008 | proxy or remote object is invalid |
C
crazy_hu 已提交
6836
  | 1900007 | communication failed              |
W
wanghaoxu 已提交
6837 6838 6839

**示例:**

6840 6841
  获取服务

Y
Yippo 已提交
6842
  ```ts
W
wanghaoxu 已提交
6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
6859
      "abilityName": "com.ohos.server.EntryAbility",
W
wanghaoxu 已提交
6860 6861
  };
  FA.connectAbility(want, connect);
6862
  ```
6863
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getDescriptor接口方法获取对象的接口描述符
6864 6865

  ```ts
W
wanghaoxu 已提交
6866 6867 6868 6869 6870 6871 6872 6873 6874
  try {
      let descriptor = proxy.getDescriptor();
      console.log("RpcClient: descriptor is " + descriptor);
  } catch(error) {
      console.info("rpc get interface descriptor fail, errorCode " + error.code);
      console.info("rpc get interface descriptor fail, errorMessage " + error.message);
  }
  ```

6875
### getInterfaceDescriptor<sup>(deprecated)</sup>
W
wanghaoxu 已提交
6876

6877 6878
>从API version 9 开始不再维护,建议使用[getDescriptor](#getdescriptor9)类替代。

W
wanghaoxu 已提交
6879 6880
getInterfaceDescriptor(): string

6881
查询当前代理对象接口的描述符。
W
wanghaoxu 已提交
6882 6883 6884 6885 6886 6887 6888

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明               |
  | ------ | ------------------ |
Y
yangguangzhao 已提交
6889
  | string | 当前的接口描述符。 |
Z
zengyawen 已提交
6890

Y
yangguangzhao 已提交
6891
**示例:**
Y
yangguangzhao 已提交
6892

6893 6894
  获取服务

Y
Yippo 已提交
6895
  ```ts
Z
zengyawen 已提交
6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
Y
yangguangzhao 已提交
6911
      "bundleName": "com.ohos.server",
6912
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6913 6914
  };
  FA.connectAbility(want, connect);
6915 6916
  ```
  
6917
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getInterfaceDescriptor接口方法查询当前代理对象接口的描述符
6918 6919

  ```ts
Z
zengyawen 已提交
6920 6921 6922 6923 6924 6925 6926 6927 6928 6929
  let descriptor = proxy.getInterfaceDescriptor();
  console.log("RpcClient: descriptor is " + descriptor);
  ```

### isObjectDead

isObjectDead(): boolean

指示对应的RemoteObject是否死亡。

Y
yangguangzhao 已提交
6930
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6931

Y
yangguangzhao 已提交
6932
**返回值:**
W
wangqinxiao 已提交
6933

W
wanghaoxu 已提交
6934 6935
  | 类型    | 说明                                                      |
  | ------- | --------------------------------------------------------- |
6936
  | boolean | true:对应的对象已经死亡,false:对应的对象未死亡|
Z
zengyawen 已提交
6937

Y
yangguangzhao 已提交
6938
**示例:**
Y
yangguangzhao 已提交
6939

6940 6941
  获取服务

Y
Yippo 已提交
6942
  ```ts
Z
zengyawen 已提交
6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957
  import FA from "@ohos.ability.featureAbility";
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
Y
yangguangzhao 已提交
6958
      "bundleName": "com.ohos.server",
6959
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6960 6961
  };
  FA.connectAbility(want, connect);
6962 6963
  ```

6964
  上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的isObjectDead接口方法判断当前对象是否已经死亡
6965 6966

  ```ts
Z
zengyawen 已提交
6967 6968 6969 6970 6971 6972 6973 6974
  let isDead = proxy.isObjectDead();
  console.log("RpcClient: isObjectDead is " + isDead);
  ```

## MessageOption

公共消息选项(int标志,int等待时间),使用标志中指定的标志构造指定的MessageOption对象。

Y
yangguangzhao 已提交
6975
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。
Z
zengyawen 已提交
6976

6977
  | 名称          | 默认值   | 说明                                                        |
W
wanghaoxu 已提交
6978
  | ------------- | ---- | ----------------------------------------------------------- |
6979 6980
  | TF_SYNC       | 0    | 同步调用标识。                                                  |
  | TF_ASYNC      | 1    | 异步调用标识。                                                  |
W
wanghaoxu 已提交
6981
  | TF_ACCEPT_FDS | 0x10 | 指示sendMessageRequest<sup>9+</sup>接口可以返回文件描述符。 |
6982
  | TF_WAIT_TIME  | 8    | 默认等待时间(单位/秒)。                                          |
W
wanghaoxu 已提交
6983 6984 6985 6986 6987 6988 6989 6990 6991 6992


### constructor<sup>9+</sup>

constructor(async?: boolean);

MessageOption构造函数。

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
6993
**参数:**
W
wanghaoxu 已提交
6994 6995 6996 6997

  | 参数名    | 类型   | 必填 | 说明                                   |
  | --------- | ------ | ---- | -------------------------------------- |
  | syncFlags | number | 否   | 同步调用或异步调用标志。默认同步调用。 |
Z
zengyawen 已提交
6998 6999


7000 7001
**示例:**

Y
Yippo 已提交
7002
  ```ts
7003 7004 7005 7006 7007 7008 7009
  class TestRemoteObject extends rpc.MessageOption {
    constructor(async) {
        super(async);
    }
  }
  ```

Z
zengyawen 已提交
7010 7011
### constructor

C
crazy_hu 已提交
7012
constructor(syncFlags?: number, waitTime?: number)
Z
zengyawen 已提交
7013 7014 7015

MessageOption构造函数。

Y
yangguangzhao 已提交
7016 7017
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7018
**参数:**
W
wangqinxiao 已提交
7019

W
wanghaoxu 已提交
7020 7021 7022 7023 7024
  | 参数名    | 类型   | 必填 | 说明                                          |
  | --------- | ------ | ---- | --------------------------------------------- |
  | syncFlags | number | 否   | 同步调用或异步调用标志。默认同步调用。        |
  | waitTime  | number | 否   | 调用rpc最长等待时间。默认&nbsp;TF_WAIT_TIME。 |

7025
**示例:**
W
wanghaoxu 已提交
7026

Y
Yippo 已提交
7027
  ```ts
7028 7029 7030 7031 7032 7033
  class TestRemoteObject extends rpc.MessageOption {
    constructor(syncFlags,waitTime) {
        super(syncFlags,waitTime);
    }
  }
  ```
W
wanghaoxu 已提交
7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045
### isAsync<sup>9+</sup>

isAsync(): boolean;

获取SendMessageRequest调用中确定同步或是异步的标志。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型    | 说明                                 |
  | ------- | ------------------------------------ |
7046
  | boolean | true:同步调用成功,false:异步调用成功。 |
W
wanghaoxu 已提交
7047

7048 7049
**示例:**

Y
Yippo 已提交
7050
  ```ts
7051 7052 7053
  let option = new rpc.MessageOption();
  let isAsync = option.isAsync();
  ```
W
wanghaoxu 已提交
7054 7055 7056 7057 7058 7059

### setAsync<sup>9+</sup>

setAsync(async: boolean): void;

设置SendMessageRequest调用中确定同步或是异步的标志。
Z
zengyawen 已提交
7060

W
wanghaoxu 已提交
7061 7062
**系统能力**:SystemCapability.Communication.IPC.Core

7063 7064
**示例:**

Y
Yippo 已提交
7065
  ```ts
7066 7067 7068 7069
  let option = new rpc.MessageOption();
  let setAsync = option.setAsync(true);
  console.log("Set synchronization flag");
  ```
Z
zengyawen 已提交
7070 7071 7072 7073 7074 7075 7076

### getFlags

getFlags(): number

获取同步调用或异步调用标志。

Y
yangguangzhao 已提交
7077
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7078

Y
yangguangzhao 已提交
7079
**返回值:**
W
wangqinxiao 已提交
7080

W
wanghaoxu 已提交
7081 7082
  | 类型   | 说明                                 |
  | ------ | ------------------------------------ |
Y
yangguangzhao 已提交
7083
  | number | 调用成功返回同步调用或异步调用标志。 |
Z
zengyawen 已提交
7084

7085
**示例:**
Z
zengyawen 已提交
7086

Y
Yippo 已提交
7087
  ```ts
7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100
  try {
      let option = new rpc.MessageOption();
      console.info("create object successfully.");
      let flog = option.getFlags();
      console.info("run getFlags success, flog is " + flog);
      option.setFlags(1)
      console.info("run setFlags success");
      let flog2 = option.getFlags();
      console.info("run getFlags success, flog2 is " + flog2);
  } catch (error) {
      console.info("error " + error);
  }
  ```
7101

Z
zengyawen 已提交
7102 7103 7104 7105 7106 7107
### setFlags

setFlags(flags: number): void

设置同步调用或异步调用标志。

Y
yangguangzhao 已提交
7108 7109
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7110
**参数:**
W
wangqinxiao 已提交
7111

W
wanghaoxu 已提交
7112 7113 7114
  | 参数名 | 类型   | 必填 | 说明                     |
  | ------ | ------ | ---- | ------------------------ |
  | flags  | number | 是   | 同步调用或异步调用标志。 |
Z
zengyawen 已提交
7115

7116 7117
**示例:**

Y
Yippo 已提交
7118
  ```ts
7119 7120 7121 7122 7123 7124 7125 7126 7127 7128
  try {
      let option = new rpc.MessageOption();
      option.setFlags(1)
      console.info("run setFlags success");
      let flog = option.getFlags();
      console.info("run getFlags success, flog is " + flog);
  } catch (error) {
      console.info("error " + error);
  }
  ```
Z
zengyawen 已提交
7129 7130 7131 7132 7133 7134 7135

### getWaitTime

getWaitTime(): number

获取rpc调用的最长等待时间。

Y
yangguangzhao 已提交
7136 7137
**系统能力**:SystemCapability.Communication.IPC.Core

Y
yangguangzhao 已提交
7138
**返回值:**
W
wangqinxiao 已提交
7139

W
wanghaoxu 已提交
7140 7141
  | 类型   | 说明              |
  | ------ | ----------------- |
Y
yangguangzhao 已提交
7142
  | number | rpc最长等待时间。 |
Z
zengyawen 已提交
7143

7144 7145
**示例:**

Y
Yippo 已提交
7146
  ```ts
7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157
  try {
      let option = new rpc.MessageOption();
      let time = option.getWaitTime();
      console.info("run getWaitTime success");
      option.setWaitTime(16);
      let time2 = option.getWaitTime();
      console.info("run getWaitTime success, time is " + time);
  } catch (error) {
      console.info("error " + error);
  }
  ```
Z
zengyawen 已提交
7158 7159 7160 7161 7162 7163 7164

### setWaitTime

setWaitTime(waitTime: number): void

设置rpc调用最长等待时间。

Y
yangguangzhao 已提交
7165 7166
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7167
**参数:**
W
wangqinxiao 已提交
7168

W
wanghaoxu 已提交
7169 7170 7171
  | 参数名   | 类型   | 必填 | 说明                  |
  | -------- | ------ | ---- | --------------------- |
  | waitTime | number | 是   | rpc调用最长等待时间。 |
Z
zengyawen 已提交
7172

7173 7174
**示例:**

Y
Yippo 已提交
7175
  ```ts
7176 7177 7178 7179 7180 7181 7182 7183 7184
  try {
      let option = new rpc.MessageOption();
      option.setWaitTime(16);
      let time = option.getWaitTime();
      console.info("run getWaitTime success, time is " + time);
  } catch (error) {
      console.info("error " + error);
  }
  ```
Z
zengyawen 已提交
7185 7186 7187 7188 7189 7190 7191 7192 7193

## IPCSkeleton

用于获取IPC上下文信息,包括获取UID和PID、获取本端和对端设备ID、检查接口调用是否在同一设备上。

### getContextObject

static getContextObject(): IRemoteObject

7194
静态方法,获取系统能力的管理者。
Z
zengyawen 已提交
7195

Y
yangguangzhao 已提交
7196
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7197

Y
yangguangzhao 已提交
7198
**返回值:**
W
wangqinxiao 已提交
7199

W
wanghaoxu 已提交
7200 7201
  | 类型                            | 说明                 |
  | ------------------------------- | -------------------- |
Y
yangguangzhao 已提交
7202
  | [IRemoteObject](#iremoteobject) | 返回系统能力管理者。 |
Z
zengyawen 已提交
7203

Y
yangguangzhao 已提交
7204
**示例:**
Y
yangguangzhao 已提交
7205

Y
Yippo 已提交
7206
  ```ts
Z
zengyawen 已提交
7207 7208 7209 7210 7211 7212 7213 7214
  let samgr = rpc.IPCSkeleton.getContextObject();
  console.log("RpcServer: getContextObject result: " + samgr);
  ```

### getCallingPid

static getCallingPid(): number

7215
静态方法,获取调用者的PID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的PID。
Z
zengyawen 已提交
7216

Y
yangguangzhao 已提交
7217
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7218

Y
yangguangzhao 已提交
7219
**返回值:**
W
wangqinxiao 已提交
7220

W
wanghaoxu 已提交
7221 7222
  | 类型   | 说明              |
  | ------ | ----------------- |
Y
yangguangzhao 已提交
7223
  | number | 返回调用者的PID。 |
Z
zengyawen 已提交
7224

Y
yangguangzhao 已提交
7225
**示例:**
Y
yangguangzhao 已提交
7226

Y
Yippo 已提交
7227
  ```ts
Z
zengyawen 已提交
7228
  class Stub extends rpc.RemoteObject {
7229
      onRemoteMessageRequest(code, data, reply, option) {
Z
zengyawen 已提交
7230 7231 7232 7233
          let callerPid = rpc.IPCSkeleton.getCallingPid();
          console.log("RpcServer: getCallingPid result: " + callerPid);
          return true;
      }
W
wanghaoxu 已提交
7234
 }
Z
zengyawen 已提交
7235 7236 7237 7238 7239 7240
  ```

### getCallingUid

static getCallingUid(): number

7241
静态方法,获取调用者的UID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的UID。
Z
zengyawen 已提交
7242

Y
yangguangzhao 已提交
7243
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7244

Y
yangguangzhao 已提交
7245
**返回值:**
W
wangqinxiao 已提交
7246

W
wanghaoxu 已提交
7247 7248
  | 类型   | 说明              |
  | ------ | ----------------- |
Y
yangguangzhao 已提交
7249
  | number | 返回调用者的UID。 |
Z
zengyawen 已提交
7250

Y
yangguangzhao 已提交
7251
**示例:**
Y
yangguangzhao 已提交
7252

Y
Yippo 已提交
7253
  ```ts
Z
zengyawen 已提交
7254
  class Stub extends rpc.RemoteObject {
7255
      onRemoteMessageRequest(code, data, reply, option) {
Z
zengyawen 已提交
7256 7257 7258 7259 7260 7261 7262
          let callerUid = rpc.IPCSkeleton.getCallingUid();
          console.log("RpcServer: getCallingUid result: " + callerUid);
          return true;
      }
  }
  ```

Y
yangguangzhao 已提交
7263
### getCallingTokenId<sup>8+</sup>
Y
yangguangzhao 已提交
7264 7265 7266

static getCallingTokenId(): number;

7267
静态方法,获取调用者的TokenId,用于被调用方对调用方的身份校验。
Y
yangguangzhao 已提交
7268

7269
**系统能力**:SystemCapability.Communication.IPC.Core
Y
yangguangzhao 已提交
7270

C
crazy_hu 已提交
7271
**返回值:**
7272

C
crazy_hu 已提交
7273 7274 7275
   | 类型   | 说明                  |
   | ------ | --------------------- |
   | number | 返回调用者的TokenId。 |
7276

C
crazy_hu 已提交
7277
**示例:**
Y
yangguangzhao 已提交
7278

Y
Yippo 已提交
7279
  ```ts
Y
yangguangzhao 已提交
7280
  class Stub extends rpc.RemoteObject {
7281
      onRemoteMessageRequest(code, data, reply, option) {
Y
yangguangzhao 已提交
7282 7283 7284 7285 7286 7287 7288
          let callerTokenId = rpc.IPCSkeleton.getCallingTokenId();
          console.log("RpcServer: getCallingTokenId result: " + callerTokenId);
          return true;
      }
  }
  ```

Z
zengyawen 已提交
7289

Y
yangguangzhao 已提交
7290
### getCallingDeviceID
Z
zengyawen 已提交
7291 7292 7293

static getCallingDeviceID(): string

7294
静态方法,获取调用者进程所在的设备ID。
Z
zengyawen 已提交
7295

Y
yangguangzhao 已提交
7296
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7297

Y
yangguangzhao 已提交
7298
**返回值:**
W
wangqinxiao 已提交
7299

W
wanghaoxu 已提交
7300 7301
  | 类型   | 说明                         |
  | ------ | ---------------------------- |
Y
yangguangzhao 已提交
7302
  | string | 返回调用者进程所在的设备ID。 |
Z
zengyawen 已提交
7303

Y
yangguangzhao 已提交
7304
**示例:**
Y
yangguangzhao 已提交
7305

Y
Yippo 已提交
7306
  ```ts
Z
zengyawen 已提交
7307
  class Stub extends rpc.RemoteObject {
7308
      onRemoteMessageRequest(code, data, reply, option) {
Y
yangguangzhao 已提交
7309
          let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID();
Z
zengyawen 已提交
7310 7311 7312 7313 7314 7315 7316 7317 7318 7319
          console.log("RpcServer: callerDeviceID is: " + callerDeviceID);
          return true;
      }
  }
  ```

### getLocalDeviceID

static getLocalDeviceID(): string

7320
静态方法,获取本端设备ID。
Z
zengyawen 已提交
7321

Y
yangguangzhao 已提交
7322
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7323

Y
yangguangzhao 已提交
7324
**返回值:**
W
wangqinxiao 已提交
7325

W
wanghaoxu 已提交
7326 7327
  | 类型   | 说明               |
  | ------ | ------------------ |
Y
yangguangzhao 已提交
7328
  | string | 返回本地设备的ID。 |
Z
zengyawen 已提交
7329

Y
yangguangzhao 已提交
7330
**示例:**
Y
yangguangzhao 已提交
7331

Y
Yippo 已提交
7332
  ```ts
Z
zengyawen 已提交
7333
  class Stub extends rpc.RemoteObject {
7334
      onRemoteMessageRequest(code, data, reply, option) {
Z
zengyawen 已提交
7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345
          let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID();
          console.log("RpcServer: localDeviceID is: " + localDeviceID);
          return true;
      }
  }
  ```

### isLocalCalling

static isLocalCalling(): boolean

7346
静态方法,检查当前通信对端是否是本设备的进程。
Z
zengyawen 已提交
7347

Y
yangguangzhao 已提交
7348
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7349

Y
yangguangzhao 已提交
7350
**返回值:**
W
wangqinxiao 已提交
7351

W
wanghaoxu 已提交
7352 7353
  | 类型    | 说明                                                      |
  | ------- | --------------------------------------------------------- |
7354
  | boolean | true:调用在同一台设备,false:调用未在同一台设备。|
Z
zengyawen 已提交
7355

Y
yangguangzhao 已提交
7356
**示例:**
Y
yangguangzhao 已提交
7357

Y
Yippo 已提交
7358
  ```ts
Z
zengyawen 已提交
7359
  class Stub extends rpc.RemoteObject {
7360
      onRemoteMessageRequest(code, data, reply, option) {
Z
zengyawen 已提交
7361 7362 7363 7364 7365 7366 7367
          let isLocalCalling = rpc.IPCSkeleton.isLocalCalling();
          console.log("RpcServer: isLocalCalling is: " + isLocalCalling);
          return true;
      }
  }
  ```

W
wanghaoxu 已提交
7368
### flushCmdBuffer<sup>9+</sup>
Z
zengyawen 已提交
7369

C
crazy_hu 已提交
7370
static flushCmdBuffer(object: IRemoteObject): void
Z
zengyawen 已提交
7371

7372
静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在执行任何时间敏感操作之前调用此方法。
Z
zengyawen 已提交
7373

Y
yangguangzhao 已提交
7374 7375
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7376
**参数:**
W
wanghaoxu 已提交
7377 7378 7379 7380 7381 7382 7383 7384

  | 参数名 | 类型                            | 必填 | 说明                |
  | ------ | ------------------------------- | ---- | ------------------- |
  | object | [IRemoteObject](#iremoteobject) | 是   | 指定的RemoteProxy。 |


**示例:**

Y
Yippo 已提交
7385
  ```ts
W
wanghaoxu 已提交
7386 7387 7388 7389 7390 7391 7392 7393 7394
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
  let remoteObject = new TestRemoteObject("aaa");
  try {
      rpc.IPCSkeleton.flushCmdBuffer(remoteObject);
  } catch(error) {
C
crazy_hu 已提交
7395 7396
      console.info("proxy set calling identity fail, errorCode " + error.code);
      console.info("proxy set calling identity fail, errorMessage " + error.message);
W
wanghaoxu 已提交
7397 7398
  }
  ```
W
wangqinxiao 已提交
7399

7400
### flushCommands<sup>(deprecated)</sup>
W
wanghaoxu 已提交
7401

7402 7403
>从API version 9 开始不再维护,建议使用[flushCmdBuffer](#flushcmdbuffer9)类替代。

C
crazy_hu 已提交
7404
static flushCommands(object: IRemoteObject): number
W
wanghaoxu 已提交
7405

7406
静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在执行任何时间敏感操作之前调用此方法。
W
wanghaoxu 已提交
7407 7408 7409

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7410
**参数:**
W
wanghaoxu 已提交
7411 7412 7413 7414

  | 参数名 | 类型                            | 必填 | 说明                |
  | ------ | ------------------------------- | ---- | ------------------- |
  | object | [IRemoteObject](#iremoteobject) | 是   | 指定的RemoteProxy。 |
Z
zengyawen 已提交
7415

Y
yangguangzhao 已提交
7416
**返回值:**
W
wangqinxiao 已提交
7417

W
wanghaoxu 已提交
7418 7419
  | 类型   | 说明                                                                              |
  | ------ | --------------------------------------------------------------------------------- |
Y
yangguangzhao 已提交
7420
  | number | 如果操作成功,返回0;如果输入对象为空或RemoteObject,或者操作失败,返回错误代码。 |
Z
zengyawen 已提交
7421

Y
yangguangzhao 已提交
7422
**示例:**
Y
yangguangzhao 已提交
7423

Y
Yippo 已提交
7424
  ```ts
Y
yangguangzhao 已提交
7425 7426
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
7427
          console.log("server died");
Y
yangguangzhao 已提交
7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444
      }
  }
  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;
      }
  }
  let remoteObject = new TestRemoteObject("aaa");
Z
zengyawen 已提交
7445 7446 7447 7448 7449 7450 7451 7452
  let ret = rpc.IPCSkeleton.flushCommands(remoteObject);
  console.log("RpcServer: flushCommands result: " + ret);
  ```

### resetCallingIdentity

static resetCallingIdentity(): string

7453
静态方法,将远程用户的UID和PID替换为本地用户的UID和PID。它可以用于身份验证等场景。
Z
zengyawen 已提交
7454

Y
yangguangzhao 已提交
7455
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7456

Y
yangguangzhao 已提交
7457
**返回值:**
W
wangqinxiao 已提交
7458

W
wanghaoxu 已提交
7459 7460
  | 类型   | 说明                                 |
  | ------ | ------------------------------------ |
Y
yangguangzhao 已提交
7461
  | string | 返回包含远程用户的UID和PID的字符串。 |
Z
zengyawen 已提交
7462

Y
yangguangzhao 已提交
7463
**示例:**
Y
yangguangzhao 已提交
7464

Y
Yippo 已提交
7465
  ```ts
Z
zengyawen 已提交
7466
  class Stub extends rpc.RemoteObject {
7467
      onRemoteMessageRequest(code, data, reply, option) {
Z
zengyawen 已提交
7468 7469 7470 7471 7472 7473 7474 7475
          let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity();
          console.log("RpcServer: callingIdentity is: " + callingIdentity);
          return true;
      }
  }
  ```


W
wanghaoxu 已提交
7476 7477
### restoreCallingIdentity<sup>9+</sup>

C
crazy_hu 已提交
7478
static restoreCallingIdentity(identity: string): void
W
wanghaoxu 已提交
7479

7480
静态方法,将远程用户的UID和PID替换为本地用户的UID和PID。它可以用于身份验证等场景。
W
wanghaoxu 已提交
7481 7482 7483

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7484
**参数:**
W
wanghaoxu 已提交
7485 7486 7487 7488 7489 7490 7491

  | 参数名   | 类型   | 必填 | 说明                                                               |
  | -------- | ------ | ---- | ------------------------------------------------------------------ |
  | identity | string | 是   | 标识表示包含远程用户UID和PID的字符串。由resetCallingIdentity返回。 |

**示例:**

Y
Yippo 已提交
7492
  ```ts
W
wanghaoxu 已提交
7493
  class Stub extends rpc.RemoteObject {
7494
      onRemoteMessageRequest(code, data, reply, option) {
W
wanghaoxu 已提交
7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506
          let callingIdentity = null;
          try {
              callingIdentity = rpc.IPCSkeleton.resetCallingIdentity();
              console.log("RpcServer: callingIdentity is: " + callingIdentity);
          } finally {
              rpc.IPCSkeleton.restoreCallingIdentity("callingIdentity ");
          }
          return true;
      }
  }
  ```

7507
### setCallingIdentity<sup>(deprecated)</sup>
Z
zengyawen 已提交
7508

7509 7510
>从API version 9 开始不再维护,建议使用[restoreCallingIdentity](#restorecallingidentity9)类替代。

C
crazy_hu 已提交
7511
static setCallingIdentity(identity: string): boolean
Z
zengyawen 已提交
7512

7513
静态方法,将UID和PID恢复为远程用户的UID和PID。它通常在使用resetCallingIdentity后调用,需要resetCallingIdentity返回的远程用户的UID和PID。
Z
zengyawen 已提交
7514

Y
yangguangzhao 已提交
7515 7516
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7517
**参数:**
W
wangqinxiao 已提交
7518

W
wanghaoxu 已提交
7519 7520 7521
  | 参数名   | 类型   | 必填 | 说明                                                               |
  | -------- | ------ | ---- | ------------------------------------------------------------------ |
  | identity | string | 是   | 标识表示包含远程用户UID和PID的字符串。由resetCallingIdentity返回。 |
Z
zengyawen 已提交
7522

Y
yangguangzhao 已提交
7523
**返回值:**
W
wangqinxiao 已提交
7524

W
wanghaoxu 已提交
7525 7526
  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
7527
  | boolean | true:设置成功,false:设置失败。|
Z
zengyawen 已提交
7528

Y
yangguangzhao 已提交
7529
**示例:**
Y
yangguangzhao 已提交
7530

Y
Yippo 已提交
7531
  ```ts
Z
zengyawen 已提交
7532
  class Stub extends rpc.RemoteObject {
7533
      onRemoteMessageRequest(code, data, reply, option) {
Z
zengyawen 已提交
7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556
          let callingIdentity = null;
          try {
              callingIdentity = rpc.IPCSkeleton.resetCallingIdentity();
              console.log("RpcServer: callingIdentity is: " + callingIdentity);
          } finally {
              let ret = rpc.IPCSkeleton.setCallingIdentity("callingIdentity ");
              console.log("RpcServer: setCallingIdentity is: " + ret);
          }
          return true;
      }
  }
  ```

## RemoteObject

实现远程对象。服务提供者必须继承此类。

### constructor

constructor(descriptor: string)

RemoteObject构造函数。

Y
yangguangzhao 已提交
7557 7558
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7559
**参数:**
W
wangqinxiao 已提交
7560

W
wanghaoxu 已提交
7561 7562 7563
  | 参数名     | 类型   | 必填 | 说明         |
  | ---------- | ------ | ---- | ------------ |
  | descriptor | string | 是   | 接口描述符。 |
Z
zengyawen 已提交
7564 7565


Y
yangguangzhao 已提交
7566
### sendRequest<sup>(deprecated)</sup>
Z
zengyawen 已提交
7567

7568 7569
>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9)类替代。

C
crazy_hu 已提交
7570
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
W
wangqinxiao 已提交
7571

7572
以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。
Z
zengyawen 已提交
7573

Y
yangguangzhao 已提交
7574 7575
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7576
**参数:**
W
wangqinxiao 已提交
7577

W
wanghaoxu 已提交
7578 7579 7580
  | 参数名  | 类型                            | 必填 | 说明                                                                                   |
  | ------- | ------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                          | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
7581 7582
  | data    | [MessageParcel](#messageparceldeprecated) | 是   | 保存待发送数据的&nbsp;MessageParcel对象。                                              |
  | reply   | [MessageParcel](#messageparceldeprecated) | 是   | 接收应答数据的MessageParcel对象。                                                      |
W
wanghaoxu 已提交
7583
  | options | [MessageOption](#messageoption) | 是   | 本次请求的同异步模式,默认同步调用。                                                   |
Z
zengyawen 已提交
7584

Y
yangguangzhao 已提交
7585
**返回值:**
W
wangqinxiao 已提交
7586

W
wanghaoxu 已提交
7587 7588
  | 类型    | 说明                                          |
  | ------- | --------------------------------------------- |
7589
  | boolean | true:发送成功,false:发送失败。|
Z
zengyawen 已提交
7590

Y
yangguangzhao 已提交
7591
**示例:**
Y
yangguangzhao 已提交
7592

Y
Yippo 已提交
7593
  ```ts
Y
yangguangzhao 已提交
7594 7595
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
7596
          console.log("server died");
Y
yangguangzhao 已提交
7597 7598
      }
  }
Z
zengyawen 已提交
7599 7600 7601 7602
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
Y
yangguangzhao 已提交
7603 7604 7605 7606 7607 7608 7609 7610 7611
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
7612 7613 7614 7615 7616 7617 7618
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  let option = new rpc.MessageOption();
  let data = rpc.MessageParcel.create();
  let reply = rpc.MessageParcel.create();
  data.writeInt(1);
  data.writeString("hello");
Y
yangguangzhao 已提交
7619
  let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option);
Y
yangguangzhao 已提交
7620 7621 7622 7623 7624 7625 7626 7627 7628 7629
  if (ret) {
      console.log("sendRequest got result");
      let msg = reply.readString();
      console.log("RPCTest: reply msg: " + msg);
  } else {
      console.log("RPCTest: sendRequest failed");
  }
  console.log("RPCTest: sendRequest ends, reclaim parcel");
  data.reclaim();
  reply.reclaim();
Z
zengyawen 已提交
7630 7631
  ```

7632 7633
### sendRequest<sup>8+(deprecated)</sup>

7634 7635
>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9)类替代。

C
crazy_hu 已提交
7636
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise&lt;SendRequestResult&gt;
W
wangqinxiao 已提交
7637

7638
以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。
Y
yangguangzhao 已提交
7639 7640 7641

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7642
**参数:**
W
wangqinxiao 已提交
7643

W
wanghaoxu 已提交
7644 7645 7646
  | 参数名  | 类型                            | 必填 | 说明                                                                                   |
  | ------- | ------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                          | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
7647 7648
  | data    | [MessageParcel](#messageparceldeprecated) | 是   | 保存待发送数据的&nbsp;MessageParcel对象。                                              |
  | reply   | [MessageParcel](#messageparceldeprecated) | 是   | 接收应答数据的MessageParcel对象。                                                      |
W
wanghaoxu 已提交
7649
  | options | [MessageOption](#messageoption) | 是   | 本次请求的同异步模式,默认同步调用。                                                   |
Y
yangguangzhao 已提交
7650 7651

**返回值:**
W
wangqinxiao 已提交
7652

W
wanghaoxu 已提交
7653 7654 7655
  | 类型                             | 说明                                          |
  | -------------------------------- | --------------------------------------------- |
  | Promise&lt;SendRequestResult&gt; | 返回一个期约,兑现值是sendRequestResult实例。 |
Y
yangguangzhao 已提交
7656 7657

**示例:**
Y
yangguangzhao 已提交
7658

Y
Yippo 已提交
7659
  ```ts
Y
yangguangzhao 已提交
7660 7661
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
7662
          console.log("server died");
Y
yangguangzhao 已提交
7663 7664
      }
  }
Z
zengyawen 已提交
7665 7666 7667 7668
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
Y
yangguangzhao 已提交
7669 7670 7671 7672 7673 7674 7675 7676 7677
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  let option = new rpc.MessageOption();
  let data = rpc.MessageParcel.create();
  let reply = rpc.MessageParcel.create();
  data.writeInt(1);
  data.writeString("hello");
  testRemoteObject.sendRequest(1, data, reply, option)
      .then(function(result) {
          if (result.errCode === 0) {
              console.log("sendRequest got result");
              result.reply.readException();
              let msg = result.reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
              console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
          }
      }).catch(function(e) {
          console.log("RPCTest: sendRequest got exception: " + e.message);
      }).finally (() => {
          console.log("RPCTest: sendRequest ends, reclaim parcel");
          data.reclaim();
          reply.reclaim();
      });
  ```

W
wanghaoxu 已提交
7704
### sendMessageRequest<sup>9+</sup>
7705

C
crazy_hu 已提交
7706
sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise&lt;RequestResult&gt;
7707

7708
以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。
7709 7710 7711

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7712
**参数:**
W
wangqinxiao 已提交
7713

W
wanghaoxu 已提交
7714 7715 7716
  | 参数名  | 类型                            | 必填 | 说明                                                                                   |
  | ------- | ------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                          | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
7717 7718
  | data    | [MessageSequence](#messagesequence9) | 是   | 保存待发送数据的&nbsp;MessageSequence对象。                                            |
  | reply   | [MessageSequence](#messagesequence9) | 是   | 接收应答数据的MessageSequence对象。                                                    |
W
wanghaoxu 已提交
7719
  | options | [MessageOption](#messageoption) | 是   | 本次请求的同异步模式,默认同步调用。                                                   |
7720 7721

**返回值:**
W
wangqinxiao 已提交
7722

W
wanghaoxu 已提交
7723 7724 7725
  | 类型                         | 说明                                          |
  | ---------------------------- | --------------------------------------------- |
  | Promise&lt;RequestResult&gt; | 返回一个期约,兑现值是sendRequestResult实例。 |
7726 7727 7728

**示例:**

Y
Yippo 已提交
7729
  ```ts
7730 7731 7732 7733 7734 7735 7736
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  let option = new rpc.MessageOption();
W
wanghaoxu 已提交
7737 7738
  let data = rpc.MessageSequence.create();
  let reply = rpc.MessageSequence.create();
7739 7740
  data.writeInt(1);
  data.writeString("hello");
W
wanghaoxu 已提交
7741
  testRemoteObject.sendMessageRequest(1, data, reply, option)
7742 7743
      .then(function(result) {
          if (result.errCode === 0) {
W
wanghaoxu 已提交
7744
              console.log("sendMessageRequest got result");
7745 7746 7747 7748
              result.reply.readException();
              let msg = result.reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
W
wanghaoxu 已提交
7749
              console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode);
7750 7751
          }
      }).catch(function(e) {
W
wanghaoxu 已提交
7752
          console.log("RPCTest: sendMessageRequest got exception: " + e.message);
7753
      }).finally (() => {
W
wanghaoxu 已提交
7754
          console.log("RPCTest: sendMessageRequest ends, reclaim parcel");
7755 7756 7757 7758
          data.reclaim();
          reply.reclaim();
      });
  ```
Z
zengyawen 已提交
7759

W
wanghaoxu 已提交
7760
### sendMessageRequest<sup>9+</sup>
Z
zengyawen 已提交
7761

W
wanghaoxu 已提交
7762 7763
sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback&lt;RequestResult&gt;): void

7764
以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendMessageRequest返回时收到回调,回复内容在reply报文里。
Z
zengyawen 已提交
7765

Y
yangguangzhao 已提交
7766 7767
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7768
**参数:**
W
wanghaoxu 已提交
7769 7770 7771 7772

  | 参数名        | 类型                               | 必填 | 说明                                                                                   |
  | ------------- | ---------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code          | number                             | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
7773 7774
  | data          | [MessageSequence](#messagesequence9) | 是   | 保存待发送数据的&nbsp;MessageSequence对象。                                            |
  | reply         | [MessageSequence](#messagesequence9) | 是   | 接收应答数据的MessageSequence对象。                                                    |
W
wanghaoxu 已提交
7775 7776 7777 7778 7779
  | options       | [MessageOption](#messageoption)    | 是   | 本次请求的同异步模式,默认同步调用。                                                   |
  | AsyncCallback | AsyncCallback&lt;RequestResult&gt; | 是   | 接收发送结果的回调。                                                                   |

**示例:**

Y
Yippo 已提交
7780
  ```ts
W
wanghaoxu 已提交
7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
  function sendRequestCallback(result) {
      if (result.errCode === 0) {
          console.log("sendRequest got result");
          result.reply.readException();
          let msg = result.reply.readString();
          console.log("RPCTest: reply msg: " + msg);
      } else {
          console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
      }
      console.log("RPCTest: sendRequest ends, reclaim parcel");
      result.data.reclaim();
      result.reply.reclaim();
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  let option = new rpc.MessageOption();
  let data = rpc.MessageSequence.create();
  let reply = rpc.MessageSequence.create();
  data.writeInt(1);
  data.writeString("hello");
C
crazy_hu 已提交
7805
  testRemoteObject.sendMessageRequest(1, data, reply, option, sendRequestCallback);
W
wanghaoxu 已提交
7806
  ```
W
wangqinxiao 已提交
7807

W
wanghaoxu 已提交
7808 7809
### sendRequest<sup>8+(deprecated)</sup>

7810 7811
>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9)类替代。

W
wanghaoxu 已提交
7812 7813
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback&lt;SendRequestResult&gt;): void

7814
以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。
W
wanghaoxu 已提交
7815 7816 7817

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7818
**参数:**
W
wanghaoxu 已提交
7819 7820 7821 7822

  | 参数名        | 类型                                   | 必填 | 说明                                                                                   |
  | ------------- | -------------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code          | number                                 | 是   | 本次请求调用的消息码,由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 |
7823 7824
  | data          | [MessageParcel](#messageparceldeprecated)        | 是   | 保存待发送数据的&nbsp;MessageParcel对象。                                              |
  | reply         | [MessageParcel](#messageparceldeprecated)        | 是   | 接收应答数据的MessageParcel对象。                                                      |
W
wanghaoxu 已提交
7825 7826
  | options       | [MessageOption](#messageoption)        | 是   | 本次请求的同异步模式,默认同步调用。                                                   |
  | AsyncCallback | AsyncCallback&lt;SendRequestResult&gt; | 是   | 接收发送结果的回调。                                                                   |
Z
zengyawen 已提交
7827

Y
yangguangzhao 已提交
7828
**示例:**
Y
yangguangzhao 已提交
7829

Y
Yippo 已提交
7830
  ```ts
Y
yangguangzhao 已提交
7831 7832
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
7833
          console.log("server died");
Y
yangguangzhao 已提交
7834 7835
      }
  }
Z
zengyawen 已提交
7836 7837 7838 7839
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
Y
yangguangzhao 已提交
7840 7841 7842 7843 7844 7845 7846 7847 7848
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871
  }
  function sendRequestCallback(result) {
      if (result.errCode === 0) {
          console.log("sendRequest got result");
          result.reply.readException();
          let msg = result.reply.readString();
          console.log("RPCTest: reply msg: " + msg);
      } else {
          console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
      }
      console.log("RPCTest: sendRequest ends, reclaim parcel");
      result.data.reclaim();
      result.reply.reclaim();
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  let option = new rpc.MessageOption();
  let data = rpc.MessageParcel.create();
  let reply = rpc.MessageParcel.create();
  data.writeInt(1);
  data.writeString("hello");
  testRemoteObject.sendRequest(1, data, reply, option, sendRequestCallback);
  ```

C
chenyuyan 已提交
7872
### onRemoteRequest<sup>8+(deprecated)</sup>
7873

7874
>从API version 9 开始不再维护,建议使用[onRemoteMessageRequest](#onremotemessagerequest9)类替代。
7875

C
crazy_hu 已提交
7876
onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
W
wangqinxiao 已提交
7877

7878
sendMessageRequest请求的响应处理函数,服务端在该函数里处理请求,回复结果。
Z
zengyawen 已提交
7879

Y
yangguangzhao 已提交
7880 7881
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7882
**参数:**
W
wangqinxiao 已提交
7883

W
wanghaoxu 已提交
7884 7885 7886
  | 参数名 | 类型                            | 必填 | 说明                                    |
  | ------ | ------------------------------- | ---- | --------------------------------------- |
  | code   | number                          | 是   | 对端发送的服务请求码。                  |
7887 7888
  | data   | [MessageParcel](#messageparceldeprecated) | 是   | 携带客户端调用参数的MessageParcel对象。 |
  | reply  | [MessageParcel](#messageparceldeprecated) | 是   | 写入结果的MessageParcel对象。           |
W
wanghaoxu 已提交
7889
  | option | [MessageOption](#messageoption) | 是   | 指示操作是同步还是异步。                |
Z
zengyawen 已提交
7890

Y
yangguangzhao 已提交
7891
**返回值:**
W
wangqinxiao 已提交
7892

W
wanghaoxu 已提交
7893 7894
  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
7895
  | boolean | true:操作成功,false:操作失败。|
Z
zengyawen 已提交
7896

Y
yangguangzhao 已提交
7897
**示例:**
Y
yangguangzhao 已提交
7898

Y
Yippo 已提交
7899
  ```ts
Y
yangguangzhao 已提交
7900 7901
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
7902
          console.log("server died");
Y
yangguangzhao 已提交
7903 7904
      }
  }
Z
zengyawen 已提交
7905 7906 7907 7908
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
Y
yangguangzhao 已提交
7909 7910 7911 7912 7913 7914 7915 7916 7917
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928
      onRemoteRequest(code, data, reply, option) {
          if (code === 1) {
              console.log("RpcServer: onRemoteRequest called");
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
  }
  ```
W
wanghaoxu 已提交
7929

7930
### onRemoteMessageRequest<sup>9+</sup>
C
chenyuyan 已提交
7931

C
crazy_hu 已提交
7932
onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean>
C
chenyuyan 已提交
7933

7934
> **说明:**
W
wanghaoxu 已提交
7935
>
7936 7937
>* 开发者应优先选择重载onRemoteMessageRequest方法,其中可以自由实现同步和异步的消息处理。
>* 开发者同时重载onRemoteRequest和onRemoteMessageRequest方法时,仅onRemoteMessageRequest方法生效。
7938

W
wanghaoxu 已提交
7939
sendMessageRequest请求的响应处理函数,服务端在该函数里同步或异步地处理请求,回复结果。
C
chenyuyan 已提交
7940 7941 7942

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
7943
**参数:**
7944

W
wanghaoxu 已提交
7945 7946 7947
  | 参数名 | 类型                            | 必填 | 说明                                      |
  | ------ | ------------------------------- | ---- | ----------------------------------------- |
  | code   | number                          | 是   | 对端发送的服务请求码。                    |
7948 7949 7950
  | data   | [MessageSequence](#messagesequence9) | 是   | 携带客户端调用参数的MessageSequence对象。 |
  | reply  | [MessageSequence](#messagesequence9) | 是   | 写入结果的MessageSequence对象。           |
  | option | [MessageOption](#messageoption) | 是   | 指示操作是同步还是异步。                  |
C
chenyuyan 已提交
7951 7952

**返回值:**
Z
zengyawen 已提交
7953

W
wanghaoxu 已提交
7954 7955
  | 类型              | 说明                                                                                           |
  | ----------------- | ---------------------------------------------------------------------------------------------- |
7956
  | boolean           | 若在onRemoteMessageRequest中同步地处理请求,则返回一个布尔值:true:操作成功,false:操作失败。 |
7957
  | Promise\<boolean> | 若在onRemoteMessageRequest中异步地处理请求,则返回一个Promise对象。                                 |
Z
zengyawen 已提交
7958

7959
**重载onRemoteMessageRequest方法同步处理请求示例:**
Z
zengyawen 已提交
7960

Y
Yippo 已提交
7961
  ```ts
C
chenyuyan 已提交
7962 7963 7964 7965
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
C
crazy_hu 已提交
7966

7967
      onRemoteMessageRequest(code, data, reply, option) {
C
chenyuyan 已提交
7968
          if (code === 1) {
7969
              console.log("RpcServer: sync onRemoteMessageRequest is called");
C
chenyuyan 已提交
7970 7971 7972 7973 7974 7975
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
7976 7977
  }
  ```
W
wanghaoxu 已提交
7978

7979
  **重载onRemoteMessageRequest方法异步处理请求示例:**
Z
zengyawen 已提交
7980

Y
Yippo 已提交
7981
  ```ts
7982 7983 7984 7985
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
C
crazy_hu 已提交
7986

7987
      async onRemoteMessageRequest(code, data, reply, option) {
C
chenyuyan 已提交
7988
          if (code === 1) {
7989
              console.log("RpcServer: async onRemoteMessageRequest is called");
C
chenyuyan 已提交
7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
          await new Promise((resolve) => {
            setTimeout(resolve, 100);
          })
          return true;
      }
  }
  ```
W
wanghaoxu 已提交
8001

8002
**同时重载onRemoteMessageRequest和onRemoteRequest方法同步处理请求示例:**
8003

Y
Yippo 已提交
8004
  ```ts
8005 8006 8007 8008
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
C
crazy_hu 已提交
8009

8010 8011
      onRemoteRequest(code, data, reply, option) {
          if (code === 1) {
8012
              console.log("RpcServer: sync onRemoteMessageRequest is called");
8013 8014 8015 8016 8017 8018
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
8019 8020
      // 同时调用仅会执行onRemoteMessageRequest
      onRemoteMessageRequest(code, data, reply, option) {
8021
          if (code === 1) {
8022
              console.log("RpcServer: async onRemoteMessageRequest is called");
8023 8024 8025 8026
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
W
wanghaoxu 已提交
8027
         
8028 8029 8030 8031
          return true;
      }
  }
  ```
W
wanghaoxu 已提交
8032

8033
  **同时重载onRemoteMessageRequest和onRemoteRequest方法异步处理请求示例:**
Z
zengyawen 已提交
8034

Y
Yippo 已提交
8035
  ```ts
8036 8037 8038 8039
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
C
crazy_hu 已提交
8040

8041 8042
      onRemoteRequest(code, data, reply, option) {
          if (code === 1) {
W
wanghaoxu 已提交
8043
              console.log("RpcServer: sync onRemoteRequest is called");
8044 8045 8046 8047 8048 8049
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
8050 8051
      // 同时调用仅会执行onRemoteMessageRequest
      async onRemoteMessageRequest(code, data, reply, option) {
8052
          if (code === 1) {
8053
              console.log("RpcServer: async onRemoteMessageRequest is called");
8054 8055 8056 8057
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
W
wanghaoxu 已提交
8058
         await new Promise((resolve) => {
8059 8060 8061 8062 8063 8064
            setTimeout(resolve, 100);
          })
          return true;
      }
  }
  ```
W
wanghaoxu 已提交
8065

Z
zengyawen 已提交
8066 8067
### getCallingUid

W
wanghaoxu 已提交
8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080
getCallingUid(): number

获取通信对端的进程Uid。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**
  | 类型   | 说明                    |
  | ------ | ----------------------- |
  | number | 返回通信对端的进程Uid。 |

**示例:**

Y
Yippo 已提交
8081
  ```ts
W
wanghaoxu 已提交
8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  console.log("RpcServer: getCallingUid: " + testRemoteObject.getCallingUid());
  ```

### getCallingPid

getCallingPid(): number

获取通信对端的进程Pid。

**系统能力**:SystemCapability.Communication.IPC.Core

**返回值:**

  | 类型   | 说明                    |
  | ------ | ----------------------- |
  | number | 返回通信对端的进程Pid。 |

**示例:**

Y
Yippo 已提交
8107
  ```ts
W
wanghaoxu 已提交
8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  console.log("RpcServer: getCallingPid: " + testRemoteObject.getCallingPid());
  ```

### getLocalInterface<sup>9+</sup>

getLocalInterface(descriptor: string): IRemoteBroker
Z
zengyawen 已提交
8120

8121
查询接口描述符的字符串。
Z
zengyawen 已提交
8122

Y
yangguangzhao 已提交
8123 8124
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8125
**参数:**
W
wanghaoxu 已提交
8126 8127 8128 8129 8130

  | 参数名     | 类型   | 必填 | 说明                 |
  | ---------- | ------ | ---- | -------------------- |
  | descriptor | string | 是   | 接口描述符的字符串。 |

Y
yangguangzhao 已提交
8131
**返回值:**
W
wangqinxiao 已提交
8132

W
wanghaoxu 已提交
8133 8134 8135
  | 类型          | 说明                                          |
  | ------------- | --------------------------------------------- |
  | IRemoteBroker | 返回绑定到指定接口描述符的IRemoteBroker对象。 |
Z
zengyawen 已提交
8136 8137


Y
yangguangzhao 已提交
8138
**示例:**
Y
yangguangzhao 已提交
8139

Y
Yippo 已提交
8140
  ```ts
Y
yangguangzhao 已提交
8141 8142
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
8143
          console.log("server died");
Y
yangguangzhao 已提交
8144 8145
      }
  }
Z
zengyawen 已提交
8146 8147 8148 8149
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
8150
      registerDeathRecipient(recipient: MyDeathRecipient, flags: number) {
8151
          // 方法逻辑需开发者根据业务需要实现
8152 8153
      }
      unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) {
8154
          // 方法逻辑需开发者根据业务需要实现
8155
      }
Y
yangguangzhao 已提交
8156 8157 8158
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
8159 8160
  }
  let testRemoteObject = new TestRemoteObject("testObject");
W
wanghaoxu 已提交
8161 8162 8163
  try {
      let broker = testRemoteObject.getLocalInterface("testObject");
  } catch(error) {
8164 8165
      console.info("rpc get local interface fail, errorCode " + error.code);
      console.info("rpc get local interface fail, errorMessage " + error.message);
W
wanghaoxu 已提交
8166
  }
Z
zengyawen 已提交
8167 8168
  ```

8169
### queryLocalInterface<sup>(deprecated)</sup>
Z
zengyawen 已提交
8170

8171 8172
>从API version 9 开始不再维护,建议使用[getLocalInterface](#getlocalinterface9)类替代。

W
wanghaoxu 已提交
8173
queryLocalInterface(descriptor: string): IRemoteBroker
Z
zengyawen 已提交
8174

8175
查询并获取当前接口描述符对应的远端对象是否已经存在。
Z
zengyawen 已提交
8176

Y
yangguangzhao 已提交
8177 8178
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8179
**参数:**
W
wangqinxiao 已提交
8180

W
wanghaoxu 已提交
8181 8182 8183
  | 参数名     | 类型   | 必填 | 说明                   |
  | ---------- | ------ | ---- | ---------------------- |
  | descriptor | string | 是   | 需要查询的接口描述符。 |
Z
zengyawen 已提交
8184

W
wanghaoxu 已提交
8185 8186 8187 8188 8189
**返回值:**

  | 类型          | 说明                                                               |
  | ------------- | ------------------------------------------------------------------ |
  | IRemoteBroker | 如果接口描述符对应的远端对象存在,则返回该远端对象,否则返回Null。 |
Z
zengyawen 已提交
8190

Y
yangguangzhao 已提交
8191
**示例:**
Y
yangguangzhao 已提交
8192

Y
Yippo 已提交
8193
  ```ts
Y
yangguangzhao 已提交
8194 8195
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
8196
          console.log("server died");
Y
yangguangzhao 已提交
8197 8198
      }
  }
Z
zengyawen 已提交
8199 8200 8201 8202
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
Y
yangguangzhao 已提交
8203 8204 8205 8206 8207 8208 8209 8210 8211
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
8212 8213
  }
  let testRemoteObject = new TestRemoteObject("testObject");
W
wanghaoxu 已提交
8214
  let broker = testRemoteObject.queryLocalInterface("testObject");
Z
zengyawen 已提交
8215 8216
  ```

W
wanghaoxu 已提交
8217
### getDescriptor<sup>9+</sup>
Z
zengyawen 已提交
8218

C
crazy_hu 已提交
8219
getDescriptor(): string
Z
zengyawen 已提交
8220

W
wanghaoxu 已提交
8221
获取对象的接口描述符。接口描述符为字符串。
Z
zengyawen 已提交
8222

Y
yangguangzhao 已提交
8223 8224
**系统能力**:SystemCapability.Communication.IPC.Core

Y
yangguangzhao 已提交
8225
**返回值:**
W
wangqinxiao 已提交
8226

W
wanghaoxu 已提交
8227 8228 8229
  | 类型   | 说明             |
  | ------ | ---------------- |
  | string | 返回接口描述符。 |
Z
zengyawen 已提交
8230

W
wanghaoxu 已提交
8231 8232
**错误码:**

C
crazy_hu 已提交
8233
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
8234

W
wanghaoxu 已提交
8235 8236 8237
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900008 | proxy or remote object is invalid |
Z
zengyawen 已提交
8238

Y
yangguangzhao 已提交
8239
**示例:**
Y
yangguangzhao 已提交
8240

Y
Yippo 已提交
8241
  ```ts
Y
yangguangzhao 已提交
8242 8243
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
8244
          console.log("server died");
Y
yangguangzhao 已提交
8245 8246
      }
  }
Z
zengyawen 已提交
8247 8248 8249 8250
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
8251
      registerDeathRecipient(recipient: MyDeathRecipient, flags: number) {
8252
          // 方法逻辑需开发者根据业务需要实现
8253 8254
      }
      unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) {
8255
          // 方法逻辑需开发者根据业务需要实现
8256
      }
Y
yangguangzhao 已提交
8257 8258 8259
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
8260 8261
  }
  let testRemoteObject = new TestRemoteObject("testObject");
W
wanghaoxu 已提交
8262 8263 8264
  try {
      let descriptor = testRemoteObject.getDescriptor();
  } catch(error) {
8265 8266
      console.info("rpc get local interface fail, errorCode " + error.code);
      console.info("rpc get local interface fail, errorMessage " + error.message);
W
wanghaoxu 已提交
8267 8268
  }
  console.log("RpcServer: descriptor is: " + descriptor);
Z
zengyawen 已提交
8269 8270
  ```

8271
### getInterfaceDescriptor<sup>(deprecated)</sup>
Z
zengyawen 已提交
8272

8273 8274
>从API version 9 开始不再维护,建议使用[getDescriptor](#getdescriptor9)类替代。

Z
zengyawen 已提交
8275 8276
getInterfaceDescriptor(): string

8277
查询接口描述符。
Z
zengyawen 已提交
8278

Y
yangguangzhao 已提交
8279 8280
**系统能力**:SystemCapability.Communication.IPC.Core

Y
yangguangzhao 已提交
8281
**返回值:**
W
wangqinxiao 已提交
8282

W
wanghaoxu 已提交
8283 8284
  | 类型   | 说明             |
  | ------ | ---------------- |
Y
yangguangzhao 已提交
8285
  | string | 返回接口描述符。 |
Z
zengyawen 已提交
8286

Y
yangguangzhao 已提交
8287
**示例:**
Y
yangguangzhao 已提交
8288

Y
Yippo 已提交
8289
  ```ts
Y
yangguangzhao 已提交
8290 8291
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
8292
          console.log("server died");
Y
yangguangzhao 已提交
8293 8294
      }
  }
Z
zengyawen 已提交
8295 8296 8297 8298
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
Y
yangguangzhao 已提交
8299 8300 8301 8302 8303 8304 8305 8306 8307
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
8308 8309 8310 8311 8312 8313
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  let descriptor = testRemoteObject.getInterfaceDescriptor();
  console.log("RpcServer: descriptor is: " + descriptor);
  ```

W
wanghaoxu 已提交
8314
### modifyLocalInterface<sup>9+</sup>
Z
zengyawen 已提交
8315

W
wanghaoxu 已提交
8316
modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void
Z
zengyawen 已提交
8317 8318 8319

此接口用于把接口描述符和IRemoteBroker对象绑定。

Y
yangguangzhao 已提交
8320 8321
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8322
**参数:**
W
wangqinxiao 已提交
8323

W
wanghaoxu 已提交
8324 8325 8326 8327 8328 8329 8330
  | 参数名         | 类型          | 必填 | 说明                                  |
  | -------------- | ------------- | ---- | ------------------------------------- |
  | localInterface | IRemoteBroker | 是   | 将与描述符绑定的IRemoteBroker对象。   |
  | descriptor     | string        | 是   | 用于与IRemoteBroker对象绑定的描述符。 |

**示例:**

Y
Yippo 已提交
8331
  ```ts
W
wanghaoxu 已提交
8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
          try {
              this.modifyLocalInterface(this, descriptor);
          } catch(error) {
              console.info(rpc attach local interface fail, errorCode " + error.code);
              console.info(rpc attach local interface fail, errorMessage " + error.message);
          }
      }
8347
      registerDeathRecipient(recipient: MyDeathRecipient, flags: number) {
8348
          // 方法逻辑需开发者根据业务需要实现
8349 8350
      }
      unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) {
8351
          // 方法逻辑需开发者根据业务需要实现
8352
      }
W
wanghaoxu 已提交
8353 8354 8355 8356 8357 8358 8359 8360 8361 8362
      isObjectDead(): boolean {
          return false;
      }
      asObject(): rpc.IRemoteObject {
          return this;
      }
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  ```

8363
### attachLocalInterface<sup>(deprecated)</sup>
W
wanghaoxu 已提交
8364

8365 8366
>从API version 9 开始不再维护,建议使用[modifyLocalInterface](#modifylocalinterface9)类替代。

W
wanghaoxu 已提交
8367 8368
attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void

8369
此接口用于把接口描述符和IRemoteBroker对象绑定。
W
wanghaoxu 已提交
8370 8371

**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8372

W
wanghaoxu 已提交
8373
**参数:**
W
wanghaoxu 已提交
8374 8375 8376 8377 8378

  | 参数名         | 类型          | 必填 | 说明                                  |
  | -------------- | ------------- | ---- | ------------------------------------- |
  | localInterface | IRemoteBroker | 是   | 将与描述符绑定的IRemoteBroker对象。   |
  | descriptor     | string        | 是   | 用于与IRemoteBroker对象绑定的描述符。 |
Z
zengyawen 已提交
8379

Y
yangguangzhao 已提交
8380
**示例:**
Y
yangguangzhao 已提交
8381

Y
Yippo 已提交
8382
  ```ts
Y
yangguangzhao 已提交
8383 8384
  class MyDeathRecipient {
      onRemoteDied() {
Y
yangguangzhao 已提交
8385
          console.log("server died");
Y
yangguangzhao 已提交
8386 8387
      }
  }
Z
zengyawen 已提交
8388 8389 8390 8391 8392
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
          this.attachLocalInterface(this, descriptor);
      }
Y
yangguangzhao 已提交
8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
      asObject(): rpc.IRemoteObject {
          return this;
      }
Z
zengyawen 已提交
8405 8406 8407 8408 8409 8410 8411 8412
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  ```

## Ashmem<sup>8+</sup>

提供与匿名共享内存对象相关的方法,包括创建、关闭、映射和取消映射Ashmem、从Ashmem读取数据和写入数据、获取Ashmem大小、设置Ashmem保护。

Y
yangguangzhao 已提交
8413 8414
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。

8415 8416 8417
映射内存保护类型:

  | 名称       | 默认值  | 说明               |
W
wanghaoxu 已提交
8418 8419 8420 8421 8422
  | ---------- | --- | ------------------ |
  | PROT_EXEC  | 4   | 映射的内存可执行   |
  | PROT_NONE  | 0   | 映射的内存不可访问 |
  | PROT_READ  | 1   | 映射的内存可读     |
  | PROT_WRITE | 2   | 映射的内存可写     |
Z
zengyawen 已提交
8423

W
wanghaoxu 已提交
8424
### create<sup>9+</sup>
Z
zengyawen 已提交
8425

W
wanghaoxu 已提交
8426
static create(name: string, size: number): Ashmem
Z
zengyawen 已提交
8427

8428
静态方法,根据指定的名称和大小创建Ashmem对象。
Z
zengyawen 已提交
8429

Y
yangguangzhao 已提交
8430 8431
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8432
**参数:**
W
wangqinxiao 已提交
8433

W
wanghaoxu 已提交
8434 8435 8436 8437
  | 参数名 | 类型   | 必填 | 说明                         |
  | ------ | ------ | ---- | ---------------------------- |
  | name   | string | 是   | 名称,用于查询Ashmem信息。   |
  | size   | number | 是   | Ashmem的大小,以字节为单位。 |
Z
zengyawen 已提交
8438

Y
yangguangzhao 已提交
8439
**返回值:**
W
wangqinxiao 已提交
8440

W
wanghaoxu 已提交
8441 8442
  | 类型   | 说明                                           |
  | ------ | ---------------------------------------------- |
Y
yangguangzhao 已提交
8443
  | Ashmem | 返回创建的Ashmem对象;如果创建失败,返回null。 |
Z
zengyawen 已提交
8444

W
wanghaoxu 已提交
8445 8446
**示例:**

Y
Yippo 已提交
8447
  ```ts
C
crazy_hu 已提交
8448
  let ashmem;
W
wanghaoxu 已提交
8449
  try {
C
crazy_hu 已提交
8450
      ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
W
wanghaoxu 已提交
8451 8452 8453 8454 8455 8456 8457 8458 8459 8460
  } catch(error) {
      console.info("Rpc creat ashmem fail, errorCode " + error.code);
      console.info("Rpc creat ashmem  fail, errorMessage " + error.message);
  }
  let size = ashmem.getAshmemSize();
  console.log("RpcTest: get ashemm by create : " + ashmem + " size is : " + size);
  ```

### createAshmem<sup>8+(deprecated)</sup>

8461 8462
>从API version 9 开始不再维护,建议使用[create](#create9)类替代。

W
wanghaoxu 已提交
8463 8464
static createAshmem(name: string, size: number): Ashmem

8465
静态方法,根据指定的名称和大小创建Ashmem对象。
W
wanghaoxu 已提交
8466 8467 8468

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8469
**参数:**
W
wanghaoxu 已提交
8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480

  | 参数名 | 类型   | 必填 | 说明                         |
  | ------ | ------ | ---- | ---------------------------- |
  | name   | string | 是   | 名称,用于查询Ashmem信息。   |
  | size   | number | 是   | Ashmem的大小,以字节为单位。 |

**返回值:**

  | 类型   | 说明                                           |
  | ------ | ---------------------------------------------- |
  | Ashmem | 返回创建的Ashmem对象;如果创建失败,返回null。 |
Z
zengyawen 已提交
8481

Y
yangguangzhao 已提交
8482
**示例:**
Y
yangguangzhao 已提交
8483

Y
Yippo 已提交
8484
  ```ts
Z
zengyawen 已提交
8485 8486 8487 8488 8489
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
  let size = ashmem.getAshmemSize();
  console.log("RpcTest: get ashemm by createAshmem : " + ashmem + " size is : " + size);
  ```

W
wanghaoxu 已提交
8490
### create<sup>9+</sup>
Z
zengyawen 已提交
8491

W
wanghaoxu 已提交
8492
static create(ashmem: Ashmem): Ashmem
Z
zengyawen 已提交
8493

8494
静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。
Z
zengyawen 已提交
8495

Y
yangguangzhao 已提交
8496 8497
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8498
**参数:**
W
wangqinxiao 已提交
8499

W
wanghaoxu 已提交
8500 8501 8502
  | 参数名 | 类型   | 必填 | 说明                 |
  | ------ | ------ | ---- | -------------------- |
  | ashmem | Ashmem | 是   | 已存在的Ashmem对象。 |
Z
zengyawen 已提交
8503

Y
yangguangzhao 已提交
8504
**返回值:**
W
wangqinxiao 已提交
8505

W
wanghaoxu 已提交
8506 8507
  | 类型   | 说明                   |
  | ------ | ---------------------- |
Y
yangguangzhao 已提交
8508
  | Ashmem | 返回创建的Ashmem对象。 |
Z
zengyawen 已提交
8509

W
wanghaoxu 已提交
8510 8511 8512

**示例:**

Y
Yippo 已提交
8513
  ```ts
C
crazy_hu 已提交
8514
  let ashmem2;
W
wanghaoxu 已提交
8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527
  try {
      let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
      let ashmem2 = rpc.Ashmem.create(ashmem);
  } catch(error) {
      console.info("Rpc creat ashmem from existing fail, errorCode " + error.code);
      console.info("Rpc creat ashmem from existing  fail, errorMessage " + error.message);
  }
  let size = ashmem2.getAshmemSize();
  console.log("RpcTest: get ashemm by create : " + ashmem2 + " size is : " + size);
  ```

### createAshmemFromExisting<sup>8+(deprecated)</sup>

8528 8529
>从API version 9 开始不再维护,建议使用[create](#create9)类替代。

W
wanghaoxu 已提交
8530 8531
static createAshmemFromExisting(ashmem: Ashmem): Ashmem

8532
静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。
W
wanghaoxu 已提交
8533 8534 8535

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8536
**参数:**
W
wanghaoxu 已提交
8537 8538 8539 8540 8541 8542 8543 8544 8545 8546

  | 参数名 | 类型   | 必填 | 说明                 |
  | ------ | ------ | ---- | -------------------- |
  | ashmem | Ashmem | 是   | 已存在的Ashmem对象。 |

**返回值:**

  | 类型   | 说明                   |
  | ------ | ---------------------- |
  | Ashmem | 返回创建的Ashmem对象。 |
Z
zengyawen 已提交
8547

Y
yangguangzhao 已提交
8548
**示例:**
Y
yangguangzhao 已提交
8549

Y
Yippo 已提交
8550
  ```ts
Z
zengyawen 已提交
8551 8552 8553 8554 8555 8556
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
  let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem);
  let size = ashmem2.getAshmemSize();
  console.log("RpcTest: get ashemm by createAshmemFromExisting : " + ashmem2 + " size is : " + size);
  ```

Y
yangguangzhao 已提交
8557
### closeAshmem<sup>8+</sup>
Z
zengyawen 已提交
8558 8559 8560 8561 8562

closeAshmem(): void

关闭这个Ashmem。

Y
yangguangzhao 已提交
8563
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8564

Y
yangguangzhao 已提交
8565
**示例:**
Y
yangguangzhao 已提交
8566

Y
Yippo 已提交
8567
  ```ts
W
wanghaoxu 已提交
8568
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
Z
zengyawen 已提交
8569 8570 8571
  ashmem.closeAshmem();
  ```

Y
yangguangzhao 已提交
8572
### unmapAshmem<sup>8+</sup>
Z
zengyawen 已提交
8573 8574 8575 8576 8577

unmapAshmem(): void

删除该Ashmem对象的地址映射。

Y
yangguangzhao 已提交
8578
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8579

Y
yangguangzhao 已提交
8580
**示例:**
Y
yangguangzhao 已提交
8581

Y
Yippo 已提交
8582
  ```ts
W
wanghaoxu 已提交
8583
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
Z
zengyawen 已提交
8584 8585 8586
  ashmem.unmapAshmem();
  ```

Y
yangguangzhao 已提交
8587
### getAshmemSize<sup>8+</sup>
Z
zengyawen 已提交
8588 8589 8590 8591 8592

getAshmemSize(): number

获取Ashmem对象的内存大小。

Y
yangguangzhao 已提交
8593
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8594

Y
yangguangzhao 已提交
8595
**返回值:**
W
wangqinxiao 已提交
8596

W
wanghaoxu 已提交
8597 8598
  | 类型   | 说明                       |
  | ------ | -------------------------- |
Y
yangguangzhao 已提交
8599
  | number | 返回Ashmem对象的内存大小。 |
Z
zengyawen 已提交
8600

Y
yangguangzhao 已提交
8601
**示例:**
Y
yangguangzhao 已提交
8602

Y
Yippo 已提交
8603
  ```ts
Z
zengyawen 已提交
8604 8605 8606 8607 8608
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
  let size = ashmem.getAshmemSize();
  console.log("RpcTest: get ashmem is " + ashmem + " size is : " + size);
  ```

W
wanghaoxu 已提交
8609
### mapTypedAshmem<sup>9+</sup>
Z
zengyawen 已提交
8610

W
wanghaoxu 已提交
8611
mapTypedAshmem(mapType: number): void
Z
zengyawen 已提交
8612 8613 8614

在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。

Y
yangguangzhao 已提交
8615
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8616

W
wanghaoxu 已提交
8617
**参数:**
W
wangqinxiao 已提交
8618

W
wanghaoxu 已提交
8619 8620 8621 8622
  | 参数名  | 类型   | 必填 | 说明                           |
  | ------- | ------ | ---- | ------------------------------ |
  | mapType | number | 是   | 指定映射的内存区域的保护等级。 |

W
wanghaoxu 已提交
8623 8624
**错误码:**

C
crazy_hu 已提交
8625
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
8626

W
wanghaoxu 已提交
8627 8628 8629
  | 错误码ID | 错误信息 |
  | ------- | ------ |
  | 1900001     | call mmap function failed |
W
wanghaoxu 已提交
8630 8631 8632

**示例:**

Y
Yippo 已提交
8633
  ```ts
W
wanghaoxu 已提交
8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  try {
      ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE);
  } catch(error) {
      console.info("Rpc map ashmem fail, errorCode " + error.code);
      console.info("Rpc map ashmem fail, errorMessage " + error.message);
  }
  ```

### mapAshmem<sup>8+(deprecated)</sup>

8645 8646
>从API version 9 开始不再维护,建议使用[mapTypedAshmem](#maptypedashmem9)类替代。

W
wanghaoxu 已提交
8647 8648
mapAshmem(mapType: number): boolean

8649
在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。
W
wanghaoxu 已提交
8650 8651 8652

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8653
**参数:**
W
wanghaoxu 已提交
8654 8655 8656 8657

  | 参数名  | 类型   | 必填 | 说明                           |
  | ------- | ------ | ---- | ------------------------------ |
  | mapType | number | 是   | 指定映射的内存区域的保护等级。 |
Z
zengyawen 已提交
8658

Y
yangguangzhao 已提交
8659
**返回值:**
W
wangqinxiao 已提交
8660

W
wanghaoxu 已提交
8661 8662
  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
8663
  | boolean | true:映射成功,false:映射失败。|
Z
zengyawen 已提交
8664

Y
yangguangzhao 已提交
8665
**示例:**
Y
yangguangzhao 已提交
8666

Y
Yippo 已提交
8667
  ```ts
Z
zengyawen 已提交
8668
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
Y
yangguangzhao 已提交
8669
  let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE);
Z
zengyawen 已提交
8670 8671 8672
  console.log("RpcTest: map ashmem result is  : " + mapReadAndWrite);
  ```

W
wanghaoxu 已提交
8673
### mapReadWriteAshmem<sup>9+</sup>
Z
zengyawen 已提交
8674

C
crazy_hu 已提交
8675
mapReadWriteAshmem(): void
Z
zengyawen 已提交
8676 8677 8678

在此进程虚拟地址空间上创建可读写的共享文件映射。

Y
yangguangzhao 已提交
8679
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8680

W
wanghaoxu 已提交
8681 8682
**错误码:**

C
crazy_hu 已提交
8683
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
8684

W
wanghaoxu 已提交
8685 8686 8687
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900001 | call mmap function failed |
W
wanghaoxu 已提交
8688 8689 8690

**示例:**

Y
Yippo 已提交
8691
  ```ts
W
wanghaoxu 已提交
8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  try {
      ashmem.mapReadWriteAshmem();
  } catch(error) {
      console.info("Rpc map read and write ashmem fail, errorCode " + error.code);
      console.info("Rpc map read and write ashmem fail, errorMessage " + error.message);
  }
  ```

### mapReadAndWriteAshmem<sup>8+(deprecated)</sup>

8703 8704
>从API version 9 开始不再维护,建议使用[mapReadWriteAshmem](#mapreadwriteashmem9)类替代。

W
wanghaoxu 已提交
8705 8706
mapReadAndWriteAshmem(): boolean

8707
在此进程虚拟地址空间上创建可读写的共享文件映射。
W
wanghaoxu 已提交
8708 8709 8710

**系统能力**:SystemCapability.Communication.IPC.Core

Y
yangguangzhao 已提交
8711
**返回值:**
W
wangqinxiao 已提交
8712

W
wanghaoxu 已提交
8713 8714
  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
8715
  | boolean | true:映射成功,false:映射失败。|
Z
zengyawen 已提交
8716

Y
yangguangzhao 已提交
8717
**示例:**
Y
yangguangzhao 已提交
8718

Y
Yippo 已提交
8719
  ```ts
Z
zengyawen 已提交
8720 8721 8722 8723 8724
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
  let mapResult = ashmem.mapReadAndWriteAshmem();
  console.log("RpcTest: map ashmem result is  : " + mapResult);
  ```

W
wanghaoxu 已提交
8725
### mapReadonlyAshmem<sup>9+</sup>
Z
zengyawen 已提交
8726

W
wanghaoxu 已提交
8727
mapReadonlyAshmem(): void
Z
zengyawen 已提交
8728 8729 8730

在此进程虚拟地址空间上创建只读的共享文件映射。

Y
yangguangzhao 已提交
8731
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8732

W
wanghaoxu 已提交
8733 8734
**错误码:**

C
crazy_hu 已提交
8735
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
8736

W
wanghaoxu 已提交
8737 8738 8739
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900001 | call mmap function failed |
W
wanghaoxu 已提交
8740 8741 8742

**示例:**

Y
Yippo 已提交
8743
  ```ts
W
wanghaoxu 已提交
8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  try {
      ashmem.mapReadonlyAshmem();
  } catch(error) {
      console.info("Rpc map read and write ashmem fail, errorCode " + error.code);
      console.info("Rpc map read and write ashmem fail, errorMessage " + error.message);
  }
  ```

### mapReadOnlyAshmem<sup>8+(deprecated)</sup>

8755 8756
>从API version 9 开始不再维护,建议使用[mapReadonlyAshmem](#mapreadonlyashmem9)类替代。

W
wanghaoxu 已提交
8757 8758
mapReadOnlyAshmem(): boolean

8759
在此进程虚拟地址空间上创建只读的共享文件映射。
W
wanghaoxu 已提交
8760 8761 8762

**系统能力**:SystemCapability.Communication.IPC.Core

Y
yangguangzhao 已提交
8763
**返回值:**
W
wangqinxiao 已提交
8764

W
wanghaoxu 已提交
8765 8766
  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
8767
  | boolean | true:映射成功,false:映射失败。|
Z
zengyawen 已提交
8768

Y
yangguangzhao 已提交
8769
**示例:**
Y
yangguangzhao 已提交
8770

Y
Yippo 已提交
8771
  ```ts
Z
zengyawen 已提交
8772 8773 8774 8775 8776
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
  let mapResult = ashmem.mapReadOnlyAshmem();
  console.log("RpcTest: Ashmem mapReadOnlyAshmem result is : " + mapResult);
  ```

W
wanghaoxu 已提交
8777
### setProtectionType<sup>9+</sup>
Z
zengyawen 已提交
8778

W
wanghaoxu 已提交
8779
setProtectionType(protectionType: number): void
Z
zengyawen 已提交
8780 8781 8782

设置映射内存区域的保护等级。

Y
yangguangzhao 已提交
8783
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8784

W
wanghaoxu 已提交
8785
**参数:**
W
wangqinxiao 已提交
8786

W
wanghaoxu 已提交
8787 8788 8789 8790
  | 参数名         | 类型   | 必填 | 说明               |
  | -------------- | ------ | ---- | ------------------ |
  | protectionType | number | 是   | 要设置的保护类型。 |

W
wanghaoxu 已提交
8791 8792
**错误码:**

C
crazy_hu 已提交
8793
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
8794

W
wanghaoxu 已提交
8795 8796 8797
  | 错误码ID | 错误信息 |
  | -------- | ------- |
  | 1900002 | call os ioctl function failed |
W
wanghaoxu 已提交
8798 8799 8800

**示例:**

Y
Yippo 已提交
8801
  ```ts
W
wanghaoxu 已提交
8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  try {
      ashmem.setProtection(ashmem.PROT_READ);
  } catch(error) {
      console.info("Rpc set protection type fail, errorCode " + error.code);
      console.info("Rpc set protection type fail, errorMessage " + error.message);
  }
  ```

### setProtection<sup>8+(deprecated)</sup>

8813 8814
>从API version 9 开始不再维护,建议使用[setProtectionType](#setprotectiontype9)类替代。

W
wanghaoxu 已提交
8815 8816
setProtection(protectionType: number): boolean

8817
设置映射内存区域的保护等级。
W
wanghaoxu 已提交
8818 8819 8820

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8821
**参数:**
W
wanghaoxu 已提交
8822 8823 8824 8825

  | 参数名         | 类型   | 必填 | 说明               |
  | -------------- | ------ | ---- | ------------------ |
  | protectionType | number | 是   | 要设置的保护类型。 |
Z
zengyawen 已提交
8826

Y
yangguangzhao 已提交
8827
**返回值:**
W
wangqinxiao 已提交
8828

W
wanghaoxu 已提交
8829 8830
  | 类型    | 说明                                      |
  | ------- | ----------------------------------------- |
8831
  | boolean | true:设置成功,false:设置失败。|
Z
zengyawen 已提交
8832

Y
yangguangzhao 已提交
8833
**示例:**
Y
yangguangzhao 已提交
8834

Y
Yippo 已提交
8835
  ```ts
Z
zengyawen 已提交
8836
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
Y
yangguangzhao 已提交
8837
  let result = ashmem.setProtection(ashmem.PROT_READ);
Z
zengyawen 已提交
8838 8839 8840
  console.log("RpcTest: Ashmem setProtection result is : " + result);
  ```

W
wanghaoxu 已提交
8841
### writeAshmem<sup>9+</sup>
Z
zengyawen 已提交
8842

W
wanghaoxu 已提交
8843
writeAshmem(buf: number[], size: number, offset: number): void
Z
zengyawen 已提交
8844 8845 8846

将数据写入此Ashmem对象关联的共享文件。

Y
yangguangzhao 已提交
8847
**系统能力**:SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8848

W
wanghaoxu 已提交
8849
**参数:**
W
wangqinxiao 已提交
8850

W
wanghaoxu 已提交
8851 8852 8853 8854 8855 8856
  | 参数名 | 类型     | 必填 | 说明                                               |
  | ------ | -------- | ---- | -------------------------------------------------- |
  | buf    | number[] | 是   | 写入Ashmem对象的数据。                             |
  | size   | number   | 是   | 要写入的数据大小。                                 |
  | offset | number   | 是   | 要写入的数据在此Ashmem对象关联的内存区间的起始位置 |

W
wanghaoxu 已提交
8857 8858
**错误码:**

C
crazy_hu 已提交
8859
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
8860

W
wanghaoxu 已提交
8861 8862 8863
  | 错误码ID | 错误信息 |
  | ------- | -------- |
  | 1900003 | write to ashmem failed |
W
wanghaoxu 已提交
8864 8865 8866

**示例:**

Y
Yippo 已提交
8867
  ```ts
W
wanghaoxu 已提交
8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  ashmem.mapReadWriteAshmem();
  var ByteArrayVar = [1, 2, 3, 4, 5];
  try {
      ashmem.writeAshmem(ByteArrayVar, 5, 0);
  } catch(error) {
      console.info("Rpc write to ashmem fail, errorCode " + error.code);
      console.info("Rpc write to ashmem fail, errorMessage " + error.message);
  }
  ```

### writeToAshmem<sup>8+(deprecated)</sup>

8881 8882
>从API version 9 开始不再维护,建议使用[writeAshmem](#writeashmem9)类替代。

W
wanghaoxu 已提交
8883 8884
writeToAshmem(buf: number[], size: number, offset: number): boolean

8885
将数据写入此Ashmem对象关联的共享文件。
W
wanghaoxu 已提交
8886 8887 8888

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8889
**参数:**
W
wanghaoxu 已提交
8890 8891 8892 8893 8894 8895

  | 参数名 | 类型     | 必填 | 说明                                               |
  | ------ | -------- | ---- | -------------------------------------------------- |
  | buf    | number[] | 是   | 写入Ashmem对象的数据。                             |
  | size   | number   | 是   | 要写入的数据大小。                                 |
  | offset | number   | 是   | 要写入的数据在此Ashmem对象关联的内存区间的起始位置 |
Z
zengyawen 已提交
8896

Y
yangguangzhao 已提交
8897
**返回值:**
W
wangqinxiao 已提交
8898

W
wanghaoxu 已提交
8899 8900
  | 类型    | 说明                                                                                      |
  | ------- | ----------------------------------------------------------------------------------------- |
8901
  | boolean | true:如果数据写入成功,false:在其他情况下,如数据写入越界或未获得写入权限。。 |
Z
zengyawen 已提交
8902

Y
yangguangzhao 已提交
8903
**示例:**
Y
yangguangzhao 已提交
8904

Y
Yippo 已提交
8905
  ```ts
Z
zengyawen 已提交
8906
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
Y
yangguangzhao 已提交
8907 8908
  let mapResult = ashmem.mapReadAndWriteAshmem();
  console.info("RpcTest map ashmem result is " + mapResult);
Y
yangguangzhao 已提交
8909
  var ByteArrayVar = [1, 2, 3, 4, 5];
Z
zengyawen 已提交
8910 8911 8912 8913
  let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0);
  console.log("RpcTest: write to Ashmem result is  : " + writeResult);
  ```

W
wanghaoxu 已提交
8914
### readAshmem<sup>9+</sup>
Z
zengyawen 已提交
8915

W
wanghaoxu 已提交
8916
readAshmem(size: number, offset: number): number[]
Z
zengyawen 已提交
8917 8918 8919

从此Ashmem对象关联的共享文件中读取数据。

Y
yangguangzhao 已提交
8920 8921
**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8922
**参数:**
W
wangqinxiao 已提交
8923

W
wanghaoxu 已提交
8924 8925 8926 8927
  | 参数名 | 类型   | 必填 | 说明                                               |
  | ------ | ------ | ---- | -------------------------------------------------- |
  | size   | number | 是   | 要读取的数据的大小。                               |
  | offset | number | 是   | 要读取的数据在此Ashmem对象关联的内存区间的起始位置 |
Z
zengyawen 已提交
8928

Y
yangguangzhao 已提交
8929
**返回值:**
W
wangqinxiao 已提交
8930

W
wanghaoxu 已提交
8931 8932
  | 类型     | 说明             |
  | -------- | ---------------- |
Y
yangguangzhao 已提交
8933
  | number[] | 返回读取的数据。 |
Z
zengyawen 已提交
8934

W
wanghaoxu 已提交
8935 8936
**错误码:**

C
crazy_hu 已提交
8937
以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md)
W
wanghaoxu 已提交
8938

W
wanghaoxu 已提交
8939 8940 8941
  | 错误码ID  | 错误信息 |
  | -------- | -------- |
  | 1900004 | read from ashmem failed |
Z
zengyawen 已提交
8942

Y
yangguangzhao 已提交
8943
**示例:**
Y
yangguangzhao 已提交
8944

Y
Yippo 已提交
8945
  ```ts
W
wanghaoxu 已提交
8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  ashmem.mapReadWriteAshmem();
  var ByteArrayVar = [1, 2, 3, 4, 5];
  ashmem.writeAshmem(ByteArrayVar, 5, 0);
  try {
      let readResult = ashmem.readAshmem(5, 0);
      console.log("RpcTest: read from Ashmem result is  : " + readResult);
  } catch(error) {
      console.info("Rpc read from ashmem fail, errorCode " + error.code);
      console.info("Rpc read from ashmem fail, errorMessage " + error.message);
  }
  ```

### readFromAshmem<sup>8+(deprecated)</sup>

8961 8962
>从API version 9 开始不再维护,建议使用[readAshmem](#readashmem9)类替代。

W
wanghaoxu 已提交
8963 8964
readFromAshmem(size: number, offset: number): number[]

8965
从此Ashmem对象关联的共享文件中读取数据。
W
wanghaoxu 已提交
8966 8967 8968

**系统能力**:SystemCapability.Communication.IPC.Core

W
wanghaoxu 已提交
8969
**参数:**
W
wanghaoxu 已提交
8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983

  | 参数名 | 类型   | 必填 | 说明                                               |
  | ------ | ------ | ---- | -------------------------------------------------- |
  | size   | number | 是   | 要读取的数据的大小。                               |
  | offset | number | 是   | 要读取的数据在此Ashmem对象关联的内存区间的起始位置 |

**返回值:**

  | 类型     | 说明             |
  | -------- | ---------------- |
  | number[] | 返回读取的数据。 |

**示例:**

Y
Yippo 已提交
8984
 ```ts
Z
zengyawen 已提交
8985
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
Y
yangguangzhao 已提交
8986 8987
  let mapResult = ashmem.mapReadAndWriteAshmem();
  console.info("RpcTest map ashmem result is " + mapResult);
Y
yangguangzhao 已提交
8988
  var ByteArrayVar = [1, 2, 3, 4, 5];
Z
zengyawen 已提交
8989 8990 8991 8992
  let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0);
  console.log("RpcTest: write to Ashmem result is  : " + writeResult);
  let readResult = ashmem.readFromAshmem(5, 0);
  console.log("RpcTest: read to Ashmem result is  : " + readResult);
8993
 ```