Started working on better error handling, by throwing exceptions and displaying error messages in the GUI
This commit is contained in:
@@ -1,38 +1,32 @@
|
||||
#include <iostream>
|
||||
#include "includes/easysock.hpp"
|
||||
#include "includes/easysock.h"
|
||||
#include "includes/connect_code.hpp"
|
||||
#include "includes/server.hpp"
|
||||
#include "includes/client.hpp"
|
||||
#include "includes/check_input.hpp"
|
||||
#include "includes/raygui/raygui.h"
|
||||
#include "includes/exception_consts.hpp"
|
||||
#include "includes/raygui_helpers.hpp"
|
||||
#include "includes/timer.h"
|
||||
|
||||
/* Display the given text, centered on the screen, as a label */
|
||||
void display_text_centered(std::string to_disp) {
|
||||
const char* to_disp_cstr = to_disp.c_str();
|
||||
Vector2 label_size = MeasureTextEx(GetFontDefault(), to_disp_cstr, GuiGetStyle(DEFAULT, TEXT_SIZE)+1, GuiGetStyle(DEFAULT, TEXT_SPACING)+1); // The '+1' is there to account for any rounding errors
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(BLACK);
|
||||
GuiLabel(Rectangle{(GetScreenWidth()/2) - (label_size.x/2), (GetScreenHeight()/2) - (label_size.y/2), label_size.x, label_size.y}, to_disp_cstr);
|
||||
EndDrawing();
|
||||
return;
|
||||
}
|
||||
|
||||
GameType check_server(char* ip_text, char* port_text) {
|
||||
GameType type;
|
||||
std::string addr;
|
||||
uint16_t port;
|
||||
|
||||
/* Check if IP address and port are in valid forms */
|
||||
if (check_ip_ver(ip_text) < 0) {
|
||||
throw std::invalid_argument("Invalid IP address");
|
||||
}
|
||||
if (port_to_num(port_text) < 0) {
|
||||
throw std::invalid_argument("Invalid port");
|
||||
}
|
||||
|
||||
/* From here on, we assume that the IP and port are valid */
|
||||
|
||||
addr = std::string(ip_text);
|
||||
port = std::stoi(std::string(port_text));
|
||||
|
||||
/* Check if IP is valid */
|
||||
if (check_ip_ver(addr.data()) < 0) {
|
||||
throw EXCEPT_INVALIDIP;
|
||||
}
|
||||
|
||||
std::string code = connect_code::encode(addr, std::to_string(port));
|
||||
|
||||
/* Create server socket and wait for client to connect */
|
||||
|
17
main.cpp
17
main.cpp
@@ -26,11 +26,12 @@
|
||||
#include "includes/paddle.hpp"
|
||||
#include "includes/ball.hpp"
|
||||
#include "includes/connect_code.hpp"
|
||||
#include "includes/easysock.hpp"
|
||||
#include "includes/client.hpp"
|
||||
#include "includes/server.hpp"
|
||||
#include "includes/exception_consts.hpp"
|
||||
#include "includes/check_input.hpp"
|
||||
#include "includes/raygui_helpers.hpp"
|
||||
#include "includes/easysock.h"
|
||||
#include "includes/serialization.h"
|
||||
#include "includes/timer.h"
|
||||
|
||||
@@ -272,7 +273,7 @@ int main(int argc, char** argv) {
|
||||
/* Server mode, ask user to input IP address and port */
|
||||
if (selected_item == M_SERVER) {
|
||||
button_pressed = false; // Whether submit button is pressed
|
||||
char* ip_text = (char *)calloc(100, sizeof(char)); // Holds input of IP text box
|
||||
char* ip_text = (char *)calloc(150, sizeof(char)); // Holds input of IP text box
|
||||
char* port_text = (char *)calloc(20, sizeof(char)); // Holds input of port text box
|
||||
const char* ip_label = "Local IP address";
|
||||
const char* port_label = "Port number (1024 - 65535)";
|
||||
@@ -303,14 +304,22 @@ int main(int argc, char** argv) {
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
type = check_server(ip_text, port_text);
|
||||
try {
|
||||
type = check_server(ip_text, port_text);
|
||||
} catch (std::invalid_argument& inv) {
|
||||
display_text_centered(std::string(inv.what()) + "\nClosing game...");
|
||||
Timer timer = timer_init(2); // Wait for two seconds
|
||||
while (!timer_done(timer));
|
||||
CloseWindow(); // Close and exit
|
||||
return -1;
|
||||
}
|
||||
free(ip_text);
|
||||
free(port_text);
|
||||
}
|
||||
|
||||
if (selected_item == M_CLIENT) {
|
||||
button_pressed = false; // Whether submit button is pressed
|
||||
char* code_text = (char *)calloc(100, sizeof(char)); // Holds the connect code
|
||||
char* code_text = (char *)calloc(150, sizeof(char)); // Holds the connect code
|
||||
const char* code_label = "Enter code:";
|
||||
bool editing_code = false; // Indicates whether the port text box is being edited
|
||||
while (button_pressed == false || ((strlen(code_text) == 0))) {
|
||||
|
Reference in New Issue
Block a user