Sfoglia il codice sorgente

1.udp发送接收消除gc成功
2.修复消息池的一个bug

tanghai 2 anni fa
parent
commit
ceae736aa6

+ 1 - 1
Unity/Assets/Resources/GlobalConfig.asset

@@ -12,6 +12,6 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 36527db572638af47b03c805671cba75, type: 3}
   m_Name: GlobalConfig
   m_EditorClassIdentifier: 
-  CodeMode: 3
+  CodeMode: 1
   BuildType: 1
   AppType: 8

+ 4 - 12
Unity/Assets/Scripts/Core/Module/Network/AService.cs

@@ -21,6 +21,10 @@ namespace ET
                 Log.Debug($"message serialize cache: {message.GetType().FullName}");
                 return lastMessageInfo.MemoryStream;
             }
+            
+            // 回收上一个消息跟MemoryBuffer
+            NetServices.Instance.RecycleMessage(this.lastMessageInfo.Message);
+            NetServices.Instance.RecycleMemoryBuffer(this.lastMessageInfo.MemoryStream);
 
             MemoryBuffer stream = NetServices.Instance.FetchMemoryBuffer();
             MessageSerializeHelper.MessageToStream(stream, message);
@@ -28,18 +32,6 @@ namespace ET
             return stream;
         }
         
-        public void Recycle(MessageObject message, MemoryBuffer memoryBuffer)
-        {
-            if (ReferenceEquals(message, this.lastMessageInfo.Message))
-            {
-                return;
-            }
-            NetServices.Instance.RecycleMessage(this.lastMessageInfo.Message);
-            NetServices.Instance.RecycleMemoryBuffer(this.lastMessageInfo.MemoryStream);
-
-            this.lastMessageInfo = (message, memoryBuffer);
-        }
-        
         public virtual void Dispose()
         {
         }

+ 23 - 14
Unity/Assets/Scripts/Core/Module/Network/Extensions.cs

@@ -20,39 +20,48 @@ namespace ET
             int offset,
             int size,
             SocketFlags socketFlags,
-            ref IPEndPointNonAlloc remoteEndPoint)
+            EndPoint remoteEndPoint)
         {
             // call ReceiveFrom with IPEndPointNonAlloc.
             // need to wrap this in ReceiveFrom_NonAlloc because it's not
             // obvious that IPEndPointNonAlloc.Create does NOT create a new
             // IPEndPoint. it saves the result in IPEndPointNonAlloc.temp!
+#if UNITY
             EndPoint casted = remoteEndPoint;
             return  socket.ReceiveFrom(buffer, offset, size, socketFlags, ref casted);
+#else
+            return  socket.ReceiveFrom(buffer, offset, size, socketFlags, ref remoteEndPoint);
+#endif
         }
 
         // same as above, different parameters
-        public static int ReceiveFrom_NonAlloc(this Socket socket, byte[] buffer, ref IPEndPointNonAlloc remoteEndPoint)
+        public static int ReceiveFrom_NonAlloc(this Socket socket, byte[] buffer, ref EndPoint remoteEndPoint)
         {
+#if UNITY
             EndPoint casted = remoteEndPoint;
             return socket.ReceiveFrom(buffer, ref casted);
-        }
+#else
+            return socket.ReceiveFrom(buffer, ref remoteEndPoint);
+#endif
 
+        }
+        
         // SendTo allocates too:
         // https://github.com/mono/mono/blob/f74eed4b09790a0929889ad7fc2cf96c9b6e3757/mcs/class/System/System.Net.Sockets/Socket.cs#L2240
         // -> the allocation is in EndPoint.Serialize()
         // NOTE: technically this function isn't necessary.
         //       could just pass IPEndPointNonAlloc.
         //       still good for strong typing.
-        public static int SendTo_NonAlloc(
-            this Socket socket,
-            byte[] buffer,
-            int offset,
-            int size,
-            SocketFlags socketFlags,
-            IPEndPointNonAlloc remoteEndPoint)
-        {
-            EndPoint casted = remoteEndPoint;
-            return socket.SendTo(buffer, offset, size, socketFlags, casted);
-        }
+        //public static int SendTo_NonAlloc(
+        //    this Socket socket,
+        //    byte[] buffer,
+        //    int offset,
+        //    int size,
+        //    SocketFlags socketFlags,
+        //    IPEndPointNonAlloc remoteEndPoint)
+        //{
+        //    EndPoint casted = remoteEndPoint;
+        //    return socket.SendTo(buffer, offset, size, socketFlags, casted);
+        //}
     }
 }

