Bladeren bron

1.简化Kcp代码, KChannel的localConn就是KChannel的Id
2.windows下调用timeBeginPeriod,设置Thread.Sleep(1)的间隔时间,windows默认是15.6ms

tanghai 3 jaren geleden
bovenliggende
commit
4927065cac

+ 2 - 2
DotNet/App/Init.cs

@@ -32,11 +32,11 @@ namespace ET
 				Game.AddSingleton<CoroutineLockComponent>();
 				
 				ETTask.ExceptionHandler += Log.Error;
+				
+				Log.Console($"{Parser.Default.FormatCommandLine(Options.Instance)}");
 
 				Game.AddSingleton<CodeLoader>().Start();
 
-				Log.Console($"app start: {Root.Instance.Scene.Id} options: {JsonHelper.ToJson(Options.Instance)} ");
-
 				while (true)
 				{
 					try

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Client/Module/Message/NetClientComponentSystem.cs

@@ -55,7 +55,7 @@ namespace ET.Client
 
         public static Session Create(this NetClientComponent self, IPEndPoint realIPEndPoint)
         {
-            long channelId = RandomGenerator.RandInt64();
+            long channelId = NetServices.Instance.CreateConnectChannelId();
             Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
             session.RemoteAddress = realIPEndPoint;
             if (self.DomainScene().SceneType != SceneType.Benchmark)
@@ -69,7 +69,7 @@ namespace ET.Client
         
         public static Session Create(this NetClientComponent self, IPEndPoint routerIPEndPoint, IPEndPoint realIPEndPoint, uint localConn)
         {
-            long channelId = NetServices.Instance.CreateConnectChannelId(localConn);
+            long channelId = localConn;
             Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
             session.RemoteAddress = realIPEndPoint;
             if (self.DomainScene().SceneType != SceneType.Benchmark)

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

@@ -262,7 +262,7 @@ namespace ET.Server
                     self.ConnectIdNodes.TryGetValue(connectId, out routerNode);
                     if (routerNode == null)
                     {
-                        outerConn = RandomGenerator.RandUInt32();
+                        outerConn = NetServices.Instance.CreateConnectChannelId();
                         routerNode = self.New(realAddress, connectId, outerConn, innerConn, self.CloneAddress());
                         Log.Info($"router create: {realAddress} {connectId} {outerConn} {innerConn} {routerNode.SyncIpEndPoint}");
                         self.OuterNodes.Add(routerNode.OuterConn, routerNode);

+ 2 - 0
Unity/Assets/Scripts/Codes/Model/Share/Entry.cs

@@ -27,6 +27,8 @@ namespace ET
         
         private static async ETTask StartAsync()
         {
+            WinPeriod.Init();
+            
             MongoRegister.Init();
             
             Game.AddSingleton<NetServices>();

+ 21 - 0
Unity/Assets/Scripts/Core/Helper/WinPeriod.cs

@@ -0,0 +1,21 @@
+using System.Runtime.InteropServices;
+
+namespace ET
+{
+    public static class WinPeriod
+    {
+        // 一般默认的精度不止1毫秒(不同操作系统有所不同),需要调用timeBeginPeriod与timeEndPeriod来设置精度
+        [DllImport("winmm")]
+        private static extern void timeBeginPeriod(int t);
+        //[DllImport("winmm")]
+        //static extern void timeEndPeriod(int t);
+
+        public static void Init()
+        {
+            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+            {
+                timeBeginPeriod(1);
+            }
+        }
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Core/Helper/WinPeriod.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 6ad83b047e765b34ea32c6ca7461406d
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 61 - 35
Unity/Assets/Scripts/Core/Module/Network/KChannel.cs

@@ -7,6 +7,8 @@ using System.Runtime.InteropServices;
 
 namespace ET
 {
+
+	
 	public struct KcpWaitPacket
 	{
 		public long ActorId;
@@ -15,7 +17,25 @@ namespace ET
 	
 	public class KChannel : AChannel
 	{
-		public KService Service;
+		[Callback(TimerCoreCallbackId.KServiceConnectTimer)]
+		public class KServiceConnectTimer: ATimer<KChannel>
+		{
+			protected override void Run(KChannel kChannel)
+			{
+				kChannel.Connect();
+			}
+		}
+		
+		[Callback(TimerCoreCallbackId.KServiceNextUpdateTimer)]
+		public class KServiceNextUpdateTimer: ATimer<KChannel>
+		{
+			protected override void Run(KChannel kChannel)
+			{
+				kChannel.Service.AddToUpdate(kChannel.Id);
+			}
+		}
+
+		private readonly KService Service;
 		
 		private Socket socket;
 
@@ -27,12 +47,22 @@ namespace ET
 		
 		public readonly uint CreateTime;
 
-		public uint LocalConn { get; set; }
+		public uint LocalConn
+		{
+			get
+			{
+				return (uint)this.Id; 
+			}
+			private set
+			{
+				this.Id = value;
+			}
+		}
 		public uint RemoteConn { get; set; }
 
 		private readonly byte[] sendCache = new byte[2 * 1024];
 		
-		public bool IsConnected { get; private set; }
+		public bool IsConnected { get; set; }
 
 		public string RealAddress { get; set; }
 		
@@ -66,14 +96,12 @@ namespace ET
 		}
 		
 		// connect
-		public KChannel(long id, uint localConn, Socket socket, IPEndPoint remoteEndPoint, KService kService)
+		public KChannel(uint localConn, Socket socket, IPEndPoint remoteEndPoint, KService kService)
 		{
 			this.LocalConn = localConn;
-
-			this.Id = id;
 			this.ChannelType = ChannelType.Connect;
 			
-			Log.Info($"channel create: {this.Id} {this.LocalConn} {remoteEndPoint} {this.ChannelType}");
+			Log.Info($"channel create: {this.LocalConn} {remoteEndPoint} {this.ChannelType}");
 			
 			this.kcp = IntPtr.Zero;
 			this.Service = kService;
@@ -87,12 +115,11 @@ namespace ET
 		}
 
 		// accept
-		public KChannel(long id, uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService)
+		public KChannel(uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService)
 		{
-			this.Id = id;
 			this.ChannelType = ChannelType.Accept;
 			
-			Log.Info($"channel create: {this.Id} {localConn} {remoteConn} {remoteEndPoint} {this.ChannelType}");
+			Log.Info($"channel create: {localConn} {remoteConn} {remoteEndPoint} {this.ChannelType}");
 
 			this.Service = kService;
 			this.LocalConn = localConn;
@@ -116,7 +143,7 @@ namespace ET
 
 			uint localConn = this.LocalConn;
 			uint remoteConn = this.RemoteConn;
-			Log.Info($"channel dispose: {this.Id} {localConn} {remoteConn}");
+			Log.Info($"channel dispose: {localConn} {remoteConn}");
 			
 			long id = this.Id;
 			this.Id = 0;
@@ -152,7 +179,7 @@ namespace ET
 			this.kcp = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.Service.Id));
 			this.InitKcp();
 
-			Log.Info($"channel connected: {this.Id} {this.LocalConn} {this.RemoteConn} {this.RemoteAddress}");
+			Log.Info($"channel connected: {this.LocalConn} {this.RemoteConn} {this.RemoteAddress}");
 			this.IsConnected = true;
 			this.lastRecvTime = this.Service.TimeNow;
 			
@@ -175,8 +202,21 @@ namespace ET
 		{
 			try
 			{
+				if (this.IsConnected)
+				{
+					return;
+				}
+				
 				uint timeNow = this.Service.TimeNow;
 				
+				// 5秒连接超时
+				if (timeNow > this.CreateTime + 5 * 1000)
+				{
+					Log.Error($"kChannel connect timeout: {this.Id} {this.RemoteConn} {timeNow} {this.CreateTime} {this.ChannelType} {this.RemoteAddress}");
+					this.OnError(ErrorCore.ERR_KcpConnectTimeout);
+					return;
+				}
+				
 				this.lastRecvTime = timeNow;
 				
 				byte[] buffer = sendCache;
@@ -184,10 +224,11 @@ namespace ET
 				buffer.WriteTo(1, this.LocalConn);
 				buffer.WriteTo(5, this.RemoteConn);
 				this.socket.SendTo(buffer, 0, 9, SocketFlags.None, this.RemoteAddress);
-				Log.Info($"kchannel connect {this.Id} {this.LocalConn} {this.RemoteConn} {this.RealAddress} {this.socket.LocalEndPoint}");
+				Log.Info($"kchannel connect {this.LocalConn} {this.RemoteConn} {this.RealAddress} {this.socket.LocalEndPoint}");
 				
 				// 300毫秒后再次update发送connect请求
-				this.Service.AddToUpdate(300, this.Id);
+				long tillTime = TimeHelper.ClientNow() + 300;
+				NetServices.Instance.TimerComponent.NewOnceTimer(tillTime, TimerCoreCallbackId.KServiceConnectTimer, this);
 			}
 			catch (Exception e)
 			{
@@ -206,21 +247,8 @@ namespace ET
 			uint timeNow = this.Service.TimeNow;
 			
 			// 如果还没连接上,发送连接请求
-			if (!this.IsConnected)
+			if (!this.IsConnected && this.ChannelType == ChannelType.Connect)
 			{
-				// 10秒超时没连接上则报错
-				if (timeNow - this.CreateTime > 10000)
-				{
-					Log.Error($"kChannel connect timeout: {this.Id} {this.RemoteConn} {timeNow} {this.CreateTime} {this.ChannelType} {this.RemoteAddress}");
-					this.OnError(ErrorCore.ERR_KcpConnectTimeout);
-					return;
-				}
-				switch (ChannelType)
-				{
-					case ChannelType.Connect:
-						this.Connect();
-						break;
-				}
 				return;
 			}
 
@@ -241,8 +269,8 @@ namespace ET
 			}
 
 			uint nextUpdateTime = Kcp.KcpCheck(this.kcp, timeNow);
-			long tillTime = TimeHelper.ClientNow() + nextUpdateTime - this.Service.TimeNow;
-			this.Service.AddToUpdate(nextUpdateTime, this.Id);
+			long tillTime = nextUpdateTime - timeNow + TimeHelper.ClientNow();
+			NetServices.Instance.TimerComponent.NewOnceTimer(tillTime, TimerCoreCallbackId.KServiceNextUpdateTimer, this);
 		}
 
 		public void HandleRecv(byte[] date, int offset, int length)
@@ -252,10 +280,8 @@ namespace ET
 				return;
 			}
 
-			this.IsConnected = true;
-			
 			Kcp.KcpInput(this.kcp, date, offset, length);
-			this.Service.AddToUpdate(0, this.Id);
+			this.Service.AddToUpdate(this.Id);
 
 			while (true)
 			{
@@ -434,7 +460,7 @@ namespace ET
 				}
 			}
 
-			this.Service.AddToUpdate(0, this.Id);
+			this.Service.AddToUpdate(this.Id);
 		}
 		
 		public void Send(long actorId, MemoryStream stream)
@@ -468,7 +494,7 @@ namespace ET
 			
 			if (n > maxWaitSize)
 			{
-				Log.Error($"kcp wait snd too large: {n}: {this.Id} {this.LocalConn} {this.RemoteConn}");
+				Log.Error($"kcp wait snd too large: {n}: {this.LocalConn} {this.RemoteConn}");
 				this.OnError(ErrorCore.ERR_KcpWaitSendSizeTooLarge);
 				return;
 			}

+ 44 - 134
Unity/Assets/Scripts/Core/Module/Network/KService.cs

@@ -116,7 +116,15 @@ namespace ET
                 this.socket.ReceiveBufferSize = Kcp.OneM * 64;
             }
 
-            this.socket.Bind(ipEndPoint);
+            try
+            {
+                this.socket.Bind(ipEndPoint);
+            }
+            catch (Exception e)
+            {
+                throw new Exception($"bind error: {ipEndPoint}", e);
+            }
+            
             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
             {
                 const uint IOC_IN = 0x80000000;
@@ -142,7 +150,6 @@ namespace ET
         }
 
         // 保存所有的channel
-        private readonly Dictionary<long, KChannel> idChannels = new Dictionary<long, KChannel>();
         private readonly Dictionary<long, KChannel> localConnChannels = new Dictionary<long, KChannel>();
         private readonly Dictionary<long, KChannel> waitConnectChannels = new Dictionary<long, KChannel>();
 
@@ -150,18 +157,8 @@ namespace ET
         private EndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 0);
 
         // 下帧要更新的channel
-        private readonly HashSet<long> updateChannels = new HashSet<long>();
+        private readonly HashSet<long> updateIds = new HashSet<long>();
         
-        // 下次时间更新的channel
-        private readonly MultiMap<long, long> timeId = new MultiMap<long, long>();
-
-        private readonly List<long> timeOutTime = new List<long>();
-
-        // 记录最小时间,不用每次都去MultiMap取第一个值
-        private long minTime;
-
-        private readonly List<long> waitRemoveChannels = new List<long>();
-
         public override bool IsDispose()
         {
             return this.socket == null;
@@ -171,7 +168,7 @@ namespace ET
         {
             base.Dispose();
             
-            foreach (long channelId in this.idChannels.Keys.ToArray())
+            foreach (long channelId in this.localConnChannels.Keys.ToArray())
             {
                 this.Remove(channelId);
             }
@@ -238,13 +235,13 @@ namespace ET
                             // 这里必须校验localConn,客户端重连,localConn一定是一样的
                             if (localConn != kChannel.LocalConn)
                             {
-                                Log.Warning($"kchannel reconnect localconn error: {kChannel.Id} {localConn} {remoteConn} {realAddress} {kChannel.LocalConn}");
+                                Log.Warning($"kchannel reconnect localconn error: {localConn} {remoteConn} {realAddress} {kChannel.LocalConn}");
                                 break;
                             }
 
                             if (remoteConn != kChannel.RemoteConn)
                             {
-                                Log.Warning($"kchannel reconnect remoteconn error: {kChannel.Id} {localConn} {remoteConn} {realAddress} {kChannel.RemoteConn}");
+                                Log.Warning($"kchannel reconnect remoteconn error: {localConn} {remoteConn} {realAddress} {kChannel.RemoteConn}");
                                 break;
                             }
 
@@ -292,21 +289,16 @@ namespace ET
                             this.waitConnectChannels.TryGetValue(remoteConn, out kChannel);
                             if (kChannel == null)
                             {
-                                // accept的localConn不能与内网进程号的ChannelId冲突,所以设置为一个大的随机数
-                                localConn = NetServices.Instance.CreateRandomLocalConn();
+                                // accept的localConn不能与connect的localConn冲突,所以设置为一个大的数
+                                // localConn被人猜出来问题不大,因为remoteConn是随机的第三方并不知道
+                                localConn = NetServices.Instance.CreateAcceptChannelId();
                                 // 已存在同样的localConn,则不处理,等待下次sync
                                 if (this.localConnChannels.ContainsKey(localConn))
                                 {
                                     break;
                                 }
-                                long id = NetServices.Instance.CreateAcceptChannelId(localConn);
-                                if (this.idChannels.ContainsKey(id))
-                                {
-                                    break;
-                                }
 
-                                kChannel = new KChannel(id, localConn, remoteConn, this.socket, this.CloneAddress(), this);
-                                this.idChannels.Add(kChannel.Id, kChannel);
+                                kChannel = new KChannel(localConn, remoteConn, this.socket, this.CloneAddress(), this);
                                 this.waitConnectChannels.Add(kChannel.RemoteConn, kChannel); // 连接上了或者超时后会删除
                                 this.localConnChannels.Add(kChannel.LocalConn, kChannel);
 
@@ -353,10 +345,10 @@ namespace ET
 
                             remoteConn = BitConverter.ToUInt32(this.cache, 1);
                             localConn = BitConverter.ToUInt32(this.cache, 5);
-                            kChannel = this.GetByLocalConn(localConn);
+                            kChannel = this.Get(localConn);
                             if (kChannel != null)
                             {
-                                Log.Info($"kservice ack: {kChannel.Id} {remoteConn} {localConn}");
+                                Log.Info($"kservice ack: {localConn} {remoteConn}");
                                 kChannel.RemoteConn = remoteConn;
                                 kChannel.HandleConnnect();
                             }
@@ -374,7 +366,7 @@ namespace ET
                             int error = BitConverter.ToInt32(this.cache, 9);
 
                             // 处理chanel
-                            kChannel = this.GetByLocalConn(localConn);
+                            kChannel = this.Get(localConn);
                             if (kChannel == null)
                             {
                                 break;
@@ -386,7 +378,7 @@ namespace ET
                                 break;
                             }
                             
-                            Log.Info($"kservice recv fin: {kChannel.Id} {localConn} {remoteConn} {error}");
+                            Log.Info($"kservice recv fin: {localConn} {remoteConn} {error}");
                             kChannel.OnError(ErrorCore.ERR_PeerDisconnect);
 
                             break;
@@ -400,7 +392,7 @@ namespace ET
                             remoteConn = BitConverter.ToUInt32(this.cache, 1);
                             localConn = BitConverter.ToUInt32(this.cache, 5);
 
-                            kChannel = this.GetByLocalConn(localConn);
+                            kChannel = this.Get(localConn);
                             if (kChannel == null)
                             {
                                 // 通知对方断开
@@ -413,7 +405,13 @@ namespace ET
                             {
                                 break;
                             }
-                            
+
+                            // 对方发来msg,说明kchannel连接完成
+                            if (!kChannel.IsConnected)
+                            {
+                                kChannel.IsConnected = true;
+                                this.waitConnectChannels.Remove(remoteConn);
+                            }
                             kChannel.HandleRecv(this.cache, 5, messageLength - 5);
                             break;
                     }
@@ -428,20 +426,13 @@ namespace ET
         public KChannel Get(long id)
         {
             KChannel channel;
-            this.idChannels.TryGetValue(id, out channel);
-            return channel;
-        }
-        
-        private KChannel GetByLocalConn(uint localConn)
-        {
-            KChannel channel;
-            this.localConnChannels.TryGetValue(localConn, out channel);
+            this.localConnChannels.TryGetValue(id, out channel);
             return channel;
         }
 
         public override void Create(long id, IPEndPoint address)
         {
-            if (this.idChannels.TryGetValue(id, out KChannel kChannel))
+            if (this.localConnChannels.TryGetValue(id, out KChannel kChannel))
             {
                 return;
             }
@@ -449,9 +440,8 @@ namespace ET
             try
             {
                 // 低32bit是localConn
-                uint localConn = (uint) ((ulong) id & uint.MaxValue);
-                kChannel = new KChannel(id, localConn, this.socket, address, this);
-                this.idChannels.Add(id, kChannel);
+                uint localConn = (uint)id;
+                kChannel = new KChannel(localConn, this.socket, address, this);
                 this.localConnChannels.Add(kChannel.LocalConn, kChannel);
             }
             catch (Exception e)
@@ -462,12 +452,12 @@ namespace ET
 
         public override void Remove(long id)
         {
-            if (!this.idChannels.TryGetValue(id, out KChannel kChannel))
+            if (!this.localConnChannels.TryGetValue(id, out KChannel kChannel))
             {
                 return;
             }
             Log.Info($"kservice remove channel: {id} {kChannel.LocalConn} {kChannel.RemoteConn}");
-            this.idChannels.Remove(id);
+            this.localConnChannels.Remove(id);
             this.localConnChannels.Remove(kChannel.LocalConn);
             if (this.waitConnectChannels.TryGetValue(kChannel.RemoteConn, out KChannel waitChannel))
             {
@@ -523,9 +513,12 @@ namespace ET
         {
             this.Recv();
             
-            this.TimerOut();
-            
-            foreach (long id in updateChannels)
+            this.UpdateChannel();
+        }
+
+        private void UpdateChannel()
+        {
+            foreach (long id in this.updateIds)
             {
                 KChannel kChannel = this.Get(id);
                 if (kChannel == null)
@@ -540,96 +533,13 @@ namespace ET
 
                 kChannel.Update();
             }
-
-            this.updateChannels.Clear();
-            
-            this.RemoveConnectTimeoutChannels();
-        }
-
-        private void RemoveConnectTimeoutChannels()
-        {
-            waitRemoveChannels.Clear();
-            foreach (long channelId in this.waitConnectChannels.Keys)
-            {
-                this.waitConnectChannels.TryGetValue(channelId, out KChannel kChannel);
-                if (kChannel == null)
-                {
-                    Log.Error($"RemoveConnectTimeoutChannels not found kchannel: {channelId}");
-                    continue;
-                }
-
-                // 连接上了要马上删除
-                if (kChannel.IsConnected)
-                {
-                    waitRemoveChannels.Add(channelId);
-                }
-
-                // 10秒连接超时
-                if (this.TimeNow > kChannel.CreateTime + 10 * 1000)
-                {
-                    waitRemoveChannels.Add(channelId);
-                }
-            }
-
-            foreach (long channelId in waitRemoveChannels)
-            {
-                this.waitConnectChannels.Remove(channelId);
-            }
-        }
-        
-        // 计算到期需要update的channel
-        private void TimerOut()
-        {
-            if (this.timeId.Count == 0)
-            {
-                return;
-            }
-
-            uint timeNow = this.TimeNow;
-
-            if (timeNow < this.minTime)
-            {
-                return;
-            }
-
-            this.timeOutTime.Clear();
-
-            foreach (KeyValuePair<long, List<long>> kv in this.timeId)
-            {
-                long k = kv.Key;
-                if (k > timeNow)
-                {
-                    minTime = k;
-                    break;
-                }
-
-                this.timeOutTime.Add(k);
-            }
-
-            foreach (long k in this.timeOutTime)
-            {
-                foreach (long v in this.timeId[k])
-                {
-                    this.updateChannels.Add(v);
-                }
-
-                this.timeId.Remove(k);
-            }
+            this.updateIds.Clear();
         }
         
         // 服务端需要看channel的update时间是否已到
-        public void AddToUpdate(long time, long id)
+        public void AddToUpdate(long id)
         {
-            if (time == 0)
-            {
-                this.updateChannels.Add(id);
-                return;
-            }
-            if (time < this.minTime)
-            {
-                this.minTime = time;
-            }
-            this.timeId.Add(time, id);
+            this.updateIds.Add(id);
         }
     }
 }

+ 25 - 20
Unity/Assets/Scripts/Core/Module/Network/NetServices.cs

@@ -78,12 +78,6 @@ namespace ET
         {
             return this.typeOpcode.GetKeyByValue(opcode);
         }
-        
-        // 防止与内网进程号的ChannelId冲突,所以设置为一个大的随机数
-        public uint CreateRandomLocalConn()
-        {
-            return (1u << 31) | RandomGenerator.RandUInt32();
-        }
 
 #endregion
 
@@ -96,13 +90,6 @@ namespace ET
         private readonly Dictionary<int, Action<long, int>> errorCallback = new Dictionary<int, Action<long, int>>();
         
         private int serviceIdGenerator;
-        
-        // localConn放在低32bit
-        private long connectIdGenerater = int.MaxValue;
-        public long CreateConnectChannelId(uint localConn)
-        {
-            return (--this.connectIdGenerater << 32) | localConn;
-        }
 
         public async Task<(uint, uint)> GetKChannelConn(int serviceId, long channelId)
         {
@@ -229,6 +216,7 @@ namespace ET
         
         private readonly Dictionary<int, AService> services = new Dictionary<int, AService>();
         private readonly Queue<int> queue = new Queue<int>();
+        public readonly TimerComponent TimerComponent = new TimerComponent();
         
         private void Add(AService aService)
         {
@@ -249,13 +237,6 @@ namespace ET
             }
         }
 
-        // localConn放在低32bit
-        private long acceptIdGenerater = 1;
-        public long CreateAcceptChannelId(uint localConn)
-        {
-            return (++this.acceptIdGenerater << 32) | localConn;
-        }
-
         private void RunNetThreadOperator()
         {
             while (true)
@@ -353,6 +334,8 @@ namespace ET
             }
             
             this.RunNetThreadOperator();
+            
+            TimerComponent.Update();
         }
 
         public void OnAccept(int serviceId, long channelId, IPEndPoint ipEndPoint)
@@ -374,5 +357,27 @@ namespace ET
         }
 
 #endregion
+
+#region 主线程kcp id生成
+
+        // 这个因为是NetClientComponent中使用,不会与Accept冲突
+        public uint CreateConnectChannelId()
+        {
+            return RandomGenerator.RandUInt32();
+        }
+
+#endregion
+
+#region 网络线程kcp id生成
+
+        // 防止与内网进程号的ChannelId冲突,所以设置为一个大的随机数
+        private uint acceptIdGenerator = uint.MaxValue;
+        public uint CreateAcceptChannelId()
+        {
+            return --this.acceptIdGenerator;
+        }
+
+#endregion
+
     }
 }

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

@@ -151,7 +151,6 @@ namespace ET
 				}
 			}
 			
-
 			if (!this.isSending)
 			{
 				//this.StartSend();

+ 1 - 1
Unity/Assets/Scripts/Core/Module/Network/TService.cs

@@ -77,7 +77,7 @@ namespace ET
 
 			try
 			{
-				long id = NetServices.Instance.CreateAcceptChannelId(0);
+				long id = NetServices.Instance.CreateAcceptChannelId();
 				TChannel channel = new TChannel(id, acceptSocket, this);
 				this.idChannels.Add(channel.Id, channel);
 				long channelId = channel.Id;

+ 2 - 0
Unity/Assets/Scripts/Core/TimerCoreCallbackId.cs

@@ -3,5 +3,7 @@
     public static class TimerCoreCallbackId
     {
         public const int CoroutineTimeout = 1;
+        public const int KServiceConnectTimer = 2;
+        public const int KServiceNextUpdateTimer = 3;
     }
 }