fishbait/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program/ProgramNATHandler.cs
2021-04-05 23:18:44 -05:00

50 lines
1.6 KiB
C#

using System;
using System.Net;
namespace LightReflectiveMirror
{
partial class Program
{
void RunNATPunchLoop()
{
WriteLogMessage("OK\n", ConsoleColor.Green);
IPEndPoint remoteEndpoint = new(IPAddress.Any, conf.NATPunchtroughPort);
// Stock Data server sends to everyone:
var serverResponse = new byte[1] { 1 };
byte[] readData;
bool isConnectionEstablishment;
int pos;
string connectionID;
while (true)
{
readData = _punchServer.Receive(ref remoteEndpoint);
pos = 0;
try
{
isConnectionEstablishment = readData.ReadBool(ref pos);
if (isConnectionEstablishment)
{
connectionID = readData.ReadString(ref pos);
if (_pendingNATPunches.TryGetBySecond(connectionID, out pos))
{
NATConnections.Add(pos, new IPEndPoint(remoteEndpoint.Address, remoteEndpoint.Port));
_pendingNATPunches.Remove(pos);
Console.WriteLine("Client Successfully Established Puncher Connection. " + remoteEndpoint.ToString());
}
}
_punchServer.Send(serverResponse, 1, remoteEndpoint);
}
catch
{
// ignore, packet got fucked up or something.
}
}
}
}
}