123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661 |
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Runtime.InteropServices;
- namespace ET.Server
- {
- [FriendOf(typeof (RouterComponent))]
- [FriendOf(typeof (RouterNode))]
- public static class RouterComponentSystem
- {
- [ObjectSystem]
- public class RandomGeneratorponentAwakeSystem: AwakeSystem<RouterComponent, IPEndPoint, string>
- {
- protected override void Awake(RouterComponent self, IPEndPoint ipEndPoint, string innerIP)
- {
- self.OuterSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
- self.OuterSocket.Bind(new IPEndPoint(IPAddress.Any, ipEndPoint.Port));
- if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- {
- self.OuterSocket.SendBufferSize = 16 * Kcp.OneM;
- self.OuterSocket.ReceiveBufferSize = 16 * Kcp.OneM;
- }
- self.InnerSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
- self.InnerSocket.Bind(new IPEndPoint(IPAddress.Any, 0));
- if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- {
- self.InnerSocket.SendBufferSize = 16 * Kcp.OneM;
- self.InnerSocket.ReceiveBufferSize = 16 * Kcp.OneM;
- }
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- const uint IOC_IN = 0x80000000;
- const uint IOC_VENDOR = 0x18000000;
- uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
- self.OuterSocket.IOControl((int) SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null);
- self.InnerSocket.IOControl((int) SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null);
- }
- }
- }
- [ObjectSystem]
- public class RouterComponentDestroySystem: DestroySystem<RouterComponent>
- {
- protected override void Destroy(RouterComponent self)
- {
- self.OuterSocket.Dispose();
- self.InnerSocket.Dispose();
- self.OuterNodes.Clear();
- self.IPEndPoint = null;
- }
- }
- [ObjectSystem]
- public class RouterComponentUpdateSystem: UpdateSystem<RouterComponent>
- {
- protected override void Update(RouterComponent self)
- {
- long timeNow = TimeHelper.ClientNow();
- self.RecvOuter(timeNow);
- self.RecvInner(timeNow);
-
- if (timeNow - self.LastCheckTime > 1000)
- {
- self.CheckConnectTimeout(timeNow);
- self.LastCheckTime = timeNow;
- }
- }
- }
- private static IPEndPoint CloneAddress(this RouterComponent self)
- {
- IPEndPoint ipEndPoint = (IPEndPoint) self.IPEndPoint;
- return new IPEndPoint(ipEndPoint.Address, ipEndPoint.Port);
- }
-
- private static void RecvOuter(this RouterComponent self, long timeNow)
- {
- while (self.OuterSocket != null && self.OuterSocket.Available > 0)
- {
- try
- {
- int messageLength = self.OuterSocket.ReceiveFrom(self.Cache, ref self.IPEndPoint);
- self.RecvOuterHandler(messageLength, timeNow);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- private static void CheckConnectTimeout(this RouterComponent self, long timeNow)
- {
-
- using (ListComponent<long> listComponent = ListComponent<long>.Create())
- {
- foreach (var kv in self.ConnectIdNodes)
- {
- if (timeNow < kv.Value.LastRecvOuterTime + 10 * 1000)
- {
- continue;
- }
- listComponent.Add(kv.Value.Id);
- }
- foreach (long id in listComponent)
- {
- self.OnError(id, ErrorCore.ERR_KcpRouterConnectFail);
- }
- }
-
- using (ListComponent<long> listComponent = ListComponent<long>.Create())
- {
- foreach (var kv in self.OuterNodes)
- {
-
- if (timeNow < kv.Value.LastRecvOuterTime + ConstValue.SessionTimeoutTime + 10 * 1000)
- {
- continue;
- }
- listComponent.Add(kv.Value.Id);
- }
- foreach (long id in listComponent)
- {
- self.OnError(id, ErrorCore.ERR_KcpRouterTimeout);
- }
- }
- }
- private static void RecvInner(this RouterComponent self, long timeNow)
- {
- while (self.InnerSocket != null && self.InnerSocket.Available > 0)
- {
- try
- {
- int messageLength = self.InnerSocket.ReceiveFrom(self.Cache, ref self.IPEndPoint);
- self.RecvInnerHandler(messageLength, timeNow);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- private static void RecvOuterHandler(this RouterComponent self, int messageLength, long timeNow)
- {
-
- if (messageLength < 1)
- {
- return;
- }
-
- byte flag = self.Cache[0];
- switch (flag)
- {
- case KcpProtocalType.RouterReconnectSYN:
- {
- if (messageLength < 13)
- {
- break;
- }
- uint outerConn = BitConverter.ToUInt32(self.Cache, 1);
- uint innerConn = BitConverter.ToUInt32(self.Cache, 5);
- uint connectId = BitConverter.ToUInt32(self.Cache, 9);
- string realAddress = self.Cache.ToStr(13, messageLength - 13);
- RouterNode routerNode;
-
- if (!self.OuterNodes.TryGetValue(outerConn, out routerNode))
- {
- self.ConnectIdNodes.TryGetValue(connectId, out routerNode);
- if (routerNode == null)
- {
- Log.Info($"router create reconnect: {self.IPEndPoint} {realAddress} {connectId} {outerConn} {innerConn}");
- routerNode = self.New(realAddress, connectId, outerConn, innerConn, self.CloneAddress());
-
- }
- }
- if (routerNode.ConnectId != connectId)
- {
- Log.Warning($"kcp router router reconnect connectId diff1: {routerNode.SyncIpEndPoint} {(IPEndPoint) self.IPEndPoint}");
- break;
- }
-
-
- if (routerNode.InnerConn != innerConn)
- {
- Log.Warning($"kcp router router reconnect inner conn diff1: {routerNode.SyncIpEndPoint} {(IPEndPoint) self.IPEndPoint}");
- break;
- }
- if (routerNode.OuterConn != outerConn)
- {
- Log.Warning($"kcp router router reconnect outer conn diff1: {routerNode.SyncIpEndPoint} {(IPEndPoint) self.IPEndPoint}");
- break;
- }
-
- if (!Equals(routerNode.SyncIpEndPoint, self.IPEndPoint))
- {
- Log.Warning($"kcp router syn ip is diff1: {routerNode.SyncIpEndPoint} {(IPEndPoint) self.IPEndPoint}");
- break;
- }
-
- if (routerNode.InnerAddress != realAddress)
- {
- Log.Warning($"router sync error2: {routerNode.OuterConn} {routerNode.InnerAddress} {outerConn} {realAddress}");
- break;
- }
- if (++routerNode.RouterSyncCount > 40)
- {
- self.OnError(routerNode.Id, ErrorCore.ERR_KcpRouterRouterSyncCountTooMuchTimes);
- break;
- }
-
- self.Cache.WriteTo(0, KcpProtocalType.RouterReconnectSYN);
- self.Cache.WriteTo(1, outerConn);
- self.Cache.WriteTo(5, innerConn);
- self.Cache.WriteTo(9, connectId);
- self.InnerSocket.SendTo(self.Cache, 0, 13, SocketFlags.None, routerNode.InnerIpEndPoint);
- if (!routerNode.CheckOuterCount(timeNow))
- {
- self.OnError(routerNode.Id, ErrorCore.ERR_KcpRouterTooManyPackets);
- }
- break;
- }
- case KcpProtocalType.RouterSYN:
- {
- if (messageLength < 13)
- {
- break;
- }
- uint outerConn = BitConverter.ToUInt32(self.Cache, 1);
- uint innerConn = BitConverter.ToUInt32(self.Cache, 5);
- uint connectId = BitConverter.ToUInt32(self.Cache, 9);
- string realAddress = self.Cache.ToStr(13, messageLength - 13);
- RouterNode routerNode;
- self.ConnectIdNodes.TryGetValue(connectId, out routerNode);
- if (routerNode == null)
- {
- 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);
- }
- if (++routerNode.RouterSyncCount > 40)
- {
- self.OnError(routerNode.Id, ErrorCore.ERR_KcpRouterRouterSyncCountTooMuchTimes);
- break;
- }
-
- if (!Equals(routerNode.SyncIpEndPoint, self.IPEndPoint))
- {
- Log.Warning($"kcp router syn ip is diff1: {routerNode.SyncIpEndPoint} {self.IPEndPoint}");
- break;
- }
-
- if (routerNode.InnerAddress != realAddress)
- {
- Log.Warning($"router sync error2: {routerNode.OuterConn} {routerNode.InnerAddress} {outerConn} {realAddress}");
- break;
- }
- self.Cache.WriteTo(0, KcpProtocalType.RouterACK);
- self.Cache.WriteTo(1, routerNode.InnerConn);
- self.Cache.WriteTo(5, routerNode.OuterConn);
- self.OuterSocket.SendTo(self.Cache, 0, 9, SocketFlags.None, routerNode.SyncIpEndPoint);
- if (!routerNode.CheckOuterCount(timeNow))
- {
- self.OnError(routerNode.Id, ErrorCore.ERR_KcpRouterTooManyPackets);
- }
- break;
- }
- case KcpProtocalType.SYN:
- {
-
- if (messageLength != 9)
- {
- break;
- }
- uint outerConn = BitConverter.ToUInt32(self.Cache, 1);
- uint innerConn = BitConverter.ToUInt32(self.Cache, 5);
- if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouter))
- {
- Log.Warning($"kcp router syn not found outer nodes: {outerConn} {innerConn}");
- break;
- }
- if (++kcpRouter.SyncCount > 20)
- {
- self.OnError(kcpRouter.Id, ErrorCore.ERR_KcpRouterSyncCountTooMuchTimes);
- break;
- }
-
- IPEndPoint ipEndPoint = (IPEndPoint) self.IPEndPoint;
- if (!Equals(kcpRouter.SyncIpEndPoint.Address, ipEndPoint.Address))
- {
- Log.Warning($"kcp router syn ip is diff3: {kcpRouter.SyncIpEndPoint.Address} {ipEndPoint.Address}");
- break;
- }
-
- self.ConnectIdNodes.Remove(kcpRouter.ConnectId);
- kcpRouter.LastRecvOuterTime = timeNow;
- kcpRouter.OuterIpEndPoint = self.CloneAddress();
-
- self.Cache.WriteTo(0, KcpProtocalType.SYN);
- self.Cache.WriteTo(1, outerConn);
- self.Cache.WriteTo(5, innerConn);
- byte[] addressBytes = ipEndPoint.ToString().ToByteArray();
- Array.Copy(addressBytes, 0, self.Cache, 9, addressBytes.Length);
- Log.Info($"kcp router syn: {outerConn} {innerConn} {kcpRouter.InnerIpEndPoint} {kcpRouter.OuterIpEndPoint}");
- self.InnerSocket.SendTo(self.Cache, 0, 9 + addressBytes.Length, SocketFlags.None, kcpRouter.InnerIpEndPoint);
- if (!kcpRouter.CheckOuterCount(timeNow))
- {
- self.OnError(kcpRouter.Id, ErrorCore.ERR_KcpRouterTooManyPackets);
- }
- break;
- }
- case KcpProtocalType.FIN:
- {
-
- if (messageLength != 13)
- {
- break;
- }
- uint outerConn = BitConverter.ToUInt32(self.Cache, 1);
- uint innerConn = BitConverter.ToUInt32(self.Cache, 5);
- if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouter))
- {
- Log.Warning($"kcp router outer fin not found outer nodes: {outerConn} {innerConn}");
- break;
- }
-
- if (kcpRouter.InnerConn != innerConn)
- {
- Log.Warning($"router node innerConn error: {innerConn} {outerConn} {kcpRouter.Status}");
- break;
- }
- kcpRouter.LastRecvOuterTime = timeNow;
- Log.Info($"kcp router outer fin: {outerConn} {innerConn} {kcpRouter.InnerIpEndPoint}");
- self.InnerSocket.SendTo(self.Cache, 0, messageLength, SocketFlags.None, kcpRouter.InnerIpEndPoint);
- if (!kcpRouter.CheckOuterCount(timeNow))
- {
- self.OnError(kcpRouter.Id, ErrorCore.ERR_KcpRouterTooManyPackets);
- }
- break;
- }
- case KcpProtocalType.MSG:
- {
-
- if (messageLength < 9)
- {
- break;
- }
-
- uint outerConn = BitConverter.ToUInt32(self.Cache, 1);
- uint innerConn = BitConverter.ToUInt32(self.Cache, 5);
- if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouter))
- {
- Log.Warning($"kcp router msg not found outer nodes: {outerConn} {innerConn}");
- break;
- }
- if (kcpRouter.Status != RouterStatus.Msg)
- {
- Log.Warning($"router node status error: {innerConn} {outerConn} {kcpRouter.Status}");
- break;
- }
-
- if (kcpRouter.InnerConn != innerConn)
- {
- Log.Warning($"router node innerConn error: {innerConn} {outerConn} {kcpRouter.Status}");
- break;
- }
-
- if (!Equals(kcpRouter.OuterIpEndPoint, self.IPEndPoint))
- {
- kcpRouter.OuterIpEndPoint = self.CloneAddress();
- }
- kcpRouter.LastRecvOuterTime = timeNow;
- self.InnerSocket.SendTo(self.Cache, 0, messageLength, SocketFlags.None, kcpRouter.InnerIpEndPoint);
- if (!kcpRouter.CheckOuterCount(timeNow))
- {
- self.OnError(kcpRouter.Id, ErrorCore.ERR_KcpRouterTooManyPackets);
- }
- break;
- }
- }
- }
- private static void RecvInnerHandler(this RouterComponent self, int messageLength, long timeNow)
- {
-
- if (messageLength < 1)
- {
- return;
- }
-
- byte flag = self.Cache[0];
- switch (flag)
- {
- case KcpProtocalType.RouterReconnectACK:
- {
- uint innerConn = BitConverter.ToUInt32(self.Cache, 1);
- uint outerConn = BitConverter.ToUInt32(self.Cache, 5);
- uint connectId = BitConverter.ToUInt32(self.Cache, 9);
- if (!self.ConnectIdNodes.TryGetValue(connectId, out RouterNode kcpRouterNode))
- {
- Log.Warning($"router node error: {innerConn} {connectId}");
- break;
- }
-
- if (innerConn != kcpRouterNode.InnerConn)
- {
- Log.Warning(
- $"router node innerConn error: {innerConn} {kcpRouterNode.InnerConn} {outerConn} {kcpRouterNode.OuterConn} {kcpRouterNode.Status}");
- break;
- }
-
- if (outerConn != kcpRouterNode.OuterConn)
- {
- Log.Warning(
- $"router node outerConn error: {innerConn} {kcpRouterNode.InnerConn} {outerConn} {kcpRouterNode.OuterConn} {kcpRouterNode.Status}");
- break;
- }
- kcpRouterNode.Status = RouterStatus.Msg;
- kcpRouterNode.LastRecvInnerTime = timeNow;
-
- if (!self.OuterNodes.ContainsKey(outerConn))
- {
- self.OuterNodes.Add(outerConn, kcpRouterNode);
- self.ConnectIdNodes.Remove(connectId);
- }
-
- self.Cache.WriteTo(0, KcpProtocalType.RouterReconnectACK);
- self.Cache.WriteTo(1, kcpRouterNode.InnerConn);
- self.Cache.WriteTo(5, kcpRouterNode.OuterConn);
- Log.Info($"kcp router RouterAck: {outerConn} {innerConn} {kcpRouterNode.SyncIpEndPoint}");
- self.OuterSocket.SendTo(self.Cache, 0, 9, SocketFlags.None, kcpRouterNode.SyncIpEndPoint);
- break;
- }
- case KcpProtocalType.ACK:
- {
- uint innerConn = BitConverter.ToUInt32(self.Cache, 1);
- uint outerConn = BitConverter.ToUInt32(self.Cache, 5);
- if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouterNode))
- {
- Log.Warning($"kcp router ack not found outer nodes: {outerConn} {innerConn}");
- break;
- }
- kcpRouterNode.Status = RouterStatus.Msg;
- kcpRouterNode.InnerConn = innerConn;
- kcpRouterNode.LastRecvInnerTime = timeNow;
-
- Log.Info($"kcp router ack: {outerConn} {innerConn} {kcpRouterNode.OuterIpEndPoint}");
- self.OuterSocket.SendTo(self.Cache, 0, messageLength, SocketFlags.None, kcpRouterNode.OuterIpEndPoint);
- break;
- }
- case KcpProtocalType.FIN:
- {
-
- if (messageLength != 13)
- {
- break;
- }
- uint innerConn = BitConverter.ToUInt32(self.Cache, 1);
- uint outerConn = BitConverter.ToUInt32(self.Cache, 5);
- if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouterNode))
- {
- Log.Warning($"kcp router inner fin not found outer nodes: {outerConn} {innerConn}");
- break;
- }
-
- if (kcpRouterNode.InnerConn != innerConn)
- {
- Log.Warning($"router node innerConn error: {innerConn} {outerConn} {kcpRouterNode.Status}");
- break;
- }
-
- if (kcpRouterNode.OuterIpEndPoint == null)
- {
- break;
- }
- kcpRouterNode.LastRecvInnerTime = timeNow;
- Log.Info($"kcp router inner fin: {outerConn} {innerConn} {kcpRouterNode.OuterIpEndPoint}");
- self.OuterSocket.SendTo(self.Cache, 0, messageLength, SocketFlags.None, kcpRouterNode.OuterIpEndPoint);
- break;
- }
- case KcpProtocalType.MSG:
- {
-
- if (messageLength < 9)
- {
- break;
- }
-
- uint innerConn = BitConverter.ToUInt32(self.Cache, 1);
- uint outerConn = BitConverter.ToUInt32(self.Cache, 5);
- if (!self.OuterNodes.TryGetValue(outerConn, out RouterNode kcpRouterNode))
- {
- Log.Warning($"kcp router inner msg not found outer nodes: {outerConn} {innerConn}");
- break;
- }
-
- if (kcpRouterNode.InnerConn != innerConn)
- {
- Log.Warning($"router node innerConn error: {innerConn} {outerConn} {kcpRouterNode.Status}");
- break;
- }
-
- if (kcpRouterNode.OuterIpEndPoint == null)
- {
- break;
- }
- kcpRouterNode.LastRecvInnerTime = timeNow;
- self.OuterSocket.SendTo(self.Cache, 0, messageLength, SocketFlags.None, kcpRouterNode.OuterIpEndPoint);
- break;
- }
- }
- }
- public static RouterNode Get(this RouterComponent self, uint outerConn)
- {
- RouterNode routerNode = null;
- self.OuterNodes.TryGetValue(outerConn, out routerNode);
- return routerNode;
- }
- private static RouterNode New(this RouterComponent self, string innerAddress, uint connectId, uint outerConn, uint innerConn, IPEndPoint syncEndPoint)
- {
- RouterNode routerNode = self.AddChild<RouterNode>();
- routerNode.ConnectId = connectId;
- routerNode.OuterConn = outerConn;
- routerNode.InnerConn = innerConn;
- routerNode.InnerIpEndPoint = NetworkHelper.ToIPEndPoint(innerAddress);
- routerNode.SyncIpEndPoint = syncEndPoint;
- routerNode.InnerAddress = innerAddress;
- routerNode.LastRecvInnerTime = TimeHelper.ClientNow();
- self.ConnectIdNodes.Add(connectId, routerNode);
- routerNode.Status = RouterStatus.Sync;
- Log.Info($"router new: outerConn: {outerConn} innerConn: {innerConn} {syncEndPoint}");
- return routerNode;
- }
- public static void OnError(this RouterComponent self, long id, int error)
- {
- RouterNode routerNode = self.GetChild<RouterNode>(id);
- if (routerNode == null)
- {
- return;
- }
- Log.Info($"router node remove: {routerNode.OuterConn} {routerNode.InnerConn} {error}");
- self.Remove(id);
- }
- private static void Remove(this RouterComponent self, long id)
- {
- RouterNode routerNode = self.GetChild<RouterNode>(id);
- if (routerNode == null)
- {
- return;
- }
- self.OuterNodes.Remove(routerNode.OuterConn);
- RouterNode connectRouterNode;
- if (self.ConnectIdNodes.TryGetValue(routerNode.ConnectId, out connectRouterNode))
- {
- if (connectRouterNode.Id == routerNode.Id)
- {
- self.ConnectIdNodes.Remove(routerNode.ConnectId);
- }
- }
- Log.Info($"router remove: {routerNode.Id} outerConn: {routerNode.OuterConn} innerConn: {routerNode.InnerConn}");
- routerNode.Dispose();
- }
- }
- }
|