lws_server.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*************************************************************************/
  2. /* lws_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. Error LWSServer::listen(int p_port, PoolVector<String> p_protocols, bool gd_mp_api) {
  34. ERR_FAIL_COND_V(context != NULL, FAILED);
  35. _is_multiplayer = gd_mp_api;
  36. struct lws_context_creation_info info;
  37. memset(&info, 0, sizeof info);
  38. // Prepare lws protocol structs
  39. _lws_make_protocols(this, &LWSServer::_lws_gd_callback, p_protocols, &_lws_ref);
  40. info.port = p_port;
  41. info.user = _lws_ref;
  42. info.protocols = _lws_ref->lws_structs;
  43. info.gid = -1;
  44. info.uid = -1;
  45. //info.ws_ping_pong_interval = 5;
  46. context = lws_create_context(&info);
  47. if (context == NULL) {
  48. _lws_free_ref(_lws_ref);
  49. _lws_ref = NULL;
  50. ERR_EXPLAIN("Unable to create LWS context");
  51. ERR_FAIL_V(FAILED);
  52. }
  53. return OK;
  54. }
  55. bool LWSServer::is_listening() const {
  56. return context != NULL;
  57. }
  58. int LWSServer::_handle_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
  59. LWSPeer::PeerData *peer_data = (LWSPeer::PeerData *)user;
  60. switch (reason) {
  61. case LWS_CALLBACK_HTTP:
  62. // no http for now
  63. // closing immediately returning -1;
  64. return -1;
  65. case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
  66. // check header here?
  67. break;
  68. case LWS_CALLBACK_ESTABLISHED: {
  69. int32_t id = _gen_unique_id();
  70. Ref<LWSPeer> peer = Ref<LWSPeer>(memnew(LWSPeer));
  71. peer->set_wsi(wsi);
  72. _peer_map[id] = peer;
  73. peer_data->peer_id = id;
  74. peer_data->force_close = false;
  75. _on_connect(id, lws_get_protocol(wsi)->name);
  76. break;
  77. }
  78. case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: {
  79. if (peer_data == NULL)
  80. return 0;
  81. int32_t id = peer_data->peer_id;
  82. if (_peer_map.has(id)) {
  83. int code;
  84. Ref<LWSPeer> peer = _peer_map[id];
  85. String reason = peer->get_close_reason(in, len, code);
  86. _on_close_request(id, code, reason);
  87. }
  88. return 0;
  89. }
  90. case LWS_CALLBACK_CLOSED: {
  91. if (peer_data == NULL)
  92. return 0;
  93. int32_t id = peer_data->peer_id;
  94. if (_peer_map.has(id)) {
  95. _peer_map[id]->close();
  96. _peer_map.erase(id);
  97. }
  98. _on_disconnect(id);
  99. return 0; // we can end here
  100. }
  101. case LWS_CALLBACK_RECEIVE: {
  102. int32_t id = peer_data->peer_id;
  103. if (_peer_map.has(id)) {
  104. static_cast<Ref<LWSPeer> >(_peer_map[id])->read_wsi(in, len);
  105. if (_peer_map[id]->get_available_packet_count() > 0)
  106. _on_peer_packet(id);
  107. }
  108. break;
  109. }
  110. case LWS_CALLBACK_SERVER_WRITEABLE: {
  111. int id = peer_data->peer_id;
  112. if (peer_data->force_close) {
  113. if (_peer_map.has(id)) {
  114. Ref<LWSPeer> peer = _peer_map[id];
  115. peer->send_close_status(wsi);
  116. }
  117. return -1;
  118. }
  119. if (_peer_map.has(id))
  120. static_cast<Ref<LWSPeer> >(_peer_map[id])->write_wsi();
  121. break;
  122. }
  123. default:
  124. break;
  125. }
  126. return 0;
  127. }
  128. void LWSServer::stop() {
  129. if (context == NULL)
  130. return;
  131. _peer_map.clear();
  132. destroy_context();
  133. context = NULL;
  134. }
  135. bool LWSServer::has_peer(int p_id) const {
  136. return _peer_map.has(p_id);
  137. }
  138. Ref<WebSocketPeer> LWSServer::get_peer(int p_id) const {
  139. ERR_FAIL_COND_V(!has_peer(p_id), NULL);
  140. return _peer_map[p_id];
  141. }
  142. IP_Address LWSServer::get_peer_address(int p_peer_id) const {
  143. ERR_FAIL_COND_V(!has_peer(p_peer_id), IP_Address());
  144. return _peer_map[p_peer_id]->get_connected_host();
  145. }
  146. int LWSServer::get_peer_port(int p_peer_id) const {
  147. ERR_FAIL_COND_V(!has_peer(p_peer_id), 0);
  148. return _peer_map[p_peer_id]->get_connected_port();
  149. }
  150. void LWSServer::disconnect_peer(int p_peer_id, int p_code, String p_reason) {
  151. ERR_FAIL_COND(!has_peer(p_peer_id));
  152. get_peer(p_peer_id)->close(p_code, p_reason);
  153. }
  154. LWSServer::LWSServer() {
  155. context = NULL;
  156. _lws_ref = NULL;
  157. }
  158. LWSServer::~LWSServer() {
  159. invalidate_lws_ref(); // we do not want any more callbacks
  160. stop();
  161. }
  162. #endif // JAVASCRIPT_ENABLED