emws_peer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**************************************************************************/
  2. /* emws_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. #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) + p_buffer_size >= (1ULL << _out_buf_size)), ERR_OUT_OF_MEMORY);
  51. int is_bin = write_mode == WebSocketPeer::WRITE_MODE_BINARY ? 1 : 0;
  52. if (godot_js_websocket_send(peer_sock, p_buffer, p_buffer_size, is_bin) != 0) {
  53. return FAILED;
  54. }
  55. return OK;
  56. };
  57. Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  58. if (_in_buffer.packets_left() == 0)
  59. return ERR_UNAVAILABLE;
  60. PoolVector<uint8_t>::Write rw = _packet_buffer.write();
  61. int read = 0;
  62. Error err = _in_buffer.read_packet(rw.ptr(), _packet_buffer.size(), &_is_string, read);
  63. ERR_FAIL_COND_V(err != OK, err);
  64. *r_buffer = rw.ptr();
  65. r_buffer_size = read;
  66. return OK;
  67. };
  68. int EMWSPeer::get_available_packet_count() const {
  69. return _in_buffer.packets_left();
  70. };
  71. int EMWSPeer::get_current_outbound_buffered_amount() const {
  72. if (peer_sock != -1) {
  73. return godot_js_websocket_buffered_amount(peer_sock);
  74. }
  75. return 0;
  76. }
  77. bool EMWSPeer::was_string_packet() const {
  78. return _is_string;
  79. };
  80. bool EMWSPeer::is_connected_to_host() const {
  81. return peer_sock != -1;
  82. };
  83. void EMWSPeer::close(int p_code, String p_reason) {
  84. if (peer_sock != -1) {
  85. godot_js_websocket_close(peer_sock, p_code, p_reason.utf8().get_data());
  86. }
  87. _is_string = 0;
  88. _in_buffer.clear();
  89. peer_sock = -1;
  90. };
  91. IP_Address EMWSPeer::get_connected_host() const {
  92. ERR_FAIL_V_MSG(IP_Address(), "Not supported in HTML5 export.");
  93. };
  94. uint16_t EMWSPeer::get_connected_port() const {
  95. ERR_FAIL_V_MSG(0, "Not supported in HTML5 export.");
  96. };
  97. void EMWSPeer::set_no_delay(bool p_enabled) {
  98. ERR_FAIL_MSG("'set_no_delay' is not supported in HTML5 export.");
  99. }
  100. EMWSPeer::EMWSPeer() {
  101. _out_buf_size = 0;
  102. peer_sock = -1;
  103. write_mode = WRITE_MODE_BINARY;
  104. close();
  105. };
  106. EMWSPeer::~EMWSPeer() {
  107. close();
  108. };
  109. #endif // JAVASCRIPT_ENABLED