Fix deprecation and nullability warnings.

This commit is contained in:
Ray 2024-05-16 12:02:48 +02:00
parent 63691f897b
commit b4e6433545
4 changed files with 11 additions and 6 deletions

View File

@ -1,4 +1,5 @@
using System.Net; using System.Net;
using System.Net.Http;
namespace Syroot.Worms.Mgame.GameServer 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. // Retrieve external IP if not yet done and given IP is invalid.
if (_ipAddress == null && (IP == null || !IPAddress.TryParse(IP, out _ipAddress))) if (_ipAddress == null && (IP == null || !IPAddress.TryParse(IP, out _ipAddress)))
{ {
using WebClient webClient = new WebClient(); using HttpClient httpClient = new HttpClient();
_ipAddress = IPAddress.Parse(webClient.DownloadString("https://ip.syroot.com")); _ipAddress = IPAddress.Parse(
httpClient.GetStringAsync("https://ip.syroot.com").GetAwaiter().GetResult());
} }
return _ipAddress; return _ipAddress;
} }

View File

@ -40,7 +40,8 @@ namespace Syroot.Worms.Worms2.GameServer
Stream stream = client.GetStream(); Stream stream = client.GetStream();
_reader = PipeReader.Create(stream); _reader = PipeReader.Create(stream);
_writer = PipeWriter.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 --------------------------------------------------------------------------------------------- // ---- PROPERTIES ---------------------------------------------------------------------------------------------

View File

@ -23,9 +23,11 @@ namespace Syroot.Worms.Worms2.GameServer
private static IPEndPoint ParseEndPoint(string? s, IPEndPoint fallback) 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); return new IPEndPoint(fallback.Address, port);
else if (IPEndPoint.TryParse(s, out IPEndPoint endPoint)) else if (IPEndPoint.TryParse(s, out IPEndPoint? endPoint))
return endPoint; return endPoint;
else else
return fallback; return fallback;

View File

@ -405,7 +405,7 @@ namespace Syroot.Worms.Worms2.GameServer
return; return;
// Require valid room ID and IP. // 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, 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 connection.RemoteEndPoint.Address, // do not use bad NAT IP reported by users here