Explorar o código

修复kcp导致unity crash的bug,kcp的C#调用增加空指针检查,空指针则抛异常,防止客户端挂掉,方便查找错误

tanghai %!s(int64=7) %!d(string=hai) anos
pai
achega
ea4bd470af

+ 4 - 7
Server/ThirdParty/Libs/KcpLib.csproj

@@ -1,17 +1,15 @@
 <Project Sdk="Microsoft.NET.Sdk">
-
   <PropertyGroup>
     <TargetFramework>netcoreapp2.1</TargetFramework>
   </PropertyGroup>
-
   <PropertyGroup>
-  	<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
+    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
-  	<OutputPath>..\..\..\Bin\</OutputPath>
+    <OutputPath>..\..\..\Bin\</OutputPath>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
-  	<OutputPath>..\..\..\Bin\</OutputPath>
+    <OutputPath>..\..\..\Bin\</OutputPath>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="..\..\..\Unity\Assets\Scripts\Module\Message\Network\KCP\Kcp.cs" Link="Kcp.cs" />
@@ -26,5 +24,4 @@
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </None>
   </ItemGroup>
-
-</Project>
+</Project>

+ 1 - 14
Tools/Config/.screenrc

@@ -6,18 +6,5 @@ startup_message off
 # define a bigger scrollback, default is 100 lines, I like large scrollback
 defscrollback 10000
 
-# An alternative hardstatus to display a bar at the bottom listing the
-# windownames and highlighting the current windowname in blue. (This is only
-# enabled if there is no hardstatus setting for your terminal)
-hardstatus on
-hardstatus alwayslastline
-hardstatus string "%{.bW}%-w%{.rY}%n %t%{-}%+w %=%{..G} %H(%l) %{..Y} %Y/%m/%d "
-
 # another way to show window list
-#caption always "%?%F%{-b 4w}%:%{-b bb}%? %H | %l | %m-%d %c |%?%F%{-b4w}%?%L=%-Lw%45>%{-b w4}%n%f* %t%{-}%+Lw%-0<"
-
-# some useful keys for resizing
-bind = resize =
-bind + resize +1
-bind - resize -1
-bind _ resize max
+caption always "%{= kw}%-w%{= kG}%{+b}[%n %t]%{-b}%{= kw}%+w %=%M%d 0c %{g}%H%{-}"

BIN=BIN
Unity/Assets/Plugins/x86/kcp.dll


BIN=BIN
Unity/Assets/Plugins/x86_64/kcp.dll


+ 6 - 2
Unity/Assets/Scripts/Module/Message/Network/KCP/KChannel.cs

@@ -275,7 +275,7 @@ namespace ETModel
 			}
 
 
-			if (kcp != IntPtr.Zero)
+			if (this.kcp != IntPtr.Zero)
 			{
 				uint nextUpdateTime = Kcp.KcpCheck(this.kcp, timeNow);
 				this.GetService().AddToUpdateNextTime(nextUpdateTime, this.Id);
@@ -309,12 +309,16 @@ namespace ETModel
 				this.GetService().RemoveFromWaitConnectChannels(this.RemoteConn);
 				this.isRecvFirstKcpMessage = true;
 			}
