working server and gameloop

This commit is contained in:
2026-04-26 13:44:54 -04:00
parent 81488417d9
commit 3607c4bf36
8 changed files with 194 additions and 116 deletions

117
main.c
View File

@@ -1,13 +1,10 @@
#include "game.h"
#include <curses.h>
#include <signal.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "network/networking.h"
/**
* Main Fucntion
*/
@@ -38,130 +35,26 @@ int main(int argc, char* argv[])
return 1;
}
// srand(getpid());
// // Setup Terminal
// if (initscr() == NULL) {
// perror("initscr failed");
// exit(1);
// }
// noecho();
// cbreak();
// nodelay(stdscr, TRUE);
int game_running = true;
if (data.host) {
// Create Server
Server s = StartServer(data.port);
printf("Server Started\n");
fd_set fds;
FD_ZERO(&fds);
FD_SET(s.server_fd, &fds);
struct timeval tv = { 0, 0 }; // non-blocking check
int updateChar = 0;
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 < s.max_clients) {
s.clients[s.client_count++] = client_fd;
printf("New client connected: %d\n", client_fd);
}
}
// 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';
printf("Client %d says: %s\n", fd, buffer);
// Example: echo back
send(fd, buffer, bytes, 0);
} else if (bytes == 0) {
printf("Client %d disconnected\n", fd);
RemoveClient(&s, i);
i--; // re-check swapped client
} else {
perror("recv");
RemoveClient(&s, i);
i--;
}
}
}
}
CreateGameServer(&s,&data);
close(s.server_fd);
} else {
printf("Connecting to %s:%i\n", data.domain, data.port);
// Create Client
Client c = StartClient(data.domain, data.port);
char* msg = "Hello from client";
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);
}
RunGameClient(&c);
close(c.client_fd);
return 0;
}
// endwin();
}