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.

IZ_app_net.c 8.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "IZ_app_net.h"
  2. void IZ_AppHandleNetworkingInboundBinaryEvents(struct IZ_App* app, void* binary_raw, size_t len) {
  3. u8* binary = binary_raw;
  4. size_t i;
  5. printf("%lu: Binary", IZ_AppGetTicks(app));
  6. for (i = 0; i < len; i += 1) {
  7. printf("%c%02x", i == 0 ? '(' : ' ', binary[i]);
  8. }
  9. printf(")\n");
  10. }
  11. void IZ_AppHandleNetworkingInboundTextEvents(struct IZ_App* app, const char* text, size_t len) {
  12. printf("%lu: String(%s)\n", IZ_AppGetTicks(app), text);
  13. }
  14. void IZ_AppHandleOutboundNetworking(struct IZ_App* app) {
  15. // TODO implement queueing of messages
  16. IZ_NetClientState* net_state = IZ_AppGetNetState(app);
  17. IZ_InputState* input_state = IZ_AppGetInputState(app);
  18. u8 player_index;
  19. for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
  20. if (net_state->action[player_index] != input_state->action[player_index]) {
  21. u8 player_actions_count = 1;
  22. IZ_AppActionSyncMessage* msg = IZ_malloc(
  23. sizeof(IZ_AppMessageHeader)
  24. + sizeof(u8)
  25. + (sizeof(IZ_AppPlayerActionSyncMessage) * player_actions_count)
  26. );
  27. msg->header.client_elapsed_time = IZ_AppGetTicks(app);
  28. msg->header.message_kind = IZ_MESSAGE_KIND_ACTION_SYNC;
  29. msg->action.player_actions_count = player_actions_count;
  30. msg->action.player[0].index = player_index;
  31. msg->action.player[0].value = input_state->action[player_index];
  32. msg->action.player[0].state = 0;
  33. IZ_NetClientSendBinaryMessage(
  34. net_state,
  35. msg,
  36. sizeof(*msg)
  37. );
  38. IZ_free(msg);
  39. }
  40. net_state->action[player_index] = input_state->action[player_index];
  41. }
  42. }
  43. void IZ_WSClientAttemptConnect(struct lws_sorted_usec_list *sul) {
  44. IZ_WSClientVHostData* vhd = lws_container_of(sul, IZ_WSClientVHostData, sul);
  45. vhd->i.context = vhd->context;
  46. vhd->i.port = *vhd->port;
  47. vhd->i.address = vhd->address;
  48. vhd->i.path = vhd->path;
  49. vhd->i.host = vhd->i.address;
  50. vhd->i.origin = vhd->i.address;
  51. vhd->i.ssl_connection = 0;
  52. vhd->i.protocol = NETWORK_PROTOCOL;
  53. vhd->i.pwsi = &vhd->client_wsi;
  54. struct IZ_App* app = (struct IZ_App*) vhd->app;
  55. IZ_NetClientState* net_state = IZ_AppGetNetState(app);
  56. net_state->status = IZ_NET_CLIENT_STATUS_CONNECTING;
  57. if (lws_client_connect_via_info(&vhd->i)) {
  58. return;
  59. }
  60. lws_sul_schedule(
  61. vhd->context,
  62. 0,
  63. &vhd->sul,
  64. IZ_WSClientAttemptConnect,
  65. 10 * LWS_US_PER_SEC
  66. );
  67. }
  68. IZ_ProcedureResult IZ_WSClientProtocolInitialize(struct lws* wsi, void* in) {
  69. const struct lws_protocols* protocols = lws_get_protocol(wsi);
  70. struct lws_vhost* vhost = lws_get_vhost(wsi);
  71. IZ_WSClientVHostData* vhd_instance = (IZ_WSClientVHostData*) lws_protocol_vh_priv_get(vhost,protocols);
  72. IZ_WSClientVHostData** vhd = &vhd_instance;
  73. *vhd = lws_protocol_vh_priv_zalloc(vhost, protocols, sizeof(IZ_WSClientVHostData));
  74. (*vhd)->ring = lws_ring_create(
  75. sizeof(IZ_WebsocketMessage),
  76. RING_COUNT,
  77. IZ_WebsocketDestroyMessage
  78. );
  79. if (!(*vhd)->ring) {
  80. return -1;
  81. }
  82. (*vhd)->context = lws_get_context(wsi);
  83. (*vhd)->protocol = protocols;
  84. (*vhd)->vhost = vhost;
  85. (*vhd)->port = (u16*) lws_pvo_search(
  86. (const struct lws_protocol_vhost_options *)in,
  87. "port"
  88. )->value;
  89. (*vhd)->address = lws_pvo_search(
  90. (const struct lws_protocol_vhost_options *)in,
  91. "address"
  92. )->value;
  93. (*vhd)->path = lws_pvo_search(
  94. (const struct lws_protocol_vhost_options *)in,
  95. "path"
  96. )->value;
  97. (*vhd)->app = lws_pvo_search(
  98. (const struct lws_protocol_vhost_options *)in,
  99. "app"
  100. )->value;
  101. IZ_WSClientAttemptConnect(&(*vhd)->sul);
  102. return 0;
  103. }
  104. void IZ_WSClientProtocolTeardown(struct lws* wsi) {
  105. IZ_WSClientVHostData* vhd = (IZ_WSClientVHostData*) lws_protocol_vh_priv_get(
  106. lws_get_vhost(wsi),
  107. lws_get_protocol(wsi)
  108. );
  109. if (vhd->ring) {
  110. lws_ring_destroy(vhd->ring);
  111. }
  112. lws_sul_cancel(&vhd->sul);
  113. struct IZ_App* app = (struct IZ_App*) vhd->app;
  114. IZ_NetClientState* net_state = IZ_AppGetNetState(app);
  115. net_state->status = IZ_NET_CLIENT_STATUS_PRISTINE;
  116. }
  117. IZ_ProcedureResult IZ_WSClientConnectionError(struct lws* wsi, void* in) {
  118. lwsl_err("CLIENT_CONNECTION_ERROR: %s\n", in ? (char *)in : "(null)");
  119. IZ_WSClientVHostData* vhd = (IZ_WSClientVHostData*) lws_protocol_vh_priv_get(
  120. lws_get_vhost(wsi),
  121. lws_get_protocol(wsi)
  122. );
  123. if (!vhd) {
  124. return -1;
  125. }
  126. struct IZ_App* app = (struct IZ_App*) vhd->app;
  127. IZ_AppBindConnection(app, NULL);
  128. vhd->client_wsi = NULL;
  129. IZ_NetClientState* net_state = IZ_AppGetNetState(app);
  130. if (net_state->retries == net_state->config.max_reconnect_retries) {
  131. lwsl_err("Max number of retries reached!\n");
  132. net_state->status = IZ_NET_CLIENT_STATUS_PRISTINE;
  133. return -1;
  134. }
  135. net_state->status = IZ_NET_CLIENT_STATUS_ERROR;
  136. net_state->retries += 1;
  137. lws_sul_schedule(
  138. vhd->context,
  139. 0,
  140. &vhd->sul,
  141. IZ_WSClientAttemptConnect,
  142. net_state->config.reconnect_interval_secs * LWS_US_PER_SEC
  143. );
  144. return 0;
  145. }
  146. IZ_ProcedureResult IZ_WSClientOnOpen(struct lws* wsi, IZ_WSClientSessionData* pss) {
  147. IZ_WSClientVHostData* vhd = (IZ_WSClientVHostData*) lws_protocol_vh_priv_get(
  148. lws_get_vhost(wsi),
  149. lws_get_protocol(wsi)
  150. );
  151. struct IZ_App* app = (struct IZ_App*) vhd->app;
  152. IZ_NetClientState* net_state = IZ_AppGetNetState(app);
  153. pss->ring = lws_ring_create(sizeof(IZ_WebsocketMessage), RING_COUNT,IZ_WebsocketDestroyMessage);
  154. if (!pss->ring) {
  155. net_state->status = IZ_NET_CLIENT_STATUS_ERROR;
  156. return -1;
  157. }
  158. IZ_AppBindConnection(app, wsi);
  159. net_state->status = IZ_NET_CLIENT_STATUS_CONNECTED;
  160. net_state->retries = 0;
  161. pss->tail = 0;
  162. return 0;
  163. }
  164. void IZ_WSClientOnClose(struct lws* wsi) {
  165. IZ_WSClientVHostData* vhd = (IZ_WSClientVHostData*) lws_protocol_vh_priv_get(
  166. lws_get_vhost(wsi),
  167. lws_get_protocol(wsi)
  168. );
  169. struct IZ_App* app = (struct IZ_App*) vhd->app;
  170. IZ_AppBindConnection(app, NULL);
  171. vhd->client_wsi = NULL;
  172. lws_sul_schedule(
  173. vhd->context,
  174. 0,
  175. &vhd->sul,
  176. IZ_WSClientAttemptConnect,
  177. LWS_US_PER_SEC
  178. );
  179. }
  180. IZ_ProcedureResult IZ_WSClientWritable(struct lws* wsi) {
  181. IZ_WSClientVHostData* vhd = (IZ_WSClientVHostData*) lws_protocol_vh_priv_get(
  182. lws_get_vhost(wsi),
  183. lws_get_protocol(wsi)
  184. );
  185. const IZ_WebsocketMessage* pmsg = lws_ring_get_element(vhd->ring, &vhd->tail);
  186. if (!pmsg) {
  187. return 0;
  188. }
  189. /* notice we allowed for LWS_PRE in the payload already */
  190. i32 m = lws_write(
  191. wsi,
  192. ((unsigned char*) pmsg->payload) + LWS_PRE,
  193. pmsg->len,
  194. pmsg->binary ? LWS_WRITE_BINARY : LWS_WRITE_TEXT
  195. );
  196. if (m < (i32)pmsg->len) {
  197. lwsl_err("ERROR %d writing to ws socket\n", m);
  198. return -1;
  199. }
  200. lws_ring_consume_single_tail(vhd->ring, &vhd->tail, 1);
  201. /* more to do for us? */
  202. if (lws_ring_get_element(vhd->ring, &vhd->tail)) {
  203. /* come back as soon as we can write more */
  204. lws_callback_on_writable(wsi);
  205. }
  206. return 0;
  207. }
  208. void IZ_WSClientOnReceive(struct lws* wsi, IZ_WSClientSessionData* pss, void* in, size_t len) {
  209. i32 n = (i32) lws_ring_get_count_free_elements(pss->ring);
  210. if (!n) {
  211. lwsl_user("dropping!\n");
  212. return;
  213. }
  214. lwsl_user("LWS_CALLBACK_CLIENT_RECEIVE: %4d (rpp %5d, first %d, last %d, bin %d)\n",
  215. (int)len, (int)lws_remaining_packet_payload(wsi),
  216. lws_is_first_fragment(wsi),
  217. lws_is_final_fragment(wsi),
  218. lws_frame_is_binary(wsi));
  219. // lwsl_hexdump_notice(in, len);
  220. IZ_WebsocketMessage amsg;
  221. i32 result = (
  222. lws_frame_is_binary(wsi)
  223. ? IZ_WebsocketCreateBinaryMessage(wsi, &amsg, in, len)
  224. : IZ_WebsocketCreateTextMessage(wsi, &amsg, in, len)
  225. );
  226. if (result < 0) {
  227. lwsl_user("OOM: dropping\n");
  228. return;
  229. }
  230. IZ_WSClientVHostData* vhd = (IZ_WSClientVHostData*) lws_protocol_vh_priv_get(
  231. lws_get_vhost(wsi),
  232. lws_get_protocol(wsi)
  233. );
  234. struct IZ_App* app = (struct IZ_App*) vhd->app;
  235. if (amsg.binary) {
  236. IZ_AppHandleNetworkingInboundBinaryEvents(app, in, len);
  237. } else {
  238. IZ_AppHandleNetworkingInboundTextEvents(app, in, len);
  239. }
  240. if (!lws_ring_insert(pss->ring, &amsg, 1)) {
  241. IZ_WebsocketDestroyMessage(&amsg);
  242. lwsl_user("dropping!\n");
  243. return;
  244. }
  245. lws_ring_consume_single_tail(pss->ring, &pss->tail, 1);
  246. lws_callback_on_writable(wsi);
  247. if (!pss->flow_controlled && n < 3) {
  248. pss->flow_controlled = true;
  249. lws_rx_flow_control(wsi, 0);
  250. }
  251. }
  252. IZ_ProcedureResult IZ_AppRunNetworkingThread(struct IZ_App* app) {
  253. IZ_NetClientState* net_state = IZ_AppGetNetState(app);
  254. if (IZ_WSClientInitialize(&net_state->binding, net_state->params)) {
  255. return -1;
  256. }
  257. i32 result = 0;
  258. while (true) {
  259. if (IZ_WSClientHandle(&net_state->binding)) {
  260. result = -1;
  261. break;
  262. }
  263. if (net_state->binding.interrupted) {
  264. break;
  265. }
  266. }
  267. IZ_WSClientTeardown(&net_state->binding);
  268. return result;
  269. }