Filled out implementation of check_client function

master
Aadhavan Srinivasan 10 months ago
parent 9e0990156e
commit 07ac3f9166

@ -6,17 +6,17 @@
#include "includes/check_input.hpp" #include "includes/check_input.hpp"
#include "includes/raygui/raygui.h" #include "includes/raygui/raygui.h"
#include "includes/exception_consts.hpp" #include "includes/exception_consts.hpp"
#include "includes/timer.h"
/* Display the given text, centered on the screen, as a label */ /* Display the given text, centered on the screen, as a label */
void display_text_centered(std::string to_disp) { void display_text_centered(std::string to_disp) {
const char* to_disp_cstr = to_disp.c_str(); const char* to_disp_cstr = to_disp.c_str();
Vector2 label_size = MeasureTextEx(GetFontDefault(), to_disp_cstr, GuiGetStyle(DEFAULT, TEXT_SIZE), GuiGetStyle(DEFAULT, TEXT_SPACING)); 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(); BeginDrawing();
ClearBackground(BLACK); 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); GuiLabel(Rectangle{(GetScreenWidth()/2) - (label_size.x/2), (GetScreenHeight()/2) - (label_size.y/2), label_size.x, label_size.y}, to_disp_cstr);
EndDrawing(); EndDrawing();
return; return;
} }
@ -34,24 +34,56 @@ GameType check_server(char* ip_text, char* port_text) {
} }
std::string code = connect_code::encode(addr, std::to_string(port)); std::string code = connect_code::encode(addr, std::to_string(port));
display_text_centered("Your code is " + code);
/* Create server socket and wait for client to connect */ /* Create server socket and wait for client to connect */
Server* server = new Server(4, ES_UDP, addr.data(), port); Server* server = new Server(4, ES_UDP, addr.data(), port);
server->create_socket(); server->create_socket();
std::cout << "Waiting for connection..." << std::endl; display_text_centered("Your code is " + code + "\nWaiting for connection...");
std::string response = ""; std::string response = "";
char* temp_response = NULL; char* temp_response = NULL;
/* Wait for the right client to connect. Since recvAll returns a char*, we need to create a temporary variable to check for NULL. */ /* Wait for the client to connect. Since recvAll returns a char*, we need to create a temporary variable to check for NULL.
TODO - Check that the client actually sends 'GG'. */
do { do {
temp_response = server->recvAll(); temp_response = server->recvAll();
} while (temp_response == NULL); } while (temp_response == NULL);
response = std::string(temp_response); response = std::string(temp_response);
std::cout << "Connection received from " << server->get_peer_addr() << std::endl;
server->sendAll("U2"); server->sendAll("U2");
display_text_centered("Connection received from " + server->get_peer_addr());
Timer timer = timer_init(3);
while (!timer_done(timer)); // Wait for five seconds
type.mode = M_SERVER; type.mode = M_SERVER;
type.netsock = server; type.netsock = server;
return type; return type;
} }
GameType check_client(char* code_text) {
GameType type;
std::vector<std::string> addr_port;
std::string connect_code = std::string(code_text); /* The connect code is a special string, that contains the server address and port. It is given by the server. */
try {
addr_port = connect_code::decode(connect_code);
Client* client = new Client(4, ES_UDP, addr_port[0].data(), std::stoi(addr_port[1]));
client->create_socket();
/* Send a specific message to the server, and wait for the appropriate response, to know that the server is ready */
client->sendAll("GG");
// display_text_centered("Connecting...");
std::string msg_from_server = client->recvAll();
if (msg_from_server == "U2") {
display_text_centered("Connection made");
Timer timer = timer_init(3);
while (!timer_done(timer));
} else {
throw EXCEPT_WRONGRESPONSE;
}
type.mode = M_CLIENT;
type.netsock = client;
return type;
} catch (int e) {
throw;
} catch (std::exception& e) {
throw;
}
}

@ -17,6 +17,7 @@ typedef struct {
/* This function checks the IP address and port passed to it, and returns a struct, /* This function checks the IP address and port passed to it, and returns a struct,
that contains information about the game mode, and contains the server socket. that contains information about the game mode, and contains the server socket.
It assumes that both ip_text and port_text are non-null
TODO - Add better error checking. */ TODO - Add better error checking. */
GameType check_server(char* ip_text, char* port_text); GameType check_server(char* ip_text, char* port_text);

Loading…
Cancel
Save