From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/civetweb/examples/_obsolete/websocket/Makefile | 36 ++++ .../_obsolete/websocket/WebSockCallbacks.c | 225 +++++++++++++++++++++ .../_obsolete/websocket/WebSockCallbacks.h | 44 ++++ .../examples/_obsolete/websocket/websock.htm | 55 +++++ .../examples/_obsolete/websocket/websocket.c | 65 ++++++ 5 files changed, 425 insertions(+) create mode 100644 src/civetweb/examples/_obsolete/websocket/Makefile create mode 100644 src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c create mode 100644 src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h create mode 100644 src/civetweb/examples/_obsolete/websocket/websock.htm create mode 100644 src/civetweb/examples/_obsolete/websocket/websocket.c (limited to 'src/civetweb/examples/_obsolete/websocket') diff --git a/src/civetweb/examples/_obsolete/websocket/Makefile b/src/civetweb/examples/_obsolete/websocket/Makefile new file mode 100644 index 00000000..3d654929 --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket/Makefile @@ -0,0 +1,36 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = websocket +SRC = WebSockCallbacks.c websocket.c + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a + +CFLAGS = -I$(TOP)/include $(COPT) +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl +endif + +all: $(PROG) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) clean lib WITH_WEBSOCKET=1 + cp $(TOP)/$(CIVETWEB_LIB) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) + +.PHONY: all clean diff --git a/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c new file mode 100644 index 00000000..bb81fb0c --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c @@ -0,0 +1,225 @@ +/* This example uses deprecated interfaces: global websocket callbacks. + They have been superseeded by URI specific callbacks. + See examples/embedded_c for an up to date example. + */ + +#include +#include +#include +#include +#include "WebSockCallbacks.h" + +#ifdef _WIN32 +#include +#define mg_sleep(x) Sleep(x) +#else +#include +#include +#define mg_sleep(x) usleep((x)*1000) +#endif + + +static void +send_to_all_websockets(struct mg_context *ctx, const char *data, int data_len) +{ + + int i; + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + + mg_lock_context(ctx); + for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) { + if (ws_ctx->socketList[i] + && (ws_ctx->socketList[i]->webSockState == 2)) { + mg_websocket_write(ws_ctx->socketList[i]->conn, + WEBSOCKET_OPCODE_TEXT, + data, + data_len); + } + } + mg_unlock_context(ctx); +} + + +void +websocket_ready_handler(struct mg_connection *conn, void *_ignored) +{ + + int i; + const struct mg_request_info *rq = mg_get_request_info(conn); + struct mg_context *ctx = mg_get_context(conn); + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + tWebSockInfo *wsock = malloc(sizeof(tWebSockInfo)); + assert(wsock); + wsock->webSockState = 0; + mg_set_user_connection_data(conn, wsock); + + mg_lock_context(ctx); + for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) { + if (0 == ws_ctx->socketList[i]) { + ws_ctx->socketList[i] = wsock; + wsock->conn = conn; + wsock->webSockState = 1; + break; + } + } + printf("\nNew websocket attached: %s:%u\n", + rq->remote_addr, + rq->remote_port); + mg_unlock_context(ctx); +} + + +static void +websocket_done(tWebSockContext *ws_ctx, tWebSockInfo *wsock) +{ + + int i; + + if (wsock) { + wsock->webSockState = 99; + for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) { + if (wsock == ws_ctx->socketList[i]) { + ws_ctx->socketList[i] = 0; + break; + } + } + printf("\nClose websocket attached: %s:%u\n", + mg_get_request_info(wsock->conn)->remote_addr, + mg_get_request_info(wsock->conn)->remote_port); + free(wsock); + } +} + + +int +websocket_data_handler(struct mg_connection *conn, + int flags, + char *data, + size_t data_len, + void *_ignored) +{ + + const struct mg_request_info *rq = mg_get_request_info(conn); + tWebSockInfo *wsock = (tWebSockInfo *)rq->conn_data; + struct mg_context *ctx = mg_get_context(conn); + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + char msg[128]; + + mg_lock_context(ctx); + if (flags == 136) { + // close websock + websocket_done(ws_ctx, wsock); + mg_set_user_connection_data(conn, NULL); + mg_unlock_context(ctx); + return 1; + } + if (((data_len >= 5) && (data_len < 100) && (flags == 129)) + || (flags == 130)) { + + // init command + if ((wsock->webSockState == 1) && (!memcmp(data, "init ", 5))) { + char *chk; + unsigned long gid; + memcpy(msg, data + 5, data_len - 5); + msg[data_len - 5] = 0; + gid = strtoul(msg, &chk, 10); + wsock->initId = gid; + if (gid > 0 && chk != NULL && *chk == 0) { + wsock->webSockState = 2; + } + mg_unlock_context(ctx); + return 1; + } + + // chat message + if ((wsock->webSockState == 2) && (!memcmp(data, "msg ", 4))) { + send_to_all_websockets(ctx, data, data_len); + mg_unlock_context(ctx); + return 1; + } + } + + // keep alive + if ((data_len == 4) && !memcmp(data, "ping", 4)) { + mg_unlock_context(ctx); + return 1; + } + + mg_unlock_context(ctx); + return 0; +} + + +void +connection_close_handler(const struct mg_connection *conn, void *_ignored) +{ + + const struct mg_request_info *rq = mg_get_request_info(conn); + tWebSockInfo *wsock = (tWebSockInfo *)rq->conn_data; + struct mg_context *ctx = mg_get_context(conn); + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + + mg_lock_context(ctx); + websocket_done(ws_ctx, wsock); + mg_set_user_connection_data(conn, NULL); + mg_unlock_context(ctx); +} + + +static void * +eventMain(void *arg) +{ + + char msg[256]; + struct mg_context *ctx = (struct mg_context *)arg; + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + + ws_ctx->runLoop = 1; + while (ws_ctx->runLoop) { + time_t t = time(0); + struct tm *timestr = localtime(&t); + strftime(msg, sizeof(msg), "title %c", timestr); + send_to_all_websockets(ctx, msg, strlen(msg)); + + mg_sleep(1000); + } + + return NULL; +} + + +void +websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len) +{ + + char buffer[260]; + + if (data_len <= 256) { + strcpy(buffer, "msg "); + memcpy(buffer + 4, data, data_len); + + send_to_all_websockets(ctx, buffer, data_len + 4); + } +} + + +void +websock_init_lib(const struct mg_context *ctx) +{ + + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + memset(ws_ctx, 0, sizeof(*ws_ctx)); + /* todo: use mg_start_thread_id instead of mg_start_thread */ + mg_start_thread(eventMain, (void *)ctx); +} + + +void +websock_exit_lib(const struct mg_context *ctx) +{ + + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + ws_ctx->runLoop = 0; + /* todo: wait for the thread instead of a timeout */ + mg_sleep(2000); +} diff --git a/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h new file mode 100644 index 00000000..f44821da --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h @@ -0,0 +1,44 @@ + +#ifndef WEBSOCKCALLBACKS_H_INCLUDED +#define WEBSOCKCALLBACKS_H_INCLUDED + +#include "civetweb.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct tWebSockInfo { + int webSockState; + unsigned long initId; + struct mg_connection *conn; +} tWebSockInfo; + +#define MAX_NUM_OF_WEBSOCKS (256) +typedef struct tWebSockContext { + int runLoop; + void *thread_id; + tWebSockInfo *socketList[MAX_NUM_OF_WEBSOCKS]; +} tWebSockContext; + + +void websock_init_lib(const struct mg_context *ctx); +void websock_exit_lib(const struct mg_context *ctx); + +void +websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len); + +void websocket_ready_handler(struct mg_connection *conn, void *_ignored); +int websocket_data_handler(struct mg_connection *conn, + int flags, + char *data, + size_t data_len, + void *_ignored); +void connection_close_handler(const struct mg_connection *conn, void *_ignored); + + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/civetweb/examples/_obsolete/websocket/websock.htm b/src/civetweb/examples/_obsolete/websocket/websock.htm new file mode 100644 index 00000000..4ff3a5fa --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket/websock.htm @@ -0,0 +1,55 @@ + + + + Test + + + + + + + + + + + \ No newline at end of file diff --git a/src/civetweb/examples/_obsolete/websocket/websocket.c b/src/civetweb/examples/_obsolete/websocket/websocket.c new file mode 100644 index 00000000..3aadf98b --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket/websocket.c @@ -0,0 +1,65 @@ +/* This example uses deprecated interfaces: global websocket callbacks. + They have been superseeded by URI specific callbacks. + See examples/embedded_c for an up to date example. + */ + +#include +#include +#include + +#include "civetweb.h" +#include "WebSockCallbacks.h" + +int +main(void) +{ + struct mg_context *ctx = 0; + struct mg_callbacks callback_funcs = {0}; + tWebSockContext ws_ctx; + char inbuf[4]; + + const char *server_options[] = { + /* document_root: The path to the test function websock.htm */ + "document_root", + "../../examples/websocket", + + /* port: use http standard to match websocket url in websock.htm: + ws://127.0.0.1/MyWebSock */ + /* if the port is changed here, it needs to be changed in + websock.htm as well */ + "listening_ports", + "80", + + NULL}; + + callback_funcs.init_context = websock_init_lib; + callback_funcs.exit_context = websock_exit_lib; + + ctx = mg_start(&callback_funcs, &ws_ctx, server_options); + + mg_set_websocket_handler(ctx, + "/MyWebSock", + NULL, + websocket_ready_handler, + websocket_data_handler, + connection_close_handler, + NULL); + + printf("Connect to localhost:%s/websock.htm\n", + mg_get_option(ctx, "listening_ports")); + + puts("Enter an (ASCII) character or * to exit:"); + for (;;) { + fgets(inbuf, sizeof(inbuf), stdin); + + if (inbuf[0] == '*') { + break; + } + inbuf[0] = toupper(inbuf[0]); + websock_send_broadcast(ctx, inbuf, 1); + } + + mg_stop(ctx); + + return 0; +} -- cgit v1.2.3