Skip to content

Commit

Permalink
Lua script callbacks in place
Browse files Browse the repository at this point in the history
  • Loading branch information
grodansparadis committed Feb 1, 2018
1 parent 4df1be6 commit f73a09c
Show file tree
Hide file tree
Showing 16 changed files with 1,677 additions and 276 deletions.
2 changes: 2 additions & 0 deletions src/common/lua/LuaXML_lib.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
LuaXML License
https://github.com/LuaDist/luaxml
LuaXml is licensed under the terms of the MIT license reproduced below,
the same as Lua itself. This means that LuaXml is free software and can be
used for both academic and commercial purposes at absolutely no cost.
Expand Down
54 changes: 33 additions & 21 deletions src/common/mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/* Amalgamated: #include "common/mg_mem.h" */

#ifndef MBUF_REALLOC
#define MBUF_REALLOC MG_REALLOC
#define MBUF_REALLOC MG_REALLOC
#endif

#ifndef MBUF_FREE
Expand Down Expand Up @@ -2834,7 +2834,6 @@ void mg_if_recv_udp_cb(struct mg_connection *nc, void *buf, int len,
} else {
/* Drop on the floor. */
MG_FREE(buf);
nc->iface->vtable->recved(nc, len);
}
}

Expand Down Expand Up @@ -3451,8 +3450,11 @@ void mg_socket_if_connect_udp(struct mg_connection *nc) {
}
if (nc->flags & MG_F_ENABLE_BROADCAST) {
int optval = 1;
setsockopt(nc->sock, SOL_SOCKET, SO_BROADCAST, (const char *) &optval,
sizeof(optval));
if (setsockopt(nc->sock, SOL_SOCKET, SO_BROADCAST, (const char *) &optval,
sizeof(optval)) < 0) {
nc->err = mg_get_errno() ? mg_get_errno() : 1;
return;
}
}
nc->err = 0;
}
Expand Down Expand Up @@ -3914,10 +3916,16 @@ time_t mg_socket_if_poll(struct mg_iface *iface, int timeout_ms) {
/* A hack to make sure all our file descriptos fit into FD_SETSIZE. */
if (nc->sock >= (sock_t) FD_SETSIZE && try_dup) {
int new_sock = dup(nc->sock);
if (new_sock >= 0 && new_sock < (sock_t) FD_SETSIZE) {
closesocket(nc->sock);
DBG(("new sock %d -> %d", nc->sock, new_sock));
nc->sock = new_sock;
if (new_sock >= 0) {
if (new_sock < (sock_t) FD_SETSIZE) {
closesocket(nc->sock);
DBG(("new sock %d -> %d", nc->sock, new_sock));
nc->sock = new_sock;
} else {
closesocket(new_sock);
DBG(("new sock is still larger than FD_SETSIZE, disregard"));
try_dup = 0;
}
} else {
try_dup = 0;
}
Expand Down Expand Up @@ -8459,9 +8467,11 @@ size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name,
size_t *data_len) {
static const char cd[] = "Content-Disposition: ";
size_t hl, bl, n, ll, pos, cdl = sizeof(cd) - 1;
int shl;

if (buf == NULL || buf_len <= 0) return 0;
if ((hl = mg_http_get_request_len(buf, buf_len)) <= 0) return 0;
if ((shl = mg_http_get_request_len(buf, buf_len)) <= 0) return 0;
hl = shl;
if (buf[0] != '-' || buf[1] != '-' || buf[2] == '\n') return 0;

/* Get boundary length */
Expand Down Expand Up @@ -8835,6 +8845,7 @@ static void mg_prepare_cgi_environment(struct mg_connection *nc,
char *p;
size_t i;
char buf[100];
size_t path_info_len = path_info != NULL ? path_info->len : 0;

blk->len = blk->nvars = 0;
blk->nc = nc;
Expand Down Expand Up @@ -8866,7 +8877,7 @@ static void mg_prepare_cgi_environment(struct mg_connection *nc,
mg_conn_addr_to_str(nc, buf, sizeof(buf), MG_SOCK_STRINGIFY_PORT);
mg_addenv(blk, "SERVER_PORT=%s", buf);

s = hm->uri.p + hm->uri.len - path_info->len - 1;
s = hm->uri.p + hm->uri.len - path_info_len - 1;
if (*s == '/') {
const char *base_name = strrchr(prog, DIRSEP);
mg_addenv(blk, "SCRIPT_NAME=%.*s/%s", (int) (s - hm->uri.p), hm->uri.p,
Expand Down Expand Up @@ -10849,16 +10860,15 @@ static void mg_mqtt_broker_handle_connect(struct mg_mqtt_broker *brk,
/* TODO(mkm): check header (magic and version) */

mg_mqtt_session_init(brk, s, nc);
s->user_data = nc->user_data;
nc->user_data = s;
nc->priv_2 = s;
mg_mqtt_add_session(s);

mg_mqtt_connack(nc, MG_EV_MQTT_CONNACK_ACCEPTED);
}

static void mg_mqtt_broker_handle_subscribe(struct mg_connection *nc,
struct mg_mqtt_message *msg) {
struct mg_mqtt_session *ss = (struct mg_mqtt_session *) nc->user_data;
struct mg_mqtt_session *ss = (struct mg_mqtt_session *) nc->priv_2;
uint8_t qoss[MG_MQTT_MAX_SESSION_SUBSCRIPTIONS];
size_t num_subs = 0;
struct mg_str topic;
Expand Down Expand Up @@ -10936,43 +10946,43 @@ void mg_mqtt_broker(struct mg_connection *nc, int ev, void *data) {
struct mg_mqtt_broker *brk;

if (nc->listener) {
brk = (struct mg_mqtt_broker *) nc->listener->user_data;
brk = (struct mg_mqtt_broker *) nc->listener->priv_2;
} else {
brk = (struct mg_mqtt_broker *) nc->user_data;
brk = (struct mg_mqtt_broker *) nc->priv_2;
}

switch (ev) {
case MG_EV_ACCEPT:
if (nc->proto_data == NULL) mg_set_protocol_mqtt(nc);
nc->user_data = NULL; /* Clear up the inherited pointer to broker */
nc->priv_2 = NULL; /* Clear up the inherited pointer to broker */
break;
case MG_EV_MQTT_CONNECT:
if (nc->user_data == NULL) {
if (nc->priv_2 == NULL) {
mg_mqtt_broker_handle_connect(brk, nc);
} else {
/* Repeated CONNECT */
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
}
break;
case MG_EV_MQTT_SUBSCRIBE:
if (nc->user_data != NULL) {
if (nc->priv_2 != NULL) {
mg_mqtt_broker_handle_subscribe(nc, msg);
} else {
/* Subscribe before CONNECT */
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
}
break;
case MG_EV_MQTT_PUBLISH:
if (nc->user_data != NULL) {
if (nc->priv_2 != NULL) {
mg_mqtt_broker_handle_publish(brk, msg);
} else {
/* Publish before CONNECT */
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
}
break;
case MG_EV_CLOSE:
if (nc->listener && nc->user_data != NULL) {
mg_mqtt_close_session((struct mg_mqtt_session *) nc->user_data);
if (nc->listener && nc->priv_2 != NULL) {
mg_mqtt_close_session((struct mg_mqtt_session *) nc->priv_2);
}
break;
}
Expand Down Expand Up @@ -11690,6 +11700,8 @@ int mg_resolve_async_opt(struct mg_mgr *mgr, const char *name, int query,
}

strncpy(req->name, name, sizeof(req->name));
req->name[sizeof(req->name) - 1] = '\0';

req->query = query;
req->callback = cb;
req->data = data;
Expand Down
8 changes: 4 additions & 4 deletions src/vscp/common/devicethread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ void *deviceThread::Entry()
// * * * * Blocking version * * * *

if ( m_pCtrlObject->m_debugFlags1 & VSCP_DEBUG1_DRIVER ) {
m_pCtrlObject->logMsg(_("[Device tread] Level I Blocking version."));
m_pCtrlObject->logMsg(_("[Device tread] Level I blocking version."));
}

/////////////////////////////////////////////////////////////////////////////
Expand All @@ -379,7 +379,7 @@ void *deviceThread::Entry()
}

if ( m_pCtrlObject->m_debugFlags1 & VSCP_DEBUG1_DRIVER ) {
m_pCtrlObject->logMsg(_("[Device tread] Level I Write thread started."));
m_pCtrlObject->logMsg(_("[Device tread] Level I write thread started."));
}

/////////////////////////////////////////////////////////////////////////////
Expand All @@ -405,7 +405,7 @@ void *deviceThread::Entry()
}

if ( m_pCtrlObject->m_debugFlags1 & VSCP_DEBUG1_DRIVER ) {
m_pCtrlObject->logMsg(_("[Device tread] Level I Receive thread started."));
m_pCtrlObject->logMsg(_("[Device tread] Level I receive thread started."));
}

// Just sit and wait until the end of the world as we know it...
Expand All @@ -414,7 +414,7 @@ void *deviceThread::Entry()
}

if ( m_pCtrlObject->m_debugFlags1 & VSCP_DEBUG1_DRIVER ) {
m_pCtrlObject->logMsg(_("[Device tread] Level I Work loop ended."));
m_pCtrlObject->logMsg(_("[Device tread] Level I work loop ended."));
}

m_preceiveThread->m_bQuit = true;
Expand Down
2 changes: 2 additions & 0 deletions src/vscp/common/duktape_vscp_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ duk_ret_t js_vscp_sendEvent( duk_context *ctx )
duk_push_boolean(ctx,0); // return code false
return JAVASCRIPT_OK;
}

duk_pop_n(ctx, 1);

// Send the event
Expand All @@ -662,6 +663,7 @@ duk_ret_t js_vscp_sendEvent( duk_context *ctx )
return JAVASCRIPT_OK;
}

pEvent->pdata = NULL;
vscp_convertVSCPfromEx( pEvent, &ex );

duk_push_global_object(ctx); /* -> stack: [ global ] */
Expand Down
Loading

0 comments on commit f73a09c

Please sign in to comment.