websocket_handle.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "websocket_handle.h"
  2. #include "mongoose.h"
  3. #include "pthread.h"
  4. #include "elog.h"
  5. #include "common.h"
  6. #include "sys.h"
  7. #define WS_MAX 10
  8. typedef struct mg_mgr mg_mgr_t;
  9. typedef struct mg_connection mg_conn_t;
  10. typedef struct mg_ws_message mg_ws_msg_t;
  11. typedef struct mg_http_message mg_http_msg_t;
  12. typedef struct {
  13. mg_mgr_t mgr;
  14. char wpath[200];
  15. char root[200];
  16. mg_conn_t *c;
  17. int inited;
  18. pthread_mutex_t mutex;
  19. }ws_handle_t;
  20. static ws_handle_t wsHandle;
  21. static void read_ipaddr(ws_handle_t *h)
  22. {
  23. sprintf(h->wpath,"ws://%s:6785", getLocalIpAddress("eth0"));
  24. printf("____ ws path: %s\n", h->wpath);
  25. }
  26. static void fn(mg_conn_t *c, int ev, void *ev_data)
  27. {
  28. ws_handle_t *wh=&wsHandle;
  29. switch(ev) {
  30. case MG_EV_OPEN:
  31. {
  32. }
  33. break;
  34. case MG_EV_CLOSE:
  35. {
  36. }
  37. break;
  38. case MG_EV_HTTP_MSG:
  39. {
  40. static int ws_cnt=0;
  41. mg_http_msg_t *hm = (mg_http_msg_t *) ev_data;
  42. if (mg_http_match_uri(hm, "/websocket/PDU-WebSocket"))
  43. {
  44. mg_ws_upgrade(c, hm, NULL);
  45. wh->c = c;
  46. }
  47. else if (mg_http_match_uri(hm, "/rest"))
  48. {
  49. // Serve REST response
  50. mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
  51. }
  52. else
  53. {
  54. wh->c = NULL;
  55. }
  56. }
  57. break;
  58. case MG_EV_WS_MSG:
  59. {
  60. struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
  61. mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
  62. }
  63. break;
  64. }
  65. }
  66. void* websocket_thread(void* arg)
  67. {
  68. int r;
  69. thread_handle_t *h=(thread_handle_t*)arg;
  70. ws_handle_t *wh=(ws_handle_t*)h->arg;
  71. mg_mgr_init(&wh->mgr); // Initialise event manager
  72. mg_http_listen(&wh->mgr, wh->wpath, fn, NULL); // Create HTTP listener
  73. wh->inited = 1;
  74. while(h->quit==0) {
  75. mg_mgr_poll(&wh->mgr, 1000); // Infinite event loop
  76. }
  77. wh->inited = 0;
  78. mg_mgr_free(&wh->mgr);
  79. pthread_exit(NULL);
  80. }
  81. int websocket_init(void)
  82. {
  83. ws_handle_t *wh=&wsHandle;
  84. memset(wh, 0, sizeof(ws_handle_t));
  85. read_ipaddr(wh);
  86. sys_start_thread(THREAD_ID_WS, websocket_thread, wh);
  87. return 0;
  88. }
  89. int websocket_send(void *data, int len)
  90. {
  91. int i;
  92. ws_handle_t *wh=&wsHandle;
  93. if(!wh->inited) {
  94. return -1;
  95. }
  96. mg_ws_send(wh->c, data, len, WEBSOCKET_OP_TEXT);
  97. return 0;
  98. }
  99. int websocket_isok(void)
  100. {
  101. ws_handle_t *wh=&wsHandle;
  102. return (wh->c)?1:0;
  103. }