1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-17 22:24:49 +00:00

Add TOCLOSE back.

This commit is contained in:
Calvin Rose 2023-09-24 12:28:35 -07:00
parent 5dd18bac2c
commit 471b6f9966
3 changed files with 18 additions and 14 deletions

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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,