Debian Codegraph @

modules/websocket/websocket_peer.h

raw symbols SCIP
1 /**************************************************************************/
2 /* websocket_peer.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 #pragma once
32
33 #include "core/crypto/crypto.h"
34 #include "core/io/packet_peer.h"
35
36 class WebSocketPeer : public PacketPeer {
37 GDCLASS(WebSocketPeer, PacketPeer);
38
39 public:
40 enum State {
41 STATE_CONNECTING,
42 STATE_OPEN,
43 STATE_CLOSING,
44 STATE_CLOSED
45 };
46
47 enum WriteMode {
48 WRITE_MODE_TEXT,
49 WRITE_MODE_BINARY,
50 };
51
52 enum {
53 DEFAULT_BUFFER_SIZE = 65535,
54 };
55
56 private:
57 virtual Error _send_bind(const PackedByteArray &p_data, WriteMode p_mode = WRITE_MODE_BINARY);
58
59 protected:
60 static WebSocketPeer *(*_create)(bool p_notify_postinitialize);
61
62 static void _bind_methods();
63
64 Vector<String> supported_protocols;
65 Vector<String> handshake_headers;
66
67 Vector<String> _get_supported_protocols() const;
68 Vector<String> _get_handshake_headers() const;
69
70 int outbound_buffer_size = DEFAULT_BUFFER_SIZE;
71 int inbound_buffer_size = DEFAULT_BUFFER_SIZE;
72 int max_queued_packets = 4096;
73 uint64_t heartbeat_interval_msec = 0;
74
75 public:
76 static WebSocketPeer *create(bool p_notify_postinitialize = true) {
77 if (!_create) {
78 return nullptr;
79 }
80 return _create(p_notify_postinitialize);
81 }
82
83 virtual Error connect_to_url(const String &p_url, const Ref<TLSOptions> &p_options = Ref<TLSOptions>()) = 0;
84 virtual Error accept_stream(const Ref<StreamPeer> &p_stream) = 0;
85
86 virtual Error send(const uint8_t *p_buffer, int p_buffer_size, WriteMode p_mode) = 0;
87 virtual void close(int p_code = 1000, const String &p_reason = "") = 0;
88
89 virtual IPAddress get_connected_host() const = 0;
90 virtual uint16_t get_connected_port() const = 0;
91 virtual bool was_string_packet() const = 0;
92 virtual void set_no_delay(bool p_enabled) = 0;
93 virtual int get_current_outbound_buffered_amount() const = 0;
94 virtual String get_selected_protocol() const = 0;
95 virtual String get_requested_url() const = 0;
96
97 virtual void poll() = 0;
98 virtual State get_ready_state() const = 0;
99 virtual int get_close_code() const = 0;
100 virtual String get_close_reason() const = 0;
101
102 Error send_text(const String &p_text);
103
104 void set_supported_protocols(const Vector<String> &p_protocols);
105 const Vector<String> get_supported_protocols() const;
106
107 void set_handshake_headers(const Vector<String> &p_headers);
108 const Vector<String> get_handshake_headers() const;
109
110 void set_outbound_buffer_size(int p_buffer_size);
111 int get_outbound_buffer_size() const;
112
113 void set_inbound_buffer_size(int p_buffer_size);
114 int get_inbound_buffer_size() const;
115
116 void set_max_queued_packets(int p_max_queued_packets);
117 int get_max_queued_packets() const;
118
119 double get_heartbeat_interval() const;
120 void set_heartbeat_interval(double p_interval);
121
122 WebSocketPeer();
123 ~WebSocketPeer();
124 };
125
126 VARIANT_ENUM_CAST(WebSocketPeer::WriteMode);
127 VARIANT_ENUM_CAST(WebSocketPeer::State);