RouterComponentSystem.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Runtime.InteropServices;
  5. namespace ET.Server
  6. {
  7. [FriendOf(typeof (RouterComponent))]
  8. [FriendOf(typeof (RouterNode))]
  9. public static class RouterComponentSystem
  10. {
  11. [ObjectSystem]
  12. public class RandomGeneratorponentAwakeSystem: AwakeSystem<RouterComponent, IPEndPoint, string>
  13. {
  14. protected override void Awake(RouterComponent self, IPEndPoint ipEndPoint, string innerIP)
  15. {
  16. self.OuterSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
  17. self.OuterSocket.Bind(new IPEndPoint(IPAddress.Any, ipEndPoint.Port));
  18. if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  19. {
  20. self.OuterSocket.SendBufferSize = 16 * Kcp.OneM;
  21. self.OuterSocket.ReceiveBufferSize = 16 * Kcp.OneM;
  22. }
  23. self.InnerSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
  24. self.InnerSocket.Bind(new IPEndPoint(IPAddress.Any, 0));
  25. if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  26. {
  27. self.InnerSocket.SendBufferSize = 16 * Kcp.OneM;
  28. self.InnerSocket.ReceiveBufferSize = 16 * Kcp.OneM;
  29. }
  30. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  31. {
  32. const uint IOC_IN = 0x80000000;
  33. const uint IOC_VENDOR = 0x18000000;
  34. uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
  35. self.OuterSocket.IOControl((int) SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null);
  36. self.InnerSocket.IOControl((int) SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null);
  37. }
  38. }
  39. }
  40. [ObjectSystem]
  41. public class RouterComponentDestroySystem: DestroySystem<RouterComponent>
  42. {
  43. protected override void Destroy(RouterComponent self)
  44. {
  45. self.OuterSocket.Dispose();
  46. self.InnerSocket.Dispose();
  47. self.OuterNodes.Clear();
  48. self.IPEndPoint = null;
  49. }
  50. }
  51. [ObjectSystem]
  52. public class RouterComponentUpdateSystem: UpdateSystem<RouterComponent>
  53. {
  54. protected override void Update(RouterComponent self)
  55. {
  56. long timeNow = TimeHelper.ClientNow();
  57. self.RecvOuter(timeNow);
  58. self.RecvInner(timeNow);
  59. // 每秒钟检查一次
  60. if (timeNow - self.LastCheckTime > 1000)
  61. {
  62. self.CheckConnectTimeout(timeNow);
  63. self.LastCheckTime = timeNow;
  64. }
  65. }
  66. }
  67. private static IPEndPoint CloneAddress(this RouterComponent self)
  68. {
  69. IPEndPoint ipEndPoint = (IPEndPoint) self.IPEndPoint;
  70. return new IPEndPoint(ipEndPoint.Address, ipEndPoint.Port);
  71. }
  72. // 接收udp消息
  73. private static void RecvOuter(this RouterComponent self, long timeNow)
  74. {
  75. while (self.OuterSocket != null && self.OuterSocket.Available > 0)
  76. {
  77. try
  78. {
  79. int messageLength = self.OuterSocket.ReceiveFrom(self.Cache, ref self.IPEndPoint);
  80. self.RecvOuterHandler(messageLength, timeNow);
  81. }
  82. catch (Exception e)
  83. {
  84. Log.Error(e);
  85. }
  86. }
  87. }
  88. private static void CheckConnectTimeout(this RouterComponent self, long timeNow)
  89. {
  90. // 检查连接过程超时
  91. using (ListComponent<long> listComponent = ListComponent<long>.Create())
  92. {
  93. foreach (var kv in self.ConnectIdNodes)
  94. {
  95. if (timeNow < kv.Value.LastRecvOuterTime + 10 * 1000)
  96. {
  97. continue;
  98. }
  99. listComponent.Add(kv.Value.Id);
  100. }
  101. foreach (long id in listComponent)
  102. {
  103. self.OnError(id, ErrorCore.ERR_KcpRouterConnectFail);
  104. }
  105. }
  106. // 外网消息超时就断开,内网因为会一直重发,没有重连之前内网连接一直存在,会导致router一直收到内网消息
  107. using (ListComponent<long> listComponent = ListComponent<long>.Create())
  108. {
  109. foreach (var kv in self.OuterNodes)
  110. {
  111. // 比session超时应该多10秒钟
  112. if (timeNow < kv.Value.LastRecvOuterTime + ConstValue.SessionTimeoutTime + 10 * 1000)
  113. {
  114. continue;
  115. }
  116. listComponent.Add(kv.Value.Id);
  117. }
  118. foreach (long id in listComponent)
  119. {
  120. self.OnError(id, ErrorCore.ERR_KcpRouterTimeout);
  121. }
  122. }
  123. }
  124. private static void RecvInner(this RouterComponent self, long timeNow)
  125. {
  126. while (self.InnerSocket != null && self.InnerSocket.Available > 0)
  127. {
  128. try
  129. {
  130. int messageLength = self.InnerSocket.ReceiveFrom(self.Cache, ref self.IPEndPoint);
  131. self.RecvInnerHandler(messageLength, timeNow);
  132. }
  133. catch (Exception e)
  134. {
  135. Log.Error(e);
  136. }
  137. }
  138. }
  139. private static void RecvOuterHandler(this RouterComponent self, int messageLength, long timeNow)
  140. {
  141. // 长度小于1,不是正常的消息
  142. if (messageLength < 1)
  143. {
  144. return;
  145. }
  146. // accept
  147. byte flag = self.Cache[0];
  148. switch (flag)
  149. {
  150. case KcpProtocalType.RouterReconnectSYN:
  151. {
  152. if (messageLength < 13)
  153. {
  154. break;
  155. }
  156. uint outerConn = BitConverter.ToUInt32(self.Cache, 1);
  157. uint innerConn = BitConverter.ToUInt32(self.Cache, 5);
  158. uint connectId = BitConverter.ToUInt32(self.Cache, 9);
  159. string realAddress = self.Cache.ToStr(13, messageLength - 13);
  160. RouterNode routerNode;
  161. // RouterAck之后ConnectIdNodes会删除,加入到OuterNodes中来
  162. if (!self.OuterNodes.TryGetValue(outerConn, out routerNode))
  163. {
  164. self.ConnectIdNodes.TryGetValue(connectId, out routerNode);
  165. if (routerNode == null)
  166. {
  167. Log.Info($"router create reconnect: {self.IPEndPoint} {realAddress} {connectId} {outerConn} {innerConn}");
  168. routerNode = self.New(realAddress, connectId, outerConn, innerConn, self.CloneAddress());
  169. // self.OuterNodes 这里不能add,因为还没验证完成,要在RouterAck中加入
  170. }
  171. }
  172. if (routerNode.ConnectId != connectId)
  173. {
  174. Log.Warning($"kcp router router reconnect connectId diff1: {routerNode.SyncIpEndPoint} {(IPEndPoint) self.IPEndPoint}");
  175. break;
  176. }
  177. // 不是自己的,outerConn冲突, 直接break,也就是说这个软路由上有个跟自己outerConn冲突的连接,就不能连接了
  178. // 这个路由连接不上,客户端会换个软路由,所以没关系
  179. if (routerNode.InnerConn != innerConn)
  180. {
  181. Log.Warning($"kcp router router reconnect inner conn diff1: {routerNode.SyncIpEndPoint} {(IPEndPoint) self.IPEndPoint}");
  182. break;
  183. }
  184. if (routerNode.OuterConn != outerConn)
  185. {
  186. Log.Warning($"kcp router router reconnect outer conn diff1: {routerNode.SyncIpEndPoint} {(IPEndPoint) self.IPEndPoint}");
  187. break;
  188. }
  189. // 校验ip,连接过程中ip不能变化
  190. if (!Equals(routerNode.SyncIpEndPoint, self.IPEndPoint))
  191. {
  192. Log.Warning($"kcp router syn ip is diff1: {routerNode.SyncIpEndPoint} {(IPEndPoint) self.IPEndPoint}");
  193. break;
  194. }
  195. // 校验内网地址
  196. if (routerNode.InnerAddress != realAddress)
  197. {
  198. Log.Warning($"router sync error2: {routerNode.OuterConn} {routerNode.InnerAddress} {outerConn} {realAddress}");
  199. break;
  200. }
  201. if (++routerNode.RouterSyncCount > 40)
  202. {
  203. self.OnError(routerNode.Id, ErrorCore.ERR_KcpRouterRouterSyncCountTooMuchTimes);
  204. break;
  205. }
  206. // 转发到内网
  207. self.Cache.WriteTo(0, KcpProtocalType.RouterReconnectSYN);
  208. self.Cache.WriteTo(1, outerConn);
  209. self.Cache.WriteTo(5, innerConn);
  210. self.Cache.WriteTo(9, connectId);
  211. self.InnerSocket.SendTo(self.Cache, 0, 13, SocketFlags.None, routerNode.InnerIpEndPoint);
  212. if (!routerNode.CheckOuterCount(timeNow))
  213. {
  214. self.OnError(routerNode.Id, ErrorCore.ERR_KcpRouterTooManyPackets);
  215. }
  216. break;
  217. }
  218. case KcpProtocalType.RouterSYN:
  219. {
  220. if (messageLength < 13)
  221. {
  222. break;
  223. }
  224. uint outerConn = BitConverter.ToUInt32(self.Cache, 1);
  225. uint innerConn = BitConverter.ToUInt32(self.Cache, 5);
  226. uint connectId = BitConverter.ToUInt32(self.Cache, 9);
  227. string realAddress = self.Cache.ToStr(13, messageLength - 13);
  228. RouterNode routerNode;
  229. self.ConnectIdNodes.TryGetValue(connectId, out routerNode);
  230. if (routerNode == null)
  231. {
  232. outerConn = NetServices.Instance.CreateConnectChannelId();
  233. routerNode = self.New(realAddress, connectId, outerConn, innerConn, self.CloneAddress());
  234. Log.Info($"router create: {realAddress} {connectId} {outerConn} {innerConn} {routerNode.SyncIpEndPoint}");
  235. self.OuterNodes.Add(routerNode.OuterConn, routerNode);
  236. }
  237. if (++routerNode.RouterSyncCount > 40)
  238. {
  239. self.OnError(routerNode.Id, ErrorCore.ERR_KcpRouterRouterSyncCountTooMuchTimes);
  240. break;
  241. }
  242. // 校验ip,连接过程中ip不能变化
  243. if (!Equals(routerNode.SyncIpEndPoint, self.IPEndPoint))
  244. {
  245. Log.Warning($"kcp router syn ip is diff1: {routerNode.SyncIpEndPoint} {self.IPEndPoint}");
  246. break;
  247. }
  248. // 校验内网地址
  249. if (routerNode.InnerAddress != realAddress)
  250. {
  251. Log.Warning($"router sync error2: {routerNode.OuterConn} {routerNode.InnerAddress} {outerConn} {realAddress}");
  252. break;
  253. }
  254. self.Cache.WriteTo(0, KcpProtocalType.RouterACK);
  255. self.Cache.WriteTo(1, routerNode.InnerConn);
  256. self.Cache.WriteTo(5, routerNode.OuterConn);
  257. self.OuterSocket.SendTo(self.Cache, 0, 9, SocketFlags.None, routerNode.SyncIpEndPoint);
  258. if (!routerNode.CheckOuterCount(timeNow))
  259. {
  260. self.OnError(routerNode.Id, ErrorCore.ERR_KcpRouterTooManyPackets);
  261. }
  262. break;
  263. }
  264. case KcpProtocalType.SYN:
  265. {
  266. // 长度!=13,不是accpet消息
  267. if (messageLength != 9)
  268. {
  269. break;
  270. }
  271. uint outerConn = BitConverter.ToUInt32(self.Cache, 1); // remote
  272. uint innerConn = BitConverter.ToUInt32(self.Cache, 5);
  273. if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouter))
  274. {
  275. Log.Warning($"kcp router syn not found outer nodes: {outerConn} {innerConn}");
  276. break;
  277. }
  278. if (++kcpRouter.SyncCount > 20)
  279. {
  280. self.OnError(kcpRouter.Id, ErrorCore.ERR_KcpRouterSyncCountTooMuchTimes);
  281. break;
  282. }
  283. // 校验ip,连接过程中ip不能变化
  284. IPEndPoint ipEndPoint = (IPEndPoint) self.IPEndPoint;
  285. if (!Equals(kcpRouter.SyncIpEndPoint.Address, ipEndPoint.Address))
  286. {
  287. Log.Warning($"kcp router syn ip is diff3: {kcpRouter.SyncIpEndPoint.Address} {ipEndPoint.Address}");
  288. break;
  289. }
  290. // 发了syn过来,那么RouterSyn就成功了,可以删除ConnectId
  291. self.ConnectIdNodes.Remove(kcpRouter.ConnectId);
  292. kcpRouter.LastRecvOuterTime = timeNow;
  293. kcpRouter.OuterIpEndPoint = self.CloneAddress();
  294. // 转发到内网, 带上客户端的地址
  295. self.Cache.WriteTo(0, KcpProtocalType.SYN);
  296. self.Cache.WriteTo(1, outerConn);
  297. self.Cache.WriteTo(5, innerConn);
  298. byte[] addressBytes = ipEndPoint.ToString().ToByteArray();
  299. Array.Copy(addressBytes, 0, self.Cache, 9, addressBytes.Length);
  300. Log.Info($"kcp router syn: {outerConn} {innerConn} {kcpRouter.InnerIpEndPoint} {kcpRouter.OuterIpEndPoint}");
  301. self.InnerSocket.SendTo(self.Cache, 0, 9 + addressBytes.Length, SocketFlags.None, kcpRouter.InnerIpEndPoint);
  302. if (!kcpRouter.CheckOuterCount(timeNow))
  303. {
  304. self.OnError(kcpRouter.Id, ErrorCore.ERR_KcpRouterTooManyPackets);
  305. }
  306. break;
  307. }
  308. case KcpProtocalType.FIN: // 断开
  309. {
  310. // 长度!=13,不是DisConnect消息
  311. if (messageLength != 13)
  312. {
  313. break;
  314. }
  315. uint outerConn = BitConverter.ToUInt32(self.Cache, 1);
  316. uint innerConn = BitConverter.ToUInt32(self.Cache, 5);
  317. if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouter))
  318. {
  319. Log.Warning($"kcp router outer fin not found outer nodes: {outerConn} {innerConn}");
  320. break;
  321. }
  322. // 比对innerConn
  323. if (kcpRouter.InnerConn != innerConn)
  324. {
  325. Log.Warning($"router node innerConn error: {innerConn} {outerConn} {kcpRouter.Status}");
  326. break;
  327. }
  328. kcpRouter.LastRecvOuterTime = timeNow;
  329. Log.Info($"kcp router outer fin: {outerConn} {innerConn} {kcpRouter.InnerIpEndPoint}");
  330. self.InnerSocket.SendTo(self.Cache, 0, messageLength, SocketFlags.None, kcpRouter.InnerIpEndPoint);
  331. if (!kcpRouter.CheckOuterCount(timeNow))
  332. {
  333. self.OnError(kcpRouter.Id, ErrorCore.ERR_KcpRouterTooManyPackets);
  334. }
  335. break;
  336. }
  337. case KcpProtocalType.MSG:
  338. {
  339. // 长度<9,不是Msg消息
  340. if (messageLength < 9)
  341. {
  342. break;
  343. }
  344. // 处理chanel
  345. uint outerConn = BitConverter.ToUInt32(self.Cache, 1); // remote
  346. uint innerConn = BitConverter.ToUInt32(self.Cache, 5); // local
  347. if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouter))
  348. {
  349. Log.Warning($"kcp router msg not found outer nodes: {outerConn} {innerConn}");
  350. break;
  351. }
  352. if (kcpRouter.Status != RouterStatus.Msg)
  353. {
  354. Log.Warning($"router node status error: {innerConn} {outerConn} {kcpRouter.Status}");
  355. break;
  356. }
  357. // 比对innerConn
  358. if (kcpRouter.InnerConn != innerConn)
  359. {
  360. Log.Warning($"router node innerConn error: {innerConn} {outerConn} {kcpRouter.Status}");
  361. break;
  362. }
  363. // 重连的时候,没有经过syn阶段,可能没有设置OuterIpEndPoint,重连请求Router的Socket跟发送消息的Socket不是同一个,所以udp出来的公网地址可能会变化
  364. if (!Equals(kcpRouter.OuterIpEndPoint, self.IPEndPoint))
  365. {
  366. kcpRouter.OuterIpEndPoint = self.CloneAddress();
  367. }
  368. kcpRouter.LastRecvOuterTime = timeNow;
  369. self.InnerSocket.SendTo(self.Cache, 0, messageLength, SocketFlags.None, kcpRouter.InnerIpEndPoint);
  370. if (!kcpRouter.CheckOuterCount(timeNow))
  371. {
  372. self.OnError(kcpRouter.Id, ErrorCore.ERR_KcpRouterTooManyPackets);
  373. }
  374. break;
  375. }
  376. }
  377. }
  378. private static void RecvInnerHandler(this RouterComponent self, int messageLength, long timeNow)
  379. {
  380. // 长度小于1,不是正常的消息
  381. if (messageLength < 1)
  382. {
  383. return;
  384. }
  385. // accept
  386. byte flag = self.Cache[0];
  387. switch (flag)
  388. {
  389. case KcpProtocalType.RouterReconnectACK:
  390. {
  391. uint innerConn = BitConverter.ToUInt32(self.Cache, 1);
  392. uint outerConn = BitConverter.ToUInt32(self.Cache, 5);
  393. uint connectId = BitConverter.ToUInt32(self.Cache, 9);
  394. if (!self.ConnectIdNodes.TryGetValue(connectId, out RouterNode kcpRouterNode))
  395. {
  396. Log.Warning($"router node error: {innerConn} {connectId}");
  397. break;
  398. }
  399. // 必须校验innerConn,防止伪造
  400. if (innerConn != kcpRouterNode.InnerConn)
  401. {
  402. Log.Warning(
  403. $"router node innerConn error: {innerConn} {kcpRouterNode.InnerConn} {outerConn} {kcpRouterNode.OuterConn} {kcpRouterNode.Status}");
  404. break;
  405. }
  406. // 必须校验outerConn,防止伪造
  407. if (outerConn != kcpRouterNode.OuterConn)
  408. {
  409. Log.Warning(
  410. $"router node outerConn error: {innerConn} {kcpRouterNode.InnerConn} {outerConn} {kcpRouterNode.OuterConn} {kcpRouterNode.Status}");
  411. break;
  412. }
  413. kcpRouterNode.Status = RouterStatus.Msg;
  414. kcpRouterNode.LastRecvInnerTime = timeNow;
  415. // 校验成功才加到outerNodes中, 如果这里有冲突,外网将连接失败,不过几率极小
  416. if (!self.OuterNodes.ContainsKey(outerConn))
  417. {
  418. self.OuterNodes.Add(outerConn, kcpRouterNode);
  419. self.ConnectIdNodes.Remove(connectId);
  420. }
  421. // 转发出去
  422. self.Cache.WriteTo(0, KcpProtocalType.RouterReconnectACK);
  423. self.Cache.WriteTo(1, kcpRouterNode.InnerConn);
  424. self.Cache.WriteTo(5, kcpRouterNode.OuterConn);
  425. Log.Info($"kcp router RouterAck: {outerConn} {innerConn} {kcpRouterNode.SyncIpEndPoint}");
  426. self.OuterSocket.SendTo(self.Cache, 0, 9, SocketFlags.None, kcpRouterNode.SyncIpEndPoint);
  427. break;
  428. }
  429. case KcpProtocalType.ACK:
  430. {
  431. uint innerConn = BitConverter.ToUInt32(self.Cache, 1); // remote
  432. uint outerConn = BitConverter.ToUInt32(self.Cache, 5); // local
  433. if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouterNode))
  434. {
  435. Log.Warning($"kcp router ack not found outer nodes: {outerConn} {innerConn}");
  436. break;
  437. }
  438. kcpRouterNode.Status = RouterStatus.Msg;
  439. kcpRouterNode.InnerConn = innerConn;
  440. kcpRouterNode.LastRecvInnerTime = timeNow;
  441. // 转发出去
  442. Log.Info($"kcp router ack: {outerConn} {innerConn} {kcpRouterNode.OuterIpEndPoint}");
  443. self.OuterSocket.SendTo(self.Cache, 0, messageLength, SocketFlags.None, kcpRouterNode.OuterIpEndPoint);
  444. break;
  445. }
  446. case KcpProtocalType.FIN: // 断开
  447. {
  448. // 长度!=13,不是DisConnect消息
  449. if (messageLength != 13)
  450. {
  451. break;
  452. }
  453. uint innerConn = BitConverter.ToUInt32(self.Cache, 1);
  454. uint outerConn = BitConverter.ToUInt32(self.Cache, 5);
  455. if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouterNode))
  456. {
  457. Log.Warning($"kcp router inner fin not found outer nodes: {outerConn} {innerConn}");
  458. break;
  459. }
  460. // 比对innerConn
  461. if (kcpRouterNode.InnerConn != innerConn)
  462. {
  463. Log.Warning($"router node innerConn error: {innerConn} {outerConn} {kcpRouterNode.Status}");
  464. break;
  465. }
  466. // 重连,这个字段可能为空,需要客户端发送消息上来才能设置
  467. if (kcpRouterNode.OuterIpEndPoint == null)
  468. {
  469. break;
  470. }
  471. kcpRouterNode.LastRecvInnerTime = timeNow;
  472. Log.Info($"kcp router inner fin: {outerConn} {innerConn} {kcpRouterNode.OuterIpEndPoint}");
  473. self.OuterSocket.SendTo(self.Cache, 0, messageLength, SocketFlags.None, kcpRouterNode.OuterIpEndPoint);
  474. break;
  475. }
  476. case KcpProtocalType.MSG:
  477. {
  478. // 长度<9,不是Msg消息
  479. if (messageLength < 9)
  480. {
  481. break;
  482. }
  483. // 处理chanel
  484. uint innerConn = BitConverter.ToUInt32(self.Cache, 1); // remote
  485. uint outerConn = BitConverter.ToUInt32(self.Cache, 5); // local
  486. if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouterNode))
  487. {
  488. Log.Warning($"kcp router inner msg not found outer nodes: {outerConn} {innerConn}");
  489. break;
  490. }
  491. // 比对innerConn
  492. if (kcpRouterNode.InnerConn != innerConn)
  493. {
  494. Log.Warning($"router node innerConn error: {innerConn} {outerConn} {kcpRouterNode.Status}");
  495. break;
  496. }
  497. // 重连,这个字段可能为空,需要客户端发送消息上来才能设置
  498. if (kcpRouterNode.OuterIpEndPoint == null)
  499. {
  500. break;
  501. }
  502. kcpRouterNode.LastRecvInnerTime = timeNow;
  503. self.OuterSocket.SendTo(self.Cache, 0, messageLength, SocketFlags.None, kcpRouterNode.OuterIpEndPoint);
  504. break;
  505. }
  506. }
  507. }
  508. public static RouterNode Get(this RouterComponent self, uint outerConn)
  509. {
  510. RouterNode routerNode = null;
  511. self.OuterNodes.TryGetValue(outerConn, out routerNode);
  512. return routerNode;
  513. }
  514. private static RouterNode New(this RouterComponent self, string innerAddress, uint connectId, uint outerConn, uint innerConn, IPEndPoint syncEndPoint)
  515. {
  516. RouterNode routerNode = self.AddChild<RouterNode>();
  517. routerNode.ConnectId = connectId;
  518. routerNode.OuterConn = outerConn;
  519. routerNode.InnerConn = innerConn;
  520. routerNode.InnerIpEndPoint = NetworkHelper.ToIPEndPoint(innerAddress);
  521. routerNode.SyncIpEndPoint = syncEndPoint;
  522. routerNode.InnerAddress = innerAddress;
  523. routerNode.LastRecvInnerTime = TimeHelper.ClientNow();
  524. self.ConnectIdNodes.Add(connectId, routerNode);
  525. routerNode.Status = RouterStatus.Sync;
  526. Log.Info($"router new: outerConn: {outerConn} innerConn: {innerConn} {syncEndPoint}");
  527. return routerNode;
  528. }
  529. public static void OnError(this RouterComponent self, long id, int error)
  530. {
  531. RouterNode routerNode = self.GetChild<RouterNode>(id);
  532. if (routerNode == null)
  533. {
  534. return;
  535. }
  536. Log.Info($"router node remove: {routerNode.OuterConn} {routerNode.InnerConn} {error}");
  537. self.Remove(id);
  538. }
  539. private static void Remove(this RouterComponent self, long id)
  540. {
  541. RouterNode routerNode = self.GetChild<RouterNode>(id);
  542. if (routerNode == null)
  543. {
  544. return;
  545. }
  546. self.OuterNodes.Remove(routerNode.OuterConn);
  547. RouterNode connectRouterNode;
  548. if (self.ConnectIdNodes.TryGetValue(routerNode.ConnectId, out connectRouterNode))
  549. {
  550. if (connectRouterNode.Id == routerNode.Id)
  551. {
  552. self.ConnectIdNodes.Remove(routerNode.ConnectId);
  553. }
  554. }
  555. Log.Info($"router remove: {routerNode.Id} outerConn: {routerNode.OuterConn} innerConn: {routerNode.InnerConn}");
  556. routerNode.Dispose();
  557. }
  558. }
  559. }