app/game_session.h

frontend-agnostic game orchestrator.

GameSession owns the single play loop (extracted from main.cpp’s runSensorGameLoop) on a worker thread. Callers issue thread-safe commands (newGame / submitHumanMove / stop / restart / shutdown) and observe progress through GameSessionListener. The CLI and the Qt GUI both drive it this way.

Threading: all listener callbacks fire on the worker thread. UI front-ends must marshal them to their own thread (the Qt SessionBridge uses queued signals). See docs/gui-docs/PLAN.md §3.1.

namespace kirin

Enums

enum class SessionState

Values:

enumerator Idle
enumerator Preparing
enumerator EngineThinking
enumerator HumanToMove
enumerator GameOver
enumerator BoardMismatch
enumerator Error
struct BoardMismatchInfo
#include <game_session.h>

Public Members

bool halted = false
std::vector<std::string> missingSquares
std::vector<std::string> extraSquares
std::string detail
int attempt = 0
int maxRetries = 0
struct ClockInfo
#include <game_session.h>

Public Members

int whiteMs = 0
int blackMs = 0
char runningSide = 0
bool enabled = false
struct GameOverInfo
#include <game_session.h>

Public Members

GameResult result
std::string reason
class GameSession
#include <game_session.h>

Public Functions

GameSession(IEngine &engine, IBoardBackend &backend)
~GameSession()
GameSession(const GameSession&) = delete
GameSession &operator=(const GameSession&) = delete
void addListener(GameSessionListener *listener)
void start()
void newGame(const NewGameConfig &config)
void submitHumanMove(const std::string &uci)
void takeback()
void stop()
void restart(ResetMode mode = ResetMode::Auto)
void resetBoard()
void shutdown()
inline SessionState state() const

Private Types

enum class CmdType

Values:

enumerator NewGame
enumerator Stop
enumerator Restart
enumerator ResetBoard
enumerator Shutdown
enum class Interrupt

Values:

enumerator None
enumerator Takeback
enumerator Flag

Private Functions

void run()
void playGame(const NewGameConfig &config)
void resetBoardToStart()
void recordMove(const std::string &uci, int encoded)
void performTakeback()
bool endIfFlagged(char sideOnMove)
void armFlagWatchdog()
void disarmFlagWatchdog()
SearchLimits limitsFor(const SearchLimits &base) const
bool reconcileBoard(int lastMove)
void enqueue(Command cmd)
void enqueueInterrupting(Command cmd)
void setState(SessionState s)
void emitGameStarted(const GameStartInfo&)
void emitMove(const MoveEvent&)
void emitHumanTurn(char side)
void emitGameOver(const GameOverInfo&)
void emitBoardMismatch(const BoardMismatchInfo&)
void emitClock()
void emitTakeback(const TakebackInfo&)
void emitThinking(const ThinkingInfo&)
void emitInfo(const std::string&)
void emitError(const std::string&)

Private Members

IEngine &engine_
IBoardBackend &backend_
std::vector<GameSessionListener*> listeners_
std::thread worker_
std::mutex cmdMutex_
std::condition_variable cmdCv_
std::deque<Command> cmdQueue_
std::atomic<bool> stopRequested_ = {false}
std::atomic<bool> shutdownRequested_ = {false}
std::atomic<SessionState> state_ = {SessionState::Idle}
std::atomic<bool> waitCancel_ = {false}
std::atomic<Interrupt> interrupt_ = {Interrupt::None}
GameClock clock_
std::thread flagWatchdog_
std::mutex watchdogMutex_
std::condition_variable watchdogCv_
std::atomic<bool> watchdogStop_ = {false}
std::vector<PlayedMove> history_
PieceTracker identity_
std::string startFen_
bool takebackable_ = false
NewGameConfig currentConfig_
struct Command

Public Members

CmdType type
NewGameConfig config
struct PlayedMove

Public Members

std::string uci
int encoded = 0
int capturedSlot = SLOT_NONE
int clockWhiteMs = 0
int clockBlackMs = 0
class GameSessionListener
#include <game_session.h>

Subclassed by kirin::GameRecorder, kirin::LedAssist, kirin::NetworkOpponent

Public Functions

virtual ~GameSessionListener() = default
inline virtual void onStateChanged(SessionState)
inline virtual void onGameStarted(const GameStartInfo&)
inline virtual void onMovePlayed(const MoveEvent&)
inline virtual void onHumanTurn(char sideToMove)
inline virtual void onGameOver(const GameOverInfo&)
inline virtual void onBoardMismatch(const BoardMismatchInfo&)
inline virtual void onClockUpdate(const ClockInfo&)
inline virtual void onTakeback(const TakebackInfo&)
inline virtual void onThinking(const ThinkingInfo&)
inline virtual void onInfo(const std::string&)
inline virtual void onError(const std::string&)
struct GameStartInfo
#include <game_session.h>

Public Members

std::string gameId
std::string startFen
bool enginePlaysWhite = true
bool opponentIsRemote = false
std::string opponentName
std::string startedAt
struct MoveEvent
#include <game_session.h>

Public Members

std::string uci
char mover
bool byEngine
std::string fen
struct NewGameConfig
#include <game_session.h>

Public Members

bool enginePlaysWhite = true
EngineOptions options
SearchLimits limits
std::string startFen
ResetMode resetMode = ResetMode::Auto
std::optional<PieceTracker> startIdentity
TimeControl timeControl
bool opponentIsRemote = false
std::string opponentName
std::string gameId
struct TakebackInfo
#include <game_session.h>

Public Members

int plyCount = 0
std::string fen