WebSockCallbacks.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. static void send_to_all_websockets(const char * data, int data_len) {
  37. int i;
  38. for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
  39. if (socketList[i] && (socketList[i]->webSockState==2)) {
  40. mg_websocket_write(socketList[i]->conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
  41. }
  42. }
  43. }
  44. void websocket_ready_handler(struct mg_connection *conn) {
  45. int i;
  46. struct mg_request_info * rq = mg_get_request_info(conn);
  47. tWebSockInfo * wsock = malloc(sizeof(tWebSockInfo));
  48. assert(wsock);
  49. wsock->webSockState = 0;
  50. rq->conn_data = wsock;
  51. pthread_mutex_lock(&sMutex);
  52. for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
  53. if (0==socketList[i]) {
  54. socketList[i] = wsock;
  55. wsock->conn = conn;
  56. wsock->webSockState = 1;
  57. break;
  58. }
  59. }
  60. printf("\nNew websocket attached: %08x:%u\n", rq->remote_ip, rq->remote_port);
  61. pthread_mutex_unlock(&sMutex);
  62. }
  63. static void websocket_done(tWebSockInfo * wsock) {
  64. int i;
  65. if (wsock) {
  66. wsock->webSockState = 99;
  67. for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
  68. if (wsock==socketList[i]) {
  69. socketList[i] = 0;
  70. break;
  71. }
  72. }
  73. printf("\nClose websocket attached: %08x:%u\n", mg_get_request_info(wsock->conn)->remote_ip, mg_get_request_info(wsock->conn)->remote_port);
  74. free(wsock);
  75. }
  76. }
  77. int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len) {
  78. struct mg_request_info * rq = mg_get_request_info(conn);
  79. tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
  80. char msg[128];
  81. pthread_mutex_lock(&sMutex);
  82. if (flags==136) {
  83. // close websock
  84. websocket_done(wsock);
  85. rq->conn_data = 0;
  86. pthread_mutex_unlock(&sMutex);
  87. return 1;
  88. }
  89. if ((data_len>=5) && (data_len<100) && (flags==129) || (flags==130)) {
  90. // init command
  91. if ((wsock->webSockState==1) && (!memcmp(data,"init ",5))) {
  92. char * chk;
  93. unsigned long gid;
  94. memcpy(msg,data+5,data_len-5);
  95. msg[data_len-5]=0;
  96. gid = strtoul(msg,&chk,10);
  97. wsock->initId = gid;
  98. if (gid>0 && chk!=NULL && *chk==0) {
  99. wsock->webSockState = 2;
  100. }
  101. pthread_mutex_unlock(&sMutex);
  102. return 1;
  103. }
  104. // chat message
  105. if ((wsock->webSockState==2) && (!memcmp(data,"msg ",4))) {
  106. send_to_all_websockets(data, data_len);
  107. pthread_mutex_unlock(&sMutex);
  108. return 1;
  109. }
  110. }
  111. // keep alive
  112. if ((data_len==4) && !memcmp(data,"ping",4)) {
  113. pthread_mutex_unlock(&sMutex);
  114. return 1;
  115. }
  116. pthread_mutex_unlock(&sMutex);
  117. return 0;
  118. }
  119. void connection_close_handler(struct mg_connection *conn) {
  120. struct mg_request_info * rq = mg_get_request_info(conn);
  121. tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
  122. pthread_mutex_lock(&sMutex);
  123. websocket_done(wsock);
  124. rq->conn_data = 0;
  125. pthread_mutex_unlock(&sMutex);
  126. }
  127. static int runLoop = 0;
  128. static void * eventMain(void * _ignored) {
  129. int i;
  130. char msg[256];
  131. runLoop = 1;
  132. while (runLoop) {
  133. time_t t = time(0);
  134. struct tm * timestr = localtime(&t);
  135. sprintf(msg,"title %s",asctime(timestr));
  136. pthread_mutex_lock(&sMutex);
  137. for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
  138. if (socketList[i] && (socketList[i]->webSockState==2)) {
  139. mg_websocket_write(socketList[i]->conn, WEBSOCKET_OPCODE_TEXT, msg, strlen(msg));
  140. }
  141. }
  142. pthread_mutex_unlock(&sMutex);
  143. mg_sleep(1000);
  144. }
  145. return _ignored;
  146. }
  147. void websock_send_broadcast(const char * data, int data_len) {
  148. char buffer[260];
  149. if (data_len<=256) {
  150. strcpy(buffer, "msg ");
  151. memcpy(buffer+4, data, data_len);
  152. pthread_mutex_lock(&sMutex);
  153. send_to_all_websockets(buffer, data_len+4);
  154. pthread_mutex_unlock(&sMutex);
  155. }
  156. }
  157. void websock_init_lib(void) {
  158. int ret;
  159. ret = pthread_mutex_init(&sMutex, 0);
  160. assert(ret==0);
  161. memset(socketList,0,sizeof(socketList));
  162. mg_start_thread(eventMain, 0);
  163. }
  164. void websock_exit_lib(void) {
  165. runLoop = 0;
  166. }