Browse Source

Update ENet module to support custom ENet lib

Keep compatibility with upstream enet libraries
Fabio Alessandrelli 8 years ago
parent
commit
38d457170a

+ 2 - 2
modules/enet/SCsub

@@ -10,6 +10,7 @@ env_enet = env_modules.Clone()
 if (env['builtin_enet'] != 'no'):
     thirdparty_dir = "#thirdparty/enet/"
     thirdparty_sources = [
+        "godot.cpp",
         "callbacks.c",
         "compress.c",
         "host.c",
@@ -17,12 +18,11 @@ if (env['builtin_enet'] != 'no'):
         "packet.c",
         "peer.c",
         "protocol.c",
-        "unix.c",
-        "win32.c",
     ]
     thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
 
     env_enet.add_source_files(env.modules_sources, thirdparty_sources)
     env_enet.Append(CPPPATH=[thirdparty_dir])
+    env_enet.Append(CPPFLAGS=["-DGODOT_ENET"])
 
 env_enet.add_source_files(env.modules_sources, "*.cpp")

+ 28 - 6
modules/enet/networked_multiplayer_enet.cpp

@@ -53,8 +53,21 @@ Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int
 	ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
 
 	ENetAddress address;
-	address.host = bind_ip;
 
+#ifdef GODOT_ENET
+	if (bind_ip.is_wildcard()) {
+		address.wildcard = 1;
+	} else {
+		enet_address_set_ip(&address, bind_ip.get_ipv6(), 16);
+	}
+#else
+	if (bind_ip.is_wildcard()) {
+		address.host = 0;
+	} else {
+		ERR_FAIL_COND_V(!bind_ip.is_ipv4(), ERR_INVALID_PARAMETER);
+		address.host = *(uint32_t *)bind_ip.get_ipv4();
+	}
+#endif
 	address.port = p_port;
 
 	host = enet_host_create(&address /* the address to bind the server host to */,
@@ -76,7 +89,6 @@ Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int
 Error NetworkedMultiplayerENet::create_client(const IP_Address &p_ip, int p_port, int p_in_bandwidth, int p_out_bandwidth) {
 
 	ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
-	ERR_FAIL_COND_V(!p_ip.is_ipv4(), ERR_INVALID_PARAMETER);
 
 	host = enet_host_create(NULL /* create a client host */,
 			1 /* only allow 1 outgoing connection */,
@@ -89,7 +101,12 @@ Error NetworkedMultiplayerENet::create_client(const IP_Address &p_ip, int p_port
 	_setup_compressor();
 
 	ENetAddress address;
-	address.host = *((uint32_t *)p_ip.get_ipv4());
+#ifdef GODOT_ENET
+	enet_address_set_ip(&address, p_ip.get_ipv6(), 16);
+#else
+	ERR_FAIL_COND_V(!p_ip.is_ipv4(), ERR_INVALID_PARAMETER);
+	address.host = *(uint32_t *)p_ip.get_ipv4();
+#endif
 	address.port = p_port;
 
 	//enet_address_set_host (& address, "localhost");
@@ -147,7 +164,11 @@ void NetworkedMultiplayerENet::poll() {
 				}
 
 				IP_Address ip;
+#ifdef GODOT_ENET
+				ip.set_ipv6((uint8_t *)&(event.peer->address.host));
+#else
 				ip.set_ipv4((uint8_t *)&(event.peer->address.host));
+#endif
 
 				int *new_id = memnew(int);
 				*new_id = event.data;
@@ -657,7 +678,7 @@ NetworkedMultiplayerENet::NetworkedMultiplayerENet() {
 	enet_compressor.decompress = enet_decompress;
 	enet_compressor.destroy = enet_compressor_destroy;
 
-	bind_ip = ENET_HOST_ANY;
+	bind_ip = IP_Address("*");
 }
 
 NetworkedMultiplayerENet::~NetworkedMultiplayerENet() {
@@ -668,6 +689,7 @@ NetworkedMultiplayerENet::~NetworkedMultiplayerENet() {
 // sets IP for ENet to bind when using create_server
 // if no IP is set, then ENet bind to ENET_HOST_ANY
 void NetworkedMultiplayerENet::set_bind_ip(const IP_Address &p_ip) {
-	ERR_FAIL_COND(!p_ip.is_ipv4());
-	bind_ip = *(uint32_t *)p_ip.get_ipv4();
+	ERR_FAIL_COND(!p_ip.is_valid() && !p_ip.is_wildcard());
+
+	bind_ip = p_ip;
 }

+ 1 - 1
modules/enet/networked_multiplayer_enet.h

@@ -100,7 +100,7 @@ private:
 	static void enet_compressor_destroy(void *context);
 	void _setup_compressor();
 
-	enet_uint32 bind_ip;
+	IP_Address bind_ip;
 
 protected:
 	static void _bind_methods();