diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-03-12 21:23:17 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-03-20 23:44:59 +0800 |
| commit | 03bce7d61a64fce9c54ae25e2e2a8543d82cc3bc (patch) | |
| tree | a18a96b0b81b79509ec7358668e5bce5758ebf30 /src/main.cpp | |
| parent | 9a73401361f8be274536164d42ba10ace050f982 (diff) | |
feat!: require arg for server port
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/main.cpp b/src/main.cpp index 115ad53..0ecd727 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,9 @@ #include "chatServer.hpp" #include "log.hpp" +#include <algorithm> #include <csignal> #include <unistd.h> +#include <vector> static void handleSignal(int sig) { signal(sig, SIG_DFL); @@ -27,11 +29,49 @@ static void registerSignals() { signal(SIGPIPE, SIG_IGN); } +static void help() { + log(ERR, "usage: chat-server <port>"); +} + // TODO: ARGC, ARGV. the CServer ctor should take a port number -int main() { +int main(int argc, char *argv[]) { + std::vector<std::string> args{argv + 1, argv + argc}; + if (args.empty()) { + help(); + return 1; + } + uint16_t port = 0; + for (const auto &arg : args) { + if (arg == "-h" || arg == "--help") { + help(); + return 0; + } else if (std::ranges::all_of(arg, ::isdigit)) { + port = std::stoi(arg); + if (std::stoi(arg) < 0) { + log(ERR, "Port number must be greater than 0"); + + return 1; + } else if (std::stoi(arg) > 65535) { + log(ERR, "Port number must be less than 65536"); + + return 1; + } + + port = std::stoi(arg); + log(LOG, "Port number: {}", port); + } + } + + if (port == 0) { + log(ERR, "Port number is required"); + + help(); + return 1; + } + registerSignals(); - g_pChatServer = std::make_unique<CChatServer>(); + g_pChatServer = std::make_unique<CChatServer>(port); g_pChatServer->start(); // died |
