84 lines
3.4 KiB
C#
Raw Normal View History

2022-09-06 21:46:35 +08:00
using Lidgren.Network;
using RageCoop.Core;
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-08-10 20:42:47 +08:00
namespace RageCoop.Client
{
internal static partial class HolePunch
{
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
{
if (!Networking.IsOnServer) { return; }
foreach (var p in PlayerList.Players.Values.ToArray())
{
2022-09-06 21:46:35 +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
{
2022-08-27 14:17:10 +08:00
Main.Logger.Trace($"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);
Networking.Peer.SendUnconnectedMessage(msg, new List<IPEndPoint> { p.InternalEndPoint, p.ExternalEndPoint });
}
}
}
catch (Exception ex)
{
Main.Logger.Error(ex);
}
}
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
{
Main.Logger.Debug($"{p.TargetID},{player.Username} added to HolePunch target");
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
{
2022-09-06 21:46:35 +08:00
Main.Logger.Warning("No player with specified TargetID found for hole punching:" + p.TargetID);
2022-08-10 20:42:47 +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
{
Main.Logger.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
{
2022-09-06 21:46:35 +08:00
Main.Logger.Debug("Puncher identified as: " + puncher.Username);
puncher.HolePunchStatus = (byte)(p.Status + 1);
if (p.Status >= 3)
2022-08-10 20:42:47 +08:00
{
2022-09-06 21:46:35 +08:00
Main.Logger.Debug("HolePunch sucess: " + from + ", " + puncher.ID);
if (puncher.ConnectWhenPunched && (puncher.Connection == null || puncher.Connection.Status == NetConnectionStatus.Disconnected))
2022-08-10 20:42:47 +08:00
{
2022-09-06 21:46:35 +08:00
Main.Logger.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();
}
}
}
}
}
}