鸿蒙开发通信与连接:ohos.rpc RPC通信

电子说

1.3w人已加入

描述

RPC通信

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

鸿蒙说明: 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 开发前请熟悉鸿蒙开发指导文档 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]

导入模块

import rpc from '@ohos.rpc';

MessageParcel

该类提供读写基础类型及数组、IPC对象、接口描述符和自定义序列化对象的方法。

create

create(): MessageParcel

静态方法,创建MessageParcel对象。

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

返回值:

类型说明
MessageParcel返回创建的MessageParcel对象。

示例:

let data = rpc.MessageParcel.create();
console.log("RpcClient: data is " + data);

reclaim

reclaim(): void

释放不再使用的MessageParcel对象。

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

示例:

let reply = rpc.MessageParcel.create();
reply.reclaim();

writeRemoteObject

writeRemoteObject(object: [IRemoteObject]): boolean

序列化远程对象并将其写入MessageParcel对象。

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

参数:

参数名类型必填说明
object[IRemoteObject]要序列化并写入MessageParcel的远程对象。

返回值:

类型说明
boolean如果操作成功,则返回true;否则返回false。

示例:

class MyDeathRecipient {
    onRemoteDied() {
        console.log("server died");
    }
}
class TestRemoteObject extends rpc.RemoteObject {
    constructor(descriptor) {
        super(descriptor);
    }
    addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    isObjectDead(): boolean {
        return false;
    }
}
let data = rpc.MessageParcel.create();
let testRemoteObject = new TestRemoteObject("testObject");
data.writeRemoteObject(testRemoteObject);

readRemoteObject

readRemoteObject(): IRemoteObject

从MessageParcel读取远程对象。此方法用于反序列化MessageParcel对象以生成IRemoteObject。远程对象按写入MessageParcel的顺序读取。

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

返回值:

类型说明
[IRemoteObject]读取到的远程对象。

示例:

class MyDeathRecipient {
    onRemoteDied() {
        console.log("server died");
    }
}
class TestRemoteObject extends rpc.RemoteObject {
    constructor(descriptor) {
        super(descriptor);
    }
    addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    isObjectDead(): boolean {
        return false;
    }
}
let data = rpc.MessageParcel.create();
let testRemoteObject = new TestRemoteObject("testObject");
data.writeRemoteObject(testRemoteObject);
let proxy = data.readRemoteObject();

writeInterfaceToken

writeInterfaceToken(token: string): boolean

将接口描述符写入MessageParcel对象。

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

参数:

参数名类型必填说明
tokenstring字符串类型描述符。

返回值:

类型说明
boolean如果操作成功,则返回true;否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeInterfaceToken("aaa");
console.log("RpcServer: writeInterfaceToken is " + result);

readInterfaceToken

readInterfaceToken(): string

从MessageParcel中读取接口描述符。接口描述符按写入MessageParcel的顺序读取。

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

返回值:

类型说明
string返回读取到的接口描述符。

示例:

class Stub extends rpc.RemoteObject {
    onRemoteRequest(code, data, reply, option) {
        let interfaceToken = data.readInterfaceToken();
        console.log("RpcServer: interfaceToken is " + interfaceToken);
        return true;
    }
}

getSize

getSize(): number

获取当前MessageParcel的数据大小。

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

返回值:

类型说明
number获取的MessageParcel的数据大小。以字节为单位。

示例:

let data = rpc.MessageParcel.create();
let size = data.getSize();
console.log("RpcClient: size is " + size);

getCapacity

getCapacity(): number

获取当前MessageParcel的容量。

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

返回值:

类型说明
number获取的MessageParcel的容量大小。以字节为单位。

示例:

let data = rpc.MessageParcel.create();
let result = data.getCapacity();
console.log("RpcClient: capacity is " + result);

setSize

setSize(size: number): boolean

设置MessageParcel实例中包含的数据大小。

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

参数:

参数名类型必填说明
sizenumberMessageParcel实例的数据大小。以字节为单位。

返回值:

类型说明
boolean设置成功返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let setSize = data.setSize(16);
console.log("RpcClient: setSize is " + setSize);

setCapacity

setCapacity(size: number): boolean

设置MessageParcel实例的存储容量。

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

参数:

参数名类型必填说明
sizenumberMessageParcel实例的存储容量。以字节为单位。

返回值:

类型说明
boolean设置成功返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.setCapacity(100);
console.log("RpcClient: setCapacity is " + result);

getWritableBytes

getWritableBytes(): number

获取MessageParcel的可写字节空间。

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

返回值:

类型说明
number获取到的MessageParcel的可写字节空间。以字节为单位。

示例:

class Stub extends rpc.RemoteObject {
    onRemoteRequest(code, data, reply, option) {
        let getWritableBytes = data.getWritableBytes();
        console.log("RpcServer: getWritableBytes is " + getWritableBytes);
        return true;
    }
}

getReadableBytes

getReadableBytes(): number

获取MessageParcel的可读字节空间。

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

