1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_CHAOTIC_GOOD_FRAME_HEADER_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHAOTIC_GOOD_FRAME_HEADER_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <cstdint>
21 
22 #include "absl/status/statusor.h"
23 
24 #include "src/core/lib/gprpp/bitset.h"
25 
26 namespace grpc_core {
27 namespace chaotic_good {
28 
29 enum class FrameType : uint8_t {
30   kSettings = 0x00,
31   kFragment = 0x80,
32   kCancel = 0x81,
33 };
34 
35 struct FrameHeader {
36   FrameType type;
37   BitSet<2> flags;
38   uint32_t stream_id;
39   uint32_t header_length;
40   uint32_t message_length;
41   uint32_t message_padding;
42   uint32_t trailer_length;
43 
44   // Parses a frame header from a buffer of 24 bytes. All 24 bytes are consumed.
45   static absl::StatusOr<FrameHeader> Parse(const uint8_t* data);
46   // Serializes a frame header into a buffer of 24 bytes.
47   void Serialize(uint8_t* data) const;
48   // Compute frame sizes from the header.
49   uint32_t GetFrameLength() const;
50 
51   bool operator==(const FrameHeader& h) const {
52     return type == h.type && flags == h.flags && stream_id == h.stream_id &&
53            header_length == h.header_length &&
54            message_length == h.message_length &&
55            message_padding == h.message_padding &&
56            trailer_length == h.trailer_length;
57   }
58 };
59 
60 }  // namespace chaotic_good
61 }  // namespace grpc_core
62 
63 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_CHAOTIC_GOOD_FRAME_HEADER_H
64