using System.Net; using System.Net.Sockets; using VeloBrawl.General.Cloud; using VeloBrawl.General.Networking.LaserUdp; using VeloBrawl.Titan.DataStream; using VeloBrawl.Titan.Graphic; namespace VeloBrawl.General.Network; public class UdpLaserSocketListener(int port) { public readonly Dictionary? Sessions = new(); private Thread? _thread; public Socket? UdpAdministrator; public void StartSocket() { UdpAdministrator = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); UdpAdministrator!.Bind(new IPEndPoint(IPAddress.Any, port)); ConsoleLogger.WriteTextWithPrefix(ConsoleLogger.Prefixes.Start, $"Network-server started! Information: " + $"server protocol type: {UdpAdministrator.ProtocolType}; " + $"server port: {port}."); _thread = new Thread(Receive); _thread.Start(); } [Obsolete("Obsolete")] public void StopSocket() { _thread!.Abort(); UdpAdministrator!.Close(); ConsoleLogger.WriteTextWithPrefix(ConsoleLogger.Prefixes.Stop, $"Network-server stopped! Information: " + $"server protocol type: {UdpAdministrator.ProtocolType}; " + $"server port: {port}."); } private void Receive() { while (true) { var buffer = new byte[1024]; EndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 0); var bytesRead = UdpAdministrator!.ReceiveFrom(buffer, ref ipEndPoint); var byteStream = new ByteStream(buffer, bytesRead); try { long sessionId = byteStream.ReadLong(); byteStream.ReadShort(); if (!InteractiveModule.LogicBattleModeServersMassive!.ContainsKey(sessionId)) return; Sessions!.TryAdd(sessionId, (IPEndPoint)ipEndPoint); Task.Run(() => { new UdpPacketHandler(sessionId, byteStream).Receive(); }); } catch { // ignored. } } } }