-
+			
 			Kcp.KcpInput(this.kcp, date, offset, length);
 			this.GetService().AddToUpdateNextTime(0, this.Id);
 
 			while (true)
 			{
+				if (this.IsDisposed)
+				{
+					return;
+				}
 				int n = Kcp.KcpPeeksize(this.kcp);
 				if (n < 0)
 				{

+ 78 - 18
Unity/Assets/Scripts/Module/Message/Network/KCP/Kcp.cs

@@ -16,40 +16,44 @@ namespace ETModel
 #endif
 
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern uint ikcp_check(IntPtr kcp, uint current);
+        private static extern uint ikcp_check(IntPtr kcp, uint current);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern IntPtr ikcp_create(uint conv, IntPtr user);
+        private static extern IntPtr ikcp_create(uint conv, IntPtr user);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern void ikcp_flush(IntPtr kcp);
+        private static extern void ikcp_flush(IntPtr kcp);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern uint ikcp_getconv(IntPtr ptr);
+        private static extern uint ikcp_getconv(IntPtr ptr);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern int ikcp_input(IntPtr kcp, byte[] data, long offset, long size);
+        private static extern int ikcp_input(IntPtr kcp, byte[] data, long offset, long size);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern int ikcp_nodelay(IntPtr kcp, int nodelay, int interval, int resend, int nc);
+        private static extern int ikcp_nodelay(IntPtr kcp, int nodelay, int interval, int resend, int nc);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern int ikcp_peeksize(IntPtr kcp);
+        private static extern int ikcp_peeksize(IntPtr kcp);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern int ikcp_recv(IntPtr kcp, byte[] buffer, int len);
+        private static extern int ikcp_recv(IntPtr kcp, byte[] buffer, int len);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern void ikcp_release(IntPtr kcp);
+        private static extern void ikcp_release(IntPtr kcp);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern int ikcp_send(IntPtr kcp, byte[] buffer, int len);
+        private static extern int ikcp_send(IntPtr kcp, byte[] buffer, int len);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern void ikcp_setminrto(IntPtr ptr, int minrto);
+        private static extern void ikcp_setminrto(IntPtr ptr, int minrto);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern int ikcp_setmtu(IntPtr kcp, int mtu);
+        private static extern int ikcp_setmtu(IntPtr kcp, int mtu);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern void ikcp_setoutput(IntPtr kcp, kcp_output output);
+        private static extern void ikcp_setoutput(IntPtr kcp, kcp_output output);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern void ikcp_update(IntPtr kcp, uint current);
+        private static extern void ikcp_update(IntPtr kcp, uint current);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern int ikcp_waitsnd(IntPtr kcp);
+        private static extern int ikcp_waitsnd(IntPtr kcp);
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
-        public static extern int ikcp_wndsize(IntPtr kcp, int sndwnd, int rcvwnd);
+        private static extern int ikcp_wndsize(IntPtr kcp, int sndwnd, int rcvwnd);
         
         public static uint KcpCheck(IntPtr kcp, uint current)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             return ikcp_check(kcp, current);
         }
         
@@ -60,71 +64,127 @@ namespace ETModel
 
         public static void KcpFlush(IntPtr kcp)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             ikcp_flush(kcp);
         }
 
         public static uint KcpGetconv(IntPtr ptr)
         {
+            if (ptr == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             return ikcp_getconv(ptr);
         }
 
         public static int KcpInput(IntPtr kcp, byte[] data, long offset, long size)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             return ikcp_input(kcp, data, offset, size);
         }
 
         public static int KcpNodelay(IntPtr kcp, int nodelay, int interval, int resend, int nc)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             return ikcp_nodelay(kcp, nodelay, interval, resend, nc);
         }
 
         public static int KcpPeeksize(IntPtr kcp)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             return ikcp_peeksize(kcp);
         }
 
         public static int KcpRecv(IntPtr kcp, byte[] buffer, int len)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             return ikcp_recv(kcp, buffer, len);
         }
 
         public static void KcpRelease(IntPtr kcp)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             ikcp_release(kcp);
         }
 
         public static int KcpSend(IntPtr kcp, byte[] buffer, int len)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             return ikcp_send(kcp, buffer, len);
         }
 
-        public static void KcpSetminrto(IntPtr ptr, int minrto)
+        public static void KcpSetminrto(IntPtr kcp, int minrto)
         {
-            ikcp_setminrto(ptr, minrto);
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
+            ikcp_setminrto(kcp, minrto);
         }
 
         public static int KcpSetmtu(IntPtr kcp, int mtu)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             return ikcp_setmtu(kcp, mtu);
         }
 
         public static void KcpSetoutput(IntPtr kcp, kcp_output output)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             ikcp_setoutput(kcp, output);
         }
 
         public static void KcpUpdate(IntPtr kcp, uint current)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             ikcp_update(kcp, current);
         }
 
         public static int KcpWaitsnd(IntPtr kcp)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             return ikcp_waitsnd(kcp);
         }
 
         public static int KcpWndsize(IntPtr kcp, int sndwnd, int rcvwnd)
         {
+            if (kcp == IntPtr.Zero)
+            {
+                throw new Exception($"kcp error, kcp point is zero");
+            }
             return ikcp_wndsize(kcp, sndwnd, rcvwnd);
         }
     }