Skip to content

Commit

Permalink
Fixed errors when sending messages to disconnected clients
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-weiland committed Nov 30, 2021
1 parent e16d97d commit 12bca39
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
25 changes: 8 additions & 17 deletions Assets/RiptideSteamTransport/Transport/SteamClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (c) 2021 Tom Weiland
// For additional information please see the included LICENSE.md file or view it on GitHub: https://github.com/tom-weiland/RiptideSteamTransport/blob/main/LICENSE.md

using RiptideNetworking.Transports.Utils;
using Steamworks;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -38,21 +39,21 @@ public class SteamClient : SteamCommon, IClient
public bool IsConnecting => connectionState == ConnectionState.connecting;
/// <inheritdoc/>
public bool IsConnected => connectionState == ConnectionState.connected;
/// <summary>The time (in milliseconds) after which to disconnect if there's no heartbeat from the server.</summary>
public ushort TimeoutTime { get; set; } = 5000; // TODO: make Steam's sockets time out after this time (assuming such functionality exists)
/// <summary>The time (in milliseconds) after which to give up on a connection attempt.</summary>
public ushort TimeoutTime { get; set; } = 5000;

private Callback<SteamNetConnectionStatusChangedCallback_t> connectionStatusChanged = null;
private CSteamID hostSteamId = CSteamID.Nil;
private HSteamNetConnection hostConnection;
private List<Action> bufferedData;
private ConnectionState connectionState;
private SteamServer localServer;
private ActionQueue actionQueue;

public SteamClient(SteamServer localServer = null, ushort timeoutTime = 5000, string logName = "Steam Client") : base(logName)
{
this.localServer = localServer;
TimeoutTime = timeoutTime;
bufferedData = new List<Action>();
actionQueue = new ActionQueue();
}

public void ChangeLocalServer(SteamServer newLocalServer)
Expand Down Expand Up @@ -174,12 +175,7 @@ private void ConnectionStatusChangedToConnected()
{
connectionState = ConnectionState.connected;

if (bufferedData.Count > 0)
{
RiptideLogger.Log(LogName, $"{bufferedData.Count} received before connection was established. Processing now.");
foreach (Action a in bufferedData)
a();
}
actionQueue.ExecuteAll();
}

/// <inheritdoc/>
Expand All @@ -203,7 +199,7 @@ private void HandleMessages()
else
{
Debug.Log("Ok this actually happens");
bufferedData.Add(() => Handle(message, messageHeader));
actionQueue.Add(() => Handle(message, messageHeader));
}
}
}
Expand Down Expand Up @@ -242,12 +238,7 @@ public void FlushMessages()
/// <inheritdoc/>
public void Send(Message message, bool shouldRelease = true)
{
EResult res = SteamSend(message, hostConnection);

if (res == EResult.k_EResultNoConnection || res == EResult.k_EResultInvalidParam)
OnDisconnected();
else if (res != EResult.k_EResultOK)
RiptideLogger.Log(LogName, $"Failed to send message: {res}");
SteamSend(message, hostConnection);

if (shouldRelease)
message.Release();
Expand Down
8 changes: 2 additions & 6 deletions Assets/RiptideSteamTransport/Transport/SteamServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private void HandleMessages()
{
IntPtr[] ptrs = new IntPtr[MaxMessages]; // TODO: remove allocation?

// TODO: consider using poll groups -> https://partner.steamgames.com/doc/api/ISteamNetworkingSockets#functions_poll_groups
int messageCount = SteamNetworkingSockets.ReceiveMessagesOnConnection(client.SteamNetConnection, ptrs, MaxMessages);
if (messageCount > 0)
{
Expand Down Expand Up @@ -181,12 +182,7 @@ public void Send(Message message, ushort toClientId, bool shouldRelease = true)

internal void Send(Message message, SteamConnection client, bool shouldRelease = true)
{
EResult res = SteamSend(message, client.SteamNetConnection);

if (res == EResult.k_EResultNoConnection || res == EResult.k_EResultInvalidParam)
LocalDisconnect(client, "Disconnected"); // TODO: LocalDisconnect removes the client from the dictionary, which is a problem if this was called from within a foreach loop, like in SendToAll
else if (res != EResult.k_EResultOK)
RiptideLogger.Log(LogName, $"Failed to send message: {res}");
SteamSend(message, client.SteamNetConnection);

if (shouldRelease)
message.Release();
Expand Down

0 comments on commit 12bca39

Please sign in to comment.