Skip to content

Commit 532c258

Browse files
committed
FIX | tls over tcp upload fix
1 parent de32eae commit 532c258

File tree

3 files changed

+48
-74
lines changed

3 files changed

+48
-74
lines changed

include/worker/TLS.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace manapi::net::worker {
8686

8787
int ssl_bio_flush_write_ (const shared_conn &conn, TLS::connection_interface *m, int max_cnt);
8888

89-
int ssl_bio_flush_read_ (const shared_conn &conn, void *rbio, connection_io_part *top, int *cnt, int max_cnt);
89+
int ssl_bio_flush_read_ (const shared_conn &conn, TLS::connection_interface *m, int max_cnt);
9090

9191
int ssl_flush_recv (const shared_conn &conn, connection_io_part *top, int *cnt);
9292
};

src/worker/TCP.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ int manapi::net::worker::TCP::event_flags(const shared_conn & conn, int flags) {
542542
flush_read_ (conn, data);
543543

544544
}
545-
if (!(status & CONN_CLOSED|CONN_WRITE)) {
545+
546+
if (data->watcher) {
546547
if (status & ev::READ) {
547548
if (!data->watcher->is_active()) {
548549
assert(!data->watcher->read_start());
@@ -553,10 +554,11 @@ int manapi::net::worker::TCP::event_flags(const shared_conn & conn, int flags) {
553554
assert(!data->watcher->read_stop());
554555
}
555556
}
557+
}
556558

557-
if ((status & CONN_RECV_END) && (status & CONN_READ) && data->ev_callback) {
558-
data->ev_callback->operator()(conn, CONN_RECV_END, nullptr, 0, nullptr);
559-
}
559+
if ((status & CONN_CLOSED|CONN_WRITE|CONN_RECV_END|CONN_READ) == (CONN_RECV_END|CONN_READ)
560+
&& data->ev_callback) {
561+
data->ev_callback->operator()(conn, CONN_RECV_END, nullptr, 0, nullptr);
560562
}
561563

562564
return prev;
@@ -718,9 +720,8 @@ void manapi::net::worker::TCP::flush_read_(const shared_conn &conn, connection_i
718720
}
719721
if (data->status & ev::READ
720722
&& !(data->status & (CONN_CLOSED|CONN_REMOVED))
721-
&& !data->watcher->is_active()) {
723+
&& data->watcher && !data->watcher->is_active())
722724
data->watcher->read_start();
723-
}
724725
}
725726

726727
void manapi::net::worker::TCP::update_limit_rate() {

src/worker/TLS.cpp

Lines changed: 40 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,13 @@ int manapi::net::worker::TLS::event_flags(const shared_conn & conn, int flags) n
292292
flush_read_ (conn, data);
293293

294294
if ((status & (CONN_READ|CONN_CLOSED|CONN_REMOVED)) == CONN_READ
295-
&& !data->watcher->is_active()) {
295+
&& data->watcher && !data->watcher->is_active()) {
296296
data->watcher->read_start();
297297
}
298298
}
299299

300300
if ((status & (CONN_READ|CONN_CLOSED|CONN_REMOVED)) == 0
301-
&& data->watcher->is_active()) {
301+
&& data->watcher && data->watcher->is_active()) {
302302
data->watcher->read_stop();
303303
}
304304

