Browse Source

Merge in "improvements" from NX's git repo to 2.3.3. Builds in Windows.

Matt Coburn 6 years ago
parent
commit
17217dbc66
3 changed files with 155 additions and 135 deletions
  1. 9 9
      DOCUMENTATION.md
  2. 2 3
      Source/Managed/ENet.cs
  3. 144 123
      Source/Native/enet.h

+ 9 - 9
DOCUMENTATION.md

@@ -62,20 +62,20 @@ Provides per packet events.
 
 ### Structures
 #### Address
-Contains marshalled structure with host data and port number.
+Contains structure with host data and port number.
 
-`Address.Port` set or get a port number.
+`Address.Port` sets or gets a port number.
 
-`Address.GetIP()` get an IP address.
-
-`Address.SetIP(string ip)` set an IP address. To use IPv4 broadcast in the local network the address can be set to _255.255.255.255_ for a client. ENet will automatically respond to the broadcast and update the address to a server's actual IP. 
+`Address.GetIP()` gets an IP address.
 
 `Address.GetHost()` attempts to do a reverse lookup from the address. Returns a string with a resolved name or an IP address.
 
-`Address.SetHost(string hostName)` set host name or an IP address. Should be used for binding to a network interface or for connection to a foreign host. Returns true on success or false on failure.
+`Address.SetIP(string ip)` sets an IP address. To use IPv4 broadcast in the local network the address can be set to _255.255.255.255_ for a client. ENet will automatically respond to the broadcast and update the address to a server's actual IP.
+
+`Address.SetHost(string hostName)` sets host name or an IP address. Should be used for binding to a network interface or for connection to a foreign host. Returns true on success or false on failure.
 
 #### Event
-Contains marshalled structure with the event type, managed pointer to the peer, channel ID, user-supplied data, and managed pointer to the packet.
+Contains structure with the event type, managed pointer to the peer, channel ID, user-supplied data, and managed pointer to the packet.
 
 `Event.Type` returns a type of the event.
 
@@ -100,9 +100,9 @@ Contains a managed pointer to the packet.
 
 `Packet.HasReferences` checks references to the packet.
 
-`Packet.SetFreeCallback(PacketFreeCallback callback)` set callback to notify the programmer when an appropriate packet is being destroyed. Pointer `IntPtr` to a callback can be used instead of a reference to a delegate.
+`Packet.SetFreeCallback(PacketFreeCallback callback)` set callback to notify the programmer when an appropriate packet is being destroyed. A pointer `IntPtr` to a callback can be used instead of a reference to a delegate.
 
-`Packet.Create(byte[] data, int offset, int length, PacketFlags flags)` creates a packet that may be sent to a peer. The offset parameter indicates the starting point of data in an array, the length is the ending point of data in an array. All parameters are optional. Multiple packet flags can be specified at once. Pointer `IntPtr` to a native buffer can be used instead of a reference to a byte array.
+`Packet.Create(byte[] data, int offset, int length, PacketFlags flags)` creates a packet that may be sent to a peer. The offset parameter indicates the starting point of data in an array, the length is the ending point of data in an array. All parameters are optional. Multiple packet flags can be specified at once. A pointer `IntPtr` to a native buffer can be used instead of a reference to a byte array.
 
 `Packet.CopyTo(byte[] destination)` copies payload from the packet to the destination array.
 

+ 2 - 3
Source/Managed/ENet.cs

@@ -64,11 +64,10 @@ namespace ENet
         Zombie = 9
     }
 
-    [StructLayout(LayoutKind.Sequential)]
+    [StructLayout(LayoutKind.Explicit, Size = 18)]
     internal struct ENetAddress
     {
-        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
-        public byte[] ip;
+        [FieldOffset(16)]
         public ushort port;
     }
 

+ 144 - 123
Source/Native/enet.h

@@ -29,12 +29,12 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <time.h>
-
+// Include the ENET Logging/Tracing Functionality.
 #include "enet_log.h"
 
 #define ENET_VERSION_MAJOR 2
 #define ENET_VERSION_MINOR 3
-#define ENET_VERSION_PATCH 0
+#define ENET_VERSION_PATCH 3
 #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)
@@ -117,109 +117,109 @@
  */
 
 #ifdef _WIN32
