Просмотр исходного кода

1、修复后台经常崩溃的bug,将http服务全部转为mangoose实现;
2、修复传感器websocket推送造成内存泄漏的问题。

liyuezong лет назад: 2
Родитель
Сommit
311015b7e8

+ 20 - 4
pro/mongoose/mongoose.c

@@ -2270,17 +2270,33 @@ void mg_http_reply(struct mg_connection *c, int code, const char *headers,
                    const char *fmt, ...) {
   va_list ap;
   size_t len;
-  mg_printf(c, "HTTP/1.1 %d %s\r\n%sContent-Length:            \r\n\r\n", code,
-            mg_http_status_code_str(code), headers == NULL ? "" : headers);
+  
+// mg_printf(c, "HTTP/1.1 %d %s\r\n%sContent-Length:            \r\n\r\n", code,
+ //           mg_http_status_code_str(code), headers == NULL ? "" : headers);
+mg_printf(c, "HTTP/1.1 %d %s\r\n"
+  "Content-Type: text/html\n"
+  "Content-Length: %d\n"
+  "Connection: Keep-Alive\n"
+  "Keep-Alive: timeout=30, max=396\n" 
+  "X-Content-Type-0ptions: nosniff\n"
+  "X-Frame-0ptions:: SAMEORIGIN\n"
+  "X-Xss-Protection: 1: mode=block\n"
+  "Accept-Ranges: bytes\n"
+  "Vary: Accept-Encoding\n"
+  "Access-Control-Allow-Origin: *\n"
+  "Access-Control-Allow-Methods: *\n"
+  "Access-Control-Allow-Headers: *\n\n"
+  "%s\n", code, mg_http_status_code_str(code),strlen(fmt), fmt == NULL ? "" : fmt);
+
   len = c->send.len;
   va_start(ap, fmt);
   mg_vxprintf(mg_pfn_iobuf, &c->send, fmt, &ap);
   va_end(ap);
-  if (c->send.len > 16) {
+  /*if (c->send.len > 16) {
     size_t n = mg_snprintf((char *) &c->send.buf[len - 15], 11, "%-10lu",
                            (unsigned long) (c->send.len - len));
     c->send.buf[len - 15 + n] = ' ';  // Change ending 0 to space
-  }
+  }*/
   c->is_resp = 0;
 }
 

+ 25 - 31
pro/src/appweb_handle.c

@@ -73,21 +73,6 @@ static char* g_product_pwr_type_str[] = {
 };
 static int webSetCallback(void);
 
-
-#ifndef USE_WEBAPP
-const char *hdr_str=
-"Vary: Accept-Encoding\r\n"
-"Keep-Alive: timeout=30 max=394\r\n"
-"Content-Type: text/html\r\n" 
-"X-Frame-Options: SAMEORIGIN\r\n"
-"X-Content-Type-Options: nosniff\r\n"
-"X-XSS-Protection: 1; mode=block\r\n"
-"Date: Thu, 01 Jan 1970 23:13:59 GMT\r\n"
-"Connection: Keep-Alive\r\n"
-"Accept-Ranges: bytes\r\n"
-"File Data: 238 bytes\r\n"
-;
-#endif
 static int http_reply(void *conn, int status, char *ack)
 {
      int r=0;
@@ -101,7 +86,8 @@ static int http_reply(void *conn, int status, char *ack)
     httpFinalize(s);
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
-    mg_http_reply(s->c, status, "", "%s", ack);
+   // mg_http_reply(s->c, status, "", "%s", ack);
+    mg_http_reply(s->c, status, "",  ack);
 #endif
 
     return r;
@@ -318,7 +304,6 @@ void* update_thread(void* arg)
             }
             //else if (gdm->view_page==3)
             {
-
                 upgrade_msg =ws_sensor_status_ack_to_json(&__globalDeviceManage._globalSensorManger);
                 websocket_send(upgrade_msg, strlen(upgrade_msg));
                 cJSON_free((void*)upgrade_msg);
@@ -327,9 +312,14 @@ void* update_thread(void* arg)
            // pthread_mutex_unlock(&Websocketlock); 
             //log_i("ws upload.");
         }
-        usleep(1000*1000);
+
+        int npoll=websocket_poll();
+        // log_i("ws poll once!  status=%d\n", npoll);
+
+        usleep(500*1000);
     }
-    
+
+    websocket_free() ;
     pthread_exit(NULL);
 }
 
