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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Bombd.Extensions;
using Bombd.Globals;
using Bombd.Helpers;
Expand DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Allow players to join mid game and fix away players from being pulled into a race by derole1 · Pull Request #3 · ennuo/Bombd · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Bombd.Extensions;
using Bombd.Globals;
using Bombd.Helpers;
Expand DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Allow players to join mid game and fix away players from being pulled into a race by derole1 · Pull Request #3 · ennuo/Bombd · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Bombd.Extensions;
using Bombd.Globals;
using Bombd.Helpers;
Expand DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Allow players to join mid game and fix away players from being pulled into a race by derole1 · Pull Request #3 · ennuo/Bombd · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Bombd.Extensions;
using Bombd.Globals;
using Bombd.Helpers;
Expand DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Allow players to join mid game and fix away players from being pulled into a race by derole1 · Pull Request #3 · ennuo/Bombd · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Bombd.Extensions;
using Bombd.Globals;
using Bombd.Helpers;
Expand DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Allow players to join mid game and fix away players from being pulled into a race by derole1 · Pull Request #3 · ennuo/Bombd · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Bombd.Extensions;
using Bombd.Globals;
using Bombd.Helpers;
Expand DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Allow players to join mid game and fix away players from being pulled into a race by derole1 · Pull Request #3 · ennuo/Bombd · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Bombd.Extensions;
using Bombd.Globals;
using Bombd.Helpers;
Expand DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); Allow players to join mid game and fix away players from being pulled into a race by derole1 · Pull Request #3 · ennuo/Bombd · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Bombd.Extensions;
using Bombd.Globals;
using Bombd.Helpers;
Expand DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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