lws_server.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. if (p_protocols.size() == 0) // default to binary protocol
  39. p_protocols.append(String("binary"));
  40. // Prepare lws protocol structs
  41. _lws_make_protocols(this, &LWSServer::_lws_gd_callback, p_protocols, &_lws_ref);
  42. info.port = p_port;
  43. info.user = _lws_ref;
  44. info.protocols = _lws_ref->lws_structs;
  45. info.gid = -1;
  46. info.uid = -1;
  47. //info.ws_ping_pong_interval = 5;
  48. context = lws_create_context(&info);
  49. if (context == NULL) {
  50. _lws_free_ref(_lws_ref);
  51. _lws_ref = NULL;
  52. ERR_EXPLAIN("Unable to create LWS context");
  53. ERR_FAIL_V(FAILED);
  54. }
  55. return OK;
  56. }
  57. bool LWSServer::is_listening() const {
  58. return context != NULL;
  59. }
  60. int LWSServer::_handle_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
  61. LWSPeer::PeerData *peer_data = (LWSPeer::PeerData *)user;
  62. switch (reason) {
  63. case LWS_CALLBACK_HTTP:
  64. // no http for now
  65. // closing immediately returning -1;
  66. return -1;
  67. case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
  68. // check header here?
  69. break;
  70. case LWS_CALLBACK_ESTABLISHED: {
  71. int32_t id = _gen_unique_id();
  72. Ref<LWSPeer> peer = Ref<LWSPeer>(memnew(LWSPeer));
  73. peer->set_wsi(wsi);
  74. _peer_map[id] = peer;
  75. peer_data->peer_id = id;
  76. peer_data->in_size = 0;
  77. peer_data->in_count = 0;
  78. peer_data->out_count = 0;
  79. peer_data->rbw.resize(16);
  80. peer_data->rbr.resize(16);
  81. peer_data->force_close = false;
  82. _on_connect(id, lws_get_protocol(wsi)->name);
  83. break;
  84. }
  85. case LWS_CALLBACK_CLOSED: {
  86. if (peer_data == NULL)
  87. return 0;
  88. int32_t id = peer_data->peer_id;
  89. if (_peer_map.has(id)) {
  90. _peer_map[id]->close();
  91. _peer_map.erase(id);
  92. }
  93. peer_data->in_count = 0;
  94. peer_data->out_count = 0;
  95. peer_data->rbr.resize(0);
  96. peer_data->rbw.resize(0);
  97. _on_disconnect(id);
  98. return 0; // we can end here
  99. }
  100. case LWS_CALLBACK_RECEIVE: {
  101. int32_t id = peer_data->peer_id;
  102. if (_peer_map.has(id)) {
  103. static_cast<Ref<LWSPeer> >(_peer_map[id])->read_wsi(in, len);
  104. if (_peer_map[id]->get_available_packet_count() > 0)
  105. _on_peer_packet(id);
  106. }
  107. break;
  108. }
  109. case LWS_CALLBACK_SERVER_WRITEABLE: {
  110. if (peer_data->force_close)
  111. return -1;
  112. int id = peer_data->peer_id;
  113. if (_peer_map.has(id))
  114. static_cast<Ref<LWSPeer> >(_peer_map[id])->write_wsi();
  115. break;
  116. }
  117. default:
  118. break;
  119. }
  120. return 0;
  121. }
  122. void LWSServer::stop() {
  123. if (context == NULL)
  124. return;
  125. _peer_map.clear();
  126. destroy_context();
  127. context = NULL;
  128. }
  129. bool LWSServer::has_peer(int p_id) const {
  130. return _peer_map.has(p_id);
  131. }
  132. Ref<WebSocketPeer> LWSServer::get_peer(int p_id) const {
  133. ERR_FAIL_COND_V(!has_peer(p_id), NULL);
  134. return _peer_map[p_id];
  135. }
  136. LWSServer::LWSServer() {
  137. context = NULL;
  138. _lws_ref = NULL;
  139. }
  140. LWSServer::~LWSServer() {
  141. invalidate_lws_ref(); // we do not want any more callbacks
  142. stop();
  143. }
  144. #endif // JAVASCRIPT_ENABLED