@@ -470,22 +470,56 @@ _mongoc_stream_tls_writev (mongoc_stream_t *stream,
470
470
mongoc_stream_tls_t * tls = (mongoc_stream_tls_t * )stream ;
471
471
ssize_t ret = 0 ;
472
472
size_t i ;
473
+ size_t iov_pos = 0 ;
473
474
int write_ret ;
474
475
476
+ int64_t now ;
477
+ int64_t expire = 0 ;
478
+
475
479
BSON_ASSERT (tls );
476
480
BSON_ASSERT (iov );
477
481
BSON_ASSERT (iovcnt );
478
482
479
483
tls -> timeout = timeout_msec ;
480
484
485
+ if (timeout_msec >= 0 ) {
486
+ expire = bson_get_monotonic_time () + (timeout_msec * 1000UL );
487
+ }
488
+
481
489
for (i = 0 ; i < iovcnt ; i ++ ) {
482
- write_ret = BIO_write ( tls -> bio , iov [ i ]. iov_base , ( int ) iov [ i ]. iov_len ) ;
490
+ iov_pos = 0 ;
483
491
484
- if (write_ret != iov [i ].iov_len ) {
485
- return write_ret ;
486
- }
492
+ while (iov_pos < iov [i ].iov_len ) {
493
+ write_ret = BIO_write (tls -> bio , (char * )iov [i ].iov_base + iov_pos ,
494
+ (int )(iov [i ].iov_len - iov_pos ));
495
+
496
+ if (write_ret < 0 ) {
497
+ return write_ret ;
498
+ }
499
+
500
+ if (expire ) {
501
+ now = bson_get_monotonic_time ();
502
+
503
+ if ((expire - now ) < 0 ) {
504
+ if (write_ret == 0 ) {
505
+ mongoc_counter_streams_timeout_inc ();
506
+ #ifdef _WIN32
507
+ errno = WSAETIMEDOUT ;
508
+ #else
509
+ errno = ETIMEDOUT ;
510
+ #endif
511
+ return -1 ;
512
+ }
487
513
488
- ret += write_ret ;
514
+ tls -> timeout = 0 ;
515
+ } else {
516
+ tls -> timeout = expire - now ;
517
+ }
518
+ }
519
+
520
+ ret += write_ret ;
521
+ iov_pos += write_ret ;
522
+ }
489
523
}
490
524
491
525
if (ret >= 0 ) {
@@ -527,48 +561,57 @@ _mongoc_stream_tls_readv (mongoc_stream_t *stream,
527
561
int read_ret ;
528
562
size_t iov_pos = 0 ;
529
563
int64_t now ;
530
- int64_t expire ;
564
+ int64_t expire = 0 ;
531
565
532
566
BSON_ASSERT (tls );
533
567
BSON_ASSERT (iov );
534
568
BSON_ASSERT (iovcnt );
535
569
536
570
tls -> timeout = timeout_msec ;
537
571
538
- expire = bson_get_monotonic_time () + (timeout_msec * 1000UL );
572
+ if (timeout_msec >= 0 ) {
573
+ expire = bson_get_monotonic_time () + (timeout_msec * 1000UL );
574
+ }
539
575
540
576
for (i = 0 ; i < iovcnt ; i ++ ) {
541
577
iov_pos = 0 ;
542
578
543
- while (iov_pos < iov [i ].iov_len - 1 ) {
579
+ while (iov_pos < iov [i ].iov_len ) {
544
580
read_ret = BIO_read (tls -> bio , (char * )iov [i ].iov_base + iov_pos ,
545
581
(int )(iov [i ].iov_len - iov_pos ));
546
582
547
- now = bson_get_monotonic_time ();
583
+ if (read_ret < 0 ) {
584
+ return read_ret ;
585
+ }
586
+
587
+ if (expire ) {
588
+ now = bson_get_monotonic_time ();
548
589
549
- if (((expire - now ) < 0 ) && (read_ret == 0 )) {
550
- mongoc_counter_streams_timeout_inc ();
590
+ if ((expire - now ) < 0 ) {
591
+ if (read_ret == 0 ) {
592
+ mongoc_counter_streams_timeout_inc ();
551
593
#ifdef _WIN32
552
- errno = WSAETIMEDOUT ;
594
+ errno = WSAETIMEDOUT ;
553
595
#else
554
- errno = ETIMEDOUT ;
596
+ errno = ETIMEDOUT ;
555
597
#endif
556
- return -1 ;
557
- }
598
+ return -1 ;
599
+ }
558
600
559
- if (read_ret == -1 ) {
560
- return read_ret ;
601
+ tls -> timeout = 0 ;
602
+ } else {
603
+ tls -> timeout = expire - now ;
604
+ }
561
605
}
562
606
563
607
ret += read_ret ;
564
608
565
- if (read_ret != iov [i ].iov_len ) {
566
- if ((size_t )read_ret >= min_bytes ) {
567
- return read_ret ;
568
- }
569
-
570
- iov_pos += read_ret ;
609
+ if ((size_t )ret >= min_bytes ) {
610
+ mongoc_counter_streams_ingress_add (ret );
611
+ return ret ;
571
612
}
613
+
614
+ iov_pos += read_ret ;
572
615
}
573
616
}
574
617
0 commit comments