summaryrefslogtreecommitdiff
path: root/modules/enet
diff options
context:
space:
mode:
Diffstat (limited to 'modules/enet')
-rw-r--r--modules/enet/doc_classes/ENetConnection.xml6
-rw-r--r--modules/enet/doc_classes/ENetMultiplayerPeer.xml6
-rw-r--r--modules/enet/doc_classes/ENetPacketPeer.xml1
-rw-r--r--modules/enet/enet_connection.cpp88
-rw-r--r--modules/enet/enet_connection.h2
-rw-r--r--modules/enet/enet_multiplayer_peer.cpp302
-rw-r--r--modules/enet/enet_multiplayer_peer.h46
7 files changed, 222 insertions, 229 deletions
diff --git a/modules/enet/doc_classes/ENetConnection.xml b/modules/enet/doc_classes/ENetConnection.xml
index 00469ab44c..fcdf282a7d 100644
--- a/modules/enet/doc_classes/ENetConnection.xml
+++ b/modules/enet/doc_classes/ENetConnection.xml
@@ -51,7 +51,7 @@
<argument index="3" name="data" type="int" default="0" />
<description>
Initiates a connection to a foreign [code]address[/code] using the specified [code]port[/code] and allocting the requested [code]channels[/code]. Optional [code]data[/code] can be passed during connection in the form of a 32 bit integer.
- Note: You must call either [method create_host] or [method create_host_bound] before calling this method.
+ [b]Note:[/b] You must call either [method create_host] or [method create_host_bound] before calling this method.
</description>
</method>
<method name="create_host">
@@ -121,7 +121,7 @@
<return type="Array" />
<description>
Returns the list of peers associated with this host.
- Note: This list might include some peers that are not fully connected or are still being disconnected.
+ [b]Note:[/b] This list might include some peers that are not fully connected or are still being disconnected.
</description>
</method>
<method name="pop_statistic">
@@ -136,7 +136,7 @@
<argument index="0" name="refuse" type="bool" />
<description>
Configures the DTLS server to automatically drop new connections.
- Note: This method is only relevant after calling [method dtls_server_setup].
+ [b]Note:[/b] This method is only relevant after calling [method dtls_server_setup].
</description>
</method>
<method name="service">
diff --git a/modules/enet/doc_classes/ENetMultiplayerPeer.xml b/modules/enet/doc_classes/ENetMultiplayerPeer.xml
index 456b390dbb..d2456d3360 100644
--- a/modules/enet/doc_classes/ENetMultiplayerPeer.xml
+++ b/modules/enet/doc_classes/ENetMultiplayerPeer.xml
@@ -8,7 +8,7 @@
[b]Note:[/b] ENet only uses UDP, not TCP. When forwarding the server port to make your server accessible on the public Internet, you only need to forward the server port in UDP. You can use the [UPNP] class to try to forward the server port automatically when starting the server.
</description>
<tutorials>
- <link title="High-level multiplayer">https://docs.godotengine.org/en/latest/tutorials/networking/high_level_multiplayer.html</link>
+ <link title="High-level multiplayer">$DOCS_URL/tutorials/networking/high_level_multiplayer.html</link>
<link title="API documentation on the ENet website">http://enet.bespin.org/usergroup0.html</link>
</tutorials>
<methods>
@@ -18,7 +18,7 @@
<argument index="1" name="host" type="ENetConnection" />
<description>
Add a new remote peer with the given [code]peer_id[/code] connected to the given [code]host[/code].
- Note: The [code]host[/code] must have exactly one peer in the [constant ENetPacketPeer.STATE_CONNECTED] state.
+ [b]Note:[/b] The [code]host[/code] must have exactly one peer in the [constant ENetPacketPeer.STATE_CONNECTED] state.
</description>
</method>
<method name="close_connection">
@@ -77,10 +77,8 @@
<member name="host" type="ENetConnection" setter="" getter="get_host">
The underlying [ENetConnection] created after [method create_client] and [method create_server].
</member>
- <member name="refuse_new_connections" type="bool" setter="set_refuse_new_connections" getter="is_refusing_new_connections" override="true" 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_mode" type="int" setter="set_transfer_mode" getter="get_transfer_mode" override="true" enum="TransferMode" default="2" />
</members>
</class>
diff --git a/modules/enet/doc_classes/ENetPacketPeer.xml b/modules/enet/doc_classes/ENetPacketPeer.xml
index 8f0693fb01..4116ba17f2 100644
--- a/modules/enet/doc_classes/ENetPacketPeer.xml
+++ b/modules/enet/doc_classes/ENetPacketPeer.xml
@@ -6,6 +6,7 @@
<description>
A PacketPeer implementation representing a peer of an [ENetConnection].
This class cannot be instantiated directly but can be retrieved during [method ENetConnection.service] or via [method ENetConnection.get_peers].
+ [b]Note:[/b] When exporting to Android, make sure to enable the [code]INTERNET[/code] permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android.
</description>
<tutorials>
<link title="API documentation on the ENet website">http://enet.bespin.org/usergroup0.html</link>
diff --git a/modules/enet/enet_connection.cpp b/modules/enet/enet_connection.cpp
index e833264d6a..62f03b444b 100644
--- a/modules/enet/enet_connection.cpp
+++ b/modules/enet/enet_connection.cpp
@@ -117,58 +117,36 @@ Ref<ENetPacketPeer> ENetConnection::connect_to_host(const String &p_address, int
return out;
}
-ENetConnection::EventType ENetConnection::service(int p_timeout, Event &r_event) {
- ERR_FAIL_COND_V_MSG(!host, EVENT_ERROR, "The ENetConnection instance isn't currently active.");
- ERR_FAIL_COND_V(r_event.peer.is_valid(), EVENT_ERROR);
-
- // Drop peers that have already been disconnected.
- // NOTE: Forcibly disconnected peers (i.e. peers disconnected via
- // enet_peer_disconnect*) do not trigger DISCONNECTED events.
- List<Ref<ENetPacketPeer>>::Element *E = peers.front();
- while (E) {
- if (!E->get()->is_active()) {
- peers.erase(E->get());
- }
- E = E->next();
- }
-
- ENetEvent event;
- int ret = enet_host_service(host, &event, p_timeout);
-
- if (ret < 0) {
- return EVENT_ERROR;
- } else if (ret == 0) {
- return EVENT_NONE;
- }
- switch (event.type) {
+ENetConnection::EventType ENetConnection::_parse_event(const ENetEvent &p_event, Event &r_event) {
+ switch (p_event.type) {
case ENET_EVENT_TYPE_CONNECT: {
- if (event.peer->data == nullptr) {
- Ref<ENetPacketPeer> pp = memnew(ENetPacketPeer(event.peer));
+ if (p_event.peer->data == nullptr) {
+ Ref<ENetPacketPeer> pp = memnew(ENetPacketPeer(p_event.peer));
peers.push_back(pp);
}
- r_event.peer = Ref<ENetPacketPeer>((ENetPacketPeer *)event.peer->data);
- r_event.data = event.data;
+ r_event.peer = Ref<ENetPacketPeer>((ENetPacketPeer *)p_event.peer->data);
+ r_event.data = p_event.data;
return EVENT_CONNECT;
} break;
case ENET_EVENT_TYPE_DISCONNECT: {
// A peer disconnected.
- if (event.peer->data != nullptr) {
- Ref<ENetPacketPeer> pp = Ref<ENetPacketPeer>((ENetPacketPeer *)event.peer->data);
+ if (p_event.peer->data != nullptr) {
+ Ref<ENetPacketPeer> pp = Ref<ENetPacketPeer>((ENetPacketPeer *)p_event.peer->data);
pp->_on_disconnect();
peers.erase(pp);
r_event.peer = pp;
- r_event.data = event.data;
+ r_event.data = p_event.data;
return EVENT_DISCONNECT;
}
return EVENT_ERROR;
} break;
case ENET_EVENT_TYPE_RECEIVE: {
// Packet reveived.
- if (event.peer->data != nullptr) {
- Ref<ENetPacketPeer> pp = Ref<ENetPacketPeer>((ENetPacketPeer *)event.peer->data);
- r_event.peer = Ref<ENetPacketPeer>((ENetPacketPeer *)event.peer->data);
- r_event.channel_id = event.channelID;
- r_event.packet = event.packet;
+ if (p_event.peer->data != nullptr) {
+ Ref<ENetPacketPeer> pp = Ref<ENetPacketPeer>((ENetPacketPeer *)p_event.peer->data);
+ r_event.peer = Ref<ENetPacketPeer>((ENetPacketPeer *)p_event.peer->data);
+ r_event.channel_id = p_event.channelID;
+ r_event.packet = p_event.packet;
return EVENT_RECEIVE;
}
return EVENT_ERROR;
@@ -180,6 +158,44 @@ ENetConnection::EventType ENetConnection::service(int p_timeout, Event &r_event)
}
}
+ENetConnection::EventType ENetConnection::service(int p_timeout, Event &r_event) {
+ ERR_FAIL_COND_V_MSG(!host, EVENT_ERROR, "The ENetConnection instance isn't currently active.");
+ ERR_FAIL_COND_V(r_event.peer.is_valid(), EVENT_ERROR);
+
+ // Drop peers that have already been disconnected.
+ // NOTE: Forcibly disconnected peers (i.e. peers disconnected via
+ // enet_peer_disconnect*) do not trigger DISCONNECTED events.
+ List<Ref<ENetPacketPeer>>::Element *E = peers.front();
+ while (E) {
+ if (!E->get()->is_active()) {
+ peers.erase(E->get());
+ }
+ E = E->next();
+ }
+
+ ENetEvent event;
+ int ret = enet_host_service(host, &event, p_timeout);
+
+ if (ret < 0) {
+ return EVENT_ERROR;
+ } else if (ret == 0) {
+ return EVENT_NONE;
+ }
+ return _parse_event(event, r_event);
+}
+
+int ENetConnection::check_events(EventType &r_type, Event &r_event) {
+ ERR_FAIL_COND_V_MSG(!host, -1, "The ENetConnection instance isn't currently active.");
+ ENetEvent event;
+ int ret = enet_host_check_events(host, &event);
+ if (ret < 0) {
+ r_type = EVENT_ERROR;
+ return ret;
+ }
+ r_type = _parse_event(event, r_event);
+ return ret;
+}
+
void ENetConnection::flush() {
ERR_FAIL_COND_MSG(!host, "The ENetConnection instance isn't currently active.");
enet_host_flush(host);
diff --git a/modules/enet/enet_connection.h b/modules/enet/enet_connection.h
index 0f7744953e..bffda81906 100644
--- a/modules/enet/enet_connection.h
+++ b/modules/enet/enet_connection.h
@@ -79,6 +79,7 @@ private:
ENetHost *host = nullptr;
List<Ref<ENetPacketPeer>> peers;
+ EventType _parse_event(const ENetEvent &p_event, Event &r_event);
Error _create(ENetAddress *p_address, int p_max_peers, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth);
Array _service(int p_timeout = 0);
void _broadcast(int p_channel, PackedByteArray p_packet, int p_flags);
@@ -110,6 +111,7 @@ public:
void destroy();
Ref<ENetPacketPeer> connect_to_host(const String &p_address, int p_port, int p_channels, int p_data = 0);
EventType service(int p_timeout, Event &r_event);
+ int check_events(EventType &r_type, Event &r_event);
void flush();
void bandwidth_limit(int p_in_bandwidth = 0, int p_out_bandwidth = 0);
void channel_limit(int p_max_channels);
diff --git a/modules/enet/enet_multiplayer_peer.cpp b/modules/enet/enet_multiplayer_peer.cpp
index afd31207f6..cb22d349f8 100644
--- a/modules/enet/enet_multiplayer_peer.cpp
+++ b/modules/enet/enet_multiplayer_peer.cpp
@@ -33,22 +33,6 @@
#include "core/io/marshalls.h"
#include "core/os/os.h"
-void ENetMultiplayerPeer::set_transfer_channel(int p_channel) {
- transfer_channel = p_channel;
-}
-
-int ENetMultiplayerPeer::get_transfer_channel() const {
- return transfer_channel;
-}
-
-void ENetMultiplayerPeer::set_transfer_mode(Multiplayer::TransferMode p_mode) {
- transfer_mode = p_mode;
-}
-
-Multiplayer::TransferMode ENetMultiplayerPeer::get_transfer_mode() const {
- return transfer_mode;
-}
-
void ENetMultiplayerPeer::set_target_peer(int p_peer) {
target_peer = p_peer;
}
@@ -62,6 +46,7 @@ int ENetMultiplayerPeer::get_packet_peer() const {
Error ENetMultiplayerPeer::create_server(int p_port, int p_max_clients, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth) {
ERR_FAIL_COND_V_MSG(_is_active(), ERR_ALREADY_IN_USE, "The multiplayer instance is already active.");
+ set_refuse_new_connections(false);
Ref<ENetConnection> host;
host.instantiate();
Error err = host->create_host_bound(bind_ip, p_port, p_max_clients, 0, p_max_channels > 0 ? p_max_channels + SYSCH_MAX : 0, p_out_bandwidth);
@@ -70,7 +55,6 @@ Error ENetMultiplayerPeer::create_server(int p_port, int p_max_clients, int p_ma
}
active_mode = MODE_SERVER;
- refuse_connections = false;
unique_id = 1;
connection_status = CONNECTION_CONNECTED;
hosts[0] = host;
@@ -79,6 +63,7 @@ Error ENetMultiplayerPeer::create_server(int p_port, int p_max_clients, int p_ma
Error ENetMultiplayerPeer::create_client(const String &p_address, int p_port, int p_channel_count, int p_in_bandwidth, int p_out_bandwidth, int p_local_port) {
ERR_FAIL_COND_V_MSG(_is_active(), ERR_ALREADY_IN_USE, "The multiplayer instance is already active.");
+ set_refuse_new_connections(false);
Ref<ENetConnection> host;
host.instantiate();
Error err;
@@ -102,7 +87,6 @@ Error ENetMultiplayerPeer::create_client(const String &p_address, int p_port, in
// Need to wait for CONNECT event.
connection_status = CONNECTION_CONNECTING;
active_mode = MODE_CLIENT;
- refuse_connections = false;
peers[1] = peer;
hosts[0] = host;
@@ -113,7 +97,6 @@ Error ENetMultiplayerPeer::create_mesh(int p_id) {
ERR_FAIL_COND_V_MSG(p_id <= 0, ERR_INVALID_PARAMETER, "The unique ID must be greater then 0");
ERR_FAIL_COND_V_MSG(_is_active(), ERR_ALREADY_IN_USE, "The multiplayer instance is already active.");
active_mode = MODE_MESH;
- refuse_connections = false;
unique_id = p_id;
connection_status = CONNECTION_CONNECTED;
return OK;
@@ -131,32 +114,21 @@ Error ENetMultiplayerPeer::add_mesh_peer(int p_id, Ref<ENetConnection> p_host) {
return OK;
}
-bool ENetMultiplayerPeer::_poll_server() {
- for (const KeyValue<int, Ref<ENetPacketPeer>> &E : peers) {
- if (!(E.value->is_active())) {
- emit_signal(SNAME("peer_disconnected"), E.value->get_meta(SNAME("_net_id")));
- peers.erase(E.key);
- }
- }
- ENetConnection::Event event;
- ENetConnection::EventType ret = hosts[0]->service(0, event);
- if (ret == ENetConnection::EVENT_ERROR) {
- return true;
- }
- switch (ret) {
+bool ENetMultiplayerPeer::_parse_server_event(ENetConnection::EventType p_type, ENetConnection::Event &p_event) {
+ switch (p_type) {
case ENetConnection::EVENT_CONNECT: {
- if (refuse_connections) {
- event.peer->reset();
+ if (is_refusing_new_connections()) {
+ p_event.peer->reset();
return false;
}
// Client joined with invalid ID, probably trying to exploit us.
- if (event.data < 2 || peers.has((int)event.data)) {
- event.peer->reset();
+ if (p_event.data < 2 || peers.has((int)p_event.data)) {
+ p_event.peer->reset();
return false;
}
- int id = event.data;
- event.peer->set_meta(SNAME("_net_id"), id);
- peers[id] = event.peer;
+ int id = p_event.data;
+ p_event.peer->set_meta(SNAME("_net_id"), id);
+ peers[id] = p_event.peer;
emit_signal(SNAME("peer_connected"), id);
if (server_relay) {
@@ -165,7 +137,7 @@ bool ENetMultiplayerPeer::_poll_server() {
return false;
}
case ENetConnection::EVENT_DISCONNECT: {
- int id = event.peer->get_meta(SNAME("_net_id"));
+ int id = p_event.peer->get_meta(SNAME("_net_id"));
if (!peers.has(id)) {
// Never fully connected.
return false;
@@ -173,34 +145,34 @@ bool ENetMultiplayerPeer::_poll_server() {
emit_signal(SNAME("peer_disconnected"), id);
peers.erase(id);
- if (!server_relay) {
+ if (server_relay) {
_notify_peers(id, false);
}
return false;
}
case ENetConnection::EVENT_RECEIVE: {
- if (event.channel_id == SYSCH_CONFIG) {
- _destroy_unused(event.packet);
+ if (p_event.channel_id == SYSCH_CONFIG) {
+ _destroy_unused(p_event.packet);
ERR_FAIL_V_MSG(false, "Only server can send config messages");
} else {
- if (event.packet->dataLength < 8) {
- _destroy_unused(event.packet);
+ if (p_event.packet->dataLength < 8) {
+ _destroy_unused(p_event.packet);
ERR_FAIL_V_MSG(false, "Invalid packet size");
}
- uint32_t source = decode_uint32(&event.packet->data[0]);
- int target = decode_uint32(&event.packet->data[4]);
+ uint32_t source = decode_uint32(&p_event.packet->data[0]);
+ int target = decode_uint32(&p_event.packet->data[4]);
- uint32_t id = event.peer->get_meta(SNAME("_net_id"));
+ uint32_t id = p_event.peer->get_meta(SNAME("_net_id"));
// Someone is cheating and trying to fake the source!
if (source != id) {
- _destroy_unused(event.packet);
+ _destroy_unused(p_event.packet);
ERR_FAIL_V_MSG(false, "Someone is cheating and trying to fake the source!");
}
Packet packet;
- packet.packet = event.packet;
- packet.channel = event.channel_id;
+ packet.packet = p_event.packet;
+ packet.channel = p_event.channel_id;
packet.from = id;
// Even if relaying is disabled, these targets are valid as incoming packets.
@@ -211,9 +183,9 @@ bool ENetMultiplayerPeer::_poll_server() {
if (server_relay && target != 1) {
packet.packet->referenceCount++;
- _relay(source, target, event.channel_id, event.packet);
+ _relay(source, target, p_event.channel_id, p_event.packet);
packet.packet->referenceCount--;
- _destroy_unused(event.packet);
+ _destroy_unused(p_event.packet);
}
// Destroy packet later
}
@@ -224,23 +196,8 @@ bool ENetMultiplayerPeer::_poll_server() {
}
}
-bool ENetMultiplayerPeer::_poll_client() {
- if (peers.has(1) && !peers[1]->is_active()) {
- if (connection_status == CONNECTION_CONNECTED) {
- // Client just disconnected from server.
- emit_signal(SNAME("server_disconnected"));
- } else {
- emit_signal(SNAME("connection_failed"));
- }
- close_connection();
- return true;
- }
- ENetConnection::Event event;
- ENetConnection::EventType ret = hosts[0]->service(0, event);
- if (ret == ENetConnection::EVENT_ERROR) {
- return true;
- }
- switch (ret) {
+bool ENetMultiplayerPeer::_parse_client_event(ENetConnection::EventType p_type, ENetConnection::Event &p_event) {
+ switch (p_type) {
case ENetConnection::EVENT_CONNECT: {
connection_status = CONNECTION_CONNECTED;
emit_signal(SNAME("peer_connected"), 1);
@@ -258,15 +215,15 @@ bool ENetMultiplayerPeer::_poll_client() {
return true;
}
case ENetConnection::EVENT_RECEIVE: {
- if (event.channel_id == SYSCH_CONFIG) {
+ if (p_event.channel_id == SYSCH_CONFIG) {
// Config message
- if (event.packet->dataLength != 8) {
- _destroy_unused(event.packet);
+ if (p_event.packet->dataLength != 8) {
+ _destroy_unused(p_event.packet);
ERR_FAIL_V(false);
}
- int msg = decode_uint32(&event.packet->data[0]);
- int id = decode_uint32(&event.packet->data[4]);
+ int msg = decode_uint32(&p_event.packet->data[0]);
+ int id = decode_uint32(&p_event.packet->data[4]);
switch (msg) {
case SYSMSG_ADD_PEER: {
@@ -279,18 +236,18 @@ bool ENetMultiplayerPeer::_poll_client() {
emit_signal(SNAME("peer_disconnected"), id);
} break;
}
- _destroy_unused(event.packet);
+ _destroy_unused(p_event.packet);
} else {
- if (event.packet->dataLength < 8) {
- _destroy_unused(event.packet);
+ if (p_event.packet->dataLength < 8) {
+ _destroy_unused(p_event.packet);
ERR_FAIL_V_MSG(false, "Invalid packet size");
}
- uint32_t source = decode_uint32(&event.packet->data[0]);
+ uint32_t source = decode_uint32(&p_event.packet->data[0]);
Packet packet;
- packet.packet = event.packet;
+ packet.packet = p_event.packet;
packet.from = source;
- packet.channel = event.channel_id;
+ packet.channel = p_event.channel_id;
packet.packet->referenceCount++;
incoming_packets.push_back(packet);
@@ -303,61 +260,37 @@ bool ENetMultiplayerPeer::_poll_client() {
}
}
-bool ENetMultiplayerPeer::_poll_mesh() {
- for (const KeyValue<int, Ref<ENetPacketPeer>> &E : peers) {
- if (!(E.value->is_active())) {
- emit_signal(SNAME("peer_disconnected"), E.key);
- peers.erase(E.key);
- if (hosts.has(E.key)) {
- hosts.erase(E.key);
+bool ENetMultiplayerPeer::_parse_mesh_event(ENetConnection::EventType p_type, ENetConnection::Event &p_event, int p_peer_id) {
+ switch (p_type) {
+ case ENetConnection::EVENT_CONNECT:
+ p_event.peer->reset();
+ return false;
+ case ENetConnection::EVENT_DISCONNECT:
+ if (peers.has(p_peer_id)) {
+ emit_signal(SNAME("peer_disconnected"), p_peer_id);
+ peers.erase(p_peer_id);
}
- }
- }
- bool should_stop = true;
- for (KeyValue<int, Ref<ENetConnection>> &E : hosts) {
- ENetConnection::Event event;
- ENetConnection::EventType ret = E.value->service(0, event);
- if (ret == ENetConnection::EVENT_ERROR) {
- if (peers.has(E.key)) {
- emit_signal(SNAME("peer_disconnected"), E.key);
- peers.erase(E.key);
+ hosts.erase(p_peer_id);
+ return true;
+ case ENetConnection::EVENT_RECEIVE: {
+ if (p_event.packet->dataLength < 8) {
+ _destroy_unused(p_event.packet);
+ ERR_FAIL_V_MSG(false, "Invalid packet size");
}
- hosts.erase(E.key);
- continue;
- }
- switch (ret) {
- case ENetConnection::EVENT_CONNECT:
- should_stop = false;
- event.peer->reset();
- break;
- case ENetConnection::EVENT_DISCONNECT:
- should_stop = false;
- if (peers.has(E.key)) {
- emit_signal(SNAME("peer_disconnected"), E.key);
- peers.erase(E.key);
- }
- hosts.erase(E.key);
- break;
- case ENetConnection::EVENT_RECEIVE: {
- should_stop = false;
- if (event.packet->dataLength < 8) {
- _destroy_unused(event.packet);
- ERR_CONTINUE_MSG(true, "Invalid packet size");
- }
- Packet packet;
- packet.packet = event.packet;
- packet.from = E.key;
- packet.channel = event.channel_id;
+ Packet packet;
+ packet.packet = p_event.packet;
+ packet.from = p_peer_id;
+ packet.channel = p_event.channel_id;
- packet.packet->referenceCount++;
- incoming_packets.push_back(packet);
- } break;
- default:
- break; // Nothing to do
- }
+ packet.packet->referenceCount++;
+ incoming_packets.push_back(packet);
+ return false;
+ } break;
+ default:
+ // Nothing to do
+ return true;
}
- return should_stop;
}
void ENetMultiplayerPeer::poll() {
@@ -365,26 +298,77 @@ void ENetMultiplayerPeer::poll() {
_pop_current_packet();
- while (true) {
- switch (active_mode) {
- case MODE_CLIENT:
- if (_poll_client()) {
- return;
+ switch (active_mode) {
+ case MODE_CLIENT: {
+ if (peers.has(1) && !peers[1]->is_active()) {
+ if (connection_status == CONNECTION_CONNECTED) {
+ // Client just disconnected from server.
+ emit_signal(SNAME("server_disconnected"));
+ } else {
+ emit_signal(SNAME("connection_failed"));
}
- break;
- case MODE_SERVER:
- if (_poll_server()) {
+ close_connection();
+ return;
+ }
+ ENetConnection::Event event;
+ ENetConnection::EventType ret = hosts[0]->service(0, event);
+ if (ret == ENetConnection::EVENT_ERROR) {
+ return;
+ }
+ do {
+ if (_parse_client_event(ret, event)) {
return;
}
- break;
- case MODE_MESH:
- if (_poll_mesh()) {
- return;
+ } while (hosts[0]->check_events(ret, event) > 0);
+ } break;
+ case MODE_SERVER: {
+ for (const KeyValue<int, Ref<ENetPacketPeer>> &E : peers) {
+ if (!(E.value->is_active())) {
+ emit_signal(SNAME("peer_disconnected"), E.value->get_meta(SNAME("_net_id")));
+ peers.erase(E.key);
}
- break;
- default:
+ }
+ ENetConnection::Event event;
+ ENetConnection::EventType ret = hosts[0]->service(0, event);
+ if (ret == ENetConnection::EVENT_ERROR) {
return;
- }
+ }
+ do {
+ if (_parse_server_event(ret, event)) {
+ return;
+ }
+ } while (hosts[0]->check_events(ret, event) > 0);
+ } break;
+ case MODE_MESH: {
+ for (const KeyValue<int, Ref<ENetPacketPeer>> &E : peers) {
+ if (!(E.value->is_active())) {
+ emit_signal(SNAME("peer_disconnected"), E.key);
+ peers.erase(E.key);
+ if (hosts.has(E.key)) {
+ hosts.erase(E.key);
+ }
+ }
+ }
+ for (KeyValue<int, Ref<ENetConnection>> &E : hosts) {
+ ENetConnection::Event event;
+ ENetConnection::EventType ret = E.value->service(0, event);
+ if (ret == ENetConnection::EVENT_ERROR) {
+ if (peers.has(E.key)) {
+ emit_signal(SNAME("peer_disconnected"), E.key);
+ peers.erase(E.key);
+ }
+ hosts.erase(E.key);
+ continue;
+ }
+ do {
+ if (_parse_mesh_event(ret, event, E.key)) {
+ break; // Keep polling the others.
+ }
+ } while (E.value->check_events(ret, event) > 0);
+ }
+ } break;
+ default:
+ return;
}
}
@@ -423,6 +407,7 @@ void ENetMultiplayerPeer::close_connection(uint32_t wait_usec) {
hosts.clear();
unique_id = 0;
connection_status = CONNECTION_DISCONNECTED;
+ set_refuse_new_connections(false);
}
int ENetMultiplayerPeer::get_available_packet_count() const {
@@ -451,16 +436,17 @@ Error ENetMultiplayerPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size
int packet_flags = 0;
int channel = SYSCH_RELIABLE;
+ int transfer_channel = get_transfer_channel();
if (transfer_channel > 0) {
channel = SYSCH_MAX + transfer_channel - 1;
} else {
- switch (transfer_mode) {
+ switch (get_transfer_mode()) {
case Multiplayer::TRANSFER_MODE_UNRELIABLE: {
- packet_flags = ENET_PACKET_FLAG_UNSEQUENCED;
+ packet_flags = ENET_PACKET_FLAG_UNSEQUENCED | ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT;
channel = SYSCH_UNRELIABLE;
} break;
- case Multiplayer::TRANSFER_MODE_ORDERED: {
- packet_flags = 0;
+ case Multiplayer::TRANSFER_MODE_UNRELIABLE_ORDERED: {
+ packet_flags = ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT;
channel = SYSCH_UNRELIABLE;
} break;
case Multiplayer::TRANSFER_MODE_RELIABLE: {
@@ -470,6 +456,12 @@ Error ENetMultiplayerPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size
}
}
+#ifdef DEBUG_ENABLED
+ if ((packet_flags & ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT) && p_buffer_size + 8 > ENET_HOST_DEFAULT_MTU) {
+ WARN_PRINT_ONCE(vformat("Sending %d bytes unrealiably which is above the MTU (%d), this will result in higher packet loss", p_buffer_size + 8, ENET_HOST_DEFAULT_MTU));
+ }
+#endif
+
ENetPacket *packet = enet_packet_create(nullptr, p_buffer_size + 8, packet_flags);
encode_uint32(unique_id, &packet->data[0]); // Source ID
encode_uint32(target_peer, &packet->data[4]); // Dest ID
@@ -545,19 +537,15 @@ int ENetMultiplayerPeer::get_unique_id() const {
return unique_id;
}
-void ENetMultiplayerPeer::set_refuse_new_connections(bool p_enable) {
- refuse_connections = p_enable;
+void ENetMultiplayerPeer::set_refuse_new_connections(bool p_enabled) {
#ifdef GODOT_ENET
if (_is_active()) {
for (KeyValue<int, Ref<ENetConnection>> &E : hosts) {
- E.value->refuse_new_connections(p_enable);
+ E.value->refuse_new_connections(p_enabled);
}
}
#endif
-}
-
-bool ENetMultiplayerPeer::is_refusing_new_connections() const {
- return refuse_connections;
+ MultiplayerPeer::set_refuse_new_connections(p_enabled);
}
void ENetMultiplayerPeer::set_server_relay_enabled(bool p_enabled) {
diff --git a/modules/enet/enet_multiplayer_peer.h b/modules/enet/enet_multiplayer_peer.h
index b5316b8292..abec1e432e 100644
--- a/modules/enet/enet_multiplayer_peer.h
+++ b/modules/enet/enet_multiplayer_peer.h
@@ -65,10 +65,7 @@ private:
uint32_t unique_id = 0;
int target_peer = 0;
- int transfer_channel = 0;
- Multiplayer::TransferMode transfer_mode = Multiplayer::TRANSFER_MODE_RELIABLE;
- bool refuse_connections = false;
bool server_relay = true;
ConnectionStatus connection_status = CONNECTION_DISCONNECTED;
@@ -87,9 +84,9 @@ private:
Packet current_packet;
void _pop_current_packet();
- bool _poll_server();
- bool _poll_client();
- bool _poll_mesh();
+ bool _parse_server_event(ENetConnection::EventType p_event_type, ENetConnection::Event &p_event);
+ bool _parse_client_event(ENetConnection::EventType p_event_type, ENetConnection::Event &p_event);
+ bool _parse_mesh_event(ENetConnection::EventType p_event_type, ENetConnection::Event &p_event, int p_peer_id);
void _relay(int p_from, int p_to, enet_uint8 p_channel, ENetPacket *p_packet);
void _notify_peers(int p_id, bool p_connected);
void _destroy_unused(ENetPacket *p_packet);
@@ -101,40 +98,31 @@ protected:
static void _bind_methods();
public:
- virtual void set_transfer_channel(int p_channel) override;
- virtual int get_transfer_channel() const override;
-
- virtual void set_transfer_mode(Multiplayer::TransferMode p_mode) override;
- virtual Multiplayer::TransferMode get_transfer_mode() const override;
virtual void set_target_peer(int p_peer) override;
-
virtual int get_packet_peer() const override;
- Error create_server(int p_port, int p_max_clients = 32, int p_max_channels = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
- Error create_client(const String &p_address, int p_port, int p_channel_count = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0, int p_local_port = 0);
- Error create_mesh(int p_id);
- Error add_mesh_peer(int p_id, Ref<ENetConnection> p_host);
-
- void close_connection(uint32_t wait_usec = 100);
-
- void disconnect_peer(int p_peer, bool now = false);
-
virtual void poll() override;
-
virtual bool is_server() const override;
+ // Overriden so we can instrument the DTLSServer when needed.
+ virtual void set_refuse_new_connections(bool p_enabled) override;
- virtual int get_available_packet_count() const override;
- virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
- virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
+ virtual ConnectionStatus get_connection_status() const override;
+
+ virtual int get_unique_id() const override;
virtual int get_max_packet_size() const override;
+ virtual int get_available_packet_count() const override;
+ virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override;
+ virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
- virtual ConnectionStatus get_connection_status() const override;
+ Error create_server(int p_port, int p_max_clients = 32, int p_max_channels = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
+ Error create_client(const String &p_address, int p_port, int p_channel_count = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0, int p_local_port = 0);
+ Error create_mesh(int p_id);
+ Error add_mesh_peer(int p_id, Ref<ENetConnection> p_host);
- virtual void set_refuse_new_connections(bool p_enable) override;
- virtual bool is_refusing_new_connections() const override;
+ void close_connection(uint32_t wait_usec = 100);
- virtual int get_unique_id() const override;
+ void disconnect_peer(int p_peer, bool now = false);
void set_bind_ip(const IPAddress &p_ip);
void set_server_relay_enabled(bool p_enabled);