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