@@ -366,7 +356,7 @@ static void changeLanguages(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
     json_to_switch_language(rx_body_buf);
     dev_update_device_genera_info(__globalDeviceManage.db,__globalDeviceManage._global_device_info);
@@ -388,7 +378,7 @@ static void loginFunc(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
 
     //用户信息创建-未析构
@@ -470,7 +460,7 @@ static void loginOutFunc(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
 
     //转换json参数
@@ -535,7 +525,7 @@ static void overallStatusMonitoring(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
 
     //转换json参数
@@ -982,7 +972,7 @@ static void channelStatusMonitoring(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
 
     //转换json参数
@@ -1507,7 +1497,7 @@ static void sensorStatusMonitoring(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
     //转换json参数
     cjson = json_to_sensor_manage(rx_body_buf,&_over_all_sensor_request);
@@ -1992,7 +1982,7 @@ static void alarmProtect(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
 
     memset(&_alarm_request_info, 0, sizeof(_alarm_request_info));
@@ -2269,7 +2259,7 @@ static void logInformation(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
     
     memset(&__logInformationRequestInfo,0,sizeof(_LogInformationRequestInfo));
@@ -2464,7 +2454,7 @@ static void communicationManagement(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
 
     //转换json参数
@@ -2564,7 +2554,7 @@ static void serviceManagement(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
 
     //转换json参数
@@ -3290,7 +3280,7 @@ static void systemManagent(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
 
     //转换json参数
@@ -3456,7 +3446,7 @@ static void equipmentMintenance(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.buf;
+    rx_body_buf = msg->body.ptr;
 #endif
 
     //转换json参数
@@ -3509,6 +3499,7 @@ static void equipmentMintenance(void *conn)
         {
           diff = ts.tv_sec;
         }
+        _over_back_sts_info.product_runTime = malloc(32);
         sprintf(_over_back_sts_info.product_runTime, "%02ld:%02ld:%02ld", diff / 3600, (diff % 3600) / 60, diff % 60);
         _over_back_sts_info.productAddr = getLocalIpAddress("eth0");
 
@@ -3571,6 +3562,7 @@ static void equipmentMintenanceUpload(void *conn)
     HttpStream *s=(HttpStream*)conn;
     http_reply(conn, 200, NULL);
 #else
+/*
     mg_stream_t *s=(mg_stream_t*)conn;
     upload_stat_t *us = (upload_stat_t *) s->c->data;
     struct mg_fs *fs = &mg_fs_posix;
@@ -3604,6 +3596,8 @@ static void equipmentMintenanceUpload(void *conn)
             s->c->is_draining = 1;          // Close connection when response gets sent
         }
     }  
+
+    */
 #endif
 }
 

+ 12 - 1
pro/src/json_handle.c

@@ -2244,6 +2244,7 @@ char* over_all_sensor_info_to_json(_OverSensorBackInfo* _over_all_sensor_back_in
  */
 char *ws_sensor_status_ack_to_json(GlobalSensorManger *_globalSensorManger)
 {
+    char *strRet = NULL;
     _OverSensorBackInfo _over_all_sensor_back_info;
     INIT_LIST_HEAD(&_over_all_sensor_back_info.list);
     // 获取传感器当前的最新值
@@ -2325,7 +2326,17 @@ char *ws_sensor_status_ack_to_json(GlobalSensorManger *_globalSensorManger)
         list_add_tail(&_overSensorBackInfoTemp->list, &_over_all_sensor_back_info.list);
     }
     // 转换
-    return over_all_sensor_info_to_json(&_over_all_sensor_back_info,1);
+
+    strRet = over_all_sensor_info_to_json(&_over_all_sensor_back_info, 1);
+    // 释放资源
+    _OverSensorBackInfo *node;
+    _OverSensorBackInfo *next;
+    list_for_each_entry_safe(node, next, &_over_all_sensor_back_info.list, list)
+    {
+        list_del(&node->list);
+        free(node);
+    }
+    return strRet;
 }
 
 /// @brief 返回传感器历史信息

+ 2 - 0
pro/src/sqlite_handle.c

@@ -467,6 +467,8 @@ int dev_search_latest_power_All_info(GlobalDeviceManager* globalDeviceManger,_Ov
             list_add_tail(&_overChnPwrBackInfoTemp->list, &__overChnPwrAckInfo->list);
         }
     }
+
+
     return 0;
 }
 /// @brief 获取所有采集统计的最新值

+ 38 - 6
pro/src/websocket_handle.c

@@ -54,9 +54,9 @@ static void fn(mg_conn_t *c, int ev, void *ev_data)
         if (c->id == wh->c->id)
         {
           wh->c = NULL;
-          log_d("ws upgrade del connection ID:%d\n",c->id);
         }
       }
+      log_i("ws upgrade del connection ID:%d\n", c->id);
     }
     break;
 
@@ -102,15 +102,15 @@ void* websocket_thread(void* arg)
     mg_http_listen(&wh->mgr, wh->wpath, fn, NULL);  // Create HTTP listener
     wh->inited = 1;
 
-    while(h->quit==0) {
-          pthread_mutex_lock(&WSlock); 
-	        mg_mgr_poll(&wh->mgr, 1000);                    // Infinite event loop
-          pthread_mutex_unlock(&WSlock); 
+    /*while(h->quit==0) {
+          pthread_mutex_lock(&WSlock);
+          mg_mgr_poll(&wh->mgr, 1000);                    // Infinite event loop
+          pthread_mutex_unlock(&WSlock);
           usleep(100000);
     }
     wh->inited = 0;
     mg_mgr_free(&wh->mgr);
-
+*/
     pthread_exit(NULL);
 }
 
@@ -148,6 +148,38 @@ int websocket_isok(void)
     return (wh->c)?1:0;
 }
 
+int websocket_poll(void) 
+{
+    int r;
+
+    ws_handle_t *wh=&wsHandle;
+    if (!wh->inited)
+    {
+      return -1;
+    }
+
+    pthread_mutex_lock(&WSlock); 
+	  mg_mgr_poll(&wh->mgr, 1000);                    // Infinite event loop
+    pthread_mutex_unlock(&WSlock); 
+
+
+    return 0;
+}
+
+int websocket_free(void) 
+{
+    int r;
+
+    ws_handle_t *wh=&wsHandle;
+    if (!wh->inited)
+    {
+      return -1;
+    }
+
+    wh->inited = 0;
+    mg_mgr_free(&wh->mgr);
+    return 0;
+}
 
 
 

+ 2 - 1
pro/src/websocket_handle.h

@@ -13,6 +13,7 @@ typedef struct {
 int websocket_init(void);
 int websocket_send(void *data, int len);
 int websocket_isok(void);
-
+int websocket_poll(void) ;
+int websocket_free(void) ;
 #endif