lws_server.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*************************************************************************/
  2. /* lws_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT WEBSOCKET MODULE */
  6. /* https://github.com/LudiDorici/godot-websocket */
  7. /*************************************************************************/
  8. /* Copyright (c) 2017 Ludi Dorici, di Alessandrelli Fabio */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef JAVASCRIPT_ENABLED
  30. #include "lws_server.h"
  31. #include "core/os/os.h"
  32. Error LWSServer::listen(int p_port, PoolVector<String> p_protocols, bool gd_mp_api) {
  33. ERR_FAIL_COND_V(context != NULL, FAILED);
  34. _is_multiplayer = gd_mp_api;
  35. struct lws_context_creation_info info;
  36. memset(&info, 0, sizeof info);
  37. if (p_protocols.size() == 0) // default to binary protocol
  38. p_protocols.append(String("binary"));
  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::_handle_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
  60. LWSPeer::PeerData *peer_data = (LWSPeer::PeerData *)user;
  61. switch (reason) {
  62. case LWS_CALLBACK_HTTP:
  63. // no http for now
  64. // closing immediately returning -1;
  65. return -1;
  66. case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
  67. // check header here?
  68. break;
  69. case LWS_CALLBACK_ESTABLISHED: {
  70. int32_t id = _gen_unique_id();
  71. Ref<LWSPeer> peer = Ref<LWSPeer>(memnew(LWSPeer));
  72. peer->set_wsi(wsi);
  73. _peer_map[id] = peer;
  74. peer_data->peer_id = id;
  75. peer_data->in_size = 0;
  76. peer_data->in_count = 0;
  77. peer_data->out_count = 0;
  78. peer_data->rbw.resize(16);
  79. peer_data->rbr.resize(16);
  80. peer_data->force_close = false;
  81. _on_connect(id, lws_get_protocol(wsi)->name);
  82. break;
  83. }
  84. case LWS_CALLBACK_CLOSED: {
  85. if (peer_data == NULL)
  86. return 0;
  87. int32_t id = peer_data->peer_id;
  88. if (_peer_map.has(id)) {
  89. _peer_map[id]->close();
  90. _peer_map.erase(id);
  91. }
  92. peer_data->in_count = 0;
  93. peer_data->out_count = 0;
  94. peer_data->rbr.resize(0);
  95. peer_data->rbw.resize(0);
  96. _on_disconnect(id);
  97. return 0; // we can end here
  98. }
  99. case LWS_CALLBACK_RECEIVE: {
  100. int32_t id = peer_data->peer_id;
  101. if (_peer_map.has(id)) {
  102. static_cast<Ref<LWSPeer> >(_peer_map[id])->read_wsi(in, len);
  103. if (_peer_map[id]->get_available_packet_count() > 0)
  104. _on_peer_packet(id);
  105. }
  106. break;
  107. }
  108. case LWS_CALLBACK_SERVER_WRITEABLE: {
  109. if (peer_data->force_close)
  110. return -1;
  111. int id = peer_data->peer_id;
  112. if (_peer_map.has(id))
  113. static_cast<Ref<LWSPeer> >(_peer_map[id])->write_wsi();
  114. break;
  115. }
  116. default:
  117. break;
  118. }
  119. return 0;
  120. }
  121. void LWSServer::stop() {
  122. if (context == NULL)
  123. return;
  124. _peer_map.clear();
  125. destroy_context();
  126. context = NULL;
  127. }
  128. bool LWSServer::has_peer(int p_id) const {
  129. return _peer_map.has(p_id);
  130. }
  131. Ref<WebSocketPeer> LWSServer::get_peer(int p_id) const {
  132. ERR_FAIL_COND_V(!has_peer(p_id), NULL);
  133. return _peer_map[p_id];
  134. }
  135. LWSServer::LWSServer() {
  136. context = NULL;
  137. _lws_ref = NULL;
  138. }
  139. LWSServer::~LWSServer() {
  140. invalidate_lws_ref(); // we do not want any more callbacks
  141. stop();
  142. }
  143. #endif // JAVASCRIPT_ENABLED