Browse Source

Change the way of obtaining an IP address of a peer

nxrighthere 7 years ago
parent
commit
d7615a3c20
2 changed files with 18 additions and 33 deletions
  1. 13 28
      Source/Managed/ENet.cs
  2. 5 5
      Source/Native/enet.h

+ 13 - 28
Source/Managed/ENet.cs

@@ -120,24 +120,6 @@ namespace ENet {
 
 			return Native.enet_address_set_host(ref nativeAddress, Encoding.ASCII.GetBytes(hostName)) == 0;
 		}
-
-		public string GetIP() {
-			byte[] data = new byte[16];
-
-			if (Native.enet_address_get_host_ip(ref nativeAddress, data, (IntPtr)16) == 0)
-				return Encoding.ASCII.GetString(data);
-			else
-				return String.Empty;
-		}
-
-		public string GetName() {
-			byte[] data = new byte[256];
-
-			if (Native.enet_address_get_host(ref nativeAddress, data, (IntPtr)256) == 0)
-				return Encoding.ASCII.GetString(data);
-			else
-				return String.Empty;
-		}
 	}
 
 	public struct Event {
@@ -528,11 +510,20 @@ namespace ENet {
 			}
 		}
 
-		public Address Address {
+		public string IP {
 			get {
 				CheckCreated();
 
-				return Native.enet_peer_get_address(nativePeer);
+				byte[] ip = new byte[16];
+
+				if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0) {
+					if (Encoding.ASCII.GetString(ip).Remove(7) == "::ffff:")
+						return Encoding.ASCII.GetString(ip).Substring(7);
+
+					return Encoding.ASCII.GetString(ip);
+				} else {
+					return String.Empty;
+				}
 			}
 		}
 
@@ -667,7 +658,7 @@ namespace ENet {
 		public const int throttleAcceleration = 2;
 		public const int throttleDeceleration = 2;
 		public const int throttleInterval = 5000;
-		public const uint version = (2 << 16) | (0 << 8) | (4);
+		public const uint version = (2 << 16) | (0 << 8) | (5);
 
 		public static int Initialize() {
 			return Native.enet_initialize();
@@ -700,15 +691,9 @@ namespace ENet {
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
 
-		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_address_get_host(ref ENetAddress address, byte[] hostName, IntPtr nameLength);
-
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern int enet_address_set_host(ref ENetAddress address, byte[] hostName);
 
-		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_address_get_host_ip(ref ENetAddress address, byte[] hostIP, IntPtr ipLength);
-
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
 
@@ -776,7 +761,7 @@ namespace ENet {
 		internal static extern uint enet_peer_get_id(IntPtr peer);
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern Address enet_peer_get_address(IntPtr peer);
+		internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern PeerState enet_peer_get_state(IntPtr peer);

+ 5 - 5
Source/Native/enet.h

@@ -32,7 +32,7 @@
 
 #define ENET_VERSION_MAJOR 2
 #define ENET_VERSION_MINOR 0
-#define ENET_VERSION_PATCH 4
+#define ENET_VERSION_PATCH 5
 #define ENET_VERSION_CREATE(major, minor, patch) (((major)<<16) | ((minor)<<8) | (patch))
 #define ENET_VERSION_GET_MAJOR(version) (((version)>>16)&0xFF)
 #define ENET_VERSION_GET_MINOR(version) (((version)>>8)&0xFF)
@@ -892,7 +892,7 @@ extern "C" {
     ENET_API enet_uint32         enet_host_get_bytes_received (ENetHost *);
 
     ENET_API enet_uint32         enet_peer_get_id (ENetPeer *);
-    ENET_API ENetAddress         enet_peer_get_address (ENetPeer *);
+    ENET_API int                 enet_peer_get_ip (ENetPeer *, char * ip, size_t ipLength);
     ENET_API ENetPeerState       enet_peer_get_state (ENetPeer *);
     ENET_API enet_uint32         enet_peer_get_rtt (ENetPeer *);
     ENET_API enet_uint64         enet_peer_get_packets_sent (ENetPeer *);
@@ -3315,8 +3315,8 @@ extern "C" {
         return peer->connectID;
     }
 
-    ENetAddress enet_peer_get_address(ENetPeer *peer) {
-        return peer->address;
+    int enet_peer_get_ip(ENetPeer *peer, char *ip, size_t ipLength) {
+        return enet_address_get_host_ip(&peer->address, ip, ipLength);
     }
 
     ENetPeerState enet_peer_get_state(ENetPeer *peer) {
@@ -5739,4 +5739,4 @@ extern "C" {
 #endif
 
 #endif // ENET_IMPLEMENTATION
-#endif // ENET_INCLUDE_H
+#endif // ENET_INCLUDE_H