Skip to content

Commit

Permalink
rubbly
Browse files Browse the repository at this point in the history
  • Loading branch information
ctiller committed Dec 13, 2024
1 parent 89cd9a2 commit 406018b
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 410 deletions.
253 changes: 0 additions & 253 deletions src/core/ext/transport/chaotic_good/chaotic_good_transport.h

This file was deleted.

30 changes: 30 additions & 0 deletions src/core/ext/transport/chaotic_good/frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "src/core/ext/transport/chaotic_good/chaotic_good_frame.pb.h"
#include "src/core/ext/transport/chaotic_good/frame_header.h"
#include "src/core/lib/promise/context.h"
#include "src/core/lib/promise/switch.h"
#include "src/core/lib/resource_quota/arena.h"
#include "src/core/lib/slice/slice.h"
#include "src/core/lib/slice/slice_buffer.h"
Expand Down Expand Up @@ -286,5 +287,34 @@ std::string MessageChunkFrame::ToString() const {
", payload=", payload.Length(), "b}");
}

namespace {
template <typename T>
absl::StatusOr<T> DeserializeFrame(const FrameHeader& header,
SliceBuffer payload) {
T frame;
GRPC_TRACE_LOG(chaotic_good, INFO)
<< "CHAOTIC_GOOD: Deserialize " << header << " with payload "
<< absl::CEscape(payload.JoinIntoString());
CHECK_EQ(header.payload_length, payload.Length());
auto s = frame.Deserialize(header, std::move(payload));
GRPC_TRACE_LOG(chaotic_good, INFO)
<< "CHAOTIC_GOOD: DeserializeFrame "
<< (s.ok() ? frame.ToString() : s.ToString());
if (s.ok()) return std::move(frame);
return std::move(s);
}
} // namespace

absl::StatusOr<Frame> ParseFrame(const FrameHeader& header,
SliceBuffer payload) {
return Switch(header.type, Case<FrameType, FrameType::kSettings>([&]() {
return DeserializeFrame<SettingsFrame>(header,
std::move(payload));
}),
Default([]() -> absl::StatusOr<Frame> {
return absl::InternalError("bah");
}));
}

} // namespace chaotic_good
} // namespace grpc_core
3 changes: 3 additions & 0 deletions src/core/ext/transport/chaotic_good/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ using Frame =
ServerTrailingMetadataFrame, MessageFrame, BeginMessageFrame,
MessageChunkFrame, ClientEndOfStream, CancelFrame>;

absl::StatusOr<Frame> ParseFrame(const FrameHeader& header,
SliceBuffer payload);

} // namespace chaotic_good
} // namespace grpc_core

Expand Down
42 changes: 1 addition & 41 deletions src/core/ext/transport/chaotic_good/frame_header.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,8 @@
namespace grpc_core {
namespace chaotic_good {

namespace {
void WriteLittleEndianUint32(uint32_t value, uint8_t* data) {
data[0] = static_cast<uint8_t>(value);
data[1] = static_cast<uint8_t>(value >> 8);
data[2] = static_cast<uint8_t>(value >> 16);
data[3] = static_cast<uint8_t>(value >> 24);
}

uint32_t ReadLittleEndianUint32(const uint8_t* data) {
return static_cast<uint32_t>(data[0]) |
(static_cast<uint32_t>(data[1]) << 8) |
(static_cast<uint32_t>(data[2]) << 16) |
(static_cast<uint32_t>(data[3]) << 24);
}
} // namespace

// Serializes a frame header into a buffer of 24 bytes.
void FrameHeader::Serialize(uint8_t* data) const {
WriteLittleEndianUint32((static_cast<uint32_t>(type) << 16) |
static_cast<uint32_t>(payload_connection_id),
data);
WriteLittleEndianUint32(stream_id, data + 4);
WriteLittleEndianUint32(payload_length, data + 8);
}

// Parses a frame header from a buffer of 24 bytes. All 24 bytes are consumed.
absl::StatusOr<FrameHeader> FrameHeader::Parse(const uint8_t* data) {
FrameHeader header;
const uint32_t type_and_conn_id = ReadLittleEndianUint32(data);
if (type_and_conn_id & 0xff000000u) {
return absl::InternalError("Non-zero reserved byte received");
}
header.type = static_cast<FrameType>(type_and_conn_id >> 16);
header.payload_connection_id = type_and_conn_id & 0xffff;
header.stream_id = ReadLittleEndianUint32(data + 4);
header.payload_length = ReadLittleEndianUint32(data + 8);
return header;
}

std::string FrameHeader::ToString() const {
return absl::StrCat("[type:", type, " conn:", payload_connection_id,
" stream_id:", stream_id,
return absl::StrCat("[type:", type, " stream_id:", stream_id,
" payload_length:", payload_length, "]");
}

Expand Down
18 changes: 0 additions & 18 deletions src/core/ext/transport/chaotic_good/frame_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,16 @@ void AbslStringify(Sink& sink, FrameType type) {

struct FrameHeader {
FrameType type = FrameType::kCancel;
uint16_t payload_connection_id = 0;
uint32_t stream_id = 0;
uint32_t payload_length = 0;

// Parses a frame header from a buffer of 12 bytes. All 12 bytes are consumed.
static absl::StatusOr<FrameHeader> Parse(const uint8_t* data);
// Serializes a frame header into a buffer of 12 bytes.
void Serialize(uint8_t* data) const;
// Report contents as a string
std::string ToString() const;
// Required padding to maintain alignment.
uint32_t Padding(uint32_t alignment) const {
if (payload_connection_id == 0) {
return 0;
}
if (payload_length % alignment == 0) {
return 0;
}
return alignment - (payload_length % alignment);
}

bool operator==(const FrameHeader& h) const {
return type == h.type && stream_id == h.stream_id &&
payload_connection_id == h.payload_connection_id &&
payload_length == h.payload_length;
}
// Frame header size is fixed to 12 bytes.
enum { kFrameHeaderSize = 12 };
};

inline std::ostream& operator<<(std::ostream& out, const FrameHeader& h) {
Expand Down
Loading

0 comments on commit 406018b

Please sign in to comment.