Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
639382b
Remove check for if race is in progress when advertising servers to game
derole1 Oct 8, 2025
ebb28c5
Merge branch 'ennuo:main' into lobby-join-state-changes
derole1 Oct 11, 2025
f134da1
Exclude away players from StartEvent
derole1 Oct 11, 2025
29cd975
Away status no longer forces you into the game
derole1 Oct 12, 2025
63d7c6e
Add code comments
derole1 Oct 12, 2025
9899335
Implement matchmaking filter to tell game when a lobby is in progress
derole1 Oct 12, 2025
67aae72
Update attribute state at start and end of race
derole1 Oct 12, 2025
63fae2f
Fix away issues
derole1 Oct 12, 2025
7d61915
More detailed exception logging
Oct 13, 2025
769a9a2
Add debugging profile required for development in WSL2, Add ability t…
Oct 13, 2025
62aa9cb
Make consistent with rest of codebase
Oct 13, 2025
01a4a29
Debug logging for loglevel
Oct 13, 2025
33fa756
Update loglevel debug
Oct 13, 2025
c1c95c4
Remove conditionals for logging
Oct 13, 2025
367a3c8
Temp for debugging
Oct 14, 2025
4268f22
Replace SemaphoreSlim with a traditional lock to fix double-lock that…
Oct 15, 2025
09ca685
Fix stale sockets getting stuck showing as connected indefinitely for…
derole1 Oct 17, 2025
96bc50f
Fix stale sockets when a client uncleanly disconnects from an SSL ser…
derole1 Oct 17, 2025
42212b1
Merge branch 'main' of https://github.com/derole1/Bombd
derole1 Oct 17, 2025
b5f3849
Merge branch 'ennuo:main' into main
derole1 Oct 17, 2025
ae4d58a
Merge branch 'ennuo:main' into lobby-join-state-changes
derole1 Oct 17, 2025
a9189d9
Merge pull request #3 from derole1/main
derole1 Oct 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Bombd/Core/BombdConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ static BombdConfig()
/// How many times each service performs an update in a second.
/// </summary>
public int TickRate { get; set; } = 15;

/// <summary>
/// The maximum log level to log messages from.
/// </summary>
public string MaxLogLevel { get; set; } = Enum.GetName(LogLevel.Info);

/// <summary>
/// Whether or not connections from LittleBigPlanet Karting are allowed.
Expand Down
10 changes: 4 additions & 6 deletions Bombd/Core/RoomManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public GameRoom CreateRoom(CreateGameRequest request)
request.Attributes["__JOIN_MODE"] = "OPEN";
request.Attributes["__MM_MODE_G"] = "OPEN";
request.Attributes["__MM_MODE_P"] = "OPEN";

request.Attributes["IS_LOCKED"] = "0";

// Set default server type if none was provided, although this
// generally shouldn't happen
request.Attributes.TryAdd("SERVER_TYPE", "kartPark");
Expand Down Expand Up @@ -253,10 +254,6 @@ public List<GameBrowserGame> SearchRooms(GameAttributes attributes, Platform id,
// before advertising the session.
if (!room.Simulation.HasRaceSettings)
return false;

// If the race is already in progress, don't advertise the session
if (room.Simulation.RaceState >= RaceState.LoadingIntoRace || !room.Simulation.CanJoinAsRacer())
return false;
}

foreach (KeyValuePair<string, string> attribute in attributes)
Expand Down Expand Up @@ -285,7 +282,8 @@ public void UpdateRoom(GameRoom room, EventSettings settings)
attr["__MM_MODE_G"] = visibility;
attr["__MM_MODE_P"] = visibility;
attr["__JOIN_MODE"] = visibility;

attr["IS_LOCKED"] = (room.Simulation.RaceState >= RaceState.LoadingIntoRace || !room.Simulation.CanJoinAsRacer()) ? "1" : "0";

attr["__MAX_PLAYERS"] = settings.MaxHumans.ToString();

// TODO: Adjust player counts on Karting?
Expand Down
36 changes: 32 additions & 4 deletions Bombd/Core/SimServer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Bombd.Extensions;
using Bombd.Globals;
using Bombd.Helpers;
Expand Down Expand Up @@ -125,7 +126,7 @@ public bool IsRoomReady()

private void SwitchAllToRacers()
{
foreach (GamePlayer player in _players)
foreach (GamePlayer player in _players.Where(x => x.State.Away == 0))
player.IsSpectator = false;
foreach (PlayerInfo info in _playerInfos)
info.Operation = PlayerJoinStatus.RacerPending;
Expand All @@ -152,6 +153,22 @@ private void SwitchToSpectator(GamePlayer player)
UpdateRaceSetup();
}

private void SwitchToRacer(GamePlayer player)
{
if (!player.IsSpectator) return;

player.IsSpectator = false;
if (IsKarting) BroadcastSessionInfo();
else
{
Broadcast(new NetMessageSessionInfo(PlayerSessionOperation.SwitchSpectatorToRacer, player.UserId),
NetMessageType.PlayerSessionInfo);
}

if (RaceState == RaceState.GameroomCountdown)
UpdateRaceSetup();
}

