1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-02 21:09:55 +00:00

Update ev.c with workaround for failing chat server.

2 issues:
- With poll backend, we were polling for writes even after we finished
  writing. Presents as wasting a lot of CPU.
- Fixes  strange closing behavior of chat server.
This commit is contained in:
Calvin Rose 2024-09-06 00:00:09 -05:00
parent 5f70a85f7e
commit f553c5da47
2 changed files with 14 additions and 2 deletions

View File

@ -8,7 +8,7 @@
(defn handler
[connection]
(print "created " connection)
(print "connection: " connection)
(net/write connection "Whats your name?\n")
(def name (string/trim (string (ev/read connection 100))))
(print name " connected")
@ -23,13 +23,14 @@
(put conmap name nil)
(:close connection))
(while (def msg (ev/read connection 100))
(broadcast name (string msg)))
(broadcast name (string msg)))
(print name " disconnected")))))
(defn main [& args]
(printf "STARTING SERVER...")
(flush)
(def my-server (net/listen "127.0.0.1" "8000"))
'(handler (net/accept my-server))
(forever
(def connection (net/accept my-server))
(ev/call handler connection)))

View File

@ -2277,6 +2277,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
break;
case JANET_ASYNC_EVENT_CLOSE:
janet_schedule(fiber, janet_wrap_nil());
stream->read_fiber = NULL;
janet_async_end(fiber);
break;
#ifdef JANET_WINDOWS
@ -2361,6 +2362,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
} else {
janet_schedule(fiber, janet_wrap_nil());
}
stream->read_fiber = NULL;
janet_async_end(fiber);
break;
}
@ -2402,6 +2404,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
nread = 0;
} else {
janet_cancel(fiber, janet_ev_lasterr());
stream->read_fiber = NULL;
janet_async_end(fiber);
break;
}
@ -2411,6 +2414,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
state->bytes_read += nread;
if (state->bytes_read == 0 && (state->mode != JANET_ASYNC_READMODE_RECVFROM)) {
janet_schedule(fiber, janet_wrap_nil());
stream->read_fiber = NULL;
janet_async_end(fiber);
break;
}
@ -2434,6 +2438,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
resume_val = janet_wrap_buffer(buffer);
}
janet_schedule(fiber, resume_val);
stream->read_fiber = NULL;
janet_async_end(fiber);
break;
}
@ -2526,6 +2531,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
}
case JANET_ASYNC_EVENT_CLOSE:
janet_cancel(fiber, janet_cstringv("stream closed"));
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
#ifdef JANET_WINDOWS
@ -2612,10 +2618,12 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
#else
case JANET_ASYNC_EVENT_ERR:
janet_cancel(fiber, janet_cstringv("stream err"));
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
case JANET_ASYNC_EVENT_HUP:
janet_cancel(fiber, janet_cstringv("stream hup"));
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
case JANET_ASYNC_EVENT_INIT:
@ -2653,6 +2661,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
if (nwrote == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
janet_cancel(fiber, janet_ev_lasterr());
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
}
@ -2660,6 +2669,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
/* Unless using datagrams, empty message is a disconnect */
if (nwrote == 0 && !dest_abst) {
janet_cancel(fiber, janet_cstringv("disconnect"));
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
}
@ -2673,6 +2683,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
state->start = start;
if (start >= len) {
janet_schedule(fiber, janet_wrap_nil());
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
}