+ 14 - 5
Unity/Assets/Scripts/Core/Module/Network/IPEndPointNonAlloc.cs

@@ -217,11 +217,6 @@ namespace ET
         public IPEndPointNonAlloc(IPAddress address, int port) : base(address, port)
         {
         }
-        
-        public IPEndPoint DeepCopyIPEndPoint()
-        {
-            return new IPEndPoint(this.Address, this.Port);
-        }
 #endif
         
         public bool Equals(IPEndPoint ipEndPoint)
@@ -239,4 +234,18 @@ namespace ET
             return true;
         }
     }
+    
+    public static class EndPointHelper
+    {
+        public static IPEndPoint Clone(this EndPoint endPoint)
+        {
+#if UNITY
+            IPEndPoint ip = ((IPEndPointNonAlloc)endPoint).DeepCopyIPEndPoint();
+#else
+            IPEndPoint ip = (IPEndPoint)endPoint;
+            ip = new IPEndPoint(ip.Address, ip.Port);
+#endif
+            return ip;
+        }
+    }
 }

+ 4 - 11
Unity/Assets/Scripts/Core/Module/Network/KChannel.cs

@@ -54,8 +54,6 @@ namespace ET
 		private int needReadSplitCount;
 
 		private IPEndPoint remoteAddress;
-		
-		public IPEndPointNonAlloc RemoteAddressNonAlloc { get; private set; }
 
 		public IPEndPoint RemoteAddress
 		{
@@ -65,9 +63,7 @@ namespace ET
 			}
 			set
 			{
-				Log.Debug($"111111111111111111111111111111 set RemoteAddress: {value}");
-				this.remoteAddress = value;
-				this.RemoteAddressNonAlloc = new IPEndPointNonAlloc(value.Address, value.Port);
+				this.remoteAddress = new IPEndPointNonAlloc(value.Address, value.Port);
 			}
 		}
 
@@ -149,7 +145,7 @@ namespace ET
 			{
 				if (this.Error != ErrorCore.ERR_PeerDisconnect)
 				{
-					this.Service.Disconnect(localConn, remoteConn, this.Error, this.RemoteAddressNonAlloc, 3);
+					this.Service.Disconnect(localConn, remoteConn, this.Error, this.RemoteAddress, 3);
 				}
 			}
 			catch (Exception e)
@@ -225,7 +221,7 @@ namespace ET
 				buffer.WriteTo(0, KcpProtocalType.SYN);
 				buffer.WriteTo(1, this.LocalConn);
 				buffer.WriteTo(5, this.RemoteConn);
-				this.socket.SendTo_NonAlloc(buffer, 0, 9, SocketFlags.None, this.RemoteAddressNonAlloc);
+				this.socket.SendTo(buffer, 0, 9, SocketFlags.None, this.RemoteAddress);
 				// 这里很奇怪 调用socket.LocalEndPoint会动到this.RemoteAddressNonAlloc里面的temp,这里就不仔细研究了
 				Log.Info($"kchannel connect {this.LocalConn} {this.RemoteConn} {this.RealAddress}");
 
@@ -401,7 +397,7 @@ namespace ET
 				// 每个消息头部写下该channel的id;
 				buffer.WriteTo(1, this.LocalConn);
 				Marshal.Copy(bytes, buffer, 5, count);
-				this.socket.SendTo_NonAlloc(buffer, 0, count + 5, SocketFlags.None, this.RemoteAddressNonAlloc);
+				this.socket.SendTo(buffer, 0, count + 5, SocketFlags.None, this.RemoteAddress);
 			}
 			catch (Exception e)
 			{
@@ -501,9 +497,6 @@ namespace ET
 			}
 
 			this.KcpSend(actorId, memoryStream);
-			
-			// 回收Message跟MemoryBuffer,减少GC
-			this.Service.Recycle(message, memoryStream);
 		}
 		
 		private void OnRead(MemoryBuffer memoryStream)

+ 8 - 17
Unity/Assets/Scripts/Core/Module/Network/KService.cs

@@ -144,7 +144,8 @@ namespace ET
         private readonly Dictionary<long, KChannel> waitAcceptChannels = new Dictionary<long, KChannel>();
 
         private readonly byte[] cache = new byte[2048];
-        private IPEndPointNonAlloc ipEndPoint = new IPEndPointNonAlloc(IPAddress.Any, 0);
+        
+        private EndPoint ipEndPoint = new IPEndPointNonAlloc(IPAddress.Any, 0);
 
         // 下帧要更新的channel
         private readonly HashSet<long> updateIds = new HashSet<long>();
