frame_data.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
22 
23 #include <stdlib.h>
24 
25 #include "absl/status/status.h"
26 #include "absl/strings/str_format.h"
27 
28 #include <grpc/slice_buffer.h>
29 #include <grpc/support/log.h>
30 
35 
37  uint32_t stream_id,
38  grpc_chttp2_stream* s) {
41  "unsupported data flags: 0x%02x stream: %d", flags, stream_id));
42  }
43 
45  s->received_last_frame = true;
46  s->eos_received = true;
47  } else {
48  s->received_last_frame = false;
49  }
50 
51  return absl::OkStatus();
52 }
53 
55  uint32_t write_bytes, int is_eof,
58  grpc_slice hdr;
59  uint8_t* p;
60  static const size_t header_size = 9;
61 
62  hdr = GRPC_SLICE_MALLOC(header_size);
63  p = GRPC_SLICE_START_PTR(hdr);
64  GPR_ASSERT(write_bytes < (1 << 24));
65  *p++ = static_cast<uint8_t>(write_bytes >> 16);
66  *p++ = static_cast<uint8_t>(write_bytes >> 8);
67  *p++ = static_cast<uint8_t>(write_bytes);
69  *p++ = is_eof ? GRPC_CHTTP2_DATA_FLAG_END_STREAM : 0;
70  *p++ = static_cast<uint8_t>(id >> 24);
71  *p++ = static_cast<uint8_t>(id >> 16);
72  *p++ = static_cast<uint8_t>(id >> 8);
73  *p++ = static_cast<uint8_t>(id);
75 
77 
78  stats->framing_bytes += header_size;
79  stats->data_bytes += write_bytes;
80 }
81 
83  grpc_chttp2_stream* s, uint32_t* min_progress_size,
84  grpc_core::SliceBuffer* stream_out, uint32_t* message_flags) {
85  grpc_slice_buffer* slices = &s->frame_storage;
87 
88  if (slices->length < 5) {
89  if (min_progress_size != nullptr) *min_progress_size = 5 - slices->length;
90  return grpc_core::Pending{};
91  }
92 
93  uint8_t header[5];
95 
96  switch (header[0]) {
97  case 0:
98  if (message_flags != nullptr) *message_flags = 0;
99  break;
100  case 1:
101  if (message_flags != nullptr) {
102  *message_flags = GRPC_WRITE_INTERNAL_COMPRESS;
103  }
104  break;
105  default:
107  absl::StrFormat("Bad GRPC frame type 0x%02x", header[0]));
109  static_cast<intptr_t>(s->id));
110  return error;
111  }
112 
113  uint64_t length = (static_cast<uint32_t>(header[1]) << 24) |
114  (static_cast<uint32_t>(header[2]) << 16) |
115  (static_cast<uint32_t>(header[3]) << 8) |
116  static_cast<uint32_t>(header[4]);
117 
118  if (slices->length < length + 5) {
119  if (min_progress_size != nullptr) {
120  *min_progress_size = length + 5 - slices->length;
121  }
122  return grpc_core::Pending{};
123  }
124 
125  if (min_progress_size != nullptr) *min_progress_size = 0;
126 
127  if (stream_out != nullptr) {
128  s->stats.incoming.framing_bytes += 5;
129  s->stats.incoming.data_bytes += length;
132  }
133 
134  return GRPC_ERROR_NONE;
135 }
136 
140  const grpc_slice& slice,
141  int is_last) {
143  grpc_slice_buffer_add(&s->frame_storage, slice);
145 
146  if (is_last && s->received_last_frame) {
148  t, s, true, false,
150  "Data frame with END_STREAM flag received")
151  : GRPC_ERROR_NONE);
152  }
153 
154  return GRPC_ERROR_NONE;
155 }
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
grpc_slice_ref_internal
const grpc_slice & grpc_slice_ref_internal(const grpc_slice &slice)
Definition: slice_refcount.h:32
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
internal.h
grpc_chttp2_maybe_complete_recv_message
void grpc_chttp2_maybe_complete_recv_message(grpc_chttp2_transport *t, grpc_chttp2_stream *s)
Definition: chttp2_transport.cc:1956
outbuf
unsigned char outbuf[SIZE]
Definition: bloaty/third_party/zlib/examples/gun.c:162
GRPC_SLICE_MALLOC
#define GRPC_SLICE_MALLOC(len)
Definition: include/grpc/slice.h:70
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_slice_buffer_move_first_into_buffer
GPRAPI void grpc_slice_buffer_move_first_into_buffer(grpc_slice_buffer *src, size_t n, void *dst)
Definition: slice/slice_buffer.cc:358
absl::OkStatus
Status OkStatus()
Definition: third_party/abseil-cpp/absl/status/status.h:882
frame_data.h
absl::InternalError
Status InternalError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:347
xds_manager.p
p
Definition: xds_manager.py:60
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
grpc_chttp2_stream
Definition: src/core/ext/transport/chttp2/transport/internal.h:456
inbuf
unsigned char inbuf[SIZE]
Definition: bloaty/third_party/zlib/examples/gun.c:161
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_core::Pending
Definition: poll.h:29
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_core::SliceBuffer::c_slice_buffer
grpc_slice_buffer * c_slice_buffer()
Return a pointer to the back raw grpc_slice_buffer.
Definition: src/core/lib/slice/slice_buffer.h:117
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
gen_stats_data.stats
list stats
Definition: gen_stats_data.py:58
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
header
struct absl::base_internal::@2940::AllocList::Header header
GRPC_ERROR_INT_STREAM_ID
@ GRPC_ERROR_INT_STREAM_ID
Definition: error.h:63
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
slice_buffer.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
grpc_chttp2_data_parser_parse
grpc_error_handle grpc_chttp2_data_parser_parse(void *, grpc_chttp2_transport *t, grpc_chttp2_stream *s, const grpc_slice &slice, int is_last)
Definition: frame_data.cc:137
grpc_transport_one_way_stats
Definition: transport.h:243
GRPC_ERROR_CREATE_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
Definition: error.h:291
grpc_slice_buffer_add
GPRAPI void grpc_slice_buffer_add(grpc_slice_buffer *sb, grpc_slice slice)
Definition: slice/slice_buffer.cc:170
grpc_slice_buffer_move_first_no_ref
GPRAPI void grpc_slice_buffer_move_first_no_ref(grpc_slice_buffer *src, size_t n, grpc_slice_buffer *dst)
Definition: slice/slice_buffer.cc:353
grpc_core::SliceBuffer
Definition: src/core/lib/slice/slice_buffer.h:44
GRPC_WRITE_INTERNAL_COMPRESS
#define GRPC_WRITE_INTERNAL_COMPRESS
Definition: transport.h:75
grpc_error_set_int
grpc_error_handle grpc_error_set_int(grpc_error_handle src, grpc_error_ints which, intptr_t value)
Definition: error.cc:613
grpc_deframe_unprocessed_incoming_frames
grpc_core::Poll< grpc_error_handle > grpc_deframe_unprocessed_incoming_frames(grpc_chttp2_stream *s, uint32_t *min_progress_size, grpc_core::SliceBuffer *stream_out, uint32_t *message_flags)
Definition: frame_data.cc:82
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
GRPC_CHTTP2_DATA_FLAG_END_STREAM
#define GRPC_CHTTP2_DATA_FLAG_END_STREAM
Definition: frame.h:37
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
GRPC_CHTTP2_FRAME_DATA
#define GRPC_CHTTP2_FRAME_DATA
Definition: frame.h:28
GRPC_ERROR_CREATE_FROM_CPP_STRING
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
Definition: error.h:297
slices
SliceBuffer * slices
Definition: retry_filter.cc:631
grpc_chttp2_transport
Definition: src/core/ext/transport/chttp2/transport/internal.h:238
slice_refcount.h
grpc_chttp2_mark_stream_closed
void grpc_chttp2_mark_stream_closed(grpc_chttp2_transport *t, grpc_chttp2_stream *s, int close_reads, int close_writes, grpc_error_handle error)
Definition: chttp2_transport.cc:2193
grpc_slice_buffer_move_first
GPRAPI void grpc_slice_buffer_move_first(grpc_slice_buffer *src, size_t n, grpc_slice_buffer *dst)
Definition: slice/slice_buffer.cc:348
transport.h
grpc_chttp2_encode_data
void grpc_chttp2_encode_data(uint32_t id, grpc_slice_buffer *inbuf, uint32_t write_bytes, int is_eof, grpc_transport_one_way_stats *stats, grpc_slice_buffer *outbuf)
Definition: frame_data.cc:54
grpc_slice_buffer
Definition: include/grpc/impl/codegen/slice.h:83
grpc_chttp2_data_parser_begin_frame
absl::Status grpc_chttp2_data_parser_begin_frame(uint8_t flags, uint32_t stream_id, grpc_chttp2_stream *s)
Definition: frame_data.cc:36
grpc_error
Definition: error_internal.h:42
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
grpc_slice_buffer_copy_first_into_buffer
void grpc_slice_buffer_copy_first_into_buffer(grpc_slice_buffer *src, size_t n, void *dst)
Definition: slice/slice_buffer.cc:384
slice_buffer.h
id
uint32_t id
Definition: flow_control_fuzzer.cc:70
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:22