Explorar o código

SIO_UDP_CONNRESET 设置抽出一个方法

tanghai %!s(int64=3) %!d(string=hai) anos
pai
achega
ec578e6c2f

+ 3 - 10
Unity/Assets/Scripts/Codes/Hotfix/Server/Module/Router/RouterComponentSystem.cs

@@ -30,16 +30,9 @@ namespace ET.Server
                     self.InnerSocket.SendBufferSize = 16 * Kcp.OneM;
                     self.InnerSocket.ReceiveBufferSize = 16 * Kcp.OneM;
                 }
-
-                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
-                {
-                    const uint IOC_IN = 0x80000000;
-                    const uint IOC_VENDOR = 0x18000000;
-                    uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
-
-                    self.OuterSocket.IOControl((int) SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null);
-                    self.InnerSocket.IOControl((int) SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null);
-                }
+                
+                NetworkHelper.SetSioUdpConnReset(self.OuterSocket);
+                NetworkHelper.SetSioUdpConnReset(self.InnerSocket);
             }
         }
 

+ 17 - 1
Unity/Assets/Scripts/Core/Helper/NetworkHelper.cs

@@ -1,7 +1,9 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Net;
 using System.Net.NetworkInformation;
 using System.Net.Sockets;
+using System.Runtime.InteropServices;
 
 namespace ET
 {
@@ -53,5 +55,19 @@ namespace ET
 			int port = int.Parse(p);
 			return ToIPEndPoint(host, port);
 		}
+
+		public static void SetSioUdpConnReset(Socket socket)
+		{
+			if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+			{
+				return;
+			}
+
+			const uint IOC_IN = 0x80000000;
+			const uint IOC_VENDOR = 0x18000000;
+			const int SIO_UDP_CONNRESET = unchecked((int)(IOC_IN | IOC_VENDOR | 12));
+
+			socket.IOControl(SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null);
+		}
 	}
 }

+ 3 - 15
Unity/Assets/Scripts/Core/Module/Network/KService.cs

@@ -126,14 +126,8 @@ namespace ET
             {
                 throw new Exception($"bind error: {ipEndPoint}", e);
             }
-            
-            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
-            {
-                const uint IOC_IN = 0x80000000;
-                const uint IOC_VENDOR = 0x18000000;
-                uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
-                this.socket.IOControl((int) SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null);
-            }
+
+            NetworkHelper.SetSioUdpConnReset(this.socket);
         }
 
         public KService(AddressFamily addressFamily, ServiceType serviceType)
@@ -142,13 +136,7 @@ namespace ET
             this.startTime = TimeHelper.ClientNow();
             this.socket = new Socket(addressFamily, SocketType.Dgram, ProtocolType.Udp);
 
-            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
-            {
-                const uint IOC_IN = 0x80000000;
-                const uint IOC_VENDOR = 0x18000000;
-                uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
-                this.socket.IOControl((int) SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null);
-            }
+            NetworkHelper.SetSioUdpConnReset(this.socket);
         }
 
         // 保存所有的channel