Compare commits
4 Commits
9972e146d5
...
9a12edcdb1
| Author | SHA1 | Date | |
|---|---|---|---|
| 9a12edcdb1 | |||
| 83a0d5beb4 | |||
| eeae444b1d | |||
| f9d5e8cdeb |
@@ -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 */
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "includes/client.hpp"
|
||||
#include "includes/exception_consts.hpp"
|
||||
#include "includes/sock.hpp"
|
||||
#include "includes/easysock.hpp"
|
||||
#include "includes/easysock.h"
|
||||
|
||||
/* Destructor - closes any open sockets */
|
||||
Client::~Client() {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "includes/easysock.hpp"
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cerrno>
|
||||
#include "includes/easysock.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#ifndef _WIN32
|
||||
@@ -187,6 +188,29 @@ int check_ip_ver(const char* address) {
|
||||
}
|
||||
}
|
||||
|
||||
int port_to_num(const char* port_str) {
|
||||
/* The largest possible port value is 65535: a 5 character string */
|
||||
if (strlen(port_str) > 5) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < strlen(port_str); i++) {
|
||||
if (isdigit(port_str[i]) == 0) { // Ensure that every character in port_str is a digit (isidigit() returns 0 if the parameter is not a digit)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* Convert the string to a base-10 integer */
|
||||
int port_num = (int)strtol(port_str, NULL, 10);
|
||||
if (port_num > 65535) {
|
||||
return -1;
|
||||
}
|
||||
if (port_num < 1024) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
return port_num;
|
||||
}
|
||||
|
||||
int int_to_inet(int network) {
|
||||
if (network == 4) {
|
||||
return AF_INET;
|
||||
@@ -1,5 +1,8 @@
|
||||
#ifndef EASYSOCK_HPP_
|
||||
#define EASYSOCK_HPP_
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#ifndef EASYSOCK_H_
|
||||
#define EASYSOCK_H_
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
@@ -70,6 +73,12 @@ IPv6 address (returns 6) or neither (returns -1). */
|
||||
|
||||
int check_ip_ver(const char* address);
|
||||
|
||||
/* port_to_num - Converts a string representing a port, into a numeric value.
|
||||
Returns -1 if the string is not numeric, or exceeds the maximum port length.
|
||||
Returns -2 if the string is lower than 1024, This serves as a warning, as ports less
|
||||
than 1023 are reserved. */
|
||||
int port_to_num(const char* port_str);
|
||||
|
||||
/* int_to_inet - Takes an int value (4 for IPv4, 6 for IPv6) and returns AF_INET or
|
||||
AF_INET6 respectively. */
|
||||
|
||||
@@ -92,3 +101,6 @@ int sock_quit(void);
|
||||
int sock_close(SOCKET);
|
||||
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
9
includes/raygui_helpers.hpp
Normal file
9
includes/raygui_helpers.hpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef _RAYGUI_HELPERS_HPP
|
||||
#define _RAYGUI_HELPERS_HPP
|
||||
#include <string>
|
||||
|
||||
/* Display the given text, centered on the screen, as a label.
|
||||
NEEDS RAYGUI LIBRARY. */
|
||||
void display_text_centered(std::string to_disp);
|
||||
|
||||
#endif
|
||||
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))) {
|
||||
|
||||
@@ -33,7 +33,7 @@ if build_machine.system() == 'windows'
|
||||
endif
|
||||
|
||||
executable('pong',
|
||||
'main.cpp', 'easysock.cpp', 'sock.cpp','paddle.cpp', 'ball.cpp', 'numeric_base.cpp', 'connect_code.cpp', 'server.cpp', 'client.cpp', 'check_input.cpp',
|
||||
'serialization.c', 'timer.c',
|
||||
'main.cpp', 'sock.cpp','paddle.cpp', 'ball.cpp', 'numeric_base.cpp', 'connect_code.cpp', 'server.cpp', 'client.cpp', 'check_input.cpp', 'raygui_helpers.cpp',
|
||||
'serialization.c', 'timer.c', 'easysock.c',
|
||||
dependencies: [raylib, ws2_dep, winmm]
|
||||
)
|
||||
|
||||
13
raygui_helpers.cpp
Normal file
13
raygui_helpers.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "includes/raygui_helpers.hpp"
|
||||
#include "includes/raygui/raygui.h"
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -8,8 +8,8 @@
|
||||
#include "includes/sock.hpp"
|
||||
#include "includes/server.hpp"
|
||||
#include "includes/exception_consts.hpp"
|
||||
#include "includes/easysock.hpp"
|
||||
#include "includes/connect_code.hpp"
|
||||
#include "includes/easysock.h"
|
||||
|
||||
/* Destructor - closes any open sockets */
|
||||
Server::~Server() {
|
||||
|
||||
2
sock.cpp
2
sock.cpp
@@ -2,7 +2,7 @@
|
||||
#include <stdexcept>
|
||||
#include "includes/sock.hpp"
|
||||
#include "includes/exception_consts.hpp"
|
||||
#include "includes/easysock.hpp"
|
||||
#include "includes/easysock.h"
|
||||
|
||||
/* Function to create socket. This function doesn't actually create a socket
|
||||
(and isn't meant to be called directly). Instead, the client and server classes
|
||||
|
||||
Reference in New Issue
Block a user