Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

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

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

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

// Set default server type if none was provided, although this
// generally shouldn't happen
request.Attributes.TryAdd("SERVER_TYPE", "kartPark");
Expand Down Expand Up @@ -253,8 +254,8 @@ 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

// For now just prevent joins into in-progress lobbies entirely, causes instability
if (room.Simulation.RaceState >= RaceState.LoadingIntoRace || !room.Simulation.CanJoinAsRacer())
return false;
}
Expand Down Expand Up @@ -285,7 +286,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 != null && (room.Simulation.RaceState >= RaceState.LoadingIntoRace || !room.Simulation.CanJoinAsRacer())) ? "1" : "0";

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

// TODO: Adjust player counts on Karting?
Expand Down
4 changes: 4 additions & 0 deletions Bombd/Core/SimServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,17 @@ private void SetCurrentGameroomState(RoomState state)
{
BroadcastPlayerState();
SwitchAllToRacers();
if (_raceSettings != null)
Room.UpdateAttributes(_raceSettings.Value);
break;
}
case RoomState.RaceInProgress:
{
StartEvent();
BroadcastSessionInfo();
BroadcastPlayerState();
if (_raceSettings != null)
Room.UpdateAttributes(_raceSettings.Value);
break;
}
case RoomState.Ready:
Expand Down
4 changes: 3 additions & 1 deletion Bombd/Data/Matchmaking/ModNation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@
<SimpleFilter name="SERVER_TYPE">
<PossibleValue>kartPark</PossibleValue>
<PossibleValue>competitive</PossibleValue>
</SimpleFilter>
</SimpleFilter>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

DoBlockAndReceive();
DoBlockAndReceive(ReadTimeout);
}

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

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

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

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

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

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

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

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

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

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