返回值:

类型说明
number获取到的MessageParcel的可读字节空间。以字节为单位。

示例:

class Stub extends rpc.RemoteObject {
    onRemoteRequest(code, data, reply, option) {
        let result = data.getReadableBytes();
        console.log("RpcServer: getReadableBytes is " + result);
        return true;
    }
}

getReadPosition

getReadPosition(): number

获取MessageParcel的读位置。

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

返回值:

类型说明
number返回MessageParcel实例中的当前读取位置。

示例:

let data = rpc.MessageParcel.create();
let readPos = data.getReadPosition();
console.log("RpcClient: readPos is " + readPos);

getWritePosition

getWritePosition(): number

获取MessageParcel的写位置。

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

返回值:

类型说明
number返回MessageParcel实例中的当前写入位置。

示例:

let data = rpc.MessageParcel.create();
data.writeInt(10);
let bwPos = data.getWritePosition();
console.log("RpcClient: bwPos is " + bwPos);

rewindRead

rewindRead(pos: number): boolean

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

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

参数:

参数名类型必填说明
posnumber开始读取数据的目标位置。

返回值:

类型说明
boolean如果读取位置发生更改,则返回true;否则返回false。

示例:

let data = rpc.MessageParcel.create();
data.writeInt(12);
data.writeString("parcel");
let number = data.readInt();
console.log("RpcClient: number is " + number);
data.rewindRead(0);
let number2 = data.readInt();
console.log("RpcClient: rewindRead is " + number2);

rewindWrite

rewindWrite(pos: number): boolean

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

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

参数:

参数名类型必填说明
posnumber开始写入数据的目标位置。

返回值:

类型说明
boolean如果写入位置更改,则返回true;否则返回false。

示例:

let data = rpc.MessageParcel.create();
data.writeInt(4);
data.rewindWrite(0);
data.writeInt(5);
let number = data.readInt();
console.log("RpcClient: rewindWrite is: " + number);

writeByte

writeByte(val: number): boolean

将字节值写入MessageParcel实例。

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

参数:

参数名类型必填说明
valnumber要写入的字节值。

返回值:

类型说明
boolean写入返回成功,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeByte(2);
console.log("RpcClient: writeByte is: " + result);

readByte

readByte(): number

从MessageParcel实例读取字节值。

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

返回值:

类型说明
number返回字节值。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeByte(2);
console.log("RpcClient: writeByte is: " + result);
let ret = data.readByte();
console.log("RpcClient: readByte is: " + ret);

writeShort

writeShort(val: number): boolean

将短整数值写入MessageParcel实例。

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

参数:

参数名类型必填说明
valnumber要写入的短整数值。

返回值:

类型说明
boolean写入返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeShort(8);
console.log("RpcClient: writeShort is: " + result);

readShort

readShort(): number

从MessageParcel实例读取短整数值。

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

返回值:

类型说明
number返回短整数值。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeShort(8);
console.log("RpcClient: writeShort is: " + result);
let ret = data.readShort();
console.log("RpcClient: readShort is: " + ret);

writeInt

writeInt(val: number): boolean

将整数值写入MessageParcel实例。

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

参数:

参数名类型必填说明
valnumber要写入的整数值。

返回值:

类型说明
boolean写入返回成功,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeInt(10);
console.log("RpcClient: writeInt is " + result);

readInt

readInt(): number

从MessageParcel实例读取整数值。

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

返回值:

类型说明
number返回整数值。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeInt(10);
console.log("RpcClient: writeInt is " + result);
let ret = data.readInt();
console.log("RpcClient: readInt is " + ret);

writeLong

writeLong(val: number): boolean

将长整数值写入MessageParcel实例。

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

参数:

参数名类型必填说明
valnumber要写入的长整数值

返回值:

类型说明
boolean写入成功返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeLong(10000);
console.log("RpcClient: writeLong is " + result);

readLong

readLong(): number

从MessageParcel实例中读取长整数值。

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

返回值:

类型说明
number返回长整数值。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeLong(10000);
console.log("RpcClient: writeLong is " + result);
let ret = data.readLong();
console.log("RpcClient: readLong is " + ret);

writeFloat

writeFloat(val: number): boolean

将浮点值写入MessageParcel实例。

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

参数:

参数名类型必填说明
valnumber要写入的浮点值。

返回值:

类型说明
boolean写入成功返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeFloat(1.2);
console.log("RpcClient: writeFloat is " + result);

readFloat

readFloat(): number

从MessageParcel实例中读取浮点值。

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

返回值:

类型说明
number返回浮点值。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeFloat(1.2);
console.log("RpcClient: writeFloat is " + result);
let ret = data.readFloat();
console.log("RpcClient: readFloat is " + ret);

writeDouble

writeDouble(val: number): boolean

将双精度浮点值写入MessageParcel实例。

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

参数:

参数名类型必填说明
valnumber要写入的双精度浮点值。

返回值:

类型说明
boolean写入成功返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeDouble(10.2);
console.log("RpcClient: writeDouble is " + result);

