From b4e6433545a02ab3b43214ecca67e6412942a27c Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 16 May 2024 12:02:48 +0200 Subject: [PATCH] Fix deprecation and nullability warnings. --- src/tool/Syroot.Worms.Mgame.GameServer/Config.cs | 6 ++++-- src/tool/Syroot.Worms.Worms2.GameServer/PacketConnection.cs | 3 ++- src/tool/Syroot.Worms.Worms2.GameServer/Program.cs | 6 ++++-- src/tool/Syroot.Worms.Worms2.GameServer/Server.cs | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/tool/Syroot.Worms.Mgame.GameServer/Config.cs b/src/tool/Syroot.Worms.Mgame.GameServer/Config.cs index b9f392e..dc3bbcb 100644 --- a/src/tool/Syroot.Worms.Mgame.GameServer/Config.cs +++ b/src/tool/Syroot.Worms.Mgame.GameServer/Config.cs @@ -1,4 +1,5 @@ using System.Net; +using System.Net.Http; namespace Syroot.Worms.Mgame.GameServer { @@ -33,8 +34,9 @@ namespace Syroot.Worms.Mgame.GameServer // Retrieve external IP if not yet done and given IP is invalid. if (_ipAddress == null && (IP == null || !IPAddress.TryParse(IP, out _ipAddress))) { - using WebClient webClient = new WebClient(); - _ipAddress = IPAddress.Parse(webClient.DownloadString("https://ip.syroot.com")); + using HttpClient httpClient = new HttpClient(); + _ipAddress = IPAddress.Parse( + httpClient.GetStringAsync("https://ip.syroot.com").GetAwaiter().GetResult()); } return _ipAddress; } diff --git a/src/tool/Syroot.Worms.Worms2.GameServer/PacketConnection.cs b/src/tool/Syroot.Worms.Worms2.GameServer/PacketConnection.cs index c2baa9f..c0bbbf3 100644 --- a/src/tool/Syroot.Worms.Worms2.GameServer/PacketConnection.cs +++ b/src/tool/Syroot.Worms.Worms2.GameServer/PacketConnection.cs @@ -40,7 +40,8 @@ namespace Syroot.Worms.Worms2.GameServer Stream stream = client.GetStream(); _reader = PipeReader.Create(stream); _writer = PipeWriter.Create(stream); - RemoteEndPoint = (IPEndPoint)client.Client.RemoteEndPoint; + RemoteEndPoint = client.Client.RemoteEndPoint as IPEndPoint + ?? throw new ArgumentException("TCP client is not connected.", nameof(client)); } // ---- PROPERTIES --------------------------------------------------------------------------------------------- diff --git a/src/tool/Syroot.Worms.Worms2.GameServer/Program.cs b/src/tool/Syroot.Worms.Worms2.GameServer/Program.cs index 0f0290c..2f27cea 100644 --- a/src/tool/Syroot.Worms.Worms2.GameServer/Program.cs +++ b/src/tool/Syroot.Worms.Worms2.GameServer/Program.cs @@ -23,9 +23,11 @@ namespace Syroot.Worms.Worms2.GameServer private static IPEndPoint ParseEndPoint(string? s, IPEndPoint fallback) { - if (UInt16.TryParse(s, out ushort port)) + if (s == null) + return fallback; + else if (UInt16.TryParse(s, out ushort port)) return new IPEndPoint(fallback.Address, port); - else if (IPEndPoint.TryParse(s, out IPEndPoint endPoint)) + else if (IPEndPoint.TryParse(s, out IPEndPoint? endPoint)) return endPoint; else return fallback; diff --git a/src/tool/Syroot.Worms.Worms2.GameServer/Server.cs b/src/tool/Syroot.Worms.Worms2.GameServer/Server.cs index 51825c8..2133d5d 100644 --- a/src/tool/Syroot.Worms.Worms2.GameServer/Server.cs +++ b/src/tool/Syroot.Worms.Worms2.GameServer/Server.cs @@ -405,7 +405,7 @@ namespace Syroot.Worms.Worms2.GameServer return; // Require valid room ID and IP. - if (IPAddress.TryParse(packet.Data, out IPAddress ip) && connection.RemoteEndPoint.Address.Equals(ip)) + if (IPAddress.TryParse(packet.Data, out IPAddress? ip) && connection.RemoteEndPoint.Address.Equals(ip)) { Game newGame = new Game(++_lastID, fromUser.Name, fromUser.Session.Nation, fromUser.RoomID, connection.RemoteEndPoint.Address, // do not use bad NAT IP reported by users here