Skip to content

Commit

Permalink
Fix connection attempts being rejected incorrectly
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
tom-weiland committed Oct 5, 2023
1 parent 14abfe1 commit c26bee2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
6 changes: 6 additions & 0 deletions Assets/RiptideSteamTransport/Transport/SteamClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class SteamClient : SteamPeer, IClient
{
public event EventHandler Connected;
public event EventHandler ConnectionFailed;
public event EventHandler<DataReceivedEventArgs> DataReceived;
public event EventHandler<DisconnectedEventArgs> Disconnected;

private const string LocalHostName = "localhost";
Expand Down Expand Up @@ -193,6 +194,11 @@ protected virtual void OnConnectionFailed()
ConnectionFailed?.Invoke(this, EventArgs.Empty);
}

protected override void OnDataReceived(byte[] dataBuffer, int amount, SteamConnection fromConnection)
{
DataReceived?.Invoke(this, new DataReceivedEventArgs(dataBuffer, amount, fromConnection));
}

protected virtual void OnDisconnected(DisconnectReason reason)
{
Disconnected?.Invoke(this, new DisconnectedEventArgs(steamConnection, reason));
Expand Down
2 changes: 2 additions & 0 deletions Assets/RiptideSteamTransport/Transport/SteamConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class SteamConnection : Connection, IEquatable<SteamConnection>
public readonly CSteamID SteamId;
public readonly HSteamNetConnection SteamNetConnection;

internal bool DidReceiveConnect;

private readonly SteamPeer peer;

internal SteamConnection(CSteamID steamId, HSteamNetConnection steamNetConnection, SteamPeer peer)
Expand Down
11 changes: 3 additions & 8 deletions Assets/RiptideSteamTransport/Transport/SteamPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

namespace Riptide.Transports.Steam
{
public class SteamPeer
public abstract class SteamPeer
{
/// <summary>The name to use when logging messages via <see cref="RiptideLogger"/>.</summary>
/// <summary>The name to use when logging messages via <see cref="Utils.RiptideLogger"/>.</summary>
public const string LogName = "STEAM";

public event EventHandler<DataReceivedEventArgs> DataReceived;

protected const int MaxMessages = 256;

private readonly byte[] receiveBuffer;
Expand Down Expand Up @@ -66,9 +64,6 @@ internal void Send(byte[] dataBuffer, int numBytes, HSteamNetConnection toConnec
handle.Free();
}

protected virtual void OnDataReceived(byte[] dataBuffer, int amount, SteamConnection fromConnection)
{
DataReceived?.Invoke(this, new DataReceivedEventArgs(dataBuffer, amount, fromConnection));
}
protected abstract void OnDataReceived(byte[] dataBuffer, int amount, SteamConnection fromConnection);
}
}
14 changes: 14 additions & 0 deletions Assets/RiptideSteamTransport/Transport/SteamServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Riptide.Transports.Steam
public class SteamServer : SteamPeer, IServer
{
public event EventHandler<ConnectedEventArgs> Connected;
public event EventHandler<DataReceivedEventArgs> DataReceived;
public event EventHandler<DisconnectedEventArgs> Disconnected;

public ushort Port { get; private set; }
Expand Down Expand Up @@ -134,6 +135,19 @@ protected internal virtual void OnConnected(Connection connection)
Connected?.Invoke(this, new ConnectedEventArgs(connection));
}

protected override void OnDataReceived(byte[] dataBuffer, int amount, SteamConnection fromConnection)
{
if ((MessageHeader)dataBuffer[0] == MessageHeader.Connect)
{
if (fromConnection.DidReceiveConnect)
return;

fromConnection.DidReceiveConnect = true;
}

DataReceived?.Invoke(this, new DataReceivedEventArgs(dataBuffer, amount, fromConnection));
}

protected virtual void OnDisconnected(CSteamID steamId, DisconnectReason reason)
{
if (connections.TryGetValue(steamId, out SteamConnection connection))
Expand Down

0 comments on commit c26bee2

Please sign in to comment.