-#if defined(_MSC_VER) && defined(ENET_IMPLEMENTATION)
-#pragma warning(disable: 4244) /* 64-bit to 32-bit integer conversion */
-#pragma warning(disable: 4267) /* size_t to integer conversion */
-#endif
-
-#ifndef ENET_NO_PRAGMA_LINK
-#pragma comment(lib, "ws2_32.lib")
-#pragma comment(lib, "winmm.lib")
-#endif
-
-#if _MSC_VER >= 1910
- /* It looks like there were changes as of Visual Studio 2017 and there are no 32/64 bit
- versions of _InterlockedExchange[operation], only InterlockedExchange[operation]
- (without leading underscore), so we have to distinguish between compiler versions */
-#define NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
-#endif
-
-#ifdef __GNUC__
-#if (_WIN32_WINNT < 0x0501)
-#undef _WIN32_WINNT
-#define _WIN32_WINNT 0x0501
-#endif
-#endif
-
-#include <winsock2.h>
-#include <ws2tcpip.h>
-#include <mmsystem.h>
-#include <intrin.h>
-
-#if defined(_WIN32) && defined(_MSC_VER)
-#if _MSC_VER < 1900
-typedef struct timespec {
-	long tv_sec;
-	long tv_nsec;
-};
-#endif
-#define CLOCK_MONOTONIC 0
-#endif
-
-typedef SOCKET ENetSocket;
-
-#define ENET_SOCKET_NULL INVALID_SOCKET
-
-typedef struct {
-	size_t dataLength;
-	void* data;
-} ENetBuffer;
-
-#define ENET_CALLBACK __cdecl
-
-#ifdef ENET_DLL
-#ifdef ENET_IMPLEMENTATION
-#define ENET_API __declspec(dllexport)
-#else
-#define ENET_API __declspec(dllimport)
-#endif
-#else
-#define ENET_API extern
-#endif
+	#if defined(_MSC_VER) && defined(ENET_IMPLEMENTATION)
+		#pragma warning(disable: 4244) /* 64-bit to 32-bit integer conversion */
+		#pragma warning(disable: 4267) /* size_t to integer conversion */
+	#endif
+
+	#ifndef ENET_NO_PRAGMA_LINK
+		#pragma comment(lib, "ws2_32.lib")
+		#pragma comment(lib, "winmm.lib")
+	#endif
+
+	#if _MSC_VER >= 1910
+		/* It looks like there were changes as of Visual Studio 2017 and there are no 32/64 bit
+		versions of _InterlockedExchange[operation], only InterlockedExchange[operation]
+		(without leading underscore), so we have to distinguish between compiler versions */
+		#define NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
+	#endif
+
+	#ifdef __GNUC__
+		#if (_WIN32_WINNT < 0x0501)
+			#undef _WIN32_WINNT
+			#define _WIN32_WINNT 0x0501
+		#endif
+	#endif
+
+	#include <winsock2.h>
+	#include <ws2tcpip.h>
+	#include <mmsystem.h>
+	#include <intrin.h>
+
+	#if defined(_WIN32) && defined(_MSC_VER)
+		#if _MSC_VER < 1900
+			typedef struct timespec {
+				long tv_sec;
+				long tv_nsec;
+			};
+		#endif
+	#define CLOCK_MONOTONIC 0
+	#endif
+
+	typedef SOCKET ENetSocket;
+
+	#define ENET_SOCKET_NULL INVALID_SOCKET
+
+	typedef struct {
+		size_t dataLength;
+		void* data;
+	} ENetBuffer;
+
+	#define ENET_CALLBACK __cdecl
+
+	#ifdef ENET_DLL
+		#ifdef ENET_IMPLEMENTATION
+			#define ENET_API __declspec(dllexport)
+		#else
+			#define ENET_API __declspec(dllimport)
+		#endif
+	#else
+		#define ENET_API extern
+	#endif
 #else
-#include <sys/types.h>
-#include <sys/ioctl.h>
-#include <sys/time.h>
-#include <sys/socket.h>
-#include <poll.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <netdb.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
-
-#ifdef __APPLE__
-#include <mach/clock.h>
-#include <mach/mach.h>
-#include <Availability.h>
-#endif
-
-#ifndef MSG_NOSIGNAL
-#define MSG_NOSIGNAL 0
-#endif
-
-#ifdef MSG_MAXIOVLEN
-#define ENET_BUFFER_MAXIMUM MSG_MAXIOVLEN
-#endif
-
-typedef int ENetSocket;
-
-#define ENET_SOCKET_NULL -1
-
-typedef struct {
-	void* data;
-	size_t dataLength;
-} ENetBuffer;
+	#include <sys/types.h>
+	#include <sys/ioctl.h>
+	#include <sys/time.h>
+	#include <sys/socket.h>
+	#include <poll.h>
+	#include <arpa/inet.h>
+	#include <netinet/in.h>
+	#include <netinet/tcp.h>
+	#include <netdb.h>
+	#include <unistd.h>
+	#include <string.h>
+	#include <errno.h>
+	#include <fcntl.h>
+
+	#ifdef __APPLE__
+		#include <mach/clock.h>
+		#include <mach/mach.h>
+		#include <Availability.h>
+	#endif
+
+	#ifndef MSG_NOSIGNAL
+		#define MSG_NOSIGNAL 0
+	#endif
+
+	#ifdef MSG_MAXIOVLEN
+		#define ENET_BUFFER_MAXIMUM MSG_MAXIOVLEN
+	#endif
+
+	typedef int ENetSocket;
+
+	#define ENET_SOCKET_NULL -1
+
+	typedef struct {
+		void* data;
+		size_t dataLength;
+	} ENetBuffer;
 
