Added comments to better explain the code, and added code to catch more exceptions

master
Aadhavan Srinivasan 11 months ago
parent 6ba3452087
commit 05cc70412c

@ -61,22 +61,45 @@ void check_server_client(int argc, char** argv, Server* server, Client* client)
} }
connect_code = std::string(argv[2]); /* The connect code is a special string, that contains the server address and port. It is given by the server. */ connect_code = std::string(argv[2]); /* The connect code is a special string, that contains the server address and port. It is given by the server. */
addr_port = connect_code::decode(connect_code); addr_port = connect_code::decode(connect_code);
try {
client = new Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1])); client = new Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1]));
} catch (int e) {
throw;
} catch (std::exception& e) {
throw;
}
} }
/* GAME STARTED IN SERVER MODE */ /* GAME STARTED IN SERVER MODE */
else if (strcmp(argv[1],"-S") == 0) { else if (strcmp(argv[1],"-S") == 0) {
std::string addr = "127.0.0.1"; std::string addr;
uint16_t port; uint16_t port;
/* No IP address or port specified */
if (argc < 3) { if (argc < 3) {
throw EXCEPT_TOOFEWARGS;
}
/* IP address but no port */
else if (argc < 4) {
std::cout << "No port specified, using 6500..." << std::endl; std::cout << "No port specified, using 6500..." << std::endl;
addr = std::string(argv[2]);
port = 6500; port = 6500;
} else { } else {
port = std::stoi(std::string(argv[2])); addr = std::string(argv[2]);
port = std::stoi(std::string(argv[3]));
} }
std::string code = connect_code::encode(addr, std::to_string(port)); std::string code = connect_code::encode(addr, std::to_string(port));
std::cout << "Your code is " << code << std::endl; std::cout << "Your code is " << code << std::endl;
try {
server = new Server(4, 'T', addr.data(), port);
} catch (int e) {
throw;
}
catch (std::exception& e) {
throw;
}
} }
else { else {
@ -102,6 +125,17 @@ int main(int argc, char** argv) {
std::cout << "Invalid argument. Optional arguments are -S for server mode, or -C for client mode." << std::endl; std::cout << "Invalid argument. Optional arguments are -S for server mode, or -C for client mode." << std::endl;
return -2; return -2;
} }
if (e == EXCEPT_CONNREFUSED) {
std::cout << "Connection refused. Wrong IP address or port specified." << std::endl;
return -3;
}
if (e == EXCEPT_ADDRNOTAVAIL) {
std:: cout << "Unable to use requested address for server." << std::endl;
return -4;
}
} catch(std::invalid_argument& inv) {
std::cout << inv.what() << std::endl;
return -5;
} }
/* Initialize window and other variables */ /* Initialize window and other variables */

Loading…
Cancel
Save