readDouble

readDouble(): number

从MessageParcel实例读取双精度浮点值。

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

返回值:

类型说明
number返回双精度浮点值。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeDouble(10.2);
console.log("RpcClient: writeDouble is " + result);
let ret = data.readDouble();
console.log("RpcClient: readDouble is " + ret);

writeBoolean

writeBoolean(val: boolean): boolean

将布尔值写入MessageParcel实例。

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

参数:

参数名类型必填说明
valboolean要写入的布尔值。

返回值:

类型说明
boolean写入成功返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeBoolean(false);
console.log("RpcClient: writeBoolean is " + result);

readBoolean

readBoolean(): boolean

从MessageParcel实例读取布尔值。

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

返回值:

类型说明
boolean返回读取到的布尔值。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeBoolean(false);
console.log("RpcClient: writeBoolean is " + result);
let ret = data.readBoolean();
console.log("RpcClient: readBoolean is " + ret);

writeChar

writeChar(val: number): boolean

将单个字符值写入MessageParcel实例。

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

参数:

参数名类型必填说明
valnumber要写入的单个字符值。

返回值:

类型说明
boolean写入返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeChar(97);
console.log("RpcClient: writeChar is " + result);

readChar

readChar(): number

从MessageParcel实例中读取单个字符值。

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

返回值:

类型说明
number返回单个字符值。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeChar(97);
console.log("RpcClient: writeChar is " + result);
let ret = data.readChar();
console.log("RpcClient: readChar is " + ret);

writeString

writeString(val: string): boolean

将字符串值写入MessageParcel实例。

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

参数:

参数名类型必填说明
valstring要写入的字符串值,其长度应小于40960字节。

返回值:

类型说明
boolean写入成功返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeString('abc');
console.log("RpcClient: writeString  is " + result);

readString

readString(): string

从MessageParcel实例读取字符串值。

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

返回值:

类型说明
string返回字符串值。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeString('abc');
console.log("RpcClient: writeString  is " + result);
let ret = data.readString();
console.log("RpcClient: readString is " + ret);

writeSequenceable

writeSequenceable(val: Sequenceable): boolean

将自定义序列化对象写入MessageParcel实例。

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

参数:

参数名类型必填说明
val[Sequenceable]要写入的可序列对象。

返回值:

类型说明
boolean写入成功返回true,否则返回false。

示例:

class MySequenceable {
    num: number;
    str: string;
    constructor(num, str) {
        this.num = num;
        this.str = str;
    }
    marshalling(messageParcel) {
        messageParcel.writeInt(this.num);
        messageParcel.writeString(this.str);
        return true;
    }
    unmarshalling(messageParcel) {
        this.num = messageParcel.readInt();
        this.str = messageParcel.readString();
        return true;
    }
}
let sequenceable = new MySequenceable(1, "aaa");
let data = rpc.MessageParcel.create();
let result = data.writeSequenceable(sequenceable);
console.log("RpcClient: writeSequenceable is " + result);

readSequenceable

readSequenceable(dataIn: Sequenceable) : boolean

从MessageParcel实例中读取成员变量到指定的对象(dataIn)。

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

参数:

参数名类型必填说明
dataIn[Sequenceable]需要从MessageParcel读取成员变量的对象。

返回值:

类型说明
boolean如果反序列成功,则返回true;否则返回false。

示例:

class MySequenceable {
    num: number;
    str: string;
    constructor(num, str) {
        this.num = num;
        this.str = str;
    }
    marshalling(messageParcel) {
        messageParcel.writeInt(this.num);
        messageParcel.writeString(this.str);
        return true;
    }
    unmarshalling(messageParcel) {
        this.num = messageParcel.readInt();
        this.str = messageParcel.readString();
        return true;
    }
}
let sequenceable = new MySequenceable(1, "aaa");
let data = rpc.MessageParcel.create();
let result = data.writeSequenceable(sequenceable);
console.log("RpcClient: writeSequenceable is " + result);
let ret = new MySequenceable(0, "");
let result2 = data.readSequenceable(ret);
console.log("RpcClient: writeSequenceable is " + result2);

writeByteArray

writeByteArray(byteArray: number[]): boolean

将字节数组写入MessageParcel实例。

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

参数:

参数名类型必填说明
byteArraynumber[]要写入的字节数组。

返回值:

类型说明
boolean写入成功返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let ByteArrayVar = [1, 2, 3, 4, 5];
let result = data.writeByteArray(ByteArrayVar);
console.log("RpcClient: writeByteArray is " + result);

readByteArray

readByteArray(dataIn: number[]) : void

从MessageParcel实例读取字节数组。

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

参数:

参数名类型必填说明
dataInnumber[]要读取的字节数组。

示例:

let data = rpc.MessageParcel.create();
let ByteArrayVar = [1, 2, 3, 4, 5];
let result = data.writeByteArray(ByteArrayVar);
console.log("RpcClient: writeByteArray is " + result);
let array = new Array(5);
data.readByteArray(array);

