Skip to content

Commit 6f256fa

Browse files
committed
Add method to get bound local address to ENetConnection
1 parent 0870525 commit 6f256fa

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

modules/enet/doc_classes/ENetConnection.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
Sends any queued packets on the host specified to its designated peers.
107107
</description>
108108
</method>
109+
<method name="get_local_address" qualifiers="const">
110+
<return type="String" />
111+
<description>
112+
Returns the local address to which this peer is bound.
113+
</description>
114+
</method>
109115
<method name="get_local_port" qualifiers="const">
110116
<return type="int" />
111117
<description>

modules/enet/enet_connection.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,24 @@ int ENetConnection::get_max_channels() const {
250250
return host->channelLimit;
251251
}
252252

253+
IPAddress ENetConnection::get_local_address() const {
254+
ERR_FAIL_NULL_V_MSG(host, IPAddress(), "The ENetConnection instance isn't currently active.");
255+
ERR_FAIL_COND_V_MSG(!(host->socket), IPAddress(), "The ENetConnection instance isn't currently bound.");
256+
ENetAddress address;
257+
ERR_FAIL_COND_V_MSG(enet_socket_get_address(host->socket, &address), IPAddress(), "Unable to get socket address");
258+
259+
IPAddress out;
260+
#ifdef GODOT_ENET
261+
out.set_ipv6((uint8_t *)&(address.host));
262+
if (out == IPAddress("::")) {
263+
return IPAddress("*");
264+
}
265+
#else
266+
out.set_ipv4((uint8_t *)&(address.host));
267+
#endif
268+
return out;
269+
}
270+
253271
int ENetConnection::get_local_port() const {
254272
ERR_FAIL_NULL_V_MSG(host, 0, "The ENetConnection instance isn't currently active.");
255273
ERR_FAIL_COND_V_MSG(!(host->socket), 0, "The ENetConnection instance isn't currently bound.");
@@ -387,6 +405,7 @@ void ENetConnection::_bind_methods() {
387405
ClassDB::bind_method(D_METHOD("refuse_new_connections", "refuse"), &ENetConnection::refuse_new_connections);
388406
ClassDB::bind_method(D_METHOD("pop_statistic", "statistic"), &ENetConnection::pop_statistic);
389407
ClassDB::bind_method(D_METHOD("get_max_channels"), &ENetConnection::get_max_channels);
408+
ClassDB::bind_method(D_METHOD("get_local_address"), &ENetConnection::get_local_address);
390409
ClassDB::bind_method(D_METHOD("get_local_port"), &ENetConnection::get_local_port);
391410
ClassDB::bind_method(D_METHOD("get_peers"), &ENetConnection::_get_peers);
392411
ClassDB::bind_method(D_METHOD("socket_send", "destination_address", "destination_port", "packet"), &ENetConnection::socket_send);

modules/enet/enet_connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class ENetConnection : public RefCounted {
125125

126126
// Extras
127127
void get_peers(List<Ref<ENetPacketPeer>> &r_peers);
128+
IPAddress get_local_address() const;
128129
int get_local_port() const;
129130

130131
// Godot additions

0 commit comments

Comments
 (0)