webrtc_data_channel_js.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include "webrtc_data_channel_js.h"
  31. #ifdef WEB_ENABLED
  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. }
  94. uint32_t to_read = 0;
  95. uint32_t left = 0;
  96. uint8_t is_string = 0;
  97. r_buffer_size = 0;
  98. in_buffer.read((uint8_t *)&to_read, 4);
  99. --queue_count;
  100. left = in_buffer.data_left();
  101. if (left < to_read + 1) {
  102. in_buffer.advance_read(left);
  103. return FAILED;
  104. }
  105. in_buffer.read(&is_string, 1);
  106. _was_string = is_string == 1;
  107. in_buffer.read(packet_buffer, to_read);
  108. *r_buffer = packet_buffer;
  109. r_buffer_size = to_read;
  110. return OK;
  111. }
  112. Error WebRTCDataChannelJS::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  113. ERR_FAIL_COND_V(get_ready_state() != STATE_OPEN, ERR_UNCONFIGURED);
  114. int is_bin = _write_mode == WebRTCDataChannel::WRITE_MODE_BINARY ? 1 : 0;
  115. godot_js_rtc_datachannel_send(_js_id, p_buffer, p_buffer_size, is_bin);
  116. return OK;
  117. }
  118. int WebRTCDataChannelJS::get_max_packet_size() const {
  119. return 1200;
  120. }
  121. void WebRTCDataChannelJS::set_write_mode(WriteMode p_mode) {
  122. _write_mode = p_mode;
  123. }
  124. WebRTCDataChannel::WriteMode WebRTCDataChannelJS::get_write_mode() const {
  125. return _write_mode;
  126. }
  127. bool WebRTCDataChannelJS::was_string_packet() const {
  128. return _was_string;
  129. }
  130. String WebRTCDataChannelJS::get_label() const {
  131. return _label;
  132. }
  133. bool WebRTCDataChannelJS::is_ordered() const {
  134. return godot_js_rtc_datachannel_is_ordered(_js_id);
  135. }
  136. int WebRTCDataChannelJS::get_id() const {
  137. return godot_js_rtc_datachannel_id_get(_js_id);
  138. }
  139. int WebRTCDataChannelJS::get_max_packet_life_time() const {
  140. return godot_js_rtc_datachannel_max_packet_lifetime_get(_js_id);
  141. }
  142. int WebRTCDataChannelJS::get_max_retransmits() const {
  143. return godot_js_rtc_datachannel_max_retransmits_get(_js_id);
  144. }
  145. String WebRTCDataChannelJS::get_protocol() const {
  146. return _protocol;
  147. }
  148. bool WebRTCDataChannelJS::is_negotiated() const {
  149. return godot_js_rtc_datachannel_is_negotiated(_js_id);
  150. }
  151. int WebRTCDataChannelJS::get_buffered_amount() const {
  152. return godot_js_rtc_datachannel_get_buffered_amount(_js_id);
  153. }
  154. WebRTCDataChannelJS::WebRTCDataChannelJS() {
  155. }
  156. WebRTCDataChannelJS::WebRTCDataChannelJS(int js_id) {
  157. _js_id = js_id;
  158. godot_js_rtc_datachannel_connect(js_id, this, &_on_open, &_on_message, &_on_error, &_on_close);
  159. // Parse label
  160. char *label = godot_js_rtc_datachannel_label_get(js_id);
  161. if (label) {
  162. _label.parse_utf8(label);
  163. free(label);
  164. }
  165. char *protocol = godot_js_rtc_datachannel_protocol_get(js_id);
  166. if (protocol) {
  167. _protocol.parse_utf8(protocol);
  168. free(protocol);
  169. }
  170. }
  171. WebRTCDataChannelJS::~WebRTCDataChannelJS() {
  172. close();
  173. godot_js_rtc_datachannel_destroy(_js_id);
  174. }
  175. #endif