Skip to content

Commit

Permalink
Allow to keep the server connection for clients when polling
Browse files Browse the repository at this point in the history
  • Loading branch information
Benau committed Sep 2, 2018
1 parent b080a9c commit c65280e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 40 deletions.
17 changes: 17 additions & 0 deletions api/server.php
Expand Up @@ -193,6 +193,23 @@
}
break;

case 'clear-user-joined-server':
try
{
$userid = isset($_POST['userid']) ? (int)$_POST['userid'] : 0;
$token = isset($_POST['token']) ? $_POST['token'] : "";
ClientSession::get($token, $userid)->clearUserJoinedServer();

$output->startElement('clear-user-joined-server');
$output->writeAttribute('success', 'yes');
$output->endElement();
}
catch(Exception $e)
{
$output->addErrorElement('clear-user-joined-server', $e->getMessage());
}
break;

default:
$output->addErrorElement('request', 'Invalid action. Action = ' . h($_POST['action']));
break;
Expand Down
91 changes: 51 additions & 40 deletions include/ClientSession.class.php
Expand Up @@ -211,26 +211,28 @@ public function setJoinServerKey(int $server_id, $address, int $port, string $ae
Server::cleanOldServers();
DBConnection::get()->query(
"INSERT INTO `{DB_VERSION}_server_conn`
(`user_id`, `server_id`, `ip`, `port`, `aes_key`, `aes_iv`) VALUES
(:user_id, :server_id, :ip, :port, :aes_key, :aes_iv)
(`user_id`, `server_id`, `ip`, `port`, `aes_key`, `aes_iv`, `connected_since`) VALUES
(:user_id, :server_id, :ip, :port, :aes_key, :aes_iv, :connected_since)
ON DUPLICATE KEY UPDATE `server_id` = :server_id,
`ip` = :ip, `port`= :port, `aes_key` = :aes_key, `aes_iv` = :aes_iv",
`ip` = :ip, `port`= :port, `aes_key` = :aes_key, `connected_since` = :connected_since",
DBConnection::NOTHING,
[
':user_id' => $this->user->getId(),
':server_id' => $server_id,
':ip' => $address,
':port' => $port,
':aes_key' => $aes_key,
':aes_iv' => $aes_iv
':user_id' => $this->user->getId(),
':server_id' => $server_id,
':ip' => $address,
':port' => $port,
':aes_key' => $aes_key,
':aes_iv' => $aes_iv,
':connected_since' => time(),
],
[
':user_id' => DBConnection::PARAM_INT,
':server_id' => DBConnection::PARAM_INT,
':ip' => DBConnection::PARAM_INT,
':port' => DBConnection::PARAM_INT,
':aes_key' => DBConnection::PARAM_STR,
':aes_iv' => DBConnection::PARAM_STR
':user_id' => DBConnection::PARAM_INT,
':server_id' => DBConnection::PARAM_INT,
':ip' => DBConnection::PARAM_INT,
':port' => DBConnection::PARAM_INT,
':aes_key' => DBConnection::PARAM_STR,
':aes_iv' => DBConnection::PARAM_STR,
':connected_since' => DBConnection::PARAM_INT
]
);
}
Expand Down Expand Up @@ -308,39 +310,25 @@ public function getServerConnectionRequests($ip, int $port, int $current_players
]
);

// Get all connection requests 45 seconds before
$timeout = time() - 45;
$connection_requests = DBConnection::get()->query(
"SELECT `user_id`, `server_id`, `ip`, `port`, `aes_key`, `aes_iv`, `username`
FROM `{DB_VERSION}_server_conn`
INNER JOIN `{DB_VERSION}_users`
ON `{DB_VERSION}_server_conn`.user_id = `{DB_VERSION}_users`.id
WHERE `server_id` = :server_id",
WHERE `server_id` = :server_id AND `connected_since` > :timeout",
DBConnection::FETCH_ALL,
[':server_id' => $server_id['id']],
[':server_id' => DBConnection::PARAM_INT]
[
':server_id' => $server_id['id'],
':timeout' => $timeout
],
[
':server_id' => DBConnection::PARAM_INT,
':timeout' => DBConnection::PARAM_INT
]
);

// Set the request bit to zero for all users we fetch
$index = 0;
$parameters = [];
$query_parts = [];
foreach ($connection_requests as $user)
{
$parameter = ":user_id" . $index;
$index++;
$query_parts[] = "`user_id` = " . $parameter;
$parameters[$parameter] = $user['user_id'];
}

if ($index > 0)
{
DBConnection::get()->query(
"DELETE FROM `{DB_VERSION}_server_conn`
WHERE " . implode(" OR ", $query_parts),
DBConnection::ROW_COUNT,
$parameters
);
// TODO Perhaps check if $count and $index are equal
}
}
catch (DBException $e)
{
Expand Down Expand Up @@ -441,6 +429,7 @@ public function clientQuit()

if ($client)
{
$this->clearUserJoinedServer();
if ($client['is_save'] == 1)
{
$this->setOnline(false);
Expand Down Expand Up @@ -701,4 +690,26 @@ public static function cron($seconds, $old_seconds)
throw new ClientSessionException($e->getMessage());
}
}

/**
* Clear the specific joined server of the current user id, called when a client leave wan server in STK
* *
* @throws ClientSessionException
*/
public function clearUserJoinedServer()
{
try
{
DBConnection::get()->delete(
"server_conn",
"`user_id` = :user_id",
[":user_id" => $this->user->getId()],
[":user_id" => DBConnection::PARAM_INT]
);
}
catch (DBException $e)
{
throw new ClientSessionException($e->getMessage());
}
}
}
1 change: 1 addition & 0 deletions install/install.sql
Expand Up @@ -348,6 +348,7 @@ CREATE TABLE IF NOT EXISTS `v3_server_conn` (
`port` SMALLINT UNSIGNED NOT NULL,
`aes_key` CHAR(24) NOT NULL,
`aes_iv` CHAR(24) NOT NULL,
`connected_since` INT NOT NULL,
PRIMARY KEY (`user_id`),
KEY `key_server_id` (`server_id`),
CONSTRAINT `v3_server_conn_ibfk_1` FOREIGN KEY (`server_id`) REFERENCES `v3_servers` (`id`)
Expand Down

0 comments on commit c65280e

Please sign in to comment.