Files
Maze/src/server/server_main.cpp
2025-05-01 01:27:21 +03:00

71 lines
2.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*! @file server_main.cpp
Главный файл сервера для игры в лабиринт на базе сокетов.
@author ParkSuMin
@date 2025.04.30 */
#include "server.hpp"
/*! Главная функция сервера.
@details Обрабатывает аргументы командной строки, создаёт экземпляр сервера и запускает его.
@throw std::runtime_error При ошибке работы сервера.
@throw std::exception При непредвиденной ошибке.
@dot
digraph main {
ranksep=0.25;
node [shape=box,fontsize="10",fixedsize=true,width=2,height=0.3]
edge [arrowsize=0.5]
Beg [label="Начало",shape=ellipse]
End [label="Конец",shape=ellipse]
A [label="Проверка аргументов"]
B [label="Создание сервера"]
C [label="Запуск сервера"]
D [label="Обработка исключений"]
Beg->A->B->C->End
B->D
C->D->End
}
@enddot */
int main(int argc, char **argv) {
int opt;
std::string host = "localhost";
int steps = 10;
bool service_mode = false;
short unsigned port = 1024u;
while ((opt = getopt(argc, argv, "h:p:sn:")) != -1) {
switch (opt) {
case 'h':
host = optarg;
break;
case 'p':
port = static_cast<unsigned short>(atoi(optarg));
break;
case 'n':
steps = atoi(optarg);
if(steps <= 0) {
std::cerr << "Invalid steps" << std::endl;
return 1;
}
break;
case 's':
service_mode = true;
break;
default:
break;
}
}
try {
Server server(host, port);
server.start(steps, service_mode);
} catch (const std::runtime_error& e){
std::cerr << "Server application error: " << e.what() << std::endl;
return 1;
} catch (...){
std::cerr << "Unexpected error in client application" << std::endl;
return 2;
}
std::cout << "Server application finished" << std::endl;
return 0;
}