aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc/ts/packets.ts
blob: 68cf80393a1fc411c060509aac7b31fdd1a1223b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2022 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

/** 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';

// Channel, Service, Method
type idSet = [number, number, number];

export function decode(data: Uint8Array): packetPb.RpcPacket {
  return packetPb.RpcPacket.deserializeBinary(data);
}

export function decodePayload(payload: Uint8Array, payloadType: any): Message {
  const message = payloadType.deserializeBinary(payload);
  return message;
}

export function forServer(packet: packetPb.RpcPacket): boolean {
  return packet.getType() % 2 == 0;
}

export function encodeClientError(
  packet: packetPb.RpcPacket,
  status: Status
): Uint8Array {
  const errorPacket = new packetPb.RpcPacket();
  errorPacket.setType(packetPb.PacketType.CLIENT_ERROR);
  errorPacket.setChannelId(packet.getChannelId());
  errorPacket.setMethodId(packet.getMethodId());
  errorPacket.setServiceId(packet.getServiceId());
  errorPacket.setStatus(status);
  return errorPacket.serializeBinary();
}

export function encodeClientStream(ids: idSet, message: Message): Uint8Array {
  const streamPacket = new packetPb.RpcPacket();
  streamPacket.setType(packetPb.PacketType.CLIENT_STREAM);
  streamPacket.setChannelId(ids[0]);
  streamPacket.setServiceId(ids[1]);
  streamPacket.setMethodId(ids[2]);
  streamPacket.setPayload(message.serializeBinary());
  return streamPacket.serializeBinary();
}

export function encodeClientStreamEnd(ids: idSet): Uint8Array {
  const streamEnd = new packetPb.RpcPacket();
  streamEnd.setType(packetPb.PacketType.CLIENT_STREAM_END);
  streamEnd.setChannelId(ids[0]);
  streamEnd.setServiceId(ids[1]);
  streamEnd.setMethodId(ids[2]);
  return streamEnd.serializeBinary();
}

export function encodeRequest(ids: idSet, request?: Message): Uint8Array {
  const payload: Uint8Array =
    typeof request !== 'undefined'
      ? request.serializeBinary()
      : new Uint8Array();

  const packet = new packetPb.RpcPacket();
  packet.setType(packetPb.PacketType.REQUEST);
  packet.setChannelId(ids[0]);
  packet.setServiceId(ids[1]);
  packet.setMethodId(ids[2]);
  packet.setPayload(payload);
  return packet.serializeBinary();
}

export function encodeResponse(ids: idSet, response: Message): Uint8Array {
  const packet = new packetPb.RpcPacket();
  packet.setType(packetPb.PacketType.RESPONSE);
  packet.setChannelId(ids[0]);
  packet.setServiceId(ids[1]);
  packet.setMethodId(ids[2]);
  packet.setPayload(response.serializeBinary());
  return packet.serializeBinary();
}

export function encodeCancel(ids: idSet): Uint8Array {
  const packet = new packetPb.RpcPacket();
  packet.setType(packetPb.PacketType.CLIENT_ERROR);
  packet.setStatus(Status.CANCELLED);
  packet.setChannelId(ids[0]);
  packet.setServiceId(ids[1]);
  packet.setMethodId(ids[2]);
  return packet.serializeBinary();
}