websocket_server.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**************************************************************************/
  2. /* websocket_server.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "websocket_server.h"
  31. GDCINULL(WebSocketServer);
  32. WebSocketServer::WebSocketServer() {
  33. _peer_id = 1;
  34. bind_ip = IP_Address("*");
  35. }
  36. WebSocketServer::~WebSocketServer() {
  37. }
  38. void WebSocketServer::_bind_methods() {
  39. ClassDB::bind_method(D_METHOD("is_listening"), &WebSocketServer::is_listening);
  40. ClassDB::bind_method(D_METHOD("set_extra_headers", "headers"), &WebSocketServer::set_extra_headers, DEFVAL(Vector<String>()));
  41. ClassDB::bind_method(D_METHOD("listen", "port", "protocols", "gd_mp_api"), &WebSocketServer::listen, DEFVAL(Vector<String>()), DEFVAL(false));
  42. ClassDB::bind_method(D_METHOD("stop"), &WebSocketServer::stop);
  43. ClassDB::bind_method(D_METHOD("has_peer", "id"), &WebSocketServer::has_peer);
  44. ClassDB::bind_method(D_METHOD("get_peer_address", "id"), &WebSocketServer::get_peer_address);
  45. ClassDB::bind_method(D_METHOD("get_peer_port", "id"), &WebSocketServer::get_peer_port);
  46. ClassDB::bind_method(D_METHOD("disconnect_peer", "id", "code", "reason"), &WebSocketServer::disconnect_peer, DEFVAL(1000), DEFVAL(""));
  47. ClassDB::bind_method(D_METHOD("get_bind_ip"), &WebSocketServer::get_bind_ip);
  48. ClassDB::bind_method(D_METHOD("set_bind_ip", "ip"), &WebSocketServer::set_bind_ip);
  49. ADD_PROPERTY(PropertyInfo(Variant::STRING, "bind_ip"), "set_bind_ip", "get_bind_ip");
  50. ClassDB::bind_method(D_METHOD("get_private_key"), &WebSocketServer::get_private_key);
  51. ClassDB::bind_method(D_METHOD("set_private_key", "key"), &WebSocketServer::set_private_key);
  52. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "private_key", PROPERTY_HINT_RESOURCE_TYPE, "CryptoKey", 0), "set_private_key", "get_private_key");
  53. ClassDB::bind_method(D_METHOD("get_ssl_certificate"), &WebSocketServer::get_ssl_certificate);
  54. ClassDB::bind_method(D_METHOD("set_ssl_certificate", "certificate"), &WebSocketServer::set_ssl_certificate);
  55. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "ssl_certificate", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", 0), "set_ssl_certificate", "get_ssl_certificate");
  56. ClassDB::bind_method(D_METHOD("get_ca_chain"), &WebSocketServer::get_ca_chain);
  57. ClassDB::bind_method(D_METHOD("set_ca_chain", "ca_chain"), &WebSocketServer::set_ca_chain);
  58. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "ca_chain", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", 0), "set_ca_chain", "get_ca_chain");
  59. ClassDB::bind_method(D_METHOD("get_handshake_timeout"), &WebSocketServer::get_handshake_timeout);
  60. ClassDB::bind_method(D_METHOD("set_handshake_timeout", "timeout"), &WebSocketServer::set_handshake_timeout);
  61. ADD_PROPERTY(PropertyInfo(Variant::REAL, "handshake_timeout"), "set_handshake_timeout", "get_handshake_timeout");
  62. ADD_SIGNAL(MethodInfo("client_close_request", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::INT, "code"), PropertyInfo(Variant::STRING, "reason")));
  63. ADD_SIGNAL(MethodInfo("client_disconnected", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::BOOL, "was_clean_close")));
  64. ADD_SIGNAL(MethodInfo("client_connected", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "protocol")));
  65. ADD_SIGNAL(MethodInfo("data_received", PropertyInfo(Variant::INT, "id")));
  66. }
  67. IP_Address WebSocketServer::get_bind_ip() const {
  68. return bind_ip;
  69. }
  70. void WebSocketServer::set_bind_ip(const IP_Address &p_bind_ip) {
  71. ERR_FAIL_COND(is_listening());
  72. ERR_FAIL_COND(!p_bind_ip.is_valid() && !p_bind_ip.is_wildcard());
  73. bind_ip = p_bind_ip;
  74. }
  75. Ref<CryptoKey> WebSocketServer::get_private_key() const {
  76. return private_key;
  77. }
  78. void WebSocketServer::set_private_key(Ref<CryptoKey> p_key) {
  79. ERR_FAIL_COND(is_listening());
  80. private_key = p_key;
  81. }
  82. Ref<X509Certificate> WebSocketServer::get_ssl_certificate() const {
  83. return ssl_cert;
  84. }
  85. void WebSocketServer::set_ssl_certificate(Ref<X509Certificate> p_cert) {
  86. ERR_FAIL_COND(is_listening());
  87. ssl_cert = p_cert;
  88. }
  89. Ref<X509Certificate> WebSocketServer::get_ca_chain() const {
  90. return ca_chain;
  91. }
  92. void WebSocketServer::set_ca_chain(Ref<X509Certificate> p_ca_chain) {
  93. ERR_FAIL_COND(is_listening());
  94. ca_chain = p_ca_chain;
  95. }
  96. float WebSocketServer::get_handshake_timeout() const {
  97. return handshake_timeout / 1000.0;
  98. }
  99. void WebSocketServer::set_handshake_timeout(float p_timeout) {
  100. ERR_FAIL_COND(p_timeout <= 0.0);
  101. handshake_timeout = p_timeout * 1000;
  102. }
  103. NetworkedMultiplayerPeer::ConnectionStatus WebSocketServer::get_connection_status() const {
  104. if (is_listening()) {
  105. return CONNECTION_CONNECTED;
  106. }
  107. return CONNECTION_DISCONNECTED;
  108. }
  109. bool WebSocketServer::is_server() const {
  110. return true;
  111. }
  112. void WebSocketServer::_on_peer_packet(int32_t p_peer_id) {
  113. if (_is_multiplayer) {
  114. _process_multiplayer(get_peer(p_peer_id), p_peer_id);
  115. } else {
  116. emit_signal("data_received", p_peer_id);
  117. }
  118. }
  119. void WebSocketServer::_on_connect(int32_t p_peer_id, String p_protocol) {
  120. if (_is_multiplayer) {
  121. // Send add to clients
  122. _send_add(p_peer_id);
  123. emit_signal("peer_connected", p_peer_id);
  124. } else {
  125. emit_signal("client_connected", p_peer_id, p_protocol);
  126. }
  127. }
  128. void WebSocketServer::_on_disconnect(int32_t p_peer_id, bool p_was_clean) {
  129. if (_is_multiplayer) {
  130. // Send delete to clients
  131. _send_del(p_peer_id);
  132. emit_signal("peer_disconnected", p_peer_id);
  133. } else {
  134. emit_signal("client_disconnected", p_peer_id, p_was_clean);
  135. }
  136. }
  137. void WebSocketServer::_on_close_request(int32_t p_peer_id, int p_code, String p_reason) {
  138. emit_signal("client_close_request", p_peer_id, p_code, p_reason);
  139. }