From 31ce219544c81b387a506dbb2118c6865edc71e6 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Thu, 12 May 2022 08:07:51 +0100 Subject: [PATCH] Add MG_ARCH_RTX --- Makefile | 2 +- docs/README.md | 21 ++++++++++++++------- mongoose.c | 33 +++++++++------------------------ mongoose.h | 37 +++++++++++++++++++++++++++++++++++-- src/arch.h | 7 +++++-- src/sock.c | 33 +++++++++------------------------ 6 files changed, 73 insertions(+), 60 deletions(-) diff --git a/Makefile b/Makefile index e721d8ae..01ede229 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ test/packed_fs.c: Makefile src/ssi.h test/fuzz.c test/data/a.txt ./pack Makefile src/ssi.h test/fuzz.c test/data/a.txt test/data/range.txt > $@ DIR ?= test/data/ -OUT ?= gui.c +OUT ?= fs_packed.c mkfs: $(CC) $(CFLAGS) test/pack.c -o pack ./pack -s $(DIR) `find $(DIR) -type f` > $(OUT) diff --git a/docs/README.md b/docs/README.md index 13dc89d1..6f12e60e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -362,22 +362,29 @@ int mg_send(struct mg_connection *c, const void *buf, size_t len) { ## Minimal HTTP server -This example is a simple static HTTP server that serves current directory: +This example is a simple HTTP server that serves both static and dynamic content: ```c #include "mongoose.h" static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - struct mg_http_serve_opts opts = {.root_dir = "."}; // Serve local dir - if (ev == MG_EV_HTTP_MSG) mg_http_serve_dir(c, ev_data, &opts); + if (ev == MG_EV_HTTP_MSG) { + struct mg_http_message *hm = (struct mg_http_message *) ev_data; + if (mg_http_match_uri(hm, "/api/hello")) { + mg_http_reply(c, 200, "", "%s\n", "hi"); // Serve dynamic content + } else { + struct mg_http_serve_opts opts = {.root_dir = "."}; // Serve + mg_http_serve_dir(c, ev_data, &opts); // static content + } + } } int main(int argc, char *argv[]) { struct mg_mgr mgr; - mg_mgr_init(&mgr); // Init manager - mg_http_listen(&mgr, "http://localhost:8000", fn, &mgr); // Setup listener - for (;;) mg_mgr_poll(&mgr, 1000); // Event loop - mg_mgr_free(&mgr); // Cleanup + mg_mgr_init(&mgr); // Init manager + mg_http_listen(&mgr, "http://0.0.0.0:8000", fn, &mgr); // Setup listener + for (;;) mg_mgr_poll(&mgr, 1000); // Event loop + mg_mgr_free(&mgr); // Cleanup return 0; } ``` diff --git a/mongoose.c b/mongoose.c index 1e7202dd..36e52c03 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3303,7 +3303,12 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) { } static void mg_set_non_blocking_mode(SOCKET fd) { -#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK +#if defined(MG_CUSTOM_NONBLOCK) + MG_CUSTOM_NONBLOCK(fd); +#elif MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK + unsigned long on = 1; + ioctlsocket(fd, FIONBIO, &on); +#elif MG_ARCH == MG_ARCH_RTX unsigned long on = 1; ioctlsocket(fd, FIONBIO, &on); #elif MG_ARCH == MG_ARCH_FREERTOS_TCP @@ -3336,8 +3341,8 @@ bool mg_open_listener(struct mg_connection *c, const char *url) { if ((fd = socket(af, type, proto)) == INVALID_SOCKET) { MG_ERROR(("socket: %d", MG_SOCK_ERRNO)); -#if (MG_ARCH != MG_ARCH_WIN32 || !defined(SO_EXCLUSIVEADDRUSE)) && \ - (!defined(LWIP_SOCKET) || (defined(LWIP_SOCKET) && SO_REUSE == 1)) +#if ((MG_ARCH == MG_ARCH_WIN32) || (MG_ARCH == MG_ARCH_UNIX) || \ + (defined(LWIP_SOCKET) && SO_REUSE == 1)) } else if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) != 0) { // 1. SO_RESUSEADDR is not enabled on Windows because the semantics of @@ -3440,29 +3445,9 @@ static void setsockopts(struct mg_connection *c) { #endif if (setsockopt(FD(c), SOL_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) != 0) (void) 0; -#if defined(TCP_QUICKACK) - if (setsockopt(FD(c), SOL_TCP, TCP_QUICKACK, (char *) &on, sizeof(on)) != 0) - (void) 0; -#endif if (setsockopt(FD(c), SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) != 0) (void) 0; -#if (defined(ESP32) && ESP32) || (defined(ESP8266) && ESP8266) || \ - defined(__linux__) - int idle = 60; - if (setsockopt(FD(c), IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)) != 0) - (void) 0; -#endif -#if MG_ARCH != MG_ARCH_WIN32 && !defined(__QNX__) && MG_ARCH != MG_ARCH_ZEPHYR - { - int cnt = 3, intvl = 20; - if (setsockopt(FD(c), IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt)) != 0) - (void) 0; - if (setsockopt(FD(c), IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl)) != - 0) - (void) 0; - } -#endif #endif } @@ -3647,7 +3632,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) { static void connect_conn(struct mg_connection *c) { int rc = 0; -#if MG_ARCH != MG_ARCH_FREERTOS_TCP +#if (MG_ARCH != MG_ARCH_FREERTOS_TCP) && (MG_ARCH != MG_ARCH_RTX) socklen_t len = sizeof(rc); if (getsockopt(FD(c), SOL_SOCKET, SO_ERROR, (char *) &rc, &len)) rc = 1; #endif diff --git a/mongoose.h b/mongoose.h index 6c5be3b9..cf36714d 100644 --- a/mongoose.h +++ b/mongoose.h @@ -38,6 +38,7 @@ extern "C" { #define MG_ARCH_RTX_LWIP 8 #define MG_ARCH_ZEPHYR 9 #define MG_ARCH_NEWLIB 10 +#define MG_ARCH_RTX 11 #if !defined(MG_ARCH) #if defined(__unix__) || defined(__APPLE__) @@ -57,7 +58,7 @@ extern "C" { #endif #if !defined(MG_ARCH) -#error "MG_ARCH is not specified and we couldn't guess it." +#error "MG_ARCH is not specified and we couldn't guess it. Set -D MG_ARCH=..." #endif #endif // !defined(MG_ARCH) @@ -68,7 +69,7 @@ extern "C" { #endif #if MG_ARCH == MG_ARCH_CUSTOM - +#include #endif @@ -81,6 +82,8 @@ extern "C" { + + #if MG_ARCH == MG_ARCH_AZURERTOS #include @@ -317,6 +320,36 @@ struct timeval { #endif +#if MG_ARCH == MG_ARCH_RTX + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define MG_ENABLE_CUSTOM_MILLIS 1 +typedef int socklen_t; +#define closesocket(x) closesocket(x) +#define mkdir(a, b) (-1) +#define EWOULDBLOCK BSD_EWOULDBLOCK +#define EAGAIN BSD_EWOULDBLOCK +#define EINPROGRESS BSD_EWOULDBLOCK +#define EINTR BSD_EWOULDBLOCK +#define ECONNRESET BSD_ECONNRESET +#define EPIPE BSD_ECONNRESET +#define TCP_NODELAY SO_KEEPALIVE + +#endif + + #if MG_ARCH == MG_ARCH_RTX_LWIP #include diff --git a/src/arch.h b/src/arch.h index 760c384f..c53be7de 100644 --- a/src/arch.h +++ b/src/arch.h @@ -11,6 +11,7 @@ #define MG_ARCH_RTX_LWIP 8 #define MG_ARCH_ZEPHYR 9 #define MG_ARCH_NEWLIB 10 +#define MG_ARCH_RTX 11 #if !defined(MG_ARCH) #if defined(__unix__) || defined(__APPLE__) @@ -30,7 +31,7 @@ #endif #if !defined(MG_ARCH) -#error "MG_ARCH is not specified and we couldn't guess it." +#error "MG_ARCH is not specified and we couldn't guess it. Set -D MG_ARCH=..." #endif #endif // !defined(MG_ARCH) @@ -41,7 +42,7 @@ #endif #if MG_ARCH == MG_ARCH_CUSTOM -#include "mongoose_custom.h" +#include #endif #include "arch_esp32.h" @@ -49,6 +50,8 @@ #include "arch_freertos_lwip.h" #include "arch_freertos_tcp.h" #include "arch_newlib.h" +#include "arch_rtx.h" +#include "arch_rtx_lwip.h" #include "arch_unix.h" #include "arch_win32.h" #include "arch_zephyr.h" diff --git a/src/sock.c b/src/sock.c index 06ec5d68..8de82d75 100644 --- a/src/sock.c +++ b/src/sock.c @@ -166,7 +166,12 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) { } static void mg_set_non_blocking_mode(SOCKET fd) { -#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK +#if defined(MG_CUSTOM_NONBLOCK) + MG_CUSTOM_NONBLOCK(fd); +#elif MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK + unsigned long on = 1; + ioctlsocket(fd, FIONBIO, &on); +#elif MG_ARCH == MG_ARCH_RTX unsigned long on = 1; ioctlsocket(fd, FIONBIO, &on); #elif MG_ARCH == MG_ARCH_FREERTOS_TCP @@ -199,8 +204,8 @@ bool mg_open_listener(struct mg_connection *c, const char *url) { if ((fd = socket(af, type, proto)) == INVALID_SOCKET) { MG_ERROR(("socket: %d", MG_SOCK_ERRNO)); -#if (MG_ARCH != MG_ARCH_WIN32 || !defined(SO_EXCLUSIVEADDRUSE)) && \ - (!defined(LWIP_SOCKET) || (defined(LWIP_SOCKET) && SO_REUSE == 1)) +#if ((MG_ARCH == MG_ARCH_WIN32) || (MG_ARCH == MG_ARCH_UNIX) || \ + (defined(LWIP_SOCKET) && SO_REUSE == 1)) } else if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) != 0) { // 1. SO_RESUSEADDR is not enabled on Windows because the semantics of @@ -303,29 +308,9 @@ static void setsockopts(struct mg_connection *c) { #endif if (setsockopt(FD(c), SOL_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) != 0) (void) 0; -#if defined(TCP_QUICKACK) - if (setsockopt(FD(c), SOL_TCP, TCP_QUICKACK, (char *) &on, sizeof(on)) != 0) - (void) 0; -#endif if (setsockopt(FD(c), SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) != 0) (void) 0; -#if (defined(ESP32) && ESP32) || (defined(ESP8266) && ESP8266) || \ - defined(__linux__) - int idle = 60; - if (setsockopt(FD(c), IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)) != 0) - (void) 0; -#endif -#if MG_ARCH != MG_ARCH_WIN32 && !defined(__QNX__) && MG_ARCH != MG_ARCH_ZEPHYR - { - int cnt = 3, intvl = 20; - if (setsockopt(FD(c), IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt)) != 0) - (void) 0; - if (setsockopt(FD(c), IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl)) != - 0) - (void) 0; - } -#endif #endif } @@ -510,7 +495,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) { static void connect_conn(struct mg_connection *c) { int rc = 0; -#if MG_ARCH != MG_ARCH_FREERTOS_TCP +#if (MG_ARCH != MG_ARCH_FREERTOS_TCP) && (MG_ARCH != MG_ARCH_RTX) socklen_t len = sizeof(rc); if (getsockopt(FD(c), SOL_SOCKET, SO_ERROR, (char *) &rc, &len)) rc = 1; #endif