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 5456**Example** 5457 5458 ```ts 5459 class MyParcelable { 5460 num: number; 5461 str: string; 5462 constructor(num, str) { 5463 this.num = num; 5464 this.str = str; 5465 } 5466 marshalling(messageSequence) { 5467 messageSequence.writeInt(this.num); 5468 messageSequence.writeString(this.str); 5469 return true; 5470 } 5471 unmarshalling(messageSequence) { 5472 this.num = messageSequence.readInt(); 5473 this.str = messageSequence.readString(); 5474 return true; 5475 } 5476 } 5477 let parcelable = new MyParcelable(1, "aaa"); 5478 let data = rpc.MessageSequence.create(); 5479 let result = data.writeParcelable(parcelable); 5480 console.log("RpcClient: writeParcelable is " + result); 5481 let ret = new MyParcelable(0, ""); 5482 let result2 = data.readParcelable(ret); 5483 console.log("RpcClient: readParcelable is " + result2); 5484 ``` 5485 5486### unmarshalling 5487 5488unmarshalling(dataIn: MessageSequence): boolean 5489 5490Unmarshals this **Parcelable** object from a **MessageSequence** object. 5491 5492**System capability**: SystemCapability.Communication.IPC.Core 5493 5494**Parameters** 5495 5496 | Name| Type | Mandatory| Description | 5497 | ------ | --------------- | ---- | ----------------------------------------------- | 5498 | dataIn | MessageSequence | Yes | **MessageSequence** object from which the **Parcelable** object is to be unmarshaled.| 5499 5500**Return value** 5501 5502 | Type | Description | 5503 | ------- | ---------------------------------------- | 5504 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5505 5506**Example** 5507 5508 ```ts 5509 class MyParcelable { 5510 num: number; 5511 str: string; 5512 constructor(num, str) { 5513 this.num = num; 5514 this.str = str; 5515 } 5516 marshalling(messageSequence) { 5517 messageSequence.writeInt(this.num); 5518 messageSequence.writeString(this.str); 5519 return true; 5520 } 5521 unmarshalling(messageSequence) { 5522 this.num = messageSequence.readInt(); 5523 this.str = messageSequence.readString(); 5524 return true; 5525 } 5526 } 5527 let parcelable = new MyParcelable(1, "aaa"); 5528 let data = rpc.MessageSequence.create(); 5529 let result = data.writeParcelable(parcelable); 5530 console.log("RpcClient: writeParcelable is " + result); 5531 let ret = new MyParcelable(0, ""); 5532 let result2 = data.readParcelable(ret); 5533 console.log("RpcClient: readParcelable is " + result2); 5534 ``` 5535 5536## Sequenceable<sup>(deprecated)</sup> 5537 5538>This class is no longer maintained since API version 9. You are advised to use the [Parcelable](#parcelable9). 5539 5540Writes objects of classes to a **MessageParcel** and reads them from the **MessageParcel** during IPC. 5541 5542### marshalling 5543 5544marshalling(dataOut: MessageParcel): boolean 5545 5546Marshals the sequenceable object into a **MessageParcel** object. 5547 5548**System capability**: SystemCapability.Communication.IPC.Core 5549 5550**Parameters** 5551 5552 | Name | Type | Mandatory| Description | 5553 | ------- | ----------------------------------------- | ---- | ----------------------------------------- | 5554 | dataOut | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object to which the sequenceable object is to be marshaled.| 5555 5556**Return value** 5557 5558 | Type | Description | 5559 | ------- | -------------------------------- | 5560 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5561 5562**Example** 5563 5564 ```ts 5565 class MySequenceable { 5566 num: number; 5567 str: string; 5568 constructor(num, str) { 5569 this.num = num; 5570 this.str = str; 5571 } 5572 marshalling(messageParcel) { 5573 messageParcel.writeInt(this.num); 5574 messageParcel.writeString(this.str); 5575 return true; 5576 } 5577 unmarshalling(messageParcel) { 5578 this.num = messageParcel.readInt(); 5579 this.str = messageParcel.readString(); 5580 return true; 5581 } 5582 } 5583 let sequenceable = new MySequenceable(1, "aaa"); 5584 let data = rpc.MessageParcel.create(); 5585 let result = data.writeSequenceable(sequenceable); 5586 console.log("RpcClient: writeSequenceable is " + result); 5587 let ret = new MySequenceable(0, ""); 5588 let result2 = data.readSequenceable(ret); 5589 console.log("RpcClient: readSequenceable is " + result2); 5590 ``` 5591 5592### unmarshalling 5593 5594unmarshalling(dataIn: MessageParcel): boolean 5595 5596Unmarshals this sequenceable object from a **MessageParcel** object. 5597 5598**System capability**: SystemCapability.Communication.IPC.Core 5599 5600**Parameters** 5601 5602 | Name| Type | Mandatory| Description | 5603 | ------ | ----------------------------------------- | ---- | --------------------------------------------- | 5604 | dataIn | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object in which the sequenceable object is to be unmarshaled.| 5605 5606**Return value** 5607 5608 | Type | Description | 5609 | ------- | ---------------------------------------- | 5610 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5611 5612**Example** 5613 5614 ```ts 5615 class MySequenceable { 5616 num: number; 5617 str: string; 5618 constructor(num, str) { 5619 this.num = num; 5620 this.str = str; 5621 } 5622 marshalling(messageParcel) { 5623 messageParcel.writeInt(this.num); 5624 messageParcel.writeString(this.str); 5625 return true; 5626 } 5627 unmarshalling(messageParcel) { 5628 this.num = messageParcel.readInt(); 5629 this.str = messageParcel.readString(); 5630 return true; 5631 } 5632 } 5633 let sequenceable = new MySequenceable(1, "aaa"); 5634 let data = rpc.MessageParcel.create(); 5635 let result = data.writeSequenceable(sequenceable); 5636 console.log("RpcClient: writeSequenceable is " + result); 5637 let ret = new MySequenceable(0, ""); 5638 let result2 = data.readSequenceable(ret); 5639 console.log("RpcClient: readSequenceable is " + result2); 5640 ``` 5641 5642## IRemoteBroker 5643 5644Provides the holder of a remote proxy object. 5645 5646### asObject 5647 5648asObject(): IRemoteObject 5649 5650Obtains a proxy or remote object. This API must be implemented by its derived classes. 5651 5652**System capability**: SystemCapability.Communication.IPC.Core 5653 5654**Return value** 5655 5656 | Type | Description | 5657 | ----- | ----- | 5658 | [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.| 5659 5660**Example** 5661 5662 ```ts 5663 class TestAbility extends rpc.RemoteObject { 5664 asObject() { 5665 return this; 5666 } 5667 } 5668 let remoteObject = new TestAbility("testObject").asObject(); 5669 ``` 5670 5671**Example** 5672 5673 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). 5674 5675 ```ts 5676 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 5677 // import FA from "@ohos.ability.featureAbility"; 5678 5679 let proxy; 5680 let connect = { 5681 onConnect: function(elementName, remoteProxy) { 5682 console.log("RpcClient: js onConnect called."); 5683 proxy = remoteProxy; 5684 }, 5685 onDisconnect: function(elementName) { 5686 console.log("RpcClient: onDisconnect"); 5687 }, 5688 onFailed: function() { 5689 console.log("RpcClient: onFailed"); 5690 } 5691 }; 5692 let want = { 5693 "bundleName": "com.ohos.server", 5694 "abilityName": "com.ohos.server.EntryAbility", 5695 }; 5696 5697 // Use this method to connect to the ability for the FA model. 5698 // FA.connectAbility(want,connect); 5699 5700 globalThis.context.connectServiceExtensionAbility(want, connect); 5701 ``` 5702 5703 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. 5704 5705 ```ts 5706 class TestProxy { 5707 remote: rpc.RemoteObject; 5708 constructor(remote) { 5709 this.remote = remote; 5710 } 5711 asObject() { 5712 return this.remote; 5713 } 5714 } 5715 let iRemoteObject = new TestProxy(proxy).asObject(); 5716 ``` 5717 5718## DeathRecipient 5719 5720Subscribes 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. 5721 5722### onRemoteDied 5723 5724onRemoteDied(): void 5725 5726Called to perform subsequent operations when a death notification of the remote object is received. 5727 5728**System capability**: SystemCapability.Communication.IPC.Core 5729 5730**Example** 5731 5732 ```ts 5733 class MyDeathRecipient { 5734 onRemoteDied() { 5735 console.log("server died"); 5736 } 5737 } 5738 ``` 5739 5740## RequestResult<sup>9+</sup> 5741 5742Defines the response to the request. 5743 5744**System capability**: SystemCapability.Communication.IPC.Core 5745 5746 | Name | Type | Readable| Writable| Description | 5747 | ------- | --------------- | ---- | ---- |-------------------------------------- | 5748 | errCode | number | Yes | No | Error code. | 5749 | code | number | Yes | No | Message code. | 5750 | data | MessageSequence | Yes | No | **MessageSequence** object sent to the remote process.| 5751 | reply | MessageSequence | Yes | No | **MessageSequence** object returned by the remote process. | 5752 5753## SendRequestResult<sup>8+(deprecated)</sup> 5754 5755>This API is no longer maintained since API version 9. You are advised to use [RequestResult](#requestresult9). 5756 5757Defines the response to the request. 5758 5759**System capability**: SystemCapability.Communication.IPC.Core 5760 5761 | Name | Type | Readable| Writable| Description | 5762 | ------- | ------------- | ---- | ---- | ----------------------------------- | 5763 | errCode | number | Yes | No | Error code. | 5764 | code | number | Yes | No | Message code. | 5765 | data | MessageParcel | Yes | No | **MessageParcel** object sent to the remote process.| 5766 | reply | MessageParcel | Yes | No | **MessageParcel** object returned by the remote process. | 5767 5768## IRemoteObject 5769 5770Provides methods to query of obtain interface descriptors, add or delete death notifications, dump object status to specific files, and send messages. 5771 5772### getLocalInterface<sup>9+</sup> 5773 5774getLocalInterface(descriptor: string): IRemoteBroker 5775 5776Obtains the interface descriptor. 5777 5778**System capability**: SystemCapability.Communication.IPC.Core 5779 5780**Parameters** 5781 5782 | Name | Type | Mandatory| Description | 5783 | ---------- | ------ | ---- | -------------------- | 5784 | descriptor | string | Yes | Interface descriptor.| 5785 5786**Return value** 5787 5788 | Type | Description | 5789 | ------------- | --------------------------------------------- | 5790 | IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.| 5791 5792### queryLocalInterface<sup>(deprecated)</sup> 5793 5794>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9). 5795 5796queryLocalInterface(descriptor: string): IRemoteBroker 5797 5798Queries the interface descriptor. 5799 5800**System capability**: SystemCapability.Communication.IPC.Core 5801 5802**Parameters** 5803 5804 | Name | Type | Mandatory| Description | 5805 | ---------- | ------ | ---- | -------------------- | 5806 | descriptor | string | Yes | Interface descriptor.| 5807 5808**Return value** 5809 5810 | Type | Description | 5811 | ------------- | --------------------------------------------- | 5812 | IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.| 5813 5814### sendRequest<sup>(deprecated)</sup> 5815 5816>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 5817 5818sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 5819 5820Sends 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. 5821 5822**System capability**: SystemCapability.Communication.IPC.Core 5823 5824**Parameters** 5825 5826 | Name | Type | Mandatory| Description | 5827 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 5828 | 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.| 5829 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 5830 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 5831 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 5832 5833**Return value** 5834 5835 | Type | Description | 5836 | ------- | -------------------------------- | 5837 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 5838 5839 5840### sendRequest<sup>8+(deprecated)</sup> 5841 5842>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 5843 5844sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 5845 5846Sends 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. 5847 5848**System capability**: SystemCapability.Communication.IPC.Core 5849 5850**Parameters** 5851 5852 | Name | Type | Mandatory| Description | 5853 | ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 5854 | 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.| 5855 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 5856 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 5857 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 5858 5859**Return value** 5860 5861 | Type | Description | 5862 | -------------------------------- | --------------------------------------------- | 5863 | Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 5864 5865 5866### sendMessageRequest<sup>9+</sup> 5867 5868sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 5869 5870Sends 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. 5871 5872**System capability**: SystemCapability.Communication.IPC.Core 5873 5874**Parameters** 5875 5876 | Name | Type | Mandatory| Description | 5877 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 5878 | 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.| 5879 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 5880 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 5881 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 5882 5883**Return value** 5884 5885 | Type | Description | 5886 | ---------------------------- | ----------------------------------------- | 5887 | Promise<RequestResult> | Promise used to return the **requestResult** object.| 5888 5889 5890### sendMessageRequest<sup>9+</sup> 5891 5892sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 5893 5894Sends 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. 5895 5896**System capability**: SystemCapability.Communication.IPC.Core 5897 5898**Parameters** 5899 5900 | Name | Type | Mandatory| Description | 5901 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 5902 | 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.| 5903 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 5904 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 5905 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 5906 | callback | AsyncCallback<RequestResult> | Yes | Callback for receiving the sending result. | 5907 5908### sendRequest<sup>8+(deprecated)</sup> 5909 5910>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 5911 5912sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 5913 5914Sends 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. 5915 5916**System capability**: SystemCapability.Communication.IPC.Core 5917 5918**Parameters** 5919 5920 | Name | Type | Mandatory| Description | 5921 | -------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 5922 | 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.| 5923 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 5924 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 5925 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 5926 | callback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. | 5927 5928### registerDeathRecipient<sup>9+</sup> 5929 5930registerDeathRecipient(recipient: DeathRecipient, flags: number): void 5931 5932Registers 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. 5933 5934**System capability**: SystemCapability.Communication.IPC.Core 5935 5936**Parameters** 5937 5938 | Name | Type | Mandatory| Description | 5939 | --------- | --------------------------------- | ---- | -------------- | 5940 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 5941 | flags | number | Yes | Flag of the death notification.| 5942 5943**Error codes** 5944 5945For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 5946 5947 | ID| Error Message| 5948 | -------- | -------- | 5949 | 1900008 | proxy or remote object is invalid | 5950 5951### addDeathrecipient<sup>(deprecated)</sup> 5952 5953>This API is no longer maintained since API version 9. You are advised to use [registerDeathRecipient](#registerdeathrecipient9). 5954 5955addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 5956 5957Adds 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. 5958 5959**System capability**: SystemCapability.Communication.IPC.Core 5960 5961**Parameters** 5962 5963 | Name | Type | Mandatory| Description | 5964 | --------- | --------------------------------- | ---- | -------------- | 5965 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add.| 5966 | flags | number | Yes | Flag of the death notification.| 5967 5968**Return value** 5969 5970 | Type | Description | 5971 | ------- | ---------------------------------------- | 5972 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 5973 5974 5975### unregisterDeathRecipient<sup>9+</sup> 5976 5977unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 5978 5979Unregisters the callback used to receive death notifications of the remote object. 5980 5981**System capability**: SystemCapability.Communication.IPC.Core 5982 5983**Parameters** 5984 5985 | Name | Type | Mandatory| Description | 5986 | --------- | --------------------------------- | ---- | -------------- | 5987 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 5988 | flags | number | Yes | Flag of the death notification.| 5989 5990**Error codes** 5991 5992For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 5993 5994 | ID| Error Message| 5995 | -------- | -------- | 5996 | 1900008 | proxy or remote object is invalid | 5997 5998### removeDeathRecipient<sup>(deprecated)</sup> 5999 6000>This API is no longer maintained since API version 9. You are advised to use [unregisterDeathRecipient](#unregisterdeathrecipient9). 6001 6002removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6003 6004Removes the callback used to receive death notifications of the remote object. 6005 6006**System capability**: SystemCapability.Communication.IPC.Core 6007 6008**Parameters** 6009 6010 | Name | Type | Mandatory| Description | 6011 | --------- | --------------------------------- | ---- | -------------- | 6012 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove.| 6013 | flags | number | Yes | Flag of the death notification.| 6014 6015**Return value** 6016 6017 | Type | Description | 6018 | ------- | -----------------------------------------| 6019 | boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 6020 6021### getDescriptor<sup>9+</sup> 6022 6023getDescriptor(): string 6024 6025Obtains the interface descriptor (which is a string) of this object. 6026 6027**System capability**: SystemCapability.Communication.IPC.Core 6028 6029**Return value** 6030 6031 | Type | Description | 6032 | ------ | ---------------- | 6033 | string | Interface descriptor obtained.| 6034 6035**Error codes** 6036 6037For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6038 6039 | ID| Error Message| 6040 | -------- | -------- | 6041 | 1900008 | proxy or remote object is invalid | 6042 6043 6044### getInterfaceDescriptor<sup>(deprecated)</sup> 6045 6046>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9). 6047 6048getInterfaceDescriptor(): string 6049 6050Obtains the interface descriptor (which is a string) of this object. 6051 6052**System capability**: SystemCapability.Communication.IPC.Core 6053 6054**Return value** 6055 6056 | Type | Description | 6057 | ------ | ---------------- | 6058 | string | Interface descriptor obtained.| 6059 6060 6061### isObjectDead 6062 6063isObjectDead(): boolean 6064 6065Checks whether this object is dead. 6066 6067**System capability**: SystemCapability.Communication.IPC.Core 6068 6069**Return value** 6070 6071 | Type | Description | 6072 | ------- | ---------------------------------- | 6073 | boolean | Returns **true** if the object is dead; returns **false** otherwise.| 6074 6075 6076## RemoteProxy 6077 6078Provides APIs to implement **IRemoteObject**. 6079 6080**System capability**: SystemCapability.Communication.IPC.Core 6081 6082| Name | Value | Description | 6083| --------------------- | ----------------------- | --------------------------------- | 6084| PING_TRANSACTION | 1599098439 (0x5f504e47) | Internal instruction code used to test whether the IPC service is normal.| 6085| DUMP_TRANSACTION | 1598311760 (0x5f444d50) | Internal instruction code used to obtain the internal status of the binder. | 6086| INTERFACE_TRANSACTION | 1598968902 (0x5f4e5446) | Internal instruction code used to obtain the remote interface token. | 6087| MIN_TRANSACTION_ID | 1 (0x00000001) | Minimum valid instruction code. | 6088| MAX_TRANSACTION_ID | 16777215 (0x00FFFFFF) | Maximum valid instruction code. | 6089 6090### sendRequest<sup>(deprecated)</sup> 6091 6092>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6093 6094sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6095 6096Sends 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. 6097 6098**System capability**: SystemCapability.Communication.IPC.Core 6099 6100**Parameters** 6101 6102 | Name | Type | Mandatory| Description | 6103 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6104 | 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.| 6105 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6106 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6107 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6108 6109**Return value** 6110 6111 | Type | Description | 6112 | ------- | ---------------------------------| 6113 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 6114 6115**Example** 6116 6117 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). 6118 6119 ```ts 6120 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6121 // import FA from "@ohos.ability.featureAbility"; 6122 6123 let proxy; 6124 let connect = { 6125 onConnect: function(elementName, remoteProxy) { 6126 console.log("RpcClient: js onConnect called."); 6127 proxy = remoteProxy; 6128 }, 6129 onDisconnect: function(elementName) { 6130 console.log("RpcClient: onDisconnect"); 6131 }, 6132 onFailed: function() { 6133 console.log("RpcClient: onFailed"); 6134 } 6135 }; 6136 let want = { 6137 "bundleName": "com.ohos.server", 6138 "abilityName": "com.ohos.server.EntryAbility", 6139 }; 6140 6141 // Use this method to connect to the ability for the FA model. 6142 // FA.connectAbility(want,connect); 6143 6144 globalThis.context.connectServiceExtensionAbility(want, connect); 6145 ``` 6146 6147 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. 6148 6149 ```ts 6150 let option = new rpc.MessageOption(); 6151 let data = rpc.MessageParcel.create(); 6152 let reply = rpc.MessageParcel.create(); 6153 data.writeInt(1); 6154 data.writeString("hello"); 6155 let ret: boolean = proxy.sendRequest(1, data, reply, option); 6156 if (ret) { 6157 console.log("sendRequest got result"); 6158 let msg = reply.readString(); 6159 console.log("RPCTest: reply msg: " + msg); 6160 } else { 6161 console.log("RPCTest: sendRequest failed"); 6162 } 6163 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6164 data.reclaim(); 6165 reply.reclaim(); 6166 ``` 6167 6168### sendMessageRequest<sup>9+</sup> 6169 6170sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6171 6172Sends 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. 6173 6174**System capability**: SystemCapability.Communication.IPC.Core 6175 6176**Parameters** 6177 6178 | Name | Type | Mandatory| Description | 6179 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6180 | 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.| 6181 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6182 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6183 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6184 6185**Return value** 6186 6187 | Type | Description | 6188 | ---------------------------- | ----------------------------------------- | 6189 | Promise<RequestResult> | Promise used to return the **requestResult** object.| 6190 6191**Example** 6192 6193 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). 6194 6195 ```ts 6196 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6197 // import FA from "@ohos.ability.featureAbility"; 6198 6199 let proxy; 6200 let connect = { 6201 onConnect: function(elementName, remoteProxy) { 6202 console.log("RpcClient: js onConnect called."); 6203 proxy = remoteProxy; 6204 }, 6205 onDisconnect: function(elementName) { 6206 console.log("RpcClient: onDisconnect"); 6207 }, 6208 onFailed: function() { 6209 console.log("RpcClient: onFailed"); 6210 } 6211 }; 6212 let want = { 6213 "bundleName": "com.ohos.server", 6214 "abilityName": "com.ohos.server.EntryAbility", 6215 }; 6216 6217 // Use this method to connect to the ability for the FA model. 6218 // FA.connectAbility(want,connect); 6219 6220 globalThis.context.connectServiceExtensionAbility(want, connect); 6221 ``` 6222 6223 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. 6224 6225 ```ts 6226 let option = new rpc.MessageOption(); 6227 let data = rpc.MessageSequence.create(); 6228 let reply = rpc.MessageSequence.create(); 6229 data.writeInt(1); 6230 data.writeString("hello"); 6231 proxy.sendMessageRequest(1, data, reply, option) 6232 .then(function(result) { 6233 if (result.errCode === 0) { 6234 console.log("sendMessageRequest got result"); 6235 result.reply.readException(); 6236 let msg = result.reply.readString(); 6237 console.log("RPCTest: reply msg: " + msg); 6238 } else { 6239 console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode); 6240 } 6241 }).catch(function(e) { 6242 console.log("RPCTest: sendMessageRequest got exception: " + e.message); 6243 }).finally (() => { 6244 console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); 6245 data.reclaim(); 6246 reply.reclaim(); 6247 }); 6248 ``` 6249 6250### sendRequest<sup>8+(deprecated)</sup> 6251 6252>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6253 6254sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6255 6256Sends 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. 6257 6258**System capability**: SystemCapability.Communication.IPC.Core 6259 6260**Parameters** 6261 6262 | Name | Type | Mandatory| Description | 6263 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6264 | 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.| 6265 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6266 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6267 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6268 6269**Return value** 6270 6271 | Type | Description | 6272 | -------------------------------- | --------------------------------------------- | 6273 | Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 6274 6275**Example** 6276 6277 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). 6278 6279 ```ts 6280 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6281 // import FA from "@ohos.ability.featureAbility"; 6282 6283 let proxy; 6284 let connect = { 6285 onConnect: function(elementName, remoteProxy) { 6286 console.log("RpcClient: js onConnect called."); 6287 proxy = remoteProxy; 6288 }, 6289 onDisconnect: function(elementName) { 6290 console.log("RpcClient: onDisconnect"); 6291 }, 6292 onFailed: function() { 6293 console.log("RpcClient: onFailed"); 6294 } 6295 }; 6296 let want = { 6297 "bundleName": "com.ohos.server", 6298 "abilityName": "com.ohos.server.EntryAbility", 6299 }; 6300 6301 // Use this method to connect to the ability for the FA model. 6302 // FA.connectAbility(want,connect); 6303 6304 globalThis.context.connectServiceExtensionAbility(want, connect); 6305 ``` 6306 6307 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. 6308 6309 ```ts 6310 let option = new rpc.MessageOption(); 6311 let data = rpc.MessageParcel.create(); 6312 let reply = rpc.MessageParcel.create(); 6313 data.writeInt(1); 6314 data.writeString("hello"); 6315 proxy.sendRequest(1, data, reply, option) 6316 .then(function(result) { 6317 if (result.errCode === 0) { 6318 console.log("sendRequest got result"); 6319 result.reply.readException(); 6320 let msg = result.reply.readString(); 6321 console.log("RPCTest: reply msg: " + msg); 6322 } else { 6323 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 6324 } 6325 }).catch(function(e) { 6326 console.log("RPCTest: sendRequest got exception: " + e.message); 6327 }).finally (() => { 6328 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6329 data.reclaim(); 6330 reply.reclaim(); 6331 }); 6332 ``` 6333 6334### sendMessageRequest<sup>9+</sup> 6335 6336sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6337 6338Sends 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. 6339 6340**System capability**: SystemCapability.Communication.IPC.Core 6341 6342**Parameters** 6343 6344 | Name | Type | Mandatory| Description | 6345 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6346 | 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.| 6347 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6348 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6349 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6350 | callback | AsyncCallback<RequestResult> | Yes | Callback for receiving the sending result. | 6351 6352**Example** 6353 6354 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). 6355 6356 ```ts 6357 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6358 // import FA from "@ohos.ability.featureAbility"; 6359 6360 let proxy; 6361 let connect = { 6362 onConnect: function(elementName, remoteProxy) { 6363 console.log("RpcClient: js onConnect called."); 6364 proxy = remoteProxy; 6365 }, 6366 onDisconnect: function(elementName) { 6367 console.log("RpcClient: onDisconnect"); 6368 }, 6369 onFailed: function() { 6370 console.log("RpcClient: onFailed"); 6371 } 6372 }; 6373 let want = { 6374 "bundleName": "com.ohos.server", 6375 "abilityName": "com.ohos.server.EntryAbility", 6376 }; 6377 function sendRequestCallback(result) { 6378 if (result.errCode === 0) { 6379 console.log("sendRequest got result"); 6380 result.reply.readException(); 6381 let msg = result.reply.readString(); 6382 console.log("RPCTest: reply msg: " + msg); 6383 } else { 6384 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 6385 } 6386 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6387 result.data.reclaim(); 6388 result.reply.reclaim(); 6389 } 6390 6391 // Use this method to connect to the ability for the FA model. 6392 // FA.connectAbility(want,connect); 6393 6394 globalThis.context.connectServiceExtensionAbility(want, connect); 6395 ``` 6396 6397 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. 6398 6399 ```ts 6400 let option = new rpc.MessageOption(); 6401 let data = rpc.MessageSequence.create(); 6402 let reply = rpc.MessageSequence.create(); 6403 data.writeInt(1); 6404 data.writeString("hello"); 6405 try { 6406 proxy.sendMessageRequest(1, data, reply, option, sendRequestCallback); 6407 } catch(error) { 6408 console.info("rpc send sequence request fail, errorCode " + error.code); 6409 console.info("rpc send sequence request fail, errorMessage " + error.message); 6410 } 6411 ``` 6412 6413### sendRequest<sup>8+(deprecated)</sup> 6414 6415>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6416 6417sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6418 6419Sends 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. 6420 6421**System capability**: SystemCapability.Communication.IPC.Core 6422 6423**Parameters** 6424 6425 | Name | Type | Mandatory| Description | 6426 | -------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6427 | 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.| 6428 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6429 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6430 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6431 | callback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. | 6432 6433**Example** 6434 6435 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). 6436 6437 ```ts 6438 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6439 // import FA from "@ohos.ability.featureAbility"; 6440 6441 let proxy; 6442 let connect = { 6443 onConnect: function(elementName, remoteProxy) { 6444 console.log("RpcClient: js onConnect called."); 6445 proxy = remoteProxy; 6446 }, 6447 onDisconnect: function(elementName) { 6448 console.log("RpcClient: onDisconnect"); 6449 }, 6450 onFailed: function() { 6451 console.log("RpcClient: onFailed"); 6452 } 6453 }; 6454 let want = { 6455 "bundleName": "com.ohos.server", 6456 "abilityName": "com.ohos.server.EntryAbility", 6457 }; 6458 function sendRequestCallback(result) { 6459 if (result.errCode === 0) { 6460 console.log("sendRequest got result"); 6461 result.reply.readException(); 6462 let msg = result.reply.readString(); 6463 console.log("RPCTest: reply msg: " + msg); 6464 } else { 6465 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 6466 } 6467 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6468 result.data.reclaim(); 6469 result.reply.reclaim(); 6470 } 6471 6472 // Use this method to connect to the ability for the FA model. 6473 // FA.connectAbility(want,connect); 6474 6475 globalThis.context.connectServiceExtensionAbility(want, connect); 6476 ``` 6477 6478 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. 6479 6480 ```ts 6481 let option = new rpc.MessageOption(); 6482 let data = rpc.MessageParcel.create(); 6483 let reply = rpc.MessageParcel.create(); 6484 data.writeInt(1); 6485 data.writeString("hello"); 6486 proxy.sendRequest(1, data, reply, option, sendRequestCallback); 6487 ``` 6488 6489### getLocalInterface<sup>9+</sup> 6490 6491getLocalInterface(interface: string): IRemoteBroker 6492 6493Obtains the **LocalInterface** object of an interface token. 6494 6495**System capability**: SystemCapability.Communication.IPC.Core 6496 6497**Parameters** 6498 6499 | Name | Type | Mandatory| Description | 6500 | --------- | ------ | ---- | ---------------------- | 6501 | interface | string | Yes | Interface descriptor.| 6502 6503**Return value** 6504 6505 | Type | Description | 6506 | ------------- | ------------------------------------------ | 6507 | IRemoteBroker | Returns **Null** by default, which indicates a proxy interface.| 6508 6509**Error codes** 6510 6511For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6512 6513 | ID| Error Message| 6514 | -------- | -------- | 6515 | 1900006 | only remote object permitted | 6516 6517**Example** 6518 6519 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). 6520 6521 ```ts 6522 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6523 // import FA from "@ohos.ability.featureAbility"; 6524 6525 let proxy; 6526 let connect = { 6527 onConnect: function(elementName, remoteProxy) { 6528 console.log("RpcClient: js onConnect called."); 6529 proxy = remoteProxy; 6530 }, 6531 onDisconnect: function(elementName) { 6532 console.log("RpcClient: onDisconnect"); 6533 }, 6534 onFailed: function() { 6535 console.log("RpcClient: onFailed"); 6536 } 6537 }; 6538 let want = { 6539 "bundleName": "com.ohos.server", 6540 "abilityName": "com.ohos.server.EntryAbility", 6541 }; 6542 6543 // Use this method to connect to the ability for the FA model. 6544 // FA.connectAbility(want,connect); 6545 6546 globalThis.context.connectServiceExtensionAbility(want, connect); 6547 ``` 6548 6549 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. 6550 6551 ```ts 6552 try { 6553 let broker = proxy.getLocalInterface("testObject"); 6554 console.log("RpcClient: getLocalInterface is " + broker); 6555 } catch(error) { 6556 console.info("rpc get local interface fail, errorCode " + error.code); 6557 console.info("rpc get local interface fail, errorMessage " + error.message); 6558 } 6559 ``` 6560 6561### queryLocalInterface<sup>(deprecated)</sup> 6562 6563>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9). 6564 6565queryLocalInterface(interface: string): IRemoteBroker 6566 6567Obtains the **LocalInterface** object of an interface token. 6568 6569**System capability**: SystemCapability.Communication.IPC.Core 6570 6571**Parameters** 6572 6573 | Name | Type | Mandatory| Description | 6574 | --------- | ------ | ---- | ---------------------- | 6575 | interface | string | Yes | Interface descriptor.| 6576 6577**Return value** 6578 6579 | Type | Description | 6580 | ------------- | ------------------------------------------ | 6581 | IRemoteBroker | Returns **Null** by default, which indicates a proxy interface.| 6582 6583**Example** 6584 6585 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). 6586 6587 ```ts 6588 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6589 // import FA from "@ohos.ability.featureAbility"; 6590 6591 let proxy; 6592 let connect = { 6593 onConnect: function(elementName, remoteProxy) { 6594 console.log("RpcClient: js onConnect called."); 6595 proxy = remoteProxy; 6596 }, 6597 onDisconnect: function(elementName) { 6598 console.log("RpcClient: onDisconnect"); 6599 }, 6600 onFailed: function() { 6601 console.log("RpcClient: onFailed"); 6602 } 6603 }; 6604 let want = { 6605 "bundleName": "com.ohos.server", 6606 "abilityName": "com.ohos.server.EntryAbility", 6607 }; 6608 6609 // Use this method to connect to the ability for the FA model. 6610 // FA.connectAbility(want,connect); 6611 6612 globalThis.context.connectServiceExtensionAbility(want, connect); 6613 ``` 6614 6615 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. 6616 6617 ```ts 6618 let broker = proxy.queryLocalInterface("testObject"); 6619 console.log("RpcClient: queryLocalInterface is " + broker); 6620 ``` 6621 6622### registerDeathRecipient<sup>9+</sup> 6623 6624registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6625 6626Registers 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. 6627 6628**System capability**: SystemCapability.Communication.IPC.Core 6629 6630**Parameters** 6631 6632 | Name | Type | Mandatory| Description | 6633 | --------- | --------------------------------- | ---- | -------------- | 6634 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 6635 | flags | number | Yes | Flag of the death notification.| 6636 6637**Error codes** 6638 6639For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6640 6641 | ID| Error Message| 6642 | -------- | -------- | 6643 | 1900008 | proxy or remote object is invalid | 6644 6645**Example** 6646 6647 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). 6648 6649 ```ts 6650 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6651 // import FA from "@ohos.ability.featureAbility"; 6652 6653 let proxy; 6654 let connect = { 6655 onConnect: function(elementName, remoteProxy) { 6656 console.log("RpcClient: js onConnect called."); 6657 proxy = remoteProxy; 6658 }, 6659 onDisconnect: function(elementName) { 6660 console.log("RpcClient: onDisconnect"); 6661 }, 6662 onFailed: function() { 6663 console.log("RpcClient: onFailed"); 6664 } 6665 }; 6666 let want = { 6667 "bundleName": "com.ohos.server", 6668 "abilityName": "com.ohos.server.EntryAbility", 6669 }; 6670 6671 // Use this method to connect to the ability for the FA model. 6672 // FA.connectAbility(want,connect); 6673 6674 globalThis.context.connectServiceExtensionAbility(want, connect); 6675 ``` 6676 6677 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. 6678 6679 ```ts 6680 class MyDeathRecipient { 6681 onRemoteDied() { 6682 console.log("server died"); 6683 } 6684 } 6685 let deathRecipient = new MyDeathRecipient(); 6686 try { 6687 proxy.registerDeathRecipient(deathRecipient, 0); 6688 } catch(error) { 6689 console.info("proxy register deathRecipient fail, errorCode " + error.code); 6690 console.info("proxy register deathRecipient fail, errorMessage " + error.message); 6691 } 6692 ``` 6693 6694### addDeathRecipient<sup>(deprecated)</sup> 6695 6696>This API is no longer maintained since API version 9. You are advised to use [registerDeathRecipient](#registerdeathrecipient9). 6697 6698addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6699 6700Adds a callback for receiving the death notifications of the remote object, including the death notifications of the remote proxy. 6701 6702**System capability**: SystemCapability.Communication.IPC.Core 6703 6704**Parameters** 6705 6706 | Name | Type | Mandatory| Description | 6707 | --------- | --------------------------------- | ---- | --------------------------------- | 6708 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add. | 6709 | flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 6710 6711**Return value** 6712 6713 | Type | Description | 6714 | ------- | ---------------------------------------- | 6715 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 6716 6717**Example** 6718 6719 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). 6720 6721 ```ts 6722 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6723 // import FA from "@ohos.ability.featureAbility"; 6724 6725 let proxy; 6726 let connect = { 6727 onConnect: function(elementName, remoteProxy) { 6728 console.log("RpcClient: js onConnect called."); 6729 proxy = remoteProxy; 6730 }, 6731 onDisconnect: function(elementName) { 6732 console.log("RpcClient: onDisconnect"); 6733 }, 6734 onFailed: function() { 6735 console.log("RpcClient: onFailed"); 6736 } 6737 }; 6738 let want = { 6739 "bundleName": "com.ohos.server", 6740 "abilityName": "com.ohos.server.EntryAbility", 6741 }; 6742 6743 // Use this method to connect to the ability for the FA model. 6744 // FA.connectAbility(want,connect); 6745 6746 globalThis.context.connectServiceExtensionAbility(want, connect); 6747 ``` 6748 6749 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **addDeathRecipient()** of the proxy object is called to add a callback for receiving the death notification of the remove object. 6750 6751 ```ts 6752 class MyDeathRecipient { 6753 onRemoteDied() { 6754 console.log("server died"); 6755 } 6756 } 6757 let deathRecipient = new MyDeathRecipient(); 6758 proxy.addDeathRecipient(deathRecipient, 0); 6759 ``` 6760 6761### unregisterDeathRecipient<sup>9+</sup> 6762 6763unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 6764 6765Unregisters the callback used to receive death notifications of the remote object. 6766 6767**System capability**: SystemCapability.Communication.IPC.Core 6768 6769**Parameters** 6770 6771 | Name | Type | Mandatory| Description | 6772 | --------- | --------------------------------- | ---- | -------------- | 6773 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 6774 | flags | number | Yes | Flag of the death notification.| 6775 6776**Error codes** 6777 6778For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6779 6780 | ID| Error Message| 6781 | -------- | -------- | 6782 | 1900008 | proxy or remote object is invalid | 6783 6784**Example** 6785 6786 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). 6787 6788 ```ts 6789 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6790 // import FA from "@ohos.ability.featureAbility"; 6791 6792 let proxy; 6793 let connect = { 6794 onConnect: function(elementName, remoteProxy) { 6795 console.log("RpcClient: js onConnect called."); 6796 proxy = remoteProxy; 6797 }, 6798 onDisconnect: function(elementName) { 6799 console.log("RpcClient: onDisconnect"); 6800 }, 6801 onFailed: function() { 6802 console.log("RpcClient: onFailed"); 6803 } 6804 }; 6805 let want = { 6806 "bundleName": "com.ohos.server", 6807 "abilityName": "com.ohos.server.EntryAbility", 6808 }; 6809 6810 // Use this method to connect to the ability for the FA model. 6811 // FA.connectAbility(want,connect); 6812 6813 globalThis.context.connectServiceExtensionAbility(want, connect); 6814 ``` 6815 6816 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. 6817 6818 ```ts 6819 class MyDeathRecipient { 6820 onRemoteDied() { 6821 console.log("server died"); 6822 } 6823 } 6824 let deathRecipient = new MyDeathRecipient(); 6825 try { 6826 proxy.registerDeathRecipient(deathRecipient, 0); 6827 proxy.unregisterDeathRecipient(deathRecipient, 0); 6828 } catch(error) { 6829 console.info("proxy register deathRecipient fail, errorCode " + error.code); 6830 console.info("proxy register deathRecipient fail, errorMessage " + error.message); 6831 } 6832 ``` 6833 6834### removeDeathRecipient<sup>(deprecated)</sup> 6835 6836>This API is no longer maintained since API version 9. You are advised to use [unregisterDeathRecipient](#unregisterdeathrecipient9). 6837 6838removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6839 6840Removes the callback used to receive death notifications of the remote object. 6841 6842**System capability**: SystemCapability.Communication.IPC.Core 6843 6844**Parameters** 6845 6846 | Name | Type | Mandatory| Description | 6847 | --------- | --------------------------------- | ---- | --------------------------------- | 6848 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove. | 6849 | flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 6850 6851**Return value** 6852 6853 | Type | Description | 6854 | ------- | ---------------------------------------- | 6855 | boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 6856 6857**Example** 6858 6859 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). 6860 6861 ```ts 6862 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6863 // import FA from "@ohos.ability.featureAbility"; 6864 6865 let proxy; 6866 let connect = { 6867 onConnect: function(elementName, remoteProxy) { 6868 console.log("RpcClient: js onConnect called."); 6869 proxy = remoteProxy; 6870 }, 6871 onDisconnect: function(elementName) { 6872 console.log("RpcClient: onDisconnect"); 6873 }, 6874 onFailed: function() { 6875 console.log("RpcClient: onFailed"); 6876 } 6877 }; 6878 let want = { 6879 "bundleName": "com.ohos.server", 6880 "abilityName": "com.ohos.server.EntryAbility", 6881 }; 6882 6883 // Use this method to connect to the ability for the FA model. 6884 // FA.connectAbility(want,connect); 6885 6886 globalThis.context.connectServiceExtensionAbility(want, connect); 6887 ``` 6888 6889 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. 6890 6891 ```ts 6892 class MyDeathRecipient { 6893 onRemoteDied() { 6894 console.log("server died"); 6895 } 6896 } 6897 let deathRecipient = new MyDeathRecipient(); 6898 proxy.addDeathRecipient(deathRecipient, 0); 6899 proxy.removeDeathRecipient(deathRecipient, 0); 6900 ``` 6901 6902### getDescriptor<sup>9+</sup> 6903 6904getDescriptor(): string 6905 6906Obtains the interface descriptor (which is a string) of this proxy object. 6907 6908**System capability**: SystemCapability.Communication.IPC.Core 6909 6910**Return value** 6911 6912 | Type | Description | 6913 | ------ | ---------------- | 6914 | string | Interface descriptor obtained.| 6915 6916**Error codes** 6917 6918For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6919 6920 | ID| Error Message| 6921 | -------- | -------- | 6922 | 1900008 | proxy or remote object is invalid | 6923 | 1900007 | communication failed | 6924 6925**Example** 6926 6927 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). 6928 6929 ```ts 6930 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6931 // import FA from "@ohos.ability.featureAbility"; 6932 6933 let proxy; 6934 let connect = { 6935 onConnect: function(elementName, remoteProxy) { 6936 console.log("RpcClient: js onConnect called."); 6937 proxy = remoteProxy; 6938 }, 6939 onDisconnect: function(elementName) { 6940 console.log("RpcClient: onDisconnect"); 6941 }, 6942 onFailed: function() { 6943 console.log("RpcClient: onFailed"); 6944 } 6945 }; 6946 let want = { 6947 "bundleName": "com.ohos.server", 6948 "abilityName": "com.ohos.server.EntryAbility", 6949 }; 6950 6951 // Use this method to connect to the ability for the FA model. 6952 // FA.connectAbility(want,connect); 6953 6954 globalThis.context.connectServiceExtensionAbility(want, connect); 6955 ``` 6956 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. 6957 6958 ```ts 6959 try { 6960 let descriptor = proxy.getDescriptor(); 6961 console.log("RpcClient: descriptor is " + descriptor); 6962 } catch(error) { 6963 console.info("rpc get interface descriptor fail, errorCode " + error.code); 6964 console.info("rpc get interface descriptor fail, errorMessage " + error.message); 6965 } 6966 ``` 6967 6968### getInterfaceDescriptor<sup>(deprecated)</sup> 6969 6970>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9). 6971 6972getInterfaceDescriptor(): string 6973 6974Obtains the interface descriptor of this proxy object. 6975 6976**System capability**: SystemCapability.Communication.IPC.Core 6977 6978**Return value** 6979 6980 | Type | Description | 6981 | ------ | ------------------ | 6982 | string | Interface descriptor obtained.| 6983 6984**Example** 6985 6986 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). 6987 6988 ```ts 6989 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6990 // import FA from "@ohos.ability.featureAbility"; 6991 6992 let proxy; 6993 let connect = { 6994 onConnect: function(elementName, remoteProxy) { 6995 console.log("RpcClient: js onConnect called."); 6996 proxy = remoteProxy; 6997 }, 6998 onDisconnect: function(elementName) { 6999 console.log("RpcClient: onDisconnect"); 7000 }, 7001 onFailed: function() { 7002 console.log("RpcClient: onFailed"); 7003 } 7004 }; 7005 let want = { 7006 "bundleName": "com.ohos.server", 7007 "abilityName": "com.ohos.server.EntryAbility", 7008 }; 7009 7010 // Use this method to connect to the ability for the FA model. 7011 // FA.connectAbility(want,connect); 7012 7013 globalThis.context.connectServiceExtensionAbility(want, connect); 7014 ``` 7015 7016 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. 7017 7018 ```ts 7019 let descriptor = proxy.getInterfaceDescriptor(); 7020 console.log("RpcClient: descriptor is " + descriptor); 7021 ``` 7022 7023### isObjectDead 7024 7025isObjectDead(): boolean 7026 7027Checks whether the **RemoteObject** is dead. 7028 7029**System capability**: SystemCapability.Communication.IPC.Core 7030 7031**Return value** 7032 7033 | Type | Description | 7034 | ------- | ------------------------------------------------- | 7035 | boolean | Returns **true** if the **RemoteObject** is dead; returns **false** otherwise.| 7036 7037**Example** 7038 7039 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). 7040 7041 ```ts 7042 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7043 // import FA from "@ohos.ability.featureAbility"; 7044 7045 let proxy; 7046 let connect = { 7047 onConnect: function(elementName, remoteProxy) { 7048 console.log("RpcClient: js onConnect called."); 7049 proxy = remoteProxy; 7050 }, 7051 onDisconnect: function(elementName) { 7052 console.log("RpcClient: onDisconnect"); 7053 }, 7054 onFailed: function() { 7055 console.log("RpcClient: onFailed"); 7056 } 7057 }; 7058 let want = { 7059 "bundleName": "com.ohos.server", 7060 "abilityName": "com.ohos.server.EntryAbility", 7061 }; 7062 7063 // Use this method to connect to the ability for the FA model. 7064 // FA.connectAbility(want,connect); 7065 7066 globalThis.context.connectServiceExtensionAbility(want, connect); 7067 ``` 7068 7069 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. 7070 7071 ```ts 7072 let isDead = proxy.isObjectDead(); 7073 console.log("RpcClient: isObjectDead is " + isDead); 7074 ``` 7075 7076## MessageOption 7077 7078Provides common message options (flag and wait time). Use the specified flag to construct the **MessageOption** object. 7079 7080**System capability**: SystemCapability.Communication.IPC.Core 7081 7082 | Name | Value | Description | 7083 | ------------- | ---- | ----------------------------------------------------------- | 7084 | TF_SYNC | 0 | Synchronous call. | 7085 | TF_ASYNC | 1 | Asynchronous call. | 7086 | TF_ACCEPT_FDS | 0x10 | Indication to **sendMessageRequest<sup>9+</sup>** for returning the file descriptor.| 7087 | TF_WAIT_TIME | 8 | Default waiting time, in seconds. | 7088 7089 7090### constructor<sup>9+</sup> 7091 7092constructor(async?: boolean); 7093 7094A constructor used to create a **MessageOption** object. 7095 7096**System capability**: SystemCapability.Communication.IPC.Core 7097 7098**Parameters** 7099 7100| Name| Type | Mandatory| Description | 7101| ------ | ------- | ---- | -------------------------------------- | 7102| async | boolean | No | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**.| 7103 7104 7105**Example** 7106 7107 ```ts 7108 class TestRemoteObject extends rpc.MessageOption { 7109 constructor(async) { 7110 super(async); 7111 } 7112 } 7113 ``` 7114 7115### constructor 7116 7117constructor(syncFlags?: number, waitTime?: number) 7118 7119A constructor used to create a **MessageOption** object. 7120 7121**System capability**: SystemCapability.Communication.IPC.Core 7122 7123**Parameters** 7124 7125 | Name | Type | Mandatory| Description | 7126 | --------- | ------ | ---- | --------------------------------------------- | 7127 | syncFlags | number | No | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**. | 7128 | waitTime | number | No | Maximum wait time for an RPC call. The default value is **TF_WAIT_TIME**.| 7129 7130**Example** 7131 7132 ```ts 7133 class TestRemoteObject extends rpc.MessageOption { 7134 constructor(syncFlags,waitTime) { 7135 super(syncFlags,waitTime); 7136 } 7137 } 7138 ``` 7139### isAsync<sup>9+</sup> 7140 7141isAsync(): boolean; 7142 7143Checks whether **SendMessageRequest** is called synchronously or asynchronously. 7144 7145**System capability**: SystemCapability.Communication.IPC.Core 7146 7147**Return value** 7148 7149 | Type | Description | 7150 | ------- | ---------------------------------------- | 7151 | boolean | Returns **true** if **SendMessageRequest** is called asynchronously; returns **false** if it is called synchronously.| 7152 7153**Example** 7154 7155 ```ts 7156 let option = new rpc.MessageOption(); 7157 let isAsync = option.isAsync(); 7158 ``` 7159 7160### setAsync<sup>9+</sup> 7161 7162setAsync(async: boolean): void; 7163 7164Sets whether **SendMessageRequest** is called synchronously or asynchronously. 7165 7166**System capability**: SystemCapability.Communication.IPC.Core 7167 7168**Example** 7169 7170 ```ts 7171 let option = new rpc.MessageOption(); 7172 let setAsync = option.setAsync(true); 7173 console.log("Set synchronization flag"); 7174 ``` 7175 7176### getFlags 7177 7178getFlags(): number 7179 7180Obtains the call flag, which can be synchronous or asynchronous. 7181 7182**System capability**: SystemCapability.Communication.IPC.Core 7183 7184**Return value** 7185 7186 | Type | Description | 7187 | ------ | ------------------------------------ | 7188 | number | Call mode obtained.| 7189 7190**Example** 7191 7192 ```ts 7193 try { 7194 let option = new rpc.MessageOption(); 7195 console.info("create object successfully."); 7196 let flog = option.getFlags(); 7197 console.info("run getFlags success, flog is " + flog); 7198 option.setFlags(1) 7199 console.info("run setFlags success"); 7200 let flog2 = option.getFlags(); 7201 console.info("run getFlags success, flog2 is " + flog2); 7202 } catch (error) { 7203 console.info("error " + error); 7204 } 7205 ``` 7206 7207### setFlags 7208 7209setFlags(flags: number): void 7210 7211Sets the call flag, which can be synchronous or asynchronous. 7212 7213**System capability**: SystemCapability.Communication.IPC.Core 7214 7215**Parameters** 7216 7217 | Name| Type | Mandatory| Description | 7218 | ------ | ------ | ---- | ------------------------ | 7219 | flags | number | Yes | Call flag to set.| 7220 7221**Example** 7222 7223 ```ts 7224 try { 7225 let option = new rpc.MessageOption(); 7226 option.setFlags(1) 7227 console.info("run setFlags success"); 7228 let flog = option.getFlags(); 7229 console.info("run getFlags success, flog is " + flog); 7230 } catch (error) { 7231 console.info("error " + error); 7232 } 7233 ``` 7234 7235### getWaitTime 7236 7237getWaitTime(): number 7238 7239Obtains the maximum wait time for this RPC call. 7240 7241**System capability**: SystemCapability.Communication.IPC.Core 7242 7243**Return value** 7244 7245 | Type | Description | 7246 | ------ | ----------------- | 7247 | number | Maximum wait time obtained.| 7248 7249**Example** 7250 7251 ```ts 7252 try { 7253 let option = new rpc.MessageOption(); 7254 let time = option.getWaitTime(); 7255 console.info("run getWaitTime success"); 7256 option.setWaitTime(16); 7257 let time2 = option.getWaitTime(); 7258 console.info("run getWaitTime success, time is " + time); 7259 } catch (error) { 7260 console.info("error " + error); 7261 } 7262 ``` 7263 7264### setWaitTime 7265 7266setWaitTime(waitTime: number): void 7267 7268Sets the maximum wait time for this RPC call. 7269 7270**System capability**: SystemCapability.Communication.IPC.Core 7271 7272**Parameters** 7273 7274 | Name | Type | Mandatory| Description | 7275 | -------- | ------ | ---- | --------------------- | 7276 | waitTime | number | Yes | Maximum wait time to set.| 7277 7278**Example** 7279 7280 ```ts 7281 try { 7282 let option = new rpc.MessageOption(); 7283 option.setWaitTime(16); 7284 let time = option.getWaitTime(); 7285 console.info("run getWaitTime success, time is " + time); 7286 } catch (error) { 7287 console.info("error " + error); 7288 } 7289 ``` 7290 7291## IPCSkeleton 7292 7293Obtains IPC context information, including the UID and PID, local and remote device IDs, and whether the method is invoked on the same device. 7294 7295### getContextObject 7296 7297static getContextObject(): IRemoteObject 7298 7299Obtains the system capability manager. This API is a static method. 7300 7301**System capability**: SystemCapability.Communication.IPC.Core 7302 7303**Return value** 7304 7305 | Type | Description | 7306 | ------------------------------- | -------------------- | 7307 | [IRemoteObject](#iremoteobject) | System capability manager obtained.| 7308 7309**Example** 7310 7311 ```ts 7312 let samgr = rpc.IPCSkeleton.getContextObject(); 7313 console.log("RpcServer: getContextObject result: " + samgr); 7314 ``` 7315 7316### getCallingPid 7317 7318static getCallingPid(): number 7319 7320Obtains 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. 7321 7322**System capability**: SystemCapability.Communication.IPC.Core 7323 7324**Return value** 7325 7326 | Type | Description | 7327 | ------ | ----------------- | 7328 | number | PID of the caller.| 7329 7330**Example** 7331 7332 ```ts 7333 class Stub extends rpc.RemoteObject { 7334 onRemoteMessageRequest(code, data, reply, option) { 7335 let callerPid = rpc.IPCSkeleton.getCallingPid(); 7336 console.log("RpcServer: getCallingPid result: " + callerPid); 7337 return true; 7338 } 7339 } 7340 ``` 7341 7342### getCallingUid 7343 7344static getCallingUid(): number 7345 7346Obtains 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. 7347 7348**System capability**: SystemCapability.Communication.IPC.Core 7349 7350**Return value** 7351 7352 | Type | Description | 7353 | ------ | ----------------- | 7354 | number | UID of the caller.| 7355 7356**Example** 7357 7358 ```ts 7359 class Stub extends rpc.RemoteObject { 7360 onRemoteMessageRequest(code, data, reply, option) { 7361 let callerUid = rpc.IPCSkeleton.getCallingUid(); 7362 console.log("RpcServer: getCallingUid result: " + callerUid); 7363 return true; 7364 } 7365 } 7366 ``` 7367 7368### getCallingTokenId<sup>8+</sup> 7369 7370static getCallingTokenId(): number; 7371 7372Obtains the caller's token ID, which is used to verify the caller identity. 7373 7374**System capability**: SystemCapability.Communication.IPC.Core 7375 7376**Return value** 7377 7378 | Type | Description | 7379 | ------ | --------------------- | 7380 | number | Token ID of the caller obtained.| 7381 7382**Example** 7383 7384 ```ts 7385 class Stub extends rpc.RemoteObject { 7386 onRemoteMessageRequest(code, data, reply, option) { 7387 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 7388 console.log("RpcServer: getCallingTokenId result: " + callerTokenId); 7389 return true; 7390 } 7391 } 7392 ``` 7393 7394 7395### getCallingDeviceID 7396 7397static getCallingDeviceID(): string 7398 7399Obtains the ID of the device hosting the caller's process. This API is a static method. 7400 7401**System capability**: SystemCapability.Communication.IPC.Core 7402 7403**Return value** 7404 7405 | Type | Description | 7406 | ------ | ---------------------------- | 7407 | string | Device ID obtained.| 7408 7409**Example** 7410 7411 ```ts 7412 class Stub extends rpc.RemoteObject { 7413 onRemoteMessageRequest(code, data, reply, option) { 7414 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 7415 console.log("RpcServer: callerDeviceID is: " + callerDeviceID); 7416 return true; 7417 } 7418 } 7419 ``` 7420 7421### getLocalDeviceID 7422 7423static getLocalDeviceID(): string 7424 7425Obtains the local device ID. This API is a static method. 7426 7427**System capability**: SystemCapability.Communication.IPC.Core 7428 7429**Return value** 7430 7431 | Type | Description | 7432 | ------ | ------------------ | 7433 | string | Local device ID obtained.| 7434 7435**Example** 7436 7437 ```ts 7438 class Stub extends rpc.RemoteObject { 7439 onRemoteMessageRequest(code, data, reply, option) { 7440 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 7441 console.log("RpcServer: localDeviceID is: " + localDeviceID); 7442 return true; 7443 } 7444 } 7445 ``` 7446 7447### isLocalCalling 7448 7449static isLocalCalling(): boolean 7450 7451Checks whether the remote process is a process of the local device. This API is a static method. 7452 7453**System capability**: SystemCapability.Communication.IPC.Core 7454 7455**Return value** 7456 7457 | Type | Description | 7458 | ------- | -------------------------------------------------- | 7459 | boolean | Returns **true** if the local and remote processes are on the same device; returns **false** otherwise.| 7460 7461**Example** 7462 7463 ```ts 7464 class Stub extends rpc.RemoteObject { 7465 onRemoteMessageRequest(code, data, reply, option) { 7466 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 7467 console.log("RpcServer: isLocalCalling is: " + isLocalCalling); 7468 return true; 7469 } 7470 } 7471 ``` 7472 7473### flushCmdBuffer<sup>9+</sup> 7474 7475static flushCmdBuffer(object: IRemoteObject): void 7476 7477Flushes 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. 7478 7479**System capability**: SystemCapability.Communication.IPC.Core 7480 7481**Parameters** 7482 7483 | Name| Type | Mandatory| Description | 7484 | ------ | ------------------------------- | ---- | ------------------- | 7485 | object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 7486 7487 7488**Example** 7489 7490 ```ts 7491 class TestRemoteObject extends rpc.RemoteObject { 7492 constructor(descriptor) { 7493 super(descriptor); 7494 } 7495 } 7496 let remoteObject = new TestRemoteObject("aaa"); 7497 try { 7498 rpc.IPCSkeleton.flushCmdBuffer(remoteObject); 7499 } catch(error) { 7500 console.info("proxy set calling identity fail, errorCode " + error.code); 7501 console.info("proxy set calling identity fail, errorMessage " + error.message); 7502 } 7503 ``` 7504 7505### flushCommands<sup>(deprecated)</sup> 7506 7507>This API is no longer maintained since API version 9. You are advised to use [flushCmdBuffer](#flushcmdbuffer9). 7508 7509static flushCommands(object: IRemoteObject): number 7510 7511Flushes 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. 7512 7513**System capability**: SystemCapability.Communication.IPC.Core 7514 7515**Parameters** 7516 7517 | Name| Type | Mandatory| Description | 7518 | ------ | ------------------------------- | ---- | ------------------- | 7519 | object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 7520 7521**Return value** 7522 7523 | Type | Description | 7524 | ------ | --------------------------------------------------------------------------------- | 7525 | 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.| 7526 7527**Example** 7528 7529 ```ts 7530 class MyDeathRecipient { 7531 onRemoteDied() { 7532 console.log("server died"); 7533 } 7534 } 7535 class TestRemoteObject extends rpc.RemoteObject { 7536 constructor(descriptor) { 7537 super(descriptor); 7538 } 7539 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7540 return true; 7541 } 7542 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7543 return true; 7544 } 7545 isObjectDead(): boolean { 7546 return false; 7547 } 7548 } 7549 let remoteObject = new TestRemoteObject("aaa"); 7550 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 7551 console.log("RpcServer: flushCommands result: " + ret); 7552 ``` 7553 7554### resetCallingIdentity 7555 7556static resetCallingIdentity(): string 7557 7558Changes 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. 7559 7560**System capability**: SystemCapability.Communication.IPC.Core 7561 7562**Return value** 7563 7564 | Type | Description | 7565 | ------ | ------------------------------------ | 7566 | string | String containing the UID and PID of the remote user.| 7567 7568**Example** 7569 7570 ```ts 7571 class Stub extends rpc.RemoteObject { 7572 onRemoteMessageRequest(code, data, reply, option) { 7573 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 7574 console.log("RpcServer: callingIdentity is: " + callingIdentity); 7575 return true; 7576 } 7577 } 7578 ``` 7579 7580 7581### restoreCallingIdentity<sup>9+</sup> 7582 7583static restoreCallingIdentity(identity: string): void 7584 7585Changes 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. 7586 7587**System capability**: SystemCapability.Communication.IPC.Core 7588 7589**Parameters** 7590 7591 | Name | Type | Mandatory| Description | 7592 | -------- | ------ | ---- | ------------------------------------------------------------------ | 7593 | identity | string | Yes | String containing the remote user UID and PID, which are returned by **resetCallingIdentity**.| 7594 7595**Example** 7596 7597 ```ts 7598 class Stub extends rpc.RemoteObject { 7599 onRemoteMessageRequest(code, data, reply, option) { 7600 let callingIdentity = null; 7601 try { 7602 callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 7603 console.log("RpcServer: callingIdentity is: " + callingIdentity); 7604 } finally { 7605 rpc.IPCSkeleton.restoreCallingIdentity("callingIdentity "); 7606 } 7607 return true; 7608 } 7609 } 7610 ``` 7611 7612### setCallingIdentity<sup>(deprecated)</sup> 7613 7614>This API is no longer maintained since API version 9. You are advised to use [restoreCallingIdentity](#restorecallingidentity9). 7615 7616static setCallingIdentity(identity: string): boolean 7617 7618Sets 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**. 7619 7620**System capability**: SystemCapability.Communication.IPC.Core 7621 7622**Parameters** 7623 7624 | Name | Type | Mandatory| Description | 7625 | -------- | ------ | ---- | ------------------------------------------------------------------ | 7626 | identity | string | Yes | String containing the remote user UID and PID, which are returned by **resetCallingIdentity**.| 7627 7628**Return value** 7629 7630 | Type | Description | 7631 | ------- | ---------------------------------| 7632 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 7633 7634**Example** 7635 7636 ```ts 7637 class Stub extends rpc.RemoteObject { 7638 onRemoteMessageRequest(code, data, reply, option) { 7639 let callingIdentity = null; 7640 try { 7641 callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 7642 console.log("RpcServer: callingIdentity is: " + callingIdentity); 7643 } finally { 7644 let ret = rpc.IPCSkeleton.setCallingIdentity("callingIdentity "); 7645 console.log("RpcServer: setCallingIdentity is: " + ret); 7646 } 7647 return true; 7648 } 7649 } 7650 ``` 7651 7652## RemoteObject 7653 7654Provides methods to implement **RemoteObject**. The service provider must inherit from this class. 7655 7656### constructor 7657 7658constructor(descriptor: string) 7659 7660A constructor used to create a **RemoteObject** object. 7661 7662**System capability**: SystemCapability.Communication.IPC.Core 7663 7664**Parameters** 7665 7666 | Name | Type | Mandatory| Description | 7667 | ---------- | ------ | ---- | ------------ | 7668 | descriptor | string | Yes | Interface descriptor.| 7669 7670 7671### sendRequest<sup>(deprecated)</sup> 7672 7673>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 7674 7675sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 7676 7677Sends 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. 7678 7679**System capability**: SystemCapability.Communication.IPC.Core 7680 7681**Parameters** 7682 7683 | Name | Type | Mandatory| Description | 7684 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7685 | 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.| 7686 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7687 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7688 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7689 7690**Return value** 7691 7692 | Type | Description | 7693 | ------- | -------------------------------- | 7694 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 7695 7696**Example** 7697 7698 ```ts 7699 class MyDeathRecipient { 7700 onRemoteDied() { 7701 console.log("server died"); 7702 } 7703 } 7704 class TestRemoteObject extends rpc.RemoteObject { 7705 constructor(descriptor) { 7706 super(descriptor); 7707 } 7708 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7709 return true; 7710 } 7711 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7712 return true; 7713 } 7714 isObjectDead(): boolean { 7715 return false; 7716 } 7717 } 7718 let testRemoteObject = new TestRemoteObject("testObject"); 7719 let option = new rpc.MessageOption(); 7720 let data = rpc.MessageParcel.create(); 7721 let reply = rpc.MessageParcel.create(); 7722 data.writeInt(1); 7723 data.writeString("hello"); 7724 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 7725 if (ret) { 7726 console.log("sendRequest got result"); 7727 let msg = reply.readString(); 7728 console.log("RPCTest: reply msg: " + msg); 7729 } else { 7730 console.log("RPCTest: sendRequest failed"); 7731 } 7732 console.log("RPCTest: sendRequest ends, reclaim parcel"); 7733 data.reclaim(); 7734 reply.reclaim(); 7735 ``` 7736 7737### sendRequest<sup>8+(deprecated)</sup> 7738 7739>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 7740 7741sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 7742 7743Sends 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. 7744 7745**System capability**: SystemCapability.Communication.IPC.Core 7746 7747**Parameters** 7748 7749 | Name | Type | Mandatory| Description | 7750 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7751 | 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.| 7752 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7753 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7754 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7755 7756**Return value** 7757 7758 | Type | Description | 7759 | -------------------------------- | --------------------------------------------- | 7760 | Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 7761 7762**Example** 7763 7764 ```ts 7765 class MyDeathRecipient { 7766 onRemoteDied() { 7767 console.log("server died"); 7768 } 7769 } 7770 class TestRemoteObject extends rpc.RemoteObject { 7771 constructor(descriptor) { 7772 super(descriptor); 7773 } 7774 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7775 return true; 7776 } 7777 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7778 return true; 7779 } 7780 isObjectDead(): boolean { 7781 return false; 7782 } 7783 } 7784 let testRemoteObject = new TestRemoteObject("testObject"); 7785 let option = new rpc.MessageOption(); 7786 let data = rpc.MessageParcel.create(); 7787 let reply = rpc.MessageParcel.create(); 7788 data.writeInt(1); 7789 data.writeString("hello"); 7790 testRemoteObject.sendRequest(1, data, reply, option) 7791 .then(function(result) { 7792 if (result.errCode === 0) { 7793 console.log("sendRequest got result"); 7794 result.reply.readException(); 7795 let msg = result.reply.readString(); 7796 console.log("RPCTest: reply msg: " + msg); 7797 } else { 7798 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 7799 } 7800 }).catch(function(e) { 7801 console.log("RPCTest: sendRequest got exception: " + e.message); 7802 }).finally (() => { 7803 console.log("RPCTest: sendRequest ends, reclaim parcel"); 7804 data.reclaim(); 7805 reply.reclaim(); 7806 }); 7807 ``` 7808 7809### sendMessageRequest<sup>9+</sup> 7810 7811sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 7812 7813Sends 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. 7814 7815**System capability**: SystemCapability.Communication.IPC.Core 7816 7817**Parameters** 7818 7819 | Name | Type | Mandatory| Description | 7820 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7821 | 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.| 7822 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 7823 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 7824 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7825 7826**Return value** 7827 7828 | Type | Description | 7829 | ---------------------------- | --------------------------------------------- | 7830 | Promise<RequestResult> | Promise used to return the **RequestResult** instance. | 7831 7832**Example** 7833 7834 ```ts 7835 class TestRemoteObject extends rpc.RemoteObject { 7836 constructor(descriptor) { 7837 super(descriptor); 7838 } 7839 } 7840 let testRemoteObject = new TestRemoteObject("testObject"); 7841 let option = new rpc.MessageOption(); 7842 let data = rpc.MessageSequence.create(); 7843 let reply = rpc.MessageSequence.create(); 7844 data.writeInt(1); 7845 data.writeString("hello"); 7846 testRemoteObject.sendMessageRequest(1, data, reply, option) 7847 .then(function(result) { 7848 if (result.errCode === 0) { 7849 console.log("sendMessageRequest got result"); 7850 result.reply.readException(); 7851 let msg = result.reply.readString(); 7852 console.log("RPCTest: reply msg: " + msg); 7853 } else { 7854 console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode); 7855 } 7856 }).catch(function(e) { 7857 console.log("RPCTest: sendMessageRequest got exception: " + e.message); 7858 }).finally (() => { 7859 console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); 7860 data.reclaim(); 7861 reply.reclaim(); 7862 }); 7863 ``` 7864 7865### sendMessageRequest<sup>9+</sup> 7866 7867sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 7868 7869Sends 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. 7870 7871**System capability**: SystemCapability.Communication.IPC.Core 7872 7873**Parameters** 7874 7875 | Name | Type | Mandatory| Description | 7876 | ------------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7877 | 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.| 7878 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 7879 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 7880 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7881 | AsyncCallback | AsyncCallback<RequestResult> | Yes | Callback for receiving the sending result. | 7882 7883**Example** 7884 7885 ```ts 7886 class TestRemoteObject extends rpc.RemoteObject { 7887 constructor(descriptor) { 7888 super(descriptor); 7889 } 7890 } 7891 function sendRequestCallback(result) { 7892 if (result.errCode === 0) { 7893 console.log("sendRequest got result"); 7894 result.reply.readException(); 7895 let msg = result.reply.readString(); 7896 console.log("RPCTest: reply msg: " + msg); 7897 } else { 7898 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 7899 } 7900 console.log("RPCTest: sendRequest ends, reclaim parcel"); 7901 result.data.reclaim(); 7902 result.reply.reclaim(); 7903 } 7904 let testRemoteObject = new TestRemoteObject("testObject"); 7905 let option = new rpc.MessageOption(); 7906 let data = rpc.MessageSequence.create(); 7907 let reply = rpc.MessageSequence.create(); 7908 data.writeInt(1); 7909 data.writeString("hello"); 7910 testRemoteObject.sendMessageRequest(1, data, reply, option, sendRequestCallback); 7911 ``` 7912 7913### sendRequest<sup>8+(deprecated)</sup> 7914 7915>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 7916 7917sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 7918 7919Sends 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. 7920 7921**System capability**: SystemCapability.Communication.IPC.Core 7922 7923**Parameters** 7924 7925 | Name | Type | Mandatory| Description | 7926 | ------------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7927 | 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.| 7928 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7929 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7930 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7931 | AsyncCallback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. | 7932 7933**Example** 7934 7935 ```ts 7936 class MyDeathRecipient { 7937 onRemoteDied() { 7938 console.log("server died"); 7939 } 7940 } 7941 class TestRemoteObject extends rpc.RemoteObject { 7942 constructor(descriptor) { 7943 super(descriptor); 7944 } 7945 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7946 return true; 7947 } 7948 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7949 return true; 7950 } 7951 isObjectDead(): boolean { 7952 return false; 7953 } 7954 } 7955 function sendRequestCallback(result) { 7956 if (result.errCode === 0) { 7957 console.log("sendRequest got result"); 7958 result.reply.readException(); 7959 let msg = result.reply.readString(); 7960 console.log("RPCTest: reply msg: " + msg); 7961 } else { 7962 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 7963 } 7964 console.log("RPCTest: sendRequest ends, reclaim parcel"); 7965 result.data.reclaim(); 7966 result.reply.reclaim(); 7967 } 7968 let testRemoteObject = new TestRemoteObject("testObject"); 7969 let option = new rpc.MessageOption(); 7970 let data = rpc.MessageParcel.create(); 7971 let reply = rpc.MessageParcel.create(); 7972 data.writeInt(1); 7973 data.writeString("hello"); 7974 testRemoteObject.sendRequest(1, data, reply, option, sendRequestCallback); 7975 ``` 7976 7977### onRemoteRequest<sup>8+(deprecated)</sup> 7978 7979>This API is no longer maintained since API version 9. You are advised to use [onRemoteMessageRequest](#onremotemessagerequest9). 7980 7981onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 7982 7983Provides a response to **sendMessageRequest()**. The server processes the request and returns a response in this API. 7984 7985**System capability**: SystemCapability.Communication.IPC.Core 7986 7987**Parameters** 7988 7989 | Name| Type | Mandatory| Description | 7990 | ------ | ----------------------------------------- | ---- | --------------------------------------- | 7991 | code | number | Yes | Service request code sent by the remote end. | 7992 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that holds the parameters called by the client.| 7993 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object carrying the result. | 7994 | option | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 7995 7996**Return value** 7997 7998 | Type | Description | 7999 | ------- | -------------------------------- | 8000 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 8001 8002**Example** 8003 8004 ```ts 8005 class MyDeathRecipient { 8006 onRemoteDied() { 8007 console.log("server died"); 8008 } 8009 } 8010 class TestRemoteObject extends rpc.RemoteObject { 8011 constructor(descriptor) { 8012 super(descriptor); 8013 } 8014 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8015 return true; 8016 } 8017 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8018 return true; 8019 } 8020 isObjectDead(): boolean { 8021 return false; 8022 } 8023 onRemoteRequest(code, data, reply, option) { 8024 if (code === 1) { 8025 console.log("RpcServer: onRemoteRequest called"); 8026 return true; 8027 } else { 8028 console.log("RpcServer: unknown code: " + code); 8029 return false; 8030 } 8031 } 8032 } 8033 ``` 8034 8035### onRemoteMessageRequest<sup>9+</sup> 8036 8037onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean> 8038 8039> **NOTE**<br/> 8040> 8041>* You are advised to overload **onRemoteMessageRequest** preferentially, which implements synchronous and asynchronous message processing. 8042>* If both onRemoteRequest() and onRemoteMessageRequest() are overloaded, only the onRemoteMessageRequest() takes effect. 8043 8044Provides a response to **sendMessageRequest()**. The server processes the request synchronously or asynchronously and returns the result in this API. 8045 8046**System capability**: SystemCapability.Communication.IPC.Core 8047 8048**Parameters** 8049 8050 | Name| Type | Mandatory| Description | 8051 | ------ | ------------------------------------ | ---- | ----------------------------------------- | 8052 | code | number | Yes | Service request code sent by the remote end. | 8053 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that holds the parameters called by the client.| 8054 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object to which the result is written. | 8055 | option | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 8056 8057**Return value** 8058 8059 | Type | Description | 8060 | ----------------- | ----------------------------------------------------------------------------------------------- | 8061 | 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.| 8062 | Promise\<boolean> | Returns a promise object if the request is processed asynchronously in **onRemoteMessageRequest**. | 8063 8064**Example**: Overload **onRemoteMessageRequest** to process requests synchronously. 8065 8066 ```ts 8067 class TestRemoteObject extends rpc.RemoteObject { 8068 constructor(descriptor) { 8069 super(descriptor); 8070 } 8071 8072 onRemoteMessageRequest(code, data, reply, option) { 8073 if (code === 1) { 8074 console.log("RpcServer: sync onRemoteMessageRequest is called"); 8075 return true; 8076 } else { 8077 console.log("RpcServer: unknown code: " + code); 8078 return false; 8079 } 8080 } 8081 } 8082 ``` 8083 8084 **Example**: Overload **onRemoteMessageRequest** to process requests asynchronously. 8085 8086 ```ts 8087 class TestRemoteObject extends rpc.RemoteObject { 8088 constructor(descriptor) { 8089 super(descriptor); 8090 } 8091 8092 async onRemoteMessageRequest(code, data, reply, option) { 8093 if (code === 1) { 8094 console.log("RpcServer: async onRemoteMessageRequest is called"); 8095 } else { 8096 console.log("RpcServer: unknown code: " + code); 8097 return false; 8098 } 8099 await new Promise((resolve) => { 8100 setTimeout(resolve, 100); 8101 }) 8102 return true; 8103 } 8104 } 8105 ``` 8106 8107**Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests synchronously. 8108 8109 ```ts 8110 class TestRemoteObject extends rpc.RemoteObject { 8111 constructor(descriptor) { 8112 super(descriptor); 8113 } 8114 8115 onRemoteRequest(code, data, reply, option) { 8116 if (code === 1) { 8117 console.log("RpcServer: sync onRemoteMessageRequest is called"); 8118 return true; 8119 } else { 8120 console.log("RpcServer: unknown code: " + code); 8121 return false; 8122 } 8123 } 8124 // Only onRemoteMessageRequest is executed. 8125 onRemoteMessageRequest(code, data, reply, option) { 8126 if (code === 1) { 8127 console.log("RpcServer: async onRemoteMessageRequest is called"); 8128 } else { 8129 console.log("RpcServer: unknown code: " + code); 8130 return false; 8131 } 8132 8133 return true; 8134 } 8135 } 8136 ``` 8137 8138 **Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests asynchronously. 8139 8140 ```ts 8141 class TestRemoteObject extends rpc.RemoteObject { 8142 constructor(descriptor) { 8143 super(descriptor); 8144 } 8145 8146 onRemoteRequest(code, data, reply, option) { 8147 if (code === 1) { 8148 console.log("RpcServer: sync onRemoteRequest is called"); 8149 return true; 8150 } else { 8151 console.log("RpcServer: unknown code: " + code); 8152 return false; 8153 } 8154 } 8155 // Only onRemoteMessageRequest is executed. 8156 async onRemoteMessageRequest(code, data, reply, option) { 8157 if (code === 1) { 8158 console.log("RpcServer: async onRemoteMessageRequest is called"); 8159 } else { 8160 console.log("RpcServer: unknown code: " + code); 8161 return false; 8162 } 8163 await new Promise((resolve) => { 8164 setTimeout(resolve, 100); 8165 }) 8166 return true; 8167 } 8168 } 8169 ``` 8170 8171### getCallingUid 8172 8173getCallingUid(): number 8174 8175Obtains the UID of the remote process. 8176 8177**System capability**: SystemCapability.Communication.IPC.Core 8178 8179**Return value** 8180 | Type | Description | 8181 | ------ | ----------------------- | 8182 | number | UID of the remote process obtained.| 8183 8184**Example** 8185 8186 ```ts 8187 class TestRemoteObject extends rpc.RemoteObject { 8188 constructor(descriptor) { 8189 super(descriptor); 8190 } 8191 } 8192 let testRemoteObject = new TestRemoteObject("testObject"); 8193 console.log("RpcServer: getCallingUid: " + testRemoteObject.getCallingUid()); 8194 ``` 8195 8196### getCallingPid 8197 8198getCallingPid(): number 8199 8200Obtains the PID of the remote process. 8201 8202**System capability**: SystemCapability.Communication.IPC.Core 8203 8204**Return value** 8205 8206 | Type | Description | 8207 | ------ | ----------------------- | 8208 | number | PID of the remote process obtained.| 8209 8210**Example** 8211 8212 ```ts 8213 class TestRemoteObject extends rpc.RemoteObject { 8214 constructor(descriptor) { 8215 super(descriptor); 8216 } 8217 } 8218 let testRemoteObject = new TestRemoteObject("testObject"); 8219 console.log("RpcServer: getCallingPid: " + testRemoteObject.getCallingPid()); 8220 ``` 8221 8222### getLocalInterface<sup>9+</sup> 8223 8224getLocalInterface(descriptor: string): IRemoteBroker 8225 8226Obtains the interface descriptor. 8227 8228**System capability**: SystemCapability.Communication.IPC.Core 8229 8230**Parameters** 8231 8232 | Name | Type | Mandatory| Description | 8233 | ---------- | ------ | ---- | -------------------- | 8234 | descriptor | string | Yes | Interface descriptor.| 8235 8236**Return value** 8237 8238 | Type | Description | 8239 | ------------- | --------------------------------------------- | 8240 | IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.| 8241 8242 8243**Example** 8244 8245 ```ts 8246 class MyDeathRecipient { 8247 onRemoteDied() { 8248 console.log("server died"); 8249 } 8250 } 8251 class TestRemoteObject extends rpc.RemoteObject { 8252 constructor(descriptor) { 8253 super(descriptor); 8254 } 8255 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8256 // Implement the method logic based on service requirements. 8257 } 8258 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8259 // Implement the method logic based on service requirements. 8260 } 8261 isObjectDead(): boolean { 8262 return false; 8263 } 8264 } 8265 let testRemoteObject = new TestRemoteObject("testObject"); 8266 try { 8267 let broker = testRemoteObject.getLocalInterface("testObject"); 8268 } catch(error) { 8269 console.info("rpc get local interface fail, errorCode " + error.code); 8270 console.info("rpc get local interface fail, errorMessage " + error.message); 8271 } 8272 ``` 8273 8274### queryLocalInterface<sup>(deprecated)</sup> 8275 8276>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9). 8277 8278queryLocalInterface(descriptor: string): IRemoteBroker 8279 8280Checks whether the remote object corresponding to the specified interface token exists. 8281 8282**System capability**: SystemCapability.Communication.IPC.Core 8283 8284**Parameters** 8285 8286 | Name | Type | Mandatory| Description | 8287 | ---------- | ------ | ---- | ---------------------- | 8288 | descriptor | string | Yes | Interface descriptor.| 8289 8290**Return value** 8291 8292 | Type | Description | 8293 | ------------- | ------------------------------------------------------------------ | 8294 | IRemoteBroker | Returns the remote object if a match is found; returns **Null** otherwise.| 8295 8296**Example** 8297 8298 ```ts 8299 class MyDeathRecipient { 8300 onRemoteDied() { 8301 console.log("server died"); 8302 } 8303 } 8304 class TestRemoteObject extends rpc.RemoteObject { 8305 constructor(descriptor) { 8306 super(descriptor); 8307 } 8308 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8309 return true; 8310 } 8311 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8312 return true; 8313 } 8314 isObjectDead(): boolean { 8315 return false; 8316 } 8317 } 8318 let testRemoteObject = new TestRemoteObject("testObject"); 8319 let broker = testRemoteObject.queryLocalInterface("testObject"); 8320 ``` 8321 8322### getDescriptor<sup>9+</sup> 8323 8324getDescriptor(): string 8325 8326Obtains the interface descriptor of this object. The interface descriptor is a string. 8327 8328**System capability**: SystemCapability.Communication.IPC.Core 8329 8330**Return value** 8331 8332 | Type | Description | 8333 | ------ | ---------------- | 8334 | string | Interface descriptor obtained.| 8335 8336**Error codes** 8337 8338For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8339 8340 | ID| Error Message| 8341 | -------- | -------- | 8342 | 1900008 | proxy or remote object is invalid | 8343 8344**Example** 8345 8346 ```ts 8347 class MyDeathRecipient { 8348 onRemoteDied() { 8349 console.log("server died"); 8350 } 8351 } 8352 class TestRemoteObject extends rpc.RemoteObject { 8353 constructor(descriptor) { 8354 super(descriptor); 8355 } 8356 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8357 // Implement the method logic based on service requirements. 8358 } 8359 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8360 // Implement the method logic based on service requirements. 8361 } 8362 isObjectDead(): boolean { 8363 return false; 8364 } 8365 } 8366 let testRemoteObject = new TestRemoteObject("testObject"); 8367 console.log("RpcServer: descriptor is: " + descriptor); 8368 try { 8369 let descriptor = testRemoteObject.getDescriptor(); 8370 } catch(error) { 8371 console.info("rpc get local interface fail, errorCode " + error.code); 8372 console.info("rpc get local interface fail, errorMessage " + error.message); 8373 } 8374 ``` 8375 8376### getInterfaceDescriptor<sup>(deprecated)</sup> 8377 8378>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9). 8379 8380getInterfaceDescriptor(): string 8381 8382Obtains the interface descriptor. 8383 8384**System capability**: SystemCapability.Communication.IPC.Core 8385 8386**Return value** 8387 8388 | Type | Description | 8389 | ------ | ---------------- | 8390 | string | Interface descriptor obtained.| 8391 8392**Example** 8393 8394 ```ts 8395 class MyDeathRecipient { 8396 onRemoteDied() { 8397 console.log("server died"); 8398 } 8399 } 8400 class TestRemoteObject extends rpc.RemoteObject { 8401 constructor(descriptor) { 8402 super(descriptor); 8403 } 8404 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8405 return true; 8406 } 8407 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8408 return true; 8409 } 8410 isObjectDead(): boolean { 8411 return false; 8412 } 8413 } 8414 let testRemoteObject = new TestRemoteObject("testObject"); 8415 let descriptor = testRemoteObject.getInterfaceDescriptor(); 8416 console.log("RpcServer: descriptor is: " + descriptor); 8417 ``` 8418 8419### modifyLocalInterface<sup>9+</sup> 8420 8421modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 8422 8423Binds an interface descriptor to an **IRemoteBroker** object. 8424 8425**System capability**: SystemCapability.Communication.IPC.Core 8426 8427**Parameters** 8428 8429 | Name | Type | Mandatory| Description | 8430 | -------------- | ------------- | ---- | ------------------------------------- | 8431 | localInterface | IRemoteBroker | Yes | **IRemoteBroker** object. | 8432 | descriptor | string | Yes | Interface descriptor.| 8433 8434**Example** 8435 8436 ```ts 8437 class MyDeathRecipient { 8438 onRemoteDied() { 8439 console.log("server died"); 8440 } 8441 } 8442 class TestRemoteObject extends rpc.RemoteObject { 8443 constructor(descriptor) { 8444 super(descriptor); 8445 try { 8446 this.modifyLocalInterface(this, descriptor); 8447 } catch(error) { 8448 console.info(" rpc attach local interface fail, errorCode " + error.code); 8449 console.info(" rpc attach local interface fail, errorMessage " + error.message); 8450 } 8451 } 8452 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8453 // Implement the method logic based on service requirements. 8454 } 8455 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8456 // Implement the method logic based on service requirements. 8457 } 8458 isObjectDead(): boolean { 8459 return false; 8460 } 8461 asObject(): rpc.IRemoteObject { 8462 return this; 8463 } 8464 } 8465 let testRemoteObject = new TestRemoteObject("testObject"); 8466 ``` 8467 8468### attachLocalInterface<sup>(deprecated)</sup> 8469 8470>This API is no longer maintained since API version 9. You are advised to use [modifyLocalInterface](#modifylocalinterface9). 8471 8472attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 8473 8474Binds an interface descriptor to an **IRemoteBroker** object. 8475 8476**System capability**: SystemCapability.Communication.IPC.Core 8477 8478**Parameters** 8479 8480 | Name | Type | Mandatory| Description | 8481 | -------------- | ------------- | ---- | ------------------------------------- | 8482 | localInterface | IRemoteBroker | Yes | **IRemoteBroker** object. | 8483 | descriptor | string | Yes | Interface descriptor.| 8484 8485**Example** 8486 8487 ```ts 8488 class MyDeathRecipient { 8489 onRemoteDied() { 8490 console.log("server died"); 8491 } 8492 } 8493 class TestRemoteObject extends rpc.RemoteObject { 8494 constructor(descriptor) { 8495 super(descriptor); 8496 this.attachLocalInterface(this, descriptor); 8497 } 8498 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8499 return true; 8500 } 8501 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8502 return true; 8503 } 8504 isObjectDead(): boolean { 8505 return false; 8506 } 8507 asObject(): rpc.IRemoteObject { 8508 return this; 8509 } 8510 } 8511 let testRemoteObject = new TestRemoteObject("testObject"); 8512 ``` 8513 8514## Ashmem<sup>8+</sup> 8515 8516Provides 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. 8517 8518**System capability**: SystemCapability.Communication.IPC.Core 8519 8520The table below describes the protection types of the mapped memory. 8521 8522 | Name | Value | Description | 8523 | ---------- | --- | ------------------ | 8524 | PROT_EXEC | 4 | The mapped memory is executable. | 8525 | PROT_NONE | 0 | The mapped memory is inaccessible.| 8526 | PROT_READ | 1 | The mapped memory is readable. | 8527 | PROT_WRITE | 2 | The mapped memory is writeable. | 8528 8529### create<sup>9+</sup> 8530 8531static create(name: string, size: number): Ashmem 8532 8533Creates an **Ashmem** object with the specified name and size. This API is a static method. 8534 8535**System capability**: SystemCapability.Communication.IPC.Core 8536 8537**Parameters** 8538 8539 | Name| Type | Mandatory| Description | 8540 | ------ | ------ | ---- | ---------------------------- | 8541 | name | string | Yes | Name of the **Ashmem** object to create. | 8542 | size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 8543 8544**Return value** 8545 8546 | Type | Description | 8547 | ------ | ---------------------------------------------- | 8548 | Ashmem | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 8549 8550**Example** 8551 8552 ```ts 8553 let ashmem; 8554 try { 8555 ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8556 } catch(error) { 8557 console.info("Rpc creat ashmem fail, errorCode " + error.code); 8558 console.info("Rpc creat ashmem fail, errorMessage " + error.message); 8559 } 8560 let size = ashmem.getAshmemSize(); 8561 console.log("RpcTest: get ashemm by create : " + ashmem + " size is : " + size); 8562 ``` 8563 8564### createAshmem<sup>8+(deprecated)</sup> 8565 8566>This API is no longer maintained since API version 9. You are advised to use [create](#create9). 8567 8568static createAshmem(name: string, size: number): Ashmem 8569 8570Creates an **Ashmem** object with the specified name and size. This API is a static method. 8571 8572**System capability**: SystemCapability.Communication.IPC.Core 8573 8574**Parameters** 8575 8576 | Name| Type | Mandatory| Description | 8577 | ------ | ------ | ---- | ---------------------------- | 8578 | name | string | Yes | Name of the **Ashmem** object to create. | 8579 | size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 8580 8581**Return value** 8582 8583 | Type | Description | 8584 | ------ | ---------------------------------------------- | 8585 | Ashmem | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 8586 8587**Example** 8588 8589 ```ts 8590 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8591 let size = ashmem.getAshmemSize(); 8592 console.log("RpcTest: get ashemm by createAshmem : " + ashmem + " size is : " + size); 8593 ``` 8594 8595### create<sup>9+</sup> 8596 8597static create(ashmem: Ashmem): Ashmem 8598 8599Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 8600 8601**System capability**: SystemCapability.Communication.IPC.Core 8602 8603**Parameters** 8604 8605 | Name| Type | Mandatory| Description | 8606 | ------ | ------ | ---- | -------------------- | 8607 | ashmem | Ashmem | Yes | Existing **Ashmem** object.| 8608 8609**Return value** 8610 8611 | Type | Description | 8612 | ------ | ---------------------- | 8613 | Ashmem | **Ashmem** object created.| 8614 8615 8616**Example** 8617 8618 ```ts 8619 let ashmem2; 8620 try { 8621 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8622 let ashmem2 = rpc.Ashmem.create(ashmem); 8623 } catch(error) { 8624 console.info("Rpc creat ashmem from existing fail, errorCode " + error.code); 8625 console.info("Rpc creat ashmem from existing fail, errorMessage " + error.message); 8626 } 8627 let size = ashmem2.getAshmemSize(); 8628 console.log("RpcTest: get ashemm by create : " + ashmem2 + " size is : " + size); 8629 ``` 8630 8631### createAshmemFromExisting<sup>8+(deprecated)</sup> 8632 8633>This API is no longer maintained since API version 9. You are advised to use [create](#create9). 8634 8635static createAshmemFromExisting(ashmem: Ashmem): Ashmem 8636 8637Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 8638 8639**System capability**: SystemCapability.Communication.IPC.Core 8640 8641**Parameters** 8642 8643 | Name| Type | Mandatory| Description | 8644 | ------ | ------ | ---- | -------------------- | 8645 | ashmem | Ashmem | Yes | Existing **Ashmem** object.| 8646 8647**Return value** 8648 8649 | Type | Description | 8650 | ------ | ---------------------- | 8651 | Ashmem | **Ashmem** object created.| 8652 8653**Example** 8654 8655 ```ts 8656 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8657 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 8658 let size = ashmem2.getAshmemSize(); 8659 console.log("RpcTest: get ashemm by createAshmemFromExisting : " + ashmem2 + " size is : " + size); 8660 ``` 8661 8662### closeAshmem<sup>8+</sup> 8663 8664closeAshmem(): void 8665 8666Closes this **Ashmem** object. 8667 8668**System capability**: SystemCapability.Communication.IPC.Core 8669 8670**Example** 8671 8672 ```ts 8673 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8674 ashmem.closeAshmem(); 8675 ``` 8676 8677### unmapAshmem<sup>8+</sup> 8678 8679unmapAshmem(): void 8680 8681Deletes the mappings for the specified address range of this **Ashmem** object. 8682 8683**System capability**: SystemCapability.Communication.IPC.Core 8684 8685**Example** 8686 8687 ```ts 8688 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8689 ashmem.unmapAshmem(); 8690 ``` 8691 8692### getAshmemSize<sup>8+</sup> 8693 8694getAshmemSize(): number 8695 8696Obtains the memory size of this **Ashmem** object. 8697 8698**System capability**: SystemCapability.Communication.IPC.Core 8699 8700**Return value** 8701 8702 | Type | Description | 8703 | ------ | -------------------------- | 8704 | number | **Ashmem** size obtained.| 8705 8706**Example** 8707 8708 ```ts 8709 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8710 let size = ashmem.getAshmemSize(); 8711 console.log("RpcTest: get ashmem is " + ashmem + " size is : " + size); 8712 ``` 8713 8714### mapTypedAshmem<sup>9+</sup> 8715 8716mapTypedAshmem(mapType: number): void 8717 8718Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 8719 8720**System capability**: SystemCapability.Communication.IPC.Core 8721 8722**Parameters** 8723 8724 | Name | Type | Mandatory| Description | 8725 | ------- | ------ | ---- | ------------------------------ | 8726 | mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 8727 8728**Error codes** 8729 8730For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8731 8732 | ID| Error Message| 8733 | -------- | -------- | 8734 | 1900001 | call mmap function failed | 8735 8736**Example** 8737 8738 ```ts 8739 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8740 try { 8741 ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 8742 } catch(error) { 8743 console.info("Rpc map ashmem fail, errorCode " + error.code); 8744 console.info("Rpc map ashmem fail, errorMessage " + error.message); 8745 } 8746 ``` 8747 8748### mapAshmem<sup>8+(deprecated)</sup> 8749 8750>This API is no longer maintained since API version 9. You are advised to use [mapTypedAshmem](#maptypedashmem9). 8751 8752mapAshmem(mapType: number): boolean 8753 8754Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 8755 8756**System capability**: SystemCapability.Communication.IPC.Core 8757 8758**Parameters** 8759 8760 | Name | Type | Mandatory| Description | 8761 | ------- | ------ | ---- | ------------------------------ | 8762 | mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 8763 8764**Return value** 8765 8766 | Type | Description | 8767 | ------- | -------------------------------- | 8768 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 8769 8770**Example** 8771 8772 ```ts 8773 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8774 let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 8775 console.log("RpcTest: map ashmem result is : " + mapReadAndWrite); 8776 ``` 8777 8778### mapReadWriteAshmem<sup>9+</sup> 8779 8780mapReadWriteAshmem(): void 8781 8782Maps the shared file to the readable and writable virtual address space of the process. 8783 8784**System capability**: SystemCapability.Communication.IPC.Core 8785 8786**Error codes** 8787 8788For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8789 8790 | ID| Error Message| 8791 | -------- | -------- | 8792 | 1900001 | call mmap function failed | 8793 8794**Example** 8795 8796 ```ts 8797 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8798 try { 8799 ashmem.mapReadWriteAshmem(); 8800 } catch(error) { 8801 console.info("Rpc map read and write ashmem fail, errorCode " + error.code); 8802 console.info("Rpc map read and write ashmem fail, errorMessage " + error.message); 8803 } 8804 ``` 8805 8806### mapReadAndWriteAshmem<sup>8+(deprecated)</sup> 8807 8808>This API is no longer maintained since API version 9. You are advised to use [mapReadWriteAshmem](#mapreadwriteashmem9). 8809 8810mapReadAndWriteAshmem(): boolean 8811 8812Maps the shared file to the readable and writable virtual address space of the process. 8813 8814**System capability**: SystemCapability.Communication.IPC.Core 8815 8816**Return value** 8817 8818 | Type | Description | 8819 | ------- | -------------------------------- | 8820 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 8821 8822**Example** 8823 8824 ```ts 8825 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8826 let mapResult = ashmem.mapReadAndWriteAshmem(); 8827 console.log("RpcTest: map ashmem result is : " + mapResult); 8828 ``` 8829 8830### mapReadonlyAshmem<sup>9+</sup> 8831 8832mapReadonlyAshmem(): void 8833 8834Maps the shared file to the read-only virtual address space of the process. 8835 8836**System capability**: SystemCapability.Communication.IPC.Core 8837 8838**Error codes** 8839 8840For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8841 8842 | ID| Error Message| 8843 | -------- | -------- | 8844 | 1900001 | call mmap function failed | 8845 8846**Example** 8847 8848 ```ts 8849 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8850 try { 8851 ashmem.mapReadonlyAshmem(); 8852 } catch(error) { 8853 console.info("Rpc map read and write ashmem fail, errorCode " + error.code); 8854 console.info("Rpc map read and write ashmem fail, errorMessage " + error.message); 8855 } 8856 ``` 8857 8858### mapReadOnlyAshmem<sup>8+(deprecated)</sup> 8859 8860>This API is no longer maintained since API version 9. You are advised to use [mapReadonlyAshmem](#mapreadonlyashmem9). 8861 8862mapReadOnlyAshmem(): boolean 8863 8864Maps the shared file to the read-only virtual address space of the process. 8865 8866**System capability**: SystemCapability.Communication.IPC.Core 8867 8868**Return value** 8869 8870 | Type | Description | 8871 | ------- | -------------------------------- | 8872 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 8873 8874**Example** 8875 8876 ```ts 8877 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8878 let mapResult = ashmem.mapReadOnlyAshmem(); 8879 console.log("RpcTest: Ashmem mapReadOnlyAshmem result is : " + mapResult); 8880 ``` 8881 8882### setProtectionType<sup>9+</sup> 8883 8884setProtectionType(protectionType: number): void 8885 8886Sets the protection level of the memory region to which the shared file is mapped. 8887 8888**System capability**: SystemCapability.Communication.IPC.Core 8889 8890**Parameters** 8891 8892 | Name | Type | Mandatory| Description | 8893 | -------------- | ------ | ---- | ------------------ | 8894 | protectionType | number | Yes | Protection type to set.| 8895 8896**Error codes** 8897 8898For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8899 8900 | ID| Error Message| 8901 | -------- | -------- | 8902 | 1900002 | call os ioctl function failed | 8903 8904**Example** 8905 8906 ```ts 8907 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8908 try { 8909 ashmem.setProtection(ashmem.PROT_READ); 8910 } catch(error) { 8911 console.info("Rpc set protection type fail, errorCode " + error.code); 8912 console.info("Rpc set protection type fail, errorMessage " + error.message); 8913 } 8914 ``` 8915 8916### setProtection<sup>8+(deprecated)</sup> 8917 8918>This API is no longer maintained since API version 9. You are advised to use [setProtectionType](#setprotectiontype9). 8919 8920setProtection(protectionType: number): boolean 8921 8922Sets the protection level of the memory region to which the shared file is mapped. 8923 8924**System capability**: SystemCapability.Communication.IPC.Core 8925 8926**Parameters** 8927 8928 | Name | Type | Mandatory| Description | 8929 | -------------- | ------ | ---- | ------------------ | 8930 | protectionType | number | Yes | Protection type to set.| 8931 8932**Return value** 8933 8934 | Type | Description | 8935 | ------- | -------------------------------- | 8936 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 8937 8938**Example** 8939 8940 ```ts 8941 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8942 let result = ashmem.setProtection(ashmem.PROT_READ); 8943 console.log("RpcTest: Ashmem setProtection result is : " + result); 8944 ``` 8945 8946### writeAshmem<sup>9+</sup> 8947 8948writeAshmem(buf: number[], size: number, offset: number): void 8949 8950Writes data to the shared file associated with this **Ashmem** object. 8951 8952**System capability**: SystemCapability.Communication.IPC.Core 8953 8954**Parameters** 8955 8956 | Name| Type | Mandatory| Description | 8957 | ------ | -------- | ---- | -------------------------------------------------- | 8958 | buf | number[] | Yes | Data to write. | 8959 | size | number | Yes | Size of the data to write. | 8960 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 8961 8962**Error codes** 8963 8964For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8965 8966 | ID| Error Message| 8967 | -------- | -------- | 8968 | 1900003 | write to ashmem failed | 8969 8970**Example** 8971 8972 ```ts 8973 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8974 ashmem.mapReadWriteAshmem(); 8975 var ByteArrayVar = [1, 2, 3, 4, 5]; 8976 try { 8977 ashmem.writeAshmem(ByteArrayVar, 5, 0); 8978 } catch(error) { 8979 console.info("Rpc write to ashmem fail, errorCode " + error.code); 8980 console.info("Rpc write to ashmem fail, errorMessage " + error.message); 8981 } 8982 ``` 8983 8984### writeToAshmem<sup>8+(deprecated)</sup> 8985 8986>This API is no longer maintained since API version 9. You are advised to use [writeAshmem](#writeashmem9). 8987 8988writeToAshmem(buf: number[], size: number, offset: number): boolean 8989 8990Writes data to the shared file associated with this **Ashmem** object. 8991 8992**System capability**: SystemCapability.Communication.IPC.Core 8993 8994**Parameters** 8995 8996 | Name| Type | Mandatory| Description | 8997 | ------ | -------- | ---- | -------------------------------------------------- | 8998 | buf | number[] | Yes | Data to write. | 8999 | size | number | Yes | Size of the data to write. | 9000 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 9001 9002**Return value** 9003 9004 | Type | Description | 9005 | ------- | ----------------------------------------------------------------------------- | 9006 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 9007 9008**Example** 9009 9010 ```ts 9011 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9012 let mapResult = ashmem.mapReadAndWriteAshmem(); 9013 console.info("RpcTest map ashmem result is " + mapResult); 9014 var ByteArrayVar = [1, 2, 3, 4, 5]; 9015 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 9016 console.log("RpcTest: write to Ashmem result is : " + writeResult); 9017 ``` 9018 9019### readAshmem<sup>9+</sup> 9020 9021readAshmem(size: number, offset: number): number[] 9022 9023Reads data from the shared file associated with this **Ashmem** object. 9024 9025**System capability**: SystemCapability.Communication.IPC.Core 9026 9027**Parameters** 9028 9029 | Name| Type | Mandatory| Description | 9030 | ------ | ------ | ---- | -------------------------------------------------- | 9031 | size | number | Yes | Size of the data to read. | 9032 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 9033 9034**Return value** 9035 9036 | Type | Description | 9037 | -------- | ---------------- | 9038 | number[] | Data read.| 9039 9040**Error codes** 9041 9042For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9043 9044 | ID| Error Message| 9045 | -------- | -------- | 9046 | 1900004 | read from ashmem failed | 9047 9048**Example** 9049 9050 ```ts 9051 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9052 ashmem.mapReadWriteAshmem(); 9053 var ByteArrayVar = [1, 2, 3, 4, 5]; 9054 ashmem.writeAshmem(ByteArrayVar, 5, 0); 9055 try { 9056 let readResult = ashmem.readAshmem(5, 0); 9057 console.log("RpcTest: read from Ashmem result is : " + readResult); 9058 } catch(error) { 9059 console.info("Rpc read from ashmem fail, errorCode " + error.code); 9060 console.info("Rpc read from ashmem fail, errorMessage " + error.message); 9061 } 9062 ``` 9063 9064### readFromAshmem<sup>8+(deprecated)</sup> 9065 9066>This API is no longer maintained since API version 9. You are advised to use [readAshmem](#readashmem9). 9067 9068readFromAshmem(size: number, offset: number): number[] 9069 9070Reads data from the shared file associated with this **Ashmem** object. 9071 9072**System capability**: SystemCapability.Communication.IPC.Core 9073 9074**Parameters** 9075 9076 | Name| Type | Mandatory| Description | 9077 | ------ | ------ | ---- | -------------------------------------------------- | 9078 | size | number | Yes | Size of the data to read. | 9079 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 9080 9081**Return value** 9082 9083 | Type | Description | 9084 | -------- | ---------------- | 9085 | number[] | Data read.| 9086 9087**Example** 9088 9089 ``` ts 9090 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9091 let mapResult = ashmem.mapReadAndWriteAshmem(); 9092 console.info("RpcTest map ashmem result is " + mapResult); 9093 var ByteArrayVar = [1, 2, 3, 4, 5]; 9094 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 9095 console.log("RpcTest: write to Ashmem result is : " + writeResult); 9096 let readResult = ashmem.readFromAshmem(5, 0); 9097 console.log("RpcTest: read to Ashmem result is : " + readResult); 9098 ``` 9099 9100## Obtaining the Context 9101 9102**Example** 9103 9104 ```ts 9105 import Ability from '@ohos.app.ability.UIAbility'; 9106 9107 export default class MainAbility extends Ability { 9108 onCreate(want, launchParam) { 9109 console.log("[Demo] MainAbility onCreate"); 9110 globalThis.context = this.context; 9111 } 9112 onDestroy() { 9113 console.log("[Demo] MainAbility onDestroy"); 9114 } 9115 onWindowStageCreate(windowStage) { 9116 // Main window is created, set main page for this ability 9117 console.log("[Demo] MainAbility onWindowStageCreate"); 9118 } 9119 onWindowStageDestroy() { 9120 // Main window is destroyed, release UI related resources 9121 console.log("[Demo] MainAbility onWindowStageDestroy"); 9122 } 9123 onForeground() { 9124 // Ability has brought to foreground 9125 console.log("[Demo] MainAbility onForeground"); 9126 } 9127 onBackground() { 9128 // Ability has back to background 9129 console.log("[Demo] MainAbility onBackground"); 9130 } 9131 }; 9132 ``` 9133