WebSockCallbacks.c 5.5 KB

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