Skip to content

Commit

Permalink
Use IPEndPoint.TryParse
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Aug 30, 2024
1 parent 223f3fd commit c7ec81a
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions SteamKit2/SteamKit2/Util/NetHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static IPAddress GetLocalIP( Socket activeSocket )

if ( ipEndPoint == null || ipEndPoint.Address == IPAddress.Any )
throw new InvalidOperationException( "Socket not connected" );

return ipEndPoint.Address;
}

Expand All @@ -42,7 +42,7 @@ public static uint GetIPAddressAsUInt( IPAddress ipAddr )

Span<byte> addrBytes = stackalloc byte[ 4 ];
ipAddr.TryWriteBytes( addrBytes, out _ );

return Unsafe.BitCast<int, uint>( IPAddress.NetworkToHostOrder( BitConverter.ToInt32( addrBytes ) ) );
}

Expand Down Expand Up @@ -110,27 +110,17 @@ public static CMsgIPAddress ObfuscatePrivateIP( this CMsgIPAddress msgIpAddress

public static bool TryParseIPEndPoint( string stringValue, [NotNullWhen( true )] out IPEndPoint? endPoint )
{
var colonPosition = stringValue.LastIndexOf( ':' );

if ( colonPosition == -1 )
if ( !IPEndPoint.TryParse( stringValue, out endPoint ) )
{
endPoint = null;
return false;
}

if ( !IPAddress.TryParse( stringValue.AsSpan( 0, colonPosition ), out var address ) )
{
endPoint = null;
return false;
}

if ( !ushort.TryParse( stringValue.AsSpan( colonPosition + 1 ), out var port ) )
if ( endPoint.Port == 0 )
{
endPoint = null;
return false;
}

endPoint = new IPEndPoint( address, port );
return true;
}

Expand Down

0 comments on commit c7ec81a

Please sign in to comment.