From 471b6f996666e0494520c5b6bf3f0f6df8447753 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 24 Sep 2023 12:28:35 -0700 Subject: [PATCH] Add TOCLOSE back. --- src/core/ev.c | 13 ++++++++++++- src/core/net.c | 18 +++++------------- src/include/janet.h | 1 + 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/core/ev.c b/src/core/ev.c index a0b77aad..f1989722 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -326,6 +326,12 @@ static void janet_unlisten_impl(JanetListenerState *state, int is_gc) { janet_free(state); } +static void janet_stream_checktoclose(JanetStream *stream) { + if ((stream->flags & JANET_STREAM_TOCLOSE) && !stream->state) { + janet_stream_close(stream); + } +} + static const JanetMethod ev_default_stream_methods[] = { {"close", janet_cfun_stream_close}, {"read", janet_cfun_stream_read}, @@ -1554,6 +1560,7 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp to) { state = state->_next; } } + janet_stream_checktoclose(stream); } } } @@ -1708,6 +1715,7 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) { janet_unlisten(state, 0); state = next_state; } + janet_stream_checktoclose(stream); } } } @@ -1906,6 +1914,7 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) { state = next_state; } + janet_stream_checktoclose(stream); } } } @@ -2021,8 +2030,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) { if (status1 == JANET_ASYNC_STATUS_DONE || status2 == JANET_ASYNC_STATUS_DONE || status3 == JANET_ASYNC_STATUS_DONE || - status4 == JANET_ASYNC_STATUS_DONE) + status4 == JANET_ASYNC_STATUS_DONE) { janet_unlisten(state, 0); + } + janet_stream_checktoclose(stream); } } diff --git a/src/core/net.c b/src/core/net.c index 0b97b496..2bce5b73 100644 --- a/src/core/net.c +++ b/src/core/net.c @@ -123,32 +123,22 @@ JanetAsyncStatus net_machine_connect(JanetListenerState *s, JanetAsyncEvent even switch (event) { default: return JANET_ASYNC_STATUS_NOT_DONE; - case JANET_ASYNC_EVENT_DEINIT: - { - if (!state->did_connect) { - janet_stream_close(s->stream); - return JANET_ASYNC_STATUS_DONE; - } - } - return JANET_ASYNC_STATUS_DONE; - case JANET_ASYNC_EVENT_CLOSE: case JANET_ASYNC_EVENT_HUP: case JANET_ASYNC_EVENT_ERR: - janet_cancel(s->fiber, janet_cstringv("failed to connect socket")); - return JANET_ASYNC_STATUS_DONE; case JANET_ASYNC_EVENT_COMPLETE: case JANET_ASYNC_EVENT_WRITE: case JANET_ASYNC_EVENT_USER: break; } + JanetStream *stream = s->stream; #ifdef JANET_WINDOWS int res = 0; int size = sizeof(res); - int r = getsockopt((SOCKET)s->stream->handle, SOL_SOCKET, SO_ERROR, (char *)&res, &size); + int r = getsockopt((SOCKET)stream->handle, SOL_SOCKET, SO_ERROR, (char *)&res, &size); #else int res = 0; socklen_t size = sizeof res; - int r = getsockopt(s->stream->handle, SOL_SOCKET, SO_ERROR, &res, &size); + int r = getsockopt(stream->handle, SOL_SOCKET, SO_ERROR, &res, &size); #endif if (r == 0) { if (res == 0) { @@ -156,9 +146,11 @@ JanetAsyncStatus net_machine_connect(JanetListenerState *s, JanetAsyncEvent even janet_schedule(s->fiber, janet_wrap_abstract(s->stream)); } else { janet_cancel(s->fiber, janet_cstringv(strerror(res))); + stream->flags |= JANET_STREAM_TOCLOSE; } } else { janet_cancel(s->fiber, janet_ev_lasterr()); + stream->flags |= JANET_STREAM_TOCLOSE; } return JANET_ASYNC_STATUS_DONE; } diff --git a/src/include/janet.h b/src/include/janet.h index 72a7adda..d1e5daa6 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -580,6 +580,7 @@ typedef void *JanetAbstract; #define JANET_STREAM_WRITABLE 0x400 #define JANET_STREAM_ACCEPTABLE 0x800 #define JANET_STREAM_UDPSERVER 0x1000 +#define JANET_STREAM_TOCLOSE 0x10000 typedef enum { JANET_ASYNC_EVENT_INIT,