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