Changing Project Structure

This commit is contained in:
2026-04-26 13:48:26 -04:00
parent 3607c4bf36
commit 8a4812973c
9 changed files with 8 additions and 10 deletions

33
src/court.c Normal file
View File

@@ -0,0 +1,33 @@
#include "../include/game.h"
#include <curses.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void DrawWalls(GameData data)
{
for (int i = LEFT_COL; i <= RIGHT_COL; i++) {
move(TOP_ROW, i);
addch('=');
move(BOT_ROW, i);
addch('-');
}
int col = LEFT_COL;
if (data.playerNumber > 0)
col = RIGHT_COL;
for (int i = TOP_ROW; i <= BOT_ROW; i++) {
move(i, col);
addch('|');
}
refresh();
}
void DrawHeader(GameData data)
{
move(TOP_ROW - 2, LEFT_COL);
addch('P');
addch('0' + (data.playerNumber + 1));
}

133
src/gameServer.c Normal file
View File

@@ -0,0 +1,133 @@
#include "../include/game.h"
#include <curses.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void CreateGameServer(Server* serv, GameData* gd)
{
Server s = *serv;
bool game_running = true;
fd_set fds;
FD_ZERO(&fds);
FD_SET(s.server_fd, &fds);
struct timeval tv = { 0, 0 }; // non-blocking check
SetupTermWin();
DrawWalls(*gd);
while (game_running) {
fd_set read_fds;
FD_ZERO(&read_fds);
// Add server socket
FD_SET(s.server_fd, &read_fds);
int max_fd = s.server_fd;
// Add client sockets
for (int i = 0; i < s.client_count; i++) {
int fd = s.clients[i];
FD_SET(fd, &read_fds);
if (fd > max_fd)
max_fd = fd;
}
// Small timeout (prevents 100% CPU usage)
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000; // 1ms
int ret = select(max_fd + 1, &read_fds, NULL, NULL, &tv);
if (ret < 0) {
perror("select");
break;
}
// New connection
if (FD_ISSET(s.server_fd, &read_fds)) {
int client_fd = accept(s.server_fd, NULL, NULL);
if (client_fd >= 0 && s.client_count < MAX_SERVER_CLIENTS) {
s.clients[s.client_count++] = client_fd;
char msg[64];
snprintf(msg, sizeof(msg), "New client connected: %d\n", client_fd);
GameAlert(msg);
}
}
// Handle client data
for (int i = 0; i < s.client_count; i++) {
int fd = s.clients[i];
if (FD_ISSET(fd, &read_fds)) {
char buffer[1024];
int bytes = recv(fd, buffer, sizeof(buffer) - 1, 0);
if (bytes > 0) {
buffer[bytes] = '\0';
char msg[1024];
snprintf(msg, sizeof(msg), "Client %d says: %s\r", fd, buffer);
GameAlert(msg);
// Example: echo back
send(fd, buffer, bytes, 0);
} else if (bytes == 0) {
char msg[64];
snprintf(msg, sizeof(msg), "Client %d disconnected\n", fd);
GameAlert(msg);
RemoveClient(&s, i);
i--; // re-check swapped client
} else {
perror("recv");
RemoveClient(&s, i);
i--;
}
}
}
if (s.client_count < 1) {
char msg[64];
snprintf(msg, sizeof(msg), "Waiting for other player\n");
GameAlert(msg);
} else {
// Process Game Data
}
}
CloseTermWin();
}
void RunGameClient(Client* client)
{
Client c = *client;
// char* msg = "Hello from client, this is a test, I really want to see what happens if I create a really long packet and try to send it. I would imagine it will be some kind of buffer overflow";
// send(c.client_fd, msg, strlen(msg), 0);
// {
// char buffer[1024] = { 0 };
// read(c.client_fd, buffer, sizeof(buffer));
// printf("Server: %s\n", buffer);
// }
sleep(10);
// char* msg2 = "Ok I am leaving now";
// send(c.client_fd, msg2, strlen(msg2), 0);
// {
// char buffer[1024] = { 0 };
// read(c.client_fd, buffer, sizeof(buffer));
// printf("Server: %s\n", buffer);
// }
}

89
src/network/networking.c Normal file
View File

@@ -0,0 +1,89 @@
#include "../../include/network/networking.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
Server StartServer(int port) {
Server s;
s.clientID = 0;
s.client_count = 0;
// Create Socket
if ((s.server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Set Address
s.address.sin_family = AF_INET;
s.address.sin_addr.s_addr = INADDR_ANY; // listen on all interfaces
s.address.sin_port = htons(port);
// Bind Port
if (bind(s.server_fd, (struct sockaddr *)&s.address, sizeof(s.address)) < 0) {
perror("bind failed");
close(s.server_fd);
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(s.server_fd, 3) < 0) {
perror("listen");
close(s.server_fd);
exit(EXIT_FAILURE);
}
printf("Server listening on port %d...\n", port);
return s;
}
void RemoveClient(Server* s, int index) {
close(s->clients[index]);
s->clients[index] = s->clients[s->client_count - 1];
s->client_count--;
}
Client StartClient(char* host, int port) {
Client c;
// Create socket
if ((c.client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Set address info
c.address.sin_family = AF_INET;
c.address.sin_port = htons(port);
// Convert hostname/IP to binary form
if (inet_pton(AF_INET, host, &c.address.sin_addr) <= 0) {
// If not a direct IP, try resolving hostname
struct hostent* he = gethostbyname(host);
if (he == NULL) {
perror("gethostbyname failed");
close(c.client_fd);
exit(EXIT_FAILURE);
}
memcpy(&c.address.sin_addr, he->h_addr_list[0], he->h_length);
}
// Connect to server
if (connect(c.client_fd, (struct sockaddr*)&c.address, sizeof(c.address)) < 0) {
perror("connect failed");
close(c.client_fd);
exit(EXIT_FAILURE);
}
printf("Connected to %s:%d\n", host, port);
return c;
}

38
src/term.c Normal file
View File

@@ -0,0 +1,38 @@
#include "../include/game.h"
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void SetupTermWin()
{
srand(getpid());
// Setup Terminal
if (initscr() == NULL) {
perror("initscr failed");
exit(1);
}
noecho();
cbreak();
nodelay(stdscr, TRUE);
}
void CloseTermWin()
{
endwin();
}
void GameAlert(char* str)
{
MGameAlert(0,0,str);
}
void MGameAlert(int y, int x, char* str)
{
move(y, x);
clrtoeol();
mvaddstr(y, x, str);
refresh();
}