readByteArray

readByteArray(): number[]

从MessageParcel实例中读取字节数组。

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

返回值:

类型说明
number[]返回字节数组。

示例:

let data = rpc.MessageParcel.create();
let ByteArrayVar = [1, 2, 3, 4, 5];
let result = data.writeByteArray(ByteArrayVar);
console.log("RpcClient: writeByteArray is " + result);
let array = data.readByteArray();
console.log("RpcClient: readByteArray is " + array);

writeShortArray

writeShortArray(shortArray: number[]): boolean

将短整数数组写入MessageParcel实例。

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

参数:

参数名类型必填说明
shortArraynumber[]要写入的短整数数组。

返回值:

类型说明
boolean写入返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeShortArray([11, 12, 13]);
console.log("RpcClient: writeShortArray is " + result);

readShortArray

readShortArray(dataIn: number[]) : void

从MessageParcel实例中读取短整数数组。

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

参数:

参数名类型必填说明
dataInnumber[]要读取的短整数数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeShortArray([11, 12, 13]);
console.log("RpcClient: writeShortArray is " + result);
let array = new Array(3);
data.readShortArray(array);

readShortArray

readShortArray(): number[]

从MessageParcel实例中读取短整数数组。

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

返回值:

类型说明
number[]返回短整数数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeShortArray([11, 12, 13]);
console.log("RpcClient: writeShortArray is " + result);
let array = data.readShortArray();
console.log("RpcClient: readShortArray is " + array);

writeIntArray

writeIntArray(intArray: number[]): boolean

将整数数组写入MessageParcel实例。

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

参数:

参数名类型必填说明
intArraynumber[]要写入的整数数组。

返回值:

类型说明
boolean写入返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeIntArray([100, 111, 112]);
console.log("RpcClient: writeIntArray is " + result);

readIntArray

readIntArray(dataIn: number[]) : void

从MessageParcel实例中读取整数数组。

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

参数:

参数名类型必填说明
dataInnumber[]要读取的整数数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeIntArray([100, 111, 112]);
console.log("RpcClient: writeIntArray is " + result);
let array = new Array(3);
data.readIntArray(array);

readIntArray

readIntArray(): number[]

从MessageParcel实例中读取整数数组。

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

返回值:

类型说明
number[]返回整数数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeIntArray([100, 111, 112]);
console.log("RpcClient: writeIntArray is " + result);
let array = data.readIntArray();
console.log("RpcClient: readIntArray is " + array);

writeLongArray

writeLongArray(longArray: number[]): boolean

将长整数数组写入MessageParcel实例。

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

参数:

参数名类型必填说明
longArraynumber[]要写入的长整数数组。

返回值:

类型说明
boolean写入返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeLongArray([1111, 1112, 1113]);
console.log("RpcClient: writeLongArray is " + result);

readLongArray

readLongArray(dataIn: number[]) : void

从MessageParcel实例读取长整数数组。

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

参数:

参数名类型必填说明
dataInnumber[]要读取的长整数数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeLongArray([1111, 1112, 1113]);
console.log("RpcClient: writeLongArray is " + result);
let array = new Array(3);
data.readLongArray(array);

readLongArray

readLongArray(): number[]

从MessageParcel实例中读取长整数数组。

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

返回值:

类型说明
number[]返回长整数数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeLongArray([1111, 1112, 1113]);
console.log("RpcClient: writeLongArray is " + result);
let array = data.readLongArray();
console.log("RpcClient: readLongArray is " + array);

writeFloatArray

writeFloatArray(floatArray: number[]): boolean

将浮点数组写入MessageParcel实例。

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

参数:

参数名类型必填说明
floatArraynumber[]要写入的浮点数组。

返回值:

类型说明
boolean写入返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeFloatArray([1.2, 1.3, 1.4]);
console.log("RpcClient: writeFloatArray is " + result);

readFloatArray

readFloatArray(dataIn: number[]) : void

从MessageParcel实例中读取浮点数组。

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

参数:

参数名类型必填说明
dataInnumber[]要读取的浮点数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeFloatArray([1.2, 1.3, 1.4]);
console.log("RpcClient: writeFloatArray is " + result);
let array = new Array(3);
data.readFloatArray(array);

readFloatArray

readFloatArray(): number[]

从MessageParcel实例中读取浮点数组。

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

返回值:

类型说明
number[]返回浮点数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeFloatArray([1.2, 1.3, 1.4]);
console.log("RpcClient: writeFloatArray is " + result);
let array = data.readFloatArray();
console.log("RpcClient: readFloatArray is " + array);

writeDoubleArray

writeDoubleArray(doubleArray: number[]): boolean

将双精度浮点数组写入MessageParcel实例。

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

参数:

参数名类型必填说明
doubleArraynumber[]要写入的双精度浮点数组。

返回值:

类型说明
boolean写入返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
console.log("RpcClient: writeDoubleArray is " + result);

readDoubleArray

