Skip to content

Commit e87f9b8

Browse files
committed
UPD | chunked #5
1 parent 71e4a32 commit e87f9b8

File tree

6 files changed

+31
-23
lines changed

6 files changed

+31
-23
lines changed

include/components/Buffer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ namespace manapi {
5757

5858
[[nodiscard]] std::size_t realsize () const;
5959

60+
void realresize (std::size_t s);
61+
6062
void resize (std::size_t s);
6163

6264
void resize_max (std::size_t s);

main.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ int main () {
206206
manapi::net::hash::SHA256 hash{};
207207
hash.init();
208208
try {
209-
co_await req.callback_async([&result, &hash] (const char *buffer, ssize_t size, bool fin)
210-
-> manapi::future<ssize_t> {
209+
co_await req.callback_sync([&result, &hash] (const char *buffer, ssize_t size, bool fin)
210+
-> ssize_t {
211211
hash.update(reinterpret_cast<const uint8_t *>(buffer), size);
212212
result += size;
213-
co_return size;
213+
return size;
214214
});
215215
}
216216
catch (std::exception const &e) {
@@ -232,17 +232,8 @@ int main () {
232232
-> manapi::future<> {
233233
resp.header(manapi::net::http::HEADER.CONTENT_LENGTH, req.header(manapi::net::http::HEADER.CONTENT_LENGTH));
234234
co_return resp.callback_stream([&resp, &req] (manapi::net::http::response::resp_stream_cb cb) -> manapi::future<> {
235-
manapi::filesystem::fstream f ("/home/Timur/Downloads/test.txt", req.cancellation());
236-
auto res = co_await f.open(manapi::ev::FS_O_RDONLY);
237-
if (!res.ok()) {
238-
co_return;
239-
}
240-
co_await req.callback_async([f, cb = std::move(cb)] (const char *buffer, ssize_t size, bool fin) mutable
235+
co_await req.callback_async([cb = std::move(cb)] (const char *buffer, ssize_t size, bool fin) mutable
241236
-> manapi::future<ssize_t> {
242-
//auto buffer1 = manapi::async::current()->memory_fabric().slice(size);
243-
//auto res = co_await f.fread(buffer1.data(), buffer1.size());
244-
//assert(res == buffer1.size());
245-
//assert(memcmp(buffer1.data(), buffer, size) == 0);
246237
co_return co_await cb (buffer, size, fin);
247238
});
248239
});

src/components/Buffer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ std::size_t manapi::bytebuffer::realsize() const {
105105
return this->reserved < 0 ? this->s : this->reserved;
106106
}
107107

108-
void manapi::bytebuffer::resize(std::size_t s) {
108+
void manapi::bytebuffer::realresize(std::size_t s) {
109109
if (this->s == s)
110110
return;
111111

@@ -146,6 +146,10 @@ void manapi::bytebuffer::resize(std::size_t s) {
146146
this->shift_ = std::min(this->shift_, this->s);
147147
}
148148

149+
void manapi::bytebuffer::resize(std::size_t s) {
150+
this->realresize(s + this->shift_);
151+
}
152+
149153
void manapi::bytebuffer::resize_max(std::size_t s) {
150154
this->resize(std::max(s, this->realsize()));
151155
}

src/worker/TCP.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,18 @@ void manapi::net::worker::TCP::flush_read_(const shared_conn &conn, connection_i
565565
object.shift_add(data->top->recv.deque_current);
566566
data->top->recv.deque_current = 0;
567567
}
568+
569+
if (!object.empty()) {
570+
data->ev_callback->operator()(conn, ev::READ, object.data(),
571+
static_cast<int>(object.size()), &object);
572+
}
568573
}
569574
}
575+
if (data->status & ev::READ
576+
&& !(data->status & (CONN_CLOSED|CONN_REMOVED))
577+
&& !data->watcher->is_active()) {
578+
data->watcher->read_start();
579+
}
570580
}
571581

572582
void manapi::net::worker::TCP::tcp_handle_read_chunked(const shared_conn &conn, connection_interface *data, int flags, const char *buffer, ssize_t size, ibuffpool_t *p) {
@@ -972,17 +982,15 @@ void manapi::net::worker::TCP::http_work_(http::http_v1_1_t *http_v1_1_ctx, cons
972982
}
973983

974984
bytebuffer obj;
975-
if (!p) {
985+
if (p) {
976986
p->shift_add(static_cast<int>(buff-p->data()));
977987
assert(p->size() == size && p->data() == buff);
978988
}
979989
else {
980990
obj = this->bufferpool().slice(
981-
std::max(this->config_->buffer_size, size + 5));
991+
std::max(this->config_->buffer_size, size));
982992
p = &obj;
983-
memcpy (p->data() + 5, buff, size);
984-
p->resize(size + 5);
985-
p->shift_add(5);
993+
memcpy (p->data(), buff, size);
986994
}
987995
connection_io_send_start(&data->top->recv, std::move(*p), &data->top->recv_size);
988996

src/worker/TLS.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ int manapi::net::worker::TLS::ssl_bio_flush_read_(const shared_conn &conn, void
527527
auto data = conn->as<TLS::connection_interface>();
528528

529529
do {
530+
if (top->last_deque) {
531+
assert (top->last_deque->buffer.size() >= top->deque_cursor);
532+
}
530533
if (!top->last_deque || top->last_deque->buffer.size() == top->deque_cursor) {
531534
if (auto const res = ssl_flush_recv(conn, top, cnt)) {
532535
return res;
@@ -569,7 +572,7 @@ int manapi::net::worker::TLS::ssl_bio_flush_read_(const shared_conn &conn, void
569572
top->deque_cursor += rhs;
570573
if (!rhs && (flags /* an empty buffer was created */ )) {
571574
/* remove an empty buffer at the end */
572-
connection_io_trim(top, parent, cnt);
575+
//connection_io_trim(top, parent, cnt);
573576
}
574577
}
575578
else {

src/worker/base_worker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ void manapi::net::worker::base::connection_io_send_start(connection_io_part *top
243243
top->deque = std::make_unique<buffer_deque>(std::move(buff), nullptr);
244244
top->last_deque = top->deque.get();
245245
top->deque_current = 0;
246-
top->deque_cursor = static_cast<int>(top->deque->buffer.size() + top->deque->buffer.shift());
247-
top->deque->buffer.resize(top->deque->buffer.realsize());
246+
top->deque_cursor = static_cast<int>(top->deque->buffer.size());
247+
top->deque->buffer.realresize(top->deque->buffer.realsize());
248248
(*cnt)++;
249249
}
250250
}
@@ -253,7 +253,7 @@ void manapi::net::worker::base::connection_io_trim(struct connection_io_part *to
253253
if (!top->deque_cursor && top->last_deque) {
254254
if (parent) {
255255
parent->next = nullptr;
256-
top->deque_cursor = static_cast<int>(parent->buffer.size()+parent->buffer.shift());
256+
top->deque_cursor = static_cast<int>(parent->buffer.size());
257257
top->last_deque = parent;
258258
}
259259
else {

0 commit comments

Comments
 (0)