@@ -342,9 +342,8 @@ void manapi::net::worker::TLS::onrecv(std::shared_ptr<ev::tcp> &watcher, const s
342342
auto const data = conn->as<TLS::connection_interface>();
343343

344344
data->transfered += size;
345-
if (data->transfered >= this->config_->speed_limit_rate) {
345+
if (data->transfered >= this->config_->speed_limit_rate && data->watcher)
346346
data->watcher->read_stop();
347-
}
348347

349348
while (size) {
350349
auto rhs = this->ssl_bio_write_(data->rbio, buff, static_cast<int>(size));
@@ -395,7 +394,7 @@ void manapi::net::worker::TLS::onrecv(std::shared_ptr<ev::tcp> &watcher, const s
395394
}
396395
}
397396
else {
398-
if (auto const res = this->ssl_bio_flush_read_(conn, data->wbio, &data->top->recv, &data->top->recv_size, 1e5)) {
397+
if (auto const res = this->ssl_bio_flush_read_(conn, data, 1e5)) {
399398
if (res == CONN_IO_ERROR)
400399
goto err;
401400
}
@@ -462,7 +461,7 @@ void manapi::net::worker::TLS::onrecv(std::shared_ptr<ev::tcp> &watcher, const s
462461

463462

464463
int manapi::net::worker::TLS::check_read_stack_full_(connection_interface *data) {
465-
if (data->top->recv_size >= this->config_->max_buffer_stack) {
464+
if (data->top->recv_size >= this->config_->max_buffer_stack && data->watcher) {
466465
/* sadness */
467466
if (auto rhs = data->watcher->read_stop()) {
468467
return rhs;
@@ -473,8 +472,6 @@ int manapi::net::worker::TLS::check_read_stack_full_(connection_interface *data)
473472

474473
int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, TLS::connection_interface *m, int max_cnt) {
475474
int rhs;
476-
int flags = 0;
477-
buffer_deque *parent = nullptr;
478475
auto top = &m->top->send;
479476

480477
char fastfast[65536];
@@ -598,76 +595,52 @@ int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, TLS:
598595
return CONN_IO_ERROR;
599596
}
600597

601-
int manapi::net::worker::TLS::ssl_bio_flush_read_(const shared_conn &conn, void *rbio, connection_io_part *top, int *cnt, int max_cnt) {
598+
int manapi::net::worker::TLS::ssl_bio_flush_read_(const shared_conn &conn, TLS::connection_interface *m, int max_cnt) {
602599
int rhs;
603-
int err;
604-
int flags = 0;
605-
buffer_deque *parent = nullptr;
606-
auto data = conn->as<TLS::connection_interface>();
600+
auto top = &m->top->recv;
607601

608-
do {
609-
if (top->last_deque) {
610-
assert (top->last_deque->buffer.size() >= top->deque_cursor);
611-
}
612-
if (!top->last_deque || top->last_deque->buffer.size() == top->deque_cursor) {
613-
if (auto const res = ssl_flush_recv(conn, top, cnt)) {
614-
return res;
615-
}
602+
char fastfast[65536];
603+
ssize_t nfastfast = 0;
616604

617-
if (this->check_read_stack_full_(conn->as<TLS::connection_interface>())) {
618-
return CONN_IO_ERROR;
619-
}
605+
try {
606+
while (true) {
607+
if (max_cnt < m->top->recv_size)
608+
break;
620609

621-
if (cnt && *cnt >= max_cnt)
622-
return CONN_IO_WANT_READ;
610+
if (max_cnt < 10000)
611+
nfastfast = std::min<ssize_t>((max_cnt - m->top->recv_size + 1) * this->config_->buffer_size,
612+
(sizeof (fastfast)));
613+
else
614+
nfastfast = sizeof (fastfast);
623615

624-
auto buffer = this->bufferpool().buffer(1, this->config_->buffer_size);
616+
rhs = this->ssl_read_(m->ssl, fastfast,
617+
static_cast<int>(nfastfast));
625618

626-
auto obj = std::make_unique<buffer_deque>(std::move(buffer), nullptr);
627-
if (top->last_deque) {
628-
parent = top->last_deque;
629-
top->last_deque->next = std::move(obj);
630-
top->last_deque = top->last_deque->next.get();
619+
if (rhs > 0) {
620+
ssize_t alr = 0;
621+
if (!m->top->recv_size && (m->status & CONN_READ) && m->ev_callback) {
622+
m->ev_callback->operator()(conn, ev::READ, fastfast, rhs, nullptr);
623+
}
624+
else {
625+
TLS::connection_io_send(top, fastfast + alr, rhs - alr, &this->bufferpool(),
626+
this->config_->buffer_size, &m->top->recv_size, 1e5);
627+
this->flush_read_(conn, m);
628+
}
631629
}
632630
else {
633-
parent = nullptr;
634-
top->deque = std::move(obj);
635-
top->last_deque = top->deque.get();
636-
top->deque_current = 0;
637-
}
638-
639-
top->deque_cursor = 0;
640-
flags = 1 /* an empty buffer was created */;
641-
if (cnt)
642-
(*cnt)++;
643-
}
644-
else
645-
flags = 0;
646-
647-
rhs = this->ssl_read_(data->ssl, top->last_deque->buffer.data() + top->deque_cursor,
648-
static_cast<int>(top->last_deque->buffer.size() - top->deque_cursor));
649-
650-
if (rhs >= 0) {
651-
top->deque_cursor += rhs;
652-
if (!rhs && (flags /* an empty buffer was created */ )) {
653-
/* remove an empty buffer at the end */
654-
//connection_io_trim(top, parent, cnt);
655-
}
656-
}
657-
else {
658-
err = this->ssl_get_error_(data->ssl, rhs);
659-
if (err == this->ssl_error_ssl_ || err == this->ssl_error_syscall_) {
660-
return CONN_IO_ERROR;
631+
if (!this->ssl_bio_should_retry_(m->rbio)) {
632+
return CONN_IO_ERROR;
633+
}
634+
break;
661635
}
662636
}
637+
return CONN_IO_OK;
663638
}
664-
while (rhs > 0);
665-
666-
if (auto const res = ssl_flush_recv(conn, top, cnt)) {
667-
return res;
639+
catch (std::exception const &e) {
640+
manapi::async::current()->logger()->error(manapi::logger::default_service,
641+
manapi::ERR_INTERNAL, "TLS::ssl_bio_flush_read_(...): {}", e.what());
668642
}
669-
670-
return CONN_IO_OK;
643+
return CONN_IO_ERROR;
671644
}
672645

673646
int manapi::net::worker::TLS::ssl_flush_recv(const shared_conn &conn, connection_io_part *top, int *cnt) {
@@ -701,7 +674,7 @@ int manapi::net::worker::TLS::ssl_flush_recv(const shared_conn &conn, connection
701674
}
702675

703676
if (data->status & ((CONN_READ|CONN_CLOSED|CONN_REMOVED)) == CONN_READ
704-
&& !data->watcher->is_active()) {
677+
&& data->watcher && !data->watcher->is_active()) {
705678
data->watcher->read_start();
706679
}
707680

0 commit comments

Comments
 (0)