Skip to content

Commit 3f94857

Browse files
committed
tcp: allow buffer reuse and pop completed after pushing to on_read
1 parent bc7ad77 commit 3f94857

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

src/net/tcp/read_request.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ namespace tcp {
5757
const auto rem = buf->capacity() - buf->size();
5858
const auto end_seq = buf->end_seq(); // store end_seq if reseted in callback
5959

60-
// Ready buffer for userspace consumption
61-
complete_buffers.push_back(buf->buffer());
60+
if (on_read_callback != nullptr) {
61+
on_read_callback(buf->buffer());
62+
} else {
63+
// Ready buffer for read_next
64+
complete_buffers.push_back(buf->buffer());
65+
}
6266

6367
// this is the only one, so we can reuse it
6468
if(buffers.size() == 1)
@@ -191,14 +195,17 @@ namespace tcp {
191195
void Read_request::signal_data() {
192196

193197
if (not complete_buffers.empty()) {
194-
if (on_read_callback != nullptr) {
195-
for (auto buf : complete_buffers) {
196-
on_read_callback(buf);
197-
}
198-
} else if (on_data_callback != nullptr){
198+
if (on_data_callback != nullptr){
199199
on_data_callback();
200200
if (not complete_buffers.empty()) {
201201
// FIXME: Make sure this event gets re-triggered
202+
// For now the user will have to make sure to re-read later if they couldn't
203+
}
204+
} else if (on_read_callback != nullptr) {
205+
for (auto buf : complete_buffers) {
206+
// Pop each time, in case callback leads to another call here.
207+
complete_buffers.pop_front();
208+
on_read_callback(buf);
202209
}
203210
}
204211
}

0 commit comments

Comments
 (0)