2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

160 lines
3.7 KiB

  1. #include "IZ_wsserver.h"
  2. IZ_ProcedureResult IZ_WSServerCallback(
  3. struct lws* wsi,
  4. enum lws_callback_reasons reason,
  5. void* user,
  6. void* in,
  7. size_t len
  8. ) {
  9. switch (reason) {
  10. case LWS_CALLBACK_PROTOCOL_INIT:
  11. return IZ_WSServerProtocolInitialize(wsi, in);
  12. case LWS_CALLBACK_PROTOCOL_DESTROY:
  13. IZ_WSServerProtocolTeardown(wsi);
  14. break;
  15. case LWS_CALLBACK_ESTABLISHED:
  16. IZ_WSServerOnOpen(wsi, user);
  17. break;
  18. case LWS_CALLBACK_CLOSED:
  19. IZ_WSServerOnClose(wsi, user);
  20. break;
  21. case LWS_CALLBACK_SERVER_WRITEABLE:
  22. return IZ_WSServerWritable(wsi, user);
  23. case LWS_CALLBACK_RECEIVE:
  24. return IZ_WSServerOnReceive(wsi, in, len);
  25. default:
  26. break;
  27. }
  28. return 0;
  29. }
  30. const char* IZ_WSServerTestPath(const char* base_dir, const char* file) {
  31. static char test_path[32];
  32. sprintf_s(test_path, 32, "%s/%s", base_dir, file);
  33. struct stat stats;
  34. stat(test_path, &stats);
  35. if (stats.st_mode & S_IREAD) {
  36. return file;
  37. }
  38. return NULL;
  39. }
  40. IZ_ProcedureResult IZ_WSServerInitialize(IZ_Websocket* state, IZ_WSServerInitializeParams params) {
  41. struct lws_context_creation_info info;
  42. memset(&info, 0, sizeof info);
  43. info.port = params.port;
  44. const char* origin = "./public";
  45. struct stat stats;
  46. stat(origin, &stats);
  47. if (S_ISDIR(stats.st_mode)) {
  48. static struct lws_http_mount mount = {
  49. .mount_next = NULL, /* linked-list "next" */
  50. .mountpoint = "/", /* mountpoint URL */
  51. .origin = NULL, /* serve from dir */
  52. .def = "index.htm", /* default filename */
  53. .protocol = "http",
  54. .cgienv = NULL,
  55. .extra_mimetypes = NULL,
  56. .interpret = NULL,
  57. .cgi_timeout = 0,
  58. .cache_max_age = 0,
  59. .auth_mask = 0,
  60. .cache_reusable = 0,
  61. .cache_revalidate = 0,
  62. .cache_intermediaries = 0,
  63. .origin_protocol = LWSMPRO_FILE, /* files in a dir */
  64. .mountpoint_len = 1, /* char count */
  65. .basic_auth_login_file = NULL,
  66. };
  67. mount.origin = origin;
  68. const char* (alt_test_paths[]) = {
  69. "index.html",
  70. };
  71. const char* default_filename;
  72. u8 i;
  73. for (i = 0; i < 1; i += 1) {
  74. default_filename = IZ_WSServerTestPath(origin, alt_test_paths[i]);
  75. }
  76. if (default_filename) {
  77. mount.def = default_filename;
  78. }
  79. info.mounts = &mount;
  80. }
  81. static const struct lws_protocols protocols[] = {
  82. {
  83. .name = NETWORK_PROTOCOL,
  84. .callback = IZ_WSServerCallback,
  85. .per_session_data_size = sizeof(IZ_WSServerSessionData),
  86. .rx_buffer_size = 0,
  87. .id = 0,
  88. .user = NULL,
  89. .tx_packet_size = 0,
  90. },
  91. {
  92. .name = "http",
  93. .callback = lws_callback_http_dummy,
  94. .per_session_data_size = 0,
  95. .rx_buffer_size = 0,
  96. .id = 0,
  97. .user = NULL,
  98. .tx_packet_size = 0,
  99. },
  100. LWS_PROTOCOL_LIST_TERM,
  101. };
  102. info.protocols = protocols;
  103. static struct lws_protocol_vhost_options pvo_port = {
  104. NULL,
  105. NULL,
  106. "port", /* pvo name */
  107. NULL /* pvo value */
  108. };
  109. pvo_port.value = (void*) &params.port;
  110. static struct lws_protocol_vhost_options pvo_app = {
  111. &pvo_port,
  112. NULL,
  113. "app",
  114. NULL,
  115. };
  116. pvo_app.value = state->user_data;
  117. static const struct lws_protocol_vhost_options pvo = {
  118. NULL, /* "next" pvo linked-list */
  119. &pvo_app, /* "child" pvo linked-list */
  120. NETWORK_PROTOCOL, /* protocol name we belong to on this vhost */
  121. "" /* ignored */
  122. };
  123. info.pvo = &pvo;
  124. info.options = (
  125. LWS_SERVER_OPTION_VALIDATE_UTF8
  126. | LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE
  127. );
  128. IZ_WebsocketInitialize(state);
  129. state->context = lws_create_context(&info);
  130. if (!state->context) {
  131. return -1;
  132. }
  133. return 0;
  134. }
  135. IZ_ProcedureResult IZ_WSServerHandle(IZ_Websocket* state) {
  136. return IZ_WebsocketHandle(state);
  137. }
  138. void IZ_WSServerTeardown(IZ_Websocket* state) {
  139. IZ_WebsocketTeardown(state);
  140. }
  141. void IZ_WSServerCancelService(IZ_Websocket* state) {
  142. IZ_WebsocketCancelService(state);
  143. }