Finished integrating check_server() and check_client(), check_server_client() has been commented out.
This commit is contained in:
103
main.cpp
103
main.cpp
@@ -32,7 +32,7 @@
|
|||||||
#include "includes/server.hpp"
|
#include "includes/server.hpp"
|
||||||
#include "includes/exception_consts.hpp"
|
#include "includes/exception_consts.hpp"
|
||||||
#include "includes/check_input.hpp"
|
#include "includes/check_input.hpp"
|
||||||
#include "includes/raygui_helpers.hpp"
|
#include "includes/display_text.hpp"
|
||||||
#include "includes/easysock.h"
|
#include "includes/easysock.h"
|
||||||
#include "includes/serialization.h"
|
#include "includes/serialization.h"
|
||||||
#include "includes/timer.h"
|
#include "includes/timer.h"
|
||||||
@@ -52,11 +52,11 @@ const float BASE_SPEED = sqrt(powf(BASE_SPEED_COMPONENTS, 2) * 2);
|
|||||||
std::string HELP_TEXT = "\nnetpong - A networked pong game for the internet era.\n"
|
std::string HELP_TEXT = "\nnetpong - A networked pong game for the internet era.\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Usage: \n"
|
"Usage: \n"
|
||||||
"netpong [MODE] [ADDRESS|CODE]\n"
|
"netpong [MODE] [ADDRESS PORT]|[CODE]\n"
|
||||||
"\n"
|
"\n"
|
||||||
"MODE: \n"
|
"MODE: \n"
|
||||||
"-S : Server mode. Starts a server to allow the other player to connect.\n"
|
"-S : Server mode. Starts a server to allow the other player to connect.\n"
|
||||||
"IP address must be specified. Port is optional, 6500 is used as default.\n"
|
"IP address and port must be specified.\n"
|
||||||
"\n"
|
"\n"
|
||||||
"-C: Client mode. Connects to a server, using the provided connection code.\n"
|
"-C: Client mode. Connects to a server, using the provided connection code.\n"
|
||||||
"\n"
|
"\n"
|
||||||
@@ -97,18 +97,24 @@ raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
|
|||||||
/* Checks the number and type of the command-line arguments. Throws an exception
|
/* Checks the number and type of the command-line arguments. Throws an exception
|
||||||
if the args are invalid. DOES NOT PROCESS VALID ARGUMENTS. */
|
if the args are invalid. DOES NOT PROCESS VALID ARGUMENTS. */
|
||||||
void check_num_args(int argc, char** argv) {
|
void check_num_args(int argc, char** argv) {
|
||||||
if (argc > 3) {
|
if (argc > 4) {
|
||||||
throw std::invalid_argument("ARGUMENT ERROR: Too many arguments. To view syntax, use -h or --help.");
|
throw std::invalid_argument("ARGUMENT ERROR: Too many arguments. To view syntax, use -h or --help.");
|
||||||
}
|
}
|
||||||
if (argc > 1) { // Either server or client mode
|
if (argc > 1) { // Either server or client mode
|
||||||
if (std::string(argv[1]) == "-S" && argc < 3) { // Server mode but no address
|
if (std::string(argv[1]) == "-S") {
|
||||||
throw std::invalid_argument("ARGUMENT ERROR: Server mode specified without any address.");
|
if (argc < 4) { // Server mode but no address and/or port
|
||||||
|
throw std::invalid_argument("ARGUMENT ERROR: Server mode specified without any address or port.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (std::string(argv[1]) == "-C" && argc < 3) { // Client mode but no code
|
else if (std::string(argv[1]) == "-C") {
|
||||||
throw std::invalid_argument("ARGUMENT ERRROR: Client mode specified without any code.");
|
if (argc < 3) { // Client mode but no code
|
||||||
|
throw std::invalid_argument("ARGUMENT ERRROR: Client mode specified without any code.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (std::string(argv[1]) == "-h" || std::string(argv[1]) == "--help") {
|
else if (std::string(argv[1]) == "-h" || std::string(argv[1]) == "--help") {
|
||||||
throw new std::invalid_argument(HELP_TEXT); // I am abusing the exception mechanism here, so that I can ensure that the caller quits the program.
|
throw std::invalid_argument(HELP_TEXT); // I am abusing the exception mechanism here, so that I can ensure that the caller quits the program after printing the help message.
|
||||||
|
} else {
|
||||||
|
throw std::invalid_argument("Unrecognized argument.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -218,7 +224,8 @@ GameType check_server_client(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
/* Check the number of command-line arguments */
|
/* Check the number and validity of command-line arguments. Invalid arguments
|
||||||
|
will throw an exception. */
|
||||||
try {
|
try {
|
||||||
check_num_args(argc, argv);
|
check_num_args(argc, argv);
|
||||||
} catch (std::invalid_argument& inv) {
|
} catch (std::invalid_argument& inv) {
|
||||||
@@ -227,43 +234,43 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* From here on, we assume that:
|
/* From here on, we assume that:
|
||||||
a. The program was started in single player mode, OR
|
a. The program was started with no arguments (User is prompted in GUI), OR
|
||||||
b. The program was started in server mode, and an address was given, OR
|
b. The program was started in server mode, and an additional was given, OR
|
||||||
c. The program was started in client mode, and a code was given. */
|
c. The program was started in client mode, and an additional argument was given. */
|
||||||
|
|
||||||
/* Check if game was started in server or client mode, and set appropriate variables */
|
|
||||||
|
|
||||||
/* GameType struct, to define whether the game is in single or multi-player mode, and
|
/* GameType struct, to define whether the game is in single or multi-player mode, and
|
||||||
to hold the appropriate socket */
|
to hold the appropriate socket */
|
||||||
GameType type;
|
GameType type;
|
||||||
|
|
||||||
try {
|
/* Check if game was started in server or client mode, and call the appropriate function to process the arguments.
|
||||||
type = check_server_client(argc, argv);
|
If game was started in single-player mode (i.e. with no arguments), then the user is prompted in the GUI. */
|
||||||
} catch(int e) {
|
try { // I put this try-catch block outside the if-statement because the exception handling is the same for both client and server.
|
||||||
if (e == EXCEPT_TOOFEWARGS) {
|
if (argc > 1) { // Server or client mode
|
||||||
std::cout << "Started in client mode, but no address was specified." << std::endl;
|
if (std::string(argv[1]) == "-S") { // Server mode
|
||||||
return -1;
|
type = check_server(argv[2], argv[3], IF_CLI);
|
||||||
}
|
}
|
||||||
if (e == EXCEPT_INVALIDARGS) {
|
if (std::string(argv[1]) == "-C") { // Client mode
|
||||||
std::cout << "Invalid argument." << std::endl;
|
type = check_client(argv[2], IF_CLI);
|
||||||
return -2;
|
}
|
||||||
}
|
}
|
||||||
if (e == EXCEPT_INVALIDIP) {
|
} catch (std::invalid_argument& inv) {
|
||||||
std::cout << "Invalid IP address provided." << std::endl;
|
|
||||||
return -5;
|
|
||||||
}
|
|
||||||
if (e == EXCEPT_WRONGRESPONSE) {
|
|
||||||
std::cout << "The server didn't respond with the correct message. Are you sure you have used the right server?" << std::endl;
|
|
||||||
return -6;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::cout << strerror(e) << std::endl;
|
|
||||||
return -7;
|
|
||||||
}
|
|
||||||
} catch(std::invalid_argument& inv) {
|
|
||||||
std::cout << inv.what() << std::endl;
|
std::cout << inv.what() << std::endl;
|
||||||
return -8;
|
} catch (int err) {
|
||||||
}
|
std::cout << strerror(err) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
//try {
|
||||||
|
//type = check_server_client(argc, argv);
|
||||||
|
//} catch(int e) {
|
||||||
|
|
||||||
|
//else {
|
||||||
|
//std::cout << strerror(e) << std::endl;
|
||||||
|
//return -7;
|
||||||
|
//}
|
||||||
|
//} catch(std::invalid_argument& inv) {
|
||||||
|
//std::cout << inv.what() << std::endl;
|
||||||
|
//return -8;
|
||||||
|
//}
|
||||||
|
|
||||||
/* Initialize window and other variables */
|
/* Initialize window and other variables */
|
||||||
SetTraceLogLevel(LOG_NONE);
|
SetTraceLogLevel(LOG_NONE);
|
||||||
@@ -371,14 +378,14 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
type = check_server(ip_text, port_text);
|
type = check_server(ip_text, port_text, IF_GUI);
|
||||||
} catch (int e) {
|
} catch (int e) {
|
||||||
display_and_exit(std::string(std::strerror(e)) + "\nClosing game...", 2); // The server constructor throws the errno if it cannot create a socket
|
display_and_exit_raygui(std::string(std::strerror(e)) + "\nClosing game...", 2); // The server constructor throws the errno if it cannot create a socket
|
||||||
free(ip_text);
|
free(ip_text);
|
||||||
free(port_text);
|
free(port_text);
|
||||||
return -1;
|
return -1;
|
||||||
} catch (std::invalid_argument& inv) {
|
} catch (std::invalid_argument& inv) {
|
||||||
display_and_exit(std::string(inv.what()) + "\nClosing game...", 2);
|
display_and_exit_raygui(std::string(inv.what()) + "\nClosing game...", 2);
|
||||||
free(ip_text);
|
free(ip_text);
|
||||||
free(port_text);
|
free(port_text);
|
||||||
return -1;
|
return -1;
|
||||||
@@ -411,12 +418,12 @@ int main(int argc, char** argv) {
|
|||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
type = check_client(code_text);
|
type = check_client(code_text, IF_GUI);
|
||||||
} catch (int e) {
|
} catch (int e) {
|
||||||
display_and_exit(std::string(std::strerror(e)) + "\nClosing game...", 2); // The client constructor throws the errno if it cannot create a socket
|
display_and_exit_raygui(std::string(std::strerror(e)) + "\nClosing game...", 2); // The client constructor throws the errno if it cannot create a socket
|
||||||
return -1;
|
return -1;
|
||||||
} catch (std::invalid_argument& inv) {
|
} catch (std::invalid_argument& inv) {
|
||||||
display_and_exit(std::string(inv.what()) + "\nClosing game...", 2);
|
display_and_exit_raygui(std::string(inv.what()) + "\nClosing game...", 2);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
free(code_text);
|
free(code_text);
|
||||||
|
Reference in New Issue
Block a user