Files
RAGECOOP-V/Client/Scripts/Networking/HolePunch.cs

86 lines
3.4 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
2022-08-10 20:42:47 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Net;
2022-09-06 21:46:35 +08:00
using System.Timers;
2022-10-23 19:02:39 +08:00
using Lidgren.Network;
using RageCoop.Core;
2022-08-10 20:42:47 +08:00
namespace RageCoop.Client
{
2022-10-23 19:02:39 +08:00
internal static class HolePunch
2022-08-10 20:42:47 +08:00
{
static HolePunch()
{
// Periodically send hole punch message as needed
2022-09-06 21:46:35 +08:00
var timer = new Timer(1000);
timer.Elapsed += DoPunch;
timer.Enabled = true;
2022-08-10 20:42:47 +08:00
}
private static void DoPunch(object sender, ElapsedEventArgs e)
{
try
{
2022-10-23 19:02:39 +08:00
if (!Networking.IsOnServer) return;
2022-08-10 20:42:47 +08:00
foreach (var p in PlayerList.Players.Values.ToArray())
2022-10-23 19:02:39 +08:00
if (p.InternalEndPoint != null && p.ExternalEndPoint != null && (p.Connection == null ||
p.Connection.Status == NetConnectionStatus.Disconnected))
2022-08-10 20:42:47 +08:00
{
2023-02-13 17:51:18 +08:00
Log.Trace(
2022-10-23 19:02:39 +08:00
$"Sending HolePunch message to {p.InternalEndPoint},{p.ExternalEndPoint}. {p.Username}:{p.ID}");
2022-08-10 20:42:47 +08:00
var msg = Networking.Peer.CreateMessage();
new Packets.HolePunch
{
2022-09-06 21:46:35 +08:00
Puncher = Main.LocalPlayerID,
Status = p.HolePunchStatus
2022-08-10 20:42:47 +08:00
}.Pack(msg);
2022-10-23 19:02:39 +08:00
Networking.Peer.SendUnconnectedMessage(msg,
new List<IPEndPoint> { p.InternalEndPoint, p.ExternalEndPoint });
2022-08-10 20:42:47 +08:00
}
}
catch (Exception ex)
{
2023-02-13 17:51:18 +08:00
Log.Error(ex);
2022-08-10 20:42:47 +08:00
}
}
public static void Add(Packets.HolePunchInit p)
{
2022-09-06 21:46:35 +08:00
if (PlayerList.Players.TryGetValue(p.TargetID, out var player))
2022-08-10 20:42:47 +08:00
{
2023-02-13 17:51:18 +08:00
Log.Debug($"{p.TargetID},{player.Username} added to HolePunch target");
2022-08-10 20:42:47 +08:00
player.InternalEndPoint = CoreUtils.StringToEndPoint(p.TargetInternal);
player.ExternalEndPoint = CoreUtils.StringToEndPoint(p.TargetExternal);
2022-09-06 21:46:35 +08:00
player.ConnectWhenPunched = p.Connect;
2022-08-10 20:42:47 +08:00
}
else
{
2023-02-13 17:51:18 +08:00
Log.Warning("No player with specified TargetID found for hole punching:" + p.TargetID);
2022-08-10 20:42:47 +08:00
}
}
2022-10-23 19:02:39 +08:00
2022-09-06 21:46:35 +08:00
public static void Punched(Packets.HolePunch p, IPEndPoint from)
2022-08-10 20:42:47 +08:00
{
2023-02-13 17:51:18 +08:00
Log.Debug($"HolePunch message received from:{from}, status:{p.Status}");
2022-09-06 21:46:35 +08:00
if (PlayerList.Players.TryGetValue(p.Puncher, out var puncher))
2022-08-10 20:42:47 +08:00
{
2023-02-13 17:51:18 +08:00
Log.Debug("Puncher identified as: " + puncher.Username);
2022-09-06 21:46:35 +08:00
puncher.HolePunchStatus = (byte)(p.Status + 1);
if (p.Status >= 3)
2022-08-10 20:42:47 +08:00
{
2023-02-13 17:51:18 +08:00
Log.Debug("HolePunch sucess: " + from + ", " + puncher.ID);
2022-10-23 19:02:39 +08:00
if (puncher.ConnectWhenPunched && (puncher.Connection == null ||
puncher.Connection.Status == NetConnectionStatus.Disconnected))
2022-08-10 20:42:47 +08:00
{
2023-02-13 17:51:18 +08:00
Log.Debug("Connecting to peer: " + from);
2022-08-10 20:42:47 +08:00
var msg = Networking.Peer.CreateMessage();
2022-09-06 21:46:35 +08:00
new Packets.P2PConnect { ID = Main.LocalPlayerID }.Pack(msg);
puncher.Connection = Networking.Peer.Connect(from, msg);
2022-08-10 20:42:47 +08:00
Networking.Peer.FlushSendQueue();
}
}
}
}
}
2022-10-23 19:02:39 +08:00
}