1# RPC 2 3This module implements communication between processes, including inter-process communication (IPC) on a single device and Remote Procedure Call (RPC) between processes of difference devices. IPC is implemented based on the Binder driver, and RPC is based on the software bus driver. 4 5> **NOTE**<br> 6> 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. 7 8 9## Modules to Import 10 11 12``` 13import rpc from '@ohos.rpc'; 14``` 15 16 17## MessageParcel 18 19Provides methods for reading and writing basic data types and arrays, inter-process communication (IPC) objects, interface tokens, and sequenceable objects. 20 21 22### create 23 24create(): MessageParcel 25 26Creates a **MessageParcel** object. This method is a static method. 27 28**System capability**: SystemCapability.Communication.IPC.Core 29 30**Return value** 31 | Type| Description| 32 | -------- | -------- | 33 | MessageParcel | **MessageParcel** object created.| 34 35**Example** 36 37 ``` 38 let data = rpc.MessageParcel.create(); 39 console.log("RpcClient: data is " + data); 40 ``` 41 42 43### reclaim 44 45reclaim(): void 46 47Reclaims the **MessageParcel** object that is no longer used. 48 49**System capability**: SystemCapability.Communication.IPC.Core 50 51**Example** 52 53 ``` 54 let reply = rpc.MessageParcel.create(); 55 reply.reclaim(); 56 ``` 57 58 59### writeRemoteObject 60 61writeRemoteObject(object: [IRemoteObject](#iremoteobject)): boolean 62 63 Serializes a remote object and writes it to this **MessageParcel** object. 64 65**System capability**: SystemCapability.Communication.IPC.Core 66 67**Parameters** 68 | Name| Type| Mandatory| Description| 69 | -------- | -------- | -------- | -------- | 70 | object | [IRemoteObject](#iremoteobject) | Yes| Remote object to serialize and write to the **MessageParcel** object.| 71 72**Return value** 73 | Type| Description| 74 | -------- | -------- | 75 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 76 77**Example** 78 79 ``` 80 class MyDeathRecipient { 81 onRemoteDied() { 82 console.log("server died"); 83 } 84 } 85 class TestRemoteObject extends rpc.RemoteObject { 86 constructor(descriptor) { 87 super(descriptor); 88 } 89 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 90 return true; 91 } 92 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 93 return true; 94 } 95 isObjectDead(): boolean { 96 return false; 97 } 98 } 99 let data = rpc.MessageParcel.create(); 100 let testRemoteObject = new TestRemoteObject("testObject"); 101 data.writeRemoteObject(testRemoteObject); 102 ``` 103 104 105### readRemoteObject 106 107readRemoteObject(): IRemoteObject 108 109Reads 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. 110 111**System capability**: SystemCapability.Communication.IPC.Core 112 113**Return value** 114 | Type| Description| 115 | -------- | -------- | 116 | [IRemoteObject](#iremoteobject) | Remote object obtained.| 117 118**Example** 119 120 ``` 121 class MyDeathRecipient { 122 onRemoteDied() { 123 console.log("server died"); 124 } 125 } 126 class TestRemoteObject extends rpc.RemoteObject { 127 constructor(descriptor) { 128 super(descriptor); 129 } 130 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 131 return true; 132 } 133 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 134 return true; 135 } 136 isObjectDead(): boolean { 137 return false; 138 } 139 } 140 let data = rpc.MessageParcel.create(); 141 let testRemoteObject = new TestRemoteObject("testObject"); 142 data.writeRemoteObject(testRemoteObject); 143 let proxy = data.readRemoteObject(); 144 ``` 145 146 147### writeInterfaceToken 148 149writeInterfaceToken(token: string): boolean 150 151Writes an interface token to this **MessageParcel** object. 152 153**System capability**: SystemCapability.Communication.IPC.Core 154 155**Parameters** 156 | Name| Type| Mandatory| Description| 157 | -------- | -------- | -------- | -------- | 158 | token | string | Yes| Interface token to write.| 159 160**Return value** 161 | Type| Description| 162 | -------- | -------- | 163 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 164 165**Example** 166 167 ``` 168 let data = rpc.MessageParcel.create(); 169 let result = data.writeInterfaceToken("aaa"); 170 console.log("RpcServer: writeInterfaceToken is " + result); 171 ``` 172 173 174### readInterfaceToken 175 176readInterfaceToken(): string 177 178Reads the interface token from this **MessageParcel** object. The interface tokens are read in the order in which they are written into the **MessageParcel** object. 179 180**System capability**: SystemCapability.Communication.IPC.Core 181 182**Return value** 183 | Type| Description| 184 | -------- | -------- | 185 | string | Interface token obtained.| 186 187**Example** 188 189 ``` 190 class Stub extends rpc.RemoteObject { 191 onRemoteRequest(code, data, reply, option) { 192 let interfaceToken = data.readInterfaceToken(); 193 console.log("RpcServer: interfaceToken is " + interfaceToken); 194 return true; 195 } 196 } 197 ``` 198 199 200### getSize 201 202getSize(): number 203 204Obtains the data size of this **MessageParcel** object. 205 206**System capability**: SystemCapability.Communication.IPC.Core 207 208**Return value** 209 | Type| Description| 210 | -------- | -------- | 211 | number | Size of the **MessageParcel** object obtained, in bytes.| 212 213**Example** 214 215 ``` 216 let data = rpc.MessageParcel.create(); 217 let size = data.getSize(); 218 console.log("RpcClient: size is " + size); 219 ``` 220 221 222### getCapacity 223 224getCapacity(): number 225 226Obtains the capacity of this **MessageParcel** object. 227 228**System capability**: SystemCapability.Communication.IPC.Core 229 230**Return value** 231 | Type| Description| 232 | -------- | -------- | 233 | number | **MessageParcel** capacity obtained, in bytes.| 234 235**Example** 236 237 ``` 238 let data = rpc.MessageParcel.create(); 239 let result = data.getCapacity(); 240 console.log("RpcClient: capacity is " + result); 241 ``` 242 243 244### setSize 245 246setSize(size: number): boolean 247 248Sets the size of data contained in this **MessageParcel** object. 249 250**System capability**: SystemCapability.Communication.IPC.Core 251 252**Parameters** 253 | Name| Type| Mandatory| Description| 254 | -------- | -------- | -------- | -------- | 255 | size | number | Yes| Data size to set, in bytes.| 256 257**Return value** 258 | Type| Description| 259 | -------- | -------- | 260 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 261 262**Example** 263 264 ``` 265 let data = rpc.MessageParcel.create(); 266 let setSize = data.setSize(16); 267 console.log("RpcClient: setSize is " + setSize); 268 ``` 269 270 271### setCapacity 272 273setCapacity(size: number): boolean 274 275Sets the storage capacity of this **MessageParcel** object. 276 277**System capability**: SystemCapability.Communication.IPC.Core 278 279**Parameters** 280 | Name| Type| Mandatory| Description| 281 | -------- | -------- | -------- | -------- | 282 | size | number | Yes| Storage capacity to set, in bytes.| 283 284**Return value** 285 | Type| Description| 286 | -------- | -------- | 287 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 288 289**Example** 290 291 ``` 292 let data = rpc.MessageParcel.create(); 293 let result = data.setCapacity(100); 294 console.log("RpcClient: setCapacity is " + result); 295 ``` 296 297 298### getWritableBytes 299 300getWritableBytes(): number 301 302Obtains the writable capacity of this **MessageParcel** object. 303 304**System capability**: SystemCapability.Communication.IPC.Core 305 306**Return value** 307 | Type| Description| 308 | -------- | -------- | 309 | number | **MessageParcel** writable capacity obtained, in bytes.| 310 311**Example** 312 313 ``` 314 class Stub extends rpc.RemoteObject { 315 onRemoteRequest(code, data, reply, option) { 316 let getWritableBytes = data.getWritableBytes(); 317 console.log("RpcServer: getWritableBytes is " + getWritableBytes); 318 return true; 319 } 320 } 321 ``` 322 323 324### getReadableBytes 325 326getReadableBytes(): number 327 328Obtains the readable capacity of this **MessageParcel** object. 329 330**System capability**: SystemCapability.Communication.IPC.Core 331 332**Return value** 333 | Type| Description| 334 | -------- | -------- | 335 | number | **MessageParcel** object readable capacity, in bytes.| 336 337**Example** 338 339 ``` 340 class Stub extends rpc.RemoteObject { 341 onRemoteRequest(code, data, reply, option) { 342 let result = data.getReadableBytes(); 343 console.log("RpcServer: getReadableBytes is " + result); 344 return true; 345 } 346 } 347 ``` 348 349 350### getReadPosition 351 352getReadPosition(): number 353 354Obtains the read position of this **MessageParcel** object. 355 356**System capability**: SystemCapability.Communication.IPC.Core 357 358**Return value** 359 | Type| Description| 360 | -------- | -------- | 361 | number | Current read position of the **MessageParcel** object.| 362 363**Example** 364 365 ``` 366 let data = rpc.MessageParcel.create(); 367 let readPos = data.getReadPosition(); 368 console.log("RpcClient: readPos is " + readPos); 369 ``` 370 371 372### getWritePosition 373 374getWritePosition(): number 375 376Obtains the write position of this **MessageParcel** object. 377 378**System capability**: SystemCapability.Communication.IPC.Core 379 380**Return value** 381 | Type| Description| 382 | -------- | -------- | 383 | number | Current write position of the **MessageParcel** object.| 384 385**Example** 386 387 ``` 388 let data = rpc.MessageParcel.create(); 389 data.writeInt(10); 390 let bwPos = data.getWritePosition(); 391 console.log("RpcClient: bwPos is " + bwPos); 392 ``` 393 394 395### rewindRead 396 397rewindRead(pos: number): boolean 398 399Moves the read pointer to the specified position. 400 401**System capability**: SystemCapability.Communication.IPC.Core 402 403**Parameters** 404 | Name| Type| Mandatory| Description| 405 | -------- | -------- | -------- | -------- | 406 | pos | number | Yes| Position from which data is to read.| 407 408**Return value** 409 | Type| Description| 410 | -------- | -------- | 411 | boolean | Returns **true** if the read position changes; returns **false** otherwise.| 412 413**Example** 414 415 ``` 416 let data = rpc.MessageParcel.create(); 417 data.writeInt(12); 418 data.writeString("parcel"); 419 let number = data.readInt(); 420 console.log("RpcClient: number is " + number); 421 data.rewindRead(0); 422 let number2 = data.readInt(); 423 console.log("RpcClient: rewindRead is " + number2); 424 ``` 425 426 427### rewindWrite 428 429rewindWrite(pos: number): boolean 430 431Moves the write pointer to the specified position. 432 433**System capability**: SystemCapability.Communication.IPC.Core 434 435**Parameters** 436 | Name| Type| Mandatory| Description| 437 | -------- | -------- | -------- | -------- | 438 | pos | number | Yes| Position from which data is to write.| 439 440**Return value** 441 | Type| Description| 442 | -------- | -------- | 443 | boolean | Returns **true** if the write position changes; returns **false** otherwise.| 444 445**Example** 446 447 ``` 448 let data = rpc.MessageParcel.create(); 449 data.writeInt(4); 450 data.rewindWrite(0); 451 data.writeInt(5); 452 let number = data.readInt(); 453 console.log("RpcClient: rewindWrite is: " + number); 454 ``` 455 456 457### writeByte 458 459writeByte(val: number): boolean 460 461Writes a Byte value to this **MessageParcel** object. 462 463**System capability**: SystemCapability.Communication.IPC.Core 464 465**Parameters** 466 | Name| Type| Mandatory| Description| 467 | -------- | -------- | -------- | -------- | 468 | val | number | Yes| Byte value to write.| 469 470**Return value** 471 | Type| Description| 472 | -------- | -------- | 473 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 474 475**Example** 476 477 ``` 478 let data = rpc.MessageParcel.create(); 479 let result = data.writeByte(2); 480 console.log("RpcClient: writeByte is: " + result); 481 ``` 482 483 484### readByte 485 486readByte(): number 487 488Reads the Byte value from this **MessageParcel** object. 489 490**System capability**: SystemCapability.Communication.IPC.Core 491 492**Return value** 493 | Type| Description| 494 | -------- | -------- | 495 | number | Byte value read.| 496 497**Example** 498 499 ``` 500 let data = rpc.MessageParcel.create(); 501 let result = data.writeByte(2); 502 console.log("RpcClient: writeByte is: " + result); 503 let ret = data.readByte(); 504 console.log("RpcClient: readByte is: " + ret); 505 ``` 506 507 508### writeShort 509 510writeShort(val: number): boolean 511 512Writes a Short int value to this **MessageParcel** object. 513 514**System capability**: SystemCapability.Communication.IPC.Core 515 516**Parameters** 517 | Name| Type| Mandatory| Description| 518 | -------- | -------- | -------- | -------- | 519 | val | number | Yes| Short int value to write.| 520 521**Return value** 522 | Type| Description| 523 | -------- | -------- | 524 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 525 526**Example** 527 528 ``` 529 let data = rpc.MessageParcel.create(); 530 let result = data.writeShort(8); 531 console.log("RpcClient: writeShort is: " + result); 532 ``` 533 534 535### readShort 536 537readShort(): number 538 539Reads the Short int value from this **MessageParcel** object. 540 541**System capability**: SystemCapability.Communication.IPC.Core 542 543**Return value** 544 | Type| Description| 545 | -------- | -------- | 546 | number | Short int value read.| 547 548**Example** 549 550 ``` 551 let data = rpc.MessageParcel.create(); 552 let result = data.writeShort(8); 553 console.log("RpcClient: writeShort is: " + result); 554 let ret = data.readShort(); 555 console.log("RpcClient: readShort is: " + ret); 556 ``` 557 558 559### writeInt 560 561writeInt(val: number): boolean 562 563Writes an Int value to this **MessageParcel** object. 564 565**System capability**: SystemCapability.Communication.IPC.Core 566 567**Parameters** 568 | Name| Type| Mandatory| Description| 569 | -------- | -------- | -------- | -------- | 570 | val | number | Yes| Int value to write.| 571 572**Return value** 573 | Type| Description| 574 | -------- | -------- | 575 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 576 577**Example** 578 579 ``` 580 let data = rpc.MessageParcel.create(); 581 let result = data.writeInt(10); 582 console.log("RpcClient: writeInt is " + result); 583 ``` 584 585 586### readInt 587 588readInt(): number 589 590Reads the Int value from this **MessageParcel** object. 591 592**System capability**: SystemCapability.Communication.IPC.Core 593 594**Return value** 595 | Type| Description| 596 | -------- | -------- | 597 | number | Int value read.| 598 599**Example** 600 601 ``` 602 let data = rpc.MessageParcel.create(); 603 let result = data.writeInt(10); 604 console.log("RpcClient: writeInt is " + result); 605 let ret = data.readInt(); 606 console.log("RpcClient: readInt is " + ret); 607 ``` 608 609 610### writeLong 611 612writeLong(val: number): boolean 613 614Writes a Long int value to this **MessageParcel** object. 615 616**System capability**: SystemCapability.Communication.IPC.Core 617 618**Parameters** 619 | Name| Type| Mandatory| Description| 620 | -------- | -------- | -------- | -------- | 621 | val | number | Yes| Long int value to write.| 622 623**Return value** 624 | Type| Description| 625 | -------- | -------- | 626 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 627 628**Example** 629 630 ``` 631 let data = rpc.MessageParcel.create(); 632 let result = data.writeLong(10000); 633 console.log("RpcClient: writeLong is " + result); 634 ``` 635 636 637### readLong 638 639readLong(): number 640 641Reads the Long int value from this **MessageParcel** object. 642 643**System capability**: SystemCapability.Communication.IPC.Core 644 645**Return value** 646 | Type| Description| 647 | -------- | -------- | 648 | number | Long int value read.| 649 650**Example** 651 652 ``` 653 let data = rpc.MessageParcel.create(); 654 let result = data.writeLong(10000); 655 console.log("RpcClient: writeLong is " + result); 656 let ret = data.readLong(); 657 console.log("RpcClient: readLong is " + ret); 658 ``` 659 660 661### writeFloat 662 663writeFloat(val: number): boolean 664 665Writes a Float value to this **MessageParcel** object. 666 667**System capability**: SystemCapability.Communication.IPC.Core 668 669**Parameters** 670 | Name| Type| Mandatory| Description| 671 | -------- | -------- | -------- | -------- | 672 | val | number | Yes| Float value to write.| 673 674**Return value** 675 | Type| Description| 676 | -------- | -------- | 677 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 678 679**Example** 680 681 ``` 682 let data = rpc.MessageParcel.create(); 683 let result = data.writeFloat(1.2); 684 console.log("RpcClient: writeFloat is " + result); 685 ``` 686 687 688### readFloat 689 690readFloat(): number 691 692Reads the Float value from this **MessageParcel** object. 693 694**System capability**: SystemCapability.Communication.IPC.Core 695 696**Return value** 697 | Type| Description| 698 | -------- | -------- | 699 | number | Float value read.| 700 701**Example** 702 703 ``` 704 let data = rpc.MessageParcel.create(); 705 let result = data.writeFloat(1.2); 706 console.log("RpcClient: writeFloat is " + result); 707 let ret = data.readFloat(); 708 console.log("RpcClient: readFloat is " + ret); 709 ``` 710 711 712### writeDouble 713 714writeDouble(val: number): boolean 715 716Writes a Double value to this **MessageParcel** object. 717 718**System capability**: SystemCapability.Communication.IPC.Core 719 720**Parameters** 721 | Name| Type| Mandatory| Description| 722 | -------- | -------- | -------- | -------- | 723 | val | number | Yes| Double value to write.| 724 725**Return value** 726 | Type| Description| 727 | -------- | -------- | 728 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 729 730**Example** 731 732 ``` 733 let data = rpc.MessageParcel.create(); 734 let result = data.writeDouble(10.2); 735 console.log("RpcClient: writeDouble is " + result); 736 ``` 737 738 739### readDouble 740 741readDouble(): number 742 743Reads the Double value from this **MessageParcel** object. 744 745**System capability**: SystemCapability.Communication.IPC.Core 746 747**Return value** 748 | Type| Description| 749 | -------- | -------- | 750 | number | Double value read.| 751 752**Example** 753 754 ``` 755 let data = rpc.MessageParcel.create(); 756 let result = data.writeDouble(10.2); 757 console.log("RpcClient: writeDouble is " + result); 758 let ret = data.readDouble(); 759 console.log("RpcClient: readDouble is " + ret); 760 ``` 761 762 763### writeBoolean 764 765writeBoolean(val: boolean): boolean 766 767Writes a Boolean value to this **MessageParcel** object. 768 769**System capability**: SystemCapability.Communication.IPC.Core 770 771**Parameters** 772 | Name| Type| Mandatory| Description| 773 | -------- | -------- | -------- | -------- | 774 | val | boolean | Yes| Boolean value to write.| 775 776**Return value** 777 | Type| Description| 778 | -------- | -------- | 779 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 780 781**Example** 782 783 ``` 784 let data = rpc.MessageParcel.create(); 785 let result = data.writeBoolean(false); 786 console.log("RpcClient: writeBoolean is " + result); 787 ``` 788 789 790### readBoolean 791 792readBoolean(): boolean 793 794Reads the Boolean value from this **MessageParcel** object. 795 796**System capability**: SystemCapability.Communication.IPC.Core 797 798**Return value** 799 | Type| Description| 800 | -------- | -------- | 801 | boolean | Boolean value read.| 802 803**Example** 804 805 ``` 806 let data = rpc.MessageParcel.create(); 807 let result = data.writeBoolean(false); 808 console.log("RpcClient: writeBoolean is " + result); 809 let ret = data.readBoolean(); 810 console.log("RpcClient: readBoolean is " + ret); 811 ``` 812 813 814### writeChar 815 816writeChar(val: number): boolean 817 818Writes a Char value to this **MessageParcel** object. 819 820**System capability**: SystemCapability.Communication.IPC.Core 821 822**Parameters** 823 | Name| Type| Mandatory| Description| 824 | -------- | -------- | -------- | -------- | 825 | val | number | Yes| Char value to write.| 826 827**Return value** 828 | Type| Description| 829 | -------- | -------- | 830 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 831 832**Example** 833 834 ``` 835 let data = rpc.MessageParcel.create(); 836 let result = data.writeChar(97); 837 console.log("RpcClient: writeChar is " + result); 838 ``` 839 840 841### readChar 842 843readChar(): number 844 845Reads the Char value from this **MessageParcel** object. 846 847**System capability**: SystemCapability.Communication.IPC.Core 848 849**Return value** 850 | Type| Description| 851 | -------- | -------- | 852 | number | Char value read.| 853 854**Example** 855 856 ``` 857 let data = rpc.MessageParcel.create(); 858 let result = data.writeChar(97); 859 console.log("RpcClient: writeChar is " + result); 860 let ret = data.readChar(); 861 console.log("RpcClient: readChar is " + ret); 862 ``` 863 864 865### writeString 866 867writeString(val: string): boolean 868 869Writes a String value to this **MessageParcel** object. 870 871**System capability**: SystemCapability.Communication.IPC.Core 872 873**Parameters** 874 | Name| Type| Mandatory| Description| 875 | -------- | -------- | -------- | -------- | 876 | val | string | Yes| String value to write. The length of the value must be less than 40960 bytes.| 877 878**Return value** 879 | Type| Description| 880 | -------- | -------- | 881 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 882 883**Example** 884 885 ``` 886 let data = rpc.MessageParcel.create(); 887 let result = data.writeString('abc'); 888 console.log("RpcClient: writeString is " + result); 889 ``` 890 891 892### readString 893 894readString(): string 895 896Reads the String value from this **MessageParcel** object. 897 898**System capability**: SystemCapability.Communication.IPC.Core 899 900**Return value** 901 | Type| Description| 902 | -------- | -------- | 903 | string | String value read.| 904 905**Example** 906 907 ``` 908 let data = rpc.MessageParcel.create(); 909 let result = data.writeString('abc'); 910 console.log("RpcClient: writeString is " + result); 911 let ret = data.readString(); 912 console.log("RpcClient: readString is " + ret); 913 ``` 914 915 916### writeSequenceable 917 918writeSequenceable(val: Sequenceable): boolean 919 920Writes a sequenceable object to this **MessageParcel** object. 921 922**System capability**: SystemCapability.Communication.IPC.Core 923 924**Parameters** 925 | Name| Type| Mandatory| Description| 926 | -------- | -------- | -------- | -------- | 927 | val | [Sequenceable](#sequenceable) | Yes| Sequenceable object to write.| 928 929**Return value** 930 | Type| Description| 931 | -------- | -------- | 932 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 933 934**Example** 935 936 ``` 937 class MySequenceable { 938 num: number; 939 str: string; 940 constructor(num, str) { 941 this.num = num; 942 this.str = str; 943 } 944 marshalling(messageParcel) { 945 messageParcel.writeInt(this.num); 946 messageParcel.writeString(this.str); 947 return true; 948 } 949 unmarshalling(messageParcel) { 950 this.num = messageParcel.readInt(); 951 this.str = messageParcel.readString(); 952 return true; 953 } 954 } 955 let sequenceable = new MySequenceable(1, "aaa"); 956 let data = rpc.MessageParcel.create(); 957 let result = data.writeSequenceable(sequenceable); 958 console.log("RpcClient: writeSequenceable is " + result); 959 ``` 960 961 962### readSequenceable 963 964readSequenceable(dataIn: Sequenceable) : boolean 965 966Reads member variables from this **MessageParcel** object. 967 968**System capability**: SystemCapability.Communication.IPC.Core 969 970**Parameters** 971 | Name| Type| Mandatory| Description| 972 | -------- | -------- | -------- | -------- | 973 | dataIn | [Sequenceable](#sequenceable) | Yes| Object that reads member variables from the **MessageParcel** object.| 974 975**Return value** 976 | Type| Description| 977 | -------- | -------- | 978 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 979 980**Example** 981 982 ``` 983 class MySequenceable { 984 num: number; 985 str: string; 986 constructor(num, str) { 987 this.num = num; 988 this.str = str; 989 } 990 marshalling(messageParcel) { 991 messageParcel.writeInt(this.num); 992 messageParcel.writeString(this.str); 993 return true; 994 } 995 unmarshalling(messageParcel) { 996 this.num = messageParcel.readInt(); 997 this.str = messageParcel.readString(); 998 return true; 999 } 1000 } 1001 let sequenceable = new MySequenceable(1, "aaa"); 1002 let data = rpc.MessageParcel.create(); 1003 let result = data.writeSequenceable(sequenceable); 1004 console.log("RpcClient: writeSequenceable is " + result); 1005 let ret = new MySequenceable(0, ""); 1006 let result2 = data.readSequenceable(ret); 1007 console.log("RpcClient: writeSequenceable is " + result2); 1008 ``` 1009 1010 1011### writeByteArray 1012 1013writeByteArray(byteArray: number[]): boolean 1014 1015Writes a ByteArray to this **MessageParcel** object. 1016 1017**System capability**: SystemCapability.Communication.IPC.Core 1018 1019**Parameters** 1020 | Name| Type| Mandatory| Description| 1021 | -------- | -------- | -------- | -------- | 1022 | byteArray | number[] | Yes| ByteArray to write.| 1023 1024**Return value** 1025 | Type| Description| 1026 | -------- | -------- | 1027 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1028 1029**Example** 1030 1031 ``` 1032 let data = rpc.MessageParcel.create(); 1033 let ByteArrayVar = [1, 2, 3, 4, 5]; 1034 let result = data.writeByteArray(ByteArrayVar); 1035 console.log("RpcClient: writeByteArray is " + result); 1036 ``` 1037 1038 1039### readByteArray 1040 1041readByteArray(dataIn: number[]) : void 1042 1043Reads the ByteArray from this **MessageParcel** object. 1044 1045**System capability**: SystemCapability.Communication.IPC.Core 1046 1047**Parameters** 1048 | Name| Type| Mandatory| Description| 1049 | -------- | -------- | -------- | -------- | 1050 | dataIn | number[] | Yes| ByteArray to read.| 1051 1052**Example** 1053 1054 ``` 1055 let data = rpc.MessageParcel.create(); 1056 let ByteArrayVar = [1, 2, 3, 4, 5]; 1057 let result = data.writeByteArray(ByteArrayVar); 1058 console.log("RpcClient: writeByteArray is " + result); 1059 let array = new Array(5); 1060 data.readByteArray(array); 1061 ``` 1062 1063 1064### readByteArray 1065 1066readByteArray(): number[] 1067 1068Reads the ByteArray from this **MessageParcel** object. 1069 1070**System capability**: SystemCapability.Communication.IPC.Core 1071 1072**Return value** 1073 | Type| Description| 1074 | -------- | -------- | 1075 | number[] | ByteArray read.| 1076 1077**Example** 1078 1079 ``` 1080 let data = rpc.MessageParcel.create(); 1081 let ByteArrayVar = [1, 2, 3, 4, 5]; 1082 let result = data.writeByteArray(ByteArrayVar); 1083 console.log("RpcClient: writeByteArray is " + result); 1084 let array = data.readByteArray(); 1085 console.log("RpcClient: readByteArray is " + array); 1086 ``` 1087 1088 1089### writeShortArray 1090 1091writeShortArray(shortArray: number[]): boolean 1092 1093Writes a ShortArray to this **MessageParcel** object. 1094 1095**System capability**: SystemCapability.Communication.IPC.Core 1096 1097**Parameters** 1098 | Name| Type| Mandatory| Description| 1099 | -------- | -------- | -------- | -------- | 1100 | shortArray | number[] | Yes| ShortArray to write.| 1101 1102**Return value** 1103 | Type| Description| 1104 | -------- | -------- | 1105 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1106 1107**Example** 1108 1109 ``` 1110 let data = rpc.MessageParcel.create(); 1111 let result = data.writeShortArray([11, 12, 13]); 1112 console.log("RpcClient: writeShortArray is " + result); 1113 ``` 1114 1115 1116### readShortArray 1117 1118readShortArray(dataIn: number[]) : void 1119 1120Reads a ShortArray from this **MessageParcel** object. 1121 1122**System capability**: SystemCapability.Communication.IPC.Core 1123 1124**Parameters** 1125 | Name| Type| Mandatory| Description| 1126 | -------- | -------- | -------- | -------- | 1127 | dataIn | number[] | Yes| ShortArray to read.| 1128 1129**Example** 1130 1131 ``` 1132 let data = rpc.MessageParcel.create(); 1133 let result = data.writeShortArray([11, 12, 13]); 1134 console.log("RpcClient: writeShortArray is " + result); 1135 let array = new Array(3); 1136 data.readShortArray(array); 1137 ``` 1138 1139 1140### readShortArray 1141 1142readShortArray(): number[] 1143 1144Reads the ShortArray from this **MessageParcel** object. 1145 1146**System capability**: SystemCapability.Communication.IPC.Core 1147 1148**Return value** 1149 | Type| Description| 1150 | -------- | -------- | 1151 | number[] | ShortArray read.| 1152 1153**Example** 1154 1155 ``` 1156 let data = rpc.MessageParcel.create(); 1157 let result = data.writeShortArray([11, 12, 13]); 1158 console.log("RpcClient: writeShortArray is " + result); 1159 let array = data.readShortArray(); 1160 console.log("RpcClient: readShortArray is " + array); 1161 ``` 1162 1163 1164### writeIntArray 1165 1166writeIntArray(intArray: number[]): boolean 1167 1168Writes an IntArray to this **MessageParcel** object. 1169 1170**System capability**: SystemCapability.Communication.IPC.Core 1171 1172**Parameters** 1173 | Name| Type| Mandatory| Description| 1174 | -------- | -------- | -------- | -------- | 1175 | intArray | number[] | Yes| IntArray to write.| 1176 1177**Return value** 1178 | Type| Description| 1179 | -------- | -------- | 1180 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1181 1182**Example** 1183 1184 ``` 1185 let data = rpc.MessageParcel.create(); 1186 let result = data.writeIntArray([100, 111, 112]); 1187 console.log("RpcClient: writeIntArray is " + result); 1188 ``` 1189 1190 1191### readIntArray 1192 1193readIntArray(dataIn: number[]) : void 1194 1195Reads an IntArray from this **MessageParcel** object. 1196 1197**System capability**: SystemCapability.Communication.IPC.Core 1198 1199**Parameters** 1200 | Name| Type| Mandatory| Description| 1201 | -------- | -------- | -------- | -------- | 1202 | dataIn | number[] | Yes| IntArray to read.| 1203 1204**Example** 1205 1206 ``` 1207 let data = rpc.MessageParcel.create(); 1208 let result = data.writeIntArray([100, 111, 112]); 1209 console.log("RpcClient: writeIntArray is " + result); 1210 let array = new Array(3); 1211 data.readIntArray(array); 1212 ``` 1213 1214 1215### readIntArray 1216 1217readIntArray(): number[] 1218 1219Reads the IntArray from this **MessageParcel** object. 1220 1221**System capability**: SystemCapability.Communication.IPC.Core 1222 1223**Return value** 1224 | Type| Description| 1225 | -------- | -------- | 1226 | number[] | IntArray read.| 1227 1228**Example** 1229 1230 ``` 1231 let data = rpc.MessageParcel.create(); 1232 let result = data.writeIntArray([100, 111, 112]); 1233 console.log("RpcClient: writeIntArray is " + result); 1234 let array = data.readIntArray(); 1235 console.log("RpcClient: readIntArray is " + array); 1236 ``` 1237 1238 1239### writeLongArray 1240 1241writeLongArray(longArray: number[]): boolean 1242 1243Writes a LongArray to this **MessageParcel** object. 1244 1245**System capability**: SystemCapability.Communication.IPC.Core 1246 1247**Parameters** 1248 | Name| Type| Mandatory| Description| 1249 | -------- | -------- | -------- | -------- | 1250 | longArray | number[] | Yes| LongArray to write.| 1251 1252**Return value** 1253 | Type| Description| 1254 | -------- | -------- | 1255 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1256 1257**Example** 1258 1259 ``` 1260 let data = rpc.MessageParcel.create(); 1261 let result = data.writeLongArray([1111, 1112, 1113]); 1262 console.log("RpcClient: writeLongArray is " + result); 1263 ``` 1264 1265 1266### readLongArray 1267 1268readLongArray(dataIn: number[]) : void 1269 1270Reads a LongArray from this **MessageParcel** object. 1271 1272**System capability**: SystemCapability.Communication.IPC.Core 1273 1274**Parameters** 1275 | Name| Type| Mandatory| Description| 1276 | -------- | -------- | -------- | -------- | 1277 | dataIn | number[] | Yes| LongArray to read.| 1278 1279**Example** 1280 1281 ``` 1282 let data = rpc.MessageParcel.create(); 1283 let result = data.writeLongArray([1111, 1112, 1113]); 1284 console.log("RpcClient: writeLongArray is " + result); 1285 let array = new Array(3); 1286 data.readLongArray(array); 1287 ``` 1288 1289 1290### readLongArray 1291 1292readLongArray(): number[] 1293 1294Reads the LongArray from this **MessageParcel** object. 1295 1296**System capability**: SystemCapability.Communication.IPC.Core 1297 1298**Return value** 1299 | Type| Description| 1300 | -------- | -------- | 1301 | number[] | LongArray read.| 1302 1303**Example** 1304 1305 ``` 1306 let data = rpc.MessageParcel.create(); 1307 let result = data.writeLongArray([1111, 1112, 1113]); 1308 console.log("RpcClient: writeLongArray is " + result); 1309 let array = data.readLongArray(); 1310 console.log("RpcClient: readLongArray is " + array); 1311 ``` 1312 1313 1314### writeFloatArray 1315 1316writeFloatArray(floatArray: number[]): boolean 1317 1318Writes a FloatArray to this **MessageParcel** object. 1319 1320**System capability**: SystemCapability.Communication.IPC.Core 1321 1322**Parameters** 1323 | Name| Type| Mandatory| Description| 1324 | -------- | -------- | -------- | -------- | 1325 | floatArray | number[] | Yes| FloatArray to write.| 1326 1327**Return value** 1328 | Type| Description| 1329 | -------- | -------- | 1330 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1331 1332**Example** 1333 1334 ``` 1335 let data = rpc.MessageParcel.create(); 1336 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 1337 console.log("RpcClient: writeFloatArray is " + result); 1338 ``` 1339 1340 1341### readFloatArray 1342 1343readFloatArray(dataIn: number[]) : void 1344 1345Reads a FloatArray from this **MessageParcel** object. 1346 1347**System capability**: SystemCapability.Communication.IPC.Core 1348 1349**Parameters** 1350 | Name| Type| Mandatory| Description| 1351 | -------- | -------- | -------- | -------- | 1352 | dataIn | number[] | Yes| FloatArray to read.| 1353 1354 1355**Example** 1356 1357 ``` 1358 let data = rpc.MessageParcel.create(); 1359 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 1360 console.log("RpcClient: writeFloatArray is " + result); 1361 let array = new Array(3); 1362 data.readFloatArray(array); 1363 ``` 1364 1365 1366### readFloatArray 1367 1368readFloatArray(): number[] 1369 1370Reads the FloatArray from this **MessageParcel** object. 1371 1372**System capability**: SystemCapability.Communication.IPC.Core 1373 1374**Return value** 1375 | Type| Description| 1376 | -------- | -------- | 1377 | number[] | FloatArray read.| 1378 1379**Example** 1380 1381 ``` 1382 let data = rpc.MessageParcel.create(); 1383 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 1384 console.log("RpcClient: writeFloatArray is " + result); 1385 let array = data.readFloatArray(); 1386 console.log("RpcClient: readFloatArray is " + array); 1387 ``` 1388 1389 1390### writeDoubleArray 1391 1392writeDoubleArray(doubleArray: number[]): boolean 1393 1394Writes a DoubleArray to this **MessageParcel** object. 1395 1396**System capability**: SystemCapability.Communication.IPC.Core 1397 1398**Parameters** 1399 | Name| Type| Mandatory| Description| 1400 | -------- | -------- | -------- | -------- | 1401 | doubleArray | number[] | Yes| DoubleArray to write.| 1402 1403**Return value** 1404 | Type| Description| 1405 | -------- | -------- | 1406 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1407 1408**Example** 1409 1410 ``` 1411 let data = rpc.MessageParcel.create(); 1412 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 1413 console.log("RpcClient: writeDoubleArray is " + result); 1414 ``` 1415 1416 1417### readDoubleArray 1418 1419readDoubleArray(dataIn: number[]) : void 1420 1421Reads a DoubleArray from this **MessageParcel** object. 1422 1423**System capability**: SystemCapability.Communication.IPC.Core 1424 1425**Parameters** 1426 | Name| Type| Mandatory| Description| 1427 | -------- | -------- | -------- | -------- | 1428 | dataIn | number[] | Yes| DoubleArray to read.| 1429 1430**Example** 1431 1432 ``` 1433 let data = rpc.MessageParcel.create(); 1434 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 1435 console.log("RpcClient: writeDoubleArray is " + result); 1436 let array = new Array(3); 1437 data.readDoubleArray(array); 1438 ``` 1439 1440 1441### readDoubleArray 1442 1443readDoubleArray(): number[] 1444 1445Reads the DoubleArray from this **MessageParcel** object. 1446 1447**System capability**: SystemCapability.Communication.IPC.Core 1448 1449**Return value** 1450 | Type| Description| 1451 | -------- | -------- | 1452 | number[] | DoubleArray read.| 1453 1454**Example** 1455 1456 ``` 1457 let data = rpc.MessageParcel.create(); 1458 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 1459 console.log("RpcClient: writeDoubleArray is " + result); 1460 let array = data.readDoubleArray(); 1461 console.log("RpcClient: readDoubleArray is " + array); 1462 ``` 1463 1464 1465### writeBooleanArray 1466 1467writeBooleanArray(booleanArray: boolean[]): boolean 1468 1469Writes a BooleanArray to this **MessageParcel** object. 1470 1471**System capability**: SystemCapability.Communication.IPC.Core 1472 1473**Parameters** 1474 | Name| Type| Mandatory| Description| 1475 | -------- | -------- | -------- | -------- | 1476 | booleanArray | boolean[] | Yes| BooleanArray to write.| 1477 1478**Return value** 1479 | Type| Description| 1480 | -------- | -------- | 1481 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1482 1483**Example** 1484 1485 ``` 1486 let data = rpc.MessageParcel.create(); 1487 let result = data.writeBooleanArray([false, true, false]); 1488 console.log("RpcClient: writeBooleanArray is " + result); 1489 ``` 1490 1491 1492### readBooleanArray 1493 1494readBooleanArray(dataIn: boolean[]) : void 1495 1496Reads a BooleanArray from this **MessageParcel** object. 1497 1498**System capability**: SystemCapability.Communication.IPC.Core 1499 1500**Parameters** 1501 | Name| Type| Mandatory| Description| 1502 | -------- | -------- | -------- | -------- | 1503 | dataIn | boolean[] | Yes| BooleanArray to read.| 1504 1505**Example** 1506 1507 ``` 1508 let data = rpc.MessageParcel.create(); 1509 let result = data.writeBooleanArray([false, true, false]); 1510 console.log("RpcClient: writeBooleanArray is " + result); 1511 let array = new Array(3); 1512 data.readBooleanArray(array); 1513 ``` 1514 1515 1516### readBooleanArray 1517 1518readBooleanArray(): boolean[] 1519 1520Reads the BooleanArray from this **MessageParcel** object. 1521 1522**System capability**: SystemCapability.Communication.IPC.Core 1523 1524**Return value** 1525 | Type| Description| 1526 | -------- | -------- | 1527 | boolean[] | BooleanArray read.| 1528 1529 1530 ``` 1531 let data = rpc.MessageParcel.create(); 1532 let result = data.writeBooleanArray([false, true, false]); 1533 console.log("RpcClient: writeBooleanArray is " + result); 1534 let array = data.readBooleanArray(); 1535 console.log("RpcClient: readBooleanArray is " + array); 1536 ``` 1537 1538 1539### writeCharArray 1540 1541writeCharArray(charArray: number[]): boolean 1542 1543Writes a CharArray to this **MessageParcel** object. 1544 1545**System capability**: SystemCapability.Communication.IPC.Core 1546 1547**Parameters** 1548 | Name| Type| Mandatory| Description| 1549 | -------- | -------- | -------- | -------- | 1550 | charArray | number[] | Yes| CharArray to write.| 1551 1552**Return value** 1553 | Type| Description| 1554 | -------- | -------- | 1555 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1556 1557**Example** 1558 1559 ``` 1560 let data = rpc.MessageParcel.create(); 1561 let result = data.writeCharArray([97, 98, 88]); 1562 console.log("RpcClient: writeCharArray is " + result); 1563 ``` 1564 1565 1566### readCharArray 1567 1568readCharArray(dataIn: number[]) : void 1569 1570Reads a CharArray from this **MessageParcel** object. 1571 1572**System capability**: SystemCapability.Communication.IPC.Core 1573 1574**Parameters** 1575 | Name| Type| Mandatory| Description| 1576 | -------- | -------- | -------- | -------- | 1577 | dataIn | number[] | Yes| CharArray to read.| 1578 1579**Example** 1580 1581 ``` 1582 let data = rpc.MessageParcel.create(); 1583 let result = data.writeCharArray([97, 98, 99]); 1584 console.log("RpcClient: writeCharArray is " + result); 1585 let array = new Array(3); 1586 data.readCharArray(array); 1587 ``` 1588 1589 1590### readCharArray 1591 1592readCharArray(): number[] 1593 1594Reads the CharArray from this **MessageParcel** object. 1595 1596**System capability**: SystemCapability.Communication.IPC.Core 1597 1598**Return value** 1599 | Type| Description| 1600 | -------- | -------- | 1601 | number[] | CharArray read.| 1602 1603**Example** 1604 1605 ``` 1606 let data = rpc.MessageParcel.create(); 1607 let result = data.writeCharArray([97, 98, 99]); 1608 console.log("RpcClient: writeCharArray is " + result); 1609 let array = data.readCharArray(); 1610 console.log("RpcClient: readCharArray is " + array); 1611 ``` 1612 1613 1614### writeStringArray 1615 1616writeStringArray(stringArray: string[]): boolean 1617 1618Writes a StringArray to this **MessageParcel** object. 1619 1620**System capability**: SystemCapability.Communication.IPC.Core 1621 1622**Parameters** 1623 | Name| Type| Mandatory| Description| 1624 | -------- | -------- | -------- | -------- | 1625 | stringArray | string[] | Yes| StringArray to write. The length of a single element in the array must be less than 40960 bytes.| 1626 1627**Return value** 1628 | Type| Description| 1629 | -------- | -------- | 1630 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1631 1632**Example** 1633 1634 ``` 1635 let data = rpc.MessageParcel.create(); 1636 let result = data.writeStringArray(["abc", "def"]); 1637 console.log("RpcClient: writeStringArray is " + result); 1638 ``` 1639 1640 1641### readStringArray 1642 1643readStringArray(dataIn: string[]) : void 1644 1645Reads a StringArray from this **MessageParcel** object. 1646 1647**System capability**: SystemCapability.Communication.IPC.Core 1648 1649**Parameters** 1650 | Name| Type| Mandatory| Description| 1651 | -------- | -------- | -------- | -------- | 1652 | dataIn | string[] | Yes| StringArray to read.| 1653 1654**Example** 1655 1656 ``` 1657 let data = rpc.MessageParcel.create(); 1658 let result = data.writeStringArray(["abc", "def"]); 1659 console.log("RpcClient: writeStringArray is " + result); 1660 let array = new Array(2); 1661 data.readStringArray(array); 1662 ``` 1663 1664 1665### readStringArray 1666 1667readStringArray(): string[] 1668 1669Reads the StringArray from this **MessageParcel** object. 1670 1671**System capability**: SystemCapability.Communication.IPC.Core 1672 1673**Return value** 1674 | Type| Description| 1675 | -------- | -------- | 1676 | string[] | StringArray read.| 1677 1678**Example** 1679 1680 ``` 1681 let data = rpc.MessageParcel.create(); 1682 let result = data.writeStringArray(["abc", "def"]); 1683 console.log("RpcClient: writeStringArray is " + result); 1684 let array = data.readStringArray(); 1685 console.log("RpcClient: readStringArray is " + array); 1686 ``` 1687 1688 1689### writeNoException<sup>8+</sup> 1690 1691writeNoException(): void 1692 1693Writes information to this **MessageParcel** object indicating that no exception occurred. 1694 1695**System capability**: SystemCapability.Communication.IPC.Core 1696 1697**Example** 1698 1699 ``` 1700 class MyDeathRecipient { 1701 onRemoteDied() { 1702 console.log("server died"); 1703 } 1704 } 1705 class TestRemoteObject extends rpc.RemoteObject { 1706 constructor(descriptor) { 1707 super(descriptor); 1708 } 1709 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 1710 return true; 1711 } 1712 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 1713 return true; 1714 } 1715 isObjectDead(): boolean { 1716 return false; 1717 } 1718 onRemoteRequest(code, data, reply, option) { 1719 if (code === 1) { 1720 console.log("RpcServer: onRemoteRequest called"); 1721 reply.writeNoException(); 1722 return true; 1723 } else { 1724 console.log("RpcServer: unknown code: " + code); 1725 return false; 1726 } 1727 } 1728 } 1729 ``` 1730 1731 1732### readException<sup>8+</sup> 1733 1734readException(): void 1735 1736Reads the exception information from this **MessageParcel** object. 1737 1738**System capability**: SystemCapability.Communication.IPC.Core 1739 1740**Example** 1741 1742 ``` 1743 import FA from "@ohos.ability.featureAbility"; 1744 let proxy; 1745 let connect = { 1746 onConnect: function(elementName, remoteProxy) { 1747 console.log("RpcClient: js onConnect called."); 1748 proxy = remoteProxy; 1749 }, 1750 onDisconnect: function(elementName) { 1751 console.log("RpcClient: onDisconnect"); 1752 }, 1753 onFailed: function() { 1754 console.log("RpcClient: onFailed"); 1755 } 1756 }; 1757 let want = { 1758 "bundleName": "com.ohos.server", 1759 "abilityName": "com.ohos.server.MainAbility", 1760 }; 1761 FA.connectAbility(want, connect); 1762 let option = new rpc.MessageOption(); 1763 let data = rpc.MessageParcel.create(); 1764 let reply = rpc.MessageParcel.create(); 1765 data.writeInt(1); 1766 data.writeString("hello"); 1767 proxy.sendRequest(1, data, reply, option) 1768 .then(function(errCode) { 1769 if (errCode === 0) { 1770 console.log("sendRequest got result"); 1771 reply.readException(); 1772 let msg = reply.readString(); 1773 console.log("RPCTest: reply msg: " + msg); 1774 } else { 1775 console.log("RPCTest: sendRequest failed, errCode: " + errCode); 1776 } 1777 }).catch(function(e) { 1778 console.log("RPCTest: sendRequest got exception: " + e.message); 1779 }).finally (() => { 1780 console.log("RPCTest: sendRequest ends, reclaim parcel"); 1781 data.reclaim(); 1782 reply.reclaim(); 1783 }); 1784 ``` 1785 1786 1787### writeSequenceableArray 1788 1789writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean 1790 1791Writes a SequenceableArray to this **MessageParcel** object. 1792 1793**System capability**: SystemCapability.Communication.IPC.Core 1794 1795**Parameters** 1796 | Name| Type| Mandatory| Description| 1797 | -------- | -------- | -------- | -------- | 1798 | sequenceableArray | Sequenceable[] | Yes| SequenceableArray to write.| 1799 1800**Return value** 1801 | Type| Description| 1802 | -------- | -------- | 1803 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1804 1805**Example** 1806 1807 ``` 1808 class MySequenceable { 1809 num: number; 1810 str: string; 1811 constructor(num, str) { 1812 this.num = num; 1813 this.str = str; 1814 } 1815 marshalling(messageParcel) { 1816 messageParcel.writeInt(this.num); 1817 messageParcel.writeString(this.str); 1818 return true; 1819 } 1820 unmarshalling(messageParcel) { 1821 this.num = messageParcel.readInt(); 1822 this.str = messageParcel.readString(); 1823 return true; 1824 } 1825 } 1826 let sequenceable = new MySequenceable(1, "aaa"); 1827 let sequenceable2 = new MySequenceable(2, "bbb"); 1828 let sequenceable3 = new MySequenceable(3, "ccc"); 1829 let a = [sequenceable, sequenceable2, sequenceable3]; 1830 let data = rpc.MessageParcel.create(); 1831 let result = data.writeSequenceableArray(a); 1832 console.log("RpcClient: writeSequenceableArray is " + result); 1833 ``` 1834 1835 1836### readSequenceableArray<sup>8+</sup> 1837 1838readSequenceableArray(sequenceableArray: Sequenceable[]): void 1839 1840Reads a SequenceableArray from this **MessageParcel** object. 1841 1842**System capability**: SystemCapability.Communication.IPC.Core 1843 1844**Parameters** 1845 | Name| Type| Mandatory| Description| 1846 | -------- | -------- | -------- | -------- | 1847 | sequenceableArray | Sequenceable[] | Yes| SequenceableArray to read.| 1848 1849**Example** 1850 1851 ``` 1852 class MySequenceable { 1853 num: number; 1854 str: string; 1855 constructor(num, str) { 1856 this.num = num; 1857 this.str = str; 1858 } 1859 marshalling(messageParcel) { 1860 messageParcel.writeInt(this.num); 1861 messageParcel.writeString(this.str); 1862 return true; 1863 } 1864 unmarshalling(messageParcel) { 1865 this.num = messageParcel.readInt(); 1866 this.str = messageParcel.readString(); 1867 return true; 1868 } 1869 } 1870 let sequenceable = new MySequenceable(1, "aaa"); 1871 let sequenceable2 = new MySequenceable(2, "bbb"); 1872 let sequenceable3 = new MySequenceable(3, "ccc"); 1873 let a = [sequenceable, sequenceable2, sequenceable3]; 1874 let data = rpc.MessageParcel.create(); 1875 let result = data.writeSequenceableArray(a); 1876 console.log("RpcClient: writeSequenceableArray is " + result); 1877 let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")]; 1878 data.readSequenceableArray(b); 1879 ``` 1880 1881 1882### writeRemoteObjectArray<sup>8+</sup> 1883 1884writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean 1885 1886Writes an array of **IRemoteObject** objects to this **MessageParcel** object. 1887 1888**System capability**: SystemCapability.Communication.IPC.Core 1889 1890**Parameters** 1891 | Name| Type| Mandatory| Description| 1892 | -------- | -------- | -------- | -------- | 1893 | objectArray | IRemoteObject[] | Yes| Array of **IRemoteObject** objects to write.| 1894 1895**Return value** 1896 | Type| Description| 1897 | -------- | -------- | 1898 | boolean | Returns **true** if the operation is successful; returns **false** if the operation fails or if the **IRemoteObject** array is null.| 1899 1900**Example** 1901 1902 ``` 1903 class MyDeathRecipient { 1904 onRemoteDied() { 1905 console.log("server died"); 1906 } 1907 } 1908 class TestRemoteObject extends rpc.RemoteObject { 1909 constructor(descriptor) { 1910 super(descriptor); 1911 this.attachLocalInterface(this, descriptor); 1912 } 1913 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 1914 return true; 1915 } 1916 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 1917 return true; 1918 } 1919 isObjectDead(): boolean { 1920 return false; 1921 } 1922 asObject(): rpc.IRemoteObject { 1923 return this; 1924 } 1925 } 1926 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 1927 let data = rpc.MessageParcel.create(); 1928 let result = data.writeRemoteObjectArray(a); 1929 console.log("RpcClient: writeRemoteObjectArray is " + result); 1930 ``` 1931 1932 1933### readRemoteObjectArray<sup>8+</sup> 1934 1935readRemoteObjectArray(objects: IRemoteObject[]): void 1936 1937Reads an **IRemoteObject** array from this **MessageParcel** object. 1938 1939**System capability**: SystemCapability.Communication.IPC.Core 1940 1941**Parameters** 1942 | Name| Type| Mandatory| Description| 1943 | -------- | -------- | -------- | -------- | 1944 | objects | IRemoteObject[] | Yes| **IRemoteObject** array to read.| 1945 1946**Example** 1947 1948 ``` 1949 class MyDeathRecipient { 1950 onRemoteDied() { 1951 console.log("server died"); 1952 } 1953 } 1954 class TestRemoteObject extends rpc.RemoteObject { 1955 constructor(descriptor) { 1956 super(descriptor); 1957 this.attachLocalInterface(this, descriptor); 1958 } 1959 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 1960 return true; 1961 } 1962 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 1963 return true; 1964 } 1965 isObjectDead(): boolean { 1966 return false; 1967 } 1968 asObject(): rpc.IRemoteObject { 1969 return this; 1970 } 1971 } 1972 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 1973 let data = rpc.MessageParcel.create(); 1974 let result = data.writeRemoteObjectArray(a); 1975 let b = new Array(3); 1976 data.readRemoteObjectArray(b); 1977 ``` 1978 1979 1980### readRemoteObjectArray<sup>8+</sup> 1981 1982readRemoteObjectArray(): IRemoteObject[] 1983 1984Reads the **IRemoteObject** array from this **MessageParcel** object. 1985 1986**System capability**: SystemCapability.Communication.IPC.Core 1987 1988**Return value** 1989 | Type| Description| 1990 | -------- | -------- | 1991 | IRemoteObject[] | **IRemoteObject** object array obtained.| 1992 1993**Example** 1994 1995 ``` 1996 class MyDeathRecipient { 1997 onRemoteDied() { 1998 console.log("server died"); 1999 } 2000 } 2001 class TestRemoteObject extends rpc.RemoteObject { 2002 constructor(descriptor) { 2003 super(descriptor); 2004 this.attachLocalInterface(this, descriptor); 2005 } 2006 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 2007 return true; 2008 } 2009 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 2010 return true; 2011 } 2012 isObjectDead(): boolean { 2013 return false; 2014 } 2015 asObject(): rpc.IRemoteObject { 2016 return this; 2017 } 2018 } 2019 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2020 let data = rpc.MessageParcel.create(); 2021 let result = data.writeRemoteObjectArray(a); 2022 console.log("RpcClient: readRemoteObjectArray is " + result); 2023 let b = data.readRemoteObjectArray(); 2024 console.log("RpcClient: readRemoteObjectArray is " + b); 2025 ``` 2026 2027 2028### closeFileDescriptor<sup>8+</sup> 2029 2030static closeFileDescriptor(fd: number): void 2031 2032Closes a file descriptor. 2033 2034**System capability**: SystemCapability.Communication.IPC.Core 2035 2036**Parameters** 2037 | Name| Type| Mandatory| Description| 2038 | -------- | -------- | -------- | -------- | 2039 | fd | number | Yes| File descriptor to close.| 2040 2041**Example** 2042 2043 ``` 2044 import fileio from '@ohos.fileio'; 2045 let filePath = "path/to/file"; 2046 let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666); 2047 rpc.MessageParcel.closeFileDescriptor(fd); 2048 ``` 2049 2050 2051### dupFileDescriptor<sup>8+</sup> 2052 2053static dupFileDescriptor(fd: number) :number 2054 2055Duplicates a file descriptor. 2056 2057**System capability**: SystemCapability.Communication.IPC.Core 2058 2059**Parameters** 2060 | Name| Type| Mandatory| Description| 2061 | -------- | -------- | -------- | -------- | 2062 | fd | number | Yes| File descriptor to duplicate.| 2063 2064**Return value** 2065 | Type| Description| 2066 | -------- | -------- | 2067 | number | New file descriptor.| 2068 2069**Example** 2070 2071 ``` 2072 import fileio from '@ohos.fileio'; 2073 let filePath = "path/to/file"; 2074 let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666); 2075 let newFd = rpc.MessageParcel.dupFileDescriptor(fd); 2076 ``` 2077 2078 2079### containFileDescriptors<sup>8+</sup> 2080 2081containFileDescriptors(): boolean 2082 2083Checks whether this **MessageParcel** object contains a file descriptor. 2084 2085**System capability**: SystemCapability.Communication.IPC.Core 2086 2087**Return value** 2088 | Type| Description| 2089 | -------- | -------- | 2090 | boolean | Returns **true** if the **MessageParcel** object contains a file descriptor; returns **false** otherwise.| 2091 2092**Example** 2093 2094 ``` 2095 import fileio from '@ohos.fileio'; 2096 let parcel = new rpc.MessageParcel(); 2097 let filePath = "path/to/file"; 2098 let r1 = parcel.containFileDescriptors(); 2099 let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666); 2100 let writeResult = parcel.writeFileDescriptor(fd); 2101 console.log("RpcTest: parcel writeFd result is : " + writeResult); 2102 let containFD = parcel.containFileDescriptors(); 2103 console.log("RpcTest: parcel after write fd containFd result is : " + containFD); 2104 ``` 2105 2106 2107### writeFileDescriptor<sup>8+</sup> 2108 2109writeFileDescriptor(fd: number): boolean 2110 2111Writes a file descriptor to this **MessageParcel** object. 2112 2113**System capability**: SystemCapability.Communication.IPC.Core 2114 2115**Parameters** 2116 | Name| Type| Mandatory| Description| 2117 | -------- | -------- | -------- | -------- | 2118 | fd | number | Yes| File descriptor to write.| 2119 2120**Return value** 2121 | Type| Description| 2122 | -------- | -------- | 2123 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2124 2125**Example** 2126 2127 ``` 2128 import fileio from '@ohos.fileio'; 2129 let parcel = new rpc.MessageParcel(); 2130 let filePath = "path/to/file"; 2131 let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666); 2132 let writeResult = parcel.writeFileDescriptor(fd); 2133 console.log("RpcTest: parcel writeFd result is : " + writeResult); 2134 ``` 2135 2136 2137### readFileDescriptor<sup>8+</sup> 2138 2139readFileDescriptor(): number 2140 2141Reads the file descriptor from this **MessageParcel** object. 2142 2143**System capability**: SystemCapability.Communication.IPC.Core 2144 2145**Return value** 2146 | Type| Description| 2147 | -------- | -------- | 2148 | number | File descriptor read.| 2149 2150**Example** 2151 2152 ``` 2153 import fileio from '@ohos.fileio'; 2154 let parcel = new rpc.MessageParcel(); 2155 let filePath = "path/to/file"; 2156 let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666); 2157 let writeResult = parcel.writeFileDescriptor(fd); 2158 let readFD = parcel.readFileDescriptor(); 2159 console.log("RpcTest: parcel read fd is : " + readFD); 2160 ``` 2161 2162 2163### writeAshmem<sup>8+</sup> 2164 2165writeAshmem(ashmem: Ashmem): boolean 2166 2167Writes an anonymous shared object to this **MessageParcel** object. 2168 2169**System capability**: SystemCapability.Communication.IPC.Core 2170 2171**Parameters** 2172 | Name| Type| Mandatory| Description| 2173 | -------- | -------- | -------- | -------- | 2174 | ashmem | Ashmem | Yes| Anonymous shared object to write.| 2175 2176**Return value** 2177 | Type| Description| 2178 | -------- | -------- | 2179 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2180 2181**Example** 2182 2183 ``` 2184 let parcel = new rpc.MessageParcel(); 2185 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 2186 let isWriteSuccess = parcel.writeAshmem(ashmem); 2187 console.log("RpcTest: write ashmem to result is : " + isWriteSuccess); 2188 ``` 2189 2190 2191### readAshmem<sup>8+</sup> 2192 2193readAshmem(): Ashmem 2194 2195Reads the anonymous shared object from this **MessageParcel** object. 2196 2197**System capability**: SystemCapability.Communication.IPC.Core 2198 2199**Return value** 2200 | Type| Description| 2201 | -------- | -------- | 2202 | Ashmem | Anonymous share object obtained.| 2203 2204**Example** 2205 2206 ``` 2207 let parcel = new rpc.MessageParcel(); 2208 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 2209 let isWriteSuccess = parcel.writeAshmem(ashmem); 2210 console.log("RpcTest: write ashmem to result is : " + isWriteSuccess); 2211 let readAshmem = parcel.readAshmem(); 2212 console.log("RpcTest: read ashmem to result is : " + readAshmem); 2213 ``` 2214 2215 2216### getRawDataCapacity<sup>8+</sup> 2217 2218getRawDataCapacity(): number 2219 2220Obtains the maximum amount of raw data that can be held by this **MessageParcel** object. 2221 2222**System capability**: SystemCapability.Communication.IPC.Core 2223 2224**Return value** 2225 | Type| Description| 2226 | -------- | -------- | 2227 | number | 128 MB, which is the maximum amount of raw data that can be held by this **MessageParcel** object.| 2228 2229**Example** 2230 2231 ``` 2232 let parcel = new rpc.MessageParcel(); 2233 let result = parcel.getRawDataCapacity(); 2234 console.log("RpcTest: parcel get RawDataCapacity result is : " + result); 2235 ``` 2236 2237 2238### writeRawData<sup>8+</sup> 2239 2240writeRawData(rawData: number[], size: number): boolean 2241 2242Writes raw data to this **MessageParcel** object. 2243 2244**System capability**: SystemCapability.Communication.IPC.Core 2245 2246**Parameters** 2247 | Name| Type| Mandatory| Description| 2248 | -------- | -------- | -------- | -------- | 2249 | rawData | number[] | Yes| Raw data to write.| 2250 | size | number | Yes| Size of the raw data, in bytes.| 2251 2252**Return value** 2253 | Type| Description| 2254 | -------- | -------- | 2255 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2256 2257**Example** 2258 2259 ``` 2260 let parcel = new rpc.MessageParcel(); 2261 let arr = [1, 2, 3, 4, 5]; 2262 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 2263 console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess); 2264 ``` 2265 2266 2267### readRawData<sup>8+</sup> 2268 2269readRawData(size: number): number[] 2270 2271Reads raw data from this **MessageParcel** object. 2272 2273**System capability**: SystemCapability.Communication.IPC.Core 2274 2275**Parameters** 2276 | Name| Type| Mandatory| Description| 2277 | -------- | -------- | -------- | -------- | 2278 | size | number | Yes| Size of the raw data to read.| 2279 2280**Return value** 2281 | Type| Description| 2282 | -------- | -------- | 2283 | number[] | Raw data obtained, in bytes.| 2284 2285**Example** 2286 2287 ``` 2288 let parcel = new rpc.MessageParcel(); 2289 let arr = [1, 2, 3, 4, 5]; 2290 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 2291 console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess); 2292 let result = parcel.readRawData(5); 2293 console.log("RpcTest: parcel read raw data result is : " + result); 2294 ``` 2295 2296## Sequenceable 2297 2298Writes objects of classes to a **MessageParcel** and reads them from the **MessageParcel** during IPC. 2299 2300 2301### marshalling 2302 2303marshalling(dataOut: MessageParcel): boolean 2304 2305Marshals the sequenceable object into a **MessageParcel** object. 2306 2307**System capability**: SystemCapability.Communication.IPC.Core 2308 2309**Parameters** 2310 | Name| Type| Mandatory| Description| 2311 | -------- | -------- | -------- | -------- | 2312 | dataOut | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object to which the sequenceable object is to be marshaled.| 2313 2314**Return value** 2315 | Type| Description| 2316 | -------- | -------- | 2317 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2318 2319**Example** 2320 2321 ``` 2322 class MySequenceable { 2323 num: number; 2324 str: string; 2325 constructor(num, str) { 2326 this.num = num; 2327 this.str = str; 2328 } 2329 marshalling(messageParcel) { 2330 messageParcel.writeInt(this.num); 2331 messageParcel.writeString(this.str); 2332 return true; 2333 } 2334 unmarshalling(messageParcel) { 2335 this.num = messageParcel.readInt(); 2336 this.str = messageParcel.readString(); 2337 return true; 2338 } 2339 } 2340 let sequenceable = new MySequenceable(1, "aaa"); 2341 let data = rpc.MessageParcel.create(); 2342 let result = data.writeSequenceable(sequenceable); 2343 console.log("RpcClient: writeSequenceable is " + result); 2344 let ret = new MySequenceable(0, ""); 2345 let result2 = data.readSequenceable(ret); 2346 console.log("RpcClient: readSequenceable is " + result2); 2347 ``` 2348 2349 2350### unmarshalling 2351 2352unmarshalling(dataIn: MessageParcel) : boolean 2353 2354Unmarshals this sequenceable object from a **MessageParcel** object. 2355 2356**System capability**: SystemCapability.Communication.IPC.Core 2357 2358**Parameters** 2359 | Name| Type| Mandatory| Description| 2360 | -------- | -------- | -------- | -------- | 2361 | dataIn | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object in which the sequenceable object is to be unmarshaled.| 2362 2363**Return value** 2364 | Type| Description| 2365 | -------- | -------- | 2366 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2367 2368**Example** 2369 2370 ``` 2371 class MySequenceable { 2372 num: number; 2373 str: string; 2374 constructor(num, str) { 2375 this.num = num; 2376 this.str = str; 2377 } 2378 marshalling(messageParcel) { 2379 messageParcel.writeInt(this.num); 2380 messageParcel.writeString(this.str); 2381 return true; 2382 } 2383 unmarshalling(messageParcel) { 2384 this.num = messageParcel.readInt(); 2385 this.str = messageParcel.readString(); 2386 return true; 2387 } 2388 } 2389 let sequenceable = new MySequenceable(1, "aaa"); 2390 let data = rpc.MessageParcel.create(); 2391 let result = data.writeSequenceable(sequenceable); 2392 console.log("RpcClient: writeSequenceable is " + result); 2393 let ret = new MySequenceable(0, ""); 2394 let result2 = data.readSequenceable(ret); 2395 console.log("RpcClient: readSequenceable is " + result2); 2396 ``` 2397 2398 2399## IRemoteBroker 2400 2401Provides the holder of a remote proxy object. 2402 2403 2404### asObject 2405 2406asObject(): IRemoteObject 2407 2408Obtains a proxy or remote object. This method must be implemented by its derived classes. 2409 2410**System capability**: SystemCapability.Communication.IPC.Core 2411 2412**Return value** 2413 | Type| Description| 2414 | -------- | -------- | 2415 | [IRemoteObject](#iremoteobject) | Returns the [RemoteObject](#ashmem8) if it is the caller; returns the [IRemoteObject](#iremoteobject), the holder of this **RemoteProxy** object, if the caller is a [RemoteProxy](#remoteproxy) object.| 2416 2417**Example** 2418 2419 ``` 2420 class TestAbility extends rpc.RemoteObject { 2421 asObject() { 2422 return this; 2423 } 2424 } 2425 ``` 2426 2427**Example** 2428 2429 ``` 2430 class TestProxy { 2431 remote: rpc.RemoteObject; 2432 constructor(remote) { 2433 this.remote = remote; 2434 } 2435 asObject() { 2436 return this.remote; 2437 } 2438 } 2439 ``` 2440 2441 2442## DeathRecipient 2443 2444Subscribes 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. 2445 2446 2447### onRemoteDied 2448 2449onRemoteDied(): void 2450 2451Called to perform subsequent operations when a death notification of the remote object is received. 2452 2453**System capability**: SystemCapability.Communication.IPC.Core 2454 2455**Example** 2456 2457 ``` 2458 class MyDeathRecipient { 2459 onRemoteDied() { 2460 console.log("server died"); 2461 } 2462 } 2463 ``` 2464 2465 2466## SendRequestResult<sup>8+</sup> 2467 2468Defines the response to the request. 2469 2470**System capability**: SystemCapability.Communication.IPC.Core 2471 2472 | Parameter| Value| Description| 2473| -------- | -------- | -------- | 2474| errCode | number | Error Code| 2475| code | number | Message code.| 2476| data | MessageParcel | **MessageParcel** object sent to the remote process.| 2477| reply | MessageParcel | **MessageParcel** object returned by the remote process.| 2478 2479 2480## IRemoteObject 2481 2482Provides methods to query of obtain interface descriptors, add or delete death notifications, dump object status to specific files, and send messages. 2483 2484 2485### queryLocalInterface 2486 2487queryLocalInterface(descriptor: string): IRemoteBroker 2488 2489Obtains the interface. 2490 2491**System capability**: SystemCapability.Communication.IPC.Core 2492 2493**Parameters** 2494 | Name| Type| Mandatory| Description| 2495 | -------- | -------- | -------- | -------- | 2496 | descriptor | string | Yes| Interface descriptor.| 2497 2498**Return value** 2499 | Type| Description| 2500 | -------- | -------- | 2501 | IRemoteBroker | **IRemoteBroker** object bound to the specified interface descriptor.| 2502 2503 2504### sendRequest<sup>(deprecated)</sup> 2505 2506> **NOTE**<br/> 2507> This API is deprecated since API Version 8. You are advised to use [sendRequest<sup>8+</sup>](#sendrequest8). 2508 2509sendRequest(code : number, data : MessageParcel, reply : MessageParcel, options : MessageOption): boolean 2510 2511Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If **options** is the asynchronous mode, a promise will be fulfilled immediately and the reply message does not contain any content. If **options** is the synchronous mode, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 2512 2513**System capability**: SystemCapability.Communication.IPC.Core 2514 2515**Parameters** 2516 | Name| Type| Mandatory| Description| 2517 | -------- | -------- | -------- | -------- | 2518 | 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.| 2519 | data | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object holding the data to send.| 2520 | reply | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object that receives the response.| 2521 | options | [MessageOption](#messageoption) | Yes| Request sending mode, which can be synchronous (default) or asynchronous.| 2522 2523**Return value** 2524 | Type| Description| 2525 | -------- | -------- | 2526 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2527 2528 2529### sendRequest<sup>8+</sup> 2530 2531sendRequest(code : number, data : MessageParcel, reply : MessageParcel, options : MessageOption): Promise<SendRequestResult> 2532 2533Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If **options** is the asynchronous mode, a promise will be fulfilled immediately and the reply message does not contain any content. If **options** is the synchronous mode, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 2534 2535**System capability**: SystemCapability.Communication.IPC.Core 2536 2537**Parameters** 2538 | Name| Type| Mandatory| Description| 2539 | -------- | -------- | -------- | -------- | 2540 | 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.| 2541 | data | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object holding the data to send.| 2542 | reply | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object that receives the response.| 2543 | options | [MessageOption](#messageoption) | Yes| Request sending mode, which can be synchronous (default) or asynchronous.| 2544 2545**Return value** 2546 | Type| Description| 2547 | -------- | -------- | 2548 | Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 2549 2550### sendRequest<sup>8+</sup> 2551 2552sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 2553 2554Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If **options** is the asynchronous mode, a callback will be invoked immediately and the reply message does not contain any content. If **options** is the synchronous mode, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information. 2555 2556**System capability**: SystemCapability.Communication.IPC.Core 2557 2558**Parameters** 2559 | Name| Type| Mandatory| Description| 2560 | -------- | -------- | -------- | -------- | 2561 | 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.| 2562 | data | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object holding the data to send.| 2563 | reply | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object that receives the response.| 2564 | options | [MessageOption](#messageoption) | Yes| Request sending mode, which can be synchronous (default) or asynchronous.| 2565 | callback | AsyncCallback<SendRequestResult> | Yes| Callback for receiving the sending result.| 2566 2567 2568### addDeathrecipient 2569 2570addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 2571 2572Adds 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. 2573 2574**System capability**: SystemCapability.Communication.IPC.Core 2575 2576**Parameters** 2577 | Name| Type| Mandatory| Description| 2578 | -------- | -------- | -------- | -------- | 2579 | recipient | [DeathRecipient](#deathrecipient) | Yes| Callback to add.| 2580 | flags | number | Yes| Flag of the death notification.| 2581 2582**Return value** 2583 | Type| Description| 2584 | -------- | -------- | 2585 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 2586 2587 2588### removeDeathRecipient 2589 2590removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 2591 2592Removes the callback used to receive death notifications of the remote object. 2593 2594**System capability**: SystemCapability.Communication.IPC.Core 2595 2596**Parameters** 2597 | Name| Type| Mandatory| Description| 2598 | -------- | -------- | -------- | -------- | 2599 | recipient | [DeathRecipient](#deathrecipient) | Yes| Callback to remove.| 2600 | flags | number | Yes| Flag of the death notification.| 2601 2602**Return value** 2603 | Type| Description| 2604 | -------- | -------- | 2605 | boolean | Returns **true** if the callback is removed successfully; returns **false** otherwise.| 2606 2607 2608### getInterfaceDescriptor 2609 2610getInterfaceDescriptor(): string 2611 2612Obtains the interface descriptor of this object. The interface descriptor is a string. 2613 2614**System capability**: SystemCapability.Communication.IPC.Core 2615 2616**Return value** 2617 | Type| Description| 2618 | -------- | -------- | 2619 | string | Interface descriptor obtained.| 2620 2621 2622### isObjectDead 2623 2624isObjectDead(): boolean 2625 2626Checks whether this object is dead. 2627 2628**System capability**: SystemCapability.Communication.IPC.Core 2629 2630**Return value** 2631 | Type| Description| 2632 | -------- | -------- | 2633 | boolean | Returns **true** if the object is dead; returns **false** otherwise.| 2634 2635 2636## RemoteProxy 2637 2638Provides methods to implement **IRemoteObject**. 2639 2640**System capability**: SystemCapability.Communication.IPC.Core 2641 2642| Parameter | Value | Description | 2643| --------------------- | ----------------------- | --------------------------------- | 2644| PING_TRANSACTION | 1599098439 (0x5f504e47) | Internal instruction code used to test whether the IPC service is normal.| 2645| DUMP_TRANSACTION | 1598311760 (0x5f444d50) | Internal instruction code used to obtain the internal status of the binder.| 2646| INTERFACE_TRANSACTION | 1598968902 (0x5f4e5446) | Internal instruction code used to obtain the remote interface descriptor. | 2647| MIN_TRANSACTION_ID | 1 (0x00000001) | Minimum valid instruction code. | 2648| MAX_TRANSACTION_ID | 16777215 (0x00FFFFFF) | Maximum valid instruction code. | 2649 2650 2651 2652 2653### sendRequest<sup>(deprecated)</sup> 2654 2655> **NOTE**<br/> 2656> This API is deprecated since API Version 8. You are advised to use [sendRequest<sup>8+</sup>](#sendrequest8-2). 2657 2658sendRequest(code : number, data : MessageParcel, reply : MessageParcel, options : MessageOption): boolean 2659 2660Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If **options** is the asynchronous mode, a promise will be fulfilled immediately and the reply message does not contain any content. If **options** is the synchronous mode, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 2661 2662**System capability**: SystemCapability.Communication.IPC.Core 2663 2664**Parameters** 2665 | Name| Type| Mandatory| Description| 2666 | -------- | -------- | -------- | -------- | 2667 | 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.| 2668 | data | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object holding the data to send.| 2669 | reply | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object that receives the response.| 2670 | options | [MessageOption](#messageoption) | Yes| Request sending mode, which can be synchronous (default) or asynchronous.| 2671 2672**Return value** 2673 | Type| Description| 2674 | -------- | -------- | 2675 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2676 2677 2678**Example** 2679 2680 ``` 2681 import FA from "@ohos.ability.featureAbility"; 2682 let proxy; 2683 let connect = { 2684 onConnect: function(elementName, remoteProxy) { 2685 console.log("RpcClient: js onConnect called."); 2686 proxy = remoteProxy; 2687 }, 2688 onDisconnect: function(elementName) { 2689 console.log("RpcClient: onDisconnect"); 2690 }, 2691 onFailed: function() { 2692 console.log("RpcClient: onFailed"); 2693 } 2694 }; 2695 let want = { 2696 "bundleName": "com.ohos.server", 2697 "abilityName": "com.ohos.server.MainAbility", 2698 }; 2699 FA.connectAbility(want, connect); 2700 let option = new rpc.MessageOption(); 2701 let data = rpc.MessageParcel.create(); 2702 let reply = rpc.MessageParcel.create(); 2703 data.writeInt(1); 2704 data.writeString("hello"); 2705 let ret: boolean = proxy.sendRequest(1, data, reply, option); 2706 if (ret) { 2707 console.log("sendRequest got result"); 2708 let msg = reply.readString(); 2709 console.log("RPCTest: reply msg: " + msg); 2710 } else { 2711 console.log("RPCTest: sendRequest failed"); 2712 } 2713 console.log("RPCTest: sendRequest ends, reclaim parcel"); 2714 data.reclaim(); 2715 reply.reclaim(); 2716 ``` 2717 2718### sendRequest<sup>8+</sup> 2719 2720sendRequest(code : number, data : MessageParcel, reply : MessageParcel, options : MessageOption): Promise<SendRequestResult> 2721 2722Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If **options** is the asynchronous mode, a promise will be fulfilled immediately and the reply message does not contain any content. If **options** is the synchronous mode, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 2723 2724**System capability**: SystemCapability.Communication.IPC.Core 2725 2726**Parameters** 2727 | Name| Type| Mandatory| Description| 2728 | -------- | -------- | -------- | -------- | 2729 | 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.| 2730 | data | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object holding the data to send.| 2731 | reply | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object that receives the response.| 2732 | options | [MessageOption](#messageoption) | Yes| Request sending mode, which can be synchronous (default) or asynchronous.| 2733 2734**Return value** 2735 | Type| Description| 2736 | -------- | -------- | 2737 | Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 2738 2739**Example** 2740 2741 ``` 2742 import FA from "@ohos.ability.featureAbility"; 2743 let proxy; 2744 let connect = { 2745 onConnect: function(elementName, remoteProxy) { 2746 console.log("RpcClient: js onConnect called."); 2747 proxy = remoteProxy; 2748 }, 2749 onDisconnect: function(elementName) { 2750 console.log("RpcClient: onDisconnect"); 2751 }, 2752 onFailed: function() { 2753 console.log("RpcClient: onFailed"); 2754 } 2755 }; 2756 let want = { 2757 "bundleName": "com.ohos.server", 2758 "abilityName": "com.ohos.server.MainAbility", 2759 }; 2760 FA.connectAbility(want, connect); 2761 let option = new rpc.MessageOption(); 2762 let data = rpc.MessageParcel.create(); 2763 let reply = rpc.MessageParcel.create(); 2764 data.writeInt(1); 2765 data.writeString("hello"); 2766 proxy.sendRequest(1, data, reply, option) 2767 .then(function(result) { 2768 if (result.errCode === 0) { 2769 console.log("sendRequest got result"); 2770 result.reply.readException(); 2771 let msg = result.reply.readString(); 2772 console.log("RPCTest: reply msg: " + msg); 2773 } else { 2774 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 2775 } 2776 }).catch(function(e) { 2777 console.log("RPCTest: sendRequest got exception: " + e.message); 2778 }).finally (() => { 2779 console.log("RPCTest: sendRequest ends, reclaim parcel"); 2780 data.reclaim(); 2781 reply.reclaim(); 2782 }); 2783 ``` 2784 2785 2786### sendRequest<sup>8+</sup> 2787 2788sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 2789 2790Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If **options** is the asynchronous mode, a callback will be invoked immediately and the reply message does not contain any content. If **options** is the synchronous mode, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information. 2791 2792**System capability**: SystemCapability.Communication.IPC.Core 2793 2794**Parameters** 2795 | Name| Type| Mandatory| Description| 2796 | -------- | -------- | -------- | -------- | 2797 | 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.| 2798 | data | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object holding the data to send.| 2799 | reply | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object that receives the response.| 2800 | options | [MessageOption](#messageoption) | Yes| Request sending mode, which can be synchronous (default) or asynchronous.| 2801 | callback | AsyncCallback<SendRequestResult> | Yes| Callback for receiving the sending result.| 2802 2803**Example** 2804 2805 ``` 2806 import FA from "@ohos.ability.featureAbility"; 2807 let proxy; 2808 let connect = { 2809 onConnect: function(elementName, remoteProxy) { 2810 console.log("RpcClient: js onConnect called."); 2811 proxy = remoteProxy; 2812 }, 2813 onDisconnect: function(elementName) { 2814 console.log("RpcClient: onDisconnect"); 2815 }, 2816 onFailed: function() { 2817 console.log("RpcClient: onFailed"); 2818 } 2819 }; 2820 let want = { 2821 "bundleName": "com.ohos.server", 2822 "abilityName": "com.ohos.server.MainAbility", 2823 }; 2824 function sendRequestCallback(result) { 2825 if (result.errCode === 0) { 2826 console.log("sendRequest got result"); 2827 result.reply.readException(); 2828 let msg = result.reply.readString(); 2829 console.log("RPCTest: reply msg: " + msg); 2830 } else { 2831 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 2832 } 2833 console.log("RPCTest: sendRequest ends, reclaim parcel"); 2834 result.data.reclaim(); 2835 result.reply.reclaim(); 2836 } 2837 FA.connectAbility(want, connect); 2838 let option = new rpc.MessageOption(); 2839 let data = rpc.MessageParcel.create(); 2840 let reply = rpc.MessageParcel.create(); 2841 data.writeInt(1); 2842 data.writeString("hello"); 2843 proxy.sendRequest(1, data, reply, option, sendRequestCallback); 2844 ``` 2845 2846 2847### queryLocalInterface 2848 2849queryLocalInterface(interface: string): IRemoteBroker 2850 2851Obtains the **LocalInterface** object of an interface descriptor. 2852 2853**System capability**: SystemCapability.Communication.IPC.Core 2854 2855**Parameters** 2856 | Name| Type| Mandatory| Description| 2857 | -------- | -------- | -------- | -------- | 2858 | interface | string | Yes| Interface descriptor.| 2859 2860**Return value** 2861 | Type| Description| 2862 | -------- | -------- | 2863 | IRemoteBroker | Returns **Null** by default, which indicates a proxy interface.| 2864 2865**Example** 2866 2867 ``` 2868 import FA from "@ohos.ability.featureAbility"; 2869 let proxy; 2870 let connect = { 2871 onConnect: function(elementName, remoteProxy) { 2872 console.log("RpcClient: js onConnect called."); 2873 proxy = remoteProxy; 2874 }, 2875 onDisconnect: function (elementName) { 2876 console.log("RpcClient: onDisconnect"); 2877 }, 2878 onFailed: function() { 2879 console.log("RpcClient: onFailed"); 2880 } 2881 }; 2882 let want = { 2883 "bundleName":"com.ohos.server", 2884 "abilityName":"com.ohos.server.MainAbility", 2885 }; 2886 FA.connectAbility(want, connect); 2887 let broker = proxy.queryLocalInterface("testObject"); 2888 console.log("RpcClient: queryLocalInterface is " + broker); 2889 ``` 2890 2891 2892### addDeathRecippient 2893 2894addDeathRecipient(recipient : DeathRecipient, flags : number): boolean 2895 2896Adds a callback for receiving the death notifications of the remote object, including the death notifications of the remote proxy. 2897 2898**System capability**: SystemCapability.Communication.IPC.Core 2899 2900**Parameters** 2901 | Name| Type| Mandatory| Description| 2902 | -------- | -------- | -------- | -------- | 2903 | recipient | [DeathRecipient](#deathrecipient) | Yes| Callback to add.| 2904 | flags | number | Yes| Flag of the death notification. This parameter is reserved. It is set to **0**.| 2905 2906**Return value** 2907 | Type| Description| 2908 | -------- | -------- | 2909 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 2910 2911**Example** 2912 2913 ``` 2914 import FA from "@ohos.ability.featureAbility"; 2915 let proxy; 2916 let connect = { 2917 onConnect: function(elementName, remoteProxy) { 2918 console.log("RpcClient: js onConnect called."); 2919 proxy = remoteProxy; 2920 }, 2921 onDisconnect: function(elementName) { 2922 console.log("RpcClient: onDisconnect"); 2923 }, 2924 onFailed: function() { 2925 console.log("RpcClient: onFailed"); 2926 } 2927 }; 2928 let want = { 2929 "bundleName": "com.ohos.server", 2930 "abilityName": "com.ohos.server.MainAbility", 2931 }; 2932 FA.connectAbility(want, connect); 2933 class MyDeathRecipient { 2934 onRemoteDied() { 2935 console.log("server died"); 2936 } 2937 } 2938 let deathRecipient = new MyDeathRecipient(); 2939 proxy.addDeathRecippient(deathRecipient, 0); 2940 ``` 2941 2942 2943### removeDeathRecipient 2944 2945removeDeathRecipient(recipient : DeathRecipient, flags : number): boolean 2946 2947Removes the callback used to receive death notifications of the remote object. 2948 2949**System capability**: SystemCapability.Communication.IPC.Core 2950 2951**Parameters** 2952 | Name| Type| Mandatory| Description| 2953 | -------- | -------- | -------- | -------- | 2954 | recipient | [DeathRecipient](#deathrecipient) | Yes| Callback to remove.| 2955 | flags | number | Yes| Flag of the death notification. This parameter is reserved. It is set to **0**.| 2956 2957**Return value** 2958 | Type| Description| 2959 | -------- | -------- | 2960 | boolean | Returns **true** if the callback is removed successfully; returns **false** otherwise.| 2961 2962**Example** 2963 2964 ``` 2965 import FA from "@ohos.ability.featureAbility"; 2966 let proxy; 2967 let connect = { 2968 onConnect: function(elementName, remoteProxy) { 2969 console.log("RpcClient: js onConnect called."); 2970 proxy = remoteProxy; 2971 }, 2972 onDisconnect: function(elementName) { 2973 console.log("RpcClient: onDisconnect"); 2974 }, 2975 onFailed: function() { 2976 console.log("RpcClient: onFailed"); 2977 } 2978 }; 2979 let want = { 2980 "bundleName": "com.ohos.server", 2981 "abilityName": "com.ohos.server.MainAbility", 2982 }; 2983 FA.connectAbility(want, connect); 2984 class MyDeathRecipient { 2985 onRemoteDied() { 2986 console.log("server died"); 2987 } 2988 } 2989 let deathRecipient = new MyDeathRecipient(); 2990 proxy.addDeathRecippient(deathRecipient, 0); 2991 proxy.removeDeathRecipient(deathRecipient, 0); 2992 ``` 2993 2994 2995### getInterfaceDescriptor 2996 2997getInterfaceDescriptor(): string 2998 2999Obtains the interface descriptor of this proxy object. 3000 3001**System capability**: SystemCapability.Communication.IPC.Core 3002 3003**Return value** 3004 | Type| Description| 3005 | -------- | -------- | 3006 | string | Interface descriptor obtained.| 3007 3008**Example** 3009 3010 ``` 3011 import FA from "@ohos.ability.featureAbility"; 3012 let proxy; 3013 let connect = { 3014 onConnect: function(elementName, remoteProxy) { 3015 console.log("RpcClient: js onConnect called."); 3016 proxy = remoteProxy; 3017 }, 3018 onDisconnect: function(elementName) { 3019 console.log("RpcClient: onDisconnect"); 3020 }, 3021 onFailed: function() { 3022 console.log("RpcClient: onFailed"); 3023 } 3024 }; 3025 let want = { 3026 "bundleName": "com.ohos.server", 3027 "abilityName": "com.ohos.server.MainAbility", 3028 }; 3029 FA.connectAbility(want, connect); 3030 let descriptor = proxy.getInterfaceDescriptor(); 3031 console.log("RpcClient: descriptor is " + descriptor); 3032 ``` 3033 3034 3035### isObjectDead 3036 3037isObjectDead(): boolean 3038 3039Checks whether the **RemoteObject** is dead. 3040 3041**System capability**: SystemCapability.Communication.IPC.Core 3042 3043**Return value** 3044 | Type| Description| 3045 | -------- | -------- | 3046 | boolean | Returns **true** if the **RemoteObject** is dead; returns **false** otherwise.| 3047 3048**Example** 3049 3050 ``` 3051 import FA from "@ohos.ability.featureAbility"; 3052 let proxy; 3053 let connect = { 3054 onConnect: function(elementName, remoteProxy) { 3055 console.log("RpcClient: js onConnect called."); 3056 proxy = remoteProxy; 3057 }, 3058 onDisconnect: function(elementName) { 3059 console.log("RpcClient: onDisconnect"); 3060 }, 3061 onFailed: function() { 3062 console.log("RpcClient: onFailed"); 3063 } 3064 }; 3065 let want = { 3066 "bundleName": "com.ohos.server", 3067 "abilityName": "com.ohos.server.MainAbility", 3068 }; 3069 FA.connectAbility(want, connect); 3070 let isDead = proxy.isObjectDead(); 3071 console.log("RpcClient: isObjectDead is " + isDead); 3072 ``` 3073 3074 3075## MessageOption 3076 3077Provides common message options (flag and wait time). The flag is used to construct the specified **MessageOption** object. 3078 3079**System capability**: SystemCapability.Communication.IPC.Core 3080 3081 | Parameter| Value| Description| 3082| -------- | -------- | -------- | 3083| TF_SYNC | 0 | Synchronous call.| 3084| TF_ASYNC | 1 | Asynchronous call.| 3085| TF_ACCEPT_FDS | 0x10 | Indication to the [sendRequest](#sendrequest8) API for returning the file descriptor.| 3086| TF_WAIT_TIME | 8 | Time to wait, in seconds.| 3087 3088 3089### constructor 3090 3091constructor(syncFlags?: number, waitTime = TF_WAIT_TIME) 3092 3093A constructor used to create a **MessageOption** object. 3094 3095**System capability**: SystemCapability.Communication.IPC.Core 3096 3097**Parameters** 3098 | Name| Type| Mandatory| Description| 3099 | -------- | -------- | -------- | -------- | 3100 | syncFlags | number | No| Call flag, which can be synchronous or asynchronous. The default value is **synchronous**.| 3101 | waitTime | number | No| Maximum wait time for an RPC call. The default value is **TF_WAIT_TIME**.| 3102 3103 3104### getFlags 3105 3106getFlags(): number 3107 3108Obtains the call flag, which can be synchronous or asynchronous. 3109 3110**System capability**: SystemCapability.Communication.IPC.Core 3111 3112**Return value** 3113 | Type| Description| 3114 | -------- | -------- | 3115 | number | Call mode obtained.| 3116 3117 3118### setFlags 3119 3120setFlags(flags: number): void 3121 3122Sets the call flag, which can be synchronous or asynchronous. 3123 3124**System capability**: SystemCapability.Communication.IPC.Core 3125 3126**Parameters** 3127 | Name| Type| Mandatory| Description| 3128 | -------- | -------- | -------- | -------- | 3129 | flags | number | Yes| Call flag to set.| 3130 3131 3132### getWaitTime 3133 3134getWaitTime(): number 3135 3136Obtains the maximum wait time for this RPC call. 3137 3138**System capability**: SystemCapability.Communication.IPC.Core 3139 3140**Return value** 3141 | Type| Description| 3142 | -------- | -------- | 3143 | number | Maximum wait time obtained.| 3144 3145 3146### setWaitTime 3147 3148setWaitTime(waitTime: number): void 3149 3150Sets the maximum wait time for this RPC call. 3151 3152**System capability**: SystemCapability.Communication.IPC.Core 3153 3154**Parameters** 3155 | Name| Type| Mandatory| Description| 3156 | -------- | -------- | -------- | -------- | 3157 | waitTime | number | Yes| Maximum wait time to set.| 3158 3159 3160## IPCSkeleton 3161 3162Obtains IPC context information, including the UID and PID, local and remote device IDs, and whether the method is invoked on the same device. 3163 3164 3165### getContextObject 3166 3167static getContextObject(): IRemoteObject 3168 3169Obtains the system capability manager. 3170 3171**System capability**: SystemCapability.Communication.IPC.Core 3172 3173**Return value** 3174 | Type| Description| 3175 | -------- | -------- | 3176 | [IRemoteObject](#iremoteobject) | System capability manager obtained.| 3177 3178**Example** 3179 3180 ``` 3181 let samgr = rpc.IPCSkeleton.getContextObject(); 3182 console.log("RpcServer: getContextObject result: " + samgr); 3183 ``` 3184 3185 3186### getCallingPid 3187 3188static getCallingPid(): number 3189 3190Obtains the PID of the caller. This method 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. 3191 3192**System capability**: SystemCapability.Communication.IPC.Core 3193 3194**Return value** 3195 | Type| Description| 3196 | -------- | -------- | 3197 | number | PID of the caller.| 3198 3199**Example** 3200 3201 ``` 3202 class Stub extends rpc.RemoteObject { 3203 onRemoteRequest(code, data, reply, option) { 3204 let callerPid = rpc.IPCSkeleton.getCallingPid(); 3205 console.log("RpcServer: getCallingPid result: " + callerPid); 3206 return true; 3207 } 3208 } 3209 ``` 3210 3211 3212### getCallingUid 3213 3214static getCallingUid(): number 3215 3216Obtains the UID of the caller. This method 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. 3217 3218**System capability**: SystemCapability.Communication.IPC.Core 3219 3220**Return value** 3221 | Type| Description| 3222 | -------- | -------- | 3223 | number | UID of the caller.| 3224 3225**Example** 3226 3227 ``` 3228 class Stub extends rpc.RemoteObject { 3229 onRemoteRequest(code, data, reply, option) { 3230 let callerUid = rpc.IPCSkeleton.getCallingUid(); 3231 console.log("RpcServer: getCallingUid result: " + callerUid); 3232 return true; 3233 } 3234 } 3235 ``` 3236 3237### getCallingTokenId<sup>8+</sup> 3238 3239static getCallingTokenId(): number; 3240 3241Obtains the caller's token ID, which is used to verify the caller identity. 3242 3243**System capability**: SystemCapability.Communication.IPC.Core 3244 3245* Return value 3246 3247 | Type | Description | 3248 | ------ | --------------------- | 3249 | number | Token ID of the caller obtained.| 3250 3251* Example 3252 3253 ``` 3254 class Stub extends rpc.RemoteObject { 3255 onRemoteRequest(code, data, reply, option) { 3256 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 3257 console.log("RpcServer: getCallingTokenId result: " + callerTokenId); 3258 return true; 3259 } 3260 } 3261 ``` 3262 3263 3264### getCallingDeviceID 3265 3266static getCallingDeviceID(): string 3267 3268Obtains the ID of the device hosting the caller's process. 3269 3270**System capability**: SystemCapability.Communication.IPC.Core 3271 3272**Return value** 3273 | Type| Description| 3274 | -------- | -------- | 3275 | string | Device ID obtained.| 3276 3277**Example** 3278 3279 ``` 3280 class Stub extends rpc.RemoteObject { 3281 onRemoteRequest(code, data, reply, option) { 3282 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 3283 console.log("RpcServer: callerDeviceID is: " + callerDeviceID); 3284 return true; 3285 } 3286 } 3287 ``` 3288 3289 3290### getLocalDeviceID 3291 3292static getLocalDeviceID(): string 3293 3294Obtains the local device ID. 3295 3296**System capability**: SystemCapability.Communication.IPC.Core 3297 3298**Return value** 3299 | Type| Description| 3300 | -------- | -------- | 3301 | string | Local device ID obtained.| 3302 3303**Example** 3304 3305 ``` 3306 class Stub extends rpc.RemoteObject { 3307 onRemoteRequest(code, data, reply, option) { 3308 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 3309 console.log("RpcServer: localDeviceID is: " + localDeviceID); 3310 return true; 3311 } 3312 } 3313 ``` 3314 3315 3316### isLocalCalling 3317 3318static isLocalCalling(): boolean 3319 3320Checks whether the remote process is a process of the local device. 3321 3322**System capability**: SystemCapability.Communication.IPC.Core 3323 3324**Return value** 3325 | Type| Description| 3326 | -------- | -------- | 3327 | boolean | Returns **true** if the local and remote processes are on the same device; returns **false** otherwise.| 3328 3329**Example** 3330 3331 ``` 3332 class Stub extends rpc.RemoteObject { 3333 onRemoteRequest(code, data, reply, option) { 3334 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 3335 console.log("RpcServer: isLocalCalling is: " + isLocalCalling); 3336 return true; 3337 } 3338 } 3339 ``` 3340 3341 3342### flushCommands 3343 3344static flushCommands(object : IRemoteObject): number 3345 3346Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. It is recommended that this method be called before any time-sensitive operation is performed. 3347 3348**System capability**: SystemCapability.Communication.IPC.Core 3349 3350**Parameters** 3351 | Name| Type| Mandatory| Description| 3352 | -------- | -------- | -------- | -------- | 3353 | object | [IRemoteObject](#iremoteobject) | Yes| **RemoteProxy** specified. | 3354 3355 3356**Return value** 3357 | Type| Description| 3358 | -------- | -------- | 3359 | 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.| 3360 3361**Example** 3362 3363 ``` 3364 class MyDeathRecipient { 3365 onRemoteDied() { 3366 console.log("server died"); 3367 } 3368 } 3369 class TestRemoteObject extends rpc.RemoteObject { 3370 constructor(descriptor) { 3371 super(descriptor); 3372 } 3373 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3374 return true; 3375 } 3376 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3377 return true; 3378 } 3379 isObjectDead(): boolean { 3380 return false; 3381 } 3382 } 3383 let remoteObject = new TestRemoteObject("aaa"); 3384 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 3385 console.log("RpcServer: flushCommands result: " + ret); 3386 ``` 3387 3388 3389### resetCallingIdentity 3390 3391static resetCallingIdentity(): string 3392 3393Changes the UID and PID of the remote user to the UID and PID of the local user. This method is used in scenarios such as identity authentication. 3394 3395**System capability**: SystemCapability.Communication.IPC.Core 3396 3397**Return value** 3398 | Type| Description| 3399 | -------- | -------- | 3400 | string | String containing the UID and PID of the remote user.| 3401 3402**Example** 3403 3404 ``` 3405 class Stub extends rpc.RemoteObject { 3406 onRemoteRequest(code, data, reply, option) { 3407 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 3408 console.log("RpcServer: callingIdentity is: " + callingIdentity); 3409 return true; 3410 } 3411 } 3412 ``` 3413 3414 3415### setCallingIdentity 3416 3417static setCallingIdentity(identity : string): boolean 3418 3419Restores the UID and PID of the remote user. 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**. 3420 3421**System capability**: SystemCapability.Communication.IPC.Core 3422 3423**Parameters** 3424 | Name| Type| Mandatory| Description| 3425 | -------- | -------- | -------- | -------- | 3426 | identity | string | Yes| String containing the remote user UID and PID, which are returned by **resetCallingIdentity**.| 3427 3428**Return value** 3429 | Type| Description| 3430 | -------- | -------- | 3431 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3432 3433**Example** 3434 3435 ``` 3436 class Stub extends rpc.RemoteObject { 3437 onRemoteRequest(code, data, reply, option) { 3438 let callingIdentity = null; 3439 try { 3440 callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 3441 console.log("RpcServer: callingIdentity is: " + callingIdentity); 3442 } finally { 3443 let ret = rpc.IPCSkeleton.setCallingIdentity("callingIdentity "); 3444 console.log("RpcServer: setCallingIdentity is: " + ret); 3445 } 3446 return true; 3447 } 3448 } 3449 ``` 3450 3451 3452## RemoteObject 3453 3454Provides methods to implement **RemoteObject**. The service provider must inherit from this class. 3455 3456 3457### constructor 3458 3459constructor(descriptor: string) 3460 3461A constructor used to create a **RemoteObject** object. 3462 3463**System capability**: SystemCapability.Communication.IPC.Core 3464 3465**Parameters** 3466 | Name| Type| Mandatory| Description| 3467 | -------- | -------- | -------- | -------- | 3468 | descriptor | string | Yes| Interface descriptor.| 3469 3470 3471### sendRequest<sup>(deprecated)</sup> 3472 3473> **NOTE**<br/> 3474> This API is deprecated since API Version 8. You are advised to use [sendRequest<sup>8+</sup>](#sendrequest8-4). 3475 3476sendRequest(code : number, data : MessageParcel, reply : MessageParcel, options : MessageOption): boolean 3477 3478Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If **options** is the asynchronous mode, a promise will be fulfilled immediately and the reply message does not contain any content. If **options** is the synchronous mode, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 3479 3480**System capability**: SystemCapability.Communication.IPC.Core 3481 3482**Parameters** 3483 | Name| Type| Mandatory| Description| 3484 | -------- | -------- | -------- | -------- | 3485 | 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.| 3486 | data | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object holding the data to send.| 3487 | reply | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object that receives the response.| 3488 | options | [MessageOption](#messageoption) | Yes| Request sending mode, which can be synchronous (default) or asynchronous.| 3489 3490**Return value** 3491 | Type| Description| 3492 | -------- | -------- | 3493 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3494 3495 3496**Example** 3497 3498 ``` 3499 class MyDeathRecipient { 3500 onRemoteDied() { 3501 console.log("server died"); 3502 } 3503 } 3504 class TestRemoteObject extends rpc.RemoteObject { 3505 constructor(descriptor) { 3506 super(descriptor); 3507 } 3508 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3509 return true; 3510 } 3511 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3512 return true; 3513 } 3514 isObjectDead(): boolean { 3515 return false; 3516 } 3517 } 3518 let testRemoteObject = new TestRemoteObject("testObject"); 3519 let option = new rpc.MessageOption(); 3520 let data = rpc.MessageParcel.create(); 3521 let reply = rpc.MessageParcel.create(); 3522 data.writeInt(1); 3523 data.writeString("hello"); 3524 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 3525 if (ret) { 3526 console.log("sendRequest got result"); 3527 let msg = reply.readString(); 3528 console.log("RPCTest: reply msg: " + msg); 3529 } else { 3530 console.log("RPCTest: sendRequest failed"); 3531 } 3532 console.log("RPCTest: sendRequest ends, reclaim parcel"); 3533 data.reclaim(); 3534 reply.reclaim(); 3535 ``` 3536 3537 3538### sendRequest<sup>8+</sup> 3539 3540sendRequest(code : number, data : MessageParcel, reply : MessageParcel, options : MessageOption): Promise<SendRequestResult> 3541 3542Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If **options** is the asynchronous mode, a promise will be fulfilled immediately and the reply message does not contain any content. If **options** is the synchronous mode, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 3543 3544**System capability**: SystemCapability.Communication.IPC.Core 3545 3546**Parameters** 3547 | Name| Type| Mandatory| Description| 3548 | -------- | -------- | -------- | -------- | 3549 | 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.| 3550 | data | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object holding the data to send.| 3551 | reply | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object that receives the response.| 3552 | options | [MessageOption](#messageoption) | Yes| Request sending mode, which can be synchronous (default) or asynchronous.| 3553 3554**Return value** 3555 | Type| Description| 3556 | -------- | -------- | 3557 | Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 3558 3559 3560**Example** 3561 3562 ``` 3563 class MyDeathRecipient { 3564 onRemoteDied() { 3565 console.log("server died"); 3566 } 3567 } 3568 class TestRemoteObject extends rpc.RemoteObject { 3569 constructor(descriptor) { 3570 super(descriptor); 3571 } 3572 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3573 return true; 3574 } 3575 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3576 return true; 3577 } 3578 isObjectDead(): boolean { 3579 return false; 3580 } 3581 } 3582 let testRemoteObject = new TestRemoteObject("testObject"); 3583 let option = new rpc.MessageOption(); 3584 let data = rpc.MessageParcel.create(); 3585 let reply = rpc.MessageParcel.create(); 3586 data.writeInt(1); 3587 data.writeString("hello"); 3588 testRemoteObject.sendRequest(1, data, reply, option) 3589 .then(function(result) { 3590 if (result.errCode === 0) { 3591 console.log("sendRequest got result"); 3592 result.reply.readException(); 3593 let msg = result.reply.readString(); 3594 console.log("RPCTest: reply msg: " + msg); 3595 } else { 3596 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 3597 } 3598 }).catch(function(e) { 3599 console.log("RPCTest: sendRequest got exception: " + e.message); 3600 }).finally (() => { 3601 console.log("RPCTest: sendRequest ends, reclaim parcel"); 3602 data.reclaim(); 3603 reply.reclaim(); 3604 }); 3605 ``` 3606 3607 3608### sendRequest<sup>8+</sup> 3609 3610sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 3611 3612Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If **options** is the asynchronous mode, a callback will be invoked immediately and the reply message does not contain any content. If **options** is the synchronous mode, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information. 3613 3614**System capability**: SystemCapability.Communication.IPC.Core 3615 3616**Parameters** 3617 | Name| Type| Mandatory| Description| 3618 | -------- | -------- | -------- | -------- | 3619 | 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.| 3620 | data | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object holding the data to send.| 3621 | reply | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object that receives the response.| 3622 | options | [MessageOption](#messageoption) | Yes| Request sending mode, which can be synchronous (default) or asynchronous.| 3623 | AsyncCallback | AsyncCallback<SendRequestResult> | Yes| Callback for receiving the sending result.| 3624 3625 3626**Example** 3627 3628 ``` 3629 class MyDeathRecipient { 3630 onRemoteDied() { 3631 console.log("server died"); 3632 } 3633 } 3634 class TestRemoteObject extends rpc.RemoteObject { 3635 constructor(descriptor) { 3636 super(descriptor); 3637 } 3638 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3639 return true; 3640 } 3641 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3642 return true; 3643 } 3644 isObjectDead(): boolean { 3645 return false; 3646 } 3647 } 3648 function sendRequestCallback(result) { 3649 if (result.errCode === 0) { 3650 console.log("sendRequest got result"); 3651 result.reply.readException(); 3652 let msg = result.reply.readString(); 3653 console.log("RPCTest: reply msg: " + msg); 3654 } else { 3655 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 3656 } 3657 console.log("RPCTest: sendRequest ends, reclaim parcel"); 3658 result.data.reclaim(); 3659 result.reply.reclaim(); 3660 } 3661 let testRemoteObject = new TestRemoteObject("testObject"); 3662 let option = new rpc.MessageOption(); 3663 let data = rpc.MessageParcel.create(); 3664 let reply = rpc.MessageParcel.create(); 3665 data.writeInt(1); 3666 data.writeString("hello"); 3667 testRemoteObject.sendRequest(1, data, reply, option, sendRequestCallback); 3668 ``` 3669 3670 3671### onRemoteRequest 3672 3673onRemoteRequest(code : number, data : MessageParcel, reply: MessageParcel, options : MessageOption): boolean 3674 3675Provides a response to **sendRequest()**. The server processes the request and returns a response in this function. 3676 3677**System capability**: SystemCapability.Communication.IPC.Core 3678 3679**Parameters** 3680 | Name| Type| Mandatory| Description| 3681 | -------- | -------- | -------- | -------- | 3682 | code | number | Yes| Service request code sent by the remote end.| 3683 | data | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object that holds the parameters called by the client.| 3684 | reply | [MessageParcel](#messageparcel) | Yes| **MessageParcel** object carrying the result.| 3685 | option | [MessageOption](#messageoption) | Yes| Whether the operation is synchronous or asynchronous.| 3686 3687**Return value** 3688 | Type| Description| 3689 | -------- | -------- | 3690 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3691 3692 3693**Example** 3694 3695 ``` 3696 class MyDeathRecipient { 3697 onRemoteDied() { 3698 console.log("server died"); 3699 } 3700 } 3701 class TestRemoteObject extends rpc.RemoteObject { 3702 constructor(descriptor) { 3703 super(descriptor); 3704 } 3705 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3706 return true; 3707 } 3708 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3709 return true; 3710 } 3711 isObjectDead(): boolean { 3712 return false; 3713 } 3714 3715 onRemoteRequest(code, data, reply, option) { 3716 if (code === 1) { 3717 console.log("RpcServer: onRemoteRequest called"); 3718 return true; 3719 } else { 3720 console.log("RpcServer: unknown code: " + code); 3721 return false; 3722 } 3723 } 3724 } 3725 ``` 3726 3727 3728### getCallingUid 3729 3730getCallingUid(): number 3731 3732Obtains the UID of the remote process. 3733 3734**System capability**: SystemCapability.Communication.IPC.Core 3735 3736**Return value** 3737 | Type| Description| 3738 | -------- | -------- | 3739 | number | UID of the remote process obtained.| 3740 3741 3742**Example** 3743 3744 ``` 3745 class MyDeathRecipient { 3746 onRemoteDied() { 3747 console.log("server died"); 3748 } 3749 } 3750 class TestRemoteObject extends rpc.RemoteObject { 3751 constructor(descriptor) { 3752 super(descriptor); 3753 } 3754 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3755 return true; 3756 } 3757 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3758 return true; 3759 } 3760 isObjectDead(): boolean { 3761 return false; 3762 } 3763 } 3764 let testRemoteObject = new TestRemoteObject("testObject"); 3765 console.log("RpcServer: getCallingUid: " + testRemoteObject.getCallingUid()); 3766 ``` 3767 3768 3769### getCallingPid 3770 3771getCallingPid(): number 3772 3773Obtains the PID of the remote process. 3774 3775**System capability**: SystemCapability.Communication.IPC.Core 3776 3777**Return value** 3778 | Type| Description| 3779 | -------- | -------- | 3780 | number | PID of the remote process obtained.| 3781 3782 3783**Example** 3784 3785 ``` 3786 class MyDeathRecipient { 3787 onRemoteDied() { 3788 console.log("server died"); 3789 } 3790 } 3791 class TestRemoteObject extends rpc.RemoteObject { 3792 constructor(descriptor) { 3793 super(descriptor); 3794 } 3795 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3796 return true; 3797 } 3798 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3799 return true; 3800 } 3801 isObjectDead(): boolean { 3802 return false; 3803 } 3804 } 3805 let testRemoteObject = new TestRemoteObject("testObject"); 3806 console.log("RpcServer: getCallingPid: " + testRemoteObject.getCallingPid()); 3807 ``` 3808 3809 3810### queryLocalInterface 3811 3812queryLocalInterface(descriptor: string): IRemoteBroker 3813 3814Checks whether the remote object corresponding to the specified interface descriptor exists. 3815 3816**System capability**: SystemCapability.Communication.IPC.Core 3817 3818**Parameters** 3819 | Name| Type| Mandatory| Description| 3820 | -------- | -------- | -------- | -------- | 3821 | descriptor | string | Yes| Interface descriptor.| 3822 3823**Return value** 3824 | Type| Description| 3825 | -------- | -------- | 3826 | IRemoteBroker | Returns the remote object if a match is found; returns **Null** otherwise.| 3827 3828 3829**Example** 3830 3831 ``` 3832 class MyDeathRecipient { 3833 onRemoteDied() { 3834 console.log("server died"); 3835 } 3836 } 3837 class TestRemoteObject extends rpc.RemoteObject { 3838 constructor(descriptor) { 3839 super(descriptor); 3840 } 3841 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3842 return true; 3843 } 3844 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3845 return true; 3846 } 3847 isObjectDead(): boolean { 3848 return false; 3849 } 3850 } 3851 let testRemoteObject = new TestRemoteObject("testObject"); 3852 let broker = testRemoteObject.queryLocalInterface("testObject"); 3853 ``` 3854 3855 3856### getInterfaceDescriptor 3857 3858getInterfaceDescriptor(): string 3859 3860Obtains the interface descriptor. 3861 3862**System capability**: SystemCapability.Communication.IPC.Core 3863 3864**Return value** 3865 | Type| Description| 3866 | -------- | -------- | 3867 | string | Interface descriptor obtained.| 3868 3869 3870**Example** 3871 3872 ``` 3873 class MyDeathRecipient { 3874 onRemoteDied() { 3875 console.log("server died"); 3876 } 3877 } 3878 class TestRemoteObject extends rpc.RemoteObject { 3879 constructor(descriptor) { 3880 super(descriptor); 3881 } 3882 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3883 return true; 3884 } 3885 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3886 return true; 3887 } 3888 isObjectDead(): boolean { 3889 return false; 3890 } 3891 } 3892 let testRemoteObject = new TestRemoteObject("testObject"); 3893 let descriptor = testRemoteObject.getInterfaceDescriptor(); 3894 console.log("RpcServer: descriptor is: " + descriptor); 3895 ``` 3896 3897 3898### attachLocalInterface 3899 3900attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 3901 3902Binds an interface descriptor to an **IRemoteBroker** object. 3903 3904**System capability**: SystemCapability.Communication.IPC.Core 3905 3906**Parameters** 3907 | Name| Type| Mandatory| Description| 3908 | -------- | -------- | -------- | -------- | 3909 | localInterface | IRemoteBroker | Yes| **IRemoteBroker** object.| 3910 | descriptor | string | Yes| Interface descriptor.| 3911 3912 3913**Example** 3914 3915 ``` 3916 class MyDeathRecipient { 3917 onRemoteDied() { 3918 console.log("server died"); 3919 } 3920 } 3921 class TestRemoteObject extends rpc.RemoteObject { 3922 constructor(descriptor) { 3923 super(descriptor); 3924 this.attachLocalInterface(this, descriptor); 3925 } 3926 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3927 return true; 3928 } 3929 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3930 return true; 3931 } 3932 isObjectDead(): boolean { 3933 return false; 3934 } 3935 asObject(): rpc.IRemoteObject { 3936 return this; 3937 } 3938 } 3939 let testRemoteObject = new TestRemoteObject("testObject"); 3940 ``` 3941 3942 3943## Ashmem<sup>8+</sup> 3944 3945Provides 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. 3946 3947The table below describes the protection types of the mapped memory. 3948 3949**System capability**: SystemCapability.Communication.IPC.Core 3950 3951 | Name| Value| Description| 3952| -------- | -------- | -------- | 3953| PROT_EXEC | 4 | The mapped memory is executable.| 3954| PROT_NONE | 0 | The mapped memory is inaccessible.| 3955| PROT_READ | 1 | The mapped memory is readable.| 3956| PROT_WRITE | 2 | The mapped memory is writeable.| 3957 3958 3959### createAshmem<sup>8+</sup> 3960 3961static createAshmem(name: string, size: number): Ashmem 3962 3963Creates an **Ashmem** object with the specified name and size. 3964 3965**System capability**: SystemCapability.Communication.IPC.Core 3966 3967**Parameters** 3968 | Name| Type| Mandatory| Description| 3969 | -------- | -------- | -------- | -------- | 3970 | name | string | Yes| Name of the **Ashmem** object to create.| 3971 | size | number | Yes| Size (in bytes) of the **Ashmem** object to create.| 3972 3973**Return value** 3974 | Type| Description| 3975 | -------- | -------- | 3976 | Ashmem | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 3977 3978 3979**Example** 3980 3981 ``` 3982 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 3983 let size = ashmem.getAshmemSize(); 3984 console.log("RpcTest: get ashemm by createAshmem : " + ashmem + " size is : " + size); 3985 ``` 3986 3987 3988### createAshmemFromExisting<sup>8+</sup> 3989 3990static createAshmemFromExisting(ashmem: Ashmem): Ashmem 3991 3992Creates an **Ashmem** object by copying the file descriptor (FD) of an existing Ashmem object. The two **Ashmem** objects point to the same shared memory region. 3993 3994**System capability**: SystemCapability.Communication.IPC.Core 3995 3996**Parameters** 3997 | Name| Type| Mandatory| Description| 3998 | -------- | -------- | -------- | -------- | 3999 | ashmem | Ashmem | Yes| Existing **Ashmem** object.| 4000 4001**Return value** 4002 | Type| Description| 4003 | -------- | -------- | 4004 | Ashmem | **Ashmem** object created.| 4005 4006 4007**Example** 4008 4009 ``` 4010 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 4011 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 4012 let size = ashmem2.getAshmemSize(); 4013 console.log("RpcTest: get ashemm by createAshmemFromExisting : " + ashmem2 + " size is : " + size); 4014 ``` 4015 4016 4017### closeAshmem<sup>8+</sup> 4018 4019closeAshmem(): void 4020 4021Closes this **Ashmem** object. 4022 4023**System capability**: SystemCapability.Communication.IPC.Core 4024 4025**Example** 4026 4027 ``` 4028 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 4029 ashmem.closeAshmem(); 4030 ``` 4031 4032 4033### unmapAshmem<sup>8+</sup> 4034 4035unmapAshmem(): void 4036 4037Deletes the mappings for the specified address range of this **Ashmem** object. 4038 4039**System capability**: SystemCapability.Communication.IPC.Core 4040 4041**Example** 4042 4043 ``` 4044 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 4045 ashmem.unmapAshmem(); 4046 ``` 4047 4048 4049### getAshmemSize<sup>8+</sup> 4050 4051getAshmemSize(): number 4052 4053Obtains the memory size of this **Ashmem** object. 4054 4055**System capability**: SystemCapability.Communication.IPC.Core 4056 4057**Return value** 4058 | Type| Description| 4059 | -------- | -------- | 4060 | number | **Ashmem** size obtained.| 4061 4062**Example** 4063 4064 ``` 4065 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 4066 let size = ashmem.getAshmemSize(); 4067 console.log("RpcTest: get ashmem is " + ashmem + " size is : " + size); 4068 ``` 4069 4070 4071### mapAshmem<sup>8+</sup> 4072 4073mapAshmem(mapType: number): boolean 4074 4075Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 4076 4077**System capability**: SystemCapability.Communication.IPC.Core 4078 4079**Parameters** 4080 | Name| Type| Mandatory| Description| 4081 | -------- | -------- | -------- | -------- | 4082 | mapType | number | Yes| Protection level of the memory region to which the shared file is mapped.| 4083 4084**Return value** 4085 | Type| Description| 4086 | -------- | -------- | 4087 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4088 4089**Example** 4090 4091 ``` 4092 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 4093 let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 4094 console.log("RpcTest: map ashmem result is : " + mapReadAndWrite); 4095 ``` 4096 4097 4098### mapReadAndWriteAshmem<sup>8+</sup> 4099 4100mapReadAndWriteAshmem(): boolean 4101 4102Maps the shared file to the readable and writable virtual address space of the process. 4103 4104**System capability**: SystemCapability.Communication.IPC.Core 4105 4106**Return value** 4107 | Type| Description| 4108 | -------- | -------- | 4109 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4110 4111**Example** 4112 4113 ``` 4114 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 4115 let mapResult = ashmem.mapReadAndWriteAshmem(); 4116 console.log("RpcTest: map ashmem result is : " + mapResult); 4117 ``` 4118 4119 4120### mapReadOnlyAshmem<sup>8+</sup> 4121 4122mapReadOnlyAshmem(): boolean 4123 4124Maps the shared file to the read-only virtual address space of the process. 4125 4126**System capability**: SystemCapability.Communication.IPC.Core 4127 4128**Return value** 4129 | Type| Description| 4130 | -------- | -------- | 4131 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4132 4133**Example** 4134 4135 ``` 4136 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 4137 let mapResult = ashmem.mapReadOnlyAshmem(); 4138 console.log("RpcTest: Ashmem mapReadOnlyAshmem result is : " + mapResult); 4139 ``` 4140 4141 4142### setProtection<sup>8+</sup> 4143 4144setProtection(protectionType: number): boolean 4145 4146Sets the protection level of the memory region to which the shared file is mapped. 4147 4148**System capability**: SystemCapability.Communication.IPC.Core 4149 4150**Parameters** 4151 | Name| Type| Mandatory| Description| 4152 | -------- | -------- | -------- | -------- | 4153 | protectionType | number | Yes| Protection type to set.| 4154 4155**Return value** 4156 | Type| Description| 4157 | -------- | -------- | 4158 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4159 4160**Example** 4161 4162 ``` 4163 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 4164 let result = ashmem.setProtection(ashmem.PROT_READ); 4165 console.log("RpcTest: Ashmem setProtection result is : " + result); 4166 ``` 4167 4168 4169### writeToAshmem<sup>8+</sup> 4170 4171writeToAshmem(buf: number[], size: number, offset: number): boolean 4172 4173Writes data to the shared file associated with this **Ashmem** object. 4174 4175**System capability**: SystemCapability.Communication.IPC.Core 4176 4177**Parameters** 4178 | Name| Type| Mandatory| Description| 4179 | -------- | -------- | -------- | -------- | 4180 | buf | number[] | Yes| Data to write.| 4181 | size | number | Yes| Size of the data to write.| 4182 | offset | number | Yes| Start position of the data to write in the memory region associated with this **Ashmem** object.| 4183 4184**Return value** 4185 | Type| Description| 4186 | -------- | -------- | 4187 | boolean | Returns **true** is the data is written successfully; returns **false** otherwise.| 4188 4189**Example** 4190 4191 ``` 4192 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 4193 let mapResult = ashmem.mapReadAndWriteAshmem(); 4194 console.info("RpcTest map ashmem result is " + mapResult); 4195 var ByteArrayVar = [1, 2, 3, 4, 5]; 4196 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 4197 console.log("RpcTest: write to Ashmem result is : " + writeResult); 4198 ``` 4199 4200 4201### readFromAshmem<sup>8+</sup> 4202 4203readFromAshmem(size: number, offset: number): number[] 4204 4205Reads data from the shared file associated with this **Ashmem** object. 4206 4207**System capability**: SystemCapability.Communication.IPC.Core 4208 4209**Parameters** 4210 | Name| Type| Mandatory| Description| 4211 | -------- | -------- | -------- | -------- | 4212 | size | number | Yes| Size of the data to read.| 4213 | offset | number | Yes| Start position of the data to read in the memory region associated with this **Ashmem** object.| 4214 4215**Return value** 4216 | Type| Description| 4217 | -------- | -------- | 4218 | number[] | Data read.| 4219 4220 4221**Example** 4222 4223 ``` 4224 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 4225 let mapResult = ashmem.mapReadAndWriteAshmem(); 4226 console.info("RpcTest map ashmem result is " + mapResult); 4227 var ByteArrayVar = [1, 2, 3, 4, 5]; 4228 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 4229 console.log("RpcTest: write to Ashmem result is : " + writeResult); 4230 let readResult = ashmem.readFromAshmem(5, 0); 4231 console.log("RpcTest: read to Ashmem result is : " + readResult); 4232 ``` 4233