webrtc_data_channel_js.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*************************************************************************/
  2. /* webrtc_data_channel_js.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 "webrtc_data_channel_js.h"
  32. #include "emscripten.h"
  33. extern "C" {
  34. typedef void (*RTCChOnOpen)(void *p_obj);
  35. typedef void (*RTCChOnMessage)(void *p_obj, const uint8_t *p_buffer, int p_size, int p_is_string);
  36. typedef void (*RTCChOnClose)(void *p_obj);
  37. typedef void (*RTCChOnError)(void *p_obj);
  38. extern int godot_js_rtc_datachannel_ready_state_get(int p_id);
  39. extern int godot_js_rtc_datachannel_send(int p_id, const uint8_t *p_buffer, int p_length, int p_raw);
  40. extern int godot_js_rtc_datachannel_is_ordered(int p_id);
  41. extern int godot_js_rtc_datachannel_id_get(int p_id);
  42. extern int godot_js_rtc_datachannel_max_packet_lifetime_get(int p_id);
  43. extern int godot_js_rtc_datachannel_max_retransmits_get(int p_id);
  44. extern int godot_js_rtc_datachannel_is_negotiated(int p_id);
  45. extern char *godot_js_rtc_datachannel_label_get(int p_id); // Must free the returned string.
  46. extern char *godot_js_rtc_datachannel_protocol_get(int p_id); // Must free the returned string.
  47. extern void godot_js_rtc_datachannel_destroy(int p_id);
  48. extern void godot_js_rtc_datachannel_connect(int p_id, void *p_obj, RTCChOnOpen p_on_open, RTCChOnMessage p_on_message, RTCChOnError p_on_error, RTCChOnClose p_on_close);
  49. extern void godot_js_rtc_datachannel_close(int p_id);
  50. }
  51. void WebRTCDataChannelJS::_on_open(void *p_obj) {
  52. WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(p_obj);
  53. peer->in_buffer.resize(peer->_in_buffer_shift);
  54. }
  55. void WebRTCDataChannelJS::_on_close(void *p_obj) {
  56. WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(p_obj);
  57. peer->close();
  58. }
  59. void WebRTCDataChannelJS::_on_error(void *p_obj) {
  60. WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(p_obj);
  61. peer->close();
  62. }
  63. void WebRTCDataChannelJS::_on_message(void *p_obj, const uint8_t *p_data, int p_size, int p_is_string) {
  64. WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(p_obj);
  65. RingBuffer<uint8_t> &in_buffer = peer->in_buffer;
  66. ERR_FAIL_COND_MSG(in_buffer.space_left() < (int)(p_size + 5), "Buffer full! Dropping data.");
  67. uint8_t is_string = p_is_string ? 1 : 0;
  68. in_buffer.write((uint8_t *)&p_size, 4);
  69. in_buffer.write((uint8_t *)&is_string, 1);
  70. in_buffer.write(p_data, p_size);
  71. peer->queue_count++;
  72. }
  73. void WebRTCDataChannelJS::close() {
  74. in_buffer.resize(0);
  75. queue_count = 0;
  76. _was_string = false;
  77. godot_js_rtc_datachannel_close(_js_id);
  78. }
  79. Error WebRTCDataChannelJS::poll() {
  80. return OK;
  81. }
  82. WebRTCDataChannelJS::ChannelState WebRTCDataChannelJS::get_ready_state() const {
  83. return (ChannelState)godot_js_rtc_datachannel_ready_state_get(_js_id);
  84. }
  85. int WebRTCDataChannelJS::get_available_packet_count() const {
  86. return queue_count;
  87. }
  88. Error WebRTCDataChannelJS::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  89. ERR_FAIL_COND_V(get_ready_state() != STATE_OPEN, ERR_UNCONFIGURED);
  90. if (queue_count == 0)
  91. return ERR_UNAVAILABLE;
  92. uint32_t to_read = 0;
  93. uint32_t left = 0;
  94. uint8_t is_string = 0;
  95. r_buffer_size = 0;
  96. in_buffer.read((uint8_t *)&to_read, 4);
  97. --queue_count;
  98. left = in_buffer.data_left();
  99. if (left < to_read + 1) {
  100. in_buffer.advance_read(left);
  101. return FAILED;
  102. }
  103. in_buffer.read(&is_string, 1);
  104. _was_string = is_string == 1;
  105. in_buffer.read(packet_buffer, to_read);
  106. *r_buffer = packet_buffer;
  107. r_buffer_size = to_read;
  108. return OK;
  109. }
  110. Error WebRTCDataChannelJS::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  111. ERR_FAIL_COND_V(get_ready_state() != STATE_OPEN, ERR_UNCONFIGURED);
  112. int is_bin = _write_mode == WebRTCDataChannel::WRITE_MODE_BINARY ? 1 : 0;
  113. godot_js_rtc_datachannel_send(_js_id, p_buffer, p_buffer_size, is_bin);
  114. return OK;
  115. }
  116. int WebRTCDataChannelJS::get_max_packet_size() const {
  117. return 1200;
  118. }
  119. void WebRTCDataChannelJS::set_write_mode(WriteMode p_mode) {
  120. _write_mode = p_mode;
  121. }
  122. WebRTCDataChannel::WriteMode WebRTCDataChannelJS::get_write_mode() const {
  123. return _write_mode;
  124. }
  125. bool WebRTCDataChannelJS::was_string_packet() const {
  126. return _was_string;
  127. }
  128. String WebRTCDataChannelJS::get_label() const {
  129. return _label;
  130. }
  131. bool WebRTCDataChannelJS::is_ordered() const {
  132. return godot_js_rtc_datachannel_is_ordered(_js_id);
  133. }
  134. int WebRTCDataChannelJS::get_id() const {
  135. return godot_js_rtc_datachannel_id_get(_js_id);
  136. }
  137. int WebRTCDataChannelJS::get_max_packet_life_time() const {
  138. return godot_js_rtc_datachannel_max_packet_lifetime_get(_js_id);
  139. }
  140. int WebRTCDataChannelJS::get_max_retransmits() const {
  141. return godot_js_rtc_datachannel_max_retransmits_get(_js_id);
  142. }
  143. String WebRTCDataChannelJS::get_protocol() const {
  144. return _protocol;
  145. }
  146. bool WebRTCDataChannelJS::is_negotiated() const {
  147. return godot_js_rtc_datachannel_is_negotiated(_js_id);
  148. }
  149. WebRTCDataChannelJS::WebRTCDataChannelJS() {
  150. }
  151. WebRTCDataChannelJS::WebRTCDataChannelJS(int js_id) {
  152. _js_id = js_id;
  153. godot_js_rtc_datachannel_connect(js_id, this, &_on_open, &_on_message, &_on_error, &_on_close);
  154. // Parse label
  155. char *label = godot_js_rtc_datachannel_label_get(js_id);
  156. if (label) {
  157. _label.parse_utf8(label);
  158. free(label);
  159. }
  160. char *protocol = godot_js_rtc_datachannel_protocol_get(js_id);
  161. if (protocol) {
  162. _protocol.parse_utf8(protocol);
  163. free(protocol);
  164. }
  165. }
  166. WebRTCDataChannelJS::~WebRTCDataChannelJS() {
  167. close();
  168. godot_js_rtc_datachannel_destroy(_js_id);
  169. }
  170. #endif