mirror of
https://github.com/lammertb/libhttp.git
synced 2025-08-07 16:02:55 +03:00
Renamed mg_ to httplib_
This commit is contained in:
@@ -11,49 +11,49 @@
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
#define mg_sleep(x) Sleep(x)
|
||||
#define httplib_sleep(x) Sleep(x)
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#define mg_sleep(x) usleep((x)*1000)
|
||||
#define httplib_sleep(x) usleep((x)*1000)
|
||||
#endif
|
||||
|
||||
|
||||
static void
|
||||
send_to_all_websockets(struct mg_context *ctx, const char *data, int data_len)
|
||||
send_to_all_websockets(struct httplib_context *ctx, const char *data, int data_len)
|
||||
{
|
||||
|
||||
int i;
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
|
||||
mg_lock_context(ctx);
|
||||
httplib_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,
|
||||
httplib_websocket_write(ws_ctx->socketList[i]->conn,
|
||||
WEBSOCKET_OPCODE_TEXT,
|
||||
data,
|
||||
data_len);
|
||||
}
|
||||
}
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
websocket_ready_handler(struct mg_connection *conn, void *_ignored)
|
||||
websocket_ready_handler(struct httplib_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);
|
||||
const struct httplib_request_info *rq = httplib_get_request_info(conn);
|
||||
struct httplib_context *ctx = httplib_get_context(conn);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
tWebSockInfo *wsock = malloc(sizeof(tWebSockInfo));
|
||||
assert(wsock);
|
||||
wsock->webSockState = 0;
|
||||
mg_set_user_connection_data(conn, wsock);
|
||||
httplib_set_user_connection_data(conn, wsock);
|
||||
|
||||
mg_lock_context(ctx);
|
||||
httplib_lock_context(ctx);
|
||||
for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) {
|
||||
if (0 == ws_ctx->socketList[i]) {
|
||||
ws_ctx->socketList[i] = wsock;
|
||||
@@ -65,7 +65,7 @@ websocket_ready_handler(struct mg_connection *conn, void *_ignored)
|
||||
printf("\nNew websocket attached: %s:%u\n",
|
||||
rq->remote_addr,
|
||||
rq->remote_port);
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
}
|
||||
|
||||
|
||||
@@ -84,33 +84,33 @@ websocket_done(tWebSockContext *ws_ctx, tWebSockInfo *wsock)
|
||||
}
|
||||
}
|
||||
printf("\nClose websocket attached: %s:%u\n",
|
||||
mg_get_request_info(wsock->conn)->remote_addr,
|
||||
mg_get_request_info(wsock->conn)->remote_port);
|
||||
httplib_get_request_info(wsock->conn)->remote_addr,
|
||||
httplib_get_request_info(wsock->conn)->remote_port);
|
||||
free(wsock);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
websocket_data_handler(struct mg_connection *conn,
|
||||
websocket_data_handler(struct httplib_connection *conn,
|
||||
int flags,
|
||||
char *data,
|
||||
size_t data_len,
|
||||
void *_ignored)
|
||||
{
|
||||
|
||||
const struct mg_request_info *rq = mg_get_request_info(conn);
|
||||
const struct httplib_request_info *rq = httplib_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);
|
||||
struct httplib_context *ctx = httplib_get_context(conn);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
char msg[128];
|
||||
|
||||
mg_lock_context(ctx);
|
||||
httplib_lock_context(ctx);
|
||||
if (flags == 136) {
|
||||
// close websock
|
||||
websocket_done(ws_ctx, wsock);
|
||||
mg_set_user_connection_data(conn, NULL);
|
||||
mg_unlock_context(ctx);
|
||||
httplib_set_user_connection_data(conn, NULL);
|
||||
httplib_unlock_context(ctx);
|
||||
return 1;
|
||||
}
|
||||
if (((data_len >= 5) && (data_len < 100) && (flags == 129))
|
||||
@@ -127,42 +127,42 @@ websocket_data_handler(struct mg_connection *conn,
|
||||
if (gid > 0 && chk != NULL && *chk == 0) {
|
||||
wsock->webSockState = 2;
|
||||
}
|
||||
mg_unlock_context(ctx);
|
||||
httplib_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);
|
||||
httplib_unlock_context(ctx);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// keep alive
|
||||
if ((data_len == 4) && !memcmp(data, "ping", 4)) {
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
connection_close_handler(const struct mg_connection *conn, void *_ignored)
|
||||
connection_close_handler(const struct httplib_connection *conn, void *_ignored)
|
||||
{
|
||||
|
||||
const struct mg_request_info *rq = mg_get_request_info(conn);
|
||||
const struct httplib_request_info *rq = httplib_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);
|
||||
struct httplib_context *ctx = httplib_get_context(conn);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
|
||||
mg_lock_context(ctx);
|
||||
httplib_lock_context(ctx);
|
||||
websocket_done(ws_ctx, wsock);
|
||||
mg_set_user_connection_data(conn, NULL);
|
||||
mg_unlock_context(ctx);
|
||||
httplib_set_user_connection_data(conn, NULL);
|
||||
httplib_unlock_context(ctx);
|
||||
}
|
||||
|
||||
|
||||
@@ -171,8 +171,8 @@ eventMain(void *arg)
|
||||
{
|
||||
|
||||
char msg[256];
|
||||
struct mg_context *ctx = (struct mg_context *)arg;
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
|
||||
struct httplib_context *ctx = (struct httplib_context *)arg;
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
|
||||
ws_ctx->runLoop = 1;
|
||||
while (ws_ctx->runLoop) {
|
||||
@@ -181,7 +181,7 @@ eventMain(void *arg)
|
||||
strftime(msg, sizeof(msg), "title %c", timestr);
|
||||
send_to_all_websockets(ctx, msg, strlen(msg));
|
||||
|
||||
mg_sleep(1000);
|
||||
httplib_sleep(1000);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -189,7 +189,7 @@ eventMain(void *arg)
|
||||
|
||||
|
||||
void
|
||||
websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len)
|
||||
websock_send_broadcast(struct httplib_context *ctx, const char *data, int data_len)
|
||||
{
|
||||
|
||||
char buffer[260];
|
||||
@@ -204,22 +204,22 @@ websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len)
|
||||
|
||||
|
||||
void
|
||||
websock_init_lib(const struct mg_context *ctx)
|
||||
websock_init_lib(const struct httplib_context *ctx)
|
||||
{
|
||||
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_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);
|
||||
/* todo: use httplib_start_thread_id instead of httplib_start_thread */
|
||||
httplib_start_thread(eventMain, (void *)ctx);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
websock_exit_lib(const struct mg_context *ctx)
|
||||
websock_exit_lib(const struct httplib_context *ctx)
|
||||
{
|
||||
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
ws_ctx->runLoop = 0;
|
||||
/* todo: wait for the thread instead of a timeout */
|
||||
mg_sleep(2000);
|
||||
httplib_sleep(2000);
|
||||
}
|
||||
|
@@ -11,7 +11,7 @@ extern "C" {
|
||||
typedef struct tWebSockInfo {
|
||||
int webSockState;
|
||||
unsigned long initId;
|
||||
struct mg_connection *conn;
|
||||
struct httplib_connection *conn;
|
||||
} tWebSockInfo;
|
||||
|
||||
#define MAX_NUM_OF_WEBSOCKS (256)
|
||||
@@ -22,23 +22,19 @@ typedef struct tWebSockContext {
|
||||
} tWebSockContext;
|
||||
|
||||
|
||||
void websock_init_lib(const struct mg_context *ctx);
|
||||
void websock_exit_lib(const struct mg_context *ctx);
|
||||
void websock_init_lib(const struct httplib_context *ctx);
|
||||
void websock_exit_lib(const struct httplib_context *ctx);
|
||||
|
||||
void
|
||||
websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len);
|
||||
websock_send_broadcast(struct httplib_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);
|
||||
void websocket_ready_handler(struct httplib_connection *conn, void *_ignored);
|
||||
int websocket_data_handler(struct httplib_connection *conn, int flags, char *data, size_t data_len, void *_ignored);
|
||||
void connection_close_handler(const struct httplib_connection *conn, void *_ignored);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@@ -13,8 +13,8 @@
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
struct mg_context *ctx = 0;
|
||||
struct mg_callbacks callback_funcs = {0};
|
||||
struct httplib_context *ctx = 0;
|
||||
struct httplib_callbacks callback_funcs = {0};
|
||||
tWebSockContext ws_ctx;
|
||||
char inbuf[4];
|
||||
|
||||
@@ -35,9 +35,9 @@ main(void)
|
||||
callback_funcs.init_context = websock_init_lib;
|
||||
callback_funcs.exit_context = websock_exit_lib;
|
||||
|
||||
ctx = mg_start(&callback_funcs, &ws_ctx, server_options);
|
||||
ctx = httplib_start(&callback_funcs, &ws_ctx, server_options);
|
||||
|
||||
mg_set_websocket_handler(ctx,
|
||||
httplib_set_websocket_handler(ctx,
|
||||
"/MyWebSock",
|
||||
NULL,
|
||||
websocket_ready_handler,
|
||||
@@ -46,7 +46,7 @@ main(void)
|
||||
NULL);
|
||||
|
||||
printf("Connect to localhost:%s/websock.htm\n",
|
||||
mg_get_option(ctx, "listening_ports"));
|
||||
httplib_get_option(ctx, "listening_ports"));
|
||||
|
||||
puts("Enter an (ASCII) character or * to exit:");
|
||||
for (;;) {
|
||||
@@ -59,7 +59,7 @@ main(void)
|
||||
websock_send_broadcast(ctx, inbuf, 1);
|
||||
}
|
||||
|
||||
mg_stop(ctx);
|
||||
httplib_stop(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user