emws_peer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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) {
  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(const 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. godot_js_websocket_send(peer_sock, p_buffer, p_buffer_size, is_bin);
  51. return OK;
  52. };
  53. Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  54. if (_in_buffer.packets_left() == 0)
  55. return ERR_UNAVAILABLE;
  56. int read = 0;
  57. Error err = _in_buffer.read_packet(_packet_buffer.ptrw(), _packet_buffer.size(), &_is_string, read);
  58. ERR_FAIL_COND_V(err != OK, err);
  59. *r_buffer = _packet_buffer.ptr();
  60. r_buffer_size = read;
  61. return OK;
  62. };
  63. int EMWSPeer::get_available_packet_count() const {
  64. return _in_buffer.packets_left();
  65. };
  66. bool EMWSPeer::was_string_packet() const {
  67. return _is_string;
  68. };
  69. bool EMWSPeer::is_connected_to_host() const {
  70. return peer_sock != -1;
  71. };
  72. void EMWSPeer::close(int p_code, String p_reason) {
  73. if (peer_sock != -1) {
  74. godot_js_websocket_close(peer_sock, p_code, p_reason.utf8().get_data());
  75. }
  76. _is_string = 0;
  77. _in_buffer.clear();
  78. peer_sock = -1;
  79. };
  80. IP_Address EMWSPeer::get_connected_host() const {
  81. ERR_FAIL_V_MSG(IP_Address(), "Not supported in HTML5 export.");
  82. };
  83. uint16_t EMWSPeer::get_connected_port() const {
  84. ERR_FAIL_V_MSG(0, "Not supported in HTML5 export.");
  85. };
  86. void EMWSPeer::set_no_delay(bool p_enabled) {
  87. ERR_FAIL_MSG("'set_no_delay' is not supported in HTML5 export.");
  88. }
  89. EMWSPeer::EMWSPeer() {
  90. close();
  91. };
  92. EMWSPeer::~EMWSPeer() {
  93. close();
  94. };
  95. #endif // JAVASCRIPT_ENABLED