webrtc_data_channel_js.cpp 7.7 KB

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