readDoubleArray(dataIn: number[]) : void

从MessageParcel实例中读取双精度浮点数组。

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

参数:

参数名类型必填说明
dataInnumber[]要读取的双精度浮点数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
console.log("RpcClient: writeDoubleArray is " + result);
let array = new Array(3);
data.readDoubleArray(array);

readDoubleArray

readDoubleArray(): number[]

从MessageParcel实例读取双精度浮点数组。

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

返回值:

类型说明
number[]返回双精度浮点数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
console.log("RpcClient: writeDoubleArray is " + result);
let array = data.readDoubleArray();
console.log("RpcClient: readDoubleArray is " + array);

writeBooleanArray

writeBooleanArray(booleanArray: boolean[]): boolean

将布尔数组写入MessageParcel实例。

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

参数:

参数名类型必填说明
booleanArrayboolean[]要写入的布尔数组。

返回值:

类型说明
boolean写入成功返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeBooleanArray([false, true, false]);
console.log("RpcClient: writeBooleanArray is " + result);

readBooleanArray

readBooleanArray(dataIn: boolean[]) : void

从MessageParcel实例中读取布尔数组。

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

参数:

参数名类型必填说明
dataInboolean[]要读取的布尔数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeBooleanArray([false, true, false]);
console.log("RpcClient: writeBooleanArray is " + result);
let array = new Array(3);
data.readBooleanArray(array);

readBooleanArray

readBooleanArray(): boolean[]

从MessageParcel实例中读取布尔数组。

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

返回值:

类型说明
boolean[]返回布尔数组。
let data = rpc.MessageParcel.create();
let result = data.writeBooleanArray([false, true, false]);
console.log("RpcClient: writeBooleanArray is " + result);
let array = data.readBooleanArray();
console.log("RpcClient: readBooleanArray is " + array);

writeCharArray

writeCharArray(charArray: number[]): boolean

将单个字符数组写入MessageParcel实例。

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

参数:

参数名类型必填说明
charArraynumber[]要写入的单个字符数组。

返回值:

类型说明
boolean写入成功返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeCharArray([97, 98, 88]);
console.log("RpcClient: writeCharArray is " + result);

readCharArray

readCharArray(dataIn: number[]) : void

从MessageParcel实例中读取单个字符数组。

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

参数:

参数名类型必填说明
dataInnumber[]要读取的单个字符数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeCharArray([97, 98, 99]);
console.log("RpcClient: writeCharArray is " + result);
let array = new Array(3);
data.readCharArray(array);

readCharArray

readCharArray(): number[]

从MessageParcel实例读取单个字符数组。

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

返回值:

类型说明
number[]返回单个字符数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeCharArray([97, 98, 99]);
console.log("RpcClient: writeCharArray is " + result);
let array = data.readCharArray();
console.log("RpcClient: readCharArray is " + array);

writeStringArray

writeStringArray(stringArray: string[]): boolean

将字符串数组写入MessageParcel实例。

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

参数:

参数名类型必填说明
stringArraystring[]要写入的字符串数组,数组单个元素的长度应小于40960字节。

返回值:

类型说明
boolean写入成功返回true,否则返回false。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeStringArray(["abc", "def"]);
console.log("RpcClient: writeStringArray is " + result);

readStringArray

readStringArray(dataIn: string[]) : void

从MessageParcel实例读取字符串数组。

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

参数:

参数名类型必填说明
dataInstring[]要读取的字符串数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeStringArray(["abc", "def"]);
console.log("RpcClient: writeStringArray is " + result);
let array = new Array(2);
data.readStringArray(array);

readStringArray

readStringArray(): string[]

从MessageParcel实例读取字符串数组。

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

返回值:

类型说明
string[]返回字符串数组。

示例:

let data = rpc.MessageParcel.create();
let result = data.writeStringArray(["abc", "def"]);
console.log("RpcClient: writeStringArray is " + result);
let array = data.readStringArray();
console.log("RpcClient: readStringArray is " + array);

writeNoException8+

writeNoException(): void

向MessageParcel写入“指示未发生异常”的信息。

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

示例:

class MyDeathRecipient {
    onRemoteDied() {
        console.log("server died");
    }
}
class TestRemoteObject extends rpc.RemoteObject {
    constructor(descriptor) {
        super(descriptor);
    }
    addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    isObjectDead(): boolean {
        return false;
    }
    onRemoteRequest(code, data, reply, option) {
        if (code === 1) {
            console.log("RpcServer: onRemoteRequest called");
            reply.writeNoException();
            return true;
        } else {
            console.log("RpcServer: unknown code: " + code);
            return false;
        }
    }
}

readException8+

readException(): void

从MessageParcel中读取异常。

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

示例:

