@@ -1502,6 +1502,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp to) {
1502
1502
state = state -> _next ;
1503
1503
}
1504
1504
}
1505
+ /* Close the stream if requested and no more listeners are left */
1506
+ if ((stream -> flags & JANET_STREAM_TOCLOSE ) && !stream -> state ) {
1507
+ janet_stream_close (stream );
1508
+ }
1505
1509
}
1506
1510
}
1507
1511
}
@@ -1656,6 +1660,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
1656
1660
janet_unlisten (state , 0 );
1657
1661
state = next_state ;
1658
1662
}
1663
+ /* Close the stream if requested and no more listeners are left */
1664
+ if ((stream -> flags & JANET_STREAM_TOCLOSE ) && !stream -> state ) {
1665
+ janet_stream_close (stream );
1666
+ }
1659
1667
}
1660
1668
}
1661
1669
}
@@ -1854,6 +1862,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
1854
1862
1855
1863
state = next_state ;
1856
1864
}
1865
+ /* Close the stream if requested and no more listeners are left */
1866
+ if ((stream -> flags & JANET_STREAM_TOCLOSE ) && !stream -> state ) {
1867
+ janet_stream_close (stream );
1868
+ }
1857
1869
}
1858
1870
}
1859
1871
}
@@ -1957,6 +1969,7 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
1957
1969
JanetAsyncStatus status3 = JANET_ASYNC_STATUS_NOT_DONE ;
1958
1970
JanetAsyncStatus status4 = JANET_ASYNC_STATUS_NOT_DONE ;
1959
1971
state -> event = pfd ;
1972
+ JanetStream * stream = state -> stream ;
1960
1973
if (mask & POLLOUT )
1961
1974
status1 = state -> machine (state , JANET_ASYNC_EVENT_WRITE );
1962
1975
if (mask & POLLIN )
@@ -1970,6 +1983,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
1970
1983
status3 == JANET_ASYNC_STATUS_DONE ||
1971
1984
status4 == JANET_ASYNC_STATUS_DONE )
1972
1985
janet_unlisten (state , 0 );
1986
+ /* Close the stream if requested and no more listeners are left */
1987
+ if ((stream -> flags & JANET_STREAM_TOCLOSE ) && !stream -> state ) {
1988
+ janet_stream_close (stream );
1989
+ }
1973
1990
}
1974
1991
}
1975
1992
@@ -2456,7 +2473,8 @@ void janet_ev_recvfrom(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, in
2456
2473
typedef enum {
2457
2474
JANET_ASYNC_WRITEMODE_WRITE ,
2458
2475
JANET_ASYNC_WRITEMODE_SEND ,
2459
- JANET_ASYNC_WRITEMODE_SENDTO
2476
+ JANET_ASYNC_WRITEMODE_SENDTO ,
2477
+ JANET_ASYNC_WRITEMODE_CONNECT
2460
2478
} JanetWriteMode ;
2461
2479
2462
2480
typedef struct {
@@ -2480,6 +2498,31 @@ typedef struct {
2480
2498
#endif
2481
2499
} StateWrite ;
2482
2500
2501
+ static JanetAsyncStatus handle_connect (JanetListenerState * s ) {
2502
+ #ifdef JANET_WINDOWS
2503
+ int res = 0 ;
2504
+ int size = sizeof (res );
2505
+ int r = getsockopt ((SOCKET )s -> stream -> handle , SOL_SOCKET , SO_ERROR , (char * )& res , & size );
2506
+ #else
2507
+ int res = 0 ;
2508
+ socklen_t size = sizeof res ;
2509
+ int r = getsockopt (s -> stream -> handle , SOL_SOCKET , SO_ERROR , & res , & size );
2510
+ #endif
2511
+ if (r == 0 ) {
2512
+ if (res == 0 ) {
2513
+ janet_schedule (s -> fiber , janet_wrap_abstract (s -> stream ));
2514
+ } else {
2515
+ s -> stream -> flags |= JANET_STREAM_TOCLOSE ;
2516
+ janet_cancel (s -> fiber , janet_cstringv (strerror (res )));
2517
+ }
2518
+ } else {
2519
+ s -> stream -> flags |= JANET_STREAM_TOCLOSE ;
2520
+ janet_cancel (s -> fiber , janet_ev_lasterr ());
2521
+ }
2522
+ return JANET_ASYNC_STATUS_DONE ;
2523
+ }
2524
+
2525
+
2483
2526
JanetAsyncStatus ev_machine_write (JanetListenerState * s , JanetAsyncEvent event ) {
2484
2527
StateWrite * state = (StateWrite * ) s ;
2485
2528
switch (event ) {
@@ -2509,6 +2552,11 @@ JanetAsyncStatus ev_machine_write(JanetListenerState *s, JanetAsyncEvent event)
2509
2552
}
2510
2553
break ;
2511
2554
case JANET_ASYNC_EVENT_USER : {
2555
+ #ifdef JANET_NET
2556
+ if (state -> mode == JANET_ASYNC_WRITEMODE_CONNECT ) {
2557
+ return handle_connect (s );
2558
+ }
2559
+ #endif
2512
2560
/* Begin write */
2513
2561
int32_t len ;
2514
2562
const uint8_t * bytes ;
@@ -2572,6 +2620,11 @@ JanetAsyncStatus ev_machine_write(JanetListenerState *s, JanetAsyncEvent event)
2572
2620
janet_cancel (s -> fiber , janet_cstringv ("stream hup" ));
2573
2621
return JANET_ASYNC_STATUS_DONE ;
2574
2622
case JANET_ASYNC_EVENT_WRITE : {
2623
+ #ifdef JANET_NET
2624
+ if (state -> mode == JANET_ASYNC_WRITEMODE_CONNECT ) {
2625
+ return handle_connect (s );
2626
+ }
2627
+ #endif
2575
2628
int32_t start , len ;
2576
2629
const uint8_t * bytes ;
2577
2630
start = state -> start ;
@@ -2674,6 +2727,10 @@ void janet_ev_sendto_buffer(JanetStream *stream, JanetBuffer *buf, void *dest, i
2674
2727
void janet_ev_sendto_string (JanetStream * stream , JanetString str , void * dest , int flags ) {
2675
2728
janet_ev_write_generic (stream , (void * ) str , dest , JANET_ASYNC_WRITEMODE_SENDTO , 0 , flags );
2676
2729
}
2730
+
2731
+ void janet_ev_connect (JanetStream * stream , int flags ) {
2732
+ janet_ev_write_generic (stream , NULL , NULL , JANET_ASYNC_WRITEMODE_CONNECT , 0 , flags );
2733
+ }
2677
2734
#endif
2678
2735
2679
2736
/* For a pipe ID */
0 commit comments