Created an implementation and header file to check the user input, if it is entered through the GUI
parent
bc0d644399
commit
7812611fe6
@ -0,0 +1,57 @@
|
||||
#include <iostream>
|
||||
#include "includes/easysock.hpp"
|
||||
#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"
|
||||
|
||||
/* 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), GuiGetStyle(DEFAULT, TEXT_SPACING));
|
||||
|
||||
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;
|
||||
|
||||
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));
|
||||
display_text_centered("Your code is " + code);
|
||||
|
||||
/* Create server socket and wait for client to connect */
|
||||
Server* server = new Server(4, ES_UDP, addr.data(), port);
|
||||
server->create_socket();
|
||||
std::cout << "Waiting for connection..." << std::endl;
|
||||
std::string response = "";
|
||||
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. */
|
||||
do {
|
||||
temp_response = server->recvAll();
|
||||
} while (temp_response == NULL);
|
||||
response = std::string(temp_response);
|
||||
|
||||
std::cout << "Connection received from " << server->get_peer_addr() << std::endl;
|
||||
server->sendAll("U2");
|
||||
type.mode = M_SERVER;
|
||||
type.netsock = server;
|
||||
return type;
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
#ifndef _CHECK_INPUT_H
|
||||
#define _CHECK_INPUT_H
|
||||
|
||||
#include "includes/sock.hpp"
|
||||
|
||||
typedef enum {M_SINGLE, M_CLIENT, M_SERVER} Mode;
|
||||
|
||||
/* This struct contains a Mode enum, which indicates the type of game we are
|
||||
playing (Single player, client mode or server mode). The netsock parameter is
|
||||
a 'Sock' object - Client and Server classes inherit from this object, so this
|
||||
parameter can be instantiated to either a client or server, depending on the
|
||||
game type. */
|
||||
typedef struct {
|
||||
Mode mode;
|
||||
Sock* netsock;
|
||||
} GameType;
|
||||
|
||||
/* 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.
|
||||
TODO - Add better error checking. */
|
||||
GameType check_server(char* ip_text, char* port_text);
|
||||
|
||||
/* NOT IMPLEMENTED YET - This function checks the code given to it, and returns a struct
|
||||
that contains information about the game mode, and contains the client socket. */
|
||||
GameType check_client(char* code);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue