@@ -392,7 +392,7 @@ stream__handshake (evcom_stream *stream)
392
392
return OKAY ;
393
393
}
394
394
395
- evcom_stream_reset_timeout ( stream );
395
+ ev_timer_again ( D_LOOP_ ( stream ) & stream -> timeout_watcher );
396
396
397
397
if (r == GNUTLS_E_INTERRUPTED || r == GNUTLS_E_AGAIN ) {
398
398
if (0 == gnutls_record_get_direction ((stream )-> session )) {
@@ -411,11 +411,14 @@ stream__handshake (evcom_stream *stream)
411
411
stream -> flags |= EVCOM_CONNECTED ;
412
412
if (stream -> on_connect ) stream -> on_connect (stream );
413
413
414
- ev_io_start (D_LOOP_ (stream ) & stream -> read_watcher );
415
- ev_io_start (D_LOOP_ (stream ) & stream -> write_watcher );
414
+ /* evcom_stream_force_close might have been called. */
415
+ if (stream -> recvfd >= 0 && stream -> sendfd >= 0 ) {
416
+ ev_io_start (D_LOOP_ (stream ) & stream -> read_watcher );
417
+ ev_io_start (D_LOOP_ (stream ) & stream -> write_watcher );
416
418
417
- stream -> send_action = stream_send__data ;
418
- stream -> recv_action = stream_recv__data ;
419
+ stream -> send_action = stream_send__data ;
420
+ stream -> recv_action = stream_recv__data ;
421
+ }
419
422
420
423
return OKAY ;
421
424
}
@@ -541,7 +544,7 @@ stream_recv__data (evcom_stream *stream)
541
544
return OKAY ;
542
545
}
543
546
544
- evcom_stream_reset_timeout ( stream );
547
+ ev_timer_again ( D_LOOP_ ( stream ) & stream -> timeout_watcher );
545
548
546
549
assert (recved >= 0 );
547
550
@@ -614,7 +617,7 @@ stream_send__data (evcom_stream *stream)
614
617
return OKAY ;
615
618
}
616
619
617
- evcom_stream_reset_timeout ( stream );
620
+ ev_timer_again ( D_LOOP_ ( stream ) & stream -> timeout_watcher );
618
621
619
622
assert (sent >= 0 );
620
623
@@ -638,14 +641,24 @@ stream_send__shutdown (evcom_stream *stream)
638
641
int r = shutdown (stream -> sendfd , SHUT_WR );
639
642
640
643
if (r < 0 ) {
641
- stream -> errorno = errno ;
642
- evcom_perror ("shutdown()" , errno );
644
+ switch (errno ) {
645
+ case EINTR :
646
+ assert (stream -> send_action == stream_send__shutdown );
647
+ return OKAY ;
648
+
649
+ case ENOTCONN :
650
+ break ;
651
+
652
+ default :
653
+ stream -> errorno = errno ;
654
+ evcom_perror ("shutdown()" , errno );
655
+ break ;
656
+ }
643
657
stream -> send_action = stream_send__close ;
644
658
return OKAY ;
645
659
}
646
660
647
661
stream -> flags &= ~EVCOM_WRITABLE ;
648
-
649
662
stream -> send_action = stream_send__wait_for_eof ;
650
663
return OKAY ;
651
664
}
@@ -985,13 +998,15 @@ on_timeout (EV_P_ ev_timer *watcher, int revents)
985
998
assert (watcher == & stream -> timeout_watcher );
986
999
987
1000
if (PAUSED (stream )) {
988
- evcom_stream_reset_timeout ( stream );
1001
+ ev_timer_again ( D_LOOP_ ( stream ) & stream -> timeout_watcher );
989
1002
return ;
990
1003
}
991
1004
992
1005
if (stream -> on_timeout ) stream -> on_timeout (stream );
993
1006
994
1007
evcom_stream_force_close (stream );
1008
+
1009
+ if (stream -> on_close ) stream -> on_close (stream );
995
1010
}
996
1011
997
1012
static void
@@ -1045,7 +1060,7 @@ stream_event (EV_P_ ev_io *w, int revents)
1045
1060
* gnutls_db_set_ptr (stream->session, _);
1046
1061
*/
1047
1062
void
1048
- evcom_stream_init (evcom_stream * stream , float timeout )
1063
+ evcom_stream_init (evcom_stream * stream )
1049
1064
{
1050
1065
stream -> flags = 0 ;
1051
1066
stream -> errorno = 0 ;
@@ -1069,7 +1084,7 @@ evcom_stream_init (evcom_stream *stream, float timeout)
1069
1084
stream -> gnutls_errorno = 0 ;
1070
1085
stream -> session = NULL ;
1071
1086
#endif
1072
- ev_timer_init (& stream -> timeout_watcher , on_timeout , 0. , timeout );
1087
+ ev_timer_init (& stream -> timeout_watcher , on_timeout , 0. , 60. );
1073
1088
stream -> timeout_watcher .data = stream ;
1074
1089
1075
1090
stream -> on_connect = NULL ;
@@ -1098,8 +1113,8 @@ void evcom_stream_force_close (evcom_stream *stream)
1098
1113
1099
1114
if (!DUPLEX (stream ) && stream -> sendfd >= 0 ) {
1100
1115
close (stream -> sendfd );
1101
- stream__set_send_closed (stream );
1102
1116
}
1117
+ stream__set_send_closed (stream );
1103
1118
1104
1119
evcom_stream_detach (stream );
1105
1120
}
@@ -1175,9 +1190,12 @@ evcom_stream_write (evcom_stream *stream, const char *str, size_t len)
1175
1190
}
1176
1191
1177
1192
void
1178
- evcom_stream_reset_timeout (evcom_stream * stream )
1193
+ evcom_stream_reset_timeout (evcom_stream * stream , float timeout )
1179
1194
{
1180
- ev_timer_again (D_LOOP_ (stream ) & stream -> timeout_watcher );
1195
+ stream -> timeout_watcher .repeat = timeout ;
1196
+ if (ATTACHED (stream )) {
1197
+ ev_timer_again (D_LOOP_ (stream ) & stream -> timeout_watcher );
1198
+ }
1181
1199
}
1182
1200
1183
1201
void
@@ -1211,6 +1229,7 @@ void
1211
1229
evcom_stream_read_pause (evcom_stream * stream )
1212
1230
{
1213
1231
stream -> flags |= EVCOM_PAUSED ;
1232
+ ev_timer_stop (D_LOOP_ (stream ) & stream -> timeout_watcher );
1214
1233
if (stream -> recv_action == stream_recv__data ) {
1215
1234
ev_io_stop (D_LOOP_ (stream ) & stream -> read_watcher );
1216
1235
stream -> recv_action = stream_recv__wait_for_resume ;
@@ -1221,12 +1240,13 @@ void
1221
1240
evcom_stream_read_resume (evcom_stream * stream )
1222
1241
{
1223
1242
stream -> flags &= ~EVCOM_PAUSED ;
1224
- evcom_stream_reset_timeout ( stream );
1243
+ ev_timer_again ( D_LOOP_ ( stream ) & stream -> timeout_watcher );
1225
1244
if (stream -> recv_action == stream_recv__wait_for_resume ) {
1226
1245
stream -> recv_action = stream_recv__data ;
1227
1246
}
1228
- if (ATTACHED (stream ) && READABLE (stream )) {
1229
- ev_io_start (D_LOOP_ (stream ) & stream -> read_watcher );
1247
+ if (ATTACHED (stream )) {
1248
+ ev_timer_again (D_LOOP_ (stream ) & stream -> timeout_watcher );
1249
+ if (READABLE (stream )) ev_io_start (D_LOOP_ (stream ) & stream -> read_watcher );
1230
1250
}
1231
1251
}
1232
1252
0 commit comments