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