MCPcopy Create free account
hub / github.com/apache/brpc / CutMessageIntoFileDescriptor

Method CutMessageIntoFileDescriptor

src/brpc/stream.cpp:148–215  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

146}
147
148ssize_t Stream::CutMessageIntoFileDescriptor(int /*fd*/,
149 butil::IOBuf **data_list,
150 size_t size) {
151 if (_host_socket == NULL) {
152 CHECK(false) << "Not connected";
153 errno = EBADF;
154 return -1;
155 }
156 if (!_remote_settings.writable()) {
157 LOG(WARNING) << "The remote side of Stream=" << id()
158 << "->" << _remote_settings.stream_id()
159 << "@" << _host_socket->remote_side()
160 << " doesn't have a handler";
161 errno = EBADF;
162 return -1;
163 }
164 butil::IOBuf out;
165 ssize_t len = 0;
166 ssize_t unwritten_data_size = 0;
167 for (size_t i = 0; i < size; ++i) {
168 butil::IOBuf *data = data_list[i];
169 size_t length = data->length();
170 if (length > FLAGS_stream_write_max_segment_size) {
171 if (unwritten_data_size) {
172 WriteToHostSocket(&out);
173 unwritten_data_size = 0;
174 out.clear();
175 }
176 // segmenting large data into multiple parts
177 butil::IOBuf segment_buf;
178 bool has_continuation = true;
179 while (has_continuation) {
180 data->cutn(&segment_buf, FLAGS_stream_write_max_segment_size);
181 StreamFrameMeta fm;
182 fm.set_stream_id(_remote_settings.stream_id());
183 fm.set_source_stream_id(id());
184 fm.set_frame_type(FRAME_TYPE_DATA);
185 has_continuation = !data->empty();
186 fm.set_has_continuation(has_continuation);
187 policy::PackStreamMessage(&out, fm, &segment_buf);
188 len += segment_buf.length();
189 segment_buf.clear();
190 WriteToHostSocket(&out);
191 out.clear();
192 }
193 } else {
194 if (unwritten_data_size + length > FLAGS_stream_write_max_segment_size) {
195 WriteToHostSocket(&out);
196 unwritten_data_size = 0;
197 out.clear();
198 }
199 unwritten_data_size += length;
200 StreamFrameMeta fm;
201 fm.set_stream_id(_remote_settings.stream_id());
202 fm.set_source_stream_id(id());
203 fm.set_frame_type(FRAME_TYPE_DATA);
204 fm.set_has_continuation(false);
205 policy::PackStreamMessage(&out, fm, data_list[i]);

Callers 2

StartWriteMethod · 0.80
DoWriteMethod · 0.80

Calls 8

idFunction · 0.85
PackStreamMessageFunction · 0.85
stream_idMethod · 0.45
remote_sideMethod · 0.45
lengthMethod · 0.45
clearMethod · 0.45
cutnMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected