mirror of
https://github.com/janet-lang/janet
synced 2024-12-11 01:10:25 +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:
parent
5f70a85f7e
commit
f553c5da47
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
(defn handler
|
(defn handler
|
||||||
[connection]
|
[connection]
|
||||||
(print "created " connection)
|
(print "connection: " connection)
|
||||||
(net/write connection "Whats your name?\n")
|
(net/write connection "Whats your name?\n")
|
||||||
(def name (string/trim (string (ev/read connection 100))))
|
(def name (string/trim (string (ev/read connection 100))))
|
||||||
(print name " connected")
|
(print name " connected")
|
||||||
@ -23,13 +23,14 @@
|
|||||||
(put conmap name nil)
|
(put conmap name nil)
|
||||||
(:close connection))
|
(:close connection))
|
||||||
(while (def msg (ev/read connection 100))
|
(while (def msg (ev/read connection 100))
|
||||||
(broadcast name (string msg)))
|
(broadcast name (string msg)))
|
||||||
(print name " disconnected")))))
|
(print name " disconnected")))))
|
||||||
|
|
||||||
(defn main [& args]
|
(defn main [& args]
|
||||||
(printf "STARTING SERVER...")
|
(printf "STARTING SERVER...")
|
||||||
(flush)
|
(flush)
|
||||||
(def my-server (net/listen "127.0.0.1" "8000"))
|
(def my-server (net/listen "127.0.0.1" "8000"))
|
||||||
|
'(handler (net/accept my-server))
|
||||||
(forever
|
(forever
|
||||||
(def connection (net/accept my-server))
|
(def connection (net/accept my-server))
|
||||||
(ev/call handler connection)))
|
(ev/call handler connection)))
|
||||||
|
@ -2277,6 +2277,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
|
|||||||
break;
|
break;
|
||||||
case JANET_ASYNC_EVENT_CLOSE:
|
case JANET_ASYNC_EVENT_CLOSE:
|
||||||
janet_schedule(fiber, janet_wrap_nil());
|
janet_schedule(fiber, janet_wrap_nil());
|
||||||
|
stream->read_fiber = NULL;
|
||||||
janet_async_end(fiber);
|
janet_async_end(fiber);
|
||||||
break;
|
break;
|
||||||
#ifdef JANET_WINDOWS
|
#ifdef JANET_WINDOWS
|
||||||
@ -2361,6 +2362,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
|
|||||||
} else {
|
} else {
|
||||||
janet_schedule(fiber, janet_wrap_nil());
|
janet_schedule(fiber, janet_wrap_nil());
|
||||||
}
|
}
|
||||||
|
stream->read_fiber = NULL;
|
||||||
janet_async_end(fiber);
|
janet_async_end(fiber);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -2402,6 +2404,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
|
|||||||
nread = 0;
|
nread = 0;
|
||||||
} else {
|
} else {
|
||||||
janet_cancel(fiber, janet_ev_lasterr());
|
janet_cancel(fiber, janet_ev_lasterr());
|
||||||
|
stream->read_fiber = NULL;
|
||||||
janet_async_end(fiber);
|
janet_async_end(fiber);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -2411,6 +2414,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
|
|||||||
state->bytes_read += nread;
|
state->bytes_read += nread;
|
||||||
if (state->bytes_read == 0 && (state->mode != JANET_ASYNC_READMODE_RECVFROM)) {
|
if (state->bytes_read == 0 && (state->mode != JANET_ASYNC_READMODE_RECVFROM)) {
|
||||||
janet_schedule(fiber, janet_wrap_nil());
|
janet_schedule(fiber, janet_wrap_nil());
|
||||||
|
stream->read_fiber = NULL;
|
||||||
janet_async_end(fiber);
|
janet_async_end(fiber);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -2434,6 +2438,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
|
|||||||
resume_val = janet_wrap_buffer(buffer);
|
resume_val = janet_wrap_buffer(buffer);
|
||||||
}
|
}
|
||||||
janet_schedule(fiber, resume_val);
|
janet_schedule(fiber, resume_val);
|
||||||
|
stream->read_fiber = NULL;
|
||||||
janet_async_end(fiber);
|
janet_async_end(fiber);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -2526,6 +2531,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
|
|||||||
}
|
}
|
||||||
case JANET_ASYNC_EVENT_CLOSE:
|
case JANET_ASYNC_EVENT_CLOSE:
|
||||||
janet_cancel(fiber, janet_cstringv("stream closed"));
|
janet_cancel(fiber, janet_cstringv("stream closed"));
|
||||||
|
stream->write_fiber = NULL;
|
||||||
janet_async_end(fiber);
|
janet_async_end(fiber);
|
||||||
break;
|
break;
|
||||||
#ifdef JANET_WINDOWS
|
#ifdef JANET_WINDOWS
|
||||||
@ -2612,10 +2618,12 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
|
|||||||
#else
|
#else
|
||||||
case JANET_ASYNC_EVENT_ERR:
|
case JANET_ASYNC_EVENT_ERR:
|
||||||
janet_cancel(fiber, janet_cstringv("stream err"));
|
janet_cancel(fiber, janet_cstringv("stream err"));
|
||||||
|
stream->write_fiber = NULL;
|
||||||
janet_async_end(fiber);
|
janet_async_end(fiber);
|
||||||
break;
|
break;
|
||||||
case JANET_ASYNC_EVENT_HUP:
|
case JANET_ASYNC_EVENT_HUP:
|
||||||
janet_cancel(fiber, janet_cstringv("stream hup"));
|
janet_cancel(fiber, janet_cstringv("stream hup"));
|
||||||
|
stream->write_fiber = NULL;
|
||||||
janet_async_end(fiber);
|
janet_async_end(fiber);
|
||||||
break;
|
break;
|
||||||
case JANET_ASYNC_EVENT_INIT:
|
case JANET_ASYNC_EVENT_INIT:
|
||||||
@ -2653,6 +2661,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
|
|||||||
if (nwrote == -1) {
|
if (nwrote == -1) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
|
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
|
||||||
janet_cancel(fiber, janet_ev_lasterr());
|
janet_cancel(fiber, janet_ev_lasterr());
|
||||||
|
stream->write_fiber = NULL;
|
||||||
janet_async_end(fiber);
|
janet_async_end(fiber);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -2660,6 +2669,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
|
|||||||
/* Unless using datagrams, empty message is a disconnect */
|
/* Unless using datagrams, empty message is a disconnect */
|
||||||
if (nwrote == 0 && !dest_abst) {
|
if (nwrote == 0 && !dest_abst) {
|
||||||
janet_cancel(fiber, janet_cstringv("disconnect"));
|
janet_cancel(fiber, janet_cstringv("disconnect"));
|
||||||
|
stream->write_fiber = NULL;
|
||||||
janet_async_end(fiber);
|
janet_async_end(fiber);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -2673,6 +2683,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
|
|||||||
state->start = start;
|
state->start = start;
|
||||||
if (start >= len) {
|
if (start >= len) {
|
||||||
janet_schedule(fiber, janet_wrap_nil());
|
janet_schedule(fiber, janet_wrap_nil());
|
||||||
|
stream->write_fiber = NULL;
|
||||||
janet_async_end(fiber);
|
janet_async_end(fiber);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user