/
Cneon90
/
xServer
Обзор
Документация
Войти
/
Cneon90
/
xServer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
Console/console_command_handler.cpp
148 строк
5 KB
Кирилл
add git files
28 май 2026, 11:55
28 май 2026, 11:55
bfdd09a
Код
Авторство
О чём код?
#include "console_command_handler.h" ConsoleCommandHandler::ConsoleCommandHandler(TVZ_Diagnostic_Tool_Server& server, QObject* parent) : QObject(parent) , m_server(server) , m_stdinStream(std::make_unique<QTextStream>(stdin)) , m_isRunning(false) { m_inputTimer.setInterval(100); // Проверяем ввод каждые 100 мс connect(&m_inputTimer, &QTimer::timeout, this, &ConsoleCommandHandler::checkInput); } ConsoleCommandHandler::~ConsoleCommandHandler() { stop(); } void ConsoleCommandHandler::start() { if (!m_isRunning) { m_isRunning = true; m_inputTimer.start(); std::cout << "Console command handler started. Type '/help' for available commands." << std::endl; std::cout.flush(); } } void ConsoleCommandHandler::stop() { if (m_isRunning) { m_inputTimer.stop(); m_isRunning = false; } } void ConsoleCommandHandler::checkInput() { if (!m_isRunning) return; if (m_stdinStream->atEnd()) { // Нет данных для чтения return; } QString line = m_stdinStream->readLine().trimmed(); if (!line.isEmpty()) { processCommand(line); } } void ConsoleCommandHandler::processCommand(const QString& command) { qDebug() << "Handler process command" << command; if (command.startsWith('/')) { if (command == "/help" || command == "/h" || command == "/?") { printHelp(); } else if (command == "/clients" || command == "/c") { printClientsInfo(); } else if (command == "/stats" || command == "/s") { printServerStats(); } else if (command == "/quit" || command == "/q" || command == "/exit") { std::cout << "Shutting down server..." << std::endl; std::cout.flush(); QCoreApplication::quit(); } else if (command == "/clear" || command == "/cls") { // Очистка консоли (работает в Windows и Unix-like) #ifdef _WIN32 system("cls"); #else system("clear"); #endif } else { std::cout << "Unknown command: " << command.toStdString() << std::endl; std::cout << "Type '/help' for available commands." << std::endl; std::cout.flush(); } } else { // Если команда не начинается с '/', просто игнорируем std::cout << "Commands must start with '/'. Type '/help' for help." << std::endl; std::cout.flush(); } } void ConsoleCommandHandler::printHelp() { std::cout << "\n=== Available Commands ===" << std::endl; std::cout << "/help, /h, /? - Show this help message" << std::endl; std::cout << "/clients, /c - Show number of connected clients" << std::endl; std::cout << "/stats, /s - Show server statistics" << std::endl; std::cout << "/clear, /cls - Clear console screen" << std::endl; std::cout << "/quit, /q, /exit - Shutdown server and exit" << std::endl; std::cout << "==========================\n" << std::endl; std::cout.flush(); } void ConsoleCommandHandler::printClientsInfo() { // Получаем информацию о клиентах из сервера // Предполагаем, что у TVZ_Diagnostic_Tool_Server есть методы для работы с клиентами // Вам нужно будет реализовать эти методы в классе сервера int clientCount = m_server.getConnectedClientsCount(); // Нужно добавить этот метод QStringList clientList = m_server.getClientList(); // Нужно добавить этот метод std::cout << "\n=== Connected Clients ===" << std::endl; std::cout << "Total clients: " << clientCount << std::endl; if (clientCount > 0) { std::cout << "\nClient details:" << std::endl; for (const QString& client : clientList) { std::cout << " - " << client.toStdString() << std::endl; } } std::cout << "========================\n" << std::endl; std::cout.flush(); } void ConsoleCommandHandler::printServerStats() { // Получаем статистику сервера // Вам нужно будет добавить соответствующие методы в класс сервера int clientCount = m_server.getConnectedClientsCount(); qint64 totalBytesReceived = m_server.getTotalBytesReceived(); // Нужно добавить qint64 totalBytesSent = m_server.getTotalBytesSent(); // Нужно добавить quint64 uptimeSeconds = m_server.getUptimeSeconds(); // Нужно добавить std::cout << "\n=== Server Statistics ===" << std::endl; std::cout << "Connected clients: " << clientCount << std::endl; std::cout << "Total bytes received: " << totalBytesReceived << std::endl; std::cout << "Total bytes sent: " << totalBytesSent << std::endl; if (uptimeSeconds > 0) { int hours = uptimeSeconds / 3600; int minutes = (uptimeSeconds % 3600) / 60; int seconds = uptimeSeconds % 60; std::cout << "Uptime: " << hours << "h " << minutes << "m " << seconds << "s" << std::endl; } std::cout << "========================\n" << std::endl; std::cout.flush(); }