@@ -195,13 +196,6 @@ namespace ET
             kChannel.RemoteAddress = newIPEndPoint;
         }
 
-        private IPEndPoint CloneAddress()
-        {
-            IPEndPoint ip = this.ipEndPoint.DeepCopyIPEndPoint();
-            Log.Debug($"111111111111111111111 clone: {this.ipEndPoint} {ip}");
-            return ip;
-        }
-
         private void Recv()
         {
             if (this.socket == null)
@@ -212,8 +206,6 @@ namespace ET
             while (socket != null && this.socket.Available > 0)
             {
                 int messageLength = this.socket.ReceiveFrom_NonAlloc(this.cache, ref this.ipEndPoint);
-                
-                Log.Debug($"11111111111111111111111111111111 recv: {this.ipEndPoint}");
 
                 // 长度小于1,不是正常的消息
                 if (messageLength < 1)
@@ -269,7 +261,7 @@ namespace ET
                             // 重连的时候router地址变化, 这个不能放到msg中,必须经过严格的验证才能切换
                             if (!this.ipEndPoint.Equals(kChannel.RemoteAddress))
                             {
-                                kChannel.RemoteAddress = this.CloneAddress();
+                                kChannel.RemoteAddress = this.ipEndPoint.Clone();
                             }
 
                             try
@@ -279,7 +271,7 @@ namespace ET
                                 buffer.WriteTo(1, kChannel.LocalConn);
                                 buffer.WriteTo(5, kChannel.RemoteConn);
                                 buffer.WriteTo(9, connectId);
-                                this.socket.SendTo_NonAlloc(buffer, 0, 13, SocketFlags.None, this.ipEndPoint);
+                                this.socket.SendTo(buffer, 0, 13, SocketFlags.None, this.ipEndPoint);
                             }
                             catch (Exception e)
                             {
@@ -319,7 +311,7 @@ namespace ET
                                     break;
                                 }
 
-                                kChannel = new KChannel(localConn, remoteConn, this.socket, this.CloneAddress(), this);
+                                kChannel = new KChannel(localConn, remoteConn, this.socket, this.ipEndPoint.Clone(), this);
                                 this.waitAcceptChannels.Add(kChannel.RemoteConn, kChannel); // 连接上了或者超时后会删除
                                 this.localConnChannels.Add(kChannel.LocalConn, kChannel);
                                 
@@ -348,8 +340,7 @@ namespace ET
                                 buffer.WriteTo(5, kChannel.RemoteConn);
                                 Log.Info($"kservice syn: {kChannel.Id} {remoteConn} {localConn}");
                                 
-                                Log.Debug($"11111111111111111111111111111111111111: {kChannel.RemoteAddressNonAlloc} {kChannel.RemoteAddress}");
-                                this.socket.SendTo_NonAlloc(buffer, 0, 9, SocketFlags.None, kChannel.RemoteAddressNonAlloc);
+                                this.socket.SendTo(buffer, 0, 9, SocketFlags.None, kChannel.RemoteAddress);
                             }
                             catch (Exception e)
                             {
@@ -499,7 +490,7 @@ namespace ET
             kChannel.Dispose();
         }
 
-        public void Disconnect(uint localConn, uint remoteConn, int error, IPEndPointNonAlloc address, int times)
+        public void Disconnect(uint localConn, uint remoteConn, int error, EndPoint address, int times)
         {
             try
             {
@@ -515,7 +506,7 @@ namespace ET
                 buffer.WriteTo(9, (uint) error);
                 for (int i = 0; i < times; ++i)
                 {
-                    this.socket.SendTo_NonAlloc(buffer, 0, 13, SocketFlags.None, address);
+                    this.socket.SendTo(buffer, 0, 13, SocketFlags.None, address);
                 }
             }
             catch (Exception e)

+ 5 - 0
Unity/Assets/Scripts/Core/Module/Network/NetServices.cs

@@ -300,6 +300,11 @@ namespace ET
 
         public void RecycleMemoryBuffer(MemoryBuffer memoryBuffer)
         {
+            if (memoryBuffer == null)
+            {
+                return;
+            }
+            
             if (!memoryBuffer.IsFromPool)
             {
                 return;

+ 0 - 2
Unity/Assets/Scripts/Core/Module/Network/TChannel.cs

@@ -132,8 +132,6 @@ namespace ET
 				}
 			}
 			
-			this.Service.Recycle(message, stream);
-			
 			if (!this.isSending)
 			{
 				//this.StartSend();