emws_peer.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*************************************************************************/
  2. /* emws_peer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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) {
  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. }
  38. void EMWSPeer::set_write_mode(WriteMode p_mode) {
  39. write_mode = p_mode;
  40. }
  41. EMWSPeer::WriteMode EMWSPeer::get_write_mode() const {
  42. return write_mode;
  43. }
  44. Error EMWSPeer::read_msg(uint8_t *p_data, uint32_t p_size, bool p_is_string) {
  45. uint8_t is_string = p_is_string ? 1 : 0;
  46. return _in_buffer.write_packet(p_data, p_size, &is_string);
  47. }
  48. Error EMWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  49. int is_bin = write_mode == WebSocketPeer::WRITE_MODE_BINARY ? 1 : 0;
  50. /* clang-format off */
  51. EM_ASM({
  52. var sock = Module.IDHandler.get($0);
  53. var bytes_array = new Uint8Array($2);
  54. var i = 0;
  55. for(i=0; i<$2; i++) {
  56. bytes_array[i] = getValue($1+i, 'i8');
  57. }
  58. if ($3) {
  59. sock.send(bytes_array.buffer);
  60. } else {
  61. var string = new TextDecoder("utf-8").decode(bytes_array);
  62. sock.send(string);
  63. }
  64. }, peer_sock, p_buffer, p_buffer_size, is_bin);
  65. /* clang-format on */
  66. return OK;
  67. };
  68. Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  69. if (_in_buffer.packets_left() == 0)
  70. return ERR_UNAVAILABLE;
  71. PoolVector<uint8_t>::Write rw = _packet_buffer.write();
  72. int read = 0;
  73. Error err = _in_buffer.read_packet(rw.ptr(), _packet_buffer.size(), &_is_string, read);
  74. ERR_FAIL_COND_V(err != OK, err);
  75. *r_buffer = rw.ptr();
  76. r_buffer_size = read;
  77. return OK;
  78. };
  79. int EMWSPeer::get_available_packet_count() const {
  80. return _in_buffer.packets_left();
  81. };
  82. bool EMWSPeer::was_string_packet() const {
  83. return _is_string;
  84. };
  85. bool EMWSPeer::is_connected_to_host() const {
  86. return peer_sock != -1;
  87. };
  88. void EMWSPeer::close(int p_code, String p_reason) {
  89. if (peer_sock != -1) {
  90. /* clang-format off */
  91. EM_ASM({
  92. var sock = Module.IDHandler.get($0);
  93. var code = $1;
  94. var reason = UTF8ToString($2);
  95. sock.close(code, reason);
  96. Module.IDHandler.remove($0);
  97. }, peer_sock, p_code, p_reason.utf8().get_data());
  98. /* clang-format on */
  99. }
  100. _is_string = 0;
  101. _in_buffer.clear();
  102. peer_sock = -1;
  103. };
  104. IP_Address EMWSPeer::get_connected_host() const {
  105. ERR_EXPLAIN("Not supported in HTML5 export");
  106. ERR_FAIL_V(IP_Address());
  107. };
  108. uint16_t EMWSPeer::get_connected_port() const {
  109. ERR_EXPLAIN("Not supported in HTML5 export");
  110. ERR_FAIL_V(0);
  111. };
  112. EMWSPeer::EMWSPeer() {
  113. peer_sock = -1;
  114. write_mode = WRITE_MODE_BINARY;
  115. close();
  116. };
  117. EMWSPeer::~EMWSPeer() {
  118. close();
  119. };
  120. #endif // JAVASCRIPT_ENABLED