UPNP network functions.
Provides UPNP functionality to discover [UPNPDevice]s on the local network and execute commands on them, like managing port mappings (port forwarding) and querying the local and remote network IP address. Note that methods on this class are synchronous and block the calling thread.
To forward a specific port:
[codeblock]
const PORT = 7777
var upnp = UPNP.new()
upnp.discover(2000, 2, "InternetGatewayDevice")
upnp.add_port_mapping(port)
[/codeblock]
To close a specific port (e.g. after you have finished using it):
[codeblock]
upnp.delete_port_mapping(port)
[/codeblock]
[b]Note:[/b] UPnP discovery blocks the current thread. To perform discovery without blocking the main thread, use [Thread]s like this:
[codeblock]
# Emitted when UPnP port mapping setup is completed (regardless of success or failure).
signal upnp_completed(error)
# Replace this with your own server port number between 1025 and 65535.
const SERVER_PORT = 3928
var thread = null
func _upnp_setup(server_port):
# UPNP queries take some time.
var upnp = UPNP.new()
var err = upnp.discover()
if err != OK:
push_error(str(err))
emit_signal("upnp_completed", err)
return
if upnp.get_gateway() and upnp.get_gateway().is_valid_gateway():
upnp.add_port_mapping(server_port, server_port, ProjectSettings.get_setting("application/config/name"), "UDP")
upnp.add_port_mapping(server_port, server_port, ProjectSettings.get_setting("application/config/name"), "TCP")
emit_signal("upnp_completed", OK)
func _ready():
thread = Thread.new()
thread.start(self, "_upnp_setup", SERVER_PORT)
func _exit_tree():
# Wait for thread finish here to handle game exit while the thread is running.
thread.wait_to_finish()
[/codeblock]
Adds the given [UPNPDevice] to the list of discovered devices.
Adds a mapping to forward the external [code]port[/code] (between 1 and 65535) on the default gateway (see [method get_gateway]) to the [code]internal_port[/code] on the local machine for the given protocol [code]proto[/code] (either [code]TCP[/code] or [code]UDP[/code], with UDP being the default). If a port mapping for the given port and protocol combination already exists on that gateway device, this method tries to overwrite it. If that is not desired, you can retrieve the gateway manually with [method get_gateway] and call [method add_port_mapping] on it, if any.
If [code]internal_port[/code] is [code]0[/code] (the default), the same port number is used for both the external and the internal port (the [code]port[/code] value).
The description ([code]desc[/code]) is shown in some router UIs and can be used to point out which application added the mapping. The mapping's lease duration can be limited by specifying a [code]duration[/code] (in seconds). However, some routers are incompatible with one or both of these, so use with caution and add fallback logic in case of errors to retry without them if in doubt.
See [enum UPNPResult] for possible return values.
Clears the list of discovered devices.
Deletes the port mapping for the given port and protocol combination on the default gateway (see [method get_gateway]) if one exists. [code]port[/code] must be a valid port between 1 and 65535, [code]proto[/code] can be either [code]TCP[/code] or [code]UDP[/code]. See [enum UPNPResult] for possible return values.
Discovers local [UPNPDevice]s. Clears the list of previously discovered devices.
Filters for IGD (InternetGatewayDevice) type devices by default, as those manage port forwarding. [code]timeout[/code] is the time to wait for responses in milliseconds. [code]ttl[/code] is the time-to-live; only touch this if you know what you're doing.
See [enum UPNPResult] for possible return values.
Returns the [UPNPDevice] at the given [code]index[/code].
Returns the number of discovered [UPNPDevice]s.
Returns the default gateway. That is the first discovered [UPNPDevice] that is also a valid IGD (InternetGatewayDevice).
Returns the external [IP] address of the default gateway (see [method get_gateway]) as string. Returns an empty string on error.
Removes the device at [code]index[/code] from the list of discovered devices.
Sets the device at [code]index[/code] from the list of discovered devices to [code]device[/code].
If [code]true[/code], IPv6 is used for [UPNPDevice] discovery.
If [code]0[/code], the local port to use for discovery is chosen automatically by the system. If [code]1[/code], discovery will be done from the source port 1900 (same as destination port). Otherwise, the value will be used as the port.
Multicast interface to use for discovery. Uses the default multicast interface if empty.
UPNP command or discovery was successful.
Not authorized to use the command on the [UPNPDevice]. May be returned when the user disabled UPNP on their router.
No port mapping was found for the given port, protocol combination on the given [UPNPDevice].
Inconsistent parameters.
No such entry in array. May be returned if a given port, protocol combination is not found on an [UPNPDevice].
The action failed.
The [UPNPDevice] does not allow wildcard values for the source IP address.
The [UPNPDevice] does not allow wildcard values for the external port.
The [UPNPDevice] does not allow wildcard values for the internal port.
The remote host value must be a wildcard.
The external port value must be a wildcard.
No port maps are available. May also be returned if port mapping functionality is not available.
Conflict with other mechanism. May be returned instead of [constant UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING] if a port mapping conflicts with an existing one.
Conflict with an existing port mapping.
External and internal port values must be the same.
Only permanent leases are supported. Do not use the [code]duration[/code] parameter when adding port mappings.
Invalid gateway.
Invalid port.
Invalid protocol.
Invalid duration.
Invalid arguments.
Invalid response.
Invalid parameter.
HTTP error.
Socket error.
Error allocating memory.
No gateway available. You may need to call [method discover] first, or discovery didn't detect any valid IGDs (InternetGatewayDevices).
No devices available. You may need to call [method discover] first, or discovery didn't detect any valid [UPNPDevice]s.
Unknown error.