|
VoltMod
C++23 framework for CS2 server plugins
|
Async database access over Postgres, MariaDB and SQLite. More...
#include <Client.hpp>
Public Types | |
| template<class Fn > | |
| using | ResultOf = std::invoke_result_t< Fn &, SqliteConnection & > |
Public Member Functions | |
| Database (Scheduler &scheduler) | |
| ~Database () | |
| Database (const Database &)=delete | |
| Database & | operator= (const Database &)=delete |
| bool | Connect (const DatabaseConfig &config) |
| void | Disconnect (std::chrono::milliseconds stopDeadline=std::chrono::seconds(5)) |
| template<class Fn > | |
| void | RunAsync (std::string name, Fn fn, std::move_only_function< void(Result< ResultOf< Fn > >)> onDone={}) |
| template<class Fn , class OnValue > requires (!std::is_void_v<ResultOf<Fn>> && std::invocable<OnValue&, ResultOf<Fn>> && !std::invocable<OnValue&, Result<ResultOf<Fn>>>) | |
| void | RunAsync (std::string name, Fn fn, OnValue onValue) |
| template<class Fn > | |
| Result< ResultOf< Fn > > | Run (std::string name, Fn fn) |
| template<class Fn > | |
| ResultOf< Fn > | RunOr (std::string name, Fn fn, ResultOf< Fn > fallback={}) |
| void | DispatchCompletions () |
| bool | IsConnected () const |
| Driver | GetDriver () const |
Async database access over Postgres, MariaDB and SQLite.
One worker thread owns the connection; jobs run in order and their completions run on the game thread. A job is a callable over auto& conn that returns the same type for every driver. Run blocks, so it is for load time; gameplay uses RunAsync.
Definition at line 39 of file Client.hpp.
| using VoltMod::Database::ResultOf = std::invoke_result_t<Fn&, SqliteConnection&> |
What a job returns; MakeJob makes every driver agree.
Definition at line 44 of file Client.hpp.
|
inlineexplicit |
scheduler drives per-frame completion delivery and must outlive this object.
Definition at line 47 of file Client.hpp.
| VoltMod::Database::~Database | ( | ) |
Definition at line 14 of file Client.cpp.
References Disconnect().
| bool VoltMod::Database::Connect | ( | const DatabaseConfig & | config | ) |
Start the worker and ping; false on a bad config or an unreachable database.
Definition at line 19 of file Client.cpp.
References VoltMod::CheckConfig(), Disconnect(), DispatchCompletions(), VoltMod::Log::Error(), VoltMod::Scheduler::EveryFrame(), VoltMod::ReadFile(), and Run().
| void VoltMod::Database::Disconnect | ( | std::chrono::milliseconds | stopDeadline = std::chrono::seconds(5) | ) |
Let queued jobs finish within stopDeadline, then join the worker; later jobs fail and undelivered completions are dropped. The destructor calls it.
Definition at line 55 of file Client.cpp.
References VoltMod::GameThreadQueue::Clear(), VoltMod::ReadFile(), and VoltMod::Subscription::Reset().
Referenced by Connect(), and ~Database().
|
inline |
Run the finished jobs' completions; Connect schedules this every frame.
Definition at line 117 of file Client.hpp.
References VoltMod::GameThreadQueue::RunAll().
Referenced by Connect().
|
inline |
The configured driver, set by Connect.
Definition at line 123 of file Client.hpp.
|
inline |
Whether the worker's last job had a live connection; a diagnostic, not a guarantee.
Definition at line 120 of file Client.hpp.
RunAsync that blocks; load time only.
Definition at line 98 of file Client.hpp.
References VoltMod::ReadFile().
Referenced by Connect().
|
inline |
RunAsync for a callback taking the value alone; a failure is only logged.
Definition at line 79 of file Client.hpp.
References VoltMod::ReadFile(), and RunAsync().
|
inline |
Run fn on the worker; onDone runs on the game thread later. name is a log label.
Definition at line 61 of file Client.hpp.
Referenced by RunAsync().
|
inline |
Run, falling back to fallback when the job fails.
Definition at line 110 of file Client.hpp.