Merge pull request #63163 from dsnopek/multiplayer-peer-custom-3.x

This commit is contained in:
Rémi Verschelde 2022-07-20 22:41:15 +02:00 committed by GitHub
commit 4ef99b409d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 336 additions and 12 deletions

View file

@ -0,0 +1,184 @@
/*************************************************************************/
/* networked_multiplayer_custom.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/io/networked_multiplayer_custom.h"
NetworkedMultiplayerCustom::NetworkedMultiplayerCustom() {
self_id = 0;
transfer_mode = TransferMode::TRANSFER_MODE_RELIABLE;
connection_status = ConnectionStatus::CONNECTION_DISCONNECTED;
refusing_new_connections = false;
target_id = 0;
// Default to a large value.
max_packet_size = 1 << 24;
}
NetworkedMultiplayerCustom::~NetworkedMultiplayerCustom() {
}
void NetworkedMultiplayerCustom::_bind_methods() {
ClassDB::bind_method(D_METHOD("initialize", "self_peer_id"), &NetworkedMultiplayerCustom::initialize);
ClassDB::bind_method(D_METHOD("set_max_packet_size", "max_packet_size"), &NetworkedMultiplayerCustom::set_max_packet_size);
ClassDB::bind_method(D_METHOD("set_connection_status", "connection_status"), &NetworkedMultiplayerCustom::set_connection_status);
ClassDB::bind_method(D_METHOD("deliver_packet", "buffer", "from_peer_id"), &NetworkedMultiplayerCustom::deliver_packet);
ADD_SIGNAL(MethodInfo("packet_generated", PropertyInfo(Variant::INT, "peer_id"), PropertyInfo(Variant::POOL_BYTE_ARRAY, "buffer"), PropertyInfo(Variant::INT, "transfer_mode")));
}
//
// PacketPeer
//
Error NetworkedMultiplayerCustom::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
ERR_FAIL_COND_V(incoming_packets.size() == 0, Error::ERR_UNAVAILABLE);
current_packet = incoming_packets.front()->get();
incoming_packets.pop_front();
*r_buffer = current_packet.data.read().ptr();
r_buffer_size = current_packet.data.size();
return Error::OK;
}
Error NetworkedMultiplayerCustom::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
PoolByteArray a;
a.resize(p_buffer_size);
PoolByteArray::Write w = a.write();
memcpy(w.ptr(), p_buffer, p_buffer_size);
emit_signal("packet_generated", target_id, a, transfer_mode);
return Error::OK;
}
int NetworkedMultiplayerCustom::get_available_packet_count() const {
return incoming_packets.size();
}
int NetworkedMultiplayerCustom::get_max_packet_size() const {
return max_packet_size;
}
//
// NetworkedMultiplayerPeer.
//
void NetworkedMultiplayerCustom::set_transfer_mode(NetworkedMultiplayerPeer::TransferMode p_mode) {
transfer_mode = p_mode;
}
NetworkedMultiplayerPeer::TransferMode NetworkedMultiplayerCustom::get_transfer_mode() const {
return transfer_mode;
}
void NetworkedMultiplayerCustom::set_target_peer(int p_target_peer) {
target_id = p_target_peer;
}
int NetworkedMultiplayerCustom::get_packet_peer() const {
ERR_FAIL_COND_V(connection_status != ConnectionStatus::CONNECTION_CONNECTED, 1);
ERR_FAIL_COND_V(incoming_packets.size() == 0, 1);
return incoming_packets.front()->get().from;
}
bool NetworkedMultiplayerCustom::is_server() const {
return self_id == 1;
}
void NetworkedMultiplayerCustom::poll() {
}
int NetworkedMultiplayerCustom::get_unique_id() const {
return self_id;
}
void NetworkedMultiplayerCustom::set_refuse_new_connections(bool p_refuse_new_connections) {
refusing_new_connections = p_refuse_new_connections;
}
bool NetworkedMultiplayerCustom::is_refusing_new_connections() const {
return refusing_new_connections;
}
NetworkedMultiplayerPeer::ConnectionStatus NetworkedMultiplayerCustom::get_connection_status() const {
return connection_status;
}
//
// Custom methods.
//
void NetworkedMultiplayerCustom::initialize(int p_self_id) {
if (connection_status != ConnectionStatus::CONNECTION_CONNECTING) {
return;
}
self_id = p_self_id;
if (self_id == 1) {
set_connection_status(ConnectionStatus::CONNECTION_CONNECTED);
}
}
void NetworkedMultiplayerCustom::set_max_packet_size(int p_max_packet_size) {
max_packet_size = p_max_packet_size;
}
void NetworkedMultiplayerCustom::set_connection_status(NetworkedMultiplayerPeer::ConnectionStatus p_connection_status) {
// Can only go to connecting, if we are disconnected.
if (p_connection_status == ConnectionStatus::CONNECTION_CONNECTING && connection_status == ConnectionStatus::CONNECTION_DISCONNECTED) {
connection_status = p_connection_status;
}
// Can only go to connected, if we are connecting.
else if (p_connection_status == ConnectionStatus::CONNECTION_CONNECTED && connection_status == ConnectionStatus::CONNECTION_CONNECTING) {
connection_status = p_connection_status;
emit_signal("connection_succeeded");
}
// Can only go to disconnected, if we aren't already disconnected.
else if (p_connection_status == ConnectionStatus::CONNECTION_DISCONNECTED && connection_status != ConnectionStatus::CONNECTION_DISCONNECTED) {
ConnectionStatus old_connection_status = connection_status;
connection_status = p_connection_status;
if (self_id != 1) {
if (old_connection_status == ConnectionStatus::CONNECTION_CONNECTING) {
emit_signal("connection_failed");
} else {
emit_signal("server_disconnected");
}
}
}
}
void NetworkedMultiplayerCustom::deliver_packet(const PoolByteArray &p_data, int p_from_peer_id) {
Packet p = { p_data, p_from_peer_id };
incoming_packets.push_back(p);
}

View file

@ -0,0 +1,87 @@
/*************************************************************************/
/* networked_multiplayer_custom.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef NETWORKED_MULTIPLAYER_CUSTOM_H
#define NETWORKED_MULTIPLAYER_CUSTOM_H
#include "core/io/networked_multiplayer_peer.h"
class NetworkedMultiplayerCustom : public NetworkedMultiplayerPeer {
GDCLASS(NetworkedMultiplayerCustom, NetworkedMultiplayerPeer);
protected:
int self_id;
TransferMode transfer_mode;
ConnectionStatus connection_status;
bool refusing_new_connections;
int target_id;
int max_packet_size;
struct Packet {
PoolVector<uint8_t> data;
int from;
};
List<Packet> incoming_packets;
Packet current_packet;
static void _bind_methods();
public:
NetworkedMultiplayerCustom();
~NetworkedMultiplayerCustom();
// PacketPeer.
Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
int get_available_packet_count() const;
int get_max_packet_size() const;
// NetworkedMultiplayerPeer.
void set_transfer_mode(TransferMode p_mode);
TransferMode get_transfer_mode() const;
void set_target_peer(int p_peer);
int get_packet_peer() const;
bool is_server() const;
void poll();
int get_unique_id() const;
void set_refuse_new_connections(bool p_enable);
bool is_refusing_new_connections() const;
ConnectionStatus get_connection_status() const;
// Custom methods.
void initialize(int p_self_id);
void set_max_packet_size(int p_max_packet_size);
void set_connection_status(ConnectionStatus p_connection_status);
void deliver_packet(const PoolByteArray &p_data, int p_from_peer_id);
};
#endif

View file

@ -46,6 +46,7 @@
#include "core/io/image_loader.h"
#include "core/io/marshalls.h"
#include "core/io/multiplayer_api.h"
#include "core/io/networked_multiplayer_custom.h"
#include "core/io/networked_multiplayer_peer.h"
#include "core/io/packet_peer.h"
#include "core/io/packet_peer_dtls.h"
@ -176,6 +177,7 @@ void register_core_types() {
ClassDB::register_virtual_class<PacketPeer>();
ClassDB::register_class<PacketPeerStream>();
ClassDB::register_virtual_class<NetworkedMultiplayerPeer>();
ClassDB::register_class<NetworkedMultiplayerCustom>();
ClassDB::register_class<MultiplayerAPI>();
ClassDB::register_class<MainLoop>();
ClassDB::register_class<Translation>();

View file

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="NetworkedMultiplayerCustom" inherits="NetworkedMultiplayerPeer" version="3.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
A [NetworkedMultiplayerPeer] implementation that can be controlled from a script.
</brief_description>
<description>
A [NetworkedMultiplayerPeer] implementation that can be used as a [member MultiplayerAPI.network_peer] and controlled from a script.
Its purpose is to allow adding a new backend for the high-Level multiplayer API without needing to use GDNative.
</description>
<tutorials>
</tutorials>
<methods>
<method name="deliver_packet">
<return type="void" />
<argument index="0" name="buffer" type="PoolByteArray" />
<argument index="1" name="from_peer_id" type="int" />
<description>
Deliver a packet to the local [MultiplayerAPI].
When your script receives a packet from other peers over the network (originating from the [signal packet_generated] signal on the sending peer), passing it to this method will deliver it locally.
</description>
</method>
<method name="initialize">
<return type="void" />
<argument index="0" name="self_peer_id" type="int" />
<description>
Initialize the peer with the given [code]peer_id[/code] (must be between 1 and 2147483647).
</description>
</method>
<method name="set_connection_status">
<return type="void" />
<argument index="0" name="connection_status" type="int" enum="NetworkedMultiplayerPeer.ConnectionStatus" />
<description>
Set the state of the connection. See [enum NetworkedMultiplayerPeer.ConnectionStatus].
</description>
</method>
<method name="set_max_packet_size">
<return type="void" />
<argument index="0" name="max_packet_size" type="int" />
<description>
Set the max packet size that this peer can handle.
</description>
</method>
</methods>
<signals>
<signal name="packet_generated">
<argument index="0" name="peer_id" type="int" />
<argument index="1" name="buffer" type="PoolByteArray" />
<argument index="2" name="transfer_mode" type="int" />
<description>
Emitted when the local [MultiplayerAPI] generates a packet.
Your script should take this packet and send it to the requested peer over the network (which should call [method deliver_packet] with the data when it's received).
</description>
</signal>
</signals>
<constants>
</constants>
</class>

View file

@ -46,10 +46,10 @@
</method>
</methods>
<members>
<member name="refuse_new_connections" type="bool" setter="set_refuse_new_connections" getter="is_refusing_new_connections" default="true">
<member name="refuse_new_connections" type="bool" setter="set_refuse_new_connections" getter="is_refusing_new_connections" default="false">
If [code]true[/code], this [NetworkedMultiplayerPeer] refuses new connections.
</member>
<member name="transfer_mode" type="int" setter="set_transfer_mode" getter="get_transfer_mode" enum="NetworkedMultiplayerPeer.TransferMode" default="0">
<member name="transfer_mode" type="int" setter="set_transfer_mode" getter="get_transfer_mode" enum="NetworkedMultiplayerPeer.TransferMode" default="2">
The manner in which to send packets to the [code]target_peer[/code]. See [enum TransferMode].
</member>
</members>

View file

@ -127,14 +127,12 @@
<member name="dtls_verify" type="bool" setter="set_dtls_verify_enabled" getter="is_dtls_verify_enabled" default="true">
Enable or disable certificate verification when [member use_dtls] is [code]true[/code].
</member>
<member name="refuse_new_connections" type="bool" setter="set_refuse_new_connections" getter="is_refusing_new_connections" overrides="NetworkedMultiplayerPeer" default="false" />
<member name="server_relay" type="bool" setter="set_server_relay_enabled" getter="is_server_relay_enabled" default="true">
Enable or disable the server feature that notifies clients of other peers' connection/disconnection, and relays messages between them. When this option is [code]false[/code], clients won't be automatically notified of other peers and won't be able to send them packets through the server.
</member>
<member name="transfer_channel" type="int" setter="set_transfer_channel" getter="get_transfer_channel" default="-1">
Set the default channel to be used to transfer data. By default, this value is [code]-1[/code] which means that ENet will only use 2 channels: one for reliable packets, and one for unreliable packets. The channel [code]0[/code] is reserved and cannot be used. Setting this member to any value between [code]0[/code] and [member channel_count] (excluded) will force ENet to use that channel for sending data. See [member channel_count] for more information about ENet channels.
</member>
<member name="transfer_mode" type="int" setter="set_transfer_mode" getter="get_transfer_mode" overrides="NetworkedMultiplayerPeer" enum="NetworkedMultiplayerPeer.TransferMode" default="2" />
<member name="use_dtls" type="bool" setter="set_dtls_enabled" getter="is_dtls_enabled" default="false">
When enabled, the client or server created by this peer, will use [PacketPeerDTLS] instead of raw UDP sockets for communicating with the remote peer. This will make the communication encrypted with DTLS at the cost of higher resource usage and potentially larger packet size.
[b]Note:[/b] When creating a DTLS server, make sure you setup the key/certificate pair via [method set_dtls_key] and [method set_dtls_certificate]. For DTLS clients, have a look at the [member dtls_verify] option, and configure the certificate accordingly via [method set_dtls_certificate].

View file

@ -8,6 +8,10 @@
</tutorials>
<methods>
</methods>
<members>
<member name="refuse_new_connections" type="bool" setter="set_refuse_new_connections" getter="is_refusing_new_connections" overrides="NetworkedMultiplayerPeer" default="true" />
<member name="transfer_mode" type="int" setter="set_transfer_mode" getter="get_transfer_mode" overrides="NetworkedMultiplayerPeer" enum="NetworkedMultiplayerPeer.TransferMode" default="0" />
</members>
<constants>
</constants>
</class>

View file

@ -65,10 +65,6 @@
</description>
</method>
</methods>
<members>
<member name="refuse_new_connections" type="bool" setter="set_refuse_new_connections" getter="is_refusing_new_connections" overrides="NetworkedMultiplayerPeer" default="false" />
<member name="transfer_mode" type="int" setter="set_transfer_mode" getter="get_transfer_mode" overrides="NetworkedMultiplayerPeer" enum="NetworkedMultiplayerPeer.TransferMode" default="2" />
</members>
<constants>
</constants>
</class>

View file

@ -30,10 +30,6 @@
</description>
</method>
</methods>
<members>
<member name="refuse_new_connections" type="bool" setter="set_refuse_new_connections" getter="is_refusing_new_connections" overrides="NetworkedMultiplayerPeer" default="false" />
<member name="transfer_mode" type="int" setter="set_transfer_mode" getter="get_transfer_mode" overrides="NetworkedMultiplayerPeer" enum="NetworkedMultiplayerPeer.TransferMode" default="2" />
</members>
<signals>
<signal name="peer_packet">
<argument index="0" name="peer_source" type="int" />