diff --git a/Fika.Core/Networking/FikaPingingClient.cs b/Fika.Core/Networking/FikaPingingClient.cs index cbf1008e..b16a8b8a 100644 --- a/Fika.Core/Networking/FikaPingingClient.cs +++ b/Fika.Core/Networking/FikaPingingClient.cs @@ -27,7 +27,8 @@ public bool Init(string serverId) { NetClient = new(this) { - UnconnectedMessagesEnabled = true + UnconnectedMessagesEnabled = true, + NatPunchEnabled = true }; GetHostRequest body = new(serverId); @@ -35,45 +36,45 @@ public bool Init(string serverId) FikaBackendUtils.IsHostNatPunch = result.NatPunch; - string ip = result.Ips[0]; - string localIp = null; - if (result.Ips.Length > 1) + if(FikaBackendUtils.IsHostNatPunch) { - localIp = result.Ips[1]; - } - int port = result.Port; + NetClient.Start(); + NetClient.NatPunchModule.Init(this); - if (string.IsNullOrEmpty(ip)) - { - _logger.LogError("IP was empty when pinging!"); - return false; - } + string natHost = RequestHandler.Host.Replace("http://", "").Split(':')[0]; + int natPort = 6970; - if (port == default) - { - _logger.LogError("Port was empty when pinging!"); - return false; + NetClient.NatPunchModule.SendNatIntroduceRequest(natHost, natPort, $"client:{serverId}"); } - - remoteEndPoint = new(IPAddress.Parse(ip), port); - if (!string.IsNullOrEmpty(localIp)) + else { - localEndPoint = new(IPAddress.Parse(localIp), port); - } + string ip = result.Ips[0]; + string localIp = null; + if (result.Ips.Length > 1) + { + localIp = result.Ips[1]; + } + int port = result.Port; - if (FikaBackendUtils.IsHostNatPunch) - { - NetClient.NatPunchModule.Init(this); - } + if (string.IsNullOrEmpty(ip)) + { + _logger.LogError("IP was empty when pinging!"); + return false; + } - NetClient.Start(); + if (port == default) + { + _logger.LogError("Port was empty when pinging!"); + return false; + } - if (FikaBackendUtils.IsHostNatPunch) - { - string natHost = RequestHandler.Host.Replace("http://", "").Split(':')[0]; - int natPort = 6970; + remoteEndPoint = new(IPAddress.Parse(ip), port); + if (!string.IsNullOrEmpty(localIp)) + { + localEndPoint = new(IPAddress.Parse(localIp), port); + } - NetClient.NatPunchModule.SendNatIntroduceRequest(natHost, natPort, $"client:{serverId}"); + NetClient.Start(); } return true; @@ -84,7 +85,10 @@ public void PingEndPoint(string message) NetDataWriter writer = new(); writer.Put(message); - NetClient.SendUnconnectedMessage(writer, remoteEndPoint); + if (remoteEndPoint != null) + { + NetClient.SendUnconnectedMessage(writer, remoteEndPoint); + } if (localEndPoint != null) { NetClient.SendUnconnectedMessage(writer, localEndPoint); @@ -108,9 +112,9 @@ public IEnumerator KeepAlive() { while(true) { - ConsoleScreen.Log($"keepalive: {remoteEndPoint}"); PingEndPoint("fika.keepalive"); NetClient.PollEvents(); + NetClient.NatPunchModule.PollEvents(); yield return new WaitForSeconds(1.0f); } @@ -149,7 +153,6 @@ public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketRead FikaBackendUtils.LocalPort = NetClient.LocalPort; break; case "fika.keepalive": - ConsoleScreen.Log("fika.keepalive"); break; default: _logger.LogError("Data was not as expected"); @@ -184,20 +187,18 @@ public void OnNatIntroductionSuccess(IPEndPoint targetEndPoint, NatAddressType t public void OnNatIntroductionResponse(IPEndPoint natLocalEndPoint, IPEndPoint natRemoteEndPoint, string token) { - ConsoleScreen.Log($"OnNatIntroductionResponse: {remoteEndPoint}"); + localEndPoint = natLocalEndPoint; + remoteEndPoint = natRemoteEndPoint; NetDataWriter data = new(); data.Put("fika.hello"); for (int i = 0; i < 10; i++) { - NetClient.SendUnconnectedMessage(data, localEndPoint); - NetClient.SendUnconnectedMessage(data, remoteEndPoint); + NetClient.SendUnconnectedMessage(data, natLocalEndPoint); + NetClient.SendUnconnectedMessage(data, natRemoteEndPoint); + Task.Delay(100); } - - remoteEndPoint = natRemoteEndPoint; - - StartKeepAliveRoutine(); } } } diff --git a/Fika.Core/Networking/FikaServer.cs b/Fika.Core/Networking/FikaServer.cs index 6b2d89eb..10922c94 100644 --- a/Fika.Core/Networking/FikaServer.cs +++ b/Fika.Core/Networking/FikaServer.cs @@ -73,6 +73,7 @@ public bool Started } } private FikaChat fikaChat; + private CancellationTokenSource natIntroduceRoutineCts; public async Task Init() { @@ -163,12 +164,14 @@ public async Task Init() _netServer.NatPunchModule.Init(this); _netServer.Start(); + natIntroduceRoutineCts = new CancellationTokenSource(); + string host = RequestHandler.Host.Replace("http://", "").Split(':')[0]; int port = 6970; - ConsoleScreen.Log($"send nat introduce request to {host}:{port}"); + IPEndPoint natServerIPEndPoint = new IPEndPoint(IPAddress.Parse(host), port); - _netServer.NatPunchModule.SendNatIntroduceRequest(host, port, $"server:{RequestHandler.SessionId}"); + Task natIntroduceTask = Task.Run(() => NatIntroduceRoutine(natServerIPEndPoint, natIntroduceRoutineCts.Token)); } else { @@ -210,6 +213,22 @@ public async Task Init() FikaEventDispatcher.DispatchEvent(new FikaServerCreatedEvent(this)); } + private async void NatIntroduceRoutine(IPEndPoint natServerIPEndPoint, CancellationToken ct) + { + ConsoleScreen.Log($"begin NatIntroduceRoutine"); + + while (!ct.IsCancellationRequested) + { + ConsoleScreen.Log($"send nat introduce request to {natServerIPEndPoint}"); + + _netServer.NatPunchModule.SendNatIntroduceRequest(natServerIPEndPoint, $"server:{RequestHandler.SessionId}"); + + await Task.Delay(TimeSpan.FromSeconds(15)); + } + + ConsoleScreen.Log($"end NatIntroduceRoutine"); + } + private void OnQuestItemPacketReceived(QuestItemPacket packet, NetPeer peer) { _dataWriter.Reset(); @@ -794,7 +813,11 @@ public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketRead resp = new(); resp.Put(data); _netServer.SendUnconnectedMessage(resp, remoteEndPoint); - ConsoleScreen.Log("fika.keepalive"); + + if(!natIntroduceRoutineCts.IsCancellationRequested) + { + natIntroduceRoutineCts.Cancel(); + } break; default: @@ -850,8 +873,6 @@ public void OnNatIntroductionSuccess(IPEndPoint targetEndPoint, NatAddressType t public void OnNatIntroductionResponse(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, string token) { - ConsoleScreen.Log($"OnNatIntroductionResponse: {remoteEndPoint}"); - NetDataWriter data = new(); data.Put("fika.hello"); @@ -859,6 +880,8 @@ public void OnNatIntroductionResponse(IPEndPoint localEndPoint, IPEndPoint remot { _netServer.SendUnconnectedMessage(data, localEndPoint); _netServer.SendUnconnectedMessage(data, remoteEndPoint); + + Task.Delay(100); } } diff --git a/Fika.Core/Networking/LiteNetLib/NatPunchModule.cs b/Fika.Core/Networking/LiteNetLib/NatPunchModule.cs index 4808a19f..e153af8d 100644 --- a/Fika.Core/Networking/LiteNetLib/NatPunchModule.cs +++ b/Fika.Core/Networking/LiteNetLib/NatPunchModule.cs @@ -240,14 +240,12 @@ private void OnNatIntroductionRequest(NatIntroduceRequestPacket req, IPEndPoint //We got introduce and must punch private void OnNatIntroductionResponse(NatIntroduceResponsePacket req) { - Console.WriteLine("[NAT] introduction received"); NetDebug.Write(NetLogLevel.Trace, "[NAT] introduction received"); // send internal punch var punchPacket = new NatPunchPacket { Token = req.Token }; Send(punchPacket, req.Internal); NetDebug.Write(NetLogLevel.Trace, $"[NAT] internal punch sent to {req.Internal}"); - Console.WriteLine($"[NAT] internal punch sent to {req.Internal}"); // hack for some routers _socket.Ttl = 2; @@ -258,7 +256,6 @@ private void OnNatIntroductionResponse(NatIntroduceResponsePacket req) punchPacket.IsExternal = true; Send(punchPacket, req.External); NetDebug.Write(NetLogLevel.Trace, $"[NAT] external punch sent to {req.External}"); - Console.WriteLine($"[NAT] external punch sent to {req.External}"); if (UnsyncedEvents) { diff --git a/Fika.Core/UI/Custom/MatchMakerUIScript.cs b/Fika.Core/UI/Custom/MatchMakerUIScript.cs index 0ae951c0..39767013 100644 --- a/Fika.Core/UI/Custom/MatchMakerUIScript.cs +++ b/Fika.Core/UI/Custom/MatchMakerUIScript.cs @@ -258,7 +258,11 @@ private IEnumerator JoinMatch(string profileId, string serverId, Button button) FikaBackendUtils.MatchingType = EMatchmakerType.GroupPlayer; FikaBackendUtils.HostExpectedNumberOfPlayers = result.ExpectedNumberOfPlayers; - if (!FikaBackendUtils.IsHostNatPunch) + if (FikaBackendUtils.IsHostNatPunch) + { + pingingClient.StartKeepAliveRoutine(); + } + else { NetManagerUtils.DestroyPingingClient(); }