private void BroadcastSessionInfo()
{
// In ModNation, the session messages seem to only be switches and joins rather than their actual states,
Expand Down Expand Up @@ -255,8 +272,9 @@ public void OnPlayerLeft(GamePlayer player, bool disconnected)
if (player.UserId == Owner)
{
var random = new Random();
int index = random.Next(0, _players.Count);
var randomPlayer = _players[index];
var availablePlayers = _players.Where(x => x.State.Away == 0).ToArray();
int index = random.Next(0, availablePlayers.Length);
var randomPlayer = availablePlayers[index];

Owner = randomPlayer.UserId;
if (_raceSettings != null)
Expand Down Expand Up @@ -420,13 +438,17 @@ private void SetCurrentGameroomState(RoomState state)
{
BroadcastPlayerState();
SwitchAllToRacers();
if (_raceSettings != null)
Room.UpdateAttributes(_raceSettings.Value);
break;
}
case RoomState.RaceInProgress:
{
StartEvent();
BroadcastSessionInfo();
BroadcastPlayerState();
if (_raceSettings != null)
Room.UpdateAttributes(_raceSettings.Value);
break;
}
case RoomState.Ready:
Expand Down Expand Up @@ -1437,6 +1459,12 @@ public void OnNetworkMessage(GamePlayer player, uint senderNameUid, NetMessageTy

// Patch our existing player state with the new message
player.State.Update(state);

// Check if player is away, if they are switch them to spectator so they do not join the race
if (player.State.Away != 0)
SwitchToSpectator(player);
else
SwitchToRacer(player);

// If we're not in a gameroom, there's no GameroomReady event, so wait until we've received
// the player config and the second player state update to finish our "connecting" process.
Expand Down Expand Up @@ -1694,7 +1722,7 @@ private void SimUpdate()
if (_raceSettings != null && room.State < RoomState.RaceInProgress)
{
int numReadyPlayers =
_players.Count(x => (x.State.Flags & PlayerStateFlags.GameRoomReady) != 0);
_players.Count(x => (x.State.Flags & PlayerStateFlags.GameRoomReady) != 0 && x.State.Away == 0);
bool hasMinPlayers = numReadyPlayers >= _raceSettings.Value.MinHumans;

// Karting, series races, and ranked races don't allow the "owner" to start the race
Expand Down
4 changes: 3 additions & 1 deletion Bombd/Data/Matchmaking/ModNation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@
<SimpleFilter name="SERVER_TYPE">
<PossibleValue>kartPark</PossibleValue>
<PossibleValue>competitive</PossibleValue>
</SimpleFilter>
</SimpleFilter>

<NumberPreference name="IS_LOCKED" range="1.0" weight="1.0" default="0.0"></NumberPreference>
13 changes: 3 additions & 10 deletions Bombd/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ namespace Bombd.Logging;

public class Logger
{
#if DEBUG
private const LogLevel MaxLevel = LogLevel.Trace;
#else
private const LogLevel MaxLevel = LogLevel.Info;
#endif

private static readonly ConcurrentQueue<LogEntry> LogQueue = new();
private static LogLevel MaxLevel = LogLevel.Info;

static Logger()
{
Expand Down Expand Up @@ -48,14 +43,14 @@ private static ConsoleColor GetLogColor(LogLevel level)
};
}

public static void SetLogMaxLevel(LogLevel level) => MaxLevel = level;

public static void LogError<T>(string message) => Log<T>(LogLevel.Error, message);
public static void LogWarning<T>(string message) => Log<T>(LogLevel.Warning, message);
public static void LogInfo<T>(string message) => Log<T>(LogLevel.Info, message);

[Conditional("DEBUG")]
public static void LogDebug<T>(string message) => Log<T>(LogLevel.Debug, message);

[Conditional("DEBUG")]
public static void LogTrace<T>(string message) => Log<T>(LogLevel.Trace, message);

public static void Log<T>(LogLevel level, string message)
Expand All @@ -67,10 +62,8 @@ public static void Log<T>(LogLevel level, string message)
public static void LogWarning(Type type, string message) => Log(type, LogLevel.Warning, message);
public static void LogInfo(Type type, string message) => Log(type, LogLevel.Info, message);

[Conditional("DEBUG")]
public static void LogDebug(Type type, string message) => Log(type, LogLevel.Debug, message);

[Conditional("DEBUG")]
public static void LogTrace(Type type, string message) => Log(type, LogLevel.Trace, message);

public static void Log(Type type, LogLevel level, string message)
Expand Down
2 changes: 2 additions & 0 deletions Bombd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Bombd.Services;
using Directory = Bombd.Services.Directory;

Logger.SetLogMaxLevel(Enum.Parse<LogLevel>(BombdConfig.Instance.MaxLogLevel));

string certificate = BombdConfig.Instance.PfxCertificate;
if (string.IsNullOrEmpty(certificate))
{
Expand Down
21 changes: 14 additions & 7 deletions Bombd/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
{
"profiles": {
"Bombd": {
"commandName": "Project"
},
"Docker": {
"commandName": "Docker"
"profiles": {
"Bombd": {
"commandName": "Project"
},
"Docker": {
"commandName": "Docker"
},
"WSL": {
"commandName": "WSL2",
"environmentVariables": {
"LD_LIBRARY_PATH": "/usr/local/lib64:/usr/local/lib:/usr/lib",
"OPENSSL_CONF": "/etc/ssl/openssl.cnf"
}
}
}
}
}
2 changes: 1 addition & 1 deletion Bombd/Protocols/ConnectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class ConnectionBase
public readonly IServer Server;

public readonly BombdService Service;
protected ConnectionState State = ConnectionState.Disconnected;
public ConnectionState State = ConnectionState.Disconnected;

protected ConnectionBase(BombdService service, IServer server)
{
Expand Down
74 changes: 46 additions & 28 deletions Bombd/Protocols/TCP/SslConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public class SslConnection : ConnectionBase
{
private const int MaxMessageSize = 8192;
private const int KeepAliveFrequency = 3000;

private const int ReadTimeout = 10_000;
private const int WriteTimeout = 10_000;

// u32 MsgLength
// char Md5Digest[16]
// char Protocol
Expand Down Expand Up @@ -48,12 +50,12 @@ public async void Connect(Socket socket)
try
{
_sslStream = new SslStream(new NetworkStream(_socket, false), false);

// Prevent lingering connections, the game should periodically send keep alive packets
// in response to our own.
_sslStream.ReadTimeout = 10_000;
_sslStream.WriteTimeout = 10_000;
_sslStream.ReadTimeout = ReadTimeout;
_sslStream.WriteTimeout = WriteTimeout;

await _sslStream.AuthenticateAsServerAsync(_server.Certificate, false, SslProtocols.Ssl3, false);

_keepAliveTimer = new Timer(KeepAliveFrequency);
Expand All @@ -67,7 +69,7 @@ public async void Connect(Socket socket)
return;
}

DoBlockAndReceive();
DoBlockAndReceive(ReadTimeout);
}

public override void Disconnect()
Expand Down Expand Up @@ -151,36 +153,40 @@ public override void Send(ArraySegment<byte> data, PacketType type)
offset += size;
} while (offset < messageSize);
}
catch (Exception)
catch (Exception e)
{
Logger.LogError<SslConnection>("An error occurred during send. Closing connection.");
Logger.LogDebug<SslConnection>(e.ToString());
Disconnect();
}
}
}

private async void DoBlockAndReceive()
private async void DoBlockAndReceive(int readTimeout)
{
while (State != ConnectionState.Disconnected)
{
int payloadSize;
PacketType type;
try
{
int len = await _sslStream.ReadAsync(_recv.AsMemory(0, MessageHeaderSize));

// Socket has been shutdown on the other side
if (len == 0)
{
Disconnect();
return;
}

if (len != MessageHeaderSize)
using (var cTokenSrc = CreateCancellationTokenTimeout(readTimeout))
{
Logger.LogError<SslConnection>("Received message with invalid header. Closing connection.");
Disconnect();
return;
int len = await _sslStream.ReadAsync(_recv.AsMemory(0, MessageHeaderSize), cTokenSrc.Token);

// Socket has been shutdown on the other side
if (len == 0)
{
Disconnect();
return;
}

if (len != MessageHeaderSize)
{
Logger.LogError<SslConnection>("Received message with invalid header. Closing connection.");
Disconnect();
return;
}
}

payloadSize = ((_recv[0] << 24) | (_recv[1] << 16) | (_recv[2] << 8) | _recv[3]) -
Expand All @@ -198,20 +204,25 @@ private async void DoBlockAndReceive()
int offset = 0;
do
{
len = await _sslStream.ReadAsync(_recv.AsMemory(offset, payloadSize - offset));
if (len == 0)
using (var cTokenSrc = CreateCancellationTokenTimeout(readTimeout))
{
Disconnect();
return;
}
int len = await _sslStream.ReadAsync(_recv.AsMemory(offset, payloadSize - offset), cTokenSrc.Token);

offset += len;
if (len == 0)
{
Disconnect();
return;
}

offset += len;
}
} while (offset < payloadSize);
}
}
catch (Exception)
catch (Exception e)
{
Logger.LogError<SslConnection>("An error occurred while reading from socket. Closing connection.");
Logger.LogDebug<SslConnection>(e.ToString());
Disconnect();
return;
}
Expand All @@ -226,4 +237,11 @@ private async void DoBlockAndReceive()
}
}
}

private CancellationTokenSource CreateCancellationTokenTimeout(int timeout)
{
var cTokenSrc = new CancellationTokenSource();
cTokenSrc.CancelAfter(timeout);
return cTokenSrc;
}
}
Loading