web.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "web.h"
  2. #include "mongoose.h"
  3. #include "thread.h"
  4. #include "cfg.h"
  5. typedef void (*http_fn_t)(void *arg);
  6. #ifdef USE_MG714
  7. typedef struct mg_tls_opts mg_tls_t;
  8. #else
  9. typedef struct mg_serve_http_opts mg_opts_t;
  10. #define MG_PATH_MAX 128
  11. #endif
  12. typedef struct {
  13. struct mg_connection *c;
  14. int ev;
  15. void *ev_data;
  16. }mg_stream_t;
  17. typedef struct {
  18. pthread_t tid;
  19. struct mg_mgr mgr;
  20. #ifdef USE_MG714
  21. mg_tls_t tls;
  22. #else
  23. mg_opts_t opts;
  24. #endif
  25. char rootDir[MG_PATH_MAX];
  26. }web_handle_t;
  27. static web_handle_t webHandle={0};
  28. static void http_fn(struct mg_connection *c, int ev, void *ev_data)
  29. {
  30. int i,j,flag=0;
  31. web_handle_t *h=&webHandle;
  32. mg_stream_t s={c,ev,ev_data};
  33. #ifdef USE_MG714
  34. if (ev == MG_EV_HTTP_MSG) {
  35. struct mg_http_serve_opts opts = {.root_dir = h->rootDir};
  36. mg_http_serve_dir(c, ev_data, &opts);
  37. }
  38. else if(ev == MG_EV_ACCEPT) {
  39. if (c->fn_data) {
  40. mg_tls_init(c, &h->tls);
  41. }
  42. }
  43. #else
  44. if (ev == MG_EV_HTTP_REQUEST) {
  45. mg_serve_http(c, (struct http_message *)ev_data, h->opts);
  46. }
  47. else if(ev == MG_EV_WEBSOCKET_FRAME) {
  48. static int cnt=0;
  49. char tmp[30];
  50. struct websocket_message *wm = (struct websocket_message *) ev_data;
  51. sprintf(tmp, "hello %d\n", cnt++);
  52. //web_send(wm->data, wm->size, 1);
  53. web_send(tmp, strlen(tmp)+1, 1);
  54. }
  55. else {
  56. //
  57. }
  58. #endif
  59. }
  60. static void *web_thread(void *arg)
  61. {
  62. struct mg_connection *nc;
  63. const char *http_addr="http://[::]:80";
  64. const char *https_addr="https://[::]:443";
  65. thread_handle_t *th=(thread_handle_t*)arg;
  66. web_handle_t *h=(web_handle_t*)th->arg;
  67. #ifdef USE_MG714
  68. //mg_log_set(MG_LL_DEBUG);
  69. mg_mgr_init(&mgr);
  70. //sys_get_path(mh->rootDir, WEB_ROOT);
  71. strcpy(h->rootDir, "/web");
  72. if (mg_http_listen(&mgr, http_addr, http_fn, NULL) == NULL) {
  73. MG_ERROR(("cannot listen on %s\n", http_addr));
  74. return NULL;
  75. }
  76. if (mg_http_listen(&mgr, https_addr, http_fn, (void*)1) == NULL) {
  77. MG_ERROR(("cannot listen on %s\n", https_addr));
  78. return NULL;
  79. }
  80. //mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
  81. //mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
  82. //mh->tls.name = mg_url_host(https_addr);
  83. #else
  84. mg_mgr_init(&h->mgr, NULL);
  85. nc = mg_bind(&h->mgr, "80", http_fn);
  86. strcpy(h->rootDir, "/web");
  87. mg_set_protocol_http_websocket(nc);
  88. h->opts.document_root = h->rootDir;
  89. h->opts.enable_directory_listing = "yes";
  90. #endif
  91. while(th->quit==0) {
  92. mg_mgr_poll(&h->mgr, 200);
  93. }
  94. mg_mgr_free(&h->mgr);
  95. pthread_exit(NULL);
  96. }
  97. int web_init(void)
  98. {
  99. web_handle_t *h=&webHandle;
  100. thread_start(THREAD_ID_WEB, web_thread, h);
  101. return 0;
  102. }
  103. int web_send(void *data, int len, int is_bin)
  104. {
  105. struct mg_connection *c;
  106. web_handle_t *h=&webHandle;
  107. for (c = mg_next(&h->mgr, NULL); c != NULL; c = mg_next(&h->mgr, c)) {
  108. mg_send_websocket_frame(c, is_bin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, data, len);
  109. }
  110. return 0;
  111. }