emws_peer.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*************************************************************************/
  2. /* emws_peer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #ifdef JAVASCRIPT_ENABLED
  31. #include "emws_peer.h"
  32. #include "core/io/ip.h"
  33. void EMWSPeer::set_sock(int p_sock, unsigned int p_in_buf_size, unsigned int p_in_pkt_size, unsigned int p_out_buf_size) {
  34. peer_sock = p_sock;
  35. _in_buffer.resize(p_in_pkt_size, p_in_buf_size);
  36. _packet_buffer.resize((1 << p_in_buf_size));
  37. _out_buf_size = p_out_buf_size;
  38. }
  39. void EMWSPeer::set_write_mode(WriteMode p_mode) {
  40. write_mode = p_mode;
  41. }
  42. EMWSPeer::WriteMode EMWSPeer::get_write_mode() const {
  43. return write_mode;
  44. }
  45. Error EMWSPeer::read_msg(const uint8_t *p_data, uint32_t p_size, bool p_is_string) {
  46. uint8_t is_string = p_is_string ? 1 : 0;
  47. return _in_buffer.write_packet(p_data, p_size, &is_string);
  48. }
  49. Error EMWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  50. ERR_FAIL_COND_V(_out_buf_size && ((uint64_t)godot_js_websocket_buffered_amount(peer_sock) >= (1ULL << _out_buf_size)), ERR_OUT_OF_MEMORY);
  51. int is_bin = write_mode == WebSocketPeer::WRITE_MODE_BINARY ? 1 : 0;
  52. godot_js_websocket_send(peer_sock, p_buffer, p_buffer_size, is_bin);
  53. return OK;
  54. };
  55. Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  56. if (_in_buffer.packets_left() == 0)
  57. return ERR_UNAVAILABLE;
  58. PoolVector<uint8_t>::Write rw = _packet_buffer.write();
  59. int read = 0;
  60. Error err = _in_buffer.read_packet(rw.ptr(), _packet_buffer.size(), &_is_string, read);
  61. ERR_FAIL_COND_V(err != OK, err);
  62. *r_buffer = rw.ptr();
  63. r_buffer_size = read;
  64. return OK;
  65. };
  66. int EMWSPeer::get_available_packet_count() const {
  67. return _in_buffer.packets_left();
  68. };
  69. int EMWSPeer::get_current_outbound_buffered_amount() const {
  70. if (peer_sock != -1) {
  71. return godot_js_websocket_buffered_amount(peer_sock);
  72. }
  73. return 0;
  74. }
  75. bool EMWSPeer::was_string_packet() const {
  76. return _is_string;
  77. };
  78. bool EMWSPeer::is_connected_to_host() const {
  79. return peer_sock != -1;
  80. };
  81. void EMWSPeer::close(int p_code, String p_reason) {
  82. if (peer_sock != -1) {
  83. godot_js_websocket_close(peer_sock, p_code, p_reason.utf8().get_data());
  84. }
  85. _is_string = 0;
  86. _in_buffer.clear();
  87. peer_sock = -1;
  88. };
  89. IP_Address EMWSPeer::get_connected_host() const {
  90. ERR_FAIL_V_MSG(IP_Address(), "Not supported in HTML5 export.");
  91. };
  92. uint16_t EMWSPeer::get_connected_port() const {
  93. ERR_FAIL_V_MSG(0, "Not supported in HTML5 export.");
  94. };
  95. void EMWSPeer::set_no_delay(bool p_enabled) {
  96. ERR_FAIL_MSG("'set_no_delay' is not supported in HTML5 export.");
  97. }
  98. EMWSPeer::EMWSPeer() {
  99. _out_buf_size = 0;
  100. peer_sock = -1;
  101. write_mode = WRITE_MODE_BINARY;
  102. close();
  103. };
  104. EMWSPeer::~EMWSPeer() {
  105. close();
  106. };
  107. #endif // JAVASCRIPT_ENABLED