Debian Codegraph @

modules/websocket/websocket_multiplayer_peer.h

raw symbols SCIP
1 /**************************************************************************/
2 /* websocket_multiplayer_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 "websocket_peer.h"
34
35 #include "core/io/tcp_server.h"
36 #include "core/templates/list.h"
37 #include "scene/main/multiplayer_peer.h"
38
39 class WebSocketMultiplayerPeer : public MultiplayerPeer {
40 GDCLASS(WebSocketMultiplayerPeer, MultiplayerPeer);
41
42 private:
43 Ref<WebSocketPeer> _create_peer();
44
45 protected:
46 enum {
47 SYS_NONE = 0,
48 SYS_ADD = 1,
49 SYS_DEL = 2,
50 SYS_ID = 3,
51
52 PROTO_SIZE = 9
53 };
54
55 struct Packet {
56 int source = 0;
57 uint8_t *data = nullptr;
58 uint32_t size = 0;
59 };
60
61 struct PendingPeer {
62 uint64_t time = 0;
63 Ref<StreamPeerTCP> tcp;
64 Ref<StreamPeer> connection;
65 Ref<WebSocketPeer> ws;
66 };
67
68 uint64_t handshake_timeout = 3000;
69 Ref<WebSocketPeer> peer_config;
70 HashMap<int, PendingPeer> pending_peers;
71 Ref<TCPServer> tcp_server;
72 Ref<TLSOptions> tls_server_options;
73
74 ConnectionStatus connection_status = CONNECTION_DISCONNECTED;
75
76 List<Packet> incoming_packets;
77 HashMap<int, Ref<WebSocketPeer>> peers_map;
78 Packet current_packet;
79
80 int target_peer = 0;
81 int unique_id = 0;
82
83 static void _bind_methods();
84
85 void _poll_client();
86 void _poll_server();
87 void _clear();
88
89 public:
90 /* MultiplayerPeer */
91 virtual void set_target_peer(int p_target_peer) override;
92 virtual int get_packet_peer() const override;
93 virtual int get_packet_channel() const override { return 0; }
94 virtual TransferMode get_packet_mode() const override { return TRANSFER_MODE_RELIABLE; }
95 virtual int get_unique_id() const override;
96 virtual bool is_server_relay_supported() const override { return true; }
97
98 virtual int get_max_packet_size() const override;
99 virtual bool is_server() const override;
100 virtual void poll() override;
101 virtual void close() override;
102 virtual void disconnect_peer(int p_peer_id, bool p_force = false) override;
103
104 virtual ConnectionStatus get_connection_status() const override;
105
106 /* PacketPeer */
107 virtual int get_available_packet_count() const override;
108 virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override;
109 virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
110
111 /* WebSocketPeer */
112 virtual Ref<WebSocketPeer> get_peer(int p_peer_id) const;
113
114 Error create_client(const String &p_url, const Ref<TLSOptions> &p_options);
115 Error create_server(int p_port, IPAddress p_bind_ip, const Ref<TLSOptions> &p_options);
116
117 void set_supported_protocols(const Vector<String> &p_protocols);
118 Vector<String> get_supported_protocols() const;
119
120 void set_handshake_headers(const Vector<String> &p_headers);
121 Vector<String> get_handshake_headers() const;
122
123 void set_outbound_buffer_size(int p_buffer_size);
124 int get_outbound_buffer_size() const;
125
126 void set_inbound_buffer_size(int p_buffer_size);
127 int get_inbound_buffer_size() const;
128
129 float get_handshake_timeout() const;
130 void set_handshake_timeout(float p_timeout);
131
132 IPAddress get_peer_address(int p_peer_id) const;
133 int get_peer_port(int p_peer_id) const;
134
135 void set_max_queued_packets(int p_max_queued_packets);
136 int get_max_queued_packets() const;
137
138 WebSocketMultiplayerPeer();
139 ~WebSocketMultiplayerPeer();
140 };