1# @ohos.rpc (RPC) 2 3The **RPC** module implements communication between processes, including inter-process communication (IPC) on a single device and remote procedure call (RPC) between processes on difference devices. IPC is implemented based on the Binder driver, and RPC is based on the DSoftBus driver. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> - This module supports return of error codes since API version 9. 10 11## Modules to Import 12 13``` 14import rpc from '@ohos.rpc'; 15``` 16 17## ErrorCode<sup>9+</sup> 18 19The APIs of this module return exceptions since API version 9. The following table lists the error codes. 20 21**System capability**: SystemCapability.Communication.IPC.Core 22 23 | Name | Value | Description | 24 | ------------------------------------- | ------- | --------------------------------------------- | 25 | CHECK_PARAM_ERROR | 401 | Parameter check failed. | 26 | OS_MMAP_ERROR | 1900001 | Failed to call mmap. | 27 | OS_IOCTL_ERROR | 1900002 | Failed to call **ioctl** with the shared memory file descriptor.| 28 | WRITE_TO_ASHMEM_ERROR | 1900003 | Failed to write data to the shared memory. | 29 | READ_FROM_ASHMEM_ERROR | 1900004 | Failed to read data from the shared memory. | 30 | ONLY_PROXY_OBJECT_PERMITTED_ERROR | 1900005 | This operation is allowed only on the proxy object. | 31 | ONLY_REMOTE_OBJECT_PERMITTED_ERROR | 1900006 | This operation is allowed only on the remote object. | 32 | COMMUNICATION_ERROR | 1900007 | Failed to communicate with the remote object over IPC. | 33 | PROXY_OR_REMOTE_OBJECT_INVALID_ERROR | 1900008 | Invalid proxy or remote object. | 34 | WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR | 1900009 | Failed to write data to MessageSequence. | 35 | READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR | 1900010 | Failed to read data from MessageSequence. | 36 | PARCEL_MEMORY_ALLOC_ERROR | 1900011 | Failed to allocate memory during serialization. | 37 | CALL_JS_METHOD_ERROR | 1900012 | Failed to invoke the JS callback. | 38 | OS_DUP_ERROR | 1900013 | Failed to call dup. | 39 40 41## MessageSequence<sup>9+</sup> 42 43Provides APIs for reading and writing data in specific format. 44During RPC or IPC, the sender can use the **write()** method provided by **MessageSequence** to write data in specific format to a **MessageSequence** object. The receiver can use the **read()** method provided by **MessageSequence** to read data in specific format from a **MessageSequence** object. The data formats include basic data types and arrays, IPC objects, interface tokens, and custom sequenceable objects. 45 46### create 47 48 static create(): MessageSequence 49 50 Creates a **MessageSequence** object. This API is a static method. 51 52**System capability**: SystemCapability.Communication.IPC.Core 53 54**Return value** 55 56 | Type | Description | 57 | --------------- | ------------------------------- | 58 | MessageSequence | **MessageSequence** object created.| 59 60**Example** 61 62 ```ts 63 let data = rpc.MessageSequence.create(); 64 console.log("RpcClient: data is " + data); 65 ``` 66 67### reclaim 68 69reclaim(): void 70 71Reclaims the **MessageSequence** object that is no longer used. 72 73**System capability**: SystemCapability.Communication.IPC.Core 74 75**Example** 76 77 ```ts 78 let reply = rpc.MessageSequence.create(); 79 reply.reclaim(); 80 ``` 81 82### writeRemoteObject 83 84writeRemoteObject(object: [IRemoteObject](#iremoteobject)): void 85 86Serializes a remote object and writes it to this **MessageSequence** object. 87 88**System capability**: SystemCapability.Communication.IPC.Core 89 90**Parameters** 91 92 | Name| Type | Mandatory| Description | 93 | ------ | ------------------------------- | ---- | ----------------------------------------- | 94 | object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageSequence** object.| 95 96**Error codes** 97 98For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 99 100 | ID| Error Message| 101 | -------- | -------- | 102 | 1900008 | proxy or remote object is invalid | 103 | 1900009 | write data to message sequence failed | 104 105**Example** 106 107 ```ts 108 class TestRemoteObject extends rpc.RemoteObject { 109 constructor(descriptor) { 110 super(descriptor); 111 } 112 } 113 let data = rpc.MessageSequence.create(); 114 let testRemoteObject = new TestRemoteObject("testObject"); 115 try { 116 data.writeRemoteObject(testRemoteObject); 117 } catch(error) { 118 console.info("Rpc write remote object fail, errorCode " + error.code); 119 console.info("Rpc write remote object fail, errorMessage " + error.message); 120 } 121 ``` 122 123### readRemoteObject 124 125readRemoteObject(): IRemoteObject 126 127Reads the remote object from **MessageSequence**. You can use this API to deserialize the **MessageSequence** object to generate an **IRemoteObject**. The remote object is read in the order in which it is written to this **MessageSequence** object. 128 129**System capability**: SystemCapability.Communication.IPC.Core 130 131**Return value** 132 133 | Type | Description | 134 | ------------------------------- | ------------------ | 135 | [IRemoteObject](#iremoteobject) | Remote object obtained.| 136 137**Error codes** 138 139For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 140 141 | ID| Error Message| 142 | -------- | -------- | 143 | 1900008 | proxy or remote object is invalid | 144 | 1900010 | read data from message sequence failed | 145 146**Example** 147 148 ```ts 149 class TestRemoteObject extends rpc.RemoteObject { 150 constructor(descriptor) { 151 super(descriptor); 152 } 153 } 154 let data = rpc.MessageSequence.create(); 155 let testRemoteObject = new TestRemoteObject("testObject"); 156 try { 157 data.writeRemoteObject(testRemoteObject); 158 let proxy = data.readRemoteObject(); 159 } catch(error) { 160 console.info("Rpc write remote object fail, errorCode " + error.code); 161 console.info("Rpc write remote object fail, errorMessage " + error.message); 162 } 163 ``` 164 165### writeInterfaceToken 166 167writeInterfaceToken(token: string): void 168 169Writes an interface token to this **MessageSequence** object. The remote object can use this interface token to verify the communication. 170 171**System capability**: SystemCapability.Communication.IPC.Core 172 173**Parameters** 174 175 | Name| Type | Mandatory| Description | 176 | ------ | ------ | ---- | ------------------ | 177 | token | string | Yes | Interface token to write.| 178 179**Error codes** 180 181For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 182 183 | ID| Error Message| 184 | -------- | -------- | 185 | 1900009 | write data to message sequence failed | 186 187**Example** 188 189 ```ts 190 let data = rpc.MessageSequence.create(); 191 try { 192 data.writeInterfaceToken("aaa"); 193 } catch(error) { 194 console.info("rpc write interface fail, errorCode " + error.code); 195 console.info("rpc write interface fail, errorMessage " + error.message); 196 } 197 ``` 198 199### readInterfaceToken 200 201readInterfaceToken(): string 202 203Reads the interface token from this **MessageSequence** object. The interface token is read in the sequence in which it is written to the **MessageSequence** object. The local object can use it to verify the communication. 204 205**System capability**: SystemCapability.Communication.IPC.Core 206 207**Return value** 208 209 | Type | Description | 210 | ------ | ------------------------ | 211 | string | Interface token obtained.| 212 213**Error codes** 214 215For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 216 217 | ID| Error Message| 218 | -------- | -------- | 219 | 1900010 | read data from message sequence failed | 220 221**Example** 222 223```ts 224class Stub extends rpc.RemoteObject { 225 onRemoteRequest(code, data, reply, option) { 226 try { 227 let interfaceToken = data.readInterfaceToken(); 228 console.log("RpcServer: interfaceToken is " + interfaceToken); 229 } catch(error) { 230 console.info("RpcServer: read interfaceToken failed, errorCode " + error.code); 231 console.info("RpcServer: read interfaceToken failed, errorMessage " + error.message); 232 } 233 return true; 234 } 235} 236``` 237 238### getSize 239 240getSize(): number 241 242Obtains the data size of this **MessageSequence** object. 243 244**System capability**: SystemCapability.Communication.IPC.Core 245 246**Return value** 247 248 | Type | Description | 249 | ------ | ----------------------------------------------- | 250 | number | Size of the **MessageSequence** object obtained, in bytes.| 251 252**Example** 253 254 ```ts 255 let data = rpc.MessageSequence.create(); 256 let size = data.getSize(); 257 console.log("RpcClient: size is " + size); 258 ``` 259 260### getCapacity 261 262getCapacity(): number 263 264Obtains the capacity of this **MessageSequence** object. 265 266**System capability**: SystemCapability.Communication.IPC.Core 267 268**Return value** 269 270 | Type | Description | 271 | ------ | ----- | 272 | number | **MessageSequence** capacity obtained, in bytes.| 273 274**Example** 275 276 ```ts 277 let data = rpc.MessageSequence.create(); 278 let result = data.getCapacity(); 279 console.log("RpcClient: capacity is " + result); 280 ``` 281 282### setSize 283 284setSize(size: number): void 285 286Sets the size of the data contained in this **MessageSequence** object. 287 288**System capability**: SystemCapability.Communication.IPC.Core 289 290**Parameters** 291 292 | Name| Type | Mandatory| Description | 293 | ------ | ------ | ---- | ------ | 294 | size | number | Yes | Data size to set, in bytes.| 295 296**Example** 297 298 ```ts 299 let data = rpc.MessageSequence.create(); 300 try { 301 data.setSize(16); 302 console.log("RpcClient: setSize is " + data.getSize()); 303 } catch(error) { 304 console.info("rpc set size of MessageSequence fail, errorCode " + error.code); 305 console.info("rpc set size of MessageSequence fail, errorMessage " + error.message); 306 } 307 ``` 308 309### setCapacity 310 311setCapacity(size: number): void 312 313Sets the storage capacity of this **MessageSequence** object. 314 315**System capability**: SystemCapability.Communication.IPC.Core 316 317**Parameters** 318 319 | Name| Type | Mandatory| Description | 320 | ------ | ------ | ---- | --------------------------------------------- | 321 | size | number | Yes | Storage capacity of the **MessageSequence** object to set, in bytes.| 322 323**Error codes** 324 325For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 326 327 | ID| Error Message| 328 | -------- | -------- | 329 | 1900011 | parcel memory alloc failed | 330 331**Example** 332 333 ```ts 334 let data = rpc.MessageSequence.create(); 335 try { 336 data.setCapacity(100); 337 console.log("RpcClient: setCapacity is " + data.getCapacity()); 338 } catch(error) { 339 console.info("rpc memory alloc fail, errorCode " + error.code); 340 console.info("rpc memory alloc fail, errorMessage " + error.message); 341 } 342 ``` 343 344### getWritableBytes 345 346getWritableBytes(): number 347 348Obtains the writable capacity (in bytes) of this **MessageSequence** object. 349 350**System capability**: SystemCapability.Communication.IPC.Core 351 352**Return value** 353 354 | Type | Description | 355 | ------ | ------ | 356 | number | Writable capacity of the **MessageSequence** instance, in bytes.| 357 358**Example** 359 360```ts 361class Stub extends rpc.RemoteObject { 362 onRemoteRequest(code, data, reply, option) { 363 let getWritableBytes = data.getWritableBytes(); 364 console.log("RpcServer: getWritableBytes is " + getWritableBytes); 365 return true; 366 } 367} 368``` 369 370### getReadableBytes 371 372getReadableBytes(): number 373 374Obtains the readable capacity of this **MessageSequence** object. 375 376**System capability**: SystemCapability.Communication.IPC.Core 377 378**Return value** 379 380 | Type | Description | 381 | ------ | ------- | 382 | number | Readable capacity of the **MessageSequence** instance, in bytes.| 383 384**Example** 385 386 ```ts 387class Stub extends rpc.RemoteObject { 388 onRemoteRequest(code, data, reply, option) { 389 let result = data.getReadableBytes(); 390 console.log("RpcServer: getReadableBytes is " + result); 391 return true; 392 } 393} 394 ``` 395 396### getReadPosition 397 398getReadPosition(): number 399 400Obtains the read position of this **MessageSequence** object. 401 402**System capability**: SystemCapability.Communication.IPC.Core 403 404**Return value** 405 406 | Type | Description | 407 | ------ | ------ | 408 | number | Read position obtained.| 409 410**Example** 411 412 ```ts 413 let data = rpc.MessageSequence.create(); 414 let readPos = data.getReadPosition(); 415 console.log("RpcClient: readPos is " + readPos); 416 ``` 417 418### getWritePosition 419 420getWritePosition(): number 421 422Obtains the write position of this **MessageSequence** object. 423 424**System capability**: SystemCapability.Communication.IPC.Core 425 426**Return value** 427 428 | Type | Description | 429 | ------ | ----- | 430 | number | Write position obtained.| 431 432**Example** 433 434 ```ts 435 let data = rpc.MessageSequence.create(); 436 data.writeInt(10); 437 let bwPos = data.getWritePosition(); 438 console.log("RpcClient: bwPos is " + bwPos); 439 ``` 440 441### rewindRead 442 443rewindRead(pos: number): void 444 445Moves the read pointer to the specified position. 446 447**System capability**: SystemCapability.Communication.IPC.Core 448 449**Parameters** 450 451 | Name| Type | Mandatory| Description | 452 | ------ | ------ | ---- | ------- | 453 | pos | number | Yes | Position from which data is to read.| 454 455**Example** 456 457 ```ts 458 let data = rpc.MessageSequence.create(); 459 data.writeInt(12); 460 data.writeString("sequence"); 461 let number = data.readInt(); 462 console.log("RpcClient: number is " + number); 463 try { 464 data.rewindRead(0); 465 } catch(error) { 466 console.info("rpc rewind read data fail, errorCode " + error.code); 467 console.info("rpc rewind read data fail, errorMessage " + error.message); 468 } 469 let number2 = data.readInt(); 470 console.log("RpcClient: rewindRead is " + number2); 471 ``` 472 473### rewindWrite 474 475rewindWrite(pos: number): void 476 477Moves the write pointer to the specified position. 478 479**System capability**: SystemCapability.Communication.IPC.Core 480 481**Parameters** 482 483 | Name| Type | Mandatory| Description | 484 | ------ | ------ | ---- | ----- | 485 | pos | number | Yes | Position from which data is to write.| 486 487**Example** 488 489 ```ts 490 let data = rpc.MessageSequence.create(); 491 data.writeInt(4); 492 try { 493 data.rewindWrite(0); 494 } catch(error) { 495 console.info("rpc rewind read data fail, errorCode " + error.code); 496 console.info("rpc rewind read data fail, errorMessage " + error.message); 497 } 498 data.writeInt(5); 499 let number = data.readInt(); 500 console.log("RpcClient: rewindWrite is: " + number); 501 ``` 502 503### writeByte 504 505writeByte(val: number): void 506 507Writes a byte value to this **MessageSequence** object. 508 509**System capability**: SystemCapability.Communication.IPC.Core 510 511**Parameters** 512 513 | Name| Type | Mandatory| Description | 514 | ------ | ------ | ---- | ----- | 515 | val | number | Yes | Byte value to write.| 516 517**Error codes** 518 519For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 520 521 | ID| Error Message| 522 | -------- | ------- | 523 | 1900009 | write data to message sequence failed | 524 525**Example** 526 527 ```ts 528 let data = rpc.MessageSequence.create(); 529 try { 530 data.writeByte(2); 531 } catch(error) { 532 console.info("rpc write byte fail, errorCode " + error.code); 533 console.info("rpc write byte fail, errorMessage" + error.message); 534 } 535 ``` 536 537### readByte 538 539readByte(): number 540 541Reads the byte value from this **MessageSequence** object. 542 543**System capability**: SystemCapability.Communication.IPC.Core 544 545**Return value** 546 547 | Type | Description | 548 | ------ | ----- | 549 | number | Byte value read.| 550 551**Error codes** 552 553For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 554 555 | ID| Error Message| 556 | ------- | -------- | 557 | 1900010 | read data from message sequence failed | 558 559**Example** 560 561 ```ts 562 let data = rpc.MessageSequence.create(); 563 try { 564 data.writeByte(2); 565 } catch(error) { 566 console.info("rpc write byte fail, errorCode " + error.code); 567 console.info("rpc write byte fail, errorMessage" + error.message); 568 } 569 try { 570 let ret = data.readByte(); 571 console.log("RpcClient: readByte is: " + ret); 572 } catch(error) { 573 console.info("rpc write byte fail, errorCode " + error.code); 574 console.info("rpc write byte fail, errorMessage" + error.message); 575 } 576 ``` 577 578### writeShort 579 580writeShort(val: number): void 581 582Writes a short integer to this **MessageSequence** object. 583 584**System capability**: SystemCapability.Communication.IPC.Core 585 586**Parameters** 587 588 | Name| Type | Mandatory| Description| 589 | ------ | ------ | --- | --- | 590 | val | number | Yes | Short integer to write.| 591 592**Error codes** 593 594For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 595 596 | ID| Error Message| 597 | -------- | -------- | 598 | 1900009 | write data to message sequence failed | 599 600**Example** 601 602 ```ts 603 let data = rpc.MessageSequence.create(); 604 try { 605 data.writeShort(8); 606 } catch(error) { 607 console.info("rpc write short fail, errorCode " + error.code); 608 console.info("rpc write short fail, errorMessage" + error.message); 609 } 610 ``` 611 612### readShort 613 614readShort(): number 615 616Reads the short integer from this **MessageSequence** object. 617 618**System capability**: SystemCapability.Communication.IPC.Core 619 620**Return value** 621 622 | Type | Description | 623 | ------ | -------------- | 624 | number | Short integer read.| 625 626**Error codes** 627 628For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 629 630 | ID| Error Message| 631 | -------- | -------- | 632 | 1900010 | read data from message sequence failed | 633 634**Example** 635 636 ```ts 637 let data = rpc.MessageSequence.create(); 638 try { 639 data.writeShort(8); 640 } catch(error) { 641 console.info("rpc write short fail, errorCode " + error.code); 642 console.info("rpc write short fail, errorMessage" + error.message); 643 } 644 try { 645 let ret = data.readShort(); 646 console.log("RpcClient: readByte is: " + ret); 647 } catch(error) { 648 console.info("rpc read short fail, errorCode " + error.code); 649 console.info("rpc read short fail, errorMessage" + error.message); 650 } 651 ``` 652 653### writeInt 654 655writeInt(val: number): void 656 657Writes an integer to this **MessageSequence** object. 658 659**System capability**: SystemCapability.Communication.IPC.Core 660 661**Parameters** 662 663 | Name| Type | Mandatory| Description | 664 | ------ | ------ | ---- | ---------------- | 665 | val | number | Yes | Integer to write.| 666 667**Error codes** 668 669For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 670 671 | ID| Error Message| 672 | -------- | -------- | 673 | 1900009 | write data to message sequence failed | 674 675**Example** 676 677 ```ts 678 let data = rpc.MessageSequence.create(); 679 try { 680 data.writeInt(10); 681 } catch(error) { 682 console.info("rpc write int fail, errorCode " + error.code); 683 console.info("rpc write int fail, errorMessage" + error.message); 684 } 685 ``` 686 687### readInt 688 689readInt(): number 690 691Reads the integer from this **MessageSequence** object. 692 693**System capability**: SystemCapability.Communication.IPC.Core 694 695**Return value** 696 697 | Type | Description | 698 | ------ | ------------ | 699 | number | Integer read.| 700 701**Error codes** 702 703For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 704 705 | ID| Error Message| 706 | -------- | -------- | 707 | 1900010 | read data from message sequence failed | 708 709**Example** 710 711 ```ts 712 let data = rpc.MessageSequence.create(); 713 try { 714 data.writeInt(10); 715 } catch(error) { 716 console.info("rpc write int fail, errorCode " + error.code); 717 console.info("rpc write int fail, errorMessage" + error.message); 718 } 719 try { 720 let ret = data.readInt(); 721 console.log("RpcClient: readInt is " + ret); 722 } catch(error) { 723 console.info("rpc read int fail, errorCode " + error.code); 724 console.info("rpc read int fail, errorMessage" + error.message); 725 } 726 ``` 727 728### writeLong 729 730writeLong(val: number): void 731 732Writes a long integer to this **MessageSequence** object. 733 734**System capability**: SystemCapability.Communication.IPC.Core 735 736**Parameters** 737 738 | Name| Type | Mandatory| Description | 739 | ------ | ------ | ---- | ---------------- | 740 | val | number | Yes | Long integer to write.| 741 742**Error codes** 743 744For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 745 746 | ID| Error Message| 747 | -------- | -------- | 748 | 1900009 | write data to message sequence failed | 749 750**Example** 751 752 ```ts 753 let data = rpc.MessageSequence.create(); 754 try { 755 data.writeLong(10000); 756 } catch(error) { 757 console.info("rpc write long fail, errorCode " + error.code); 758 console.info("rpc write long fail, errorMessage" + error.message); 759 } 760 ``` 761 762### readLong 763 764readLong(): number 765 766Reads the long integer from this **MessageSequence** object. 767 768**System capability**: SystemCapability.Communication.IPC.Core 769 770**Return value** 771 772 | Type | Description | 773 | ------ | -------------- | 774 | number | Long integer read.| 775 776**Error codes** 777 778For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 779 780 | ID| Error Message| 781 | -------- | -------- | 782 | 1900010 | read data from message sequence failed | 783 784**Example** 785 786 ```ts 787 let data = rpc.MessageSequence.create(); 788 try { 789 data.writeLong(10000); 790 } catch(error) { 791 console.info("rpc write long fail, errorCode " + error.code); 792 console.info("rpc write long fail, errorMessage" + error.message); 793 } 794 try { 795 let ret = data.readLong(); 796 console.log("RpcClient: readLong is " + ret); 797 } catch(error) { 798 console.info("rpc read long fail, errorCode " + error.code); 799 console.info("rpc read long fail, errorMessage" + error.message); 800 } 801 ``` 802 803### writeFloat 804 805writeFloat(val: number): void 806 807Writes a floating-point number to this **MessageSequence** object. 808 809**System capability**: SystemCapability.Communication.IPC.Core 810 811**Parameters** 812 813 | Name| Type | Mandatory| Description | 814 | ------ | ------ | ---- | ----- | 815 | val | number | Yes | Floating-point number to write.| 816 817**Error codes** 818 819For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 820 821 | ID| Error Message| 822 | -------- | -------- | 823 | 1900009 | write data to message sequence failed | 824 825**Example** 826 827 ```ts 828 let data = rpc.MessageSequence.create(); 829 try { 830 data.writeFloat(1.2); 831 } catch(error) { 832 console.info("rpc write float fail, errorCode " + error.code); 833 console.info("rpc write float fail, errorMessage" + error.message); 834 } 835 ``` 836 837### readFloat 838 839readFloat(): number 840 841Reads the floating-pointer number from this **MessageSequence** object. 842 843**System capability**: SystemCapability.Communication.IPC.Core 844 845**Return value** 846 847 | Type | Description | 848 | ------ | ------------ | 849 | number | Floating-point number read.| 850 851**Error codes** 852 853For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 854 855 | ID| Error Message| 856 | -------- | -------- | 857 | 1900010 | read data from message sequence failed | 858 859**Example** 860 861 ```ts 862 let data = rpc.MessageSequence.create(); 863 try { 864 data.writeFloat(1.2); 865 } catch(error) { 866 console.info("rpc write float fail, errorCode " + error.code); 867 console.info("rpc write float fail, errorMessage" + error.message); 868 } 869 try { 870 let ret = data.readFloat(); 871 console.log("RpcClient: readFloat is " + ret); 872 } catch(error) { 873 console.info("rpc read float fail, errorCode " + error.code); 874 console.info("rpc read float fail, errorMessage" + error.message); 875 } 876 ``` 877 878### writeDouble 879 880writeDouble(val: number): void 881 882Writes a double-precision floating-point number to this **MessageSequence** object. 883 884**System capability**: SystemCapability.Communication.IPC.Core 885 886**Parameters** 887 888 | Name| Type | Mandatory| Description | 889 | ------ | ------ | ---- | ---------------------- | 890 | val | number | Yes | Double-precision floating-point number to write.| 891 892**Error codes** 893 894For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 895 896 | ID| Error Message| 897 | -------- | -------- | 898 | 1900009 | write data to message sequence failed | 899 900**Example** 901 902 ```ts 903 let data = rpc.MessageSequence.create(); 904 try { 905 data.writeDouble(10.2); 906 } catch(error) { 907 console.info("rpc read float fail, errorCode " + error.code); 908 console.info("rpc read float fail, errorMessage" + error.message); 909 } 910 ``` 911 912### readDouble 913 914readDouble(): number 915 916Reads the double-precision floating-point number from this **MessageSequence** object. 917 918**System capability**: SystemCapability.Communication.IPC.Core 919 920**Return value** 921 922 | Type | Description | 923 | ------ | ------------------ | 924 | number | Double-precision floating-point number read.| 925 926**Error codes** 927 928For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 929 930 | ID| Error Message| 931 | -------- | -------- | 932 | 1900010 | read data from message sequence failed | 933 934**Example** 935 936 ```ts 937 let data = rpc.MessageSequence.create(); 938 try { 939 data.writeDouble(10.2); 940 } catch(error) { 941 console.info("rpc write double fail, errorCode " + error.code); 942 console.info("rpc write double fail, errorMessage" + error.message); 943 } 944 try { 945 let ret = data.readDouble(); 946 console.log("RpcClient: readDouble is " + ret); 947 } catch(error) { 948 console.info("rpc read double fail, errorCode " + error.code); 949 console.info("rpc read double fail, errorMessage" + error.message); 950 } 951 ``` 952 953### writeBoolean 954 955writeBoolean(val: boolean): void 956 957Writes a Boolean value to this **MessageSequence** object. 958 959**System capability**: SystemCapability.Communication.IPC.Core 960 961**Parameters** 962 963 | Name| Type | Mandatory| Description | 964 | ------ | ------- | ---- | ---------------- | 965 | val | boolean | Yes | Boolean value to write.| 966 967**Error codes** 968 969For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 970 971 | ID| Error Message| 972 | -------- | -------- | 973 | 1900009 | write data to message sequence failed | 974 975**Example** 976 977 ```ts 978 let data = rpc.MessageSequence.create(); 979 try { 980 data.writeBoolean(false); 981 } catch(error) { 982 console.info("rpc write boolean fail, errorCode " + error.code); 983 console.info("rpc write boolean fail, errorMessage" + error.message); 984 } 985 ``` 986 987### readBoolean 988 989readBoolean(): boolean 990 991Reads the Boolean value from this **MessageSequence** object. 992 993**System capability**: SystemCapability.Communication.IPC.Core 994 995**Return value** 996 997 | Type | Description | 998 | ------- | -------------------- | 999 | boolean | Boolean value read.| 1000 1001**Error codes** 1002 1003For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1004 1005 | ID| Error Message| 1006 | -------- | -------- | 1007 | 1900010 | read data from message sequence failed | 1008 1009**Example** 1010 1011 ```ts 1012 let data = rpc.MessageSequence.create(); 1013 try { 1014 data.writeBoolean(false); 1015 } catch(error) { 1016 console.info("rpc write boolean fail, errorCode " + error.code); 1017 console.info("rpc write boolean fail, errorMessage" + error.message); 1018 } 1019 try { 1020 let ret = data.readBoolean(); 1021 console.log("RpcClient: readBoolean is " + ret); 1022 } catch(error) { 1023 console.info("rpc read boolean fail, errorCode " + error.code); 1024 console.info("rpc read boolean fail, errorMessage" + error.message); 1025 } 1026 ``` 1027 1028### writeChar 1029 1030writeChar(val: number): void 1031 1032Writes a character to this **MessageSequence** object. 1033 1034**System capability**: SystemCapability.Communication.IPC.Core 1035 1036**Parameters** 1037 1038 | Name| Type | Mandatory| Description | 1039 | ------ | ------ | ---- | -------------------- | 1040 | val | number | Yes | Single character to write.| 1041 1042**Error codes** 1043 1044For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1045 1046 | ID| Error Message| 1047 | -------- | -------- | 1048 | 1900009 | write data to message sequence failed | 1049 1050**Example** 1051 1052 ```ts 1053 let data = rpc.MessageSequence.create(); 1054 try { 1055 data.writeChar(97); 1056 } catch(error) { 1057 console.info("rpc write char fail, errorCode " + error.code); 1058 console.info("rpc write char fail, errorMessage" + error.message); 1059 } 1060 ``` 1061 1062### readChar 1063 1064readChar(): number 1065 1066Reads the character from this **MessageSequence** object. 1067 1068**System capability**: SystemCapability.Communication.IPC.Core 1069 1070**Return value** 1071 1072 | Type | Description| 1073 | ------ | ---- | 1074 | number | Character read.| 1075 1076**Error codes** 1077 1078For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1079 1080 | ID| Error Message| 1081 | -------- | -------- | 1082 | 1900010 | read data from message sequence failed | 1083 1084**Example** 1085 1086 ```ts 1087 let data = rpc.MessageSequence.create(); 1088 try { 1089 data.writeChar(97); 1090 } catch(error) { 1091 console.info("rpc write char fail, errorCode " + error.code); 1092 console.info("rpc write char fail, errorMessage" + error.message); 1093 } 1094 try { 1095 let ret = data.readChar(); 1096 console.log("RpcClient: readChar is " + ret); 1097 } catch(error) { 1098 console.info("rpc read char fail, errorCode " + error.code); 1099 console.info("rpc read char fail, errorMessage" + error.message); 1100 } 1101 ``` 1102 1103### writeString 1104 1105writeString(val: string): void 1106 1107Writes a string to this **MessageSequence** object. 1108 1109**System capability**: SystemCapability.Communication.IPC.Core 1110 1111**Parameters** 1112 1113 | Name| Type | Mandatory| Description | 1114 | ------ | ------ | ---- | ----------------------------------------- | 1115 | val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 1116 1117**Error codes** 1118 1119For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1120 1121 | ID| Error Message| 1122 | -------- | -------- | 1123 | 1900009 | write data to message sequence failed | 1124 1125**Example** 1126 1127 ```ts 1128 let data = rpc.MessageSequence.create(); 1129 try { 1130 data.writeString('abc'); 1131 } catch(error) { 1132 console.info("rpc write string fail, errorCode " + error.code); 1133 console.info("rpc write string fail, errorMessage" + error.message); 1134 } 1135 ``` 1136 1137### readString 1138 1139readString(): string 1140 1141Reads the string from this **MessageSequence** object. 1142 1143**System capability**: SystemCapability.Communication.IPC.Core 1144 1145**Return value** 1146 1147 | Type | Description | 1148 | ------ | -------------- | 1149 | string | String read.| 1150 1151**Error codes** 1152 1153For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1154 1155 | ID| Error Message| 1156 | -------- | -------- | 1157 | 1900010 | read data from message sequence failed | 1158 1159**Example** 1160 1161 ```ts 1162 let data = rpc.MessageSequence.create(); 1163 try { 1164 data.writeString('abc'); 1165 } catch(error) { 1166 console.info("rpc write string fail, errorCode " + error.code); 1167 console.info("rpc write string fail, errorMessage" + error.message); 1168 } 1169 try { 1170 let ret = data.readString(); 1171 console.log("RpcClient: readString is " + ret); 1172 } catch(error) { 1173 console.info("rpc read string fail, errorCode " + error.code); 1174 console.info("rpc read string fail, errorMessage" + error.message); 1175 } 1176 ``` 1177 1178### writeParcelable 1179 1180writeParcelable(val: Parcelable): void 1181 1182Writes a **Parcelable** object to this **MessageSequence** object. 1183 1184**System capability**: SystemCapability.Communication.IPC.Core 1185 1186**Parameters** 1187 1188 | Name| Type| Mandatory| Description| 1189 | ------ | --------- | ---- | ------ | 1190 | val | Parcelable | Yes | **Parcelable** object to write.| 1191 1192**Error codes** 1193 1194For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1195 1196 | ID| Error Message| 1197 | -------- | -------- | 1198 | 1900009 | write data to message sequence failed | 1199 1200**Example** 1201 1202 ```ts 1203 class MySequenceable { 1204 num: number; 1205 str: string; 1206 constructor(num, str) { 1207 this.num = num; 1208 this.str = str; 1209 } 1210 marshalling(messageSequence) { 1211 messageSequence.writeInt(this.num); 1212 messageSequence.writeString(this.str); 1213 return true; 1214 } 1215 unmarshalling(messageSequence) { 1216 this.num = messageSequence.readInt(); 1217 this.str = messageSequence.readString(); 1218 return true; 1219 } 1220 } 1221 let parcelable = new MySequenceable(1, "aaa"); 1222 let data = rpc.MessageSequence.create(); 1223 try { 1224 data.writeParcelable(parcelable); 1225 } catch(error) { 1226 console.info("rpc write parcelable fail, errorCode " + error.code); 1227 console.info("rpc write parcelable fail, errorMessage" + error.message); 1228 } 1229 ``` 1230 1231### readParcelable 1232 1233readParcelable(dataIn: Parcelable): void 1234 1235Reads a **Parcelable** object from this **MessageSequence** object to the specified object (**dataIn**). 1236 1237**System capability**: SystemCapability.Communication.IPC.Core 1238 1239**Parameters** 1240 1241 | Name| Type | Mandatory| Description | 1242 | ------ | ------------------------- | ---- | ----------------------------------------- | 1243 | dataIn | Parcelable | Yes | **Parcelable** object to read.| 1244 1245**Error codes** 1246 1247For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1248 1249 | ID| Error Message| 1250 | -------- | -------- | 1251 | 1900010 | read data from message sequence failed | 1252 | 1900012 | call js callback function failed | 1253 1254**Example** 1255 1256 ```ts 1257 class MySequenceable { 1258 num: number; 1259 str: string; 1260 constructor(num, str) { 1261 this.num = num; 1262 this.str = str; 1263 } 1264 marshalling(messageSequence) { 1265 messageSequence.writeInt(this.num); 1266 messageSequence.writeString(this.str); 1267 return true; 1268 } 1269 unmarshalling(messageSequence) { 1270 this.num = messageSequence.readInt(); 1271 this.str = messageSequence.readString(); 1272 return true; 1273 } 1274 } 1275 let parcelable = new MySequenceable(1, "aaa"); 1276 let data = rpc.MessageSequence.create(); 1277 data.writeParcelable(parcelable); 1278 let ret = new MySequenceable(0, ""); 1279 try { 1280 data.readParcelable(ret); 1281 }catch(error) { 1282 console.info("rpc read parcelable fail, errorCode " + error.code); 1283 console.info("rpc read parcelable fail, errorMessage" + error.message); 1284 } 1285 ``` 1286 1287### writeByteArray 1288 1289writeByteArray(byteArray: number[]): void 1290 1291Writes a byte array to this **MessageSequence** object. 1292 1293**System capability**: SystemCapability.Communication.IPC.Core 1294 1295**Parameters** 1296 1297 | Name | Type | Mandatory| Description | 1298 | --------- | -------- | ---- | ------------------ | 1299 | byteArray | number[] | Yes | Byte array to write.| 1300 1301**Error codes** 1302 1303For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1304 1305 | ID| Error Message| 1306 | -------- | -------- | 1307 | 1900009 | write data to message sequence failed | 1308 1309**Example** 1310 1311 ```ts 1312 let data = rpc.MessageSequence.create(); 1313 let ByteArrayVar = [1, 2, 3, 4, 5]; 1314 try { 1315 data.writeByteArray(ByteArrayVar); 1316 } catch(error) { 1317 console.info("rpc write byteArray fail, errorCode " + error.code); 1318 console.info("rpc write byteArray fail, errorMessage" + error.message); 1319 } 1320 ``` 1321 1322### readByteArray 1323 1324readByteArray(dataIn: number[]): void 1325 1326Reads a byte array from this **MessageSequence** object. 1327 1328**System capability**: SystemCapability.Communication.IPC.Core 1329 1330**Parameters** 1331 1332 | Name| Type | Mandatory| Description | 1333 | ------ | -------- | ---- | ------------------ | 1334 | dataIn | number[] | Yes | Byte array to read.| 1335 1336**Error codes** 1337 1338For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1339 1340 | ID| Error Message| 1341 | -------- | -------- | 1342 | 1900010 | read data from message sequence failed | 1343 1344**Example** 1345 1346 ```ts 1347 let data = rpc.MessageSequence.create(); 1348 let ByteArrayVar = [1, 2, 3, 4, 5]; 1349 try { 1350 data.writeByteArray(ByteArrayVar); 1351 } catch(error) { 1352 console.info("rpc write byteArray fail, errorCode " + error.code); 1353 console.info("rpc write byteArray fail, errorMessage" + error.message); 1354 } 1355 try { 1356 let array = new Array(5); 1357 data.readByteArray(array); 1358 } catch(error) { 1359 console.info("rpc write byteArray fail, errorCode " + error.code); 1360 console.info("rpc write byteArray fail, errorMessage" + error.message); 1361 } 1362 ``` 1363 1364### readByteArray 1365 1366readByteArray(): number[] 1367 1368Reads the byte array from this **MessageSequence** object. 1369 1370**System capability**: SystemCapability.Communication.IPC.Core 1371 1372**Return value** 1373 1374 | Type | Description | 1375 | -------- | -------------- | 1376 | number[] | Byte array read.| 1377 1378**Error codes** 1379 1380For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1381 1382 | ID| Error Message| 1383 | -------- | -------- | 1384 | 1900010 | read data from message sequence failed | 1385 1386**Example** 1387 1388 ```ts 1389 let data = rpc.MessageSequence.create(); 1390 let byteArrayVar = [1, 2, 3, 4, 5]; 1391 try { 1392 data.writeByteArray(byteArrayVar); 1393 } catch(error) { 1394 console.info("rpc write byteArray fail, errorCode " + error.code); 1395 console.info("rpc write byteArray fail, errorMessage" + error.message); 1396 } 1397 try { 1398 let array = data.readByteArray(); 1399 console.log("RpcClient: readByteArray is " + array); 1400 } catch(error) { 1401 console.info("rpc read byteArray fail, errorCode " + error.code); 1402 console.info("rpc read byteArray fail, errorMessage" + error.message); 1403 } 1404 ``` 1405 1406### writeShortArray 1407 1408writeShortArray(shortArray: number[]): void 1409 1410Writes a short array to this **MessageSequence** object. 1411 1412**System capability**: SystemCapability.Communication.IPC.Core 1413 1414**Parameters** 1415 1416 | Name | Type | Mandatory| Description | 1417 | ---------- | -------- | ---- | -------------------- | 1418 | shortArray | number[] | Yes | Short array to write.| 1419 1420**Error codes** 1421 1422For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1423 1424 | ID| Error Message| 1425 | -------- | -------- | 1426 | 1900009 | write data to message sequence failed | 1427 1428**Example** 1429 1430 ```ts 1431 let data = rpc.MessageSequence.create(); 1432 try { 1433 data.writeShortArray([11, 12, 13]); 1434 } catch(error) { 1435 console.info("rpc read byteArray fail, errorCode " + error.code); 1436 console.info("rpc read byteArray fail, errorMessage" + error.message); 1437 } 1438 ``` 1439 1440### readShortArray 1441 1442readShortArray(dataIn: number[]): void 1443 1444Reads a short array from this **MessageSequence** object. 1445 1446**System capability**: SystemCapability.Communication.IPC.Core 1447 1448**Parameters** 1449 1450 | Name| Type | Mandatory| Description | 1451 | ------ | -------- | ---- | -------------------- | 1452 | dataIn | number[] | Yes | Short array to read.| 1453 1454**Error codes** 1455 1456For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1457 1458 | ID| Error Message| 1459 | -------- | -------- | 1460 | 1900010 | read data from message sequence failed | 1461 1462**Example** 1463 1464 ```ts 1465 let data = rpc.MessageSequence.create(); 1466 try { 1467 data.writeShortArray([11, 12, 13]); 1468 } catch(error) { 1469 console.info("rpc write shortArray fail, errorCode " + error.code); 1470 console.info("rpc write shortArray fail, errorMessage" + error.message); 1471 } 1472 try { 1473 let array = new Array(3); 1474 data.readShortArray(array); 1475 } catch(error) { 1476 console.info("rpc read shortArray fail, errorCode " + error.code); 1477 console.info("rpc read shortArray fail, errorMessage" + error.message); 1478 } 1479 ``` 1480 1481### readShortArray 1482 1483readShortArray(): number[] 1484 1485Reads the short array from this **MessageSequence** object. 1486 1487**System capability**: SystemCapability.Communication.IPC.Core 1488 1489**Return value** 1490 1491 | Type | Description | 1492 | -------- | ---------------- | 1493 | number[] | Short array read.| 1494 1495**Error codes** 1496 1497For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1498 1499 | ID| Error Message| 1500 | -------- | -------- | 1501 | 1900010 | read data from message sequence failed | 1502 1503**Example** 1504 1505 ```ts 1506 let data = rpc.MessageSequence.create(); 1507 try { 1508 data.writeShortArray([11, 12, 13]); 1509 } catch(error) { 1510 console.info("rpc write shortArray fail, errorCode " + error.code); 1511 console.info("rpc write shortArray fail, errorMessage" + error.message); 1512 } 1513 try { 1514 let array = data.readShortArray(); 1515 console.log("RpcClient: readShortArray is " + array); 1516 } catch(error) { 1517 console.info("rpc read shortArray fail, errorCode " + error.code); 1518 console.info("rpc read shortArray fail, errorMessage" + error.message); 1519 } 1520 ``` 1521 1522### writeIntArray 1523 1524writeIntArray(intArray: number[]): void 1525 1526Writes an integer array to this **MessageSequence** object. 1527 1528**System capability**: SystemCapability.Communication.IPC.Core 1529 1530**Parameters** 1531 1532 | Name | Type | Mandatory| Description | 1533 | -------- | -------- | ---- | ------------------ | 1534 | intArray | number[] | Yes | Integer array to write.| 1535 1536**Error codes** 1537 1538For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1539 1540 | ID| Error Message| 1541 | -------- | -------- | 1542 | 1900009 | write data to message sequence failed | 1543 1544**Example** 1545 1546 ```ts 1547 let data = rpc.MessageSequence.create(); 1548 try { 1549 data.writeIntArray([100, 111, 112]); 1550 } catch(error) { 1551 console.info("rpc write intArray fail, errorCode " + error.code); 1552 console.info("rpc write intArray fail, errorMessage" + error.message); 1553 } 1554 ``` 1555 1556### readIntArray 1557 1558readIntArray(dataIn: number[]): void 1559 1560Reads an integer array from this **MessageSequence** object. 1561 1562**System capability**: SystemCapability.Communication.IPC.Core 1563 1564**Parameters** 1565 1566 | Name| Type | Mandatory| Description | 1567 | ------ | -------- | ---- | ------------------ | 1568 | dataIn | number[] | Yes | Integer array to read.| 1569 1570**Error codes** 1571 1572For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1573 1574 | ID| Error Message| 1575 | -------- | -------- | 1576 | 1900010 | read data from message sequence failed | 1577 1578**Example** 1579 1580 ```ts 1581 let data = rpc.MessageSequence.create(); 1582 try { 1583 data.writeIntArray([100, 111, 112]); 1584 } catch(error) { 1585 console.info("rpc write intArray fail, errorCode " + error.code); 1586 console.info("rpc write intArray fail, errorMessage" + error.message); 1587 } 1588 let array = new Array(3); 1589 try { 1590 data.readIntArray(array); 1591 } catch(error) { 1592 console.info("rpc read intArray fail, errorCode " + error.code); 1593 console.info("rpc read intArray fail, errorMessage" + error.message); 1594 } 1595 ``` 1596 1597### readIntArray 1598 1599readIntArray(): number[] 1600 1601Reads the integer array from this **MessageSequence** object. 1602 1603**System capability**: SystemCapability.Communication.IPC.Core 1604 1605**Return value** 1606 1607 | Type | Description | 1608 | -------- | -------------- | 1609 | number[] | Integer array read.| 1610 1611**Error codes** 1612 1613For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1614 1615 | ID| Error Message| 1616 | -------- | -------- | 1617 | 1900010 | read data from message sequence failed | 1618 1619**Example** 1620 1621 ```ts 1622 let data = rpc.MessageSequence.create(); 1623 try { 1624 data.writeIntArray([100, 111, 112]); 1625 } catch(error) { 1626 console.info("rpc write intArray fail, errorCode " + error.code); 1627 console.info("rpc write intArray fail, errorMessage" + error.message); 1628 } 1629 try { 1630 let array = data.readIntArray(); 1631 console.log("RpcClient: readIntArray is " + array); 1632 } catch(error) { 1633 console.info("rpc read intArray fail, errorCode " + error.code); 1634 console.info("rpc read intArray fail, errorMessage" + error.message); 1635 } 1636 ``` 1637 1638### writeLongArray 1639 1640writeLongArray(longArray: number[]): void 1641 1642Writes a long array to this **MessageSequence** object. 1643 1644**System capability**: SystemCapability.Communication.IPC.Core 1645 1646**Parameters** 1647 1648 | Name | Type | Mandatory| Description | 1649 | --------- | -------- | ---- | -------------------- | 1650 | longArray | number[] | Yes | Long array to write.| 1651 1652**Error codes** 1653 1654For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1655 1656 | ID| Error Message| 1657 | -------- | -------- | 1658 | 1900009 | write data to message sequence failed | 1659 1660**Example** 1661 1662 ```ts 1663 let data = rpc.MessageSequence.create(); 1664 try { 1665 data.writeLongArray([1111, 1112, 1113]); 1666 }catch(error){ 1667 console.info("rpc write longArray fail, errorCode " + error.code); 1668 console.info("rpc write longArray fail, errorMessage" + error.message); 1669 } 1670 ``` 1671 1672### readLongArray 1673 1674readLongArray(dataIn: number[]): void 1675 1676Reads a long array from this **MessageSequence** object. 1677 1678**System capability**: SystemCapability.Communication.IPC.Core 1679 1680**Parameters** 1681 1682 | Name| Type | Mandatory| Description | 1683 | ------ | -------- | ---- | -------------------- | 1684 | dataIn | number[] | Yes | Long array to read.| 1685 1686**Error codes** 1687 1688For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1689 1690 | ID| Error Message| 1691 | -------- | -------- | 1692 | 1900010 | read data from message sequence failed | 1693 1694**Example** 1695 1696 ```ts 1697 let data = rpc.MessageSequence.create(); 1698 try { 1699 data.writeLongArray([1111, 1112, 1113]); 1700 } catch(error) { 1701 console.info("rpc write longArray fail, errorCode " + error.code); 1702 console.info("rpc write longArray fail, errorMessage" + error.message); 1703 } 1704 let array = new Array(3); 1705 try { 1706 data.readLongArray(array); 1707 } catch(error) { 1708 console.info("rpc read longArray fail, errorCode " + error.code); 1709 console.info("rpc read longArray fail, errorMessage" + error.message); 1710 } 1711 ``` 1712 1713### readLongArray 1714 1715readLongArray(): number[] 1716 1717Reads the long array from this **MessageSequence** object. 1718 1719**System capability**: SystemCapability.Communication.IPC.Core 1720 1721**Return value** 1722 1723 | Type | Description | 1724 | -------- | ---------------- | 1725 | number[] | Long array read.| 1726 1727**Error codes** 1728 1729For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1730 1731 | ID| Error Message| 1732 | -------- | -------- | 1733 | 1900010 | read data from message sequence failed | 1734 1735**Example** 1736 1737 ```ts 1738 let data = rpc.MessageSequence.create(); 1739 try { 1740 data.writeLongArray([1111, 1112, 1113]); 1741 } catch(error) { 1742 console.info("rpc write longArray fail, errorCode " + error.code); 1743 console.info("rpc write longArray fail, errorMessage" + error.message); 1744 } 1745 try { 1746 let array = data.readLongArray(); 1747 console.log("RpcClient: readLongArray is " + array); 1748 } catch(error) { 1749 console.info("rpc read longArray fail, errorCode " + error.code); 1750 console.info("rpc read longArray fail, errorMessage" + error.message); 1751 } 1752 ``` 1753 1754### writeFloatArray 1755 1756writeFloatArray(floatArray: number[]): void 1757 1758Writes a floating-point array to this **MessageSequence** object. 1759 1760**System capability**: SystemCapability.Communication.IPC.Core 1761 1762**Parameters** 1763 1764 | Name | Type | Mandatory| Description | 1765 | ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 1766 | floatArray | number[] | Yes | Floating-point array to write. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 1767 1768**Error codes** 1769 1770For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1771 1772 | ID| Error Message| 1773 | -------- | -------- | 1774 | 1900009 | write data to message sequence failed | 1775 1776**Example** 1777 1778 ```ts 1779 let data = rpc.MessageSequence.create(); 1780 try { 1781 data.writeFloatArray([1.2, 1.3, 1.4]); 1782 } catch(error) { 1783 console.info("rpc write floatArray fail, errorCode " + error.code); 1784 console.info("rpc write floatArray fail, errorMessage" + error.message); 1785 } 1786 ``` 1787 1788### readFloatArray 1789 1790readFloatArray(dataIn: number[]): void 1791 1792Reads a floating-point array from this **MessageSequence** object. 1793 1794**System capability**: SystemCapability.Communication.IPC.Core 1795 1796**Parameters** 1797 1798 | Name| Type | Mandatory| Description | 1799 | ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 1800 | dataIn | number[] | Yes | Floating-point array to read. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 1801 1802**Error codes** 1803 1804For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1805 1806 | ID| Error Message| 1807 | -------- | -------- | 1808 | 1900010 | read data from message sequence failed | 1809 1810**Example** 1811 1812 ```ts 1813 let data = rpc.MessageSequence.create(); 1814 try { 1815 data.writeFloatArray([1.2, 1.3, 1.4]); 1816 }catch(error){ 1817 console.info("rpc write floatArray fail, errorCode " + error.code); 1818 console.info("rpc write floatArray fail, errorMessage" + error.message); 1819 } 1820 let array = new Array(3); 1821 try { 1822 data.readFloatArray(array); 1823 } catch(error) { 1824 console.info("rpc read floatArray fail, errorCode " + error.code); 1825 console.info("rpc read floatArray fail, errorMessage" + error.message); 1826 } 1827 ``` 1828 1829### readFloatArray 1830 1831readFloatArray(): number[] 1832 1833Reads the floating-point array from this **MessageSequence** object. 1834 1835**System capability**: SystemCapability.Communication.IPC.Core 1836 1837**Return value** 1838 1839 | Type | Description | 1840 | -------- | -------------- | 1841 | number[] | Floating-point array read.| 1842 1843**Error codes** 1844 1845For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1846 1847 | ID| Error Message| 1848 | -------- | -------- | 1849 | 1900010 | read data from message sequence failed | 1850 1851**Example** 1852 1853 ```ts 1854 let data = rpc.MessageSequence.create(); 1855 try { 1856 data.writeFloatArray([1.2, 1.3, 1.4]); 1857 } catch(error) { 1858 console.info("rpc write floatArray fail, errorCode " + error.code); 1859 console.info("rpc write floatArray fail, errorMessage" + error.message); 1860 } 1861 try { 1862 let array = data.readFloatArray(); 1863 console.log("RpcClient: readFloatArray is " + array); 1864 } catch(error) { 1865 console.info("rpc read floatArray fail, errorCode " + error.code); 1866 console.info("rpc read floatArray fail, errorMessage" + error.message); 1867 } 1868 ``` 1869 1870### writeDoubleArray 1871 1872writeDoubleArray(doubleArray: number[]): void 1873 1874Writes a double-precision floating-point array to this **MessageSequence** object. 1875 1876**System capability**: SystemCapability.Communication.IPC.Core 1877 1878**Parameters** 1879 1880 | Name | Type | Mandatory| Description | 1881 | ----------- | -------- | ---- | ------------------------ | 1882 | doubleArray | number[] | Yes | Double-precision floating-point array to write.| 1883 1884**Error codes** 1885 1886For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1887 1888 | ID| Error Message| 1889 | -------- | -------- | 1890 | 1900009 | write data to message sequence failed | 1891 1892**Example** 1893 1894 ```ts 1895 let data = rpc.MessageSequence.create(); 1896 try { 1897 data.writeDoubleArray([11.1, 12.2, 13.3]); 1898 } catch(error) { 1899 console.info("rpc write doubleArray fail, errorCode " + error.code); 1900 console.info("rpc write doubleArray fail, errorMessage" + error.message); 1901 } 1902 ``` 1903 1904### readDoubleArray 1905 1906readDoubleArray(dataIn: number[]): void 1907 1908Reads a double-precision floating-point array from this **MessageSequence** object. 1909 1910**System capability**: SystemCapability.Communication.IPC.Core 1911 1912**Parameters** 1913 1914 | Name| Type | Mandatory| Description | 1915 | ------ | -------- | ---- | ------------------------ | 1916 | dataIn | number[] | Yes | Double-precision floating-point array to read.| 1917 1918**Error codes** 1919 1920For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1921 1922 | ID| Error Message| 1923 | -------- | -------- | 1924 | 1900010 | read data from message sequence failed | 1925 1926**Example** 1927 1928 ```ts 1929 let data = rpc.MessageSequence.create(); 1930 try { 1931 data.writeDoubleArray([11.1, 12.2, 13.3]); 1932 } catch(error) { 1933 console.info("rpc write doubleArray fail, errorCode " + error.code); 1934 console.info("rpc write doubleArray fail, errorMessage" + error.message); 1935 } 1936 let array = new Array(3); 1937 try { 1938 data.readDoubleArray(array); 1939 } catch(error) { 1940 console.info("rpc read doubleArray fail, errorCode " + error.code); 1941 console.info("rpc read doubleArray fail, errorMessage" + error.message); 1942 } 1943 ``` 1944 1945### readDoubleArray 1946 1947readDoubleArray(): number[] 1948 1949Reads the double-precision floating-point array from this **MessageSequence** object. 1950 1951**System capability**: SystemCapability.Communication.IPC.Core 1952 1953**Return value** 1954 1955 | Type | Description | 1956 | -------- | -------------------- | 1957 | number[] | Double-precision floating-point array read.| 1958 1959**Error codes** 1960 1961For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1962 1963 | ID| Error Message| 1964 | -------- | -------- | 1965 | 1900010 | read data from message sequence failed | 1966 1967**Example** 1968 1969 ```ts 1970 let data = rpc.MessageSequence.create(); 1971 try { 1972 data.writeDoubleArray([11.1, 12.2, 13.3]); 1973 } catch(error) { 1974 console.info("rpc write doubleArray fail, errorCode " + error.code); 1975 console.info("rpc write doubleArray fail, errorMessage" + error.message); 1976 } 1977 try { 1978 let array = data.readDoubleArray(); 1979 console.log("RpcClient: readDoubleArray is " + array); 1980 } catch(error) { 1981 console.info("rpc read doubleArray fail, errorCode " + error.code); 1982 console.info("rpc read doubleArray fail, errorMessage" + error.message); 1983 } 1984 ``` 1985 1986### writeBooleanArray 1987 1988writeBooleanArray(booleanArray: boolean[]): void 1989 1990Writes a Boolean array to this **MessageSequence** object. 1991 1992**System capability**: SystemCapability.Communication.IPC.Core 1993 1994**Parameters** 1995 1996 | Name | Type | Mandatory| Description | 1997 | ------------ | --------- | ---- | ------------------ | 1998 | booleanArray | boolean[] | Yes | Boolean array to write.| 1999 2000**Error codes** 2001 2002For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2003 2004 | ID| Error Message| 2005 | -------- | -------- | 2006 | 1900009 | write data to message sequence failed | 2007 2008**Example** 2009 2010 ```ts 2011 let data = rpc.MessageSequence.create(); 2012 try { 2013 data.writeBooleanArray([false, true, false]); 2014 } catch(error) { 2015 console.info("rpc write booleanArray fail, errorCode " + error.code); 2016 console.info("rpc write booleanArray fail, errorMessage" + error.message); 2017 } 2018 ``` 2019 2020### readBooleanArray 2021 2022readBooleanArray(dataIn: boolean[]): void 2023 2024Reads a Boolean array from this **MessageSequence** object. 2025 2026**System capability**: SystemCapability.Communication.IPC.Core 2027 2028**Parameters** 2029 2030 | Name| Type | Mandatory| Description | 2031 | ------ | --------- | ---- | ------------------ | 2032 | dataIn | boolean[] | Yes | Boolean array to read.| 2033 2034**Error codes** 2035 2036For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2037 2038 | ID| Error Message| 2039 | -------- | -------- | 2040 | 1900010 | read data from message sequence failed | 2041 2042**Example** 2043 2044 ```ts 2045 let data = rpc.MessageSequence.create(); 2046 try { 2047 data.writeBooleanArray([false, true, false]); 2048 } catch(error) { 2049 console.info("rpc write booleanArray fail, errorCode " + error.code); 2050 console.info("rpc write booleanArray fail, errorMessage" + error.message); 2051 } 2052 let array = new Array(3); 2053 try { 2054 data.readBooleanArray(array); 2055 } catch(error) { 2056 console.info("rpc read booleanArray fail, errorCode " + error.code); 2057 console.info("rpc read booleanArray fail, errorMessage" + error.message); 2058 } 2059 ``` 2060 2061### readBooleanArray 2062 2063readBooleanArray(): boolean[] 2064 2065Reads the Boolean array from this **MessageSequence** object. 2066 2067**System capability**: SystemCapability.Communication.IPC.Core 2068 2069**Return value** 2070 2071 | Type | Description | 2072 | --------- | -------------- | 2073 | boolean[] | Boolean array read.| 2074 2075**Error codes** 2076 2077For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2078 2079 | ID| Error Message| 2080 | -------- | -------- | 2081 | 1900010 | read data from message sequence failed | 2082 2083**Example** 2084 2085 ```ts 2086 let data = rpc.MessageSequence.create(); 2087 try { 2088 data.writeBooleanArray([false, true, false]); 2089 } catch(error) { 2090 console.info("rpc write booleanArray fail, errorCode " + error.code); 2091 console.info("rpc write booleanArray fail, errorMessage" + error.message); 2092 } 2093 try { 2094 let array = data.readBooleanArray(); 2095 console.log("RpcClient: readBooleanArray is " + array); 2096 } catch(error) { 2097 console.info("rpc read booleanArray fail, errorCode " + error.code); 2098 console.info("rpc read booleanArray fail, errorMessage" + error.message); 2099 } 2100 ``` 2101 2102### writeCharArray 2103 2104writeCharArray(charArray: number[]): void 2105 2106Writes a character array to this **MessageSequence** object. 2107 2108**System capability**: SystemCapability.Communication.IPC.Core 2109 2110**Parameters** 2111 2112 | Name | Type | Mandatory| Description | 2113 | --------- | -------- | ---- | ---------------------- | 2114 | charArray | number[] | Yes | Character array to write.| 2115 2116**Error codes** 2117 2118For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2119 2120 | ID| Error Message| 2121 | -------- | -------- | 2122 | 1900009 | write data to message sequence failed | 2123 2124**Example** 2125 2126 ```ts 2127 let data = rpc.MessageSequence.create(); 2128 try { 2129 data.writeCharArray([97, 98, 88]); 2130 } catch(error) { 2131 console.info("rpc write charArray fail, errorCode " + error.code); 2132 console.info("rpc write charArray fail, errorMessage" + error.message); 2133 } 2134 ``` 2135 2136### readCharArray 2137 2138readCharArray(dataIn: number[]): void 2139 2140Reads a character array from this **MessageSequence** object. 2141 2142**System capability**: SystemCapability.Communication.IPC.Core 2143 2144**Parameters** 2145 2146 | Name| Type | Mandatory| Description | 2147 | ------ | -------- | ---- | ---------------------- | 2148 | dataIn | number[] | Yes | Character array to read.| 2149 2150**Error codes** 2151 2152For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2153 2154 | ID| Error Message| 2155 | -------- | -------- | 2156 | 1900010 | read data from message sequence failed | 2157 2158**Example** 2159 2160 ```ts 2161 let data = rpc.MessageSequence.create(); 2162 try { 2163 data.writeCharArray([97, 98, 88]); 2164 } catch(error) { 2165 console.info("rpc write charArray fail, errorCode " + error.code); 2166 console.info("rpc write charArray fail, errorMessage" + error.message); 2167 } 2168 let array = new Array(3); 2169 try { 2170 data.readCharArray(array); 2171 } catch(error) { 2172 console.info("rpc read charArray fail, errorCode " + error.code); 2173 console.info("rpc read charArray fail, errorMessage" + error.message); 2174 } 2175 ``` 2176 2177### readCharArray 2178 2179readCharArray(): number[] 2180 2181Reads the character array from this **MessageSequence** object. 2182 2183**System capability**: SystemCapability.Communication.IPC.Core 2184 2185**Return value** 2186 2187 | Type | Description | 2188 | -------- | ------------------ | 2189 | number[] | Character array read.| 2190 2191**Error codes** 2192 2193For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2194 2195 | ID| Error Message| 2196 | -------- | -------- | 2197 | 1900010 | read data from message sequence failed | 2198 2199**Example** 2200 2201 ```ts 2202 let data = rpc.MessageSequence.create(); 2203 try { 2204 data.writeCharArray([97, 98, 88]); 2205 } catch(error) { 2206 console.info("rpc write charArray fail, errorCode " + error.code); 2207 console.info("rpc write charArray fail, errorMessage" + error.message); 2208 } 2209 let array = new Array(3); 2210 try { 2211 let array = data.readCharArray(); 2212 console.log("RpcClient: readCharArray is " + array); 2213 } catch(error) { 2214 console.info("rpc read charArray fail, errorCode " + error.code); 2215 console.info("rpc read charArray fail, errorMessage" + error.message); 2216 } 2217 ``` 2218 2219### writeStringArray 2220 2221writeStringArray(stringArray: string[]): void 2222 2223Writes a string array to this **MessageSequence** object. 2224 2225**System capability**: SystemCapability.Communication.IPC.Core 2226 2227**Parameters** 2228 2229 | Name | Type | Mandatory| Description | 2230 | ----------- | -------- | ---- | ------------------------------------------------------- | 2231 | stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 2232 2233**Error codes** 2234 2235For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2236 2237 | ID| Error Message| 2238 | -------- | -------- | 2239 | 1900009 | write data to message sequence failed | 2240 2241**Example** 2242 2243 ```ts 2244 let data = rpc.MessageSequence.create(); 2245 try { 2246 data.writeStringArray(["abc", "def"]); 2247 } catch(error) { 2248 console.info("rpc write stringArray fail, errorCode " + error.code); 2249 console.info("rpc write stringArray fail, errorMessage" + error.message); 2250 } 2251 ``` 2252 2253### readStringArray 2254 2255readStringArray(dataIn: string[]): void 2256 2257Reads a string array from this **MessageSequence** object. 2258 2259**System capability**: SystemCapability.Communication.IPC.Core 2260 2261**Parameters** 2262 2263 | Name| Type | Mandatory| Description | 2264 | ------ | -------- | ---- | -------------------- | 2265 | dataIn | string[] | Yes | String array to read.| 2266 2267**Error codes** 2268 2269For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2270 2271 | ID| Error Message| 2272 | -------- | -------- | 2273 | 1900010 | read data from message sequence failed | 2274 2275**Example** 2276 2277 ```ts 2278 let data = rpc.MessageSequence.create(); 2279 try { 2280 data.writeStringArray(["abc", "def"]); 2281 } catch(error) { 2282 console.info("rpc write stringArray fail, errorCode " + error.code); 2283 console.info("rpc write stringArray fail, errorMessage" + error.message); 2284 } 2285 let array = new Array(2); 2286 try { 2287 data.readStringArray(array); 2288 } catch(error) { 2289 console.info("rpc read stringArray fail, errorCode " + error.code); 2290 console.info("rpc read stringArray fail, errorMessage" + error.message); 2291 } 2292 ``` 2293 2294### readStringArray 2295 2296readStringArray(): string[] 2297 2298Reads the string array from this **MessageSequence** object. 2299 2300**System capability**: SystemCapability.Communication.IPC.Core 2301 2302**Return value** 2303 2304 | Type | Description | 2305 | -------- | ---------------- | 2306 | string[] | String array read.| 2307 2308**Error codes** 2309 2310For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2311 2312 | ID| Error Message| 2313 | -------- | -------- | 2314 | 1900010 | read data from message sequence failed | 2315 2316**Example** 2317 2318 ```ts 2319 let data = rpc.MessageSequence.create(); 2320 try { 2321 data.writeStringArray(["abc", "def"]); 2322 } catch(error) { 2323 console.info("rpc write stringArray fail, errorCode " + error.code); 2324 console.info("rpc write stringArray fail, errorMessage" + error.message); 2325 } 2326 try { 2327 let array = data.readStringArray(); 2328 console.log("RpcClient: readStringArray is " + array); 2329 } catch(error) { 2330 console.info("rpc read stringArray fail, errorCode " + error.code); 2331 console.info("rpc read stringArray fail, errorMessage" + error.message); 2332 } 2333 ``` 2334 2335### writeNoException 2336 2337writeNoException(): void 2338 2339Writes information to this **MessageSequence** object indicating that no exception occurred. 2340 2341**System capability**: SystemCapability.Communication.IPC.Core 2342 2343**Error codes** 2344 2345For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2346 2347 | ID| Error Message| 2348 | -------- | -------- | 2349 | 1900009 | write data to message sequence failed | 2350 2351**Example** 2352 2353 ```ts 2354 class TestRemoteObject extends rpc.RemoteObject { 2355 constructor(descriptor) { 2356 super(descriptor); 2357 } 2358 2359 onRemoteRequest(code, data, reply, option) { 2360 if (code === 1) { 2361 console.log("RpcServer: onRemoteRequest called"); 2362 try { 2363 reply.writeNoException(); 2364 } catch(error) { 2365 console.info("rpc write no exception fail, errorCode " + error.code); 2366 console.info("rpc write no exception fail, errorMessage" + error.message); 2367 } 2368 return true; 2369 } else { 2370 console.log("RpcServer: unknown code: " + code); 2371 return false; 2372 } 2373 } 2374 } 2375 ``` 2376 2377### readException 2378 2379readException(): void 2380 2381Reads the exception information from this **MessageSequence** object. 2382 2383**System capability**: SystemCapability.Communication.IPC.Core 2384 2385**Error codes** 2386 2387For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2388 2389 | ID| Error Message| 2390 | -------- | -------- | 2391 | 1900010 | read data from message sequence failed | 2392 2393**Example** 2394 2395 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 2396 2397 ```ts 2398 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 2399 // import FA from "@ohos.ability.featureAbility"; 2400 2401 let proxy; 2402 let connect = { 2403 onConnect: function(elementName, remoteProxy) { 2404 console.log("RpcClient: js onConnect called."); 2405 proxy = remoteProxy; 2406 }, 2407 onDisconnect: function(elementName) { 2408 console.log("RpcClient: onDisconnect"); 2409 }, 2410 onFailed: function() { 2411 console.log("RpcClient: onFailed"); 2412 } 2413 }; 2414 let want = { 2415 "bundleName": "com.ohos.server", 2416 "abilityName": "com.ohos.server.EntryAbility", 2417 }; 2418 2419 // Use this method to connect to the ability for the FA model. 2420 // FA.connectAbility(want,connect); 2421 2422 globalThis.context.connectServiceExtensionAbility(want, connect); 2423 ``` 2424 2425 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 2426 2427 ```ts 2428 let option = new rpc.MessageOption(); 2429 let data = rpc.MessageSequence.create(); 2430 let reply = rpc.MessageSequence.create(); 2431 data.writeInt(1); 2432 data.writeString("hello"); 2433 proxy.sendMessageRequest(1, data, reply, option) 2434 .then(function(errCode) { 2435 if (errCode === 0) { 2436 console.log("sendMessageRequest got result"); 2437 try { 2438 reply.readException(); 2439 } catch(error) { 2440 console.info("rpc read exception fail, errorCode " + error.code); 2441 console.info("rpc read no exception fail, errorMessage" + error.message); 2442 } 2443 let msg = reply.readString(); 2444 console.log("RPCTest: reply msg: " + msg); 2445 } else { 2446 console.log("RPCTest: sendMessageRequest failed, errCode: " + errCode); 2447 } 2448 }).catch(function(e) { 2449 console.log("RPCTest: sendMessageRequest got exception: " + e.message); 2450 }).finally (() => { 2451 console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); 2452 data.reclaim(); 2453 reply.reclaim(); 2454 }); 2455 ``` 2456 2457### writeParcelableArray 2458 2459writeParcelableArray(parcelableArray: Parcelable[]): void 2460 2461Writes a **Parcelable** array to this **MessageSequence** object. 2462 2463**System capability**: SystemCapability.Communication.IPC.Core 2464 2465**Parameters** 2466 2467 | Name | Type | Mandatory| Description | 2468 | --------------- | ------------ | ---- | -------------------------- | 2469 | parcelableArray | Parcelable[] | Yes | **Parcelable** array to write.| 2470 2471**Error codes** 2472 2473For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2474 2475 | ID| Error Message| 2476 | -------- | -------- | 2477 | 1900009 | write data to message sequence failed | 2478 2479**Example** 2480 2481 ```ts 2482 class MyParcelable { 2483 num: number; 2484 str: string; 2485 constructor(num, str) { 2486 this.num = num; 2487 this.str = str; 2488 } 2489 marshalling(messageSequence) { 2490 messageSequence.writeInt(this.num); 2491 messageSequence.writeString(this.str); 2492 return true; 2493 } 2494 unmarshalling(messageSequence) { 2495 this.num = messageSequence.readInt(); 2496 this.str = messageSequence.readString(); 2497 return true; 2498 } 2499 } 2500 let parcelable = new MyParcelable(1, "aaa"); 2501 let parcelable2 = new MyParcelable(2, "bbb"); 2502 let parcelable3 = new MyParcelable(3, "ccc"); 2503 let a = [parcelable, parcelable2, parcelable3]; 2504 let data = rpc.MessageSequence.create(); 2505 try { 2506 data.writeParcelableArray(a); 2507 } catch(error) { 2508 console.info("rpc write parcelable array fail, errorCode " + error.code); 2509 console.info("rpc write parcelable array fail, errorMessage" + error.message); 2510 } 2511 ``` 2512 2513### readParcelableArray 2514 2515readParcelableArray(parcelableArray: Parcelable[]): void 2516 2517Reads a **Parcelable** array from this **MessageSequence** object. 2518 2519**System capability**: SystemCapability.Communication.IPC.Core 2520 2521**Parameters** 2522 2523 | Name | Type | Mandatory| Description | 2524 | --------------- | ------------ | ---- | -------------------------- | 2525 | parcelableArray | Parcelable[] | Yes | **Parcelable** array to read.| 2526 2527**Error codes** 2528 2529For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2530 2531 | ID| Error Message| 2532 | -------- | -------- | 2533 | 1900010 | read data from message sequence failed | 2534 | 1900012 | call js callback function failed | 2535 2536**Example** 2537 2538 ```ts 2539 class MyParcelable { 2540 num: number; 2541 str: string; 2542 constructor(num, str) { 2543 this.num = num; 2544 this.str = str; 2545 } 2546 marshalling(messageSequence) { 2547 messageSequence.writeInt(this.num); 2548 messageSequence.writeString(this.str); 2549 return true; 2550 } 2551 unmarshalling(messageSequence) { 2552 this.num = messageSequence.readInt(); 2553 this.str = messageSequence.readString(); 2554 return true; 2555 } 2556 } 2557 let parcelable = new MyParcelable(1, "aaa"); 2558 let parcelable2 = new MyParcelable(2, "bbb"); 2559 let parcelable3 = new MyParcelable(3, "ccc"); 2560 let a = [parcelable, parcelable2, parcelable3]; 2561 let data = rpc.MessageSequence.create(); 2562 let result = data.writeParcelableArray(a); 2563 console.log("RpcClient: writeParcelableArray is " + result); 2564 let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")]; 2565 try { 2566 data.readParcelableArray(b); 2567 } catch(error) { 2568 console.info("rpc read parcelable array fail, errorCode " + error.code); 2569 console.info("rpc read parcelable array fail, errorMessage" + error.message); 2570 } 2571 data.readParcelableArray(b); 2572 ``` 2573 2574### writeRemoteObjectArray 2575 2576writeRemoteObjectArray(objectArray: IRemoteObject[]): void 2577 2578Writes an array of **IRemoteObject** objects to this **MessageSequence** object. 2579 2580**System capability**: SystemCapability.Communication.IPC.Core 2581 2582**Parameters** 2583 2584 | Name | Type | Mandatory| Description | 2585 | ----------- | --------------- | ---- | ---------------------------------------------- | 2586 | objectArray | IRemoteObject[] | Yes | Array of **IRemoteObject** objects to write.| 2587 2588**Error codes** 2589 2590For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2591 2592 | ID| Error Message| 2593 | -------- | -------- | 2594 | 1900009 | write data to message sequence failed | 2595 2596**Example** 2597 2598 ```ts 2599 class TestRemoteObject extends rpc.RemoteObject { 2600 constructor(descriptor) { 2601 super(descriptor); 2602 this.modifyLocalInterface(this, descriptor); 2603 } 2604 2605 asObject(): rpc.IRemoteObject { 2606 return this; 2607 } 2608 } 2609 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2610 let data = rpc.MessageSequence.create(); 2611 let result = data.writeRemoteObjectArray(a); 2612 try { 2613 data.writeRemoteObjectArray(a); 2614 } catch(error) { 2615 console.info("rpc write remote object array fail, errorCode " + error.code); 2616 console.info("rpc write remote object array fail, errorMessage" + error.message); 2617 } 2618 console.log("RpcClient: writeRemoteObjectArray is " + result); 2619 ``` 2620 2621### readRemoteObjectArray 2622 2623readRemoteObjectArray(objects: IRemoteObject[]): void 2624 2625Reads an array of **IRemoteObject** objects from this **MessageSequence** object. 2626 2627**System capability**: SystemCapability.Communication.IPC.Core 2628 2629**Parameters** 2630 2631 | Name | Type | Mandatory| Description | 2632 | ------- | --------------- | ---- | ---------------------------------------------- | 2633 | objects | IRemoteObject[] | Yes | **IRemoteObject** array to read.| 2634 2635**Error codes** 2636 2637For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2638 2639 | ID| Error Message| 2640 | -------- | -------- | 2641 | 1900010 | read data from message sequence failed | 2642 2643**Example** 2644 2645 ```ts 2646 class MyDeathRecipient { 2647 onRemoteDied() { 2648 console.log("server died"); 2649 } 2650 } 2651 class TestRemoteObject extends rpc.RemoteObject { 2652 constructor(descriptor) { 2653 super(descriptor); 2654 this.modifyLocalInterface(this, descriptor); 2655 } 2656 2657 asObject(): rpc.IRemoteObject { 2658 return this; 2659 } 2660 } 2661 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2662 let data = rpc.MessageSequence.create(); 2663 data.writeRemoteObjectArray(a); 2664 let b = new Array(3); 2665 try { 2666 data.readRemoteObjectArray(b); 2667 } catch(error) { 2668 console.info("rpc read remote object array fail, errorCode " + error.code); 2669 console.info("rpc read remote object array fail, errorMessage" + error.message); 2670 } 2671 data.readRemoteObjectArray(b); 2672 ``` 2673 2674### readRemoteObjectArray 2675 2676readRemoteObjectArray(): IRemoteObject[] 2677 2678Reads the **IRemoteObject** object array from this **MessageSequence** object. 2679 2680**System capability**: SystemCapability.Communication.IPC.Core 2681 2682**Return value** 2683 2684 | Type | Description | 2685 | --------------- | --------------------------- | 2686 | IRemoteObject[] | **IRemoteObject** object array read.| 2687 2688**Error codes** 2689 2690For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2691 2692 | ID| Error Message| 2693 | -------- | -------- | 2694 | 1900010 | read data from message sequence failed | 2695 2696**Example** 2697 2698 ```ts 2699 class TestRemoteObject extends rpc.RemoteObject { 2700 constructor(descriptor) { 2701 super(descriptor); 2702 this.modifyLocalInterface(this, descriptor); 2703 } 2704 2705 asObject(): rpc.IRemoteObject { 2706 return this; 2707 } 2708 } 2709 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2710 let data = rpc.MessageSequence.create(); 2711 data.writeRemoteObjectArray(a); 2712 try { 2713 let b = data.readRemoteObjectArray(); 2714 console.log("RpcClient: readRemoteObjectArray is " + b); 2715 } catch(error) { 2716 console.info("rpc read remote object array fail, errorCode " + error.code); 2717 console.info("rpc read remote object array fail, errorMessage" + error.message); 2718 } 2719 ``` 2720 2721 2722### closeFileDescriptor<sup>9+</sup> 2723 2724static closeFileDescriptor(fd: number): void 2725 2726Closes a file descriptor. This API is a static method. 2727 2728**System capability**: SystemCapability.Communication.IPC.Core 2729 2730**Parameters** 2731 2732 | Name| Type | Mandatory| Description | 2733 | ------ | ------ | ---- | -------------------- | 2734 | fd | number | Yes | File descriptor to close.| 2735 2736**Example** 2737 2738 ```ts 2739 import fs from '@ohos.file.fs'; 2740 let filePath = "path/to/file"; 2741 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 2742 try { 2743 rpc.MessageSequence.closeFileDescriptor(file.fd); 2744 } catch(error) { 2745 console.info("rpc close file descriptor fail, errorCode " + error.code); 2746 console.info("rpc close file descriptor fail, errorMessage" + error.message); 2747 } 2748 ``` 2749 2750### dupFileDescriptor 2751 2752static dupFileDescriptor(fd: number) :number 2753 2754Duplicates a file descriptor. This API is a static method. 2755 2756**System capability**: SystemCapability.Communication.IPC.Core 2757 2758**Parameters** 2759 2760 | Name| Type | Mandatory| Description | 2761 | ------ | ------ | ---- | ------------------------ | 2762 | fd | number | Yes | File descriptor to duplicate.| 2763 2764**Return value** 2765 2766 | Type | Description | 2767 | ------ | -------------------- | 2768 | number | New file descriptor.| 2769 2770**Error codes** 2771 2772For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2773 2774 | ID| Error Message| 2775 | -------- | -------- | 2776 | 1900013 | call os dup function failed | 2777 2778**Example** 2779 2780 ```ts 2781 import fs from '@ohos.file.fs'; 2782 let filePath = "path/to/file"; 2783 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 2784 try { 2785 let newFd = rpc.MessageSequence.dupFileDescriptor(file.fd); 2786 } catch(error) { 2787 console.info("rpc dup file descriptor fail, errorCode " + error.code); 2788 console.info("rpc dup file descriptor fail, errorMessage" + error.message); 2789 } 2790 ``` 2791 2792### containFileDescriptors 2793 2794containFileDescriptors(): boolean 2795 2796Checks whether this **MessageSequence** object contains file descriptors. 2797 2798**System capability**: SystemCapability.Communication.IPC.Core 2799 2800**Return value** 2801 2802 | Type | Description | 2803 | ------- | -------------------------------------------------------------------- | 2804 | boolean | Returns **true** if the **MessageSequence** object contains file descriptors; returns **false** otherwise.| 2805 2806**Example** 2807 2808 2809 ```ts 2810 import fs from '@ohos.file.fs'; 2811 let sequence = new rpc.MessageSequence(); 2812 let filePath = "path/to/file"; 2813 let r1 = sequence.containFileDescriptors(); 2814 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 2815 try { 2816 sequence.writeFileDescriptor(file.fd); 2817 } catch(error) { 2818 console.info("rpc write file descriptor fail, errorCode " + error.code); 2819 console.info("rpc write file descriptor fail, errorMessage" + error.message); 2820 } 2821 try { 2822 let containFD = sequence.containFileDescriptors(); 2823 console.log("RpcTest: sequence after write fd containFd result is : " + containFD); 2824 } catch(error) { 2825 console.info("rpc contain file descriptor fail, errorCode " + error.code); 2826 console.info("rpc contain file descriptor fail, errorMessage" + error.message); 2827 } 2828 ``` 2829 2830### writeFileDescriptor 2831 2832writeFileDescriptor(fd: number): void 2833 2834Writes a file descriptor to this **MessageSequence** object. 2835 2836**System capability**: SystemCapability.Communication.IPC.Core 2837 2838**Parameters** 2839 2840 | Name| Type | Mandatory| Description | 2841 | ------ | ------ | ---- | ------------ | 2842 | fd | number | Yes | File descriptor to write.| 2843 2844**Error codes** 2845 2846For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2847 2848 | ID| Error Message| 2849 | -------- | -------- | 2850 | 1900009 | write data to message sequence failed | 2851 2852**Example** 2853 2854 ```ts 2855 import fs from '@ohos.file.fs'; 2856 let sequence = new rpc.MessageSequence(); 2857 let filePath = "path/to/file"; 2858 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 2859 try { 2860 sequence.writeFileDescriptor(file.fd); 2861 } catch(error) { 2862 console.info("rpc write file descriptor fail, errorCode " + error.code); 2863 console.info("rpc write file descriptor fail, errorMessage" + error.message); 2864 } 2865 ``` 2866 2867### readFileDescriptor 2868 2869readFileDescriptor(): number 2870 2871Reads the file descriptor from this **MessageSequence** object. 2872 2873**System capability**: SystemCapability.Communication.IPC.Core 2874 2875**Return value** 2876 2877 | Type | Description | 2878 | ------ | ---------------- | 2879 | number | File descriptor read.| 2880 2881**Error codes** 2882 2883For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2884 2885 | ID| Error Message| 2886 | -------- | -------- | 2887 | 1900010 | read data from message sequence failed | 2888 2889**Example** 2890 2891 ```ts 2892 import fs from '@ohos.file.fs'; 2893 let sequence = new rpc.MessageSequence(); 2894 let filePath = "path/to/file"; 2895 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 2896 try { 2897 sequence.writeFileDescriptor(file.fd); 2898 } catch(error) { 2899 console.info("rpc write file descriptor fail, errorCode " + error.code); 2900 console.info("rpc write file descriptor fail, errorMessage" + error.message); 2901 } 2902 try { 2903 let readFD = sequence.readFileDescriptor(); 2904 } catch(error) { 2905 console.info("rpc read file descriptor fail, errorCode " + error.code); 2906 console.info("rpc read file descriptor fail, errorMessage" + error.message); 2907 } 2908 ``` 2909 2910### writeAshmem 2911 2912writeAshmem(ashmem: Ashmem): void 2913 2914Writes an anonymous shared object to this **MessageSequence** object. 2915 2916**System capability**: SystemCapability.Communication.IPC.Core 2917 2918**Parameters** 2919 2920 | Name| Type | Mandatory| Description | 2921 | ------ | ------ | ---- | ------------------------------------- | 2922 | ashmem | Ashmem | Yes | Anonymous shared object to write.| 2923 2924**Error codes** 2925 2926For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2927 2928 | ID| Error Message| 2929 | -------- | ------- | 2930 | 1900003 | write to ashmem failed | 2931 2932**Example** 2933 2934 ```ts 2935 let sequence = new rpc.MessageSequence(); 2936 let ashmem; 2937 try { 2938 ashmem = rpc.Ashmem.create("ashmem", 1024); 2939 } catch(error) { 2940 console.info("rpc create ashmem fail, errorCode " + error.code); 2941 console.info("rpc creat ashmem fail, errorMessage" + error.message); 2942 } 2943 try { 2944 sequence.writeAshmem(ashmem); 2945 } catch(error) { 2946 console.info("rpc write ashmem fail, errorCode " + error.code); 2947 console.info("rpc write ashmem fail, errorMessage" + error.message); 2948 } 2949 ``` 2950 2951 2952### readAshmem 2953 2954readAshmem(): Ashmem 2955 2956Reads the anonymous shared object from this **MessageSequence** object. 2957 2958**System capability**: SystemCapability.Communication.IPC.Core 2959 2960**Return value** 2961 2962 | Type | Description | 2963 | ------ | ------------------ | 2964 | Ashmem | Anonymous share object read.| 2965 2966**Error codes** 2967 2968For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2969 2970 | ID| Error Message| 2971 | -------- | -------- | 2972 | 1900004 | read from ashmem failed | 2973 2974**Example** 2975 2976 ```ts 2977 let sequence = new rpc.MessageSequence(); 2978 let ashmem; 2979 try { 2980 ashmem = rpc.Ashmem.create("ashmem", 1024); 2981 } catch(error) { 2982 console.info("rpc create ashmem fail, errorCode " + error.code); 2983 console.info("rpc creat ashmem fail, errorMessage" + error.message); 2984 } 2985 try { 2986 sequence.writeAshmem(ashmem); 2987 } catch(error) { 2988 console.info("rpc write ashmem fail, errorCode " + error.code); 2989 console.info("rpc write ashmem fail, errorMessage" + error.message); 2990 } 2991 try { 2992 let readAshmem = sequence.readAshmem(); 2993 console.log("RpcTest: read ashmem to result is : " + readAshmem); 2994 } catch(error) { 2995 console.info("rpc read ashmem fail, errorCode " + error.code); 2996 console.info("rpc read ashmem fail, errorMessage" + error.message); 2997 } 2998 ``` 2999 3000### getRawDataCapacity 3001 3002getRawDataCapacity(): number 3003 3004Obtains the maximum amount of raw data that can be held by this **MessageSequence** object. 3005 3006**System capability**: SystemCapability.Communication.IPC.Core 3007 3008**Return value** 3009 3010 | Type | Description | 3011 | ------ | ------------------------------------------------------------ | 3012 | number | 128 MB, which is the maximum amount of raw data that can be held by this **MessageSequence** object.| 3013 3014**Example** 3015 3016 ```ts 3017 let sequence = new rpc.MessageSequence(); 3018 let result = sequence.getRawDataCapacity(); 3019 console.log("RpcTest: sequence get RawDataCapacity result is : " + result); 3020 ``` 3021 3022### writeRawData 3023 3024writeRawData(rawData: number[], size: number): void 3025 3026Writes raw data to this **MessageSequence** object. 3027 3028**System capability**: SystemCapability.Communication.IPC.Core 3029 3030**Parameters** 3031 3032 | Name | Type | Mandatory| Description | 3033 | ------- | -------- | ---- | ---------------------------------- | 3034 | rawData | number[] | Yes | Raw data to write. | 3035 | size | number | Yes | Size of the raw data, in bytes.| 3036 3037**Error codes** 3038 3039For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3040 3041 | ID| Error Message| 3042 | -------- | -------- | 3043 | 1900009 | write data to message sequence failed | 3044 3045**Example** 3046 3047 ```ts 3048 let sequence = new rpc.MessageSequence(); 3049 let arr = [1, 2, 3, 4, 5]; 3050 try { 3051 sequence.writeRawData(arr, arr.length); 3052 } catch(error) { 3053 console.info("rpc write rawdata fail, errorCode " + error.code); 3054 console.info("rpc write rawdata fail, errorMessage" + error.message); 3055 } 3056 ``` 3057 3058### readRawData 3059 3060readRawData(size: number): number[] 3061 3062Reads raw data from this **MessageSequence** object. 3063 3064**System capability**: SystemCapability.Communication.IPC.Core 3065 3066**Parameters** 3067 3068 | Name| Type | Mandatory| Description | 3069 | ------ | ------ | ---- | ------------------------ | 3070 | size | number | Yes | Size of the raw data to read.| 3071 3072**Return value** 3073 3074 | Type | Description | 3075 | -------- | ------------------------------ | 3076 | number[] | Raw data read, in bytes.| 3077 3078**Error codes** 3079 3080For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3081 3082 | ID| Error Message| 3083 | -------- | -------- | 3084 | 1900010 | read data from message sequence failed | 3085 3086**Example** 3087 3088 ```ts 3089 let sequence = new rpc.MessageSequence(); 3090 let arr = [1, 2, 3, 4, 5]; 3091 try { 3092 sequence.writeRawData(arr, arr.length); 3093 } catch(error) { 3094 console.info("rpc write rawdata fail, errorCode " + error.code); 3095 console.info("rpc write rawdata fail, errorMessage" + error.message); 3096 } 3097 try { 3098 let result = sequence.readRawData(5); 3099 console.log("RpcTest: sequence read raw data result is : " + result); 3100 } catch(error) { 3101 console.info("rpc read rawdata fail, errorCode " + error.code); 3102 console.info("rpc read rawdata fail, errorMessage" + error.message); 3103 } 3104 ``` 3105 3106## MessageParcel<sup>(deprecated)</sup> 3107 3108>This class is no longer maintained since API version 9. You are advised to use [MessageSequence](#messagesequence9). 3109 3110Provides APIs for reading and writing data in specific format. 3111During RPC, the sender can use the **write()** method provided by **MessageParcel** to write data in specific format to a **MessageParcel** object. The receiver can use the **read()** method provided by **MessageParcel** to read data in specific format from a **MessageParcel** object. The data formats include basic data types and arrays, IPC objects, interface tokens, and custom sequenceable objects. 3112 3113### create 3114 3115static create(): MessageParcel 3116 3117Creates a **MessageParcel** object. This method is a static method. 3118 3119**System capability**: SystemCapability.Communication.IPC.Core 3120 3121**Return value** 3122 3123 | Type | Description | 3124 | ------------- | ----------------------------- | 3125 | MessageParcel | **MessageParcel** object created.| 3126 3127**Example** 3128 3129 ```ts 3130 let data = rpc.MessageParcel.create(); 3131 console.log("RpcClient: data is " + data); 3132 ``` 3133 3134### reclaim 3135 3136reclaim(): void 3137 3138Reclaims the **MessageParcel** object that is no longer used. 3139 3140**System capability**: SystemCapability.Communication.IPC.Core 3141 3142**Example** 3143 3144 ```ts 3145 let reply = rpc.MessageParcel.create(); 3146 reply.reclaim(); 3147 ``` 3148 3149### writeRemoteObject 3150 3151writeRemoteObject(object: [IRemoteObject](#iremoteobject)): boolean 3152 3153Serializes a remote object and writes it to this **MessageParcel** object. 3154 3155**System capability**: SystemCapability.Communication.IPC.Core 3156 3157**Parameters** 3158 3159 | Name| Type | Mandatory| Description | 3160 | ------ | ------------------------------- | ---- | --------------------------------------- | 3161 | object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageParcel** object.| 3162 3163**Return value** 3164 3165 | Type | Description | 3166 | ------- | ----------------------------------------- | 3167 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3168 3169**Example** 3170 3171 ```ts 3172 class MyDeathRecipient { 3173 onRemoteDied() { 3174 console.log("server died"); 3175 } 3176 } 3177 class TestRemoteObject extends rpc.RemoteObject { 3178 constructor(descriptor) { 3179 super(descriptor); 3180 } 3181 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3182 return true; 3183 } 3184 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3185 return true; 3186 } 3187 isObjectDead(): boolean { 3188 return false; 3189 } 3190 } 3191 let data = rpc.MessageParcel.create(); 3192 let testRemoteObject = new TestRemoteObject("testObject"); 3193 data.writeRemoteObject(testRemoteObject); 3194 ``` 3195 3196### readRemoteObject 3197 3198readRemoteObject(): IRemoteObject 3199 3200Reads the remote object from this **MessageParcel** object. You can use this method to deserialize the **MessageParcel** object to generate an **IRemoteObject**. The remote objects are read in the order in which they are written to this **MessageParcel** object. 3201 3202**System capability**: SystemCapability.Communication.IPC.Core 3203 3204**Return value** 3205 3206 | Type | Description | 3207 | ------------------------------- | ------------------ | 3208 | [IRemoteObject](#iremoteobject) | Remote object obtained.| 3209 3210**Example** 3211 3212 ```ts 3213 class MyDeathRecipient { 3214 onRemoteDied() { 3215 console.log("server died"); 3216 } 3217 } 3218 class TestRemoteObject extends rpc.RemoteObject { 3219 constructor(descriptor) { 3220 super(descriptor); 3221 } 3222 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3223 return true; 3224 } 3225 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3226 return true; 3227 } 3228 isObjectDead(): boolean { 3229 return false; 3230 } 3231 } 3232 let data = rpc.MessageParcel.create(); 3233 let testRemoteObject = new TestRemoteObject("testObject"); 3234 data.writeRemoteObject(testRemoteObject); 3235 let proxy = data.readRemoteObject(); 3236 ``` 3237 3238### writeInterfaceToken 3239 3240writeInterfaceToken(token: string): boolean 3241 3242Writes an interface token to this **MessageParcel** object. The remote object can use this interface token to verify the communication. 3243 3244**System capability**: SystemCapability.Communication.IPC.Core 3245 3246**Parameters** 3247 3248 | Name| Type | Mandatory| Description | 3249 | ------ | ------ | ---- | ------------------ | 3250 | token | string | Yes | Interface token to write.| 3251 3252**Return value** 3253 3254 | Type | Description | 3255 | ------- | ----------------------------------------- | 3256 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3257 3258**Example** 3259 3260 ```ts 3261 let data = rpc.MessageParcel.create(); 3262 let result = data.writeInterfaceToken("aaa"); 3263 console.log("RpcServer: writeInterfaceToken is " + result); 3264 ``` 3265 3266 3267### readInterfaceToken 3268 3269readInterfaceToken(): string 3270 3271Reads the interface token from this **MessageParcel** object. The interface token is read in the sequence in which it is written to the **MessageParcel** object. The local object can use it to verify the communication. 3272 3273**System capability**: SystemCapability.Communication.IPC.Core 3274 3275**Return value** 3276 3277 | Type | Description | 3278 | ------ | ------------------------ | 3279 | string | Interface token obtained.| 3280 3281**Example** 3282 3283 ```ts 3284 class Stub extends rpc.RemoteObject { 3285 onRemoteMessageRequest(code, data, reply, option) { 3286 let interfaceToken = data.readInterfaceToken(); 3287 console.log("RpcServer: interfaceToken is " + interfaceToken); 3288 return true; 3289 } 3290 } 3291 ``` 3292 3293### getSize 3294 3295getSize(): number 3296 3297Obtains the data size of this **MessageParcel** object. 3298 3299**System capability**: SystemCapability.Communication.IPC.Core 3300 3301**Return value** 3302 3303 | Type | Description | 3304 | ------ | --------------------------------------------- | 3305 | number | Size of the **MessageParcel** object obtained, in bytes.| 3306 3307**Example** 3308 3309 ```ts 3310 let data = rpc.MessageParcel.create(); 3311 let size = data.getSize(); 3312 console.log("RpcClient: size is " + size); 3313 ``` 3314 3315### getCapacity 3316 3317getCapacity(): number 3318 3319Obtains the capacity of this **MessageParcel** object. 3320 3321**System capability**: SystemCapability.Communication.IPC.Core 3322 3323**Return value** 3324 3325 | Type | Description | 3326 | ------ | --------------------------------------------- | 3327 | number | **MessageParcel** capacity obtained, in bytes.| 3328 3329**Example** 3330 3331 ```ts 3332 let data = rpc.MessageParcel.create(); 3333 let result = data.getCapacity(); 3334 console.log("RpcClient: capacity is " + result); 3335 ``` 3336 3337### setSize 3338 3339setSize(size: number): boolean 3340 3341Sets the size of data contained in this **MessageParcel** object. 3342 3343**System capability**: SystemCapability.Communication.IPC.Core 3344 3345**Parameters** 3346 3347 | Name| Type | Mandatory| Description | 3348 | ------ | ------ | ---- | ------------------------------------------- | 3349 | size | number | Yes | Data size to set, in bytes.| 3350 3351**Return value** 3352 3353 | Type | Description | 3354 | ------- | --------------------------------- | 3355 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3356 3357**Example** 3358 3359 ```ts 3360 let data = rpc.MessageParcel.create(); 3361 let setSize = data.setSize(16); 3362 console.log("RpcClient: setSize is " + setSize); 3363 ``` 3364 3365### setCapacity 3366 3367setCapacity(size: number): boolean 3368 3369Sets the storage capacity of this **MessageParcel** object. 3370 3371**System capability**: SystemCapability.Communication.IPC.Core 3372 3373**Parameters** 3374 3375 | Name| Type | Mandatory| Description | 3376 | ------ | ------ | ---- | ------------------------------------------- | 3377 | size | number | Yes | Storage capacity to set, in bytes.| 3378 3379**Return value** 3380 3381 | Type | Description | 3382 | ------- | --------------------------------- | 3383 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3384 3385**Example** 3386 3387 ```ts 3388 let data = rpc.MessageParcel.create(); 3389 let result = data.setCapacity(100); 3390 console.log("RpcClient: setCapacity is " + result); 3391 ``` 3392 3393### getWritableBytes 3394 3395getWritableBytes(): number 3396 3397Obtains the writable capacity of this **MessageParcel** object. 3398 3399**System capability**: SystemCapability.Communication.IPC.Core 3400 3401**Return value** 3402 3403 | Type | Description | 3404 | ------ | --------------------------------------------------- | 3405 | number | **MessageParcel** writable capacity obtained, in bytes.| 3406 3407**Example** 3408 3409 ```ts 3410 class Stub extends rpc.RemoteObject { 3411 onRemoteMessageRequest(code, data, reply, option) { 3412 let getWritableBytes = data.getWritableBytes(); 3413 console.log("RpcServer: getWritableBytes is " + getWritableBytes); 3414 return true; 3415 } 3416 } 3417 ``` 3418 3419### getReadableBytes 3420 3421getReadableBytes(): number 3422 3423Obtains the readable capacity of this **MessageParcel** object. 3424 3425**System capability**: SystemCapability.Communication.IPC.Core 3426 3427**Return value** 3428 3429 | Type | Description | 3430 | ------ | --------------------------------------------------- | 3431 | number | **MessageParcel** object readable capacity, in bytes.| 3432 3433**Example** 3434 3435 ```ts 3436 class Stub extends rpc.RemoteObject { 3437 onRemoteRequest(code, data, reply, option) { 3438 let result = data.getReadableBytes(); 3439 console.log("RpcServer: getReadableBytes is " + result); 3440 return true; 3441 } 3442 } 3443 ``` 3444 3445### getReadPosition 3446 3447getReadPosition(): number 3448 3449Obtains the read position of this **MessageParcel** object. 3450 3451**System capability**: SystemCapability.Communication.IPC.Core 3452 3453**Return value** 3454 3455 | Type | Description | 3456 | ------ | --------------------------------------- | 3457 | number | Current read position of the **MessageParcel** object.| 3458 3459**Example** 3460 3461 ```ts 3462 let data = rpc.MessageParcel.create(); 3463 let readPos = data.getReadPosition(); 3464 console.log("RpcClient: readPos is " + readPos); 3465 ``` 3466 3467### getWritePosition 3468 3469getWritePosition(): number 3470 3471Obtains the write position of this **MessageParcel** object. 3472 3473**System capability**: SystemCapability.Communication.IPC.Core 3474 3475**Return value** 3476 3477 | Type | Description | 3478 | ------ | --------------------------------------- | 3479 | number | Current write position of the **MessageParcel** object.| 3480 3481**Example** 3482 3483 ```ts 3484 let data = rpc.MessageParcel.create(); 3485 data.writeInt(10); 3486 let bwPos = data.getWritePosition(); 3487 console.log("RpcClient: bwPos is " + bwPos); 3488 ``` 3489 3490### rewindRead 3491 3492rewindRead(pos: number): boolean 3493 3494Moves the read pointer to the specified position. 3495 3496**System capability**: SystemCapability.Communication.IPC.Core 3497 3498**Parameters** 3499 3500 | Name| Type | Mandatory| Description | 3501 | ------ | ------ | ---- | ------------------------ | 3502 | pos | number | Yes | Position from which data is to read.| 3503 3504**Return value** 3505 3506 | Type | Description | 3507 | ------- | ------------------------------------------------- | 3508 | boolean | Returns **true** if the read position changes; returns **false** otherwise.| 3509 3510**Example** 3511 3512 ```ts 3513 let data = rpc.MessageParcel.create(); 3514 data.writeInt(12); 3515 data.writeString("parcel"); 3516 let number = data.readInt(); 3517 console.log("RpcClient: number is " + number); 3518 data.rewindRead(0); 3519 let number2 = data.readInt(); 3520 console.log("RpcClient: rewindRead is " + number2); 3521 ``` 3522 3523### rewindWrite 3524 3525rewindWrite(pos: number): boolean 3526 3527Moves the write pointer to the specified position. 3528 3529**System capability**: SystemCapability.Communication.IPC.Core 3530 3531**Parameters** 3532 3533 | Name| Type | Mandatory| Description | 3534 | ------ | ------ | ---- | ------------------------ | 3535 | pos | number | Yes | Position from which data is to write.| 3536 3537**Return value** 3538 3539 | Type | Description | 3540 | ------- | --------------------------------------------- | 3541 | boolean | Returns **true** if the write position changes; returns **false** otherwise.| 3542 3543**Example** 3544 3545 ```ts 3546 let data = rpc.MessageParcel.create(); 3547 data.writeInt(4); 3548 data.rewindWrite(0); 3549 data.writeInt(5); 3550 let number = data.readInt(); 3551 console.log("RpcClient: rewindWrite is: " + number); 3552 ``` 3553 3554### writeByte 3555 3556writeByte(val: number): boolean 3557 3558Writes a Byte value to this **MessageParcel** object. 3559 3560**System capability**: SystemCapability.Communication.IPC.Core 3561 3562**Parameters** 3563 3564 | Name| Type | Mandatory| Description | 3565 | ------ | ------ | ---- | ---------------- | 3566 | val | number | Yes | Byte value to write.| 3567 3568**Return value** 3569 3570 | Type | Description | 3571 | ------- | ----------------------------- | 3572 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3573 3574**Example** 3575 3576 ```ts 3577 let data = rpc.MessageParcel.create(); 3578 let result = data.writeByte(2); 3579 console.log("RpcClient: writeByte is: " + result); 3580 ``` 3581 3582### readByte 3583 3584readByte(): number 3585 3586Reads the Byte value from this **MessageParcel** object. 3587 3588**System capability**: SystemCapability.Communication.IPC.Core 3589 3590**Return value** 3591 3592 | Type | Description | 3593 | ------ | ------------ | 3594 | number | Byte value read.| 3595 3596**Example** 3597 3598 ```ts 3599 let data = rpc.MessageParcel.create(); 3600 let result = data.writeByte(2); 3601 console.log("RpcClient: writeByte is: " + result); 3602 let ret = data.readByte(); 3603 console.log("RpcClient: readByte is: " + ret); 3604 ``` 3605 3606### writeShort 3607 3608writeShort(val: number): boolean 3609 3610Writes a Short int value to this **MessageParcel** object. 3611 3612**System capability**: SystemCapability.Communication.IPC.Core 3613 3614**Parameters** 3615 3616 | Name| Type | Mandatory| Description | 3617 | ------ | ------ | ---- | ------------------ | 3618 | val | number | Yes | Short int value to write.| 3619 3620**Return value** 3621 3622 | Type | Description | 3623 | ------- | ----------------------------- | 3624 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 3625 3626**Example** 3627 3628 ```ts 3629 let data = rpc.MessageParcel.create(); 3630 let result = data.writeShort(8); 3631 console.log("RpcClient: writeShort is: " + result); 3632 ``` 3633 3634### readShort 3635 3636readShort(): number 3637 3638Reads the Short int value from this **MessageParcel** object. 3639 3640**System capability**: SystemCapability.Communication.IPC.Core 3641 3642**Return value** 3643 3644 | Type | Description | 3645 | ------ | -------------- | 3646 | number | Short int value read.| 3647 3648**Example** 3649 3650 ```ts 3651 let data = rpc.MessageParcel.create(); 3652 let result = data.writeShort(8); 3653 console.log("RpcClient: writeShort is: " + result); 3654 let ret = data.readShort(); 3655 console.log("RpcClient: readShort is: " + ret); 3656 ``` 3657 3658### writeInt 3659 3660writeInt(val: number): boolean 3661 3662Writes an Int value to this **MessageParcel** object. 3663 3664**System capability**: SystemCapability.Communication.IPC.Core 3665 3666**Parameters** 3667 3668 | Name| Type | Mandatory| Description | 3669 | ------ | ------ | ---- | ---------------- | 3670 | val | number | Yes | Int value to write.| 3671 3672**Return value** 3673 3674 | Type | Description | 3675 | ------- | ----------------------------- | 3676 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3677 3678**Example** 3679 3680 ```ts 3681 let data = rpc.MessageParcel.create(); 3682 let result = data.writeInt(10); 3683 console.log("RpcClient: writeInt is " + result); 3684 ``` 3685 3686### readInt 3687 3688readInt(): number 3689 3690Reads the Int value from this **MessageParcel** object. 3691 3692**System capability**: SystemCapability.Communication.IPC.Core 3693 3694**Return value** 3695 3696 | Type | Description | 3697 | ------ | ------------ | 3698 | number | Int value read.| 3699 3700**Example** 3701 3702 ```ts 3703 let data = rpc.MessageParcel.create(); 3704 let result = data.writeInt(10); 3705 console.log("RpcClient: writeInt is " + result); 3706 let ret = data.readInt(); 3707 console.log("RpcClient: readInt is " + ret); 3708 ``` 3709 3710### writeLong 3711 3712writeLong(val: number): boolean 3713 3714Writes a Long int value to this **MessageParcel** object. 3715 3716**System capability**: SystemCapability.Communication.IPC.Core 3717 3718**Parameters** 3719 3720 | Name| Type | Mandatory| Description | 3721 | ------ | ------ | ---- | ---------------- | 3722 | val | number | Yes | Long int value to write.| 3723 3724**Return value** 3725 3726 | Type | Description | 3727 | ------- | --------------------------------- | 3728 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 3729 3730**Example** 3731 3732 ```ts 3733 let data = rpc.MessageParcel.create(); 3734 let result = data.writeLong(10000); 3735 console.log("RpcClient: writeLong is " + result); 3736 ``` 3737 3738### readLong 3739 3740readLong(): number 3741 3742Reads the Long int value from this **MessageParcel** object. 3743 3744**System capability**: SystemCapability.Communication.IPC.Core 3745 3746**Return value** 3747 3748 | Type | Description | 3749 | ------ | -------------- | 3750 | number | Long int value read.| 3751 3752**Example** 3753 3754 ```ts 3755 let data = rpc.MessageParcel.create(); 3756 let result = data.writeLong(10000); 3757 console.log("RpcClient: writeLong is " + result); 3758 let ret = data.readLong(); 3759 console.log("RpcClient: readLong is " + ret); 3760 ``` 3761 3762### writeFloat 3763 3764writeFloat(val: number): boolean 3765 3766Writes a Float value to this **MessageParcel** object. 3767 3768**System capability**: SystemCapability.Communication.IPC.Core 3769 3770**Parameters** 3771 3772 | Name| Type | Mandatory| Description | 3773 | ------ | ------ | ---- | ---------------- | 3774 | val | number | Yes | Float value to write.| 3775 3776**Return value** 3777 3778 | Type | Description | 3779 | ------- | --------------------------------- | 3780 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 3781 3782**Example** 3783 3784 ```ts 3785 let data = rpc.MessageParcel.create(); 3786 let result = data.writeFloat(1.2); 3787 console.log("RpcClient: writeFloat is " + result); 3788 ``` 3789 3790### readFloat 3791 3792readFloat(): number 3793 3794Reads the Float value from this **MessageParcel** object. 3795 3796**System capability**: SystemCapability.Communication.IPC.Core 3797 3798**Return value** 3799 3800 | Type | Description | 3801 | ------ | ------------ | 3802 | number | Float value read.| 3803 3804**Example** 3805 3806 ```ts 3807 let data = rpc.MessageParcel.create(); 3808 let result = data.writeFloat(1.2); 3809 console.log("RpcClient: writeFloat is " + result); 3810 let ret = data.readFloat(); 3811 console.log("RpcClient: readFloat is " + ret); 3812 ``` 3813 3814### writeDouble 3815 3816writeDouble(val: number): boolean 3817 3818Writes a Double value to this **MessageParcel** object. 3819 3820**System capability**: SystemCapability.Communication.IPC.Core 3821 3822**Parameters** 3823 3824 | Name| Type | Mandatory| Description | 3825 | ------ | ------ | ---- | ---------------------- | 3826 | val | number | Yes | Double value to write.| 3827 3828**Return value** 3829 3830 | Type | Description | 3831 | ------- | --------------------------------- | 3832 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 3833 3834**Example** 3835 3836 ```ts 3837 let data = rpc.MessageParcel.create(); 3838 let result = data.writeDouble(10.2); 3839 console.log("RpcClient: writeDouble is " + result); 3840 ``` 3841 3842### readDouble 3843 3844readDouble(): number 3845 3846Reads the Double value from this **MessageParcel** object. 3847 3848**System capability**: SystemCapability.Communication.IPC.Core 3849 3850**Return value** 3851 3852 | Type | Description | 3853 | ------ | ------------------ | 3854 | number | Double value read.| 3855 3856**Example** 3857 3858 ```ts 3859 let data = rpc.MessageParcel.create(); 3860 let result = data.writeDouble(10.2); 3861 console.log("RpcClient: writeDouble is " + result); 3862 let ret = data.readDouble(); 3863 console.log("RpcClient: readDouble is " + ret); 3864 ``` 3865 3866### writeBoolean 3867 3868writeBoolean(val: boolean): boolean 3869 3870Writes a Boolean value to this **MessageParcel** object. 3871 3872**System capability**: SystemCapability.Communication.IPC.Core 3873 3874**Parameters** 3875 3876 | Name| Type | Mandatory| Description | 3877 | ------ | ------- | ---- | ---------------- | 3878 | val | boolean | Yes | Boolean value to write.| 3879 3880**Return value** 3881 3882 | Type | Description | 3883 | ------- | --------------------------------- | 3884 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 3885 3886**Example** 3887 3888 ```ts 3889 let data = rpc.MessageParcel.create(); 3890 let result = data.writeBoolean(false); 3891 console.log("RpcClient: writeBoolean is " + result); 3892 ``` 3893 3894### readBoolean 3895 3896readBoolean(): boolean 3897 3898Reads the Boolean value from this **MessageParcel** object. 3899 3900**System capability**: SystemCapability.Communication.IPC.Core 3901 3902**Return value** 3903 3904 | Type | Description | 3905 | ------- | -------------------- | 3906 | boolean | Boolean value read.| 3907 3908**Example** 3909 3910 ```ts 3911 let data = rpc.MessageParcel.create(); 3912 let result = data.writeBoolean(false); 3913 console.log("RpcClient: writeBoolean is " + result); 3914 let ret = data.readBoolean(); 3915 console.log("RpcClient: readBoolean is " + ret); 3916 ``` 3917 3918### writeChar 3919 3920writeChar(val: number): boolean 3921 3922Writes a Char value to this **MessageParcel** object. 3923 3924**System capability**: SystemCapability.Communication.IPC.Core 3925 3926**Parameters** 3927 3928 | Name| Type | Mandatory| Description | 3929 | ------ | ------ | ---- | -------------------- | 3930 | val | number | Yes | Char value to write.| 3931 3932**Return value** 3933 3934 | Type | Description | 3935 | ------- | ----------------------------- | 3936 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 3937 3938**Example** 3939 3940 ```ts 3941 let data = rpc.MessageParcel.create(); 3942 let result = data.writeChar(97); 3943 console.log("RpcClient: writeChar is " + result); 3944 ``` 3945 3946### readChar 3947 3948readChar(): number 3949 3950Reads the Char value from this **MessageParcel** object. 3951 3952**System capability**: SystemCapability.Communication.IPC.Core 3953 3954**Return value** 3955 3956 | Type | Description | 3957 | ------ | ---------------- | 3958 | number | Char value read.| 3959 3960**Example** 3961 3962 ```ts 3963 let data = rpc.MessageParcel.create(); 3964 let result = data.writeChar(97); 3965 console.log("RpcClient: writeChar is " + result); 3966 let ret = data.readChar(); 3967 console.log("RpcClient: readChar is " + ret); 3968 ``` 3969 3970### writeString 3971 3972writeString(val: string): boolean 3973 3974Writes a string to this **MessageParcel** object. 3975 3976**System capability**: SystemCapability.Communication.IPC.Core 3977 3978**Parameters** 3979 3980 | Name| Type | Mandatory| Description | 3981 | ------ | ------ | ---- | ----------------------------------------- | 3982 | val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 3983 3984**Return value** 3985 3986 | Type | Description | 3987 | ------- | --------------------------------- | 3988 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 3989 3990**Example** 3991 3992 ```ts 3993 let data = rpc.MessageParcel.create(); 3994 let result = data.writeString('abc'); 3995 console.log("RpcClient: writeString is " + result); 3996 ``` 3997 3998### readString 3999 4000readString(): string 4001 4002Reads the string from this **MessageParcel** object. 4003 4004**System capability**: SystemCapability.Communication.IPC.Core 4005 4006**Return value** 4007 4008 | Type | Description | 4009 | ------ | -------------- | 4010 | string | String read.| 4011 4012**Example** 4013 4014 ```ts 4015 let data = rpc.MessageParcel.create(); 4016 let result = data.writeString('abc'); 4017 console.log("RpcClient: writeString is " + result); 4018 let ret = data.readString(); 4019 console.log("RpcClient: readString is " + ret); 4020 ``` 4021 4022### writeSequenceable 4023 4024writeSequenceable(val: Sequenceable): boolean 4025 4026Writes a sequenceable object to this **MessageParcel** object. 4027 4028**System capability**: SystemCapability.Communication.IPC.Core 4029 4030**Parameters** 4031 4032 | Name| Type | Mandatory| Description | 4033 | ------ | ----------------------------- | ---- | -------------------- | 4034 | val | [Sequenceable](#sequenceable) | Yes | Sequenceable object to write.| 4035 4036**Return value** 4037 4038 | Type | Description | 4039 | ------- | -------------------------------- | 4040 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4041 4042**Example** 4043 4044 ```ts 4045 class MySequenceable { 4046 num: number; 4047 str: string; 4048 constructor(num, str) { 4049 this.num = num; 4050 this.str = str; 4051 } 4052 marshalling(messageParcel) { 4053 messageParcel.writeInt(this.num); 4054 messageParcel.writeString(this.str); 4055 return true; 4056 } 4057 unmarshalling(messageParcel) { 4058 this.num = messageParcel.readInt(); 4059 this.str = messageParcel.readString(); 4060 return true; 4061 } 4062 } 4063 let sequenceable = new MySequenceable(1, "aaa"); 4064 let data = rpc.MessageParcel.create(); 4065 let result = data.writeSequenceable(sequenceable); 4066 console.log("RpcClient: writeSequenceable is " + result); 4067 ``` 4068 4069### readSequenceable 4070 4071readSequenceable(dataIn: Sequenceable): boolean 4072 4073Reads member variables from this **MessageParcel** object. 4074 4075**System capability**: SystemCapability.Communication.IPC.Core 4076 4077**Parameters** 4078 4079 | Name| Type | Mandatory | Description | 4080 | ------ | ----------------------------- | ------- | ---------------------------------------------- | 4081 | dataIn | [Sequenceable](#sequenceabledeprecated) | Yes | Object that reads member variables from the **MessageParcel** object.| 4082 4083**Return value** 4084 4085 | Type | Description | 4086 | ------- | ---------------------------------------- | 4087 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4088 4089**Example** 4090 4091 ```ts 4092 class MySequenceable { 4093 num: number; 4094 str: string; 4095 constructor(num, str) { 4096 this.num = num; 4097 this.str = str; 4098 } 4099 marshalling(messageParcel) { 4100 messageParcel.writeInt(this.num); 4101 messageParcel.writeString(this.str); 4102 return true; 4103 } 4104 unmarshalling(messageParcel) { 4105 this.num = messageParcel.readInt(); 4106 this.str = messageParcel.readString(); 4107 return true; 4108 } 4109 } 4110 let sequenceable = new MySequenceable(1, "aaa"); 4111 let data = rpc.MessageParcel.create(); 4112 let result = data.writeSequenceable(sequenceable); 4113 console.log("RpcClient: writeSequenceable is " + result); 4114 let ret = new MySequenceable(0, ""); 4115 let result2 = data.readSequenceable(ret); 4116 console.log("RpcClient: writeSequenceable is " + result2); 4117 ``` 4118 4119### writeByteArray 4120 4121writeByteArray(byteArray: number[]): boolean 4122 4123Writes a byte array to this **MessageParcel** object. 4124 4125**System capability**: SystemCapability.Communication.IPC.Core 4126 4127**Parameters** 4128 4129 | Name | Type | Mandatory| Description | 4130 | --------- | -------- | ---- | ------------------ | 4131 | byteArray | number[] | Yes | Byte array to write.| 4132 4133**Return value** 4134 4135 | Type | Description | 4136 | ------- | -------------------------------- | 4137 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4138 4139**Example** 4140 4141 ```ts 4142 let data = rpc.MessageParcel.create(); 4143 let ByteArrayVar = [1, 2, 3, 4, 5]; 4144 let result = data.writeByteArray(ByteArrayVar); 4145 console.log("RpcClient: writeByteArray is " + result); 4146 ``` 4147 4148### readByteArray 4149 4150readByteArray(dataIn: number[]): void 4151 4152Reads a byte array from this **MessageParcel** object. 4153 4154**System capability**: SystemCapability.Communication.IPC.Core 4155 4156**Parameters** 4157 4158 | Name| Type | Mandatory| Description | 4159 | ------ | -------- | ---- | ------------------ | 4160 | dataIn | number[] | Yes | Byte array to read.| 4161 4162**Example** 4163 4164 ```ts 4165 let data = rpc.MessageParcel.create(); 4166 let ByteArrayVar = [1, 2, 3, 4, 5]; 4167 let result = data.writeByteArray(ByteArrayVar); 4168 console.log("RpcClient: writeByteArray is " + result); 4169 let array = new Array(5); 4170 data.readByteArray(array); 4171 ``` 4172 4173### readByteArray 4174 4175readByteArray(): number[] 4176 4177Reads the byte array from this **MessageParcel** object. 4178 4179**System capability**: SystemCapability.Communication.IPC.Core 4180 4181**Return value** 4182 4183 | Type | Description | 4184 | -------- | -------------- | 4185 | number[] | Byte array read.| 4186 4187**Example** 4188 4189 ```ts 4190 let data = rpc.MessageParcel.create(); 4191 let ByteArrayVar = [1, 2, 3, 4, 5]; 4192 let result = data.writeByteArray(ByteArrayVar); 4193 console.log("RpcClient: writeByteArray is " + result); 4194 let array = data.readByteArray(); 4195 console.log("RpcClient: readByteArray is " + array); 4196 ``` 4197 4198### writeShortArray 4199 4200writeShortArray(shortArray: number[]): boolean 4201 4202Writes a short array to this **MessageParcel** object. 4203 4204**System capability**: SystemCapability.Communication.IPC.Core 4205 4206**Parameters** 4207 4208 | Name | Type | Mandatory| Description | 4209 | ---------- | -------- | ---- | -------------------- | 4210 | shortArray | number[] | Yes | Short array to write.| 4211 4212**Return value** 4213 4214 | Type | Description | 4215 | ------- | -------------------------------- | 4216 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4217 4218**Example** 4219 4220 ```ts 4221 let data = rpc.MessageParcel.create(); 4222 let result = data.writeShortArray([11, 12, 13]); 4223 console.log("RpcClient: writeShortArray is " + result); 4224 ``` 4225 4226### readShortArray 4227 4228readShortArray(dataIn: number[]): void 4229 4230Reads a short array from this **MessageParcel** object. 4231 4232**System capability**: SystemCapability.Communication.IPC.Core 4233 4234**Parameters** 4235 4236 | Name| Type | Mandatory| Description | 4237 | ------ | -------- | ---- | -------------------- | 4238 | dataIn | number[] | Yes | Short array to read.| 4239 4240**Example** 4241 4242 ```ts 4243 let data = rpc.MessageParcel.create(); 4244 let result = data.writeShortArray([11, 12, 13]); 4245 console.log("RpcClient: writeShortArray is " + result); 4246 let array = new Array(3); 4247 data.readShortArray(array); 4248 ``` 4249 4250### readShortArray 4251 4252readShortArray(): number[] 4253 4254Reads the short array from this **MessageParcel** object. 4255 4256**System capability**: SystemCapability.Communication.IPC.Core 4257 4258**Return value** 4259 4260 | Type | Description | 4261 | -------- | ---------------- | 4262 | number[] | Short array read.| 4263 4264**Example** 4265 4266 ```ts 4267 let data = rpc.MessageParcel.create(); 4268 let result = data.writeShortArray([11, 12, 13]); 4269 console.log("RpcClient: writeShortArray is " + result); 4270 let array = data.readShortArray(); 4271 console.log("RpcClient: readShortArray is " + array); 4272 ``` 4273 4274### writeIntArray 4275 4276writeIntArray(intArray: number[]): boolean 4277 4278Writes an integer array to this **MessageParcel** object. 4279 4280**System capability**: SystemCapability.Communication.IPC.Core 4281 4282**Parameters** 4283 4284 | Name | Type | Mandatory| Description | 4285 | -------- | -------- | ---- | ------------------ | 4286 | intArray | number[] | Yes | Integer array to write.| 4287 4288**Return value** 4289 4290 | Type | Description | 4291 | ------- | -------------------------------- | 4292 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4293 4294**Example** 4295 4296 ```ts 4297 let data = rpc.MessageParcel.create(); 4298 let result = data.writeIntArray([100, 111, 112]); 4299 console.log("RpcClient: writeIntArray is " + result); 4300 ``` 4301 4302### readIntArray 4303 4304readIntArray(dataIn: number[]): void 4305 4306Reads an integer array from this **MessageParcel** object. 4307 4308**System capability**: SystemCapability.Communication.IPC.Core 4309 4310**Parameters** 4311 4312 | Name| Type | Mandatory| Description | 4313 | ------ | -------- | ---- | ------------------ | 4314 | dataIn | number[] | Yes | Integer array to read.| 4315 4316**Example** 4317 4318 ```ts 4319 let data = rpc.MessageParcel.create(); 4320 let result = data.writeIntArray([100, 111, 112]); 4321 console.log("RpcClient: writeIntArray is " + result); 4322 let array = new Array(3); 4323 data.readIntArray(array); 4324 ``` 4325 4326### readIntArray 4327 4328readIntArray(): number[] 4329 4330Reads the integer array from this **MessageParcel** object. 4331 4332**System capability**: SystemCapability.Communication.IPC.Core 4333 4334**Return value** 4335 4336 | Type | Description | 4337 | -------- | -------------- | 4338 | number[] | Integer array read.| 4339 4340**Example** 4341 4342 ```ts 4343 let data = rpc.MessageParcel.create(); 4344 let result = data.writeIntArray([100, 111, 112]); 4345 console.log("RpcClient: writeIntArray is " + result); 4346 let array = data.readIntArray(); 4347 console.log("RpcClient: readIntArray is " + array); 4348 ``` 4349 4350### writeLongArray 4351 4352writeLongArray(longArray: number[]): boolean 4353 4354Writes a long array to this **MessageParcel** object. 4355 4356**System capability**: SystemCapability.Communication.IPC.Core 4357 4358**Parameters** 4359 4360 | Name | Type | Mandatory| Description | 4361 | --------- | -------- | ---- | -------------------- | 4362 | longArray | number[] | Yes | Long array to write.| 4363 4364**Return value** 4365 4366 | Type | Description | 4367 | ------- | ----------------------------- | 4368 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4369 4370**Example** 4371 4372 ```ts 4373 let data = rpc.MessageParcel.create(); 4374 let result = data.writeLongArray([1111, 1112, 1113]); 4375 console.log("RpcClient: writeLongArray is " + result); 4376 ``` 4377 4378### readLongArray 4379 4380readLongArray(dataIn: number[]): void 4381 4382Reads a long array from this **MessageParcel** object. 4383 4384**System capability**: SystemCapability.Communication.IPC.Core 4385 4386**Parameters** 4387 4388 | Name| Type | Mandatory| Description | 4389 | ------ | -------- | ---- | -------------------- | 4390 | dataIn | number[] | Yes | Long array to read.| 4391 4392**Example** 4393 4394 ```ts 4395 let data = rpc.MessageParcel.create(); 4396 let result = data.writeLongArray([1111, 1112, 1113]); 4397 console.log("RpcClient: writeLongArray is " + result); 4398 let array = new Array(3); 4399 data.readLongArray(array); 4400 ``` 4401 4402### readLongArray 4403 4404readLongArray(): number[] 4405 4406Reads the long array from this **MessageParcel** object. 4407 4408**System capability**: SystemCapability.Communication.IPC.Core 4409 4410**Return value** 4411 4412 | Type | Description | 4413 | -------- | ---------------- | 4414 | number[] | Long array read.| 4415 4416**Example** 4417 4418 ```ts 4419 let data = rpc.MessageParcel.create(); 4420 let result = data.writeLongArray([1111, 1112, 1113]); 4421 console.log("RpcClient: writeLongArray is " + result); 4422 let array = data.readLongArray(); 4423 console.log("RpcClient: readLongArray is " + array); 4424 ``` 4425 4426### writeFloatArray 4427 4428writeFloatArray(floatArray: number[]): boolean 4429 4430Writes a FloatArray to this **MessageParcel** object. 4431 4432**System capability**: SystemCapability.Communication.IPC.Core 4433 4434**Parameters** 4435 4436 | Name| Type| Mandatory| Description | 4437 | ---------- | -------- | ---- | --- | 4438 | floatArray | number[] | Yes | Floating-point array to write. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 4439 4440**Return value** 4441 4442 | Type | Description | 4443 | ------- | -------------------------------- | 4444 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4445 4446**Example** 4447 4448 ```ts 4449 let data = rpc.MessageParcel.create(); 4450 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 4451 console.log("RpcClient: writeFloatArray is " + result); 4452 ``` 4453 4454### readFloatArray 4455 4456readFloatArray(dataIn: number[]): void 4457 4458Reads a FloatArray from this **MessageParcel** object. 4459 4460**System capability**: SystemCapability.Communication.IPC.Core 4461 4462**Parameters** 4463 4464 | Name| Type | Mandatory| Description | 4465 | ------ | -------- | ---- | ------ | 4466 | dataIn | number[] | Yes | Floating-point array to read. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 4467 4468**Example** 4469 4470 ```ts 4471 let data = rpc.MessageParcel.create(); 4472 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 4473 console.log("RpcClient: writeFloatArray is " + result); 4474 let array = new Array(3); 4475 data.readFloatArray(array); 4476 ``` 4477 4478### readFloatArray 4479 4480readFloatArray(): number[] 4481 4482Reads the FloatArray from this **MessageParcel** object. 4483 4484**System capability**: SystemCapability.Communication.IPC.Core 4485 4486**Return value** 4487 4488 | Type | Description | 4489 | -------- | -------------- | 4490 | number[] | FloatArray read.| 4491 4492**Example** 4493 4494 ```ts 4495 let data = rpc.MessageParcel.create(); 4496 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 4497 console.log("RpcClient: writeFloatArray is " + result); 4498 let array = data.readFloatArray(); 4499 console.log("RpcClient: readFloatArray is " + array); 4500 ``` 4501 4502### writeDoubleArray 4503 4504writeDoubleArray(doubleArray: number[]): boolean 4505 4506Writes a DoubleArray to this **MessageParcel** object. 4507 4508**System capability**: SystemCapability.Communication.IPC.Core 4509 4510**Parameters** 4511 4512 | Name | Type | Mandatory| Description | 4513 | ----------- | -------- | ---- | ------------------------ | 4514 | doubleArray | number[] | Yes | DoubleArray to write.| 4515 4516**Return value** 4517 4518 | Type | Description | 4519 | ------- | -------------------------------- | 4520 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4521 4522**Example** 4523 4524 ```ts 4525 let data = rpc.MessageParcel.create(); 4526 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 4527 console.log("RpcClient: writeDoubleArray is " + result); 4528 ``` 4529 4530### readDoubleArray 4531 4532readDoubleArray(dataIn: number[]): void 4533 4534Reads a DoubleArray from this **MessageParcel** object. 4535 4536**System capability**: SystemCapability.Communication.IPC.Core 4537 4538**Parameters** 4539 4540 | Name| Type | Mandatory| Description | 4541 | ------ | -------- | ---- | ------------------------ | 4542 | dataIn | number[] | Yes | DoubleArray to read.| 4543 4544**Example** 4545 4546 ```ts 4547 let data = rpc.MessageParcel.create(); 4548 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 4549 console.log("RpcClient: writeDoubleArray is " + result); 4550 let array = new Array(3); 4551 data.readDoubleArray(array); 4552 ``` 4553 4554### readDoubleArray 4555 4556readDoubleArray(): number[] 4557 4558Reads the DoubleArray from this **MessageParcel** object. 4559 4560**System capability**: SystemCapability.Communication.IPC.Core 4561 4562**Return value** 4563 4564 | Type | Description | 4565 | -------- | -------------------- | 4566 | number[] | DoubleArray read.| 4567 4568**Example** 4569 4570 ```ts 4571 let data = rpc.MessageParcel.create(); 4572 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 4573 console.log("RpcClient: writeDoubleArray is " + result); 4574 let array = data.readDoubleArray(); 4575 console.log("RpcClient: readDoubleArray is " + array); 4576 ``` 4577 4578### writeBooleanArray 4579 4580writeBooleanArray(booleanArray: boolean[]): boolean 4581 4582Writes a Boolean array to this **MessageParcel** object. 4583 4584**System capability**: SystemCapability.Communication.IPC.Core 4585 4586**Parameters** 4587 4588 | Name | Type | Mandatory| Description | 4589 | ------------ | --------- | ---- | ------------------ | 4590 | booleanArray | boolean[] | Yes | Boolean array to write.| 4591 4592**Return value** 4593 4594 | Type | Description | 4595 | ------- | -------------------------------- | 4596 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4597 4598**Example** 4599 4600 ```ts 4601 let data = rpc.MessageParcel.create(); 4602 let result = data.writeBooleanArray([false, true, false]); 4603 console.log("RpcClient: writeBooleanArray is " + result); 4604 ``` 4605 4606### readBooleanArray 4607 4608readBooleanArray(dataIn: boolean[]): void 4609 4610Reads a Boolean array from this **MessageParcel** object. 4611 4612**System capability**: SystemCapability.Communication.IPC.Core 4613 4614**Parameters** 4615 4616 | Name| Type | Mandatory| Description | 4617 | ------ | --------- | ---- | ------------------ | 4618 | dataIn | boolean[] | Yes | Boolean array to read.| 4619 4620**Example** 4621 4622 ```ts 4623 let data = rpc.MessageParcel.create(); 4624 let result = data.writeBooleanArray([false, true, false]); 4625 console.log("RpcClient: writeBooleanArray is " + result); 4626 let array = new Array(3); 4627 data.readBooleanArray(array); 4628 ``` 4629 4630### readBooleanArray 4631 4632readBooleanArray(): boolean[] 4633 4634Reads the Boolean array from this **MessageParcel** object. 4635 4636**System capability**: SystemCapability.Communication.IPC.Core 4637 4638**Return value** 4639 4640 | Type | Description | 4641 | --------- | -------------- | 4642 | boolean[] | Boolean array read.| 4643 4644**Example** 4645 4646 ```ts 4647 let data = rpc.MessageParcel.create(); 4648 let result = data.writeBooleanArray([false, true, false]); 4649 console.log("RpcClient: writeBooleanArray is " + result); 4650 let array = data.readBooleanArray(); 4651 console.log("RpcClient: readBooleanArray is " + array); 4652 ``` 4653 4654### writeCharArray 4655 4656writeCharArray(charArray: number[]): boolean 4657 4658Writes a character array to this **MessageParcel** object. 4659 4660**System capability**: SystemCapability.Communication.IPC.Core 4661 4662**Parameters** 4663 4664 | Name | Type | Mandatory| Description | 4665 | --------- | -------- | ---- | ---------------------- | 4666 | charArray | number[] | Yes | Character array to write.| 4667 4668**Return value** 4669 4670 | Type | Description | 4671 | ------- | -------------------------------- | 4672 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4673 4674**Example** 4675 4676 ```ts 4677 let data = rpc.MessageParcel.create(); 4678 let result = data.writeCharArray([97, 98, 88]); 4679 console.log("RpcClient: writeCharArray is " + result); 4680 ``` 4681 4682### readCharArray 4683 4684readCharArray(dataIn: number[]): void 4685 4686Reads a character array from this **MessageParcel** object. 4687 4688**System capability**: SystemCapability.Communication.IPC.Core 4689 4690**Parameters** 4691 4692 | Name| Type | Mandatory| Description | 4693 | ------ | -------- | ---- | ---------------------- | 4694 | dataIn | number[] | Yes | Character array to read.| 4695 4696**Example** 4697 4698 ```ts 4699 let data = rpc.MessageParcel.create(); 4700 let result = data.writeCharArray([97, 98, 99]); 4701 console.log("RpcClient: writeCharArray is " + result); 4702 let array = new Array(3); 4703 data.readCharArray(array); 4704 ``` 4705 4706### readCharArray 4707 4708readCharArray(): number[] 4709 4710Reads the character array from this **MessageParcel** object. 4711 4712**System capability**: SystemCapability.Communication.IPC.Core 4713 4714**Return value** 4715 4716 | Type | Description | 4717 | -------- | ------------------ | 4718 | number[] | Character array read.| 4719 4720**Example** 4721 4722 ```ts 4723 let data = rpc.MessageParcel.create(); 4724 let result = data.writeCharArray([97, 98, 99]); 4725 console.log("RpcClient: writeCharArray is " + result); 4726 let array = data.readCharArray(); 4727 console.log("RpcClient: readCharArray is " + array); 4728 ``` 4729 4730### writeStringArray 4731 4732writeStringArray(stringArray: string[]): boolean 4733 4734Writes a string array to this **MessageParcel** object. 4735 4736**System capability**: SystemCapability.Communication.IPC.Core 4737 4738**Parameters** 4739 4740 | Name | Type | Mandatory| Description | 4741 | ----------- | -------- | ---- | ---------------- | 4742 | stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 4743 4744**Return value** 4745 4746 | Type | Description| 4747 | ------- | -------------------------------- | 4748 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4749 4750**Example** 4751 4752 ```ts 4753 let data = rpc.MessageParcel.create(); 4754 let result = data.writeStringArray(["abc", "def"]); 4755 console.log("RpcClient: writeStringArray is " + result); 4756 ``` 4757 4758### readStringArray 4759 4760readStringArray(dataIn: string[]): void 4761 4762Reads a string array from this **MessageParcel** object. 4763 4764**System capability**: SystemCapability.Communication.IPC.Core 4765 4766**Parameters** 4767 4768 | Name| Type | Mandatory| Description | 4769 | ------ | -------- | ---- | -------------------- | 4770 | dataIn | string[] | Yes | String array to read.| 4771 4772**Example** 4773 4774 ```ts 4775 let data = rpc.MessageParcel.create(); 4776 let result = data.writeStringArray(["abc", "def"]); 4777 console.log("RpcClient: writeStringArray is " + result); 4778 let array = new Array(2); 4779 data.readStringArray(array); 4780 ``` 4781 4782### readStringArray 4783 4784readStringArray(): string[] 4785 4786Reads the string array from this **MessageParcel** object. 4787 4788**System capability**: SystemCapability.Communication.IPC.Core 4789 4790**Return value** 4791 4792 | Type | Description | 4793 | -------- | ---------------- | 4794 | string[] | String array read.| 4795 4796**Example** 4797 4798 ```ts 4799 let data = rpc.MessageParcel.create(); 4800 let result = data.writeStringArray(["abc", "def"]); 4801 console.log("RpcClient: writeStringArray is " + result); 4802 let array = data.readStringArray(); 4803 console.log("RpcClient: readStringArray is " + array); 4804 ``` 4805 4806### writeNoException<sup>8+</sup> 4807 4808writeNoException(): void 4809 4810Writes information to this **MessageParcel** object indicating that no exception occurred. 4811 4812**System capability**: SystemCapability.Communication.IPC.Core 4813 4814**Example** 4815 4816 ```ts 4817 class MyDeathRecipient { 4818 onRemoteDied() { 4819 console.log("server died"); 4820 } 4821 } 4822 class TestRemoteObject extends rpc.RemoteObject { 4823 constructor(descriptor) { 4824 super(descriptor); 4825 } 4826 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 4827 return true; 4828 } 4829 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 4830 return true; 4831 } 4832 isObjectDead(): boolean { 4833 return false; 4834 } 4835 onRemoteRequest(code, data, reply, option) { 4836 if (code === 1) { 4837 console.log("RpcServer: onRemoteRequest called"); 4838 reply.writeNoException(); 4839 return true; 4840 } else { 4841 console.log("RpcServer: unknown code: " + code); 4842 return false; 4843 } 4844 } 4845 } 4846 ``` 4847 4848### readException<sup>8+</sup> 4849 4850readException(): void 4851 4852Reads the exception information from this **MessageParcel** object. 4853 4854**System capability**: SystemCapability.Communication.IPC.Core 4855 4856**Example** 4857 4858 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 4859 4860 ```ts 4861 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 4862 // import FA from "@ohos.ability.featureAbility"; 4863 4864 let proxy; 4865 let connect = { 4866 onConnect: function(elementName, remoteProxy) { 4867 console.log("RpcClient: js onConnect called."); 4868 proxy = remoteProxy; 4869 }, 4870 onDisconnect: function(elementName) { 4871 console.log("RpcClient: onDisconnect"); 4872 }, 4873 onFailed: function() { 4874 console.log("RpcClient: onFailed"); 4875 } 4876 }; 4877 let want = { 4878 "bundleName": "com.ohos.server", 4879 "abilityName": "com.ohos.server.EntryAbility", 4880 }; 4881 4882 // Use this method to connect to the ability for the FA model. 4883 // FA.connectAbility(want,connect); 4884 4885 globalThis.context.connectServiceExtensionAbility(want, connect); 4886 ``` 4887 4888 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 4889 4890 ```ts 4891 let option = new rpc.MessageOption(); 4892 let data = rpc.MessageParcel.create(); 4893 let reply = rpc.MessageParcel.create(); 4894 data.writeInt(1); 4895 data.writeString("hello"); 4896 proxy.sendMessageRequest(1, data, reply, option) 4897 .then(function(errCode) { 4898 if (errCode === 0) { 4899 console.log("sendMessageRequest got result"); 4900 reply.readException(); 4901 let msg = reply.readString(); 4902 console.log("RPCTest: reply msg: " + msg); 4903 } else { 4904 console.log("RPCTest: sendMessageRequest failed, errCode: " + errCode); 4905 } 4906 }).catch(function(e) { 4907 console.log("RPCTest: sendMessageRequest got exception: " + e.message); 4908 }).finally (() => { 4909 console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); 4910 data.reclaim(); 4911 reply.reclaim(); 4912 }); 4913 ``` 4914 4915### writeSequenceableArray 4916 4917writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean 4918 4919Writes a sequenceable array to this **MessageParcel** object. 4920 4921**System capability**: SystemCapability.Communication.IPC.Core 4922 4923**Parameters** 4924 4925 | Name | Type | Mandatory| Description | 4926 | ----------------- | -------------- | ---- | -------------------------- | 4927 | sequenceableArray | Sequenceable[] | Yes | Sequenceable array to write.| 4928 4929**Return value** 4930 4931 | Type | Description | 4932 | ------- | -------------------------------- | 4933 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4934 4935**Example** 4936 4937 ```ts 4938 class MySequenceable { 4939 num: number; 4940 str: string; 4941 constructor(num, str) { 4942 this.num = num; 4943 this.str = str; 4944 } 4945 marshalling(messageParcel) { 4946 messageParcel.writeInt(this.num); 4947 messageParcel.writeString(this.str); 4948 return true; 4949 } 4950 unmarshalling(messageParcel) { 4951 this.num = messageParcel.readInt(); 4952 this.str = messageParcel.readString(); 4953 return true; 4954 } 4955 } 4956 let sequenceable = new MySequenceable(1, "aaa"); 4957 let sequenceable2 = new MySequenceable(2, "bbb"); 4958 let sequenceable3 = new MySequenceable(3, "ccc"); 4959 let a = [sequenceable, sequenceable2, sequenceable3]; 4960 let data = rpc.MessageParcel.create(); 4961 let result = data.writeSequenceableArray(a); 4962 console.log("RpcClient: writeSequenceableArray is " + result); 4963 ``` 4964 4965### readSequenceableArray<sup>8+</sup> 4966 4967readSequenceableArray(sequenceableArray: Sequenceable[]): void 4968 4969Reads a sequenceable array from this **MessageParcel** object. 4970 4971**System capability**: SystemCapability.Communication.IPC.Core 4972 4973**Parameters** 4974 4975 | Name | Type | Mandatory| Description | 4976 | ----------------- | -------------- | ---- | -------------------------- | 4977 | sequenceableArray | Sequenceable[] | Yes | Sequenceable array to read.| 4978 4979**Example** 4980 4981 ```ts 4982 class MySequenceable { 4983 num: number; 4984 str: string; 4985 constructor(num, str) { 4986 this.num = num; 4987 this.str = str; 4988 } 4989 marshalling(messageParcel) { 4990 messageParcel.writeInt(this.num); 4991 messageParcel.writeString(this.str); 4992 return true; 4993 } 4994 unmarshalling(messageParcel) { 4995 this.num = messageParcel.readInt(); 4996 this.str = messageParcel.readString(); 4997 return true; 4998 } 4999 } 5000 let sequenceable = new MySequenceable(1, "aaa"); 5001 let sequenceable2 = new MySequenceable(2, "bbb"); 5002 let sequenceable3 = new MySequenceable(3, "ccc"); 5003 let a = [sequenceable, sequenceable2, sequenceable3]; 5004 let data = rpc.MessageParcel.create(); 5005 let result = data.writeSequenceableArray(a); 5006 console.log("RpcClient: writeSequenceableArray is " + result); 5007 let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")]; 5008 data.readSequenceableArray(b); 5009 ``` 5010 5011### writeRemoteObjectArray<sup>8+</sup> 5012 5013writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean 5014 5015Writes an array of **IRemoteObject** objects to this **MessageParcel** object. 5016 5017**System capability**: SystemCapability.Communication.IPC.Core 5018 5019**Parameters** 5020 5021 | Name | Type | Mandatory| Description | 5022 | ----------- | --------------- | ---- | ----- | 5023 | objectArray | IRemoteObject[] | Yes | Array of **IRemoteObject** objects to write.| 5024 5025**Return value** 5026 5027 | Type | Description | 5028 | ------- | -------------------------------- | 5029 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5030 5031**Example** 5032 5033 ```ts 5034 class MyDeathRecipient { 5035 onRemoteDied() { 5036 console.log("server died"); 5037 } 5038 } 5039 class TestRemoteObject extends rpc.RemoteObject { 5040 constructor(descriptor) { 5041 super(descriptor); 5042 this.attachLocalInterface(this, descriptor); 5043 } 5044 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5045 return true; 5046 } 5047 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5048 return true; 5049 } 5050 isObjectDead(): boolean { 5051 return false; 5052 } 5053 asObject(): rpc.IRemoteObject { 5054 return this; 5055 } 5056 } 5057 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5058 let data = rpc.MessageParcel.create(); 5059 let result = data.writeRemoteObjectArray(a); 5060 console.log("RpcClient: writeRemoteObjectArray is " + result); 5061 ``` 5062 5063### readRemoteObjectArray<sup>8+</sup> 5064 5065readRemoteObjectArray(objects: IRemoteObject[]): void 5066 5067Reads an **IRemoteObject** array from this **MessageParcel** object. 5068 5069**System capability**: SystemCapability.Communication.IPC.Core 5070 5071**Parameters** 5072 5073 | Name | Type | Mandatory| Description | 5074 | ------- | --------------- | ---- | --------- | 5075 | objects | IRemoteObject[] | Yes | **IRemoteObject** array to read.| 5076 5077**Example** 5078 5079 ```ts 5080 class MyDeathRecipient { 5081 onRemoteDied() { 5082 console.log("server died"); 5083 } 5084 } 5085 class TestRemoteObject extends rpc.RemoteObject { 5086 constructor(descriptor) { 5087 super(descriptor); 5088 this.attachLocalInterface(this, descriptor); 5089 } 5090 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5091 return true; 5092 } 5093 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5094 return true; 5095 } 5096 isObjectDead(): boolean { 5097 return false; 5098 } 5099 asObject(): rpc.IRemoteObject { 5100 return this; 5101 } 5102 } 5103 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5104 let data = rpc.MessageParcel.create(); 5105 let result = data.writeRemoteObjectArray(a); 5106 let b = new Array(3); 5107 data.readRemoteObjectArray(b); 5108 ``` 5109 5110### readRemoteObjectArray<sup>8+</sup> 5111 5112readRemoteObjectArray(): IRemoteObject[] 5113 5114Reads the **IRemoteObject** array from this **MessageParcel** object. 5115 5116**System capability**: SystemCapability.Communication.IPC.Core 5117 5118**Return value** 5119 5120 | Type | Description | 5121 | --------------- | --------------------------- | 5122 | IRemoteObject[] | **IRemoteObject** object array obtained.| 5123 5124**Example** 5125 5126 ```ts 5127 class MyDeathRecipient { 5128 onRemoteDied() { 5129 console.log("server died"); 5130 } 5131 } 5132 class TestRemoteObject extends rpc.RemoteObject { 5133 constructor(descriptor) { 5134 super(descriptor); 5135 this.attachLocalInterface(this, descriptor); 5136 } 5137 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5138 return true; 5139 } 5140 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5141 return true; 5142 } 5143 isObjectDead(): boolean { 5144 return false; 5145 } 5146 asObject(): rpc.IRemoteObject { 5147 return this; 5148 } 5149 } 5150 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5151 let data = rpc.MessageParcel.create(); 5152 let result = data.writeRemoteObjectArray(a); 5153 console.log("RpcClient: readRemoteObjectArray is " + result); 5154 let b = data.readRemoteObjectArray(); 5155 console.log("RpcClient: readRemoteObjectArray is " + b); 5156 ``` 5157 5158### closeFileDescriptor<sup>8+</sup> 5159 5160static closeFileDescriptor(fd: number): void 5161 5162Closes a file descriptor. This API is a static method. 5163 5164**System capability**: SystemCapability.Communication.IPC.Core 5165 5166**Parameters** 5167 5168 | Name| Type | Mandatory| Description | 5169 | ------ | ------ | ---- | -------------------- | 5170 | fd | number | Yes | File descriptor to close.| 5171 5172**Example** 5173 5174 ```ts 5175 import fs from '@ohos.file.fs'; 5176 let filePath = "path/to/file"; 5177 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5178 rpc.MessageParcel.closeFileDescriptor(file.fd); 5179 ``` 5180 5181### dupFileDescriptor<sup>8+</sup> 5182 5183static dupFileDescriptor(fd: number) :number 5184 5185Duplicates a file descriptor. This API is a static method. 5186 5187**System capability**: SystemCapability.Communication.IPC.Core 5188 5189**Parameters** 5190 5191 | Name| Type | Mandatory| Description | 5192 | ------ | ------ | ---- | ------------------------ | 5193 | fd | number | Yes | File descriptor to duplicate.| 5194 5195**Return value** 5196 5197 | Type | Description | 5198 | ------ | -------------------- | 5199 | number | New file descriptor.| 5200 5201**Example** 5202 5203 ```ts 5204 import fs from '@ohos.file.fs'; 5205 let filePath = "path/to/file"; 5206 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5207 let newFd = rpc.MessageParcel.dupFileDescriptor(file.fd); 5208 ``` 5209 5210### containFileDescriptors<sup>8+</sup> 5211 5212containFileDescriptors(): boolean 5213 5214Checks whether this **MessageParcel** object contains file descriptors. 5215 5216**System capability**: SystemCapability.Communication.IPC.Core 5217 5218**Return value** 5219 5220 | Type | Description | 5221 | ------- | --------------------------------------------- | 5222 | boolean |Returns **true** if the **MessageParcel** object contains file descriptors; returns **false** otherwise.| 5223 5224**Example** 5225 5226 ```ts 5227 import fs from '@ohos.file.fs'; 5228 let parcel = new rpc.MessageParcel(); 5229 let filePath = "path/to/file"; 5230 let r1 = parcel.containFileDescriptors(); 5231 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5232 let writeResult = parcel.writeFileDescriptor(file.fd); 5233 console.log("RpcTest: parcel writeFd result is : " + writeResult); 5234 let containFD = parcel.containFileDescriptors(); 5235 console.log("RpcTest: parcel after write fd containFd result is : " + containFD); 5236 ``` 5237 5238### writeFileDescriptor<sup>8+</sup> 5239 5240writeFileDescriptor(fd: number): boolean 5241 5242Writes a file descriptor to this **MessageParcel** object. 5243 5244**System capability**: SystemCapability.Communication.IPC.Core 5245 5246**Parameters** 5247 5248 | Name| Type | Mandatory| Description | 5249 | ------ | ------ | ---- | ------------ | 5250 | fd | number | Yes | File descriptor to write.| 5251 5252**Return value** 5253 5254 | Type | Description | 5255 | ------- | -------------------------------- | 5256 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5257 5258**Example** 5259 5260 ```ts 5261 import fs from '@ohos.file.fs'; 5262 let parcel = new rpc.MessageParcel(); 5263 let filePath = "path/to/file"; 5264 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5265 let writeResult = parcel.writeFileDescriptor(file.fd); 5266 console.log("RpcTest: parcel writeFd result is : " + writeResult); 5267 ``` 5268 5269### readFileDescriptor<sup>8+</sup> 5270 5271readFileDescriptor(): number 5272 5273Reads the file descriptor from this **MessageParcel** object. 5274 5275**System capability**: SystemCapability.Communication.IPC.Core 5276 5277**Return value** 5278 5279 | Type | Description | 5280 | ------ | ---------------- | 5281 | number | File descriptor read.| 5282 5283**Example** 5284 5285 ```ts 5286 import fs from '@ohos.file.fs'; 5287 let parcel = new rpc.MessageParcel(); 5288 let filePath = "path/to/file"; 5289 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5290 let writeResult = parcel.writeFileDescriptor(file.fd); 5291 let readFD = parcel.readFileDescriptor(); 5292 console.log("RpcTest: parcel read fd is : " + readFD); 5293 ``` 5294 5295### writeAshmem<sup>8+</sup> 5296 5297writeAshmem(ashmem: Ashmem): boolean 5298 5299Writes an anonymous shared object to this **MessageParcel** object. 5300 5301**System capability**: SystemCapability.Communication.IPC.Core 5302 5303**Parameters** 5304 5305 | Name| Type | Mandatory| Description | 5306 | ------ | ------ | ---- | ----------------------------------- | 5307 | ashmem | Ashmem | Yes | Anonymous shared object to write.| 5308 5309**Return value** 5310 5311 | Type | Description | 5312 | ------- | -------------------------------- | 5313 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5314 5315**Example** 5316 5317 ```ts 5318 let parcel = new rpc.MessageParcel(); 5319 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 5320 let isWriteSuccess = parcel.writeAshmem(ashmem); 5321 console.log("RpcTest: write ashmem to result is : " + isWriteSuccess); 5322 ``` 5323 5324### readAshmem<sup>8+</sup> 5325 5326readAshmem(): Ashmem 5327 5328Reads the anonymous shared object from this **MessageParcel** object. 5329 5330**System capability**: SystemCapability.Communication.IPC.Core 5331 5332**Return value** 5333 5334 | Type | Description | 5335 | ------ | ------------------ | 5336 | Ashmem | Anonymous share object obtained.| 5337 5338**Example** 5339 5340 ```ts 5341 let parcel = new rpc.MessageParcel(); 5342 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 5343 let isWriteSuccess = parcel.writeAshmem(ashmem); 5344 console.log("RpcTest: write ashmem to result is : " + isWriteSuccess); 5345 let readAshmem = parcel.readAshmem(); 5346 console.log("RpcTest: read ashmem to result is : " + readAshmem); 5347 ``` 5348 5349### getRawDataCapacity<sup>8+</sup> 5350 5351getRawDataCapacity(): number 5352 5353Obtains the maximum amount of raw data that can be held by this **MessageParcel** object. 5354 5355**System capability**: SystemCapability.Communication.IPC.Core 5356 5357**Return value** 5358 5359 | Type | Description | 5360 | ------ | ---------------------------------------------------------- | 5361 | number | 128 MB, which is the maximum amount of raw data that can be held by this **MessageParcel** object.| 5362 5363**Example** 5364 5365 ```ts 5366 let parcel = new rpc.MessageParcel(); 5367 let result = parcel.getRawDataCapacity(); 5368 console.log("RpcTest: parcel get RawDataCapacity result is : " + result); 5369 ``` 5370 5371### writeRawData<sup>8+</sup> 5372 5373writeRawData(rawData: number[], size: number): boolean 5374 5375Writes raw data to this **MessageParcel** object. 5376 5377**System capability**: SystemCapability.Communication.IPC.Core 5378 5379**Parameters** 5380 5381 | Name | Type | Mandatory| Description | 5382 | ------- | -------- | ---- | ---------------------------------- | 5383 | rawData | number[] | Yes | Raw data to write. | 5384 | size | number | Yes | Size of the raw data, in bytes.| 5385 5386**Return value** 5387 5388 | Type | Description | 5389 | ------- | -------------------------------- | 5390 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5391 5392**Example** 5393 5394 ```ts 5395 let parcel = new rpc.MessageParcel(); 5396 let arr = [1, 2, 3, 4, 5]; 5397 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 5398 console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess); 5399 ``` 5400 5401### readRawData<sup>8+</sup> 5402 5403readRawData(size: number): number[] 5404 5405Reads raw data from this **MessageParcel** object. 5406 5407**System capability**: SystemCapability.Communication.IPC.Core 5408 5409**Parameters** 5410 5411 | Name| Type | Mandatory| Description | 5412 | ------ | ------ | ---- | ------------------------ | 5413 | size | number | Yes | Size of the raw data to read.| 5414 5415**Return value** 5416 5417 | Type | Description | 5418 | -------- | ------------------------------ | 5419 | number[] | Raw data obtained, in bytes.| 5420 5421**Example** 5422 5423 ```ts 5424 let parcel = new rpc.MessageParcel(); 5425 let arr = [1, 2, 3, 4, 5]; 5426 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 5427 console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess); 5428 let result = parcel.readRawData(5); 5429 console.log("RpcTest: parcel read raw data result is : " + result); 5430 ``` 5431 5432## Parcelable<sup>9+</sup> 5433 5434Writes an object to a **MessageSequence** and reads it from the **MessageSequence** during IPC. 5435 5436### marshalling 5437 5438marshalling(dataOut: MessageSequence): boolean 5439 5440Marshals this **Parcelable** object into a **MessageSequence** object. 5441 5442**System capability**: SystemCapability.Communication.IPC.Core 5443 5444**Parameters** 5445 5446 | Name | Type | Mandatory| Description | 5447 | ------- | --------------- | ---- | ------------------------------------------- | 5448 | dataOut | MessageSequence | Yes | **MessageSequence** object to which the **Parcelable** object is to be marshaled.| 5449 5450**Return value** 5451 5452 | Type | Description | 5453 | ------- | -------------------------------- | 5454 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5455**Example** 5456 5457 ```ts 5458 class MyParcelable { 5459 num: number; 5460 str: string; 5461 constructor(num, str) { 5462 this.num = num; 5463 this.str = str; 5464 } 5465 marshalling(messageSequence) { 5466 messageSequence.writeInt(this.num); 5467 messageSequence.writeString(this.str); 5468 return true; 5469 } 5470 unmarshalling(messageSequence) { 5471 this.num = messageSequence.readInt(); 5472 this.str = messageSequence.readString(); 5473 return true; 5474 } 5475 } 5476 let parcelable = new MyParcelable(1, "aaa"); 5477 let data = rpc.MessageSequence.create(); 5478 let result = data.writeParcelable(parcelable); 5479 console.log("RpcClient: writeParcelable is " + result); 5480 let ret = new MyParcelable(0, ""); 5481 let result2 = data.readParcelable(ret); 5482 console.log("RpcClient: readParcelable is " + result2); 5483 ``` 5484 5485### unmarshalling 5486 5487unmarshalling(dataIn: MessageSequence): boolean 5488 5489Unmarshals this **Parcelable** object from a **MessageSequence** object. 5490 5491**System capability**: SystemCapability.Communication.IPC.Core 5492 5493**Parameters** 5494 5495 | Name| Type | Mandatory| Description | 5496 | ------ | --------------- | ---- | ----------------------------------------------- | 5497 | dataIn | MessageSequence | Yes | **MessageSequence** object from which the **Parcelable** object is to be unmarshaled.| 5498 5499**Return value** 5500 5501 | Type | Description | 5502 | ------- | ---------------------------------------- | 5503 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5504 5505**Example** 5506 5507 ```ts 5508 class MyParcelable { 5509 num: number; 5510 str: string; 5511 constructor(num, str) { 5512 this.num = num; 5513 this.str = str; 5514 } 5515 marshalling(messageSequence) { 5516 messageSequence.writeInt(this.num); 5517 messageSequence.writeString(this.str); 5518 return true; 5519 } 5520 unmarshalling(messageSequence) { 5521 this.num = messageSequence.readInt(); 5522 this.str = messageSequence.readString(); 5523 return true; 5524 } 5525 } 5526 let parcelable = new MyParcelable(1, "aaa"); 5527 let data = rpc.MessageSequence.create(); 5528 let result = data.writeParcelable(parcelable); 5529 console.log("RpcClient: writeParcelable is " + result); 5530 let ret = new MyParcelable(0, ""); 5531 let result2 = data.readParcelable(ret); 5532 console.log("RpcClient: readParcelable is " + result2); 5533 ``` 5534 5535## Sequenceable<sup>(deprecated)</sup> 5536 5537>This class is no longer maintained since API version 9. You are advised to use the [Parcelable](#parcelable9). 5538 5539Writes objects of classes to a **MessageParcel** and reads them from the **MessageParcel** during IPC. 5540 5541### marshalling 5542 5543marshalling(dataOut: MessageParcel): boolean 5544 5545Marshals the sequenceable object into a **MessageParcel** object. 5546 5547**System capability**: SystemCapability.Communication.IPC.Core 5548 5549**Parameters** 5550 5551 | Name | Type | Mandatory| Description | 5552 | ------- | ----------------------------------------- | ---- | ----------------------------------------- | 5553 | dataOut | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object to which the sequenceable object is to be marshaled.| 5554 5555**Return value** 5556 5557 | Type | Description | 5558 | ------- | -------------------------------- | 5559 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5560**Example** 5561 5562 ```ts 5563 class MySequenceable { 5564 num: number; 5565 str: string; 5566 constructor(num, str) { 5567 this.num = num; 5568 this.str = str; 5569 } 5570 marshalling(messageParcel) { 5571 messageParcel.writeInt(this.num); 5572 messageParcel.writeString(this.str); 5573 return true; 5574 } 5575 unmarshalling(messageParcel) { 5576 this.num = messageParcel.readInt(); 5577 this.str = messageParcel.readString(); 5578 return true; 5579 } 5580 } 5581 let sequenceable = new MySequenceable(1, "aaa"); 5582 let data = rpc.MessageParcel.create(); 5583 let result = data.writeSequenceable(sequenceable); 5584 console.log("RpcClient: writeSequenceable is " + result); 5585 let ret = new MySequenceable(0, ""); 5586 let result2 = data.readSequenceable(ret); 5587 console.log("RpcClient: readSequenceable is " + result2); 5588 ``` 5589 5590### unmarshalling 5591 5592unmarshalling(dataIn: MessageParcel): boolean 5593 5594Unmarshals this sequenceable object from a **MessageParcel** object. 5595 5596**System capability**: SystemCapability.Communication.IPC.Core 5597 5598**Parameters** 5599 5600 | Name| Type | Mandatory| Description | 5601 | ------ | ----------------------------------------- | ---- | --------------------------------------------- | 5602 | dataIn | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object in which the sequenceable object is to be unmarshaled.| 5603 5604**Return value** 5605 5606 | Type | Description | 5607 | ------- | ---------------------------------------- | 5608 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5609 5610**Example** 5611 5612 ```ts 5613 class MySequenceable { 5614 num: number; 5615 str: string; 5616 constructor(num, str) { 5617 this.num = num; 5618 this.str = str; 5619 } 5620 marshalling(messageParcel) { 5621 messageParcel.writeInt(this.num); 5622 messageParcel.writeString(this.str); 5623 return true; 5624 } 5625 unmarshalling(messageParcel) { 5626 this.num = messageParcel.readInt(); 5627 this.str = messageParcel.readString(); 5628 return true; 5629 } 5630 } 5631 let sequenceable = new MySequenceable(1, "aaa"); 5632 let data = rpc.MessageParcel.create(); 5633 let result = data.writeSequenceable(sequenceable); 5634 console.log("RpcClient: writeSequenceable is " + result); 5635 let ret = new MySequenceable(0, ""); 5636 let result2 = data.readSequenceable(ret); 5637 console.log("RpcClient: readSequenceable is " + result2); 5638 ``` 5639 5640## IRemoteBroker 5641 5642Provides the holder of a remote proxy object. 5643 5644### asObject 5645 5646asObject(): IRemoteObject 5647 5648Obtains a proxy or remote object. This API must be implemented by its derived classes. 5649 5650**System capability**: SystemCapability.Communication.IPC.Core 5651 5652**Return value** 5653 5654 | Type | Description | 5655 | ----- | ----- | 5656 | [IRemoteObject](#iremoteobject) | Returns the **RemoteObject** if it is the caller; returns the [IRemoteObject](#iremoteobject), the holder of this **RemoteProxy** object, if the caller is a [RemoteProxy](#remoteproxy) object.| 5657 5658**Example** 5659 5660 ```ts 5661 class TestAbility extends rpc.RemoteObject { 5662 asObject() { 5663 return this; 5664 } 5665 } 5666 let remoteObject = new TestAbility("testObject").asObject(); 5667 ``` 5668 5669**Example** 5670 5671 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 5672 5673 ```ts 5674 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 5675 // import FA from "@ohos.ability.featureAbility"; 5676 5677 let proxy; 5678 let connect = { 5679 onConnect: function(elementName, remoteProxy) { 5680 console.log("RpcClient: js onConnect called."); 5681 proxy = remoteProxy; 5682 }, 5683 onDisconnect: function(elementName) { 5684 console.log("RpcClient: onDisconnect"); 5685 }, 5686 onFailed: function() { 5687 console.log("RpcClient: onFailed"); 5688 } 5689 }; 5690 let want = { 5691 "bundleName": "com.ohos.server", 5692 "abilityName": "com.ohos.server.EntryAbility", 5693 }; 5694 5695 // Use this method to connect to the ability for the FA model. 5696 // FA.connectAbility(want,connect); 5697 5698 globalThis.context.connectServiceExtensionAbility(want, connect); 5699 ``` 5700 5701 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **asObject()** of the proxy object is called to obtain the proxy or remote object. 5702 5703 ```ts 5704 class TestProxy { 5705 remote: rpc.RemoteObject; 5706 constructor(remote) { 5707 this.remote = remote; 5708 } 5709 asObject() { 5710 return this.remote; 5711 } 5712 } 5713 let iRemoteObject = new TestProxy(proxy).asObject(); 5714 ``` 5715 5716## DeathRecipient 5717 5718Subscribes to death notifications of a remote object. When the remote object is dead, the local end will receive a notification and **[onRemoteDied](#onremotedied)** will be called. A remote object is dead when the process holding the object is terminated or the device of the remote object is shut down or restarted. If the local and remote objects belong to different devices, the remote object is dead when the device holding the remote object is detached from the network. 5719 5720### onRemoteDied 5721 5722onRemoteDied(): void 5723 5724Called to perform subsequent operations when a death notification of the remote object is received. 5725 5726**System capability**: SystemCapability.Communication.IPC.Core 5727 5728**Example** 5729 5730 ```ts 5731 class MyDeathRecipient { 5732 onRemoteDied() { 5733 console.log("server died"); 5734 } 5735 } 5736 ``` 5737 5738## RequestResult<sup>9+</sup> 5739 5740Defines the response to the request. 5741 5742**System capability**: SystemCapability.Communication.IPC.Core 5743 5744 | Name | Type | Readable| Writable| Description | 5745 | ------- | --------------- | ---- | ---- |-------------------------------------- | 5746 | errCode | number | Yes | No | Error code. | 5747 | code | number | Yes | No | Message code. | 5748 | data | MessageSequence | Yes | No | **MessageSequence** object sent to the remote process.| 5749 | reply | MessageSequence | Yes | No | **MessageSequence** object returned by the remote process. | 5750 5751## SendRequestResult<sup>8+(deprecated)</sup> 5752 5753>This API is no longer maintained since API version 9. You are advised to use [RequestResult](#requestresult9). 5754 5755Defines the response to the request. 5756 5757**System capability**: SystemCapability.Communication.IPC.Core 5758 5759 | Name | Type | Readable| Writable| Description | 5760 | ------- | ------------- | ---- | ---- | ----------------------------------- | 5761 | errCode | number | Yes | No | Error code. | 5762 | code | number | Yes | No | Message code. | 5763 | data | MessageParcel | Yes | No | **MessageParcel** object sent to the remote process.| 5764 | reply | MessageParcel | Yes | No | **MessageParcel** object returned by the remote process. | 5765 5766## IRemoteObject 5767 5768Provides methods to query of obtain interface descriptors, add or delete death notifications, dump object status to specific files, and send messages. 5769 5770### getLocalInterface<sup>9+</sup> 5771 5772getLocalInterface(descriptor: string): IRemoteBroker 5773 5774Obtains the interface descriptor. 5775 5776**System capability**: SystemCapability.Communication.IPC.Core 5777 5778**Parameters** 5779 5780 | Name | Type | Mandatory| Description | 5781 | ---------- | ------ | ---- | -------------------- | 5782 | descriptor | string | Yes | Interface descriptor.| 5783 5784**Return value** 5785 5786 | Type | Description | 5787 | ------------- | --------------------------------------------- | 5788 | IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.| 5789 5790### queryLocalInterface<sup>(deprecated)</sup> 5791 5792>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9). 5793 5794queryLocalInterface(descriptor: string): IRemoteBroker 5795 5796Queries the interface descriptor. 5797 5798**System capability**: SystemCapability.Communication.IPC.Core 5799 5800**Parameters** 5801 5802 | Name | Type | Mandatory| Description | 5803 | ---------- | ------ | ---- | -------------------- | 5804 | descriptor | string | Yes | Interface descriptor.| 5805 5806**Return value** 5807 5808 | Type | Description | 5809 | ------------- | --------------------------------------------- | 5810 | IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.| 5811 5812### sendRequest<sup>(deprecated)</sup> 5813 5814>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 5815 5816sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 5817 5818Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message does not contain any content. If synchronous mode is set in **options** , a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 5819 5820**System capability**: SystemCapability.Communication.IPC.Core 5821 5822**Parameters** 5823 5824 | Name | Type | Mandatory| Description | 5825 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 5826 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 5827 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 5828 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 5829 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 5830 5831**Return value** 5832 5833 | Type | Description | 5834 | ------- | -------------------------------- | 5835 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 5836 5837 5838### sendRequest<sup>8+(deprecated)</sup> 5839 5840>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 5841 5842sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 5843 5844Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 5845 5846**System capability**: SystemCapability.Communication.IPC.Core 5847 5848**Parameters** 5849 5850 | Name | Type | Mandatory| Description | 5851 | ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 5852 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 5853 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 5854 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 5855 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 5856 5857**Return value** 5858 5859 | Type | Description | 5860 | -------------------------------- | --------------------------------------------- | 5861 | Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 5862 5863 5864### sendMessageRequest<sup>9+</sup> 5865 5866sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 5867 5868Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendMessageRequest** is returned, and the reply message contains the returned information. 5869 5870**System capability**: SystemCapability.Communication.IPC.Core 5871 5872**Parameters** 5873 5874 | Name | Type | Mandatory| Description | 5875 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 5876 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 5877 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 5878 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 5879 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 5880 5881**Return value** 5882 5883 | Type | Description | 5884 | ---------------------------- | ----------------------------------------- | 5885 | Promise<RequestResult> | Promise used to return the **requestResult** object.| 5886 5887 5888### sendMessageRequest<sup>9+</sup> 5889 5890sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 5891 5892Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information. 5893 5894**System capability**: SystemCapability.Communication.IPC.Core 5895 5896**Parameters** 5897 5898 | Name | Type | Mandatory| Description | 5899 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 5900 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 5901 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 5902 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 5903 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 5904 | callback | AsyncCallback<RequestResult> | Yes | Callback for receiving the sending result. | 5905 5906### sendRequest<sup>8+(deprecated)</sup> 5907 5908>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 5909 5910sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 5911 5912Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information. 5913 5914**System capability**: SystemCapability.Communication.IPC.Core 5915 5916**Parameters** 5917 5918 | Name | Type | Mandatory| Description | 5919 | -------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 5920 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 5921 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 5922 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 5923 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 5924 | callback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. | 5925 5926### registerDeathRecipient<sup>9+</sup> 5927 5928registerDeathRecipient(recipient: DeathRecipient, flags: number): void 5929 5930Registers a callback for receiving death notifications of the remote object. The callback will be called if the remote object process matching the **RemoteProxy** object is killed. 5931 5932**System capability**: SystemCapability.Communication.IPC.Core 5933 5934**Parameters** 5935 5936 | Name | Type | Mandatory| Description | 5937 | --------- | --------------------------------- | ---- | -------------- | 5938 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 5939 | flags | number | Yes | Flag of the death notification.| 5940 5941**Error codes** 5942 5943For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 5944 5945 | ID| Error Message| 5946 | -------- | -------- | 5947 | 1900008 | proxy or remote object is invalid | 5948 5949### addDeathrecipient<sup>(deprecated)</sup> 5950 5951>This API is no longer maintained since API version 9. You are advised to use [registerDeathRecipient](#registerdeathrecipient9). 5952 5953addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 5954 5955Adds a callback for receiving death notifications of the remote object. This method is called if the remote object process matching the **RemoteProxy** object is killed. 5956 5957**System capability**: SystemCapability.Communication.IPC.Core 5958 5959**Parameters** 5960 5961 | Name | Type | Mandatory| Description | 5962 | --------- | --------------------------------- | ---- | -------------- | 5963 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add.| 5964 | flags | number | Yes | Flag of the death notification.| 5965 5966**Return value** 5967 5968 | Type | Description | 5969 | ------- | ---------------------------------------- | 5970 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 5971 5972 5973### unregisterDeathRecipient<sup>9+</sup> 5974 5975unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 5976 5977Unregisters the callback used to receive death notifications of the remote object. 5978 5979**System capability**: SystemCapability.Communication.IPC.Core 5980 5981**Parameters** 5982 5983 | Name | Type | Mandatory| Description | 5984 | --------- | --------------------------------- | ---- | -------------- | 5985 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 5986 | flags | number | Yes | Flag of the death notification.| 5987 5988**Error codes** 5989 5990For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 5991 5992 | ID| Error Message| 5993 | -------- | -------- | 5994 | 1900008 | proxy or remote object is invalid | 5995 5996### removeDeathRecipient<sup>(deprecated)</sup> 5997 5998>This API is no longer maintained since API version 9. You are advised to use [unregisterDeathRecipient](#unregisterdeathrecipient9). 5999 6000removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6001 6002Removes the callback used to receive death notifications of the remote object. 6003 6004**System capability**: SystemCapability.Communication.IPC.Core 6005 6006**Parameters** 6007 6008 | Name | Type | Mandatory| Description | 6009 | --------- | --------------------------------- | ---- | -------------- | 6010 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove.| 6011 | flags | number | Yes | Flag of the death notification.| 6012 6013**Return value** 6014 6015 | Type | Description | 6016 | ------- | -----------------------------------------| 6017 | boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 6018 6019### getDescriptor<sup>9+</sup> 6020 6021getDescriptor(): string 6022 6023Obtains the interface descriptor (which is a string) of this object. 6024 6025**System capability**: SystemCapability.Communication.IPC.Core 6026 6027**Return value** 6028 6029 | Type | Description | 6030 | ------ | ---------------- | 6031 | string | Interface descriptor obtained.| 6032 6033**Error codes** 6034 6035For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6036 6037 | ID| Error Message| 6038 | -------- | -------- | 6039 | 1900008 | proxy or remote object is invalid | 6040 6041 6042### getInterfaceDescriptor<sup>(deprecated)</sup> 6043 6044>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9). 6045 6046getInterfaceDescriptor(): string 6047 6048Obtains the interface descriptor (which is a string) of this object. 6049 6050**System capability**: SystemCapability.Communication.IPC.Core 6051 6052**Return value** 6053 6054 | Type | Description | 6055 | ------ | ---------------- | 6056 | string | Interface descriptor obtained.| 6057 6058 6059### isObjectDead 6060 6061isObjectDead(): boolean 6062 6063Checks whether this object is dead. 6064 6065**System capability**: SystemCapability.Communication.IPC.Core 6066 6067**Return value** 6068 6069 | Type | Description | 6070 | ------- | ---------------------------------- | 6071 | boolean | Returns **true** if the object is dead; returns **false** otherwise.| 6072 6073 6074## RemoteProxy 6075 6076Provides APIs to implement **IRemoteObject**. 6077 6078**System capability**: SystemCapability.Communication.IPC.Core 6079 6080| Name | Value | Description | 6081| --------------------- | ----------------------- | --------------------------------- | 6082| PING_TRANSACTION | 1599098439 (0x5f504e47) | Internal instruction code used to test whether the IPC service is normal.| 6083| DUMP_TRANSACTION | 1598311760 (0x5f444d50) | Internal instruction code used to obtain the internal status of the binder. | 6084| INTERFACE_TRANSACTION | 1598968902 (0x5f4e5446) | Internal instruction code used to obtain the remote interface token. | 6085| MIN_TRANSACTION_ID | 1 (0x00000001) | Minimum valid instruction code. | 6086| MAX_TRANSACTION_ID | 16777215 (0x00FFFFFF) | Maximum valid instruction code. | 6087 6088### sendRequest<sup>(deprecated)</sup> 6089 6090>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6091 6092sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6093 6094Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 6095 6096**System capability**: SystemCapability.Communication.IPC.Core 6097 6098**Parameters** 6099 6100 | Name | Type | Mandatory| Description | 6101 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6102 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6103 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6104 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6105 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6106 6107**Return value** 6108 6109 | Type | Description | 6110 | ------- | ---------------------------------| 6111 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 6112 6113**Example** 6114 6115 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6116 6117 ```ts 6118 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6119 // import FA from "@ohos.ability.featureAbility"; 6120 6121 let proxy; 6122 let connect = { 6123 onConnect: function(elementName, remoteProxy) { 6124 console.log("RpcClient: js onConnect called."); 6125 proxy = remoteProxy; 6126 }, 6127 onDisconnect: function(elementName) { 6128 console.log("RpcClient: onDisconnect"); 6129 }, 6130 onFailed: function() { 6131 console.log("RpcClient: onFailed"); 6132 } 6133 }; 6134 let want = { 6135 "bundleName": "com.ohos.server", 6136 "abilityName": "com.ohos.server.EntryAbility", 6137 }; 6138 6139 // Use this method to connect to the ability for the FA model. 6140 // FA.connectAbility(want,connect); 6141 6142 globalThis.context.connectServiceExtensionAbility(want, connect); 6143 ``` 6144 6145 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 6146 6147 ```ts 6148 let option = new rpc.MessageOption(); 6149 let data = rpc.MessageParcel.create(); 6150 let reply = rpc.MessageParcel.create(); 6151 data.writeInt(1); 6152 data.writeString("hello"); 6153 let ret: boolean = proxy.sendRequest(1, data, reply, option); 6154 if (ret) { 6155 console.log("sendRequest got result"); 6156 let msg = reply.readString(); 6157 console.log("RPCTest: reply msg: " + msg); 6158 } else { 6159 console.log("RPCTest: sendRequest failed"); 6160 } 6161 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6162 data.reclaim(); 6163 reply.reclaim(); 6164 ``` 6165 6166### sendMessageRequest<sup>9+</sup> 6167 6168sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6169 6170Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendMessageRequest** is returned, and the reply message contains the returned information. 6171 6172**System capability**: SystemCapability.Communication.IPC.Core 6173 6174**Parameters** 6175 6176 | Name | Type | Mandatory| Description | 6177 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6178 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6179 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6180 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6181 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6182 6183**Return value** 6184 6185 | Type | Description | 6186 | ---------------------------- | ----------------------------------------- | 6187 | Promise<RequestResult> | Promise used to return the **requestResult** object.| 6188 6189**Example** 6190 6191 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6192 6193 ```ts 6194 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6195 // import FA from "@ohos.ability.featureAbility"; 6196 6197 let proxy; 6198 let connect = { 6199 onConnect: function(elementName, remoteProxy) { 6200 console.log("RpcClient: js onConnect called."); 6201 proxy = remoteProxy; 6202 }, 6203 onDisconnect: function(elementName) { 6204 console.log("RpcClient: onDisconnect"); 6205 }, 6206 onFailed: function() { 6207 console.log("RpcClient: onFailed"); 6208 } 6209 }; 6210 let want = { 6211 "bundleName": "com.ohos.server", 6212 "abilityName": "com.ohos.server.EntryAbility", 6213 }; 6214 6215 // Use this method to connect to the ability for the FA model. 6216 // FA.connectAbility(want,connect); 6217 6218 globalThis.context.connectServiceExtensionAbility(want, connect); 6219 ``` 6220 6221 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 6222 6223 ```ts 6224 let option = new rpc.MessageOption(); 6225 let data = rpc.MessageSequence.create(); 6226 let reply = rpc.MessageSequence.create(); 6227 data.writeInt(1); 6228 data.writeString("hello"); 6229 proxy.sendMessageRequest(1, data, reply, option) 6230 .then(function(result) { 6231 if (result.errCode === 0) { 6232 console.log("sendMessageRequest got result"); 6233 result.reply.readException(); 6234 let msg = result.reply.readString(); 6235 console.log("RPCTest: reply msg: " + msg); 6236 } else { 6237 console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode); 6238 } 6239 }).catch(function(e) { 6240 console.log("RPCTest: sendMessageRequest got exception: " + e.message); 6241 }).finally (() => { 6242 console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); 6243 data.reclaim(); 6244 reply.reclaim(); 6245 }); 6246 ``` 6247 6248### sendRequest<sup>8+(deprecated)</sup> 6249 6250>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6251 6252sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6253 6254Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 6255 6256**System capability**: SystemCapability.Communication.IPC.Core 6257 6258**Parameters** 6259 6260 | Name | Type | Mandatory| Description | 6261 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6262 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6263 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6264 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6265 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6266 6267**Return value** 6268 6269 | Type | Description | 6270 | -------------------------------- | --------------------------------------------- | 6271 | Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 6272 6273**Example** 6274 6275 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6276 6277 ```ts 6278 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6279 // import FA from "@ohos.ability.featureAbility"; 6280 6281 let proxy; 6282 let connect = { 6283 onConnect: function(elementName, remoteProxy) { 6284 console.log("RpcClient: js onConnect called."); 6285 proxy = remoteProxy; 6286 }, 6287 onDisconnect: function(elementName) { 6288 console.log("RpcClient: onDisconnect"); 6289 }, 6290 onFailed: function() { 6291 console.log("RpcClient: onFailed"); 6292 } 6293 }; 6294 let want = { 6295 "bundleName": "com.ohos.server", 6296 "abilityName": "com.ohos.server.EntryAbility", 6297 }; 6298 6299 // Use this method to connect to the ability for the FA model. 6300 // FA.connectAbility(want,connect); 6301 6302 globalThis.context.connectServiceExtensionAbility(want, connect); 6303 ``` 6304 6305 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 6306 6307 ```ts 6308 let option = new rpc.MessageOption(); 6309 let data = rpc.MessageParcel.create(); 6310 let reply = rpc.MessageParcel.create(); 6311 data.writeInt(1); 6312 data.writeString("hello"); 6313 proxy.sendRequest(1, data, reply, option) 6314 .then(function(result) { 6315 if (result.errCode === 0) { 6316 console.log("sendRequest got result"); 6317 result.reply.readException(); 6318 let msg = result.reply.readString(); 6319 console.log("RPCTest: reply msg: " + msg); 6320 } else { 6321 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 6322 } 6323 }).catch(function(e) { 6324 console.log("RPCTest: sendRequest got exception: " + e.message); 6325 }).finally (() => { 6326 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6327 data.reclaim(); 6328 reply.reclaim(); 6329 }); 6330 ``` 6331 6332### sendMessageRequest<sup>9+</sup> 6333 6334sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6335 6336Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked at certain time after the response to **sendMessageRequest** is returned, and the reply contains the returned information. 6337 6338**System capability**: SystemCapability.Communication.IPC.Core 6339 6340**Parameters** 6341 6342 | Name | Type | Mandatory| Description | 6343 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6344 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6345 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6346 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6347 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6348 | callback | AsyncCallback<RequestResult> | Yes | Callback for receiving the sending result. | 6349 6350**Example** 6351 6352 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6353 6354 ```ts 6355 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6356 // import FA from "@ohos.ability.featureAbility"; 6357 6358 let proxy; 6359 let connect = { 6360 onConnect: function(elementName, remoteProxy) { 6361 console.log("RpcClient: js onConnect called."); 6362 proxy = remoteProxy; 6363 }, 6364 onDisconnect: function(elementName) { 6365 console.log("RpcClient: onDisconnect"); 6366 }, 6367 onFailed: function() { 6368 console.log("RpcClient: onFailed"); 6369 } 6370 }; 6371 let want = { 6372 "bundleName": "com.ohos.server", 6373 "abilityName": "com.ohos.server.EntryAbility", 6374 }; 6375 function sendRequestCallback(result) { 6376 if (result.errCode === 0) { 6377 console.log("sendRequest got result"); 6378 result.reply.readException(); 6379 let msg = result.reply.readString(); 6380 console.log("RPCTest: reply msg: " + msg); 6381 } else { 6382 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 6383 } 6384 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6385 result.data.reclaim(); 6386 result.reply.reclaim(); 6387 } 6388 6389 // Use this method to connect to the ability for the FA model. 6390 // FA.connectAbility(want,connect); 6391 6392 globalThis.context.connectServiceExtensionAbility(want, connect); 6393 ``` 6394 6395 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 6396 6397 ```ts 6398 let option = new rpc.MessageOption(); 6399 let data = rpc.MessageSequence.create(); 6400 let reply = rpc.MessageSequence.create(); 6401 data.writeInt(1); 6402 data.writeString("hello"); 6403 try { 6404 proxy.sendMessageRequest(1, data, reply, option, sendRequestCallback); 6405 } catch(error) { 6406 console.info("rpc send sequence request fail, errorCode " + error.code); 6407 console.info("rpc send sequence request fail, errorMessage " + error.message); 6408 } 6409 ``` 6410 6411### sendRequest<sup>8+(deprecated)</sup> 6412 6413>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6414 6415sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6416 6417Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information. 6418 6419**System capability**: SystemCapability.Communication.IPC.Core 6420 6421**Parameters** 6422 6423 | Name | Type | Mandatory| Description | 6424 | -------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6425 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6426 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6427 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6428 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6429 | callback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. | 6430 6431**Example** 6432 6433 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6434 6435 ```ts 6436 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6437 // import FA from "@ohos.ability.featureAbility"; 6438 6439 let proxy; 6440 let connect = { 6441 onConnect: function(elementName, remoteProxy) { 6442 console.log("RpcClient: js onConnect called."); 6443 proxy = remoteProxy; 6444 }, 6445 onDisconnect: function(elementName) { 6446 console.log("RpcClient: onDisconnect"); 6447 }, 6448 onFailed: function() { 6449 console.log("RpcClient: onFailed"); 6450 } 6451 }; 6452 let want = { 6453 "bundleName": "com.ohos.server", 6454 "abilityName": "com.ohos.server.EntryAbility", 6455 }; 6456 function sendRequestCallback(result) { 6457 if (result.errCode === 0) { 6458 console.log("sendRequest got result"); 6459 result.reply.readException(); 6460 let msg = result.reply.readString(); 6461 console.log("RPCTest: reply msg: " + msg); 6462 } else { 6463 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 6464 } 6465 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6466 result.data.reclaim(); 6467 result.reply.reclaim(); 6468 } 6469 6470 // Use this method to connect to the ability for the FA model. 6471 // FA.connectAbility(want,connect); 6472 6473 globalThis.context.connectServiceExtensionAbility(want, connect); 6474 ``` 6475 6476 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 6477 6478 ```ts 6479 let option = new rpc.MessageOption(); 6480 let data = rpc.MessageParcel.create(); 6481 let reply = rpc.MessageParcel.create(); 6482 data.writeInt(1); 6483 data.writeString("hello"); 6484 proxy.sendRequest(1, data, reply, option, sendRequestCallback); 6485 ``` 6486 6487### getLocalInterface<sup>9+</sup> 6488 6489getLocalInterface(interface: string): IRemoteBroker 6490 6491Obtains the **LocalInterface** object of an interface token. 6492 6493**System capability**: SystemCapability.Communication.IPC.Core 6494 6495**Parameters** 6496 6497 | Name | Type | Mandatory| Description | 6498 | --------- | ------ | ---- | ---------------------- | 6499 | interface | string | Yes | Interface descriptor.| 6500 6501**Return value** 6502 6503 | Type | Description | 6504 | ------------- | ------------------------------------------ | 6505 | IRemoteBroker | Returns **Null** by default, which indicates a proxy interface.| 6506 6507**Error codes** 6508 6509For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6510 6511 | ID| Error Message| 6512 | -------- | -------- | 6513 | 1900006 | only remote object permitted | 6514 6515**Example** 6516 6517 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6518 6519 ```ts 6520 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6521 // import FA from "@ohos.ability.featureAbility"; 6522 6523 let proxy; 6524 let connect = { 6525 onConnect: function(elementName, remoteProxy) { 6526 console.log("RpcClient: js onConnect called."); 6527 proxy = remoteProxy; 6528 }, 6529 onDisconnect: function(elementName) { 6530 console.log("RpcClient: onDisconnect"); 6531 }, 6532 onFailed: function() { 6533 console.log("RpcClient: onFailed"); 6534 } 6535 }; 6536 let want = { 6537 "bundleName": "com.ohos.server", 6538 "abilityName": "com.ohos.server.EntryAbility", 6539 }; 6540 6541 // Use this method to connect to the ability for the FA model. 6542 // FA.connectAbility(want,connect); 6543 6544 globalThis.context.connectServiceExtensionAbility(want, connect); 6545 ``` 6546 6547 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **getLocalInterface()** of the proxy object is called to obtain the interface descriptor. 6548 6549 ```ts 6550 try { 6551 let broker = proxy.getLocalInterface("testObject"); 6552 console.log("RpcClient: getLocalInterface is " + broker); 6553 } catch(error) { 6554 console.info("rpc get local interface fail, errorCode " + error.code); 6555 console.info("rpc get local interface fail, errorMessage " + error.message); 6556 } 6557 ``` 6558 6559### queryLocalInterface<sup>(deprecated)</sup> 6560 6561>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9). 6562 6563queryLocalInterface(interface: string): IRemoteBroker 6564 6565Obtains the **LocalInterface** object of an interface token. 6566 6567**System capability**: SystemCapability.Communication.IPC.Core 6568 6569**Parameters** 6570 6571 | Name | Type | Mandatory| Description | 6572 | --------- | ------ | ---- | ---------------------- | 6573 | interface | string | Yes | Interface descriptor.| 6574 6575**Return value** 6576 6577 | Type | Description | 6578 | ------------- | ------------------------------------------ | 6579 | IRemoteBroker | Returns **Null** by default, which indicates a proxy interface.| 6580 6581**Example** 6582 6583 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6584 6585 ```ts 6586 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6587 // import FA from "@ohos.ability.featureAbility"; 6588 6589 let proxy; 6590 let connect = { 6591 onConnect: function(elementName, remoteProxy) { 6592 console.log("RpcClient: js onConnect called."); 6593 proxy = remoteProxy; 6594 }, 6595 onDisconnect: function(elementName) { 6596 console.log("RpcClient: onDisconnect"); 6597 }, 6598 onFailed: function() { 6599 console.log("RpcClient: onFailed"); 6600 } 6601 }; 6602 let want = { 6603 "bundleName": "com.ohos.server", 6604 "abilityName": "com.ohos.server.EntryAbility", 6605 }; 6606 6607 // Use this method to connect to the ability for the FA model. 6608 // FA.connectAbility(want,connect); 6609 6610 globalThis.context.connectServiceExtensionAbility(want, connect); 6611 ``` 6612 6613 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **queryLocalInterface()** of the proxy object is called to obtain the interface descriptor. 6614 6615 ```ts 6616 let broker = proxy.queryLocalInterface("testObject"); 6617 console.log("RpcClient: queryLocalInterface is " + broker); 6618 ``` 6619 6620### registerDeathRecipient<sup>9+</sup> 6621 6622registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6623 6624Registers a callback for receiving death notifications of the remote object. The callback will be invoked when the remote object process matching the **RemoteProxy** object is killed. 6625 6626**System capability**: SystemCapability.Communication.IPC.Core 6627 6628**Parameters** 6629 6630 | Name | Type | Mandatory| Description | 6631 | --------- | --------------------------------- | ---- | -------------- | 6632 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 6633 | flags | number | Yes | Flag of the death notification.| 6634 6635**Error codes** 6636 6637For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6638 6639 | ID| Error Message| 6640 | -------- | -------- | 6641 | 1900008 | proxy or remote object is invalid | 6642 6643**Example** 6644 6645 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6646 6647 ```ts 6648 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6649 // import FA from "@ohos.ability.featureAbility"; 6650 6651 let proxy; 6652 let connect = { 6653 onConnect: function(elementName, remoteProxy) { 6654 console.log("RpcClient: js onConnect called."); 6655 proxy = remoteProxy; 6656 }, 6657 onDisconnect: function(elementName) { 6658 console.log("RpcClient: onDisconnect"); 6659 }, 6660 onFailed: function() { 6661 console.log("RpcClient: onFailed"); 6662 } 6663 }; 6664 let want = { 6665 "bundleName": "com.ohos.server", 6666 "abilityName": "com.ohos.server.EntryAbility", 6667 }; 6668 6669 // Use this method to connect to the ability for the FA model. 6670 // FA.connectAbility(want,connect); 6671 6672 globalThis.context.connectServiceExtensionAbility(want, connect); 6673 ``` 6674 6675 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **registerDeathRecipient()** of the proxy object is called to register a callback for receiving the death notification of the remote object. 6676 6677 ```ts 6678 class MyDeathRecipient { 6679 onRemoteDied() { 6680 console.log("server died"); 6681 } 6682 } 6683 let deathRecipient = new MyDeathRecipient(); 6684 try { 6685 proxy.registerDeathRecipient(deathRecipient, 0); 6686 } catch(error) { 6687 console.info("proxy register deathRecipient fail, errorCode " + error.code); 6688 console.info("proxy register deathRecipient fail, errorMessage " + error.message); 6689 } 6690 ``` 6691 6692### addDeathRecippient<sup>(deprecated)</sup> 6693 6694>This API is no longer maintained since API version 9. You are advised to use [registerDeathRecipient](#registerdeathrecipient9). 6695 6696addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6697 6698Adds a callback for receiving the death notifications of the remote object, including the death notifications of the remote proxy. 6699 6700**System capability**: SystemCapability.Communication.IPC.Core 6701 6702**Parameters** 6703 6704 | Name | Type | Mandatory| Description | 6705 | --------- | --------------------------------- | ---- | --------------------------------- | 6706 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add. | 6707 | flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 6708 6709**Return value** 6710 6711 | Type | Description | 6712 | ------- | ---------------------------------------- | 6713 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 6714 6715**Example** 6716 6717 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6718 6719 ```ts 6720 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6721 // import FA from "@ohos.ability.featureAbility"; 6722 6723 let proxy; 6724 let connect = { 6725 onConnect: function(elementName, remoteProxy) { 6726 console.log("RpcClient: js onConnect called."); 6727 proxy = remoteProxy; 6728 }, 6729 onDisconnect: function(elementName) { 6730 console.log("RpcClient: onDisconnect"); 6731 }, 6732 onFailed: function() { 6733 console.log("RpcClient: onFailed"); 6734 } 6735 }; 6736 let want = { 6737 "bundleName": "com.ohos.server", 6738 "abilityName": "com.ohos.server.EntryAbility", 6739 }; 6740 6741 // Use this method to connect to the ability for the FA model. 6742 // FA.connectAbility(want,connect); 6743 6744 globalThis.context.connectServiceExtensionAbility(want, connect); 6745 ``` 6746 6747 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **addDeathRecippient()** of the proxy object is called to add a callback for receiving the death notification of the remove object. 6748 6749 ```ts 6750 class MyDeathRecipient { 6751 onRemoteDied() { 6752 console.log("server died"); 6753 } 6754 } 6755 let deathRecipient = new MyDeathRecipient(); 6756 proxy.addDeathRecipient(deathRecipient, 0); 6757 ``` 6758 6759### unregisterDeathRecipient<sup>9+</sup> 6760 6761unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 6762 6763Unregisters the callback used to receive death notifications of the remote object. 6764 6765**System capability**: SystemCapability.Communication.IPC.Core 6766 6767**Parameters** 6768 6769 | Name | Type | Mandatory| Description | 6770 | --------- | --------------------------------- | ---- | -------------- | 6771 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 6772 | flags | number | Yes | Flag of the death notification.| 6773 6774**Error codes** 6775 6776For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6777 6778 | ID| Error Message| 6779 | -------- | -------- | 6780 | 1900008 | proxy or remote object is invalid | 6781 6782**Example** 6783 6784 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6785 6786 ```ts 6787 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6788 // import FA from "@ohos.ability.featureAbility"; 6789 6790 let proxy; 6791 let connect = { 6792 onConnect: function(elementName, remoteProxy) { 6793 console.log("RpcClient: js onConnect called."); 6794 proxy = remoteProxy; 6795 }, 6796 onDisconnect: function(elementName) { 6797 console.log("RpcClient: onDisconnect"); 6798 }, 6799 onFailed: function() { 6800 console.log("RpcClient: onFailed"); 6801 } 6802 }; 6803 let want = { 6804 "bundleName": "com.ohos.server", 6805 "abilityName": "com.ohos.server.EntryAbility", 6806 }; 6807 6808 // Use this method to connect to the ability for the FA model. 6809 // FA.connectAbility(want,connect); 6810 6811 globalThis.context.connectServiceExtensionAbility(want, connect); 6812 ``` 6813 6814 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **unregisterDeathRecipient()** of the proxy object is called to unregister the callback for receiving the death notification of the remote object. 6815 6816 ```ts 6817 class MyDeathRecipient { 6818 onRemoteDied() { 6819 console.log("server died"); 6820 } 6821 } 6822 let deathRecipient = new MyDeathRecipient(); 6823 try { 6824 proxy.registerDeathRecippient(deathRecipient, 0); 6825 proxy.unregisterDeathRecippient(deathRecipient, 0); 6826 } catch(error) { 6827 console.info("proxy register deathRecipient fail, errorCode " + error.code); 6828 console.info("proxy register deathRecipient fail, errorMessage " + error.message); 6829 } 6830 ``` 6831 6832### removeDeathRecipient<sup>(deprecated)</sup> 6833 6834>This API is no longer maintained since API version 9. You are advised to use [unregisterDeathRecipient](#unregisterdeathrecipient9). 6835 6836removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6837 6838Removes the callback used to receive death notifications of the remote object. 6839 6840**System capability**: SystemCapability.Communication.IPC.Core 6841 6842**Parameters** 6843 6844 | Name | Type | Mandatory| Description | 6845 | --------- | --------------------------------- | ---- | --------------------------------- | 6846 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove. | 6847 | flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 6848 6849**Return value** 6850 6851 | Type | Description | 6852 | ------- | ---------------------------------------- | 6853 | boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 6854 6855**Example** 6856 6857 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6858 6859 ```ts 6860 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6861 // import FA from "@ohos.ability.featureAbility"; 6862 6863 let proxy; 6864 let connect = { 6865 onConnect: function(elementName, remoteProxy) { 6866 console.log("RpcClient: js onConnect called."); 6867 proxy = remoteProxy; 6868 }, 6869 onDisconnect: function(elementName) { 6870 console.log("RpcClient: onDisconnect"); 6871 }, 6872 onFailed: function() { 6873 console.log("RpcClient: onFailed"); 6874 } 6875 }; 6876 let want = { 6877 "bundleName": "com.ohos.server", 6878 "abilityName": "com.ohos.server.EntryAbility", 6879 }; 6880 6881 // Use this method to connect to the ability for the FA model. 6882 // FA.connectAbility(want,connect); 6883 6884 globalThis.context.connectServiceExtensionAbility(want, connect); 6885 ``` 6886 6887 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **removeDeathRecipient()** of the proxy object is called to remove the callback used to receive the death notification of the remote object. 6888 6889 ```ts 6890 class MyDeathRecipient { 6891 onRemoteDied() { 6892 console.log("server died"); 6893 } 6894 } 6895 let deathRecipient = new MyDeathRecipient(); 6896 proxy.addDeathRecippient(deathRecipient, 0); 6897 proxy.removeDeathRecipient(deathRecipient, 0); 6898 ``` 6899 6900### getDescriptor<sup>9+</sup> 6901 6902getDescriptor(): string 6903 6904Obtains the interface descriptor (which is a string) of this proxy object. 6905 6906**System capability**: SystemCapability.Communication.IPC.Core 6907 6908**Return value** 6909 6910 | Type | Description | 6911 | ------ | ---------------- | 6912 | string | Interface descriptor obtained.| 6913 6914**Error codes** 6915 6916For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6917 6918 | ID| Error Message| 6919 | -------- | -------- | 6920 | 1900008 | proxy or remote object is invalid | 6921 | 1900007 | communication failed | 6922 6923**Example** 6924 6925 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6926 6927 ```ts 6928 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6929 // import FA from "@ohos.ability.featureAbility"; 6930 6931 let proxy; 6932 let connect = { 6933 onConnect: function(elementName, remoteProxy) { 6934 console.log("RpcClient: js onConnect called."); 6935 proxy = remoteProxy; 6936 }, 6937 onDisconnect: function(elementName) { 6938 console.log("RpcClient: onDisconnect"); 6939 }, 6940 onFailed: function() { 6941 console.log("RpcClient: onFailed"); 6942 } 6943 }; 6944 let want = { 6945 "bundleName": "com.ohos.server", 6946 "abilityName": "com.ohos.server.EntryAbility", 6947 }; 6948 6949 // Use this method to connect to the ability for the FA model. 6950 // FA.connectAbility(want,connect); 6951 6952 globalThis.context.connectServiceExtensionAbility(want, connect); 6953 ``` 6954 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **getDescriptor()** of the proxy object is called to obtain the interface descriptor of the object. 6955 6956 ```ts 6957 try { 6958 let descriptor = proxy.getDescriptor(); 6959 console.log("RpcClient: descriptor is " + descriptor); 6960 } catch(error) { 6961 console.info("rpc get interface descriptor fail, errorCode " + error.code); 6962 console.info("rpc get interface descriptor fail, errorMessage " + error.message); 6963 } 6964 ``` 6965 6966### getInterfaceDescriptor<sup>(deprecated)</sup> 6967 6968>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9). 6969 6970getInterfaceDescriptor(): string 6971 6972Obtains the interface descriptor of this proxy object. 6973 6974**System capability**: SystemCapability.Communication.IPC.Core 6975 6976**Return value** 6977 6978 | Type | Description | 6979 | ------ | ------------------ | 6980 | string | Interface descriptor obtained.| 6981 6982**Example** 6983 6984 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6985 6986 ```ts 6987 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6988 // import FA from "@ohos.ability.featureAbility"; 6989 6990 let proxy; 6991 let connect = { 6992 onConnect: function(elementName, remoteProxy) { 6993 console.log("RpcClient: js onConnect called."); 6994 proxy = remoteProxy; 6995 }, 6996 onDisconnect: function(elementName) { 6997 console.log("RpcClient: onDisconnect"); 6998 }, 6999 onFailed: function() { 7000 console.log("RpcClient: onFailed"); 7001 } 7002 }; 7003 let want = { 7004 "bundleName": "com.ohos.server", 7005 "abilityName": "com.ohos.server.EntryAbility", 7006 }; 7007 7008 // Use this method to connect to the ability for the FA model. 7009 // FA.connectAbility(want,connect); 7010 7011 globalThis.context.connectServiceExtensionAbility(want, connect); 7012 ``` 7013 7014 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **getInterfaceDescriptor()** of the proxy object is called to obtain the interface descriptor of the current proxy object. 7015 7016 ```ts 7017 let descriptor = proxy.getInterfaceDescriptor(); 7018 console.log("RpcClient: descriptor is " + descriptor); 7019 ``` 7020 7021### isObjectDead 7022 7023isObjectDead(): boolean 7024 7025Checks whether the **RemoteObject** is dead. 7026 7027**System capability**: SystemCapability.Communication.IPC.Core 7028 7029**Return value** 7030 7031 | Type | Description | 7032 | ------- | ------------------------------------------------- | 7033 | boolean | Returns **true** if the **RemoteObject** is dead; returns **false** otherwise.| 7034 7035**Example** 7036 7037 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7038 7039 ```ts 7040 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7041 // import FA from "@ohos.ability.featureAbility"; 7042 7043 let proxy; 7044 let connect = { 7045 onConnect: function(elementName, remoteProxy) { 7046 console.log("RpcClient: js onConnect called."); 7047 proxy = remoteProxy; 7048 }, 7049 onDisconnect: function(elementName) { 7050 console.log("RpcClient: onDisconnect"); 7051 }, 7052 onFailed: function() { 7053 console.log("RpcClient: onFailed"); 7054 } 7055 }; 7056 let want = { 7057 "bundleName": "com.ohos.server", 7058 "abilityName": "com.ohos.server.EntryAbility", 7059 }; 7060 7061 // Use this method to connect to the ability for the FA model. 7062 // FA.connectAbility(want,connect); 7063 7064 globalThis.context.connectServiceExtensionAbility(want, connect); 7065 ``` 7066 7067 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **isObjectDead()** of the proxy object is called to check whether this object is dead. 7068 7069 ```ts 7070 let isDead = proxy.isObjectDead(); 7071 console.log("RpcClient: isObjectDead is " + isDead); 7072 ``` 7073 7074## MessageOption 7075 7076Provides common message options (flag and wait time). Use the specified flag to construct the **MessageOption** object. 7077 7078**System capability**: SystemCapability.Communication.IPC.Core 7079 7080 | Name | Value | Description | 7081 | ------------- | ---- | ----------------------------------------------------------- | 7082 | TF_SYNC | 0 | Synchronous call. | 7083 | TF_ASYNC | 1 | Asynchronous call. | 7084 | TF_ACCEPT_FDS | 0x10 | Indication to **sendMessageRequest<sup>9+</sup>** for returning the file descriptor.| 7085 | TF_WAIT_TIME | 8 | Default waiting time, in seconds. | 7086 7087 7088### constructor<sup>9+</sup> 7089 7090constructor(async?: boolean); 7091 7092A constructor used to create a **MessageOption** object. 7093 7094**System capability**: SystemCapability.Communication.IPC.Core 7095 7096**Parameters** 7097 7098 | Name | Type | Mandatory| Description | 7099 | --------- | ------ | ---- | -------------------------------------- | 7100 | syncFlags | number | No | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**.| 7101 7102 7103**Example** 7104 7105 ```ts 7106 class TestRemoteObject extends rpc.MessageOption { 7107 constructor(async) { 7108 super(async); 7109 } 7110 } 7111 ``` 7112 7113### constructor 7114 7115constructor(syncFlags?: number, waitTime?: number) 7116 7117A constructor used to create a **MessageOption** object. 7118 7119**System capability**: SystemCapability.Communication.IPC.Core 7120 7121**Parameters** 7122 7123 | Name | Type | Mandatory| Description | 7124 | --------- | ------ | ---- | --------------------------------------------- | 7125 | syncFlags | number | No | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**. | 7126 | waitTime | number | No | Maximum wait time for an RPC call. The default value is **TF_WAIT_TIME**.| 7127 7128**Example** 7129 7130 ```ts 7131 class TestRemoteObject extends rpc.MessageOption { 7132 constructor(syncFlags,waitTime) { 7133 super(syncFlags,waitTime); 7134 } 7135 } 7136 ``` 7137### isAsync<sup>9+</sup> 7138 7139isAsync(): boolean; 7140 7141Checks whether **SendMessageRequest** is called synchronously or asynchronously. 7142 7143**System capability**: SystemCapability.Communication.IPC.Core 7144 7145**Return value** 7146 7147 | Type | Description | 7148 | ------- | ---------------------------------------- | 7149 | boolean | Returns **true** if **SendMessageRequest** is called synchronously; returns **false** if **SendMessageRequest** is called asynchronously.| 7150 7151**Example** 7152 7153 ```ts 7154 let option = new rpc.MessageOption(); 7155 let isAsync = option.isAsync(); 7156 ``` 7157 7158### setAsync<sup>9+</sup> 7159 7160setAsync(async: boolean): void; 7161 7162Sets whether **SendMessageRequest** is called synchronously or asynchronously. 7163 7164**System capability**: SystemCapability.Communication.IPC.Core 7165 7166**Example** 7167 7168 ```ts 7169 let option = new rpc.MessageOption(); 7170 let setAsync = option.setAsync(true); 7171 console.log("Set synchronization flag"); 7172 ``` 7173 7174### getFlags 7175 7176getFlags(): number 7177 7178Obtains the call flag, which can be synchronous or asynchronous. 7179 7180**System capability**: SystemCapability.Communication.IPC.Core 7181 7182**Return value** 7183 7184 | Type | Description | 7185 | ------ | ------------------------------------ | 7186 | number | Call mode obtained.| 7187 7188**Example** 7189 7190 ```ts 7191 try { 7192 let option = new rpc.MessageOption(); 7193 console.info("create object successfully."); 7194 let flog = option.getFlags(); 7195 console.info("run getFlags success, flog is " + flog); 7196 option.setFlags(1) 7197 console.info("run setFlags success"); 7198 let flog2 = option.getFlags(); 7199 console.info("run getFlags success, flog2 is " + flog2); 7200 } catch (error) { 7201 console.info("error " + error); 7202 } 7203 ``` 7204 7205### setFlags 7206 7207setFlags(flags: number): void 7208 7209Sets the call flag, which can be synchronous or asynchronous. 7210 7211**System capability**: SystemCapability.Communication.IPC.Core 7212 7213**Parameters** 7214 7215 | Name| Type | Mandatory| Description | 7216 | ------ | ------ | ---- | ------------------------ | 7217 | flags | number | Yes | Call flag to set.| 7218 7219**Example** 7220 7221 ```ts 7222 try { 7223 let option = new rpc.MessageOption(); 7224 option.setFlags(1) 7225 console.info("run setFlags success"); 7226 let flog = option.getFlags(); 7227 console.info("run getFlags success, flog is " + flog); 7228 } catch (error) { 7229 console.info("error " + error); 7230 } 7231 ``` 7232 7233### getWaitTime 7234 7235getWaitTime(): number 7236 7237Obtains the maximum wait time for this RPC call. 7238 7239**System capability**: SystemCapability.Communication.IPC.Core 7240 7241**Return value** 7242 7243 | Type | Description | 7244 | ------ | ----------------- | 7245 | number | Maximum wait time obtained.| 7246 7247**Example** 7248 7249 ```ts 7250 try { 7251 let option = new rpc.MessageOption(); 7252 let time = option.getWaitTime(); 7253 console.info("run getWaitTime success"); 7254 option.setWaitTime(16); 7255 let time2 = option.getWaitTime(); 7256 console.info("run getWaitTime success, time is " + time); 7257 } catch (error) { 7258 console.info("error " + error); 7259 } 7260 ``` 7261 7262### setWaitTime 7263 7264setWaitTime(waitTime: number): void 7265 7266Sets the maximum wait time for this RPC call. 7267 7268**System capability**: SystemCapability.Communication.IPC.Core 7269 7270**Parameters** 7271 7272 | Name | Type | Mandatory| Description | 7273 | -------- | ------ | ---- | --------------------- | 7274 | waitTime | number | Yes | Maximum wait time to set.| 7275 7276**Example** 7277 7278 ```ts 7279 try { 7280 let option = new rpc.MessageOption(); 7281 option.setWaitTime(16); 7282 let time = option.getWaitTime(); 7283 console.info("run getWaitTime success, time is " + time); 7284 } catch (error) { 7285 console.info("error " + error); 7286 } 7287 ``` 7288 7289## IPCSkeleton 7290 7291Obtains IPC context information, including the UID and PID, local and remote device IDs, and whether the method is invoked on the same device. 7292 7293### getContextObject 7294 7295static getContextObject(): IRemoteObject 7296 7297Obtains the system capability manager. This API is a static method. 7298 7299**System capability**: SystemCapability.Communication.IPC.Core 7300 7301**Return value** 7302 7303 | Type | Description | 7304 | ------------------------------- | -------------------- | 7305 | [IRemoteObject](#iremoteobject) | System capability manager obtained.| 7306 7307**Example** 7308 7309 ```ts 7310 let samgr = rpc.IPCSkeleton.getContextObject(); 7311 console.log("RpcServer: getContextObject result: " + samgr); 7312 ``` 7313 7314### getCallingPid 7315 7316static getCallingPid(): number 7317 7318Obtains the PID of the caller. This API is a static method, which is invoked by the **RemoteObject** object in the **onRemoteRequest** method. If this method is not invoked in the IPC context (**onRemoteRequest**), the PID of the process will be returned. 7319 7320**System capability**: SystemCapability.Communication.IPC.Core 7321 7322**Return value** 7323 7324 | Type | Description | 7325 | ------ | ----------------- | 7326 | number | PID of the caller.| 7327 7328**Example** 7329 7330 ```ts 7331 class Stub extends rpc.RemoteObject { 7332 onRemoteMessageRequest(code, data, reply, option) { 7333 let callerPid = rpc.IPCSkeleton.getCallingPid(); 7334 console.log("RpcServer: getCallingPid result: " + callerPid); 7335 return true; 7336 } 7337 } 7338 ``` 7339 7340### getCallingUid 7341 7342static getCallingUid(): number 7343 7344Obtains the UID of the caller. This API is a static method, which is invoked by the **RemoteObject** object in the **onRemoteRequest** method. If this method is not invoked in the IPC context (**onRemoteRequest**), the UID of the process will be returned. 7345 7346**System capability**: SystemCapability.Communication.IPC.Core 7347 7348**Return value** 7349 7350 | Type | Description | 7351 | ------ | ----------------- | 7352 | number | UID of the caller.| 7353 7354**Example** 7355 7356 ```ts 7357 class Stub extends rpc.RemoteObject { 7358 onRemoteMessageRequest(code, data, reply, option) { 7359 let callerUid = rpc.IPCSkeleton.getCallingUid(); 7360 console.log("RpcServer: getCallingUid result: " + callerUid); 7361 return true; 7362 } 7363 } 7364 ``` 7365 7366### getCallingTokenId<sup>8+</sup> 7367 7368static getCallingTokenId(): number; 7369 7370Obtains the caller's token ID, which is used to verify the caller identity. 7371 7372**System capability**: SystemCapability.Communication.IPC.Core 7373 7374**Return value** 7375 7376 | Type | Description | 7377 | ------ | --------------------- | 7378 | number | Token ID of the caller obtained.| 7379 7380**Example** 7381 7382 ```ts 7383 class Stub extends rpc.RemoteObject { 7384 onRemoteMessageRequest(code, data, reply, option) { 7385 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 7386 console.log("RpcServer: getCallingTokenId result: " + callerTokenId); 7387 return true; 7388 } 7389 } 7390 ``` 7391 7392 7393### getCallingDeviceID 7394 7395static getCallingDeviceID(): string 7396 7397Obtains the ID of the device hosting the caller's process. This API is a static method. 7398 7399**System capability**: SystemCapability.Communication.IPC.Core 7400 7401**Return value** 7402 7403 | Type | Description | 7404 | ------ | ---------------------------- | 7405 | string | Device ID obtained.| 7406 7407**Example** 7408 7409 ```ts 7410 class Stub extends rpc.RemoteObject { 7411 onRemoteMessageRequest(code, data, reply, option) { 7412 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 7413 console.log("RpcServer: callerDeviceID is: " + callerDeviceID); 7414 return true; 7415 } 7416 } 7417 ``` 7418 7419### getLocalDeviceID 7420 7421static getLocalDeviceID(): string 7422 7423Obtains the local device ID. This API is a static method. 7424 7425**System capability**: SystemCapability.Communication.IPC.Core 7426 7427**Return value** 7428 7429 | Type | Description | 7430 | ------ | ------------------ | 7431 | string | Local device ID obtained.| 7432 7433**Example** 7434 7435 ```ts 7436 class Stub extends rpc.RemoteObject { 7437 onRemoteMessageRequest(code, data, reply, option) { 7438 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 7439 console.log("RpcServer: localDeviceID is: " + localDeviceID); 7440 return true; 7441 } 7442 } 7443 ``` 7444 7445### isLocalCalling 7446 7447static isLocalCalling(): boolean 7448 7449Checks whether the remote process is a process of the local device. This API is a static method. 7450 7451**System capability**: SystemCapability.Communication.IPC.Core 7452 7453**Return value** 7454 7455 | Type | Description | 7456 | ------- | -------------------------------------------------- | 7457 | boolean | Returns **true** if the local and remote processes are on the same device; returns **false** otherwise.| 7458 7459**Example** 7460 7461 ```ts 7462 class Stub extends rpc.RemoteObject { 7463 onRemoteMessageRequest(code, data, reply, option) { 7464 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 7465 console.log("RpcServer: isLocalCalling is: " + isLocalCalling); 7466 return true; 7467 } 7468 } 7469 ``` 7470 7471### flushCmdBuffer<sup>9+</sup> 7472 7473static flushCmdBuffer(object: IRemoteObject): void 7474 7475Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. It is recommended that this method be called before any time-sensitive operation is performed. 7476 7477**System capability**: SystemCapability.Communication.IPC.Core 7478 7479**Parameters** 7480 7481 | Name| Type | Mandatory| Description | 7482 | ------ | ------------------------------- | ---- | ------------------- | 7483 | object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 7484 7485 7486**Example** 7487 7488 ```ts 7489 class TestRemoteObject extends rpc.RemoteObject { 7490 constructor(descriptor) { 7491 super(descriptor); 7492 } 7493 } 7494 let remoteObject = new TestRemoteObject("aaa"); 7495 try { 7496 rpc.IPCSkeleton.flushCmdBuffer(remoteObject); 7497 } catch(error) { 7498 console.info("proxy set calling identity fail, errorCode " + error.code); 7499 console.info("proxy set calling identity fail, errorMessage " + error.message); 7500 } 7501 ``` 7502 7503### flushCommands<sup>(deprecated)</sup> 7504 7505>This API is no longer maintained since API version 9. You are advised to use [flushCmdBuffer](#flushcmdbuffer9). 7506 7507static flushCommands(object: IRemoteObject): number 7508 7509Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. It is recommended that this method be called before any time-sensitive operation is performed. 7510 7511**System capability**: SystemCapability.Communication.IPC.Core 7512 7513**Parameters** 7514 7515 | Name| Type | Mandatory| Description | 7516 | ------ | ------------------------------- | ---- | ------------------- | 7517 | object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 7518 7519**Return value** 7520 7521 | Type | Description | 7522 | ------ | --------------------------------------------------------------------------------- | 7523 | number | Returns **0** if the operation is successful; returns an error code if the input object is null or a **RemoteObject**, or if the operation fails.| 7524 7525**Example** 7526 7527 ```ts 7528 class MyDeathRecipient { 7529 onRemoteDied() { 7530 console.log("server died"); 7531 } 7532 } 7533 class TestRemoteObject extends rpc.RemoteObject { 7534 constructor(descriptor) { 7535 super(descriptor); 7536 } 7537 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7538 return true; 7539 } 7540 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7541 return true; 7542 } 7543 isObjectDead(): boolean { 7544 return false; 7545 } 7546 } 7547 let remoteObject = new TestRemoteObject("aaa"); 7548 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 7549 console.log("RpcServer: flushCommands result: " + ret); 7550 ``` 7551 7552### resetCallingIdentity 7553 7554static resetCallingIdentity(): string 7555 7556Changes the UID and PID of the remote user to the UID and PID of the local user. This API is a static method. You can use it in scenarios such as identity authentication. 7557 7558**System capability**: SystemCapability.Communication.IPC.Core 7559 7560**Return value** 7561 7562 | Type | Description | 7563 | ------ | ------------------------------------ | 7564 | string | String containing the UID and PID of the remote user.| 7565 7566**Example** 7567 7568 ```ts 7569 class Stub extends rpc.RemoteObject { 7570 onRemoteMessageRequest(code, data, reply, option) { 7571 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 7572 console.log("RpcServer: callingIdentity is: " + callingIdentity); 7573 return true; 7574 } 7575 } 7576 ``` 7577 7578 7579### restoreCallingIdentity<sup>9+</sup> 7580 7581static restoreCallingIdentity(identity: string): void 7582 7583Changes the UID and PID of the remote user to the UID and PID of the local user. This API is a static method. You can use it in scenarios such as identity authentication. 7584 7585**System capability**: SystemCapability.Communication.IPC.Core 7586 7587**Parameters** 7588 7589 | Name | Type | Mandatory| Description | 7590 | -------- | ------ | ---- | ------------------------------------------------------------------ | 7591 | identity | string | Yes | String containing the remote user UID and PID, which are returned by **resetCallingIdentity**.| 7592 7593**Example** 7594 7595 ```ts 7596 class Stub extends rpc.RemoteObject { 7597 onRemoteMessageRequest(code, data, reply, option) { 7598 let callingIdentity = null; 7599 try { 7600 callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 7601 console.log("RpcServer: callingIdentity is: " + callingIdentity); 7602 } finally { 7603 rpc.IPCSkeleton.restoreCallingIdentity("callingIdentity "); 7604 } 7605 return true; 7606 } 7607 } 7608 ``` 7609 7610### setCallingIdentity<sup>(deprecated)</sup> 7611 7612>This API is no longer maintained since API version 9. You are advised to use [restoreCallingIdentity](#restorecallingidentity9). 7613 7614static setCallingIdentity(identity: string): boolean 7615 7616Sets the UID and PID of the remote user. This API is a static method. It is usually called when the UID and PID of the remote user are required. The UID and PID of the remote user are returned by **resetCallingIdentity**. 7617 7618**System capability**: SystemCapability.Communication.IPC.Core 7619 7620**Parameters** 7621 7622 | Name | Type | Mandatory| Description | 7623 | -------- | ------ | ---- | ------------------------------------------------------------------ | 7624 | identity | string | Yes | String containing the remote user UID and PID, which are returned by **resetCallingIdentity**.| 7625 7626**Return value** 7627 7628 | Type | Description | 7629 | ------- | ---------------------------------| 7630 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 7631 7632**Example** 7633 7634 ```ts 7635 class Stub extends rpc.RemoteObject { 7636 onRemoteMessageRequest(code, data, reply, option) { 7637 let callingIdentity = null; 7638 try { 7639 callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 7640 console.log("RpcServer: callingIdentity is: " + callingIdentity); 7641 } finally { 7642 let ret = rpc.IPCSkeleton.setCallingIdentity("callingIdentity "); 7643 console.log("RpcServer: setCallingIdentity is: " + ret); 7644 } 7645 return true; 7646 } 7647 } 7648 ``` 7649 7650## RemoteObject 7651 7652Provides methods to implement **RemoteObject**. The service provider must inherit from this class. 7653 7654### constructor 7655 7656constructor(descriptor: string) 7657 7658A constructor used to create a **RemoteObject** object. 7659 7660**System capability**: SystemCapability.Communication.IPC.Core 7661 7662**Parameters** 7663 7664 | Name | Type | Mandatory| Description | 7665 | ---------- | ------ | ---- | ------------ | 7666 | descriptor | string | Yes | Interface descriptor.| 7667 7668 7669### sendRequest<sup>(deprecated)</sup> 7670 7671>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 7672 7673sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 7674 7675Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 7676 7677**System capability**: SystemCapability.Communication.IPC.Core 7678 7679**Parameters** 7680 7681 | Name | Type | Mandatory| Description | 7682 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7683 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 7684 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7685 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7686 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7687 7688**Return value** 7689 7690 | Type | Description | 7691 | ------- | -------------------------------- | 7692 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 7693 7694**Example** 7695 7696 ```ts 7697 class MyDeathRecipient { 7698 onRemoteDied() { 7699 console.log("server died"); 7700 } 7701 } 7702 class TestRemoteObject extends rpc.RemoteObject { 7703 constructor(descriptor) { 7704 super(descriptor); 7705 } 7706 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7707 return true; 7708 } 7709 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7710 return true; 7711 } 7712 isObjectDead(): boolean { 7713 return false; 7714 } 7715 } 7716 let testRemoteObject = new TestRemoteObject("testObject"); 7717 let option = new rpc.MessageOption(); 7718 let data = rpc.MessageParcel.create(); 7719 let reply = rpc.MessageParcel.create(); 7720 data.writeInt(1); 7721 data.writeString("hello"); 7722 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 7723 if (ret) { 7724 console.log("sendRequest got result"); 7725 let msg = reply.readString(); 7726 console.log("RPCTest: reply msg: " + msg); 7727 } else { 7728 console.log("RPCTest: sendRequest failed"); 7729 } 7730 console.log("RPCTest: sendRequest ends, reclaim parcel"); 7731 data.reclaim(); 7732 reply.reclaim(); 7733 ``` 7734 7735### sendRequest<sup>8+(deprecated)</sup> 7736 7737>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 7738 7739sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 7740 7741Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 7742 7743**System capability**: SystemCapability.Communication.IPC.Core 7744 7745**Parameters** 7746 7747 | Name | Type | Mandatory| Description | 7748 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7749 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 7750 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7751 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7752 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7753 7754**Return value** 7755 7756 | Type | Description | 7757 | -------------------------------- | --------------------------------------------- | 7758 | Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 7759 7760**Example** 7761 7762 ```ts 7763 class MyDeathRecipient { 7764 onRemoteDied() { 7765 console.log("server died"); 7766 } 7767 } 7768 class TestRemoteObject extends rpc.RemoteObject { 7769 constructor(descriptor) { 7770 super(descriptor); 7771 } 7772 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7773 return true; 7774 } 7775 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7776 return true; 7777 } 7778 isObjectDead(): boolean { 7779 return false; 7780 } 7781 } 7782 let testRemoteObject = new TestRemoteObject("testObject"); 7783 let option = new rpc.MessageOption(); 7784 let data = rpc.MessageParcel.create(); 7785 let reply = rpc.MessageParcel.create(); 7786 data.writeInt(1); 7787 data.writeString("hello"); 7788 testRemoteObject.sendRequest(1, data, reply, option) 7789 .then(function(result) { 7790 if (result.errCode === 0) { 7791 console.log("sendRequest got result"); 7792 result.reply.readException(); 7793 let msg = result.reply.readString(); 7794 console.log("RPCTest: reply msg: " + msg); 7795 } else { 7796 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 7797 } 7798 }).catch(function(e) { 7799 console.log("RPCTest: sendRequest got exception: " + e.message); 7800 }).finally (() => { 7801 console.log("RPCTest: sendRequest ends, reclaim parcel"); 7802 data.reclaim(); 7803 reply.reclaim(); 7804 }); 7805 ``` 7806 7807### sendMessageRequest<sup>9+</sup> 7808 7809sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 7810 7811Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendMessageRequest** is returned, and the reply message contains the returned information. 7812 7813**System capability**: SystemCapability.Communication.IPC.Core 7814 7815**Parameters** 7816 7817 | Name | Type | Mandatory| Description | 7818 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7819 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 7820 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 7821 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 7822 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7823 7824**Return value** 7825 7826 | Type | Description | 7827 | ---------------------------- | --------------------------------------------- | 7828 | Promise<RequestResult> | Promise used to return the **sendRequestResult** object.| 7829 7830**Example** 7831 7832 ```ts 7833 class TestRemoteObject extends rpc.RemoteObject { 7834 constructor(descriptor) { 7835 super(descriptor); 7836 } 7837 } 7838 let testRemoteObject = new TestRemoteObject("testObject"); 7839 let option = new rpc.MessageOption(); 7840 let data = rpc.MessageSequence.create(); 7841 let reply = rpc.MessageSequence.create(); 7842 data.writeInt(1); 7843 data.writeString("hello"); 7844 testRemoteObject.sendMessageRequest(1, data, reply, option) 7845 .then(function(result) { 7846 if (result.errCode === 0) { 7847 console.log("sendMessageRequest got result"); 7848 result.reply.readException(); 7849 let msg = result.reply.readString(); 7850 console.log("RPCTest: reply msg: " + msg); 7851 } else { 7852 console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode); 7853 } 7854 }).catch(function(e) { 7855 console.log("RPCTest: sendMessageRequest got exception: " + e.message); 7856 }).finally (() => { 7857 console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); 7858 data.reclaim(); 7859 reply.reclaim(); 7860 }); 7861 ``` 7862 7863### sendMessageRequest<sup>9+</sup> 7864 7865sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 7866 7867Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked when the response to **sendMessageRequest** is returned, and the reply message contains the returned information. 7868 7869**System capability**: SystemCapability.Communication.IPC.Core 7870 7871**Parameters** 7872 7873 | Name | Type | Mandatory| Description | 7874 | ------------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7875 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 7876 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 7877 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 7878 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7879 | AsyncCallback | AsyncCallback<RequestResult> | Yes | Callback for receiving the sending result. | 7880 7881**Example** 7882 7883 ```ts 7884 class TestRemoteObject extends rpc.RemoteObject { 7885 constructor(descriptor) { 7886 super(descriptor); 7887 } 7888 } 7889 function sendRequestCallback(result) { 7890 if (result.errCode === 0) { 7891 console.log("sendRequest got result"); 7892 result.reply.readException(); 7893 let msg = result.reply.readString(); 7894 console.log("RPCTest: reply msg: " + msg); 7895 } else { 7896 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 7897 } 7898 console.log("RPCTest: sendRequest ends, reclaim parcel"); 7899 result.data.reclaim(); 7900 result.reply.reclaim(); 7901 } 7902 let testRemoteObject = new TestRemoteObject("testObject"); 7903 let option = new rpc.MessageOption(); 7904 let data = rpc.MessageSequence.create(); 7905 let reply = rpc.MessageSequence.create(); 7906 data.writeInt(1); 7907 data.writeString("hello"); 7908 testRemoteObject.sendMessageRequest(1, data, reply, option, sendRequestCallback); 7909 ``` 7910 7911### sendRequest<sup>8+(deprecated)</sup> 7912 7913>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 7914 7915sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 7916 7917Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information. 7918 7919**System capability**: SystemCapability.Communication.IPC.Core 7920 7921**Parameters** 7922 7923 | Name | Type | Mandatory| Description | 7924 | ------------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7925 | code | number | Yes | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 7926 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7927 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7928 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7929 | AsyncCallback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. | 7930 7931**Example** 7932 7933 ```ts 7934 class MyDeathRecipient { 7935 onRemoteDied() { 7936 console.log("server died"); 7937 } 7938 } 7939 class TestRemoteObject extends rpc.RemoteObject { 7940 constructor(descriptor) { 7941 super(descriptor); 7942 } 7943 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7944 return true; 7945 } 7946 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7947 return true; 7948 } 7949 isObjectDead(): boolean { 7950 return false; 7951 } 7952 } 7953 function sendRequestCallback(result) { 7954 if (result.errCode === 0) { 7955 console.log("sendRequest got result"); 7956 result.reply.readException(); 7957 let msg = result.reply.readString(); 7958 console.log("RPCTest: reply msg: " + msg); 7959 } else { 7960 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 7961 } 7962 console.log("RPCTest: sendRequest ends, reclaim parcel"); 7963 result.data.reclaim(); 7964 result.reply.reclaim(); 7965 } 7966 let testRemoteObject = new TestRemoteObject("testObject"); 7967 let option = new rpc.MessageOption(); 7968 let data = rpc.MessageParcel.create(); 7969 let reply = rpc.MessageParcel.create(); 7970 data.writeInt(1); 7971 data.writeString("hello"); 7972 testRemoteObject.sendRequest(1, data, reply, option, sendRequestCallback); 7973 ``` 7974 7975### onRemoteRequest<sup>8+(deprecated)</sup> 7976 7977>This API is no longer maintained since API version 9. You are advised to use [onRemoteMessageRequest](#onremotemessagerequest9). 7978 7979onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 7980 7981Provides a response to **sendMessageRequest()**. The server processes the request and returns a response in this API. 7982 7983**System capability**: SystemCapability.Communication.IPC.Core 7984 7985**Parameters** 7986 7987 | Name| Type | Mandatory| Description | 7988 | ------ | ----------------------------------------- | ---- | --------------------------------------- | 7989 | code | number | Yes | Service request code sent by the remote end. | 7990 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that holds the parameters called by the client.| 7991 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object carrying the result. | 7992 | option | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 7993 7994**Return value** 7995 7996 | Type | Description | 7997 | ------- | -------------------------------- | 7998 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 7999 8000**Example** 8001 8002 ```ts 8003 class MyDeathRecipient { 8004 onRemoteDied() { 8005 console.log("server died"); 8006 } 8007 } 8008 class TestRemoteObject extends rpc.RemoteObject { 8009 constructor(descriptor) { 8010 super(descriptor); 8011 } 8012 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8013 return true; 8014 } 8015 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8016 return true; 8017 } 8018 isObjectDead(): boolean { 8019 return false; 8020 } 8021 onRemoteRequest(code, data, reply, option) { 8022 if (code === 1) { 8023 console.log("RpcServer: onRemoteRequest called"); 8024 return true; 8025 } else { 8026 console.log("RpcServer: unknown code: " + code); 8027 return false; 8028 } 8029 } 8030 } 8031 ``` 8032 8033### onRemoteMessageRequest<sup>9+</sup> 8034 8035onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean> 8036 8037> **NOTE**<br/> 8038> 8039>* You are advised to overload **onRemoteMessageRequest** preferentially, which implements synchronous and asynchronous message processing. 8040>* If both onRemoteRequest() and onRemoteMessageRequest() are overloaded, only the onRemoteMessageRequest() takes effect. 8041 8042Provides a response to **sendMessageRequest()**. The server processes the request synchronously or asynchronously and returns the result in this API. 8043 8044**System capability**: SystemCapability.Communication.IPC.Core 8045 8046**Parameters** 8047 8048 | Name| Type | Mandatory| Description | 8049 | ------ | ------------------------------------ | ---- | ----------------------------------------- | 8050 | code | number | Yes | Service request code sent by the remote end. | 8051 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that holds the parameters called by the client.| 8052 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object to which the result is written. | 8053 | option | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 8054 8055**Return value** 8056 8057 | Type | Description | 8058 | ----------------- | ----------------------------------------------------------------------------------------------- | 8059 | boolean | Returns a Boolean value if the request is processed synchronously in **onRemoteMessageRequest**. The value **true** means the operation is successful; the value **false** means the opposite.| 8060 | Promise\<boolean> | Returns a promise object if the request is processed asynchronously in **onRemoteMessageRequest**. | 8061 8062**Example**: Overload **onRemoteMessageRequest** to process requests synchronously. 8063 8064 ```ts 8065 class TestRemoteObject extends rpc.RemoteObject { 8066 constructor(descriptor) { 8067 super(descriptor); 8068 } 8069 8070 onRemoteMessageRequest(code, data, reply, option) { 8071 if (code === 1) { 8072 console.log("RpcServer: sync onRemoteMessageRequest is called"); 8073 return true; 8074 } else { 8075 console.log("RpcServer: unknown code: " + code); 8076 return false; 8077 } 8078 } 8079 } 8080 ``` 8081 8082 **Example**: Overload **onRemoteMessageRequest** to process requests asynchronously. 8083 8084 ```ts 8085 class TestRemoteObject extends rpc.RemoteObject { 8086 constructor(descriptor) { 8087 super(descriptor); 8088 } 8089 8090 async onRemoteMessageRequest(code, data, reply, option) { 8091 if (code === 1) { 8092 console.log("RpcServer: async onRemoteMessageRequest is called"); 8093 } else { 8094 console.log("RpcServer: unknown code: " + code); 8095 return false; 8096 } 8097 await new Promise((resolve) => { 8098 setTimeout(resolve, 100); 8099 }) 8100 return true; 8101 } 8102 } 8103 ``` 8104 8105**Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests synchronously. 8106 8107 ```ts 8108 class TestRemoteObject extends rpc.RemoteObject { 8109 constructor(descriptor) { 8110 super(descriptor); 8111 } 8112 8113 onRemoteRequest(code, data, reply, option) { 8114 if (code === 1) { 8115 console.log("RpcServer: sync onRemoteMessageRequest is called"); 8116 return true; 8117 } else { 8118 console.log("RpcServer: unknown code: " + code); 8119 return false; 8120 } 8121 } 8122 // Only onRemoteMessageRequest is executed. 8123 onRemoteMessageRequest(code, data, reply, option) { 8124 if (code === 1) { 8125 console.log("RpcServer: async onRemoteMessageRequest is called"); 8126 } else { 8127 console.log("RpcServer: unknown code: " + code); 8128 return false; 8129 } 8130 8131 return true; 8132 } 8133 } 8134 ``` 8135 8136 **Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests asynchronously. 8137 8138 ```ts 8139 class TestRemoteObject extends rpc.RemoteObject { 8140 constructor(descriptor) { 8141 super(descriptor); 8142 } 8143 8144 onRemoteRequest(code, data, reply, option) { 8145 if (code === 1) { 8146 console.log("RpcServer: sync onRemoteRequest is called"); 8147 return true; 8148 } else { 8149 console.log("RpcServer: unknown code: " + code); 8150 return false; 8151 } 8152 } 8153 // Only onRemoteMessageRequest is executed. 8154 async onRemoteMessageRequest(code, data, reply, option) { 8155 if (code === 1) { 8156 console.log("RpcServer: async onRemoteMessageRequest is called"); 8157 } else { 8158 console.log("RpcServer: unknown code: " + code); 8159 return false; 8160 } 8161 await new Promise((resolve) => { 8162 setTimeout(resolve, 100); 8163 }) 8164 return true; 8165 } 8166 } 8167 ``` 8168 8169### getCallingUid 8170 8171getCallingUid(): number 8172 8173Obtains the UID of the remote process. 8174 8175**System capability**: SystemCapability.Communication.IPC.Core 8176 8177**Return value** 8178 | Type | Description | 8179 | ------ | ----------------------- | 8180 | number | UID of the remote process obtained.| 8181 8182**Example** 8183 8184 ```ts 8185 class TestRemoteObject extends rpc.RemoteObject { 8186 constructor(descriptor) { 8187 super(descriptor); 8188 } 8189 } 8190 let testRemoteObject = new TestRemoteObject("testObject"); 8191 console.log("RpcServer: getCallingUid: " + testRemoteObject.getCallingUid()); 8192 ``` 8193 8194### getCallingPid 8195 8196getCallingPid(): number 8197 8198Obtains the PID of the remote process. 8199 8200**System capability**: SystemCapability.Communication.IPC.Core 8201 8202**Return value** 8203 8204 | Type | Description | 8205 | ------ | ----------------------- | 8206 | number | PID of the remote process obtained.| 8207 8208**Example** 8209 8210 ```ts 8211 class TestRemoteObject extends rpc.RemoteObject { 8212 constructor(descriptor) { 8213 super(descriptor); 8214 } 8215 } 8216 let testRemoteObject = new TestRemoteObject("testObject"); 8217 console.log("RpcServer: getCallingPid: " + testRemoteObject.getCallingPid()); 8218 ``` 8219 8220### getLocalInterface<sup>9+</sup> 8221 8222getLocalInterface(descriptor: string): IRemoteBroker 8223 8224Obtains the interface descriptor. 8225 8226**System capability**: SystemCapability.Communication.IPC.Core 8227 8228**Parameters** 8229 8230 | Name | Type | Mandatory| Description | 8231 | ---------- | ------ | ---- | -------------------- | 8232 | descriptor | string | Yes | Interface descriptor.| 8233 8234**Return value** 8235 8236 | Type | Description | 8237 | ------------- | --------------------------------------------- | 8238 | IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.| 8239 8240 8241**Example** 8242 8243 ```ts 8244 class MyDeathRecipient { 8245 onRemoteDied() { 8246 console.log("server died"); 8247 } 8248 } 8249 class TestRemoteObject extends rpc.RemoteObject { 8250 constructor(descriptor) { 8251 super(descriptor); 8252 } 8253 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8254 // Implement the method logic based on service requirements. 8255 } 8256 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8257 // Implement the method logic based on service requirements. 8258 } 8259 isObjectDead(): boolean { 8260 return false; 8261 } 8262 } 8263 let testRemoteObject = new TestRemoteObject("testObject"); 8264 try { 8265 let broker = testRemoteObject.getLocalInterface("testObject"); 8266 } catch(error) { 8267 console.info("rpc get local interface fail, errorCode " + error.code); 8268 console.info("rpc get local interface fail, errorMessage " + error.message); 8269 } 8270 ``` 8271 8272### queryLocalInterface<sup>(deprecated)</sup> 8273 8274>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9). 8275 8276queryLocalInterface(descriptor: string): IRemoteBroker 8277 8278Checks whether the remote object corresponding to the specified interface token exists. 8279 8280**System capability**: SystemCapability.Communication.IPC.Core 8281 8282**Parameters** 8283 8284 | Name | Type | Mandatory| Description | 8285 | ---------- | ------ | ---- | ---------------------- | 8286 | descriptor | string | Yes | Interface descriptor.| 8287 8288**Return value** 8289 8290 | Type | Description | 8291 | ------------- | ------------------------------------------------------------------ | 8292 | IRemoteBroker | Returns the remote object if a match is found; returns **Null** otherwise.| 8293 8294**Example** 8295 8296 ```ts 8297 class MyDeathRecipient { 8298 onRemoteDied() { 8299 console.log("server died"); 8300 } 8301 } 8302 class TestRemoteObject extends rpc.RemoteObject { 8303 constructor(descriptor) { 8304 super(descriptor); 8305 } 8306 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8307 return true; 8308 } 8309 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8310 return true; 8311 } 8312 isObjectDead(): boolean { 8313 return false; 8314 } 8315 } 8316 let testRemoteObject = new TestRemoteObject("testObject"); 8317 let broker = testRemoteObject.queryLocalInterface("testObject"); 8318 ``` 8319 8320### getDescriptor<sup>9+</sup> 8321 8322getDescriptor(): string 8323 8324Obtains the interface descriptor of this object. The interface descriptor is a string. 8325 8326**System capability**: SystemCapability.Communication.IPC.Core 8327 8328**Return value** 8329 8330 | Type | Description | 8331 | ------ | ---------------- | 8332 | string | Interface descriptor obtained.| 8333 8334**Error codes** 8335 8336For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8337 8338 | ID| Error Message| 8339 | -------- | -------- | 8340 | 1900008 | proxy or remote object is invalid | 8341 8342**Example** 8343 8344 ```ts 8345 class MyDeathRecipient { 8346 onRemoteDied() { 8347 console.log("server died"); 8348 } 8349 } 8350 class TestRemoteObject extends rpc.RemoteObject { 8351 constructor(descriptor) { 8352 super(descriptor); 8353 } 8354 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8355 // Implement the method logic based on service requirements. 8356 } 8357 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8358 // Implement the method logic based on service requirements. 8359 } 8360 isObjectDead(): boolean { 8361 return false; 8362 } 8363 } 8364 let testRemoteObject = new TestRemoteObject("testObject"); 8365 console.log("RpcServer: descriptor is: " + descriptor); 8366 try { 8367 let descriptor = testRemoteObject.getDescriptor(); 8368 } catch(error) { 8369 console.info("rpc get local interface fail, errorCode " + error.code); 8370 console.info("rpc get local interface fail, errorMessage " + error.message); 8371 } 8372 ``` 8373 8374### getInterfaceDescriptor<sup>(deprecated)</sup> 8375 8376>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9). 8377 8378getInterfaceDescriptor(): string 8379 8380Obtains the interface descriptor. 8381 8382**System capability**: SystemCapability.Communication.IPC.Core 8383 8384**Return value** 8385 8386 | Type | Description | 8387 | ------ | ---------------- | 8388 | string | Interface descriptor obtained.| 8389 8390**Example** 8391 8392 ```ts 8393 class MyDeathRecipient { 8394 onRemoteDied() { 8395 console.log("server died"); 8396 } 8397 } 8398 class TestRemoteObject extends rpc.RemoteObject { 8399 constructor(descriptor) { 8400 super(descriptor); 8401 } 8402 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8403 return true; 8404 } 8405 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8406 return true; 8407 } 8408 isObjectDead(): boolean { 8409 return false; 8410 } 8411 } 8412 let testRemoteObject = new TestRemoteObject("testObject"); 8413 let descriptor = testRemoteObject.getInterfaceDescriptor(); 8414 console.log("RpcServer: descriptor is: " + descriptor); 8415 ``` 8416 8417### modifyLocalInterface<sup>9+</sup> 8418 8419modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 8420 8421Binds an interface descriptor to an **IRemoteBroker** object. 8422 8423**System capability**: SystemCapability.Communication.IPC.Core 8424 8425**Parameters** 8426 8427 | Name | Type | Mandatory| Description | 8428 | -------------- | ------------- | ---- | ------------------------------------- | 8429 | localInterface | IRemoteBroker | Yes | **IRemoteBroker** object. | 8430 | descriptor | string | Yes | Interface descriptor.| 8431 8432**Example** 8433 8434 ```ts 8435 class MyDeathRecipient { 8436 onRemoteDied() { 8437 console.log("server died"); 8438 } 8439 } 8440 class TestRemoteObject extends rpc.RemoteObject { 8441 constructor(descriptor) { 8442 super(descriptor); 8443 try { 8444 this.modifyLocalInterface(this, descriptor); 8445 } catch(error) { 8446 console.info(" rpc attach local interface fail, errorCode " + error.code); 8447 console.info(" rpc attach local interface fail, errorMessage " + error.message); 8448 } 8449 } 8450 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8451 // Implement the method logic based on service requirements. 8452 } 8453 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8454 // Implement the method logic based on service requirements. 8455 } 8456 isObjectDead(): boolean { 8457 return false; 8458 } 8459 asObject(): rpc.IRemoteObject { 8460 return this; 8461 } 8462 } 8463 let testRemoteObject = new TestRemoteObject("testObject"); 8464 ``` 8465 8466### attachLocalInterface<sup>(deprecated)</sup> 8467 8468>This API is no longer maintained since API version 9. You are advised to use [modifyLocalInterface](#modifylocalinterface9). 8469 8470attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 8471 8472Binds an interface descriptor to an **IRemoteBroker** object. 8473 8474**System capability**: SystemCapability.Communication.IPC.Core 8475 8476**Parameters** 8477 8478 | Name | Type | Mandatory| Description | 8479 | -------------- | ------------- | ---- | ------------------------------------- | 8480 | localInterface | IRemoteBroker | Yes | **IRemoteBroker** object. | 8481 | descriptor | string | Yes | Interface descriptor.| 8482 8483**Example** 8484 8485 ```ts 8486 class MyDeathRecipient { 8487 onRemoteDied() { 8488 console.log("server died"); 8489 } 8490 } 8491 class TestRemoteObject extends rpc.RemoteObject { 8492 constructor(descriptor) { 8493 super(descriptor); 8494 this.attachLocalInterface(this, descriptor); 8495 } 8496 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8497 return true; 8498 } 8499 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8500 return true; 8501 } 8502 isObjectDead(): boolean { 8503 return false; 8504 } 8505 asObject(): rpc.IRemoteObject { 8506 return this; 8507 } 8508 } 8509 let testRemoteObject = new TestRemoteObject("testObject"); 8510 ``` 8511 8512## Ashmem<sup>8+</sup> 8513 8514Provides methods related to anonymous shared memory objects, including creating, closing, mapping, and unmapping an **Ashmem** object, reading data from and writing data to an **Ashmem** object, obtaining the **Ashmem** size, and setting **Ashmem** protection. 8515 8516**System capability**: SystemCapability.Communication.IPC.Core 8517 8518The table below describes the protection types of the mapped memory. 8519 8520 | Name | Value | Description | 8521 | ---------- | --- | ------------------ | 8522 | PROT_EXEC | 4 | The mapped memory is executable. | 8523 | PROT_NONE | 0 | The mapped memory is inaccessible.| 8524 | PROT_READ | 1 | The mapped memory is readable. | 8525 | PROT_WRITE | 2 | The mapped memory is writeable. | 8526 8527### create<sup>9+</sup> 8528 8529static create(name: string, size: number): Ashmem 8530 8531Creates an **Ashmem** object with the specified name and size. This API is a static method. 8532 8533**System capability**: SystemCapability.Communication.IPC.Core 8534 8535**Parameters** 8536 8537 | Name| Type | Mandatory| Description | 8538 | ------ | ------ | ---- | ---------------------------- | 8539 | name | string | Yes | Name of the **Ashmem** object to create. | 8540 | size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 8541 8542**Return value** 8543 8544 | Type | Description | 8545 | ------ | ---------------------------------------------- | 8546 | Ashmem | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 8547 8548**Example** 8549 8550 ```ts 8551 let ashmem; 8552 try { 8553 ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8554 } catch(error) { 8555 console.info("Rpc creat ashmem fail, errorCode " + error.code); 8556 console.info("Rpc creat ashmem fail, errorMessage " + error.message); 8557 } 8558 let size = ashmem.getAshmemSize(); 8559 console.log("RpcTest: get ashemm by create : " + ashmem + " size is : " + size); 8560 ``` 8561 8562### createAshmem<sup>8+(deprecated)</sup> 8563 8564>This API is no longer maintained since API version 9. You are advised to use [create](#create9). 8565 8566static createAshmem(name: string, size: number): Ashmem 8567 8568Creates an **Ashmem** object with the specified name and size. This API is a static method. 8569 8570**System capability**: SystemCapability.Communication.IPC.Core 8571 8572**Parameters** 8573 8574 | Name| Type | Mandatory| Description | 8575 | ------ | ------ | ---- | ---------------------------- | 8576 | name | string | Yes | Name of the **Ashmem** object to create. | 8577 | size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 8578 8579**Return value** 8580 8581 | Type | Description | 8582 | ------ | ---------------------------------------------- | 8583 | Ashmem | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 8584 8585**Example** 8586 8587 ```ts 8588 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8589 let size = ashmem.getAshmemSize(); 8590 console.log("RpcTest: get ashemm by createAshmem : " + ashmem + " size is : " + size); 8591 ``` 8592 8593### create<sup>9+</sup> 8594 8595static create(ashmem: Ashmem): Ashmem 8596 8597Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 8598 8599**System capability**: SystemCapability.Communication.IPC.Core 8600 8601**Parameters** 8602 8603 | Name| Type | Mandatory| Description | 8604 | ------ | ------ | ---- | -------------------- | 8605 | ashmem | Ashmem | Yes | Existing **Ashmem** object.| 8606 8607**Return value** 8608 8609 | Type | Description | 8610 | ------ | ---------------------- | 8611 | Ashmem | **Ashmem** object created.| 8612 8613 8614**Example** 8615 8616 ```ts 8617 let ashmem2; 8618 try { 8619 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8620 let ashmem2 = rpc.Ashmem.create(ashmem); 8621 } catch(error) { 8622 console.info("Rpc creat ashmem from existing fail, errorCode " + error.code); 8623 console.info("Rpc creat ashmem from existing fail, errorMessage " + error.message); 8624 } 8625 let size = ashmem2.getAshmemSize(); 8626 console.log("RpcTest: get ashemm by create : " + ashmem2 + " size is : " + size); 8627 ``` 8628 8629### createAshmemFromExisting<sup>8+(deprecated)</sup> 8630 8631>This API is no longer maintained since API version 9. You are advised to use [create](#create9). 8632 8633static createAshmemFromExisting(ashmem: Ashmem): Ashmem 8634 8635Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 8636 8637**System capability**: SystemCapability.Communication.IPC.Core 8638 8639**Parameters** 8640 8641 | Name| Type | Mandatory| Description | 8642 | ------ | ------ | ---- | -------------------- | 8643 | ashmem | Ashmem | Yes | Existing **Ashmem** object.| 8644 8645**Return value** 8646 8647 | Type | Description | 8648 | ------ | ---------------------- | 8649 | Ashmem | **Ashmem** object created.| 8650 8651**Example** 8652 8653 ```ts 8654 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8655 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 8656 let size = ashmem2.getAshmemSize(); 8657 console.log("RpcTest: get ashemm by createAshmemFromExisting : " + ashmem2 + " size is : " + size); 8658 ``` 8659 8660### closeAshmem<sup>8+</sup> 8661 8662closeAshmem(): void 8663 8664Closes this **Ashmem** object. 8665 8666**System capability**: SystemCapability.Communication.IPC.Core 8667 8668**Example** 8669 8670 ```ts 8671 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8672 ashmem.closeAshmem(); 8673 ``` 8674 8675### unmapAshmem<sup>8+</sup> 8676 8677unmapAshmem(): void 8678 8679Deletes the mappings for the specified address range of this **Ashmem** object. 8680 8681**System capability**: SystemCapability.Communication.IPC.Core 8682 8683**Example** 8684 8685 ```ts 8686 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8687 ashmem.unmapAshmem(); 8688 ``` 8689 8690### getAshmemSize<sup>8+</sup> 8691 8692getAshmemSize(): number 8693 8694Obtains the memory size of this **Ashmem** object. 8695 8696**System capability**: SystemCapability.Communication.IPC.Core 8697 8698**Return value** 8699 8700 | Type | Description | 8701 | ------ | -------------------------- | 8702 | number | **Ashmem** size obtained.| 8703 8704**Example** 8705 8706 ```ts 8707 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8708 let size = ashmem.getAshmemSize(); 8709 console.log("RpcTest: get ashmem is " + ashmem + " size is : " + size); 8710 ``` 8711 8712### mapTypedAshmem<sup>9+</sup> 8713 8714mapTypedAshmem(mapType: number): void 8715 8716Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 8717 8718**System capability**: SystemCapability.Communication.IPC.Core 8719 8720**Parameters** 8721 8722 | Name | Type | Mandatory| Description | 8723 | ------- | ------ | ---- | ------------------------------ | 8724 | mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 8725 8726**Error codes** 8727 8728For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8729 8730 | ID| Error Message| 8731 | -------- | -------- | 8732 | 1900001 | call mmap function failed | 8733 8734**Example** 8735 8736 ```ts 8737 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8738 try { 8739 ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 8740 } catch(error) { 8741 console.info("Rpc map ashmem fail, errorCode " + error.code); 8742 console.info("Rpc map ashmem fail, errorMessage " + error.message); 8743 } 8744 ``` 8745 8746### mapAshmem<sup>8+(deprecated)</sup> 8747 8748>This API is no longer maintained since API version 9. You are advised to use [mapTypedAshmem](#maptypedashmem9). 8749 8750mapAshmem(mapType: number): boolean 8751 8752Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 8753 8754**System capability**: SystemCapability.Communication.IPC.Core 8755 8756**Parameters** 8757 8758 | Name | Type | Mandatory| Description | 8759 | ------- | ------ | ---- | ------------------------------ | 8760 | mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 8761 8762**Return value** 8763 8764 | Type | Description | 8765 | ------- | -------------------------------- | 8766 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 8767 8768**Example** 8769 8770 ```ts 8771 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8772 let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 8773 console.log("RpcTest: map ashmem result is : " + mapReadAndWrite); 8774 ``` 8775 8776### mapReadWriteAshmem<sup>9+</sup> 8777 8778mapReadWriteAshmem(): void 8779 8780Maps the shared file to the readable and writable virtual address space of the process. 8781 8782**System capability**: SystemCapability.Communication.IPC.Core 8783 8784**Error codes** 8785 8786For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8787 8788 | ID| Error Message| 8789 | -------- | -------- | 8790 | 1900001 | call mmap function failed | 8791 8792**Example** 8793 8794 ```ts 8795 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8796 try { 8797 ashmem.mapReadWriteAshmem(); 8798 } catch(error) { 8799 console.info("Rpc map read and write ashmem fail, errorCode " + error.code); 8800 console.info("Rpc map read and write ashmem fail, errorMessage " + error.message); 8801 } 8802 ``` 8803 8804### mapReadAndWriteAshmem<sup>8+(deprecated)</sup> 8805 8806>This API is no longer maintained since API version 9. You are advised to use [mapReadWriteAshmem](#mapreadwriteashmem9). 8807 8808mapReadAndWriteAshmem(): boolean 8809 8810Maps the shared file to the readable and writable virtual address space of the process. 8811 8812**System capability**: SystemCapability.Communication.IPC.Core 8813 8814**Return value** 8815 8816 | Type | Description | 8817 | ------- | -------------------------------- | 8818 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 8819 8820**Example** 8821 8822 ```ts 8823 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8824 let mapResult = ashmem.mapReadAndWriteAshmem(); 8825 console.log("RpcTest: map ashmem result is : " + mapResult); 8826 ``` 8827 8828### mapReadonlyAshmem<sup>9+</sup> 8829 8830mapReadonlyAshmem(): void 8831 8832Maps the shared file to the read-only virtual address space of the process. 8833 8834**System capability**: SystemCapability.Communication.IPC.Core 8835 8836**Error codes** 8837 8838For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8839 8840 | ID| Error Message| 8841 | -------- | -------- | 8842 | 1900001 | call mmap function failed | 8843 8844**Example** 8845 8846 ```ts 8847 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8848 try { 8849 ashmem.mapReadonlyAshmem(); 8850 } catch(error) { 8851 console.info("Rpc map read and write ashmem fail, errorCode " + error.code); 8852 console.info("Rpc map read and write ashmem fail, errorMessage " + error.message); 8853 } 8854 ``` 8855 8856### mapReadOnlyAshmem<sup>8+(deprecated)</sup> 8857 8858>This API is no longer maintained since API version 9. You are advised to use [mapReadonlyAshmem](#mapreadonlyashmem9). 8859 8860mapReadOnlyAshmem(): boolean 8861 8862Maps the shared file to the read-only virtual address space of the process. 8863 8864**System capability**: SystemCapability.Communication.IPC.Core 8865 8866**Return value** 8867 8868 | Type | Description | 8869 | ------- | -------------------------------- | 8870 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 8871 8872**Example** 8873 8874 ```ts 8875 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8876 let mapResult = ashmem.mapReadOnlyAshmem(); 8877 console.log("RpcTest: Ashmem mapReadOnlyAshmem result is : " + mapResult); 8878 ``` 8879 8880### setProtectionType<sup>9+</sup> 8881 8882setProtectionType(protectionType: number): void 8883 8884Sets the protection level of the memory region to which the shared file is mapped. 8885 8886**System capability**: SystemCapability.Communication.IPC.Core 8887 8888**Parameters** 8889 8890 | Name | Type | Mandatory| Description | 8891 | -------------- | ------ | ---- | ------------------ | 8892 | protectionType | number | Yes | Protection type to set.| 8893 8894**Error codes** 8895 8896For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8897 8898 | ID| Error Message| 8899 | -------- | -------- | 8900 | 1900002 | call os ioctl function failed | 8901 8902**Example** 8903 8904 ```ts 8905 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8906 try { 8907 ashmem.setProtection(ashmem.PROT_READ); 8908 } catch(error) { 8909 console.info("Rpc set protection type fail, errorCode " + error.code); 8910 console.info("Rpc set protection type fail, errorMessage " + error.message); 8911 } 8912 ``` 8913 8914### setProtection<sup>8+(deprecated)</sup> 8915 8916>This API is no longer maintained since API version 9. You are advised to use [setProtectionType](#setprotectiontype9). 8917 8918setProtection(protectionType: number): boolean 8919 8920Sets the protection level of the memory region to which the shared file is mapped. 8921 8922**System capability**: SystemCapability.Communication.IPC.Core 8923 8924**Parameters** 8925 8926 | Name | Type | Mandatory| Description | 8927 | -------------- | ------ | ---- | ------------------ | 8928 | protectionType | number | Yes | Protection type to set.| 8929 8930**Return value** 8931 8932 | Type | Description | 8933 | ------- | -------------------------------- | 8934 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 8935 8936**Example** 8937 8938 ```ts 8939 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8940 let result = ashmem.setProtection(ashmem.PROT_READ); 8941 console.log("RpcTest: Ashmem setProtection result is : " + result); 8942 ``` 8943 8944### writeAshmem<sup>9+</sup> 8945 8946writeAshmem(buf: number[], size: number, offset: number): void 8947 8948Writes data to the shared file associated with this **Ashmem** object. 8949 8950**System capability**: SystemCapability.Communication.IPC.Core 8951 8952**Parameters** 8953 8954 | Name| Type | Mandatory| Description | 8955 | ------ | -------- | ---- | -------------------------------------------------- | 8956 | buf | number[] | Yes | Data to write. | 8957 | size | number | Yes | Size of the data to write. | 8958 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 8959 8960**Error codes** 8961 8962For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8963 8964 | ID| Error Message| 8965 | -------- | -------- | 8966 | 1900003 | write to ashmem failed | 8967 8968**Example** 8969 8970 ```ts 8971 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8972 ashmem.mapReadWriteAshmem(); 8973 var ByteArrayVar = [1, 2, 3, 4, 5]; 8974 try { 8975 ashmem.writeAshmem(ByteArrayVar, 5, 0); 8976 } catch(error) { 8977 console.info("Rpc write to ashmem fail, errorCode " + error.code); 8978 console.info("Rpc write to ashmem fail, errorMessage " + error.message); 8979 } 8980 ``` 8981 8982### writeToAshmem<sup>8+(deprecated)</sup> 8983 8984>This API is no longer maintained since API version 9. You are advised to use [writeAshmem](#writeashmem9). 8985 8986writeToAshmem(buf: number[], size: number, offset: number): boolean 8987 8988Writes data to the shared file associated with this **Ashmem** object. 8989 8990**System capability**: SystemCapability.Communication.IPC.Core 8991 8992**Parameters** 8993 8994 | Name| Type | Mandatory| Description | 8995 | ------ | -------- | ---- | -------------------------------------------------- | 8996 | buf | number[] | Yes | Data to write. | 8997 | size | number | Yes | Size of the data to write. | 8998 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 8999 9000**Return value** 9001 9002 | Type | Description | 9003 | ------- | ----------------------------------------------------------------------------- | 9004 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 9005 9006**Example** 9007 9008 ```ts 9009 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9010 let mapResult = ashmem.mapReadAndWriteAshmem(); 9011 console.info("RpcTest map ashmem result is " + mapResult); 9012 var ByteArrayVar = [1, 2, 3, 4, 5]; 9013 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 9014 console.log("RpcTest: write to Ashmem result is : " + writeResult); 9015 ``` 9016 9017### readAshmem<sup>9+</sup> 9018 9019readAshmem(size: number, offset: number): number[] 9020 9021Reads data from the shared file associated with this **Ashmem** object. 9022 9023**System capability**: SystemCapability.Communication.IPC.Core 9024 9025**Parameters** 9026 9027 | Name| Type | Mandatory| Description | 9028 | ------ | ------ | ---- | -------------------------------------------------- | 9029 | size | number | Yes | Size of the data to read. | 9030 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 9031 9032**Return value** 9033 9034 | Type | Description | 9035 | -------- | ---------------- | 9036 | number[] | Data read.| 9037 9038**Error codes** 9039 9040For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9041 9042 | ID| Error Message| 9043 | -------- | -------- | 9044 | 1900004 | read from ashmem failed | 9045 9046**Example** 9047 9048 ```ts 9049 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9050 ashmem.mapReadWriteAshmem(); 9051 var ByteArrayVar = [1, 2, 3, 4, 5]; 9052 ashmem.writeAshmem(ByteArrayVar, 5, 0); 9053 try { 9054 let readResult = ashmem.readAshmem(5, 0); 9055 console.log("RpcTest: read from Ashmem result is : " + readResult); 9056 } catch(error) { 9057 console.info("Rpc read from ashmem fail, errorCode " + error.code); 9058 console.info("Rpc read from ashmem fail, errorMessage " + error.message); 9059 } 9060 ``` 9061 9062### readFromAshmem<sup>8+(deprecated)</sup> 9063 9064>This API is no longer maintained since API version 9. You are advised to use [readAshmem](#readashmem9). 9065 9066readFromAshmem(size: number, offset: number): number[] 9067 9068Reads data from the shared file associated with this **Ashmem** object. 9069 9070**System capability**: SystemCapability.Communication.IPC.Core 9071 9072**Parameters** 9073 9074 | Name| Type | Mandatory| Description | 9075 | ------ | ------ | ---- | -------------------------------------------------- | 9076 | size | number | Yes | Size of the data to read. | 9077 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 9078 9079**Return value** 9080 9081 | Type | Description | 9082 | -------- | ---------------- | 9083 | number[] | Data read.| 9084 9085**Example** 9086 9087 ``` ts 9088 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9089 let mapResult = ashmem.mapReadAndWriteAshmem(); 9090 console.info("RpcTest map ashmem result is " + mapResult); 9091 var ByteArrayVar = [1, 2, 3, 4, 5]; 9092 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 9093 console.log("RpcTest: write to Ashmem result is : " + writeResult); 9094 let readResult = ashmem.readFromAshmem(5, 0); 9095 console.log("RpcTest: read to Ashmem result is : " + readResult); 9096 ``` 9097 9098## Obtaining the Context 9099 9100**Example** 9101 9102 ```ts 9103 import Ability from '@ohos.app.ability.UIAbility'; 9104 export default class MainAbility extends Ability { 9105 onCreate(want, launchParam) { 9106 console.log("[Demo] MainAbility onCreate"); 9107 globalThis.context = this.context; 9108 } 9109 onDestroy() { 9110 console.log("[Demo] MainAbility onDestroy"); 9111 } 9112 onWindowStageCreate(windowStage) { 9113 // Main window is created, set main page for this ability 9114 console.log("[Demo] MainAbility onWindowStageCreate"); 9115 } 9116 onWindowStageDestroy() { 9117 // Main window is destroyed, release UI related resources 9118 console.log("[Demo] MainAbility onWindowStageDestroy"); 9119 } 9120 onForeground() { 9121 // Ability has brought to foreground 9122 console.log("[Demo] MainAbility onForeground"); 9123 } 9124 onBackground() { 9125 // Ability has back to background 9126 console.log("[Demo] MainAbility onBackground"); 9127 } 9128 }; 9129 ``` 9130