WebSockCallbacks.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <assert.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "WebSockCallbacks.h"
  5. #ifdef _WIN32
  6. #include <Windows.h>
  7. typedef HANDLE pthread_mutex_t;
  8. static int pthread_mutex_init(pthread_mutex_t *mutex, void *unused) {
  9. unused = NULL;
  10. *mutex = CreateMutex(NULL, FALSE, NULL);
  11. return *mutex == NULL ? -1 : 0;
  12. }
  13. static int pthread_mutex_destroy(pthread_mutex_t *mutex) {
  14. return CloseHandle(*mutex) == 0 ? -1 : 0;
  15. }
  16. static int pthread_mutex_lock(pthread_mutex_t *mutex) {
  17. return WaitForSingleObject(*mutex, INFINITE) == WAIT_OBJECT_0? 0 : -1;
  18. }
  19. static int pthread_mutex_unlock(pthread_mutex_t *mutex) {
  20. return ReleaseMutex(*mutex) == 0 ? -1 : 0;
  21. }
  22. #define mg_sleep(x) Sleep(x)
  23. #else
  24. #include <unistd.h>
  25. #include <pthread.h>
  26. #define mg_sleep(x) usleep((x) * 1000)
  27. #endif
  28. typedef struct tWebSockInfo {
  29. int webSockState;
  30. unsigned long initId;
  31. struct mg_connection *conn;
  32. } tWebSockInfo;
  33. static pthread_mutex_t sMutex;
  34. #define MAX_NUM_OF_WEBSOCKS (256)
  35. static tWebSockInfo *socketList[MAX_NUM_OF_WEBSOCKS];
  36. void websocket_ready_handler(struct mg_connection *conn) {
  37. int i;
  38. struct mg_request_info * rq = mg_get_request_info(conn);
  39. tWebSockInfo * wsock = malloc(sizeof(tWebSockInfo));
  40. assert(wsock);
  41. wsock->webSockState = 0;
  42. rq->conn_data = wsock;
  43. pthread_mutex_lock(&sMutex);
  44. for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
  45. if (0==socketList[i]) {
  46. socketList[i] = wsock;
  47. wsock->conn = conn;
  48. wsock->webSockState = 1;
  49. break;
  50. }
  51. }
  52. pthread_mutex_unlock(&sMutex);
  53. }
  54. static void websocket_done(tWebSockInfo * wsock) {
  55. int i;
  56. if (wsock) {
  57. wsock->webSockState = 99;
  58. for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
  59. if (wsock==socketList[i]) {
  60. socketList[i] = 0;
  61. break;
  62. }
  63. }
  64. free(wsock);
  65. }
  66. }
  67. int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len) {
  68. struct mg_request_info * rq = mg_get_request_info(conn);
  69. tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
  70. char msg[128];
  71. int i;
  72. pthread_mutex_lock(&sMutex);
  73. if (flags==136) {
  74. // close websock
  75. websocket_done(wsock);
  76. rq->conn_data = 0;
  77. pthread_mutex_unlock(&sMutex);
  78. return 1;
  79. }
  80. if ((data_len>=5) && (data_len<100) && (flags==129) || (flags==130)) {
  81. // init command
  82. if ((wsock->webSockState==1) && (!memcmp(data,"init ",5))) {
  83. char * chk;
  84. unsigned long gid;
  85. memcpy(msg,data+5,data_len-5);
  86. msg[data_len-5]=0;
  87. gid = strtoul(msg,&chk,10);
  88. wsock->initId = gid;
  89. if (gid>0 && chk!=NULL && *chk==0) {
  90. wsock->webSockState = 2;
  91. }
  92. pthread_mutex_unlock(&sMutex);
  93. return 1;
  94. }
  95. // chat message
  96. if ((wsock->webSockState==2) && (!memcmp(data,"msg ",4))) {
  97. for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
  98. if (socketList[i] && (socketList[i]->webSockState==2)) {
  99. mg_websocket_write(socketList[i]->conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
  100. }
  101. }
  102. pthread_mutex_unlock(&sMutex);
  103. return 1;
  104. }
  105. }
  106. // keep alive
  107. if ((data_len==4) && !memcmp(data,"ping",4)) {
  108. pthread_mutex_unlock(&sMutex);
  109. return 1;
  110. }
  111. pthread_mutex_unlock(&sMutex);
  112. return 0;
  113. }
  114. void connection_close_handler(struct mg_connection *conn) {
  115. struct mg_request_info * rq = mg_get_request_info(conn);
  116. tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
  117. pthread_mutex_lock(&sMutex);
  118. websocket_done(wsock);
  119. rq->conn_data = 0;
  120. pthread_mutex_unlock(&sMutex);
  121. }
  122. static void * eventMain(void * _ignored) {
  123. int i;
  124. char msg[256];
  125. for (;;) {
  126. time_t t = time(0);
  127. struct tm * timestr = localtime(&t);
  128. sprintf(msg,"title %s",asctime(timestr));
  129. pthread_mutex_lock(&sMutex);
  130. for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
  131. if (socketList[i] && (socketList[i]->webSockState==2)) {
  132. mg_websocket_write(socketList[i]->conn, WEBSOCKET_OPCODE_TEXT, msg, strlen(msg));
  133. }
  134. }
  135. pthread_mutex_unlock(&sMutex);
  136. mg_sleep(1000);
  137. }
  138. return _ignored;
  139. }
  140. void websock_init_lib(void) {
  141. int ret;
  142. ret = pthread_mutex_init(&sMutex, 0);
  143. assert(ret==0);
  144. memset(socketList,0,sizeof(socketList));
  145. mg_start_thread(eventMain, 0);
  146. }