From c87c3ce3a2874927382a553c3dfbe94fa407112b Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 29 Jan 2024 22:46:55 -0500 Subject: [PATCH] Moved header files to source directory; converted easysock to CPP --- includes/ball.hpp | 48 +++++++++++++++++++++++++ includes/client.hpp | 59 ++++++++++++++++++++++++++++++ includes/easysock.hpp | 76 +++++++++++++++++++++++++++++++++++++++ includes/math-helpers.hpp | 6 ++++ includes/paddle.hpp | 56 +++++++++++++++++++++++++++++ includes/server.hpp | 62 ++++++++++++++++++++++++++++++++ 6 files changed, 307 insertions(+) create mode 100644 includes/ball.hpp create mode 100644 includes/client.hpp create mode 100644 includes/easysock.hpp create mode 100644 includes/math-helpers.hpp create mode 100644 includes/paddle.hpp create mode 100644 includes/server.hpp diff --git a/includes/ball.hpp b/includes/ball.hpp new file mode 100644 index 0000000..f6f01ca --- /dev/null +++ b/includes/ball.hpp @@ -0,0 +1,48 @@ +#ifndef BALL_H +#define BALL_H + +class Ball { +public: + /* Variables */ + raylib::Vector2 pos; + raylib::Vector2 initial_pos; + raylib::Vector2 vel; + raylib::Vector2 initial_vel; + int radius; + raylib::Color color; + + /* Functions */ + Ball(int pos_x, int pos_y, int radius, int vel_x = 0, int vel_y = 0) { + this->pos = raylib::Vector2(pos_x, pos_y); + this->initial_pos = this->pos; // Create a copy of the position vector + this->radius = radius; + this->color = raylib::Color::White(); + this->vel = raylib::Vector2(vel_x, vel_y); + this->initial_vel = this->vel; // Create a copy of the velocity vector + return; + } + + void setPosition(Vector2 pos) { + this->pos = pos; + } + + void reset() { + this->pos = this->initial_pos; + this->vel = this->initial_vel; + return; + } + + void updatePosition() { + this->pos.x += this->vel.x; + this->pos.y += this->vel.y; + return; + } + + void draw() { + DrawCircle(this->pos.x, this->pos.y, this->radius, this->color); // This is a raylib function, not a raylib-cpp function + return; + } + +}; + +#endif diff --git a/includes/client.hpp b/includes/client.hpp new file mode 100644 index 0000000..4607381 --- /dev/null +++ b/includes/client.hpp @@ -0,0 +1,59 @@ +#include "easysock.hpp" +#include + +/* +Client class - Defines a TCP/UDP client. +- The constructor takes in a **remote** address and port, and connects the client to that address/port. +*/ + +class Client { +private: + int ip_ver; + char protocol; + int port; + int sock_fd; + std::string address; + + void create_socket() { + struct sockaddr* dest = (struct sockaddr *)malloc(sizeof(struct sockaddr)); + this->sock_fd = create_remote(this->ip_ver, this->protocol, this->address.data(), this->port, dest); + } + +public: + Client(int ip_ver, char protocol, const char* address, int port) { + /* Error checking */ + if (ip_ver != 4 && ip_ver != 6) { + throw std::invalid_argument("Invalid IP address type"); + } + if (port < 1024 || port > 65535) { + throw std::invalid_argument("Invalid port"); + } + if (protocol != 'T' && protocol != 'U') { + throw std::invalid_argument("Invalid protocol"); + } + + this->ip_ver = ip_ver; + this->protocol = protocol; + this->port = port; + this->address = std::string(address); + create_socket(); + } + + void sendAll(std::string to_send) { + int str_length = to_send.length(); + int num_bytes_sent = 0; /* Number of bytes sent in one call to send */ + int total_bytes_sent = 0; /* Total number of bytes sent */ + + while (total_bytes_sent < str_length) { + num_bytes_sent = send(this->getSockFD(), to_send.substr(total_bytes_sent).data(), str_length - total_bytes_sent, 0); + total_bytes_sent += num_bytes_sent; + } + + return; + } + + int getSockFD() { + return sock_fd; + } + +}; diff --git a/includes/easysock.hpp b/includes/easysock.hpp new file mode 100644 index 0000000..68563d4 --- /dev/null +++ b/includes/easysock.hpp @@ -0,0 +1,76 @@ +#ifndef EASYSOCK_HPP_ +#define EASYSOCK_HPP_ + +#include +#include +#include +#include +#include +#include +#include +#include + + +/* This function takes: +a layer 3 - network layer - integer, which must be '4' for IPv4 +and 6 for IPv6; and +a layer 4 - transport layer - character, which must be 'T' for +TCP or 'U' for UDP. + +It returns the created socket, or -1 if the socket creation failed.*/ + +int create_socket(int network, char transport); + + +/* This function fills in the sockaddr struct 'dest' based on the given information. +'network' is an integer that contains '4' for IPv4 or '6' for IPv6; +'address' is the address that is filled into the struct; +port is self-explanatory; +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' +nor '6'. */ + +int create_addr(int network, char* address, int port,struct sockaddr* dest); + + + +/* This function utilizes the above two functions; it creates the socket and +_binds_ the addresses. It is used for local sockets (server sockets). Parameters are +same as above. + +It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/ + +int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct); + + +/* This function utilizes the same functions as 'create_local' but _connects_ to the +requested address. It is used for remote sockets (client sockets). The paramters are same +as above. This function needs an empty 'sockaddr *' structure passed to it, which it will fill. + +If something goes wrong, this function returns with ( -1 * errno ). */ + +int create_remote (int network,char transport,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), +IPv6 address (returns 6) or neither (returns -1). */ + +int check_ip_ver(char* address); + +/* int_to_inet - Takes an int value (4 for IPv4, 6 for IPv6) and returns AF_INET or +AF_INET6 respectively. */ + +int int_to_inet(int network); + +/* char_to_socktype - Takes a character that represents a transport-layer protocol +(currently only supports 'T' for TCP or 'U' for UDP - it returns -250 if +the given characters is neither of these) and return the appropriate SOCKTYPE value. */ + +int char_to_socktype(char transport); + +/* inet_to_int - Takes an int value that corresponds to AF_INET or AF_INET6, +and returns the appropriate int value. */ +int inet_to_int(int af_type); + + +#endif diff --git a/includes/math-helpers.hpp b/includes/math-helpers.hpp new file mode 100644 index 0000000..d3daaf6 --- /dev/null +++ b/includes/math-helpers.hpp @@ -0,0 +1,6 @@ +int signum(int num) { + int retval = 0; + (num > 0) ? retval = 1 : retval = -1; + return retval; +} + diff --git a/includes/paddle.hpp b/includes/paddle.hpp new file mode 100644 index 0000000..9f6dae8 --- /dev/null +++ b/includes/paddle.hpp @@ -0,0 +1,56 @@ +#ifndef PADDLE_H +#define PADDLE_H + +class Paddle { +private: + /* Variables */ + raylib::Rectangle rectangle; + raylib::Color color; + int points; + raylib::Vector2 initial_pos; + +public: + /* Variables */ + raylib::Vector2 velocity; /* This variable is made public to allow easy modification (no need for getters/setters) */ + + /* Functions */ + Paddle(int pos_x, int pos_y, int width, int height) { + this->rectangle = raylib::Rectangle(pos_x, pos_y, width, height); + this->initial_pos = raylib::Vector2(pos_x, pos_y); + this->color = raylib::Color::White(); + this->velocity = raylib::Vector2(0,0); + points = 0; + return; + } + + raylib::Rectangle getRect() { + return this->rectangle; + } + + int getPoints() { + return points; + } + void incrementPoints() { + points++; + return; + } + + void reset() { + this->rectangle.x = this->initial_pos.x; + this->rectangle.y = this->initial_pos.y; + this->velocity = raylib::Vector2(0,0); + return; + } + + void updatePosition() { + this->rectangle.x += this->velocity.x; + this->rectangle.y += this->velocity.y; + return; + } + void draw() { + this->rectangle.Draw(this->color); + return; + } +}; + +#endif diff --git a/includes/server.hpp b/includes/server.hpp new file mode 100644 index 0000000..a70537a --- /dev/null +++ b/includes/server.hpp @@ -0,0 +1,62 @@ +#include "easysock.hpp" +#include + +/* +Server class - Defines a TCP/UDP server. +- The constructor takes in a **local** address and port, on which the server listens. +*/ + +class Server { +private: + int ip_ver; + char protocol; + int port; + int sock_fd; + std::string address; + + void create_socket() { + struct sockaddr* dest = (struct sockaddr *)malloc(sizeof(struct sockaddr)); + this->sock_fd = create_local(this->ip_ver, this->protocol, this->address.data(), this->port, dest); + } + +public: + Server(int ip_ver, char protocol, int port) { + /* Error checking */ + if (ip_ver != 4 && ip_ver != 6) { + throw std::invalid_argument("Invalid IP address type"); + } + if (port < 1024 || port > 65535) { + throw std::invalid_argument("Invalid port"); + } + if (protocol != 'T' && protocol != 'U') { + throw std::invalid_argument("Invalid protocol"); + } + + this->ip_ver = ip_ver; + this->protocol = protocol; + this->port = port; + if (ip_ver == 4) { + this->address = "127.0.0.1"; + } else if (ip_ver == 6) { + this->address = "::1"; + } + + create_socket(); + } + + + std::string recvAll() { + std::string string = std::string(); + char* buffer = (char *)malloc(100); + while (recv(this->getSockFD(), buffer, 100, 0) != 0) { + string.append(std::string(buffer)); + } + + return string; + } + + int getSockFD() { + return sock_fd; + } + +};