mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-26 22:41:03 +08:00
Add MG_ARCH_RTX
This commit is contained in:
parent
86cd567968
commit
31ce219544
2
Makefile
2
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)
|
||||
|
@ -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;
|
||||
}
|
||||
```
|
||||
|
33
mongoose.c
33
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
|
||||
|
37
mongoose.h
37
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 <mongoose_custom.h>
|
||||
#endif
|
||||
|
||||
|
||||
@ -81,6 +82,8 @@ extern "C" {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#if MG_ARCH == MG_ARCH_AZURERTOS
|
||||
|
||||
#include <stdarg.h>
|
||||
@ -317,6 +320,36 @@ struct timeval {
|
||||
#endif
|
||||
|
||||
|
||||
#if MG_ARCH == MG_ARCH_RTX
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <rl_net.h>
|
||||
|
||||
#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 <ctype.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 <mongoose_custom.h>
|
||||
#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"
|
||||
|
33
src/sock.c
33
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user