webrtc_data_channel_js.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**************************************************************************/
  2. /* webrtc_data_channel_js.h */
  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. #pragma once
  31. #ifdef WEB_ENABLED
  32. #include "webrtc_data_channel.h"
  33. class WebRTCDataChannelJS : public WebRTCDataChannel {
  34. GDCLASS(WebRTCDataChannelJS, WebRTCDataChannel);
  35. private:
  36. String _label;
  37. String _protocol;
  38. bool _was_string = false;
  39. WriteMode _write_mode = WRITE_MODE_BINARY;
  40. enum {
  41. PACKET_BUFFER_SIZE = 65536 - 5 // 4 bytes for the size, 1 for for type
  42. };
  43. int _js_id = 0;
  44. RingBuffer<uint8_t> in_buffer;
  45. int queue_count = 0;
  46. uint8_t packet_buffer[PACKET_BUFFER_SIZE];
  47. static void _on_open(void *p_obj);
  48. static void _on_close(void *p_obj);
  49. static void _on_error(void *p_obj);
  50. static void _on_message(void *p_obj, const uint8_t *p_data, int p_size, int p_is_string);
  51. public:
  52. virtual void set_write_mode(WriteMode mode) override;
  53. virtual WriteMode get_write_mode() const override;
  54. virtual bool was_string_packet() const override;
  55. virtual ChannelState get_ready_state() const override;
  56. virtual String get_label() const override;
  57. virtual bool is_ordered() const override;
  58. virtual int get_id() const override;
  59. virtual int get_max_packet_life_time() const override;
  60. virtual int get_max_retransmits() const override;
  61. virtual String get_protocol() const override;
  62. virtual bool is_negotiated() const override;
  63. virtual int get_buffered_amount() const override;
  64. virtual Error poll() override;
  65. virtual void close() override;
  66. /** Inherited from PacketPeer: **/
  67. virtual int get_available_packet_count() const override;
  68. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
  69. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  70. virtual int get_max_packet_size() const override;
  71. WebRTCDataChannelJS();
  72. WebRTCDataChannelJS(int js_id);
  73. ~WebRTCDataChannelJS();
  74. };
  75. #endif // WEB_ENABLED