Skip to content

Commit

Permalink
Handle Steam server custom ports
Browse files Browse the repository at this point in the history
* SteamClient.Connect accepts ports

hostAddress can optionally be of format <steam64>:<port>

* inline TrySplitHostAddress

* fail with an error if port fails to parse

we used to silently fail and fallback to parsing everything as a regular
steam64. this would probably also fail but this error is nicer

* rename a variable

* SteamClient parses out port after checking for a local address

ConnectLocal() doesn't even respect ports so better to error out if we
specify localhost:8000 or something

---------

Co-authored-by: Drew <[email protected]>
  • Loading branch information
hedgewizards and Drew authored May 6, 2024
1 parent c5a4a19 commit 1e78190
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions Assets/RiptideSteamTransport/Transport/SteamClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void ChangeLocalServer(SteamServer newLocalServer)
public bool Connect(string hostAddress, out Connection connection, out string connectError)
{
connection = null;
int port = 0;

try
{
Expand Down Expand Up @@ -65,9 +66,21 @@ public bool Connect(string hostAddress, out Connection connection, out string co
connection = steamConnection = ConnectLocal();
return true;
}
else if (ulong.TryParse(hostAddress, out ulong hostId))

int portSeperatorIndex = hostAddress.IndexOf(':');
if (portSeperatorIndex != -1)
{
if (!int.TryParse(hostAddress[(portSeperatorIndex + 1)..], out port))
{
connectError = $"Couldn't connect: Failed to parse port '{hostAddress[(portSeperatorIndex + 1)..]}'";
return false;
}
hostAddress = hostAddress[..portSeperatorIndex];
}

if (ulong.TryParse(hostAddress, out ulong hostId))
{
connection = steamConnection = TryConnect(new CSteamID(hostId));
connection = steamConnection = TryConnect(new CSteamID(hostId), port);
return connection != null;
}

Expand All @@ -93,7 +106,7 @@ private SteamConnection ConnectLocal()
return new SteamConnection(playerSteamId, connectionToServer, this);
}

private SteamConnection TryConnect(CSteamID hostId)
private SteamConnection TryConnect(CSteamID hostId, int port)
{
try
{
Expand All @@ -103,7 +116,7 @@ private SteamConnection TryConnect(CSteamID hostId)
serverIdentity.SetSteamID(hostId);

SteamNetworkingConfigValue_t[] options = new SteamNetworkingConfigValue_t[] { };
HSteamNetConnection connectionToServer = SteamNetworkingSockets.ConnectP2P(ref serverIdentity, 0, options.Length, options);
HSteamNetConnection connectionToServer = SteamNetworkingSockets.ConnectP2P(ref serverIdentity, port, options.Length, options);

ConnectTimeout();
return new SteamConnection(hostId, connectionToServer, this);
Expand Down

0 comments on commit 1e78190

Please sign in to comment.