webrtc_data_channel_gdnative.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**************************************************************************/
  2. /* webrtc_data_channel_gdnative.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 WEBRTC_GDNATIVE_ENABLED
  31. #include "webrtc_data_channel_gdnative.h"
  32. #include "core/io/resource_loader.h"
  33. #include "modules/gdnative/nativescript/nativescript.h"
  34. void WebRTCDataChannelGDNative::_bind_methods() {
  35. ADD_PROPERTY_DEFAULT("write_mode", WRITE_MODE_BINARY);
  36. }
  37. WebRTCDataChannelGDNative::WebRTCDataChannelGDNative() {
  38. interface = nullptr;
  39. }
  40. WebRTCDataChannelGDNative::~WebRTCDataChannelGDNative() {
  41. }
  42. Error WebRTCDataChannelGDNative::poll() {
  43. ERR_FAIL_COND_V(interface == nullptr, ERR_UNCONFIGURED);
  44. return (Error)interface->poll(interface->data);
  45. }
  46. void WebRTCDataChannelGDNative::close() {
  47. ERR_FAIL_COND(interface == nullptr);
  48. interface->close(interface->data);
  49. }
  50. void WebRTCDataChannelGDNative::set_write_mode(WriteMode p_mode) {
  51. ERR_FAIL_COND(interface == nullptr);
  52. interface->set_write_mode(interface->data, p_mode);
  53. }
  54. WebRTCDataChannel::WriteMode WebRTCDataChannelGDNative::get_write_mode() const {
  55. ERR_FAIL_COND_V(interface == nullptr, WRITE_MODE_BINARY);
  56. return (WriteMode)interface->get_write_mode(interface->data);
  57. }
  58. bool WebRTCDataChannelGDNative::was_string_packet() const {
  59. ERR_FAIL_COND_V(interface == nullptr, false);
  60. return interface->was_string_packet(interface->data);
  61. }
  62. WebRTCDataChannel::ChannelState WebRTCDataChannelGDNative::get_ready_state() const {
  63. ERR_FAIL_COND_V(interface == nullptr, STATE_CLOSED);
  64. return (ChannelState)interface->get_ready_state(interface->data);
  65. }
  66. String WebRTCDataChannelGDNative::get_label() const {
  67. ERR_FAIL_COND_V(interface == nullptr, "");
  68. return String(interface->get_label(interface->data));
  69. }
  70. bool WebRTCDataChannelGDNative::is_ordered() const {
  71. ERR_FAIL_COND_V(interface == nullptr, false);
  72. return interface->is_ordered(interface->data);
  73. }
  74. int WebRTCDataChannelGDNative::get_id() const {
  75. ERR_FAIL_COND_V(interface == nullptr, -1);
  76. return interface->get_id(interface->data);
  77. }
  78. int WebRTCDataChannelGDNative::get_max_packet_life_time() const {
  79. ERR_FAIL_COND_V(interface == nullptr, -1);
  80. return interface->get_max_packet_life_time(interface->data);
  81. }
  82. int WebRTCDataChannelGDNative::get_max_retransmits() const {
  83. ERR_FAIL_COND_V(interface == nullptr, -1);
  84. return interface->get_max_retransmits(interface->data);
  85. }
  86. String WebRTCDataChannelGDNative::get_protocol() const {
  87. ERR_FAIL_COND_V(interface == nullptr, "");
  88. return String(interface->get_protocol(interface->data));
  89. }
  90. bool WebRTCDataChannelGDNative::is_negotiated() const {
  91. ERR_FAIL_COND_V(interface == nullptr, false);
  92. return interface->is_negotiated(interface->data);
  93. }
  94. int WebRTCDataChannelGDNative::get_buffered_amount() const {
  95. ERR_FAIL_COND_V(interface == nullptr, 0);
  96. ERR_FAIL_COND_V(interface->next == nullptr, 0);
  97. return ((godot_net_webrtc_data_channel_ext *)interface->next)->get_buffered_amount(interface->data);
  98. }
  99. Error WebRTCDataChannelGDNative::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  100. ERR_FAIL_COND_V(interface == nullptr, ERR_UNCONFIGURED);
  101. return (Error)interface->get_packet(interface->data, r_buffer, &r_buffer_size);
  102. }
  103. Error WebRTCDataChannelGDNative::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  104. ERR_FAIL_COND_V(interface == nullptr, ERR_UNCONFIGURED);
  105. return (Error)interface->put_packet(interface->data, p_buffer, p_buffer_size);
  106. }
  107. int WebRTCDataChannelGDNative::get_max_packet_size() const {
  108. ERR_FAIL_COND_V(interface == nullptr, 0);
  109. return interface->get_max_packet_size(interface->data);
  110. }
  111. int WebRTCDataChannelGDNative::get_available_packet_count() const {
  112. ERR_FAIL_COND_V(interface == nullptr, 0);
  113. return interface->get_available_packet_count(interface->data);
  114. }
  115. void WebRTCDataChannelGDNative::set_native_webrtc_data_channel(const godot_net_webrtc_data_channel *p_impl) {
  116. interface = p_impl;
  117. }
  118. #endif // WEBRTC_GDNATIVE_ENABLED