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