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.
 
 
 
 
 
 

188 rivejä
4.8 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. void IZ_WSServerLoadConfig(IZ_WSServerState* state, const char* config_path, u8 argc, const char* argv[]) {
  31. // TODO unify loading of config from cmdline and config file
  32. memcpy_s(state, sizeof(IZ_WSServerState), &IZ_DEFAULT_STATE, sizeof(IZ_WSServerState));
  33. const char *cmdline_buffer;
  34. // if ((cmdline_buffer = lws_cmdline_option(argc, argv, "-d"))) {
  35. // state->config.log_level = atoi(cmdline_buffer);
  36. // }
  37. if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-p"))) {
  38. state->config.port = atoi(cmdline_buffer);
  39. }
  40. if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-n"))) {
  41. memcpy_s(state->config.server_name, 64, cmdline_buffer, 64);
  42. }
  43. }
  44. const char* IZ_WSServerTestPath(const char* base_dir, const char* file) {
  45. static char test_path[32];
  46. sprintf_s(test_path, 32, "%s/%s", base_dir, file);
  47. struct stat stats;
  48. stat(test_path, &stats);
  49. if (stats.st_mode & S_IREAD) {
  50. return file;
  51. }
  52. return NULL;
  53. }
  54. IZ_ProcedureResult IZ_WSServerInitialize(IZ_WSServerState* state, void* userdata, const char* config_path, u8 argc, const char* argv[]) {
  55. IZ_WSServerLoadConfig(state, config_path, argc, argv);
  56. state->userdata = userdata;
  57. struct lws_context_creation_info info;
  58. memset(&info, 0, sizeof info);
  59. info.port = state->config.port;
  60. // char server_string[64];
  61. // if (*state->config.server_name) {
  62. // sprintf_s(server_string, 64, "%s Dedicated Server [%s]", IZ_APP_NAME, state->config.server_name);
  63. // } else {
  64. // sprintf_s(server_string, 64, "%s Dedicated Server", IZ_APP_NAME, state->config.server_name);
  65. // }
  66. // info.server_string = server_string;
  67. const char* origin = "./public";
  68. struct stat stats;
  69. stat(origin, &stats);
  70. if (S_ISDIR(stats.st_mode)) {
  71. static struct lws_http_mount mount = {
  72. .mount_next = NULL, /* linked-list "next" */
  73. .mountpoint = "/", /* mountpoint URL */
  74. .origin = NULL, /* serve from dir */
  75. .def = "index.htm", /* default filename */
  76. .protocol = "http",
  77. .cgienv = NULL,
  78. .extra_mimetypes = NULL,
  79. .interpret = NULL,
  80. .cgi_timeout = 0,
  81. .cache_max_age = 0,
  82. .auth_mask = 0,
  83. .cache_reusable = 0,
  84. .cache_revalidate = 0,
  85. .cache_intermediaries = 0,
  86. .origin_protocol = LWSMPRO_FILE, /* files in a dir */
  87. .mountpoint_len = 1, /* char count */
  88. .basic_auth_login_file = NULL,
  89. };
  90. mount.origin = origin;
  91. const char* (alt_test_paths[]) = {
  92. "index.html",
  93. };
  94. const char* default_filename;
  95. u8 i;
  96. for (i = 0; i < 1; i += 1) {
  97. default_filename = IZ_WSServerTestPath(origin, alt_test_paths[i]);
  98. }
  99. if (default_filename) {
  100. mount.def = default_filename;
  101. }
  102. info.mounts = &mount;
  103. }
  104. static const struct lws_protocols protocols[] = {
  105. {
  106. .name = NETWORK_PROTOCOL,
  107. .callback = IZ_WSServerCallback,
  108. .per_session_data_size = sizeof(IZ_WSServerSessionData),
  109. .rx_buffer_size = 0,
  110. .id = 0,
  111. .user = NULL,
  112. .tx_packet_size = 0,
  113. },
  114. {
  115. .name = "http",
  116. .callback = lws_callback_http_dummy,
  117. .per_session_data_size = 0,
  118. .rx_buffer_size = 0,
  119. .id = 0,
  120. .user = NULL,
  121. .tx_packet_size = 0,
  122. },
  123. LWS_PROTOCOL_LIST_TERM,
  124. };
  125. info.protocols = protocols;
  126. static struct lws_protocol_vhost_options pvo_port = {
  127. NULL,
  128. NULL,
  129. "port", /* pvo name */
  130. NULL /* pvo value */
  131. };
  132. pvo_port.value = (void*) &state->config.port;
  133. static struct lws_protocol_vhost_options pvo_app = {
  134. &pvo_port,
  135. NULL,
  136. "app",
  137. NULL,
  138. };
  139. pvo_app.value = state->userdata;
  140. static const struct lws_protocol_vhost_options pvo = {
  141. NULL, /* "next" pvo linked-list */
  142. &pvo_app, /* "child" pvo linked-list */
  143. NETWORK_PROTOCOL, /* protocol name we belong to on this vhost */
  144. "" /* ignored */
  145. };
  146. info.pvo = &pvo;
  147. info.options = (
  148. LWS_SERVER_OPTION_VALIDATE_UTF8
  149. | LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE
  150. );
  151. IZ_WebsocketInitialize(&state->ws);
  152. state->ws.context = lws_create_context(&info);
  153. if (!state->ws.context) {
  154. return -1;
  155. }
  156. return 0;
  157. }
  158. IZ_ProcedureResult IZ_WSServerHandle(IZ_WSServerState* state) {
  159. return IZ_WebsocketHandle(&state->ws);
  160. }
  161. void IZ_WSServerTeardown(IZ_WSServerState* state) {
  162. IZ_WebsocketTeardown(&state->ws);
  163. }
  164. void IZ_WSServerCancelService(IZ_WSServerState* state) {
  165. IZ_WebsocketCancelService(&state->ws);
  166. }