Debian Codegraph @

modules/webrtc/webrtc_multiplayer_peer.h

raw symbols SCIP
1 /**************************************************************************/
2 /* webrtc_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 "webrtc_peer_connection.h"
34
35 #include "scene/main/multiplayer_peer.h"
36
37 class WebRTCMultiplayerPeer : public MultiplayerPeer {
38 GDCLASS(WebRTCMultiplayerPeer, MultiplayerPeer);
39
40 protected:
41 static void _bind_methods();
42
43 private:
44 enum {
45 CH_RELIABLE = 0,
46 CH_ORDERED = 1,
47 CH_UNRELIABLE = 2,
48 CH_RESERVED_MAX = 3
49 };
50
51 enum NetworkMode {
52 MODE_NONE,
53 MODE_SERVER,
54 MODE_CLIENT,
55 MODE_MESH,
56 };
57
58 class ConnectedPeer : public RefCounted {
59 public:
60 Ref<WebRTCPeerConnection> connection;
61 List<Ref<WebRTCDataChannel>> channels;
62 bool connected;
63
64 ConnectedPeer() {
65 connected = false;
66 for (int i = 0; i < CH_RESERVED_MAX; i++) {
67 channels.push_front(Ref<WebRTCDataChannel>());
68 }
69 }
70 };
71
72 uint32_t unique_id = 0;
73 int target_peer = 0;
74 int client_count = 0;
75 ConnectionStatus connection_status = CONNECTION_DISCONNECTED;
76 int next_packet_peer = 0;
77 int next_packet_channel = 0;
78 NetworkMode network_mode = MODE_NONE;
79
80 HashMap<int, Ref<ConnectedPeer>> peer_map;
81 List<TransferMode> channels_modes;
82 List<Dictionary> channels_config;
83
84 void _peer_to_dict(const Ref<ConnectedPeer> &p_connected_peer, Dictionary &r_dict);
85 void _find_next_peer();
86 Ref<ConnectedPeer> _get_next_peer();
87 Error _initialize(int p_self_id, NetworkMode p_mode, const Array &p_channels_config = Array());
88
89 public:
90 WebRTCMultiplayerPeer() {}
91 ~WebRTCMultiplayerPeer();
92
93 Error create_server(const Array &p_channels_config = Array());
94 Error create_client(int p_self_id, const Array &p_channels_config = Array());
95 Error create_mesh(int p_self_id, const Array &p_channels_config = Array());
96 Error add_peer(const Ref<WebRTCPeerConnection> &p_peer, int p_peer_id, int p_unreliable_lifetime = 1);
97 void remove_peer(int p_peer_id);
98 bool has_peer(int p_peer_id);
99 Dictionary get_peer(int p_peer_id);
100 Dictionary get_peers();
101
102 // PacketPeer
103 virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
104 virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
105 virtual int get_available_packet_count() const override;
106 virtual int get_max_packet_size() const override;
107
108 // MultiplayerPeer
109 virtual void set_target_peer(int p_peer_id) override;
110
111 virtual int get_unique_id() const override;
112 virtual int get_packet_peer() const override;
113 virtual int get_packet_channel() const override;
114 virtual TransferMode get_packet_mode() const override;
115
116 virtual bool is_server() const override;
117 virtual bool is_server_relay_supported() const override;
118
119 virtual void poll() override;
120 virtual void close() override;
121 virtual void disconnect_peer(int p_peer_id, bool p_force = false) override;
122
123 virtual ConnectionStatus get_connection_status() const override;
124 };