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
4.1 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. sprintf_s(state->config.server_name, 64, "%s Dedicated Server [%s]", IZ_APP_NAME, cmdline_buffer);
  42. } else {
  43. sprintf_s(state->config.server_name, 128, "%s Dedicated Server", IZ_APP_NAME);
  44. }
  45. }
  46. IZ_ProcedureResult IZ_WSServerInitialize(IZ_WSServerState* state, void* userdata, const char* config_path, u8 argc, const char* argv[]) {
  47. IZ_WSServerLoadConfig(state, config_path, argc, argv);
  48. state->userdata = userdata;
  49. struct lws_context_creation_info info;
  50. memset(&info, 0, sizeof info);
  51. info.port = state->config.port;
  52. const char* origin = "./public";
  53. struct stat stats;
  54. stat(origin, &stats);
  55. if (S_ISDIR(stats.st_mode)) {
  56. static struct lws_http_mount mount = {
  57. .mount_next = NULL, /* linked-list "next" */
  58. .mountpoint = "/", /* mountpoint URL */
  59. .origin = NULL, /* serve from dir */
  60. .def = "index.html", /* default filename */
  61. .protocol = "http",
  62. .cgienv = NULL,
  63. .extra_mimetypes = NULL,
  64. .interpret = NULL,
  65. .cgi_timeout = 0,
  66. .cache_max_age = 0,
  67. .auth_mask = 0,
  68. .cache_reusable = 0,
  69. .cache_revalidate = 0,
  70. .cache_intermediaries = 0,
  71. .origin_protocol = LWSMPRO_FILE, /* files in a dir */
  72. .mountpoint_len = 1, /* char count */
  73. .basic_auth_login_file = NULL,
  74. };
  75. mount.origin = origin;
  76. info.mounts = &mount;
  77. }
  78. static const struct lws_protocols protocols[] = {
  79. {
  80. .name = NETWORK_PROTOCOL,
  81. .callback = IZ_WSServerCallback,
  82. .per_session_data_size = sizeof(IZ_WSServerSessionData),
  83. .rx_buffer_size = 0,
  84. .id = 0,
  85. .user = NULL,
  86. .tx_packet_size = 0,
  87. },
  88. {
  89. .name = "http",
  90. .callback = lws_callback_http_dummy,
  91. .per_session_data_size = 0,
  92. .rx_buffer_size = 0,
  93. .id = 0,
  94. .user = NULL,
  95. .tx_packet_size = 0,
  96. },
  97. LWS_PROTOCOL_LIST_TERM,
  98. };
  99. info.protocols = protocols;
  100. static struct lws_protocol_vhost_options pvo_port = {
  101. NULL,
  102. NULL,
  103. "port", /* pvo name */
  104. NULL /* pvo value */
  105. };
  106. pvo_port.value = (void*) &state->config.port;
  107. static struct lws_protocol_vhost_options pvo_app = {
  108. &pvo_port,
  109. NULL,
  110. "app",
  111. NULL,
  112. };
  113. pvo_app.value = state->userdata;
  114. static const struct lws_protocol_vhost_options pvo = {
  115. NULL, /* "next" pvo linked-list */
  116. &pvo_app, /* "child" pvo linked-list */
  117. NETWORK_PROTOCOL, /* protocol name we belong to on this vhost */
  118. "" /* ignored */
  119. };
  120. info.pvo = &pvo;
  121. info.options = (
  122. LWS_SERVER_OPTION_VALIDATE_UTF8
  123. | LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE
  124. );
  125. IZ_WebsocketInitialize(&state->ws);
  126. state->ws.context = lws_create_context(&info);
  127. if (!state->ws.context) {
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. IZ_ProcedureResult IZ_WSServerHandle(IZ_WSServerState* state) {
  133. return IZ_WebsocketHandle(&state->ws);
  134. }
  135. void IZ_WSServerTeardown(IZ_WSServerState* state) {
  136. IZ_WebsocketTeardown(&state->ws);
  137. }
  138. void IZ_WSServerCancelService(IZ_WSServerState* state) {
  139. IZ_WebsocketCancelService(&state->ws);
  140. }