@@ -2456,7 +2456,8 @@ void janet_ev_recvfrom(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, in
2456
2456
typedef enum {
2457
2457
JANET_ASYNC_WRITEMODE_WRITE ,
2458
2458
JANET_ASYNC_WRITEMODE_SEND ,
2459
- JANET_ASYNC_WRITEMODE_SENDTO
2459
+ JANET_ASYNC_WRITEMODE_SENDTO ,
2460
+ JANET_ASYNC_WRITEMODE_CONNECT
2460
2461
} JanetWriteMode ;
2461
2462
2462
2463
typedef struct {
@@ -2480,6 +2481,31 @@ typedef struct {
2480
2481
#endif
2481
2482
} StateWrite ;
2482
2483
2484
+ static JanetAsyncStatus handle_connect (JanetListenerState * s ) {
2485
+ #ifdef JANET_WINDOWS
2486
+ int res = 0 ;
2487
+ int size = sizeof (res );
2488
+ int r = getsockopt ((SOCKET )s -> stream -> handle , SOL_SOCKET , SO_ERROR , (char * )& res , & size );
2489
+ #else
2490
+ int res = 0 ;
2491
+ socklen_t size = sizeof res ;
2492
+ int r = getsockopt (s -> stream -> handle , SOL_SOCKET , SO_ERROR , & res , & size );
2493
+ #endif
2494
+ if (r == 0 ) {
2495
+ if (res == 0 ) {
2496
+ janet_schedule (s -> fiber , janet_wrap_abstract (s -> stream ));
2497
+ } else {
2498
+ // TODO help needed. janet_stream_close(s->stream);
2499
+ janet_cancel (s -> fiber , janet_cstringv (strerror (res )));
2500
+ }
2501
+ } else {
2502
+ // TODO help needed. janet_stream_close(s->stream);
2503
+ janet_cancel (s -> fiber , janet_ev_lasterr ());
2504
+ }
2505
+ return JANET_ASYNC_STATUS_DONE ;
2506
+ }
2507
+
2508
+
2483
2509
JanetAsyncStatus ev_machine_write (JanetListenerState * s , JanetAsyncEvent event ) {
2484
2510
StateWrite * state = (StateWrite * ) s ;
2485
2511
switch (event ) {
@@ -2509,6 +2535,11 @@ JanetAsyncStatus ev_machine_write(JanetListenerState *s, JanetAsyncEvent event)
2509
2535
}
2510
2536
break ;
2511
2537
case JANET_ASYNC_EVENT_USER : {
2538
+ #ifdef JANET_NET
2539
+ if (state -> mode == JANET_ASYNC_WRITEMODE_CONNECT ) {
2540
+ return handle_connect (s );
2541
+ }
2542
+ #endif
2512
2543
/* Begin write */
2513
2544
int32_t len ;
2514
2545
const uint8_t * bytes ;
@@ -2572,6 +2603,11 @@ JanetAsyncStatus ev_machine_write(JanetListenerState *s, JanetAsyncEvent event)
2572
2603
janet_cancel (s -> fiber , janet_cstringv ("stream hup" ));
2573
2604
return JANET_ASYNC_STATUS_DONE ;
2574
2605
case JANET_ASYNC_EVENT_WRITE : {
2606
+ #ifdef JANET_NET
2607
+ if (state -> mode == JANET_ASYNC_WRITEMODE_CONNECT ) {
2608
+ return handle_connect (s );
2609
+ }
2610
+ #endif
2575
2611
int32_t start , len ;
2576
2612
const uint8_t * bytes ;
2577
2613
start = state -> start ;
@@ -2674,6 +2710,10 @@ void janet_ev_sendto_buffer(JanetStream *stream, JanetBuffer *buf, void *dest, i
2674
2710
void janet_ev_sendto_string (JanetStream * stream , JanetString str , void * dest , int flags ) {
2675
2711
janet_ev_write_generic (stream , (void * ) str , dest , JANET_ASYNC_WRITEMODE_SENDTO , 0 , flags );
2676
2712
}
2713
+
2714
+ void janet_ev_connect (JanetStream * stream , int flags ) {
2715
+ janet_ev_write_generic (stream , NULL , NULL , JANET_ASYNC_WRITEMODE_CONNECT , 0 , flags );
2716
+ }
2677
2717
#endif
2678
2718
2679
2719
/* For a pipe ID */
0 commit comments