Browse Source

Expose host buffer size option

NX 6 years ago
parent
commit
7e72ed4b8f
2 changed files with 27 additions and 18 deletions
  1. 14 10
      Source/Managed/ENet.cs
  2. 13 8
      Source/Native/enet.h

+ 14 - 10
Source/Managed/ENet.cs

@@ -461,23 +461,27 @@ namespace ENet {
 			Create(null, 1, 0);
 		}
 
+		public void Create(int bufferSize) {
+			Create(null, 1, 0, 0, 0, bufferSize);
+		}
+
 		public void Create(Address? address, int peerLimit) {
 			Create(address, peerLimit, 0);
 		}
 
 		public void Create(Address? address, int peerLimit, int channelLimit) {
-			Create(address, peerLimit, channelLimit, 0, 0);
+			Create(address, peerLimit, channelLimit, 0, 0, 0);
 		}
 
 		public void Create(int peerLimit, int channelLimit) {
-			Create(null, peerLimit, channelLimit, 0, 0);
+			Create(null, peerLimit, channelLimit, 0, 0, 0);
 		}
 
 		public void Create(int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
-			Create(null, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
+			Create(null, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth, 0);
 		}
 
-		public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
+		public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize) {
 			if (nativeHost != IntPtr.Zero)
 				throw new InvalidOperationException("Host already created");
 
@@ -489,9 +493,9 @@ namespace ENet {
 			if (address != null) {
 				var nativeAddress = address.Value.NativeData;
 
-				nativeHost = Native.enet_host_create(ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth);
+				nativeHost = Native.enet_host_create(ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
 			} else {
-				nativeHost = Native.enet_host_create(IntPtr.Zero, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth);
+				nativeHost = Native.enet_host_create(IntPtr.Zero, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
 			}
 
 			if (nativeHost == IntPtr.Zero)
@@ -870,7 +874,7 @@ namespace ENet {
 		public const uint timeoutLimit = 32;
 		public const uint timeoutMinimum = 5000;
 		public const uint timeoutMaximum = 30000;
-		public const uint version = (2 << 16) | (2 << 8) | (6);
+		public const uint version = (2 << 16) | (2 << 8) | (7);
 
 		public static bool Initialize() {
 			return Native.enet_initialize() == 0;
@@ -951,10 +955,10 @@ namespace ENet {
 		internal static extern void enet_packet_dispose(IntPtr packet);
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
+		internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
+		internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
@@ -1079,4 +1083,4 @@ namespace ENet {
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern void enet_peer_reset(IntPtr peer);
 	}
-}
+}

+ 13 - 8
Source/Native/enet.h

@@ -31,7 +31,7 @@
 
 #define ENET_VERSION_MAJOR 2
 #define ENET_VERSION_MINOR 2
-#define ENET_VERSION_PATCH 6
+#define ENET_VERSION_PATCH 7
 #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)
@@ -521,8 +521,8 @@ extern "C" {
 	} ENetPeerState;
 
 	enum {
-		ENET_HOST_RECEIVE_BUFFER_SIZE          = 256 * 1024,
-		ENET_HOST_SEND_BUFFER_SIZE             = 256 * 1024,
+		ENET_HOST_BUFFER_SIZE_MIN              = 256 * 1024,
+		ENET_HOST_BUFFER_SIZE_MAX              = 1024 * 1024,
 		ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL  = 1000,
 		ENET_HOST_DEFAULT_MTU                  = 1280,
 		ENET_HOST_DEFAULT_MAXIMUM_PACKET_SIZE  = 32 * 1024 * 1024,
@@ -728,7 +728,7 @@ extern "C" {
 
 	ENET_API enet_uint32 enet_crc32(const ENetBuffer*, size_t);
 
-	ENET_API ENetHost* enet_host_create(const ENetAddress*, size_t, size_t, enet_uint32, enet_uint32);
+	ENET_API ENetHost* enet_host_create(const ENetAddress*, size_t, size_t, enet_uint32, enet_uint32, int);
 	ENET_API void enet_host_destroy(ENetHost*);
 	ENET_API void enet_host_enable_compression(ENetHost*);
 	ENET_API void enet_host_prevent_connections(ENetHost*, enet_uint8);
@@ -3695,7 +3695,7 @@ extern "C" {
 // !
 // =======================================================================//
 
-	ENetHost* enet_host_create(const ENetAddress* address, size_t peerCount, size_t channelLimit, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth) {
+	ENetHost* enet_host_create(const ENetAddress* address, size_t peerCount, size_t channelLimit, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth, int bufferSize) {
 		ENetHost* host;
 		ENetPeer* currentPeer;
 
@@ -3734,10 +3734,15 @@ extern "C" {
 			return NULL;
 		}
 
+		if (bufferSize > ENET_HOST_BUFFER_SIZE_MAX)
+			bufferSize = ENET_HOST_BUFFER_SIZE_MAX;
+		else if (bufferSize < ENET_HOST_BUFFER_SIZE_MIN)
+			bufferSize = ENET_HOST_BUFFER_SIZE_MIN;
+
 		enet_socket_set_option(host->socket, ENET_SOCKOPT_NONBLOCK, 1);
 		enet_socket_set_option(host->socket, ENET_SOCKOPT_BROADCAST, 1);
-		enet_socket_set_option(host->socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);
-		enet_socket_set_option(host->socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);
+		enet_socket_set_option(host->socket, ENET_SOCKOPT_RCVBUF, bufferSize);
+		enet_socket_set_option(host->socket, ENET_SOCKOPT_SNDBUF, bufferSize);
 
 		if (address != NULL && enet_socket_get_address(host->socket, &host->address) < 0)
 			host->address = *address;
@@ -5084,4 +5089,4 @@ extern "C" {
 }
 #endif
 #endif
-#endif
+#endif