import FA from "@ohos.ability.featureAbility";
let proxy;
let connect = {
    onConnect: function(elementName, remoteProxy) {
        console.log("RpcClient: js onConnect called.");
        proxy = remoteProxy;
    },
    onDisconnect: function(elementName) {
        console.log("RpcClient: onDisconnect");
    },
    onFailed: function() {
        console.log("RpcClient: onFailed");
    }
};
let want = {
    "bundleName": "com.ohos.server",
    "abilityName": "com.ohos.server.MainAbility",
};
FA.connectAbility(want, connect);
let option = new rpc.MessageOption();
let data = rpc.MessageParcel.create();
let reply = rpc.MessageParcel.create();
data.writeInt(1);
data.writeString("hello");
proxy.sendRequest(1, data, reply, option)
    .then(function(errCode) {
        if (errCode === 0) {
            console.log("sendRequest got result");
            reply.readException();
            let msg = reply.readString();
            console.log("RPCTest: reply msg: " + msg);
        } else {
            console.log("RPCTest: sendRequest failed, errCode: " + errCode);
        }
    }).catch(function(e) {
        console.log("RPCTest: sendRequest got exception: " + e.message);
    }).finally (() = > {
        console.log("RPCTest: sendRequest ends, reclaim parcel");
        data.reclaim();
        reply.reclaim();
    });

writeSequenceableArray

writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean

将可序列化对象数组写入MessageParcel实例。

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

参数:

参数名类型必填说明
sequenceableArraySequenceable[]要写入的可序列化对象数组。

返回值:

类型说明
boolean写入成功返回true,否则返回false。

示例:

class MySequenceable {
    num: number;
    str: string;
    constructor(num, str) {
        this.num = num;
        this.str = str;
    }
    marshalling(messageParcel) {
        messageParcel.writeInt(this.num);
        messageParcel.writeString(this.str);
        return true;
    }
    unmarshalling(messageParcel) {
        this.num = messageParcel.readInt();
        this.str = messageParcel.readString();
        return true;
    }
}
let sequenceable = new MySequenceable(1, "aaa");
let sequenceable2 = new MySequenceable(2, "bbb");
let sequenceable3 = new MySequenceable(3, "ccc");
let a = [sequenceable, sequenceable2, sequenceable3];
let data = rpc.MessageParcel.create();
let result = data.writeSequenceableArray(a);
console.log("RpcClient: writeSequenceableArray is " + result);

readSequenceableArray8+

readSequenceableArray(sequenceableArray: Sequenceable[]): void

从MessageParcel实例读取可序列化对象数组。

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

参数:

参数名类型必填说明
sequenceableArraySequenceable[]要读取的可序列化对象数组。

示例:

class MySequenceable {
    num: number;
    str: string;
    constructor(num, str) {
        this.num = num;
        this.str = str;
    }
    marshalling(messageParcel) {
        messageParcel.writeInt(this.num);
        messageParcel.writeString(this.str);
        return true;
    }
    unmarshalling(messageParcel) {
        this.num = messageParcel.readInt();
        this.str = messageParcel.readString();
        return true;
    }
}
let sequenceable = new MySequenceable(1, "aaa");
let sequenceable2 = new MySequenceable(2, "bbb");
let sequenceable3 = new MySequenceable(3, "ccc");
let a = [sequenceable, sequenceable2, sequenceable3];
let data = rpc.MessageParcel.create();
let result = data.writeSequenceableArray(a);
console.log("RpcClient: writeSequenceableArray is " + result);
let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")];
data.readSequenceableArray(b);

writeRemoteObjectArray8+

writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean

将IRemoteObject对象数组写入MessageParcel。

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

参数:

参数名类型必填说明
objectArrayIRemoteObject[]要写入MessageParcel的IRemoteObject对象数组。

返回值:

类型说明
boolean如果IRemoteObject对象数组成功写入MessageParcel,则返回true;如果对象为null或数组写入MessageParcel失败,则返回false。

示例:

class MyDeathRecipient {
    onRemoteDied() {
        console.log("server died");
    }
}
class TestRemoteObject extends rpc.RemoteObject {
    constructor(descriptor) {
        super(descriptor);
        this.attachLocalInterface(this, descriptor);
    }
    addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    isObjectDead(): boolean {
        return false;
    }
    asObject(): rpc.IRemoteObject {
        return this;
    }
}
let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
let data = rpc.MessageParcel.create();
let result = data.writeRemoteObjectArray(a);
console.log("RpcClient: writeRemoteObjectArray is " + result);

[](https://gitee.com/openharmony/docs/blob/OpenHarmony-3.2-Beta1/zh-cn/application-dev/reference/apis/js-apis-rpc.md#readremoteobjectarray8)readRemoteObjectArray8+

readRemoteObjectArray(objects: IRemoteObject[]): void

从MessageParcel读取IRemoteObject对象数组。

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

参数:

参数名类型必填说明
objectsIRemoteObject[]从MessageParcel读取的IRemoteObject对象数组。

示例:

class MyDeathRecipient {
    onRemoteDied() {
        console.log("server died");
    }
}
class TestRemoteObject extends rpc.RemoteObject {
    constructor(descriptor) {
        super(descriptor);
        this.attachLocalInterface(this, descriptor);
    }
    addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    isObjectDead(): boolean {
        return false;
    }
    asObject(): rpc.IRemoteObject {
        return this;
    }
}
let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
let data = rpc.MessageParcel.create();
let result = data.writeRemoteObjectArray(a);
let b = new Array(3);
data.readRemoteObjectArray(b);

readRemoteObjectArray8+

readRemoteObjectArray(): IRemoteObject[]

从MessageParcel读取IRemoteObject对象数组。

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

返回值:

类型说明
IRemoteObject[]返回IRemoteObject对象数组。

示例:

class MyDeathRecipient {
    onRemoteDied() {
        console.log("server died");
    }
}
class TestRemoteObject extends rpc.RemoteObject {
    constructor(descriptor) {
        super(descriptor);
        this.attachLocalInterface(this, descriptor);
    }
    addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
        return true;
    }
    isObjectDead(): boolean {
        return false;
    }
    asObject(): rpc.IRemoteObject {
        return this;
    }
}
let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
let data = rpc.MessageParcel.create();
let result = data.writeRemoteObjectArray(a);
console.log("RpcClient: readRemoteObjectArray is " + result);
let b = data.readRemoteObjectArray();
console.log("RpcClient: readRemoteObjectArray is " + b);

closeFileDescriptor8+

static closeFileDescriptor(fd: number): void

关闭给定的文件描述符。

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

参数:

参数名类型必填说明
fdnumber要关闭的文件描述符。

示例:

import fileio from '@ohos.fileio';
let filePath = "path/to/file";
let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
rpc.MessageParcel.closeFileDescriptor(fd);

dupFileDescriptor8+

static dupFileDescriptor(fd: number) :number

复制给定的文件描述符。

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

参数:

参数名类型必填说明
fdnumber表示已存在的文件描述符。

返回值:

类型说明
number返回新的文件描述符。

示例:

import fileio from '@ohos.fileio';
let filePath = "path/to/file";
let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
let newFd = rpc.MessageParcel.dupFileDescriptor(fd);

containFileDescriptors8+

containFileDescriptors(): boolean

检查此MessageParcel对象是否包含文件描述符。

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

返回值:

类型说明
boolean如果此MessageParcel对象包含文件描述符,则返回true;否则返回false。

示例:

import fileio from '@ohos.fileio';
let parcel = new rpc.MessageParcel();
let filePath = "path/to/file";
let r1 = parcel.containFileDescriptors();
let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
let writeResult = parcel.writeFileDescriptor(fd);
console.log("RpcTest: parcel writeFd result is : " + writeResult);
let containFD = parcel.containFileDescriptors();
console.log("RpcTest: parcel after write fd containFd result is : " + containFD);

writeFileDescriptor8+

writeFileDescriptor(fd: number): boolean

写入文件描述符到MessageParcel。

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

参数:

参数名类型必填说明
fdnumber文件描述符。

返回值:

类型说明
boolean如果操作成功,则返回true;否则返回false。

示例:

import fileio from '@ohos.fileio';
let parcel = new rpc.MessageParcel();
let filePath = "path/to/file";
let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
let writeResult = parcel.writeFileDescriptor(fd);
console.log("RpcTest: parcel writeFd result is : " + writeResult);

readFileDescriptor8+

readFileDescriptor(): number

从MessageParcel中读取文件描述符。

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

返回值:

类型说明
number返回文件描述符。

示例:

import fileio from '@ohos.fileio';
let parcel = new rpc.MessageParcel();
let filePath = "path/to/file";
let fd = fileio.openSync(filePath, 0o2| 0o100, 0o666);
let writeResult = parcel.writeFileDescriptor(fd);
let readFD = parcel.readFileDescriptor();
console.log("RpcTest: parcel read fd is : " + readFD);

writeAshmem8+

writeAshmem(ashmem: Ashmem): boolean

将指定的匿名共享对象写入此MessageParcel。

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

参数:

参数名类型必填说明
ashmemAshmem要写入MessageParcel的匿名共享对象。

返回值:

类型说明
boolean如果匿名共享对象成功写入此MessageParcel,则返回true;否则返回false。

示例:

let parcel = new rpc.MessageParcel();
let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024);
let isWriteSuccess = parcel.writeAshmem(ashmem);
console.log("RpcTest: write ashmem to result is : " + isWriteSuccess);

readAshmem8+

readAshmem(): Ashmem

从MessageParcel读取匿名共享对象。

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

返回值:

类型说明
Ashmem返回匿名共享对象。

示例:

let parcel = new rpc.MessageParcel();
let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024);
let isWriteSuccess = parcel.writeAshmem(ashmem);
console.log("RpcTest: write ashmem to result is : " + isWriteSuccess);
let readAshmem = parcel.readAshmem();
console.log("RpcTest: read ashmem to result is : " + readAshmem);

getRawDataCapacity8+

getRawDataCapacity(): number

获取MessageParcel可以容纳的最大原始数据量。

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

返回值:

类型说明
number返回MessageParcel可以容纳的最大原始数据量,即128 Mb。

示例:

let parcel = new rpc.MessageParcel();
let result = parcel.getRawDataCapacity();
console.log("RpcTest: parcel get RawDataCapacity result is : " + result);

writeRawData8+

writeRawData(rawData: number[], size: number): boolean

将原始数据写入MessageParcel对象。

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

参数:

参数名类型必填说明
rawDatanumber[]要写入的原始数据。
sizenumber发送的原始数据大小,以字节为单位。

返回值:

类型说明
boolean如果操作成功,则返回true;否则返回false。

示例:

let parcel = new rpc.MessageParcel();
let arr = [1, 2, 3, 4, 5];
let isWriteSuccess = parcel.writeRawData(arr, arr.length);
console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess);

readRawData8+

readRawData(size: number): number[]

从MessageParcel读取原始数据。

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

参数:

参数名类型必填说明
sizenumber要读取的原始数据的大小。

返回值:

类型说明
number[]返回原始数据(以字节为单位)。

示例:

let parcel = new rpc.MessageParcel();
let arr = [1, 2, 3, 4, 5];
let isWriteSuccess = parcel.writeRawData(arr, arr.length);
console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess);
let result = parcel.readRawData(5);
console.log("RpcTest: parcel read raw data result is : " + result);

Sequenceable

在进程间通信(IPC)期间,将类的对象写入MessageParcel并从MessageParcel中恢复它们。

marshalling

marshalling(dataOut: MessageParcel): boolean

将此可序列对象封送到MessageParcel中。

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

参数:

参数名类型必填说明
dataOut[MessageParcel]可序列对象将被封送到的MessageParcel对象。

返回值:

类型说明
boolean如果封送成功,则返回true;否则返回false。

示例:

class MySequenceable {
    num: number;
    str: string;
    constructor(num, str) {
        this.num = num;
        this.str = str;
    }
    marshalling(messageParcel) {
        messageParcel.writeInt(this.num);
        messageParcel.writeString(this.str);
        return true;
    }
    unmarshalling(messageParcel) {
        this.num = messageParcel.readInt();
        this.str = messageParcel.readString();
        return true;
    }
}
let sequenceable = new MySequenceable(1, "aaa");
let data = rpc.MessageParcel.create();
let result = data.writeSequenceable(sequenceable);
console.log("RpcClient: writeSequenceable is " + result);
let ret = new MySequenceable(0, "");
let result2 = data.readSequenceable(ret);
console.log("RpcClient: readSequenceable is " + result2);

unmarshalling

unmarshalling(dataIn: MessageParcel) : boolean

从MessageParcel中解封此可序列对象。

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

参数:

参数名类型必填说明
dataIn[MessageParcel]已将可序列对象封送到其中的MessageParcel对象。

返回值:

类型说明
boolean如果可序列化成功,则返回true;否则返回false。

示例:

class MySequenceable {
    num: number;
    str: string;
    constructor(num, str) {
        this.num = num;
        this.str = str;
    }
    marshalling(messageParcel) {
        messageParcel.writeInt(this.num);
        messageParcel.writeString(this.str);
        return true;
    }
    unmarshalling(messageParcel) {
        this.num = messageParcel.readInt();
        this.str = messageParcel.readString();
        return true;
    }
}
let sequenceable = new MySequenceable(1, "aaa");
let data = rpc.MessageParcel.create();
let result = data.writeSequenceable(sequenceable);
console.log("RpcClient: writeSequenceable is " + result);
let ret = new MySequenceable(0, "");
let result2 = data.readSequenceable(ret);
console.log("RpcClient: readSequenceable is " + result2);

IRemoteBroker

远端对象的代理持有者。用于获取代理对象。

asObject

asObject(): IRemoteObject

需派生类实现,获取代理或远端对象。

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

返回值:

类型说明
[IRemoteObject]如果调用者是[RemoteObject]对象,则直接返回本身;如果调用者是[RemoteProxy]对象,则返回它的持有者[IRemoteObject]。

示例:

class TestAbility extends rpc.RemoteObject {
    asObject() {
        return this;
    }
}

示例:

class TestProxy {
    remote: rpc.RemoteObject;
    constructor(remote) {
        this.remote = remote;
    }
    asObject() {
        return this.remote;
    }
}

DeathRecipient

用于订阅远端对象的死亡通知。当被订阅该通知的远端对象死亡时,本端可收到消息,调用[onRemoteDied]接口。远端对象死亡可以为远端对象所在进程死亡,远端对象所在设备关机或重启,当远端对象与本端对象属于不同设备时,也可为远端对象离开组网时。

onRemoteDied

onRemoteDied(): void

HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿

鸿蒙

在成功添加死亡通知订阅后,当远端对象死亡时,将自动调用本方法。

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

示例:

class MyDeathRecipient {
    onRemoteDied() {
        console.log("server died");
    }
}

审核编辑 黄宇

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分