@@ -292,13 +292,13 @@ int manapi::net::worker::TLS::event_flags(const shared_conn & conn, int flags) n
292
292
flush_read_ (conn, data);
293
293
294
294
if ((status & (CONN_READ|CONN_CLOSED|CONN_REMOVED)) == CONN_READ
295
- && !data->watcher ->is_active ()) {
295
+ && data-> watcher && !data->watcher ->is_active ()) {
296
296
data->watcher ->read_start ();
297
297
}
298
298
}
299
299
300
300
if ((status & (CONN_READ|CONN_CLOSED|CONN_REMOVED)) == 0
301
- && data->watcher ->is_active ()) {
301
+ && data->watcher && data-> watcher ->is_active ()) {
302
302
data->watcher ->read_stop ();
303
303
}
304
304
@@ -342,9 +342,8 @@ void manapi::net::worker::TLS::onrecv(std::shared_ptr<ev::tcp> &watcher, const s
342
342
auto const data = conn->as <TLS::connection_interface>();
343
343
344
344
data->transfered += size;
345
- if (data->transfered >= this ->config_ ->speed_limit_rate ) {
345
+ if (data->transfered >= this ->config_ ->speed_limit_rate && data-> watcher )
346
346
data->watcher ->read_stop ();
347
- }
348
347
349
348
while (size) {
350
349
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
395
394
}
396
395
}
397
396
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 )) {
399
398
if (res == CONN_IO_ERROR)
400
399
goto err;
401
400
}
@@ -462,7 +461,7 @@ void manapi::net::worker::TLS::onrecv(std::shared_ptr<ev::tcp> &watcher, const s
462
461
463
462
464
463
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 ) {
466
465
/* sadness */
467
466
if (auto rhs = data->watcher ->read_stop ()) {
468
467
return rhs;
@@ -473,8 +472,6 @@ int manapi::net::worker::TLS::check_read_stack_full_(connection_interface *data)
473
472
474
473
int manapi::net::worker::TLS::ssl_bio_flush_write_ (const shared_conn &conn, TLS::connection_interface *m, int max_cnt) {
475
474
int rhs;
476
- int flags = 0 ;
477
- buffer_deque *parent = nullptr ;
478
475
auto top = &m->top ->send ;
479
476
480
477
char fastfast[65536 ];
@@ -598,76 +595,52 @@ int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, TLS:
598
595
return CONN_IO_ERROR;
599
596
}
600
597
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) {
602
599
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 ;
607
601
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 ;
616
604
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 ;
620
609
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);
623
615
624
- auto buffer = this ->bufferpool ().buffer (1 , this ->config_ ->buffer_size );
616
+ rhs = this ->ssl_read_ (m->ssl , fastfast,
617
+ static_cast <int >(nfastfast));
625
618
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
+ }
631
629
}
632
630
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 ;
661
635
}
662
636
}
637
+ return CONN_IO_OK;
663
638
}
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 ());
668
642
}
669
-
670
- return CONN_IO_OK;
643
+ return CONN_IO_ERROR;
671
644
}
672
645
673
646
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
701
674
}
702
675
703
676
if (data->status & ((CONN_READ|CONN_CLOSED|CONN_REMOVED)) == CONN_READ
704
- && !data->watcher ->is_active ()) {
677
+ && data-> watcher && !data->watcher ->is_active ()) {
705
678
data->watcher ->read_start ();
706
679
}
707
680
0 commit comments