|
@@ -11,15 +11,9 @@
|
|
|
#define TIMER_INTERVAL 1000
|
|
#define TIMER_INTERVAL 1000
|
|
|
typedef void (*http_fn_t)(void *arg);
|
|
typedef void (*http_fn_t)(void *arg);
|
|
|
|
|
|
|
|
-#define USE_MG716
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-#ifdef USE_MG716
|
|
|
|
|
|
|
+typedef struct mg_mgr mg_mgr_t;
|
|
|
typedef struct mg_tls_opts mg_tls_t;
|
|
typedef struct mg_tls_opts mg_tls_t;
|
|
|
-#else
|
|
|
|
|
-typedef struct mg_serve_http_opts mg_opts_t;
|
|
|
|
|
-#define MG_PATH_MAX 128
|
|
|
|
|
-#endif
|
|
|
|
|
|
|
+typedef struct mg_connection mg_conn_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
typedef struct {
|
|
|
struct mg_connection *c;
|
|
struct mg_connection *c;
|
|
@@ -30,12 +24,8 @@ typedef struct {
|
|
|
typedef struct {
|
|
typedef struct {
|
|
|
pthread_t tid;
|
|
pthread_t tid;
|
|
|
|
|
|
|
|
- struct mg_mgr mgr;
|
|
|
|
|
-#ifdef USE_MG716
|
|
|
|
|
|
|
+ mg_mgr_t mgr;
|
|
|
mg_tls_t tls;
|
|
mg_tls_t tls;
|
|
|
-#else
|
|
|
|
|
- mg_opts_t opts;
|
|
|
|
|
-#endif
|
|
|
|
|
|
|
|
|
|
handle_t list;
|
|
handle_t list;
|
|
|
char rootDir[MG_PATH_MAX];
|
|
char rootDir[MG_PATH_MAX];
|
|
@@ -44,27 +34,18 @@ typedef struct {
|
|
|
}web_handle_t;
|
|
}web_handle_t;
|
|
|
static web_handle_t webHandle={0};
|
|
static web_handle_t webHandle={0};
|
|
|
|
|
|
|
|
-static int is_ws(struct mg_connection *c)
|
|
|
|
|
|
|
+static int is_ws(mg_conn_t *c)
|
|
|
{
|
|
{
|
|
|
- int r=0;
|
|
|
|
|
-#ifdef USE_MG716
|
|
|
|
|
- r = c->is_websocket;
|
|
|
|
|
-#else
|
|
|
|
|
- r = (c->flags & MG_F_IS_WEBSOCKET)?1:0;
|
|
|
|
|
-#endif
|
|
|
|
|
|
|
+ return c->is_websocket;
|
|
|
}
|
|
}
|
|
|
-static int ws_send(struct mg_connection *c, void *data, int len, int isbin)
|
|
|
|
|
|
|
+static int ws_send(mg_conn_t *c, void *data, int len, int isbin)
|
|
|
{
|
|
{
|
|
|
LOGD("____ws_send, len: %d\n", len);
|
|
LOGD("____ws_send, len: %d\n", len);
|
|
|
-#ifdef USE_MG716
|
|
|
|
|
mg_ws_send(c, data, len, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT);
|
|
mg_ws_send(c, data, len, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT);
|
|
|
-#else
|
|
|
|
|
- mg_send_websocket_frame(c, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, data, len);
|
|
|
|
|
-#endif
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
-static int ws_send_pkt(web_handle_t *h, struct mg_connection *c, uint8_t type, void *data, int len, int isbin)
|
|
|
|
|
|
|
+static int ws_send_pkt(web_handle_t *h, mg_conn_t *c, uint8_t type, void *data, int len, int isbin)
|
|
|
{
|
|
{
|
|
|
pkt_hdr_t *hdr=NULL;
|
|
pkt_hdr_t *hdr=NULL;
|
|
|
|
|
|
|
@@ -85,7 +66,7 @@ static int ws_send_pkt(web_handle_t *h, struct mg_connection *c, uint8_t type, v
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void send_powerc(web_handle_t *h, struct mg_connection *c)
|
|
|
|
|
|
|
+static void send_powerc(web_handle_t *h, mg_conn_t *c)
|
|
|
{
|
|
{
|
|
|
int i,xlen,offset;
|
|
int i,xlen,offset;
|
|
|
pkt_hdr_t *hdr=NULL;
|
|
pkt_hdr_t *hdr=NULL;
|
|
@@ -95,7 +76,7 @@ static void send_powerc(web_handle_t *h, struct mg_connection *c)
|
|
|
ws_send_pkt(h, c, PKT_TYPE_POWER_CH, &pd.pch[i], sizeof(power_ch_t), 1);
|
|
ws_send_pkt(h, c, PKT_TYPE_POWER_CH, &pd.pch[i], sizeof(power_ch_t), 1);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-static void send_powerc_once(web_handle_t *h, struct mg_connection *c)
|
|
|
|
|
|
|
+static void send_powerc_once(web_handle_t *h, mg_conn_t *c)
|
|
|
{
|
|
{
|
|
|
int i,xlen,offset;
|
|
int i,xlen,offset;
|
|
|
pkt_hdr_t *hdr=NULL;
|
|
pkt_hdr_t *hdr=NULL;
|
|
@@ -132,7 +113,7 @@ static void send_powerc_once(web_handle_t *h, struct mg_connection *c)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-static void send_powert(web_handle_t *h, struct mg_connection *c)
|
|
|
|
|
|
|
+static void send_powert(web_handle_t *h, mg_conn_t *c)
|
|
|
{
|
|
{
|
|
|
int i,r;
|
|
int i,r;
|
|
|
power_data_t pd=power_data_get();
|
|
power_data_t pd=power_data_get();
|
|
@@ -142,7 +123,7 @@ static void send_powert(web_handle_t *h, struct mg_connection *c)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-static int ws_handle(web_handle_t *h, pkt_hdr_t *hdr, struct mg_connection *c)
|
|
|
|
|
|
|
+static int ws_handle(web_handle_t *h, pkt_hdr_t *hdr, mg_conn_t *c)
|
|
|
{
|
|
{
|
|
|
int r=0;
|
|
int r=0;
|
|
|
|
|
|
|
@@ -258,12 +239,11 @@ static int ws_handle(web_handle_t *h, pkt_hdr_t *hdr, struct mg_connection *c)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void http_fn(struct mg_connection *c, int ev, void *ev_data)
|
|
|
|
|
|
|
+static void http_fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
{
|
|
{
|
|
|
int r,flag=0;
|
|
int r,flag=0;
|
|
|
web_handle_t *h=&webHandle;
|
|
web_handle_t *h=&webHandle;
|
|
|
|
|
|
|
|
-#ifdef USE_MG716
|
|
|
|
|
if (ev == MG_EV_HTTP_MSG) {
|
|
if (ev == MG_EV_HTTP_MSG) {
|
|
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
|
|
if (mg_match(hm->uri, mg_str("/ws"), NULL)) {
|
|
if (mg_match(hm->uri, mg_str("/ws"), NULL)) {
|
|
@@ -286,49 +266,18 @@ static void http_fn(struct mg_connection *c, int ev, void *ev_data)
|
|
|
pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data.buf;
|
|
pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data.buf;
|
|
|
ws_handle(h, hdr, c);
|
|
ws_handle(h, hdr, c);
|
|
|
}
|
|
}
|
|
|
-#else
|
|
|
|
|
- if (ev == MG_EV_HTTP_REQUEST) {
|
|
|
|
|
- mg_serve_http(c, (struct http_message *)ev_data, h->opts);
|
|
|
|
|
- }
|
|
|
|
|
- else if(ev == MG_EV_WEBSOCKET_HANDSHAKE_DONE) {
|
|
|
|
|
- paras_data_t *p=paras_get();
|
|
|
|
|
- //ws_send(h, c, PKT_TYPE_PARAS, p, sizeof(paras_data_t), 1);
|
|
|
|
|
- //set_timer(c, TIMER_INTERVAL);
|
|
|
|
|
- }
|
|
|
|
|
- else if(ev == MG_EV_WEBSOCKET_FRAME) {
|
|
|
|
|
- struct websocket_message *wm = (struct websocket_message *) ev_data;
|
|
|
|
|
- pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data;
|
|
|
|
|
- ws_handle(h, hdr, c);
|
|
|
|
|
- }
|
|
|
|
|
- else if(ev==MG_EV_TIMER) {
|
|
|
|
|
-
|
|
|
|
|
- #if 0
|
|
|
|
|
- list_node_t *ln=NULL;
|
|
|
|
|
- r = list_take_node(h->list, &ln, 0);
|
|
|
|
|
- if(r==0) {
|
|
|
|
|
- node_t *n=&ln->data;
|
|
|
|
|
- ws_send(h, c, n->tp, n->buf, n->dlen, 1);
|
|
|
|
|
- list_back_node(h->list, ln);
|
|
|
|
|
- }
|
|
|
|
|
- #endif
|
|
|
|
|
- }
|
|
|
|
|
-#endif
|
|
|
|
|
}
|
|
}
|
|
|
static void timer_fn(void *arg)
|
|
static void timer_fn(void *arg)
|
|
|
{
|
|
{
|
|
|
int r;
|
|
int r;
|
|
|
web_handle_t *h=(web_handle_t*)arg;
|
|
web_handle_t *h=(web_handle_t*)arg;
|
|
|
list_node_t *ln=NULL;
|
|
list_node_t *ln=NULL;
|
|
|
- struct mg_connection* c;
|
|
|
|
|
|
|
+ mg_conn_t* c;
|
|
|
|
|
|
|
|
r = list_take_node(h->list, &ln, 0);
|
|
r = list_take_node(h->list, &ln, 0);
|
|
|
if(r==0) {
|
|
if(r==0) {
|
|
|
node_t *n=&ln->data;
|
|
node_t *n=&ln->data;
|
|
|
-#ifdef USE_MG716
|
|
|
|
|
for (c = h->mgr.conns; c != NULL; c = c->next) {
|
|
for (c = h->mgr.conns; c != NULL; c = c->next) {
|
|
|
-#else
|
|
|
|
|
- for (c = mg_next(&h->mgr, NULL); c != NULL; c = mg_next(&h->mgr, c)) {
|
|
|
|
|
-#endif
|
|
|
|
|
ws_send_pkt(h, c, n->tp, n->buf, n->dlen, 1);
|
|
ws_send_pkt(h, c, n->tp, n->buf, n->dlen, 1);
|
|
|
}
|
|
}
|
|
|
list_back_node(h->list, ln);
|
|
list_back_node(h->list, ln);
|
|
@@ -336,13 +285,12 @@ static void timer_fn(void *arg)
|
|
|
}
|
|
}
|
|
|
static void web_thread(void *arg)
|
|
static void web_thread(void *arg)
|
|
|
{
|
|
{
|
|
|
- struct mg_connection *nc;
|
|
|
|
|
|
|
+ mg_conn_t *nc;
|
|
|
const char *http_addr="http://[::]:80";
|
|
const char *http_addr="http://[::]:80";
|
|
|
const char *https_addr="https://[::]:443";
|
|
const char *https_addr="https://[::]:443";
|
|
|
thread_handle_t *th=(thread_handle_t*)arg;
|
|
thread_handle_t *th=(thread_handle_t*)arg;
|
|
|
web_handle_t *h=(web_handle_t*)th->attr->arg;
|
|
web_handle_t *h=(web_handle_t*)th->attr->arg;
|
|
|
|
|
|
|
|
-#ifdef USE_MG716
|
|
|
|
|
mg_log_set(MG_LL_DEBUG);
|
|
mg_log_set(MG_LL_DEBUG);
|
|
|
mg_mgr_init(&h->mgr);
|
|
mg_mgr_init(&h->mgr);
|
|
|
|
|
|
|
@@ -361,16 +309,6 @@ static void web_thread(void *arg)
|
|
|
//mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
|
|
//mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
|
|
|
//mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
|
|
//mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
|
|
|
//mh->tls.name = mg_url_host(https_addr);
|
|
//mh->tls.name = mg_url_host(https_addr);
|
|
|
-#else
|
|
|
|
|
- mg_mgr_init(&h->mgr, NULL);
|
|
|
|
|
- nc = mg_bind(&h->mgr, "80", http_fn);
|
|
|
|
|
-
|
|
|
|
|
- strcpy(h->rootDir, "/web");
|
|
|
|
|
-
|
|
|
|
|
- mg_set_protocol_http_websocket(nc);
|
|
|
|
|
- h->opts.document_root = h->rootDir;
|
|
|
|
|
- h->opts.enable_directory_listing = "yes";
|
|
|
|
|
-#endif
|
|
|
|
|
|
|
|
|
|
while(th->quit==0) {
|
|
while(th->quit==0) {
|
|
|
mg_mgr_poll(&h->mgr, 300);
|
|
mg_mgr_poll(&h->mgr, 300);
|