2020-02-02 02:39:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Calvin Rose
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef JANET_AMALG
|
|
|
|
#include "features.h"
|
|
|
|
#include <janet.h>
|
|
|
|
#include "util.h"
|
|
|
|
#endif
|
|
|
|
|
2020-04-18 23:14:38 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#pragma comment (lib, "Ws2_32.lib")
|
|
|
|
#pragma comment (lib, "Mswsock.lib")
|
|
|
|
#pragma comment (lib, "Advapi32.lib")
|
|
|
|
#else
|
2020-02-03 15:29:51 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2020-04-18 17:12:27 +00:00
|
|
|
#include <poll.h>
|
2020-02-03 15:29:51 +00:00
|
|
|
#include <netdb.h>
|
2020-05-07 19:54:03 +00:00
|
|
|
#include <fcntl.h>
|
2020-04-18 23:14:38 +00:00
|
|
|
#endif
|
2020-02-03 15:29:51 +00:00
|
|
|
|
|
|
|
/*
|
2020-02-11 14:57:44 +00:00
|
|
|
* Streams
|
2020-02-03 15:29:51 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
#define JANET_STREAM_CLOSED 1
|
2020-02-11 14:57:44 +00:00
|
|
|
#define JANET_STREAM_READABLE 2
|
|
|
|
#define JANET_STREAM_WRITABLE 4
|
2020-02-10 01:04:34 +00:00
|
|
|
|
|
|
|
static int janet_stream_close(void *p, size_t s);
|
2020-02-12 15:32:41 +00:00
|
|
|
static int janet_stream_getter(void *p, Janet key, Janet *out);
|
2020-02-10 01:04:34 +00:00
|
|
|
static const JanetAbstractType StreamAT = {
|
|
|
|
"core/stream",
|
|
|
|
janet_stream_close,
|
2020-02-12 15:32:41 +00:00
|
|
|
NULL,
|
|
|
|
janet_stream_getter,
|
|
|
|
JANET_ATEND_GET
|
2020-02-10 01:04:34 +00:00
|
|
|
};
|
|
|
|
|
2020-04-18 23:14:38 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2020-04-30 02:07:21 +00:00
|
|
|
typedef struct {
|
|
|
|
SOCKET fd;
|
|
|
|
int flags;
|
|
|
|
} JanetStream;
|
|
|
|
#define JSOCKCLOSE(x) closesocket(x)
|
|
|
|
#define JSOCKDEFAULT INVALID_SOCKET
|
2020-04-30 18:26:14 +00:00
|
|
|
#define JLASTERR WSAGetLastError()
|
2020-04-30 02:07:21 +00:00
|
|
|
#define JSOCKVALID(x) ((x) != INVALID_SOCKET)
|
|
|
|
#define JEINTR WSAEINTR
|
|
|
|
#define JEWOULDBLOCK WSAEWOULDBLOCK
|
|
|
|
#define JEAGAIN WSAEWOULDBLOCK
|
|
|
|
#define JPOLL WSAPoll
|
|
|
|
#define JPollStruct WSAPOLLFD
|
|
|
|
#define JSock SOCKET
|
|
|
|
#define JReadInt long
|
2020-05-06 23:33:25 +00:00
|
|
|
#define JSOCKFLAGS 0
|
2020-04-30 02:07:21 +00:00
|
|
|
static JanetStream *make_stream(SOCKET fd, int flags) {
|
2020-04-18 23:14:38 +00:00
|
|
|
u_long iMode = 0;
|
|
|
|
JanetStream *stream = janet_abstract(&StreamAT, sizeof(JanetStream));
|
2020-04-30 02:07:21 +00:00
|
|
|
ioctlsocket(fd, FIONBIO, &iMode);
|
|
|
|
stream->fd = fd;
|
2020-04-18 23:14:38 +00:00
|
|
|
stream->flags = flags;
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
#else
|
2020-04-30 02:07:21 +00:00
|
|
|
typedef struct {
|
|
|
|
int fd;
|
|
|
|
int flags;
|
|
|
|
} JanetStream;
|
|
|
|
#define JSOCKCLOSE(x) close(x)
|
|
|
|
#define JSOCKDEFAULT 0
|
|
|
|
#define JLASTERR errno
|
|
|
|
#define JSOCKVALID(x) ((x) >= 0)
|
|
|
|
#define JEINTR EINTR
|
|
|
|
#define JEWOULDBLOCK EWOULDBLOCK
|
|
|
|
#define JEAGAIN EAGAIN
|
|
|
|
#define JPOLL poll
|
|
|
|
#define JPollStruct struct pollfd
|
|
|
|
#define JSock int
|
|
|
|
#define JReadInt ssize_t
|
2020-05-07 04:44:01 +00:00
|
|
|
#ifdef SOCK_CLOEXEC
|
2020-05-06 23:33:25 +00:00
|
|
|
#define JSOCKFLAGS SOCK_CLOEXEC
|
2020-05-07 04:44:01 +00:00
|
|
|
#else
|
|
|
|
#define JSOCKFLAGS 0
|
|
|
|
#endif
|
2020-02-11 14:57:44 +00:00
|
|
|
static JanetStream *make_stream(int fd, int flags) {
|
2020-02-10 01:04:34 +00:00
|
|
|
JanetStream *stream = janet_abstract(&StreamAT, sizeof(JanetStream));
|
2020-05-07 04:44:01 +00:00
|
|
|
#ifndef SOCK_CLOEXEC
|
|
|
|
int extra = O_CLOEXEC;
|
|
|
|
#else
|
|
|
|
int extra = 0;
|
|
|
|
#endif
|
|
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK | extra);
|
2020-02-10 01:04:34 +00:00
|
|
|
stream->fd = fd;
|
2020-02-11 14:57:44 +00:00
|
|
|
stream->flags = flags;
|
2020-02-10 01:04:34 +00:00
|
|
|
return stream;
|
|
|
|
}
|
2020-04-18 23:14:38 +00:00
|
|
|
#endif
|
2020-02-10 01:04:34 +00:00
|
|
|
|
2020-04-30 02:07:21 +00:00
|
|
|
/* We pass this flag to all send calls to prevent sigpipe */
|
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int janet_stream_close(void *p, size_t s) {
|
|
|
|
(void) s;
|
|
|
|
JanetStream *stream = p;
|
|
|
|
if (!(stream->flags & JANET_STREAM_CLOSED)) {
|
|
|
|
stream->flags |= JANET_STREAM_CLOSED;
|
|
|
|
JSOCKCLOSE(stream->fd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:57:44 +00:00
|
|
|
/*
|
|
|
|
* Event loop
|
|
|
|
*/
|
|
|
|
|
2020-02-03 15:29:51 +00:00
|
|
|
/* This large struct describes a waiting file descriptor, as well
|
2020-02-10 01:04:34 +00:00
|
|
|
* as what to do when we get an event for it. It is a variant type, where
|
|
|
|
* each variant implements a simple state machine. */
|
2020-02-03 15:29:51 +00:00
|
|
|
typedef struct {
|
|
|
|
|
|
|
|
/* File descriptor to listen for events on. */
|
2020-02-11 14:57:44 +00:00
|
|
|
JanetStream *stream;
|
2020-02-03 15:29:51 +00:00
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
/* Fiber to resume when event finishes. Can be NULL, in which case,
|
|
|
|
* no fiber is resumed when event completes. */
|
2020-02-03 15:29:51 +00:00
|
|
|
JanetFiber *fiber;
|
|
|
|
|
|
|
|
/* What kind of event we are listening for.
|
|
|
|
* As more IO functionality get's added, we can
|
|
|
|
* expand this. */
|
|
|
|
enum {
|
2020-02-10 01:04:34 +00:00
|
|
|
JLE_READ_CHUNK,
|
|
|
|
JLE_READ_SOME,
|
2020-02-03 15:29:51 +00:00
|
|
|
JLE_READ_ACCEPT,
|
2020-02-10 01:04:34 +00:00
|
|
|
JLE_CONNECT,
|
2020-02-03 15:29:51 +00:00
|
|
|
JLE_WRITE_FROM_BUFFER,
|
|
|
|
JLE_WRITE_FROM_STRINGLIKE
|
|
|
|
} event_type;
|
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
/* Each variant can have a different payload. */
|
2020-02-03 15:29:51 +00:00
|
|
|
union {
|
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
/* JLE_READ_CHUNK/JLE_READ_SOME */
|
2020-02-03 15:29:51 +00:00
|
|
|
struct {
|
2020-02-10 01:04:34 +00:00
|
|
|
int32_t bytes_left;
|
2020-02-03 15:29:51 +00:00
|
|
|
JanetBuffer *buf;
|
2020-02-10 01:04:34 +00:00
|
|
|
} read_chunk;
|
2020-02-03 15:29:51 +00:00
|
|
|
|
|
|
|
/* JLE_READ_ACCEPT */
|
|
|
|
struct {
|
|
|
|
JanetFunction *handler;
|
|
|
|
} read_accept;
|
|
|
|
|
|
|
|
/* JLE_WRITE_FROM_BUFFER */
|
|
|
|
struct {
|
|
|
|
JanetBuffer *buf;
|
2020-02-10 01:04:34 +00:00
|
|
|
int32_t start;
|
2020-02-03 15:29:51 +00:00
|
|
|
} write_from_buffer;
|
|
|
|
|
|
|
|
/* JLE_WRITE_FROM_STRINGLIKE */
|
|
|
|
struct {
|
|
|
|
const uint8_t *str;
|
2020-02-10 01:04:34 +00:00
|
|
|
int32_t start;
|
2020-02-03 15:29:51 +00:00
|
|
|
} write_from_stringlike;
|
2020-02-10 01:04:34 +00:00
|
|
|
|
2020-02-03 15:29:51 +00:00
|
|
|
} data;
|
|
|
|
|
|
|
|
} JanetLoopFD;
|
|
|
|
|
|
|
|
#define JANET_LOOPFD_MAX 1024
|
|
|
|
|
|
|
|
/* Global loop data */
|
2020-04-30 02:07:21 +00:00
|
|
|
JANET_THREAD_LOCAL JPollStruct janet_vm_pollfds[JANET_LOOPFD_MAX];
|
2020-02-03 15:29:51 +00:00
|
|
|
JANET_THREAD_LOCAL JanetLoopFD janet_vm_loopfds[JANET_LOOPFD_MAX];
|
|
|
|
JANET_THREAD_LOCAL int janet_vm_loop_count;
|
|
|
|
|
|
|
|
/* We could also add/remove gc roots. This is easier for now. */
|
|
|
|
void janet_net_markloop(void) {
|
|
|
|
for (int i = 0; i < janet_vm_loop_count; i++) {
|
|
|
|
JanetLoopFD lfd = janet_vm_loopfds[i];
|
2020-02-21 01:54:31 +00:00
|
|
|
if (lfd.fiber != NULL) {
|
|
|
|
janet_mark(janet_wrap_fiber(lfd.fiber));
|
|
|
|
}
|
2020-02-11 14:57:44 +00:00
|
|
|
janet_mark(janet_wrap_abstract(lfd.stream));
|
2020-02-03 15:29:51 +00:00
|
|
|
switch (lfd.event_type) {
|
|
|
|
default:
|
|
|
|
break;
|
2020-02-10 01:04:34 +00:00
|
|
|
case JLE_READ_CHUNK:
|
|
|
|
case JLE_READ_SOME:
|
|
|
|
janet_mark(janet_wrap_buffer(lfd.data.read_chunk.buf));
|
2020-02-03 15:29:51 +00:00
|
|
|
break;
|
|
|
|
case JLE_READ_ACCEPT:
|
|
|
|
janet_mark(janet_wrap_function(lfd.data.read_accept.handler));
|
|
|
|
break;
|
2020-02-10 01:04:34 +00:00
|
|
|
case JLE_CONNECT:
|
|
|
|
break;
|
2020-02-03 15:29:51 +00:00
|
|
|
case JLE_WRITE_FROM_BUFFER:
|
|
|
|
janet_mark(janet_wrap_buffer(lfd.data.write_from_buffer.buf));
|
|
|
|
break;
|
|
|
|
case JLE_WRITE_FROM_STRINGLIKE:
|
2020-02-10 01:04:34 +00:00
|
|
|
janet_mark(janet_wrap_string(lfd.data.write_from_stringlike.str));
|
2020-02-03 15:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a loop fd to the global event loop */
|
2020-04-18 17:12:27 +00:00
|
|
|
static int janet_loop_schedule(JanetLoopFD lfd, short events) {
|
2020-02-03 15:29:51 +00:00
|
|
|
if (janet_vm_loop_count == JANET_LOOPFD_MAX) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-04-18 17:12:27 +00:00
|
|
|
int index = janet_vm_loop_count++;
|
|
|
|
janet_vm_loopfds[index] = lfd;
|
|
|
|
janet_vm_pollfds[index].fd = lfd.stream->fd;
|
|
|
|
janet_vm_pollfds[index].events = events;
|
|
|
|
janet_vm_pollfds[index].revents = 0;
|
2020-02-03 15:29:51 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2020-04-18 17:12:27 +00:00
|
|
|
/* Remove event from list */
|
|
|
|
static void janet_loop_rmindex(int index) {
|
|
|
|
janet_vm_loopfds[index] = janet_vm_loopfds[--janet_vm_loop_count];
|
|
|
|
janet_vm_pollfds[index] = janet_vm_pollfds[janet_vm_loop_count];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-03 15:29:51 +00:00
|
|
|
/* Return delta in number of loop fds. Abstracted out so
|
|
|
|
* we can separate out the polling logic */
|
|
|
|
static size_t janet_loop_event(size_t index) {
|
|
|
|
JanetLoopFD *jlfd = janet_vm_loopfds + index;
|
2020-02-11 14:57:44 +00:00
|
|
|
JanetStream *stream = jlfd->stream;
|
2020-04-30 02:07:21 +00:00
|
|
|
JSock fd = stream->fd;
|
2020-02-03 15:29:51 +00:00
|
|
|
int ret = 1;
|
|
|
|
int should_resume = 0;
|
|
|
|
Janet resumeval = janet_wrap_nil();
|
2020-02-11 14:57:44 +00:00
|
|
|
if (stream->flags & JANET_STREAM_CLOSED) {
|
|
|
|
should_resume = 1;
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
switch (jlfd->event_type) {
|
|
|
|
case JLE_READ_CHUNK:
|
|
|
|
case JLE_READ_SOME: {
|
|
|
|
JanetBuffer *buffer = jlfd->data.read_chunk.buf;
|
|
|
|
int32_t bytes_left = jlfd->data.read_chunk.bytes_left;
|
|
|
|
janet_buffer_extra(buffer, bytes_left);
|
|
|
|
if (!(stream->flags & JANET_STREAM_READABLE)) {
|
|
|
|
should_resume = 1;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-30 02:07:21 +00:00
|
|
|
JReadInt nread;
|
2020-02-11 14:57:44 +00:00
|
|
|
do {
|
2020-04-30 02:07:21 +00:00
|
|
|
nread = recv(fd, buffer->data + buffer->count, bytes_left, 0);
|
|
|
|
} while (nread == -1 && JLASTERR == JEINTR);
|
|
|
|
if (JLASTERR == JEAGAIN || JLASTERR == JEWOULDBLOCK) {
|
2020-02-11 14:57:44 +00:00
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-30 02:07:21 +00:00
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
if (nread > 0) {
|
2020-02-11 14:57:44 +00:00
|
|
|
buffer->count += nread;
|
|
|
|
bytes_left -= nread;
|
|
|
|
} else {
|
|
|
|
bytes_left = 0;
|
2020-02-03 15:29:51 +00:00
|
|
|
}
|
2020-02-11 14:57:44 +00:00
|
|
|
if (jlfd->event_type == JLE_READ_SOME || bytes_left == 0) {
|
|
|
|
should_resume = 1;
|
|
|
|
if (nread > 0) {
|
|
|
|
resumeval = janet_wrap_buffer(buffer);
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
jlfd->data.read_chunk.bytes_left = bytes_left;
|
|
|
|
ret = 1;
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
2020-02-11 14:57:44 +00:00
|
|
|
break;
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
2020-02-11 14:57:44 +00:00
|
|
|
case JLE_READ_ACCEPT: {
|
2020-04-30 02:07:21 +00:00
|
|
|
JSock connfd = accept(fd, NULL, NULL);
|
|
|
|
if (JSOCKVALID(connfd)) {
|
2020-02-11 14:57:44 +00:00
|
|
|
/* Made a new connection socket */
|
|
|
|
JanetStream *stream = make_stream(connfd, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE);
|
|
|
|
Janet streamv = janet_wrap_abstract(stream);
|
|
|
|
JanetFunction *handler = jlfd->data.read_accept.handler;
|
|
|
|
Janet out;
|
|
|
|
JanetFiber *fiberp = NULL;
|
|
|
|
/* Launch connection fiber */
|
|
|
|
JanetSignal sig = janet_pcall(handler, 1, &streamv, &out, &fiberp);
|
2020-04-17 21:27:02 +00:00
|
|
|
if (sig != JANET_SIGNAL_OK && sig != JANET_SIGNAL_EVENT) {
|
2020-02-11 14:57:44 +00:00
|
|
|
janet_stacktrace(fiberp, out);
|
|
|
|
}
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
2020-02-11 14:57:44 +00:00
|
|
|
ret = JANET_LOOPFD_MAX;
|
|
|
|
break;
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
2020-02-11 14:57:44 +00:00
|
|
|
case JLE_WRITE_FROM_BUFFER:
|
|
|
|
case JLE_WRITE_FROM_STRINGLIKE: {
|
|
|
|
int32_t start, len;
|
|
|
|
const uint8_t *bytes;
|
|
|
|
if (!(stream->flags & JANET_STREAM_WRITABLE)) {
|
|
|
|
should_resume = 1;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2020-02-10 01:04:34 +00:00
|
|
|
if (jlfd->event_type == JLE_WRITE_FROM_BUFFER) {
|
2020-02-11 14:57:44 +00:00
|
|
|
JanetBuffer *buffer = jlfd->data.write_from_buffer.buf;
|
|
|
|
bytes = buffer->data;
|
|
|
|
len = buffer->count;
|
|
|
|
start = jlfd->data.write_from_buffer.start;
|
2020-02-10 01:04:34 +00:00
|
|
|
} else {
|
2020-02-11 14:57:44 +00:00
|
|
|
bytes = jlfd->data.write_from_stringlike.str;
|
|
|
|
len = janet_string_length(bytes);
|
|
|
|
start = jlfd->data.write_from_stringlike.start;
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
2020-02-11 14:57:44 +00:00
|
|
|
if (start < len) {
|
|
|
|
int32_t nbytes = len - start;
|
2020-04-30 02:07:21 +00:00
|
|
|
JReadInt nwrote;
|
2020-02-11 14:57:44 +00:00
|
|
|
do {
|
2020-04-30 02:07:21 +00:00
|
|
|
nwrote = send(fd, bytes + start, nbytes, MSG_NOSIGNAL);
|
|
|
|
} while (nwrote == -1 && JLASTERR == JEINTR);
|
2020-02-11 14:57:44 +00:00
|
|
|
if (nwrote > 0) {
|
|
|
|
start += nwrote;
|
|
|
|
} else {
|
|
|
|
start = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (start >= len) {
|
|
|
|
should_resume = 1;
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
if (jlfd->event_type == JLE_WRITE_FROM_BUFFER) {
|
|
|
|
jlfd->data.write_from_buffer.start = start;
|
|
|
|
} else {
|
|
|
|
jlfd->data.write_from_stringlike.start = start;
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
break;
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
2020-02-11 14:57:44 +00:00
|
|
|
case JLE_CONNECT: {
|
|
|
|
break;
|
|
|
|
}
|
2020-04-30 02:07:21 +00:00
|
|
|
}
|
2020-02-03 15:29:51 +00:00
|
|
|
}
|
2020-02-10 01:04:34 +00:00
|
|
|
|
|
|
|
/* Resume a fiber for some events */
|
2020-02-03 15:29:51 +00:00
|
|
|
if (NULL != jlfd->fiber && should_resume) {
|
|
|
|
/* Resume the fiber */
|
|
|
|
Janet out;
|
2020-02-10 01:04:34 +00:00
|
|
|
JanetSignal sig = janet_continue(jlfd->fiber, resumeval, &out);
|
2020-04-17 21:27:02 +00:00
|
|
|
if (sig != JANET_SIGNAL_OK && sig != JANET_SIGNAL_EVENT) {
|
2020-02-10 01:04:34 +00:00
|
|
|
janet_stacktrace(jlfd->fiber, out);
|
|
|
|
}
|
2020-02-03 15:29:51 +00:00
|
|
|
}
|
2020-02-10 01:04:34 +00:00
|
|
|
|
|
|
|
/* Remove this handler from the handler pool. */
|
2020-04-18 23:14:38 +00:00
|
|
|
if (should_resume) janet_loop_rmindex((int) index);
|
2020-02-10 01:04:34 +00:00
|
|
|
|
2020-02-03 15:29:51 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
static void janet_loop1(void) {
|
2020-04-18 17:12:27 +00:00
|
|
|
/* Remove closed file descriptors */
|
|
|
|
for (int i = 0; i < janet_vm_loop_count;) {
|
|
|
|
if (janet_vm_loopfds[i].stream->flags & JANET_STREAM_CLOSED) {
|
|
|
|
janet_loop_rmindex(i);
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
2020-02-03 15:29:51 +00:00
|
|
|
}
|
2020-04-18 17:12:27 +00:00
|
|
|
/* Poll */
|
|
|
|
if (janet_vm_loop_count == 0) return;
|
|
|
|
int ready;
|
|
|
|
do {
|
2020-04-30 02:07:21 +00:00
|
|
|
ready = JPOLL(janet_vm_pollfds, janet_vm_loop_count, -1);
|
|
|
|
} while (ready == -1 && JLASTERR == JEINTR);
|
2020-04-18 17:12:27 +00:00
|
|
|
if (ready == -1) return;
|
|
|
|
/* Handle events */
|
2020-02-03 15:29:51 +00:00
|
|
|
for (int i = 0; i < janet_vm_loop_count;) {
|
2020-04-18 23:14:38 +00:00
|
|
|
int revents = janet_vm_pollfds[i].revents;
|
|
|
|
janet_vm_pollfds[i].revents = 0;
|
2020-04-18 23:15:59 +00:00
|
|
|
if ((janet_vm_pollfds[i].events | POLLHUP | POLLERR) & revents) {
|
2020-02-03 15:29:51 +00:00
|
|
|
size_t delta = janet_loop_event(i);
|
2020-04-18 23:14:38 +00:00
|
|
|
i += (int) delta;
|
2020-02-03 15:29:51 +00:00
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_loop(void) {
|
2020-02-10 01:04:34 +00:00
|
|
|
while (janet_vm_loop_count) {
|
|
|
|
janet_loop1();
|
|
|
|
}
|
2020-02-03 15:29:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 02:39:54 +00:00
|
|
|
/*
|
2020-02-10 01:04:34 +00:00
|
|
|
* Scheduling Helpers
|
2020-02-02 02:39:54 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
#define JANET_SCHED_FSOME 1
|
2020-02-03 15:29:51 +00:00
|
|
|
|
2020-02-11 14:57:44 +00:00
|
|
|
JANET_NO_RETURN static void janet_sched_read(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, int flags) {
|
2020-05-10 21:00:55 +00:00
|
|
|
JanetLoopFD lfd;
|
2020-02-11 14:57:44 +00:00
|
|
|
lfd.stream = stream;
|
2020-02-10 01:04:34 +00:00
|
|
|
lfd.fiber = janet_root_fiber();
|
|
|
|
lfd.event_type = (flags & JANET_SCHED_FSOME) ? JLE_READ_SOME : JLE_READ_CHUNK;
|
|
|
|
lfd.data.read_chunk.buf = buf;
|
|
|
|
lfd.data.read_chunk.bytes_left = nbytes;
|
2020-04-18 17:12:27 +00:00
|
|
|
janet_loop_schedule(lfd, POLLIN);
|
2020-04-17 21:27:02 +00:00
|
|
|
janet_signalv(JANET_SIGNAL_EVENT, janet_wrap_nil());
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 14:57:44 +00:00
|
|
|
JANET_NO_RETURN static void janet_sched_write_buffer(JanetStream *stream, JanetBuffer *buf) {
|
2020-05-10 21:00:55 +00:00
|
|
|
JanetLoopFD lfd;
|
2020-02-11 14:57:44 +00:00
|
|
|
lfd.stream = stream;
|
2020-02-10 01:04:34 +00:00
|
|
|
lfd.fiber = janet_root_fiber();
|
|
|
|
lfd.event_type = JLE_WRITE_FROM_BUFFER;
|
|
|
|
lfd.data.write_from_buffer.buf = buf;
|
|
|
|
lfd.data.write_from_buffer.start = 0;
|
2020-04-18 17:12:27 +00:00
|
|
|
janet_loop_schedule(lfd, POLLOUT);
|
2020-04-17 21:27:02 +00:00
|
|
|
janet_signalv(JANET_SIGNAL_EVENT, janet_wrap_nil());
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
2020-02-03 15:29:51 +00:00
|
|
|
|
2020-02-11 14:57:44 +00:00
|
|
|
JANET_NO_RETURN static void janet_sched_write_stringlike(JanetStream *stream, const uint8_t *str) {
|
2020-05-10 21:00:55 +00:00
|
|
|
JanetLoopFD lfd;
|
2020-02-11 14:57:44 +00:00
|
|
|
lfd.stream = stream;
|
2020-02-10 01:04:34 +00:00
|
|
|
lfd.fiber = janet_root_fiber();
|
|
|
|
lfd.event_type = JLE_WRITE_FROM_STRINGLIKE;
|
|
|
|
lfd.data.write_from_stringlike.str = str;
|
|
|
|
lfd.data.write_from_stringlike.start = 0;
|
2020-04-18 17:12:27 +00:00
|
|
|
janet_loop_schedule(lfd, POLLOUT);
|
2020-04-17 21:27:02 +00:00
|
|
|
janet_signalv(JANET_SIGNAL_EVENT, janet_wrap_nil());
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Needs argc >= offset + 2 */
|
|
|
|
static struct addrinfo *janet_get_addrinfo(Janet *argv, int32_t offset) {
|
|
|
|
/* Get host and port */
|
|
|
|
const char *host = janet_getcstring(argv, offset);
|
2020-05-01 04:21:26 +00:00
|
|
|
const char *port;
|
|
|
|
if (janet_checkint(argv[offset + 1])) {
|
|
|
|
port = (const char *)janet_to_string(argv[offset + 1]);
|
|
|
|
} else {
|
|
|
|
port = janet_getcstring(argv, offset + 1);
|
|
|
|
}
|
2020-02-03 15:29:51 +00:00
|
|
|
/* getaddrinfo */
|
2020-02-10 01:04:34 +00:00
|
|
|
struct addrinfo *ai = NULL;
|
2020-05-10 21:00:55 +00:00
|
|
|
struct addrinfo hints;
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
2020-02-03 15:29:51 +00:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = 0;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
int status = getaddrinfo(host, port, &hints, &ai);
|
|
|
|
if (status) {
|
|
|
|
janet_panicf("could not get address info: %s", gai_strerror(status));
|
|
|
|
}
|
2020-02-10 01:04:34 +00:00
|
|
|
return ai;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* C Funs
|
|
|
|
*/
|
|
|
|
|
|
|
|
static Janet cfun_net_connect(int32_t argc, Janet *argv) {
|
2020-02-21 02:10:03 +00:00
|
|
|
janet_fixarity(argc, 2);
|
2020-02-10 01:04:34 +00:00
|
|
|
|
|
|
|
struct addrinfo *ai = janet_get_addrinfo(argv, 0);
|
|
|
|
|
2020-04-18 23:14:38 +00:00
|
|
|
/* Create socket */
|
2020-05-07 12:55:08 +00:00
|
|
|
JSock sock = socket(ai->ai_family, ai->ai_socktype | JSOCKFLAGS, ai->ai_protocol);
|
2020-04-30 02:07:21 +00:00
|
|
|
if (!JSOCKVALID(sock)) {
|
2020-04-18 23:14:38 +00:00
|
|
|
freeaddrinfo(ai);
|
|
|
|
janet_panic("could not create socket");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect to socket */
|
|
|
|
int status = connect(sock, ai->ai_addr, (int) ai->ai_addrlen);
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
if (status == -1) {
|
2020-04-30 02:07:21 +00:00
|
|
|
JSOCKCLOSE(sock);
|
2020-02-10 01:04:34 +00:00
|
|
|
janet_panic("could not connect to socket");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrap socket in abstract type JanetStream */
|
2020-02-11 14:57:44 +00:00
|
|
|
JanetStream *stream = make_stream(sock, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE);
|
2020-02-10 01:04:34 +00:00
|
|
|
return janet_wrap_abstract(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Janet cfun_net_server(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 3);
|
|
|
|
|
|
|
|
/* Get host, port, and handler*/
|
|
|
|
JanetFunction *fun = janet_getfunction(argv, 2);
|
|
|
|
|
|
|
|
struct addrinfo *ai = janet_get_addrinfo(argv, 0);
|
2020-02-03 15:29:51 +00:00
|
|
|
|
2020-04-18 23:14:38 +00:00
|
|
|
/* Check all addrinfos in a loop for the first that we can bind to. */
|
2020-04-30 02:07:21 +00:00
|
|
|
JSock sfd = JSOCKDEFAULT;
|
2020-04-18 23:14:38 +00:00
|
|
|
struct addrinfo *rp = NULL;
|
|
|
|
for (rp = ai; rp != NULL; rp = rp->ai_next) {
|
2020-05-07 12:55:08 +00:00
|
|
|
sfd = socket(rp->ai_family, rp->ai_socktype | JSOCKFLAGS, rp->ai_protocol);
|
2020-04-30 02:07:21 +00:00
|
|
|
if (!JSOCKVALID(sfd)) continue;
|
2020-04-18 23:14:38 +00:00
|
|
|
/* Set various socket options */
|
|
|
|
int enable = 1;
|
|
|
|
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char *) &enable, sizeof(int)) < 0) {
|
2020-04-30 02:07:21 +00:00
|
|
|
JSOCKCLOSE(sfd);
|
2020-04-18 23:14:38 +00:00
|
|
|
janet_panic("setsockopt(SO_REUSEADDR) failed");
|
|
|
|
}
|
2020-04-30 02:07:21 +00:00
|
|
|
#ifdef SO_NOSIGPIPE
|
|
|
|
if (setsockopt(sfd, SOL_SOCKET, SO_NOSIGPIPE, &enable, sizeof(int)) < 0) {
|
|
|
|
JSOCKCLOSE(sfd);
|
|
|
|
janet_panic("setsockopt(SO_NOSIGPIPE) failed");
|
2020-04-18 17:12:27 +00:00
|
|
|
}
|
2020-04-30 02:07:21 +00:00
|
|
|
#endif
|
2020-04-18 17:12:27 +00:00
|
|
|
#ifdef SO_REUSEPORT
|
|
|
|
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0) {
|
2020-04-30 02:07:21 +00:00
|
|
|
JSOCKCLOSE(sfd);
|
2020-04-18 17:12:27 +00:00
|
|
|
janet_panic("setsockopt(SO_REUSEPORT) failed");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Bind */
|
2020-04-30 02:07:21 +00:00
|
|
|
if (bind(sfd, rp->ai_addr, (int) rp->ai_addrlen) == 0) break;
|
|
|
|
JSOCKCLOSE(sfd);
|
2020-02-03 15:29:51 +00:00
|
|
|
}
|
|
|
|
if (NULL == rp) {
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
janet_panic("could not bind to any sockets");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* listen */
|
2020-02-10 01:04:34 +00:00
|
|
|
int status = listen(sfd, 1024);
|
|
|
|
freeaddrinfo(ai);
|
2020-02-03 15:29:51 +00:00
|
|
|
if (status) {
|
2020-04-30 02:07:21 +00:00
|
|
|
JSOCKCLOSE(sfd);
|
2020-02-03 15:29:51 +00:00
|
|
|
janet_panic("could not listen on file descriptor");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put sfd on our loop */
|
|
|
|
JanetLoopFD lfd = {0};
|
2020-02-11 14:57:44 +00:00
|
|
|
lfd.stream = make_stream(sfd, 0);
|
2020-02-03 15:29:51 +00:00
|
|
|
lfd.event_type = JLE_READ_ACCEPT;
|
|
|
|
lfd.data.read_accept.handler = fun;
|
2020-04-18 17:12:27 +00:00
|
|
|
janet_loop_schedule(lfd, POLLIN);
|
2020-02-03 15:29:51 +00:00
|
|
|
|
2020-02-11 14:57:44 +00:00
|
|
|
return janet_wrap_abstract(lfd.stream);
|
2020-02-03 15:29:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
static Janet cfun_stream_read(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 2, 3);
|
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &StreamAT);
|
|
|
|
int32_t n = janet_getnat(argv, 1);
|
|
|
|
JanetBuffer *buffer = janet_optbuffer(argv, argc, 2, 10);
|
2020-02-11 14:57:44 +00:00
|
|
|
janet_sched_read(stream, buffer, n, JANET_SCHED_FSOME);
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Janet cfun_stream_chunk(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 2, 3);
|
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &StreamAT);
|
|
|
|
int32_t n = janet_getnat(argv, 1);
|
|
|
|
JanetBuffer *buffer = janet_optbuffer(argv, argc, 2, 10);
|
2020-02-11 14:57:44 +00:00
|
|
|
janet_sched_read(stream, buffer, n, 0);
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Janet cfun_stream_close(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &StreamAT);
|
|
|
|
janet_stream_close(stream, 0);
|
2020-02-02 02:39:54 +00:00
|
|
|
return janet_wrap_nil();
|
|
|
|
}
|
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
static Janet cfun_stream_write(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 2);
|
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &StreamAT);
|
|
|
|
if (janet_checktype(argv[1], JANET_BUFFER)) {
|
2020-02-11 14:57:44 +00:00
|
|
|
janet_sched_write_buffer(stream, janet_getbuffer(argv, 1));
|
2020-02-10 01:04:34 +00:00
|
|
|
} else {
|
|
|
|
JanetByteView bytes = janet_getbytes(argv, 1);
|
2020-02-11 14:57:44 +00:00
|
|
|
janet_sched_write_stringlike(stream, bytes.bytes);
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 15:32:41 +00:00
|
|
|
static const JanetMethod stream_methods[] = {
|
|
|
|
{"chunk", cfun_stream_chunk},
|
|
|
|
{"close", cfun_stream_close},
|
|
|
|
{"read", cfun_stream_read},
|
|
|
|
{"write", cfun_stream_write},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int janet_stream_getter(void *p, Janet key, Janet *out) {
|
|
|
|
(void) p;
|
|
|
|
if (!janet_checktype(key, JANET_KEYWORD)) return 0;
|
|
|
|
return janet_getmethod(janet_unwrap_keyword(key), stream_methods, out);
|
|
|
|
}
|
|
|
|
|
2020-02-02 02:39:54 +00:00
|
|
|
static const JanetReg net_cfuns[] = {
|
2020-02-10 01:04:34 +00:00
|
|
|
{
|
|
|
|
"net/server", cfun_net_server,
|
2020-04-26 19:11:47 +00:00
|
|
|
JDOC("(net/server host port handler)\n\n"
|
|
|
|
"Start a TCP server. handler is a function that will be called with a stream "
|
|
|
|
"on each connection to the server. Returns a new stream that is neither readable nor "
|
|
|
|
"writeable.")
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"net/read", cfun_stream_read,
|
|
|
|
JDOC("(net/read stream nbytes &opt buf)\n\n"
|
2020-04-28 00:25:28 +00:00
|
|
|
"Read up to n bytes from a stream, suspending the current fiber until the bytes are available. "
|
|
|
|
"If less than n bytes are available (and more than 0), will push those bytes and return early. "
|
|
|
|
"Returns a buffer with up to n more bytes in it.")
|
2020-04-26 19:11:47 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"net/chunk", cfun_stream_chunk,
|
|
|
|
JDOC("(net/chunk stream nbytes &opt buf)\n\n"
|
2020-04-28 00:25:28 +00:00
|
|
|
"Same a net/read, but will wait for all n bytes to arrive rather than return early.")
|
2020-04-26 19:11:47 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"net/write", cfun_stream_write,
|
|
|
|
JDOC("(net/write stream data)\n\n"
|
|
|
|
"Write data to a stream, suspending the current fiber until the write "
|
|
|
|
"completes. Returns stream.")
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"net/close", cfun_stream_close,
|
|
|
|
JDOC("(net/close stream)\n\n"
|
|
|
|
"Close a stream so that no further communication can occur.")
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"net/connect", cfun_net_connect,
|
|
|
|
JDOC("(net/connect host port)\n\n"
|
|
|
|
"Open a connection to communicate with a server. Returns a duplex stream "
|
|
|
|
"that can be used to communicate with the server.")
|
2020-02-10 01:04:34 +00:00
|
|
|
},
|
2020-02-02 02:39:54 +00:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
void janet_lib_net(JanetTable *env) {
|
2020-02-03 15:29:51 +00:00
|
|
|
janet_vm_loop_count = 0;
|
2020-04-18 23:14:38 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
WSADATA wsaData;
|
|
|
|
janet_assert(!WSAStartup(MAKEWORD(2, 2), &wsaData), "could not start winsock");
|
|
|
|
#endif
|
2020-02-02 02:39:54 +00:00
|
|
|
janet_core_cfuns(env, NULL, net_cfuns);
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:14:38 +00:00
|
|
|
void janet_net_deinit(void) {
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
}
|