Compare commits

5 Commits

8 changed files with 27 additions and 16 deletions

View File

@@ -11,8 +11,10 @@ namespace connect_code {
/* Tokenizes a string, based on the given delimiter */ /* Tokenizes a string, based on the given delimiter */
std::vector<std::string> tokenize_str(std::string str, std::string delim) { std::vector<std::string> tokenize_str(std::string str, std::string delim) {
std::vector<std::string> result; std::vector<std::string> result;
char* c_str = str.data(); /* &str[0] is used to convert an std::string to a char*. I tried using string.data(),
char* c_delim = delim.data(); but that appears to return a const char*. */
char* c_str = &str[0];
char* c_delim = &delim[0];
char* tok = strtok(c_str, c_delim); char* tok = strtok(c_str, c_delim);
while (tok != NULL) { while (tok != NULL) {

View File

@@ -59,7 +59,7 @@ SOCKET create_socket(int network, char transport) {
} }
int create_addr(int network, char* address, int port,struct sockaddr* dest) { int create_addr(int network, const char* address, int port,struct sockaddr* dest) {
if (network == 4) { if (network == 4) {
struct sockaddr_in listen_address; struct sockaddr_in listen_address;
@@ -83,7 +83,7 @@ int create_addr(int network, char* address, int port,struct sockaddr* dest) {
} }
SOCKET create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct) { SOCKET create_local (int network, char transport, const char* address, int port,struct sockaddr* addr_struct) {
int socket = create_socket(network,transport); int socket = create_socket(network,transport);
if (socket < 0) { if (socket < 0) {
return (-1 * errno); return (-1 * errno);
@@ -109,7 +109,7 @@ SOCKET create_local (int network, char transport, char* address, int port,struct
return socket; return socket;
} }
SOCKET create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct) { SOCKET create_remote (int network,char transport, const char* address,int port,struct sockaddr* remote_addr_struct) {
struct addrinfo hints; /* Used to tell getaddrinfo what kind of address we want */ struct addrinfo hints; /* Used to tell getaddrinfo what kind of address we want */
struct addrinfo* results; /* Used by getaddrinfo to store the addresses */ struct addrinfo* results; /* Used by getaddrinfo to store the addresses */

View File

@@ -44,7 +44,7 @@ and dest is a pointer to the sockaddr struct that will be filled in.
The function returns with -202 if the network parameter contained neither '4' The function returns with -202 if the network parameter contained neither '4'
nor '6'. */ nor '6'. */
int create_addr(int network, char* address, int port,struct sockaddr* dest); int create_addr(int network, const char* address, int port,struct sockaddr* dest);
@@ -54,7 +54,7 @@ same as above.
It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/ It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/
SOCKET create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct); SOCKET create_local (int network, char transport, const char* address, int port,struct sockaddr* addr_struct);
/* This function utilizes the same functions as 'create_local' but _connects_ to the /* This function utilizes the same functions as 'create_local' but _connects_ to the
@@ -63,7 +63,7 @@ as above. This function needs an empty 'sockaddr *' structure passed to it, whic
If something goes wrong, this function returns with ( -1 * errno ). */ If something goes wrong, this function returns with ( -1 * errno ). */
SOCKET create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct); SOCKET create_remote (int network,char transport, const char* address,int port,struct sockaddr* remote_addr_struct);
/* check_ip_ver - This function checks if the given string is an IPv4 address (returns 4), /* check_ip_ver - This function checks if the given string is an IPv4 address (returns 4),
IPv6 address (returns 6) or neither (returns -1). */ IPv6 address (returns 6) or neither (returns -1). */

View File

@@ -3,10 +3,10 @@
#include <string> #include <string>
/* Convert the given value from the given base, to base 10 */ /* Convert the given value from the given base, to base 10 */
int to_decimal(std::string num, int from_base); unsigned int to_decimal(std::string num, int from_base);
/* Convert the given value from base 10 to the given base */ /* Convert the given value from base 10 to the given base */
std::string from_decimal(int num, int to_base); std::string from_decimal(unsigned int num, int to_base);
/* Convert the given value from 'from_base', to 'to_base' */ /* Convert the given value from 'from_base', to 'to_base' */
std::string base_convert(std::string num, int from_base, int to_base); std::string base_convert(std::string num, int from_base, int to_base);

View File

@@ -34,6 +34,9 @@ public:
/* Default constructor */ /* Default constructor */
Sock() {} Sock() {}
/* Virtual destructor */
virtual ~Sock();
/* Regular constructor - defined in sock.cpp */ /* Regular constructor - defined in sock.cpp */
Sock(int ip_ver, char protocol, const char* address, int port); Sock(int ip_ver, char protocol, const char* address, int port);

View File

@@ -2,10 +2,12 @@ project('Pong', ['cpp', 'c'])
add_global_arguments('-g', '-Wall', '-pedantic', '-Wno-unused-function', '-Werror', language : ['cpp', 'c']) add_global_arguments('-g', '-Wall', '-pedantic', '-Wno-unused-function', '-Werror', language : ['cpp', 'c'])
raylib = dependency('raylib', native: true) raylib = dependency('raylib', native: true)
static_lib_path = ''
compiler = meson.get_compiler('cpp')
ws2_dep = compiler.find_library('ws2_32', required: false)
executable('pong', executable('pong',
'main.cpp', 'easysock.cpp', 'sock.cpp','paddle.cpp', 'ball.cpp', 'numeric_base.cpp', 'connect_code.cpp', 'server.cpp', 'client.cpp', 'main.cpp', 'easysock.cpp', 'sock.cpp','paddle.cpp', 'ball.cpp', 'numeric_base.cpp', 'connect_code.cpp', 'server.cpp', 'client.cpp',
'serialization.c', 'serialization.c',
dependencies: raylib, dependencies: [raylib, ws2_dep]
) )

View File

@@ -5,10 +5,10 @@
std::string possible_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; std::string possible_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int to_decimal(std::string num, int from_base) { unsigned int to_decimal(std::string num, int from_base) {
char current_char = 0; char current_char = 0;
int index = 0; int index = 0;
int value = 0; unsigned int value = 0;
/* Here, we convert 'num' to decimal (base 10) - Find the index of /* Here, we convert 'num' to decimal (base 10) - Find the index of
every character in the string, in 'possible_chars' and every character in the string, in 'possible_chars' and
@@ -23,7 +23,7 @@ int to_decimal(std::string num, int from_base) {
} }
/* Convert the given value from base 10 to the given base */ /* Convert the given value from base 10 to the given base */
std::string from_decimal(int num, int to_base) { std::string from_decimal(unsigned int num, int to_base) {
std::string return_val; std::string return_val;
int val = 0; int val = 0;
while (num > 0) { while (num > 0) {
@@ -40,7 +40,7 @@ std::string from_decimal(int num, int to_base) {
/* Convert the given value from 'from_base', to 'to_base' */ /* Convert the given value from 'from_base', to 'to_base' */
std::string base_convert(std::string num, int from_base, int to_base) { std::string base_convert(std::string num, int from_base, int to_base) {
int temp = to_decimal(num, from_base); unsigned int temp = to_decimal(num, from_base);
std::string result = from_decimal(temp, to_base); std::string result = from_decimal(temp, to_base);
return result; return result;

View File

@@ -13,6 +13,10 @@ void Sock::create_socket() {
addrlen = sizeof(*dest); addrlen = sizeof(*dest);
} }
/* Virtual destructor, allows 'Server' and 'Client' to override this destructor */
Sock::~Sock() {}
/* Constructor - This function initializes the object attributes with the given /* Constructor - This function initializes the object attributes with the given
parameters. It throws an exception if an IPv4 address was given, but the type parameters. It throws an exception if an IPv4 address was given, but the type
given is IPv6 (or the other way around). */ given is IPv6 (or the other way around). */