Compare commits

..
3 Commits
Author SHA1 Message Date
thayol 781c6047ca Add a feature switch to reachability 2026-08-03 21:02:31 +02:00
thayol 51d76cd451 Pre-check reachability to avoid long locks 2026-08-03 21:00:43 +02:00
thayol 16e409727b Lock state file while processing request 2026-08-03 20:54:52 +02:00
2 changed files with 37 additions and 18 deletions
+2
View File
@@ -5,3 +5,5 @@ $state_file = "state.json"; // The location for the state file. If it does not e
$error_reporting = false; // Whether PHP errors should be shown. Not recommended for production. $error_reporting = false; // Whether PHP errors should be shown. Not recommended for production.
$lobby_timeout_seconds = 30; // How long it should take for unresponsive lobbies to get auto-delisted. $lobby_timeout_seconds = 30; // How long it should take for unresponsive lobbies to get auto-delisted.
$check_reachability = true; // Whether to reach out to the lobby listing host to confirm the lobby is reachable.
+35 -18
View File
@@ -28,16 +28,38 @@ if ($error_reporting) {
ini_set('display_errors', 1); ini_set('display_errors', 1);
} }
if (!file_exists($state_file)) { $reachable = false;
$dirname = dirname($state_file); if (isset($_GET["new"])) {
if (!is_dir($dirname)) { if ($check_reachability) {
mkdir($dirname, 0777, true); require "enet_probe.php";
$host = $_SERVER['REMOTE_ADDR'];
if (!empty($_GET["host"])) {
$host = $_GET["host"];
}
$port = 50403;
if (!empty($_GET["port"]) && is_numeric($_GET["port"])) {
$port = intval($_GET["port"]);
}
$reachable = enet_probe($host, $port);
}
else {
$reachable = true;
} }
file_put_contents($state_file, json_encode([ "lobbies" => []]));
} }
$state = json_decode(file_get_contents($state_file), true); $dirname = dirname($state_file);
if (!is_dir($dirname)) {
mkdir($dirname, 0777, true);
}
$state_fp = fopen($state_file, "c+");
flock($state_fp, LOCK_EX);
$contents = stream_get_contents($state_fp);
$state = json_decode($contents, true);
if (empty($state)) {
$state = ["lobbies" => []];
}
$cutoff_timestamp = time() - $lobby_timeout_seconds; $cutoff_timestamp = time() - $lobby_timeout_seconds;
foreach (array_keys($state["lobbies"]) as $id) { foreach (array_keys($state["lobbies"]) as $id) {
@@ -63,16 +85,6 @@ if (isset($_GET["list"])) {
echo json_encode(["lobbies" => $list]); echo json_encode(["lobbies" => $list]);
} }
elseif (isset($_GET["new"])) { elseif (isset($_GET["new"])) {
require "enet_probe.php";
$host = $_SERVER['REMOTE_ADDR'];
if (!empty($_GET["host"])) {
$host = $_GET["host"];
}
$port = 50403;
if (!empty($_GET["port"]) && is_numeric($_GET["port"])) {
$port = intval($_GET["port"]);
}
$reachable = enet_probe($host, $port);
header("Content-Type: application/json"); header("Content-Type: application/json");
if ($reachable) { if ($reachable) {
$id = generate_uuid_v4(); $id = generate_uuid_v4();
@@ -148,4 +160,9 @@ else {
echo json_encode(["status" => "OK"]); echo json_encode(["status" => "OK"]);
} }
file_put_contents($state_file, json_encode($state, JSON_PRETTY_PRINT)); ftruncate($state_fp, 0);
rewind($state_fp);
fwrite($state_fp, json_encode($state, JSON_PRETTY_PRINT));
fflush($state_fp);
flock($state_fp, LOCK_UN);
fclose($state_fp);