diff options
Diffstat (limited to 'pw_rpc/ts/packets.ts')
-rw-r--r-- | pw_rpc/ts/packets.ts | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/pw_rpc/ts/packets.ts b/pw_rpc/ts/packets.ts index 68cf80393..b6cb71395 100644 --- a/pw_rpc/ts/packets.ts +++ b/pw_rpc/ts/packets.ts @@ -14,33 +14,34 @@ /** Functions for working with pw_rpc packets. */ -import {Message} from 'google-protobuf'; -import {MethodDescriptorProto} from 'google-protobuf/google/protobuf/descriptor_pb'; -import * as packetPb from 'pigweedjs/protos/pw_rpc/internal/packet_pb'; -import {Status} from 'pigweedjs/pw_status'; +import { Message } from 'google-protobuf'; +import { + RpcPacket, + PacketType, +} from 'pigweedjs/protos/pw_rpc/internal/packet_pb'; +import { Status } from 'pigweedjs/pw_status'; // Channel, Service, Method type idSet = [number, number, number]; -export function decode(data: Uint8Array): packetPb.RpcPacket { - return packetPb.RpcPacket.deserializeBinary(data); +export function decode(data: Uint8Array): RpcPacket { + return RpcPacket.deserializeBinary(data); } -export function decodePayload(payload: Uint8Array, payloadType: any): Message { - const message = payloadType.deserializeBinary(payload); - return message; +export function decodePayload(payload: Uint8Array, payloadType: any): any { + return payloadType['deserializeBinary'](payload); } -export function forServer(packet: packetPb.RpcPacket): boolean { +export function forServer(packet: RpcPacket): boolean { return packet.getType() % 2 == 0; } export function encodeClientError( - packet: packetPb.RpcPacket, - status: Status + packet: RpcPacket, + status: Status, ): Uint8Array { - const errorPacket = new packetPb.RpcPacket(); - errorPacket.setType(packetPb.PacketType.CLIENT_ERROR); + const errorPacket = new RpcPacket(); + errorPacket.setType(PacketType.CLIENT_ERROR); errorPacket.setChannelId(packet.getChannelId()); errorPacket.setMethodId(packet.getMethodId()); errorPacket.setServiceId(packet.getServiceId()); @@ -49,18 +50,19 @@ export function encodeClientError( } export function encodeClientStream(ids: idSet, message: Message): Uint8Array { - const streamPacket = new packetPb.RpcPacket(); - streamPacket.setType(packetPb.PacketType.CLIENT_STREAM); + const streamPacket = new RpcPacket(); + streamPacket.setType(PacketType.CLIENT_STREAM); streamPacket.setChannelId(ids[0]); streamPacket.setServiceId(ids[1]); streamPacket.setMethodId(ids[2]); - streamPacket.setPayload(message.serializeBinary()); + const msgSerialized = (message as any)['serializeBinary'](); + streamPacket.setPayload(msgSerialized); return streamPacket.serializeBinary(); } export function encodeClientStreamEnd(ids: idSet): Uint8Array { - const streamEnd = new packetPb.RpcPacket(); - streamEnd.setType(packetPb.PacketType.CLIENT_STREAM_END); + const streamEnd = new RpcPacket(); + streamEnd.setType(PacketType.CLIENT_REQUEST_COMPLETION); streamEnd.setChannelId(ids[0]); streamEnd.setServiceId(ids[1]); streamEnd.setMethodId(ids[2]); @@ -70,11 +72,11 @@ export function encodeClientStreamEnd(ids: idSet): Uint8Array { export function encodeRequest(ids: idSet, request?: Message): Uint8Array { const payload: Uint8Array = typeof request !== 'undefined' - ? request.serializeBinary() - : new Uint8Array(); + ? (request as any)['serializeBinary']() + : new Uint8Array(0); - const packet = new packetPb.RpcPacket(); - packet.setType(packetPb.PacketType.REQUEST); + const packet = new RpcPacket(); + packet.setType(PacketType.REQUEST); packet.setChannelId(ids[0]); packet.setServiceId(ids[1]); packet.setMethodId(ids[2]); @@ -83,18 +85,19 @@ export function encodeRequest(ids: idSet, request?: Message): Uint8Array { } export function encodeResponse(ids: idSet, response: Message): Uint8Array { - const packet = new packetPb.RpcPacket(); - packet.setType(packetPb.PacketType.RESPONSE); + const packet = new RpcPacket(); + packet.setType(PacketType.RESPONSE); packet.setChannelId(ids[0]); packet.setServiceId(ids[1]); packet.setMethodId(ids[2]); - packet.setPayload(response.serializeBinary()); + const msgSerialized = (response as any)['serializeBinary'](); + packet.setPayload(msgSerialized); return packet.serializeBinary(); } export function encodeCancel(ids: idSet): Uint8Array { - const packet = new packetPb.RpcPacket(); - packet.setType(packetPb.PacketType.CLIENT_ERROR); + const packet = new RpcPacket(); + packet.setType(PacketType.CLIENT_ERROR); packet.setStatus(Status.CANCELLED); packet.setChannelId(ids[0]); packet.setServiceId(ids[1]); |