-#define ENET_CALLBACK
-#define ENET_API extern
+	#define ENET_CALLBACK
+	#define ENET_API extern
 #endif
 
 #ifndef ENET_BUFFER_MAXIMUM
-#define ENET_BUFFER_MAXIMUM (1 + 2 * ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS)
+	#define ENET_BUFFER_MAXIMUM (1 + 2 * ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS)
 #endif
 
 #define ENET_HOST_ANY in6addr_any
@@ -284,17 +284,6 @@ extern "C" {
 #define enet_list_front(list) ((void*)(list)->sentinel.next)
 #define enet_list_back(list) ((void*)(list)->sentinel.previous)
 
-#ifndef IN4ADDR
-#define IN4ADDR
-
-	struct in4_addr {
-		uint8_t zeros[10];
-		uint16_t ffff;
-		struct in_addr ip;
-	};
-
-#endif
-
 	// Cherry picked from nxrighthere repo, commit d9ad27e: "Avoid naming collision"
 #define enet_in6_equal(a, b) (memcmp(&a, &b, sizeof(struct in6_addr)) == 0)
 
@@ -511,7 +500,11 @@ extern "C" {
 	typedef struct _ENetAddress {
 		union {
 			struct in6_addr ipv6;
-			struct in4_addr ipv4;
+			struct {
+				uint8_t zeros[10];
+				uint16_t ffff;
+				struct in_addr ip;
+			} ipv4;
 		};
 		uint16_t port;
 	} ENetAddress;
@@ -4406,6 +4399,10 @@ extern "C" {
 
 		if (strchr(ip, ':') == NULL) {
 			type = AF_INET;
+
+			// Cherry pick from nx repo, commit c885a64
+			memset(address, 0, sizeof(address->ipv4.zeros));
+
 			address->ipv4.ffff = 0xFFFF;
 			destination = &address->ipv4.ip;
 		}
@@ -4548,17 +4545,29 @@ extern "C" {
 	}
 
 	int enet_socket_get_address(ENetSocket socket, ENetAddress* address) {
-		struct sockaddr_in6 sin;
-		socklen_t sinLength = sizeof(struct sockaddr_in6);
+		struct sockaddr_storage sa;
+		socklen_t saLength = sizeof(sa);
 
-		if (getsockname(socket, (struct sockaddr*) & sin, &sinLength) == -1) {
+		if (getsockname(socket, (struct sockaddr*) & sa, &saLength) == -1)
 			ENET_LOG_ERROR("getsockname failure");
 			return ENET_GETSOCKINFO_FAILURE;
 		}
 
+		if (sa.ss_family == AF_INET) {
+			struct sockaddr_in* sin = (struct sockaddr_in*) & sa;
+
+			memset(address, 0, sizeof(address->ipv4.zeros));
 
-		address->ipv6 = sin.sin6_addr;
-		address->port = ENET_NET_TO_HOST_16(sin.sin6_port);
+			address->ipv4.ffff = 0xFFFF;
+			address->ipv4.ip = sin->sin_addr;
+			address->port = ENET_NET_TO_HOST_16(sin->sin_port);
+		}
+		else if (sa.ss_family == AF_INET6) {
+			struct sockaddr_in6* sin = (struct sockaddr_in6*) & sa;
+
+			address->ipv6 = sin->sin6_addr;
+			address->port = ENET_NET_TO_HOST_16(sin->sin6_port);
+		}
 
 		return 0;
 	}
@@ -4885,17 +4894,29 @@ extern "C" {
 	}
 
 	int enet_socket_get_address(ENetSocket socket, ENetAddress* address) {
-		struct sockaddr_in6 sin;
-		int sinLength = sizeof(struct sockaddr_in6);
+		struct sockaddr_storage sa;
+		socklen_t saLength = sizeof(sa);
 
-		if (getsockname(socket, (struct sockaddr*) & sin, &sinLength) == -1) {
+		if (getsockname(socket, (struct sockaddr*) & sa, &saLength) == -1) {
 			ENET_LOG_ERROR("getsockname failure");
-			return ENET_SOCKET_GETSOCKNAME_FAILURE;
+			return ENET_GETSOCKINFO_FAILURE;
 		}
 
+		if (sa.ss_family == AF_INET) {
+			struct sockaddr_in* sin = (struct sockaddr_in*) & sa;
 
-		address->ipv6 = sin.sin6_addr;
-		address->port = ENET_NET_TO_HOST_16(sin.sin6_port);
+			memset(address, 0, sizeof(address->ipv4.zeros));
+
+			address->ipv4.ffff = 0xFFFF;
+			address->ipv4.ip = sin->sin_addr;
+			address->port = ENET_NET_TO_HOST_16(sin->sin_port);
+		}
+		else if (sa.ss_family == AF_INET6) {
+			struct sockaddr_in6* sin = (struct sockaddr_in6*) & sa;
+
+			address->ipv6 = sin->sin6_addr;
+			address->port = ENET_NET_TO_HOST_16(sin->sin6_port);
+		}
 
 		return 0;
 	}