// Copyright 2020 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.
syntax = "proto3";

package pw.rpc.internal;

option java_package = "dev.pigweed.pw_rpc.internal";

enum PacketType {
  // To simplify identifying the origin of a packet, client-to-server packets
  // use even numbers and server-to-client packets use odd numbers.

  // Client-to-server packets

  // The client invokes an RPC. Always the first packet.
  REQUEST = 0;

  // A message in a client stream. Always sent after a REQUEST and before a
  // CLIENT_REQUEST_COMPLETION.
  CLIENT_STREAM = 2;

  // The client received a packet for an RPC it did not request.
  CLIENT_ERROR = 4;

  // Client has requested for call completion. In client streaming and
  // bi-directional streaming RPCs, this also indicates that the client is done
  // with sending requests.
  CLIENT_REQUEST_COMPLETION = 8;

  // Server-to-client packets

  // The RPC has finished.
  RESPONSE = 1;

  // The server was unable to process a request.
  SERVER_ERROR = 5;

  // A message in a server stream.
  SERVER_STREAM = 7;

  // Reserve field numbers for deprecated PacketTypes.
  reserved 3;  // SERVER_STREAM_END (equivalent to RESPONSE now)
  reserved 6;  // CANCEL (replaced by CLIENT_ERROR with status CANCELLED)
}

message RpcPacket {
  // The type of packet. Determines which other fields are used.
  PacketType type = 1;

  // Channel through which the packet is sent.
  uint32 channel_id = 2;

  // Hash of the fully-qualified name of the service with which this packet is
  // associated. For RPC packets, this is the service that processes the packet.
  fixed32 service_id = 3;

  // Hash of the name of the method which should process this packet.
  fixed32 method_id = 4;

  // The packet's payload, which is an encoded protobuf.
  bytes payload = 5;

  // Status code for the RPC response or error.
  uint32 status = 6;

  // Unique identifier for the call that initiated this RPC. Optionally set by
  // the client in the initial request and sent in all subsequent client
  // packets; echoed by the server.
  uint32 call_id = 7;
}
