lws_server.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*************************************************************************/
  2. /* lws_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef JAVASCRIPT_ENABLED
  31. #include "lws_server.h"
  32. #include "core/os/os.h"
  33. #include "core/project_settings.h"
  34. Error LWSServer::listen(int p_port, PoolVector<String> p_protocols, bool gd_mp_api) {
  35. ERR_FAIL_COND_V(context != NULL, FAILED);
  36. _is_multiplayer = gd_mp_api;
  37. struct lws_context_creation_info info;
  38. memset(&info, 0, sizeof info);
  39. // Prepare lws protocol structs
  40. _lws_make_protocols(this, &LWSServer::_lws_gd_callback, p_protocols, &_lws_ref);
  41. info.port = p_port;
  42. info.user = _lws_ref;
  43. info.protocols = _lws_ref->lws_structs;
  44. info.gid = -1;
  45. info.uid = -1;
  46. //info.ws_ping_pong_interval = 5;
  47. context = lws_create_context(&info);
  48. if (context == NULL) {
  49. _lws_free_ref(_lws_ref);
  50. _lws_ref = NULL;
  51. ERR_EXPLAIN("Unable to create LWS context");
  52. ERR_FAIL_V(FAILED);
  53. }
  54. return OK;
  55. }
  56. bool LWSServer::is_listening() const {
  57. return context != NULL;
  58. }
  59. int LWSServer::get_max_packet_size() const {
  60. return (1 << _out_buf_size) - PROTO_SIZE;
  61. }
  62. int LWSServer::_handle_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
  63. LWSPeer::PeerData *peer_data = (LWSPeer::PeerData *)user;
  64. switch (reason) {
  65. case LWS_CALLBACK_HTTP:
  66. // no http for now
  67. // closing immediately returning -1;
  68. return -1;
  69. case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
  70. // check header here?
  71. break;
  72. case LWS_CALLBACK_ESTABLISHED: {
  73. int32_t id = _gen_unique_id();
  74. Ref<LWSPeer> peer = Ref<LWSPeer>(memnew(LWSPeer));
  75. peer->set_wsi(wsi, _in_buf_size, _in_pkt_size, _out_buf_size, _out_pkt_size);
  76. _peer_map[id] = peer;
  77. peer_data->peer_id = id;
  78. peer_data->force_close = false;
  79. peer_data->clean_close = false;
  80. _on_connect(id, lws_get_protocol(wsi)->name);
  81. break;
  82. }
  83. case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: {
  84. if (peer_data == NULL)
  85. return 0;
  86. int32_t id = peer_data->peer_id;
  87. if (_peer_map.has(id)) {
  88. int code;
  89. Ref<LWSPeer> peer = _peer_map[id];
  90. String reason = peer->get_close_reason(in, len, code);
  91. peer_data->clean_close = true;
  92. _on_close_request(id, code, reason);
  93. }
  94. return 0;
  95. }
  96. case LWS_CALLBACK_CLOSED: {
  97. if (peer_data == NULL)
  98. return 0;
  99. int32_t id = peer_data->peer_id;
  100. bool clean = peer_data->clean_close;
  101. if (_peer_map.has(id)) {
  102. _peer_map[id]->close();
  103. _peer_map.erase(id);
  104. }
  105. _on_disconnect(id, clean);
  106. return 0; // we can end here
  107. }
  108. case LWS_CALLBACK_RECEIVE: {
  109. int32_t id = peer_data->peer_id;
  110. if (_peer_map.has(id)) {
  111. static_cast<Ref<LWSPeer> >(_peer_map[id])->read_wsi(in, len);
  112. if (_peer_map[id]->get_available_packet_count() > 0)
  113. _on_peer_packet(id);
  114. }
  115. break;
  116. }
  117. case LWS_CALLBACK_SERVER_WRITEABLE: {
  118. int id = peer_data->peer_id;
  119. if (peer_data->force_close) {
  120. if (_peer_map.has(id)) {
  121. Ref<LWSPeer> peer = _peer_map[id];
  122. peer->send_close_status(wsi);
  123. }
  124. return -1;
  125. }
  126. if (_peer_map.has(id))
  127. static_cast<Ref<LWSPeer> >(_peer_map[id])->write_wsi();
  128. break;
  129. }
  130. default:
  131. break;
  132. }
  133. return 0;
  134. }
  135. void LWSServer::stop() {
  136. if (context == NULL)
  137. return;
  138. _peer_map.clear();
  139. destroy_context();
  140. context = NULL;
  141. }
  142. bool LWSServer::has_peer(int p_id) const {
  143. return _peer_map.has(p_id);
  144. }
  145. Ref<WebSocketPeer> LWSServer::get_peer(int p_id) const {
  146. ERR_FAIL_COND_V(!has_peer(p_id), NULL);
  147. return _peer_map[p_id];
  148. }
  149. IP_Address LWSServer::get_peer_address(int p_peer_id) const {
  150. ERR_FAIL_COND_V(!has_peer(p_peer_id), IP_Address());
  151. return _peer_map[p_peer_id]->get_connected_host();
  152. }
  153. int LWSServer::get_peer_port(int p_peer_id) const {
  154. ERR_FAIL_COND_V(!has_peer(p_peer_id), 0);
  155. return _peer_map[p_peer_id]->get_connected_port();
  156. }
  157. void LWSServer::disconnect_peer(int p_peer_id, int p_code, String p_reason) {
  158. ERR_FAIL_COND(!has_peer(p_peer_id));
  159. get_peer(p_peer_id)->close(p_code, p_reason);
  160. }
  161. LWSServer::LWSServer() {
  162. _in_buf_size = nearest_shift((int)GLOBAL_GET(WSS_IN_BUF) - 1) + 10;
  163. _in_pkt_size = nearest_shift((int)GLOBAL_GET(WSS_IN_PKT) - 1);
  164. _out_buf_size = nearest_shift((int)GLOBAL_GET(WSS_OUT_BUF) - 1) + 10;
  165. _out_pkt_size = nearest_shift((int)GLOBAL_GET(WSS_OUT_PKT) - 1);
  166. context = NULL;
  167. _lws_ref = NULL;
  168. }
  169. LWSServer::~LWSServer() {
  170. invalidate_lws_ref(); // we do not want any more callbacks
  171. stop();
  172. }
  173. #endif // JAVASCRIPT_ENABLED