Skip to content
Merged
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
53 changes: 31 additions & 22 deletions AuthServ/AuthDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <string_theory/stdio>
#include <unordered_map>
#include <chrono>
#include <mutex>

std::thread s_authDaemonThread;
DS::MsgChannel s_authChannel;
Expand All @@ -37,8 +38,6 @@ std::unordered_map<ST::string, SDL::State, ST::hash_i, ST::equal_i> s_globalStat

void dm_auth_addacct(Auth_AddAcct* msg)
{
check_postgres(s_postgres);

DS::PGresultRef result = DS::PQexecVA(s_postgres,
"SELECT idx, \"AcctUuid\" FROM auth.\"Accounts\""
" WHERE LOWER(\"Login\")=LOWER($1)",
Expand Down Expand Up @@ -103,8 +102,6 @@ void dm_auth_shutdown()

void dm_auth_login(Auth_LoginInfo* info)
{
check_postgres(s_postgres);

DEBUG_printf("[Auth] Login U:{} P:{} T:{} O:{}\n",
info->m_acctName, info->m_passHash.toString(),
info->m_token, info->m_os);
Expand Down Expand Up @@ -270,7 +267,6 @@ void dm_auth_disconnect(Auth_ClientMessage* msg)
AuthServer_Private* client = reinterpret_cast<AuthServer_Private*>(msg->m_client);
if (client->m_player.m_playerId) {
// Mark player as offline
check_postgres(s_postgres);
DS::PGresultRef result = DS::PQexecVA(s_postgres,
"UPDATE vault.\"Nodes\" SET"
" \"Int32_1\"=0, \"String64_1\"='',"
Expand All @@ -293,8 +289,6 @@ void dm_auth_disconnect(Auth_ClientMessage* msg)

void dm_auth_setPlayer(Auth_ClientMessage* msg)
{
check_postgres(s_postgres);

AuthServer_Private* client = reinterpret_cast<AuthServer_Private*>(msg->m_client);
DS::PGresultRef result = DS::PQexecVA(s_postgres,
"SELECT \"PlayerName\", \"AvatarShape\", \"Explorer\""
Expand Down Expand Up @@ -903,8 +897,6 @@ void dm_auth_acctFlags(Auth_AccountFlags* msg)

void dm_auth_addAllPlayers(Auth_AddAllPlayers* msg)
{
check_postgres(s_postgres);

if (v_has_node(msg->m_playerId, s_allPlayers)) {
if (!v_unref_node(msg->m_playerId, s_allPlayers)) {
SEND_REPLY(msg, DS::e_NetInternalError);
Expand Down Expand Up @@ -1004,20 +996,8 @@ void dm_auth_update_globalSDL(Auth_UpdateGlobalSDL* msg)
SEND_REPLY(msg, DS::e_NetInvalidParameter);
}

void dm_authDaemon()
void dm_authInit()
{
s_postgres = PQconnectdb(ST::format(
"host='{}' port='{}' user='{}' password='{}' dbname='{}'",
DS::Settings::DbHostname(), DS::Settings::DbPort(),
DS::Settings::DbUsername(), DS::Settings::DbPassword(),
DS::Settings::DbDbaseName()).c_str());
if (PQstatus(s_postgres) != CONNECTION_OK) {
ST::printf(stderr, "Error connecting to postgres: {}", PQerrorMessage(s_postgres));
PQfinish(s_postgres);
s_postgres = nullptr;
return;
}

if (!dm_vault_init()) {
fputs("[Auth] Vault failed to initialize\n", stderr);
return;
Expand Down Expand Up @@ -1045,11 +1025,40 @@ void dm_authDaemon()
PQ_PRINT_ERROR(s_postgres, UPDATE);
// This doesn't block continuing...
}
}

void dm_authCheck(bool reconnect)
{
if (reconnect)
check_postgres(s_postgres);
if (PQstatus(s_postgres) != CONNECTION_OK)
return;

static std::once_flag s_authInit;
std::call_once(s_authInit, dm_authInit);
}

void dm_authDaemon()
{
s_postgres = PQconnectdb(ST::format(
"host='{}' port='{}' user='{}' password='{}' dbname='{}'",
DS::Settings::DbHostname(), DS::Settings::DbPort(),
DS::Settings::DbUsername(), DS::Settings::DbPassword(),
DS::Settings::DbDbaseName()).c_str());
if (PQstatus(s_postgres) != CONNECTION_OK)
ST::printf(stderr, "Error connecting to postgres: {}", PQerrorMessage(s_postgres));

// If the connection to postgres was successful, initialize the vault here.
dm_authCheck(false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's still probably room for improvement here -- for example, if we failed due to an invalid DB config rather than a server issue, we'll just keep spitting out errors on every incoming message and hope the user eventually notices :). But not dropping the thread and continuing silently is a definite improvement, so +1 from me.


for ( ;; ) {
DS::FifoMessage msg { -1, nullptr };
try {
msg = s_authChannel.getMessage();

// We have a message from a client. Make sure the vault is ready.
dm_authCheck(true);

switch (msg.m_messageType) {
case e_AuthShutdown:
dm_auth_shutdown();
Expand Down