From 117ae196fd7edb5cf8ab1faf51e952abff13bcb2 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 28 May 2020 19:14:35 -0500 Subject: [PATCH] Add net/flush. Useful for simple TCP protocols (netrepl), which benefit from being able to immediately send a message. --- src/core/ev.c | 7 +++++++ src/core/net.c | 25 ++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/core/ev.c b/src/core/ev.c index 896c1fe9..3aafa9fd 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -295,6 +295,7 @@ void janet_loop(void) { } } +#ifdef JANET_LINUX /* * Start linux/epoll implementation @@ -436,6 +437,12 @@ void janet_ev_deinit(void) { * End epoll implementation */ +#else + + +#endif + + /* C functions */ static Janet cfun_ev_spawn(int32_t argc, Janet *argv) { diff --git a/src/core/net.c b/src/core/net.c index b722445b..77cb67e9 100644 --- a/src/core/net.c +++ b/src/core/net.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include #include #endif @@ -75,7 +75,6 @@ typedef JanetPollable JanetStream; #define JEWOULDBLOCK WSAEWOULDBLOCK #define JEAGAIN WSAEWOULDBLOCK #define JPOLL WSAPoll -#define JPollStruct WSAPOLLFD #define JSock SOCKET #define JReadInt long #define JSOCKFLAGS 0 @@ -96,7 +95,6 @@ static JanetStream *make_stream(SOCKET fd, uint32_t flags) { #define JEWOULDBLOCK EWOULDBLOCK #define JEAGAIN EAGAIN #define JPOLL poll -#define JPollStruct struct pollfd #define JSock int #define JReadInt ssize_t #ifdef SOCK_CLOEXEC @@ -490,11 +488,26 @@ static Janet cfun_stream_write(int32_t argc, Janet *argv) { } } +static Janet cfun_stream_flush(int32_t argc, Janet *argv) { + janet_fixarity(argc, 2); + JanetStream *stream = janet_getabstract(argv, 0, &StreamAT); + if (!(stream->flags & JANET_STREAM_WRITABLE) || (stream->flags & JANET_POLL_FLAG_CLOSED)) { + janet_panic("got non writeable stream"); + } + /* Toggle no delay flag */ + int flag = 1; + setsockopt(stream->handle, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)); + flag = 0; + setsockopt(stream->handle, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)); + return argv[0]; +} + static const JanetMethod stream_methods[] = { {"chunk", cfun_stream_chunk}, {"close", cfun_stream_close}, {"read", cfun_stream_read}, {"write", cfun_stream_write}, + {"flush", cfun_stream_flush}, {NULL, NULL} }; @@ -530,6 +543,12 @@ static const JanetReg net_cfuns[] = { "Write data to a stream, suspending the current fiber until the write " "completes. Returns stream.") }, + { + "net/flush", cfun_stream_flush, + JDOC("(net/flush stream)\n\n" + "Make sure that a stream is not buffering any data. This temporarily disables Nagle's algorithm. " + "Use this to make sure data is sent without delay. Returns stream.") + }, { "net/close", cfun_stream_close, JDOC("(net/close stream)\n\n"