Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,26 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "NetB

list(APPEND EVENT_LOOP_DEFINES "KQUEUE")
set(USE_S2N ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
option(USE_VSOCK
"Build in support for VSOCK sockets"
OFF)

file(GLOB AWS_IO_OS_HEADERS
)

file(GLOB AWS_IO_OS_SRC
"source/emscripten/*.c"
"source/linux/*.c"
"source/posix/*.c"
)

set(EPOLL_SHIM_INCLUDE "source/emscripten")

set(PLATFORM_LIBS "")

set(EVENT_LOOP_DEFINE "EPOLL")
set(USE_S2N ON)
endif()

if (BYO_CRYPTO)
Expand Down Expand Up @@ -215,6 +234,7 @@ endif()
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_include_directories(${PROJECT_NAME} PRIVATE ${EPOLL_SHIM_INCLUDE})

aws_use_package(aws-c-common)
aws_use_package(aws-c-cal)
Expand Down
35 changes: 35 additions & 0 deletions source/emscripten/epoll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <errno.h>
#include <limits.h>
#include <up.h>

#include <sys/epoll.h>

static upoll_t* ups[OPEN_MAX];
static int index = 1;

int
epoll_create(int size)
{
if (index >= OPEN_MAX) {
errno = ENFILE;
return -1;
}
ups[index++] = upoll_create(size);
return index - 1;
}

int
epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
{
upoll_t* upq = ups[epfd];
upoll_event_t* uevent = (upoll_event_t*) event;
return upoll_ctl(upq, op, fd, uevent);
}

int
epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
{
upoll_t* upq = ups[epfd];
upoll_event_t* uevents = (upoll_event_t*) events;
return upoll_wait(upq, uevents, maxevents, timeout);
}
73 changes: 73 additions & 0 deletions source/emscripten/sys/epoll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* epoll.h
Copyright (c) fd0, All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library.*/

#ifndef _SYS_EPOLL_H
#define _SYS_EPOLL_H 1

#include <stdint.h>
#include <sys/types.h>

#ifndef OPEN_MAX
#define OPEN_MAX 256
#endif

#ifndef __EPOLL_PACKED
# define __EPOLL_PACKED __attribute__ ((__packed__))
#endif

#define EPOLLIN 0x01
#define EPOLLOUT 0x02
#define EPOLLERR 0x04
#define EPOLLET 0x08
#define EPOLLHUP 0x010

/* Valid opcodes ( "op" parameter ) to issue to epoll_ctl(). */
#define EPOLL_CTL_ADD 1 /* Add a file descriptor to the interface. */
#define EPOLL_CTL_DEL 2 /* Remove a file descriptor from the interface. */
#define EPOLL_CTL_MOD 3 /* Change file descriptor epoll_event structure. */

typedef union epoll_data
{
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;

struct epoll_event
{
uint32_t events; /* Epoll events */
epoll_data_t data; /* User data variable */
};

#ifdef __cplusplus
extern "C" {
#endif

int
epoll_create (int size);

int
epoll_ctl (int epfd, int op, int fd, struct epoll_event *event);

int
epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout);

#ifdef __cplusplus
}
#endif

#endif /* sys/epoll.h */
48 changes: 48 additions & 0 deletions source/emscripten/up.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef _UP_H_
#define _UP_H_

#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>

#define UPOLL_CTL_ADD 1
#define UPOLL_CTL_DEL 2
#define UPOLL_CTL_MOD 3

#define UPOLLIN 0x01
#define UPOLLOUT 0x02
#define UPOLLERR 0x04
#define UPOLLET 0x08

typedef struct upoll upoll_t;

typedef union upoll_data {
void *ptr;
intptr_t fd;
uint32_t u32;
uint64_t u64;
} upoll_data_t;

typedef struct upoll_event {
uint32_t events;
upoll_data_t data;
} upoll_event_t;

upoll_t* upoll_create(uint32_t size);
int upoll_ctl(upoll_t* upq, int op, intptr_t fd, upoll_event_t *event);
int upoll_wait(upoll_t* upq, upoll_event_t *events, int maxevents, int timeout);
void upoll_destroy(upoll_t* upq);

intptr_t usocket(int domain, int type, int proto);
intptr_t uaccept(intptr_t sock);

int ubind(intptr_t sock, const char* name, const char* serv);
int ulisten(intptr_t sock, int backlog);
int uconnect(intptr_t sock, const char* name, const char* serv);
int uclose(intptr_t sock);
int uread(intptr_t fd, char* buf, size_t len);
int uwrite(intptr_t fd, const char* buf, size_t len);
int usocketpair(intptr_t socks[2], int async);

#endif /* _UP_H_ */

Loading