1/**************************************************************************/
2/* 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 MULTIPLAYER_PEER_H
32#define MULTIPLAYER_PEER_H
33
34#include "core/io/packet_peer.h"
35
36#include "core/extension/ext_wrappers.gen.inc"
37#include "core/object/gdvirtual.gen.inc"
38#include "core/variant/native_ptr.h"
39
40class MultiplayerPeer : public PacketPeer {
41 GDCLASS(MultiplayerPeer, PacketPeer);
42
43public:
44 enum TransferMode {
45 TRANSFER_MODE_UNRELIABLE,
46 TRANSFER_MODE_UNRELIABLE_ORDERED,
47 TRANSFER_MODE_RELIABLE
48 };
49
50protected:
51 static void _bind_methods();
52
53private:
54 int transfer_channel = 0;
55 TransferMode transfer_mode = TRANSFER_MODE_RELIABLE;
56 bool refuse_connections = false;
57
58public:
59 enum {
60 TARGET_PEER_BROADCAST = 0,
61 TARGET_PEER_SERVER = 1
62 };
63
64 enum ConnectionStatus {
65 CONNECTION_DISCONNECTED,
66 CONNECTION_CONNECTING,
67 CONNECTION_CONNECTED,
68 };
69
70 virtual void set_transfer_channel(int p_channel);
71 virtual int get_transfer_channel() const;
72 virtual void set_transfer_mode(TransferMode p_mode);
73 virtual TransferMode get_transfer_mode() const;
74 virtual void set_refuse_new_connections(bool p_enable);
75 virtual bool is_refusing_new_connections() const;
76 virtual bool is_server_relay_supported() const;
77
78 virtual void set_target_peer(int p_peer_id) = 0;
79
80 virtual int get_packet_peer() const = 0;
81 virtual TransferMode get_packet_mode() const = 0;
82 virtual int get_packet_channel() const = 0;
83
84 virtual void disconnect_peer(int p_peer, bool p_force = false) = 0;
85
86 virtual bool is_server() const = 0;
87
88 virtual void poll() = 0;
89 virtual void close() = 0;
90
91 virtual int get_unique_id() const = 0;
92
93 virtual ConnectionStatus get_connection_status() const = 0;
94
95 uint32_t generate_unique_id() const;
96
97 MultiplayerPeer() {}
98};
99
100VARIANT_ENUM_CAST(MultiplayerPeer::ConnectionStatus);
101VARIANT_ENUM_CAST(MultiplayerPeer::TransferMode);
102
103class MultiplayerPeerExtension : public MultiplayerPeer {
104 GDCLASS(MultiplayerPeerExtension, MultiplayerPeer);
105
106protected:
107 static void _bind_methods();
108
109 PackedByteArray script_buffer;
110
111public:
112 /* PacketPeer extension */
113 virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
114 GDVIRTUAL2R(Error, _get_packet, GDExtensionConstPtr<const uint8_t *>, GDExtensionPtr<int>);
115 GDVIRTUAL0R(PackedByteArray, _get_packet_script); // For GDScript.
116
117 virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
118 GDVIRTUAL2R(Error, _put_packet, GDExtensionConstPtr<const uint8_t>, int);
119 GDVIRTUAL1R(Error, _put_packet_script, PackedByteArray); // For GDScript.
120
121 EXBIND0RC(int, get_available_packet_count);
122 EXBIND0RC(int, get_max_packet_size);
123
124 /* MultiplayerPeer extension */
125 virtual void set_refuse_new_connections(bool p_enable) override;
126 GDVIRTUAL1(_set_refuse_new_connections, bool); // Optional.
127
128 virtual bool is_refusing_new_connections() const override;
129 GDVIRTUAL0RC(bool, _is_refusing_new_connections); // Optional.
130
131 virtual bool is_server_relay_supported() const override;
132 GDVIRTUAL0RC(bool, _is_server_relay_supported); // Optional.
133
134 EXBIND1(set_transfer_channel, int);
135 EXBIND0RC(int, get_transfer_channel);
136 EXBIND1(set_transfer_mode, TransferMode);
137 EXBIND0RC(TransferMode, get_transfer_mode);
138 EXBIND1(set_target_peer, int);
139 EXBIND0RC(int, get_packet_peer);
140 EXBIND0RC(TransferMode, get_packet_mode);
141 EXBIND0RC(int, get_packet_channel);
142 EXBIND0RC(bool, is_server);
143 EXBIND0(poll);
144 EXBIND0(close);
145 EXBIND2(disconnect_peer, int, bool);
146 EXBIND0RC(int, get_unique_id);
147 EXBIND0RC(ConnectionStatus, get_connection_status);
148};
149
150#endif // MULTIPLAYER_PEER_H
151