Skip to content

Commit

Permalink
feat: validate port number and add getter methods for game directory,…
Browse files Browse the repository at this point in the history
… IP, and port in ParseArgument class
  • Loading branch information
MasterLaplace committed Dec 9, 2024
1 parent b52e365 commit 3c77008
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Flakkari/ParseArgument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ ParseArgument::ParseArgument(int ac, const char *av[])
else if (std::string(av[i]) == "-port")
{
_port = static_cast<unsigned short>(std::stoi(av[i + 1]));
if (_port < 1024 || _port > 65535)
throw std::runtime_error("Invalid port number, must be between 1024 and 65535");
++i;
}
else if (std::string(av[i]) == "-h" || std::string(av[i]) == "--help")
Expand Down
18 changes: 18 additions & 0 deletions Flakkari/ParseArgument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ class ParseArgument {
*/
~ParseArgument() = default;

/**
* @brief Gets the game directory.
* @return A constant reference to the game directory string.
*/
const std::string &getGameDir() const;

/**
* @brief Gets the IP address.
* @return A constant reference to the IP address string.
*/
const std::string &getIp() const;

/**
* @brief Gets the port number.
* @return The port number as an unsigned short.
*/
unsigned short getPort() const;

private:
std::string _gameDir; ///< The game directory.
std::string _ip; ///< The IP address, default is "localhost".
Expand Down
6 changes: 3 additions & 3 deletions Flakkari/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

int main(int ac, const char *av[])
{
if (ac != 4)
return FLAKKARI_LOG_ERROR("Usage: ./r-type_server <gameDir> <ip> <port>"), 84;
try
{
Flakkari::UDPServer server(av[1], av[2], static_cast<unsigned short>(std::stoi(av[3])));
Flakkari::ParseArgument parseArg(ac, av);

Flakkari::UDPServer server(parseArg.getGameDir(), parseArg.getIp(), parseArg.getPort());
server.run();
}
catch (const std::exception &e)
Expand Down

0 comments on commit 3c77008

Please sign in to comment.