RouterComponentSystem.cs 29 KB

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