websocket_peer.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**************************************************************************/
  2. /* websocket_peer.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_peer.h"
  31. WebSocketPeer *(*WebSocketPeer::_create)(bool p_notify_postinitialize) = nullptr;
  32. WebSocketPeer::WebSocketPeer() {
  33. }
  34. WebSocketPeer::~WebSocketPeer() {
  35. }
  36. void WebSocketPeer::_bind_methods() {
  37. ClassDB::bind_method(D_METHOD("connect_to_url", "url", "tls_client_options"), &WebSocketPeer::connect_to_url, DEFVAL(Ref<TLSOptions>()));
  38. ClassDB::bind_method(D_METHOD("accept_stream", "stream"), &WebSocketPeer::accept_stream);
  39. ClassDB::bind_method(D_METHOD("send", "message", "write_mode"), &WebSocketPeer::_send_bind, DEFVAL(WRITE_MODE_BINARY));
  40. ClassDB::bind_method(D_METHOD("send_text", "message"), &WebSocketPeer::send_text);
  41. ClassDB::bind_method(D_METHOD("was_string_packet"), &WebSocketPeer::was_string_packet);
  42. ClassDB::bind_method(D_METHOD("poll"), &WebSocketPeer::poll);
  43. ClassDB::bind_method(D_METHOD("close", "code", "reason"), &WebSocketPeer::close, DEFVAL(1000), DEFVAL(""));
  44. ClassDB::bind_method(D_METHOD("get_connected_host"), &WebSocketPeer::get_connected_host);
  45. ClassDB::bind_method(D_METHOD("get_connected_port"), &WebSocketPeer::get_connected_port);
  46. ClassDB::bind_method(D_METHOD("get_selected_protocol"), &WebSocketPeer::get_selected_protocol);
  47. ClassDB::bind_method(D_METHOD("get_requested_url"), &WebSocketPeer::get_requested_url);
  48. ClassDB::bind_method(D_METHOD("set_no_delay", "enabled"), &WebSocketPeer::set_no_delay);
  49. ClassDB::bind_method(D_METHOD("get_current_outbound_buffered_amount"), &WebSocketPeer::get_current_outbound_buffered_amount);
  50. ClassDB::bind_method(D_METHOD("get_ready_state"), &WebSocketPeer::get_ready_state);
  51. ClassDB::bind_method(D_METHOD("get_close_code"), &WebSocketPeer::get_close_code);
  52. ClassDB::bind_method(D_METHOD("get_close_reason"), &WebSocketPeer::get_close_reason);
  53. ClassDB::bind_method(D_METHOD("get_supported_protocols"), &WebSocketPeer::_get_supported_protocols);
  54. ClassDB::bind_method(D_METHOD("set_supported_protocols", "protocols"), &WebSocketPeer::set_supported_protocols);
  55. ClassDB::bind_method(D_METHOD("get_handshake_headers"), &WebSocketPeer::_get_handshake_headers);
  56. ClassDB::bind_method(D_METHOD("set_handshake_headers", "protocols"), &WebSocketPeer::set_handshake_headers);
  57. ClassDB::bind_method(D_METHOD("get_inbound_buffer_size"), &WebSocketPeer::get_inbound_buffer_size);
  58. ClassDB::bind_method(D_METHOD("set_inbound_buffer_size", "buffer_size"), &WebSocketPeer::set_inbound_buffer_size);
  59. ClassDB::bind_method(D_METHOD("get_outbound_buffer_size"), &WebSocketPeer::get_outbound_buffer_size);
  60. ClassDB::bind_method(D_METHOD("set_outbound_buffer_size", "buffer_size"), &WebSocketPeer::set_outbound_buffer_size);
  61. ClassDB::bind_method(D_METHOD("set_max_queued_packets", "buffer_size"), &WebSocketPeer::set_max_queued_packets);
  62. ClassDB::bind_method(D_METHOD("get_max_queued_packets"), &WebSocketPeer::get_max_queued_packets);
  63. ClassDB::bind_method(D_METHOD("set_heartbeat_interval", "interval"), &WebSocketPeer::set_heartbeat_interval);
  64. ClassDB::bind_method(D_METHOD("get_heartbeat_interval"), &WebSocketPeer::get_heartbeat_interval);
  65. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "supported_protocols"), "set_supported_protocols", "get_supported_protocols");
  66. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "handshake_headers"), "set_handshake_headers", "get_handshake_headers");
  67. ADD_PROPERTY(PropertyInfo(Variant::INT, "inbound_buffer_size"), "set_inbound_buffer_size", "get_inbound_buffer_size");
  68. ADD_PROPERTY(PropertyInfo(Variant::INT, "outbound_buffer_size"), "set_outbound_buffer_size", "get_outbound_buffer_size");
  69. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_queued_packets"), "set_max_queued_packets", "get_max_queued_packets");
  70. ADD_PROPERTY(PropertyInfo(Variant::INT, "heartbeat_interval"), "set_heartbeat_interval", "get_heartbeat_interval");
  71. BIND_ENUM_CONSTANT(WRITE_MODE_TEXT);
  72. BIND_ENUM_CONSTANT(WRITE_MODE_BINARY);
  73. BIND_ENUM_CONSTANT(STATE_CONNECTING);
  74. BIND_ENUM_CONSTANT(STATE_OPEN);
  75. BIND_ENUM_CONSTANT(STATE_CLOSING);
  76. BIND_ENUM_CONSTANT(STATE_CLOSED);
  77. }
  78. Error WebSocketPeer::_send_bind(const PackedByteArray &p_message, WriteMode p_mode) {
  79. return send(p_message.ptr(), p_message.size(), p_mode);
  80. }
  81. Error WebSocketPeer::send_text(const String &p_text) {
  82. const CharString cs = p_text.utf8();
  83. return send((const uint8_t *)cs.ptr(), cs.length(), WRITE_MODE_TEXT);
  84. }
  85. void WebSocketPeer::set_supported_protocols(const Vector<String> &p_protocols) {
  86. // Strip edges from protocols.
  87. supported_protocols.resize(p_protocols.size());
  88. for (int i = 0; i < p_protocols.size(); i++) {
  89. supported_protocols.write[i] = p_protocols[i].strip_edges();
  90. }
  91. }
  92. const Vector<String> WebSocketPeer::get_supported_protocols() const {
  93. return supported_protocols;
  94. }
  95. Vector<String> WebSocketPeer::_get_supported_protocols() const {
  96. Vector<String> out;
  97. out.append_array(supported_protocols);
  98. return out;
  99. }
  100. void WebSocketPeer::set_handshake_headers(const Vector<String> &p_headers) {
  101. handshake_headers = p_headers;
  102. }
  103. const Vector<String> WebSocketPeer::get_handshake_headers() const {
  104. return handshake_headers;
  105. }
  106. Vector<String> WebSocketPeer::_get_handshake_headers() const {
  107. Vector<String> out;
  108. out.append_array(handshake_headers);
  109. return out;
  110. }
  111. void WebSocketPeer::set_outbound_buffer_size(int p_buffer_size) {
  112. outbound_buffer_size = p_buffer_size;
  113. }
  114. int WebSocketPeer::get_outbound_buffer_size() const {
  115. return outbound_buffer_size;
  116. }
  117. void WebSocketPeer::set_inbound_buffer_size(int p_buffer_size) {
  118. inbound_buffer_size = p_buffer_size;
  119. }
  120. int WebSocketPeer::get_inbound_buffer_size() const {
  121. return inbound_buffer_size;
  122. }
  123. void WebSocketPeer::set_max_queued_packets(int p_max_queued_packets) {
  124. max_queued_packets = p_max_queued_packets;
  125. }
  126. int WebSocketPeer::get_max_queued_packets() const {
  127. return max_queued_packets;
  128. }
  129. double WebSocketPeer::get_heartbeat_interval() const {
  130. return heartbeat_interval_msec / 1000.0;
  131. }
  132. void WebSocketPeer::set_heartbeat_interval(double p_interval) {
  133. ERR_FAIL_COND(p_interval < 0);
  134. heartbeat_interval_msec = p_interval * 1000.0;
  135. }