Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DNS resolution in NetworkedMultiplayerEnet::create_client() #18154

Merged
merged 1 commit into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions modules/enet/networked_multiplayer_enet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/*************************************************************************/

#include "networked_multiplayer_enet.h"
#include "io/ip.h"
#include "io/marshalls.h"
#include "os/os.h"

Expand Down Expand Up @@ -92,26 +93,39 @@ Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int
connection_status = CONNECTION_CONNECTED;
return OK;
}
Error NetworkedMultiplayerENet::create_client(const IP_Address &p_ip, int p_port, int p_in_bandwidth, int p_out_bandwidth) {
Error NetworkedMultiplayerENet::create_client(const String &p_address, int p_port, int p_in_bandwidth, int p_out_bandwidth) {

ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);

host = enet_host_create(NULL /* create a client host */,
1 /* only allow 1 outgoing connection */,
SYSCH_MAX /* allow up to SYSCH_MAX channels to be used */,
p_in_bandwidth /* 56K modem with 56 Kbps downstream bandwidth */,
p_out_bandwidth /* 56K modem with 14 Kbps upstream bandwidth */);
p_in_bandwidth /* limit incoming bandwith if > 0 */,
p_out_bandwidth /* limit outgoing bandwith if > 0 */);

ERR_FAIL_COND_V(!host, ERR_CANT_CREATE);

_setup_compressor();

IP_Address ip;
if (p_address.is_valid_ip_address()) {
ip = p_address;
} else {
#ifdef GODOT_ENET
ip = IP::get_singleton()->resolve_hostname(p_address);
#else
ip = IP::get_singleton()->resolve_hostname(p_address, IP::TYPE_IPV4);
#endif

ERR_FAIL_COND_V(!ip.is_valid(), ERR_CANT_RESOLVE);
}

ENetAddress address;
#ifdef GODOT_ENET
enet_address_set_ip(&address, p_ip.get_ipv6(), 16);
enet_address_set_ip(&address, ip.get_ipv6(), 16);
#else
ERR_FAIL_COND_V(!p_ip.is_ipv4(), ERR_INVALID_PARAMETER);
address.host = *(uint32_t *)p_ip.get_ipv4();
ERR_FAIL_COND_V(!ip.is_ipv4(), ERR_INVALID_PARAMETER);
address.host = *(uint32_t *)ip.get_ipv4();
#endif
address.port = p_port;

Expand Down Expand Up @@ -708,7 +722,7 @@ int NetworkedMultiplayerENet::get_peer_port(int p_peer_id) const {
void NetworkedMultiplayerENet::_bind_methods() {

ClassDB::bind_method(D_METHOD("create_server", "port", "max_clients", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_server, DEFVAL(32), DEFVAL(0), DEFVAL(0));
ClassDB::bind_method(D_METHOD("create_client", "ip", "port", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_client, DEFVAL(0), DEFVAL(0));
ClassDB::bind_method(D_METHOD("create_client", "address", "port", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_client, DEFVAL(0), DEFVAL(0));
ClassDB::bind_method(D_METHOD("close_connection"), &NetworkedMultiplayerENet::close_connection);
ClassDB::bind_method(D_METHOD("disconnect_peer", "id", "now"), &NetworkedMultiplayerENet::disconnect_peer, DEFVAL(false));
ClassDB::bind_method(D_METHOD("set_compression_mode", "mode"), &NetworkedMultiplayerENet::set_compression_mode);
Expand Down
2 changes: 1 addition & 1 deletion modules/enet/networked_multiplayer_enet.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer {
virtual int get_peer_port(int p_peer_id) const;

Error create_server(int p_port, int p_max_clients = 32, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
Error create_client(const IP_Address &p_ip, int p_port, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
Error create_client(const String &p_address, int p_port, int p_in_bandwidth = 0, int p_out_bandwidth = 0);

void close_connection();

Expand Down