git had the makefile ignored and not im stranded. updated gitignoreand have a good starting point for the game server. working on moving it into the game

This commit is contained in:
2026-01-30 13:30:24 -06:00
parent d29e772272
commit 6856f6f2c8
9 changed files with 340 additions and 52 deletions

1
.gitignore vendored
View File

@@ -467,7 +467,6 @@ CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json

48
net.h Normal file
View File

@@ -0,0 +1,48 @@
//Troy Rosin
#ifndef __my_net_h__
#define __my_net_h__
#define PORT 36911
#define SERVER "localhost"
#define PACKSZ 1024
enum pack_type{
G_STATE, S_STATE, HOSTS_REQ, NEW_HOST, JOIN_REQ, U_NAME
};
typedef struct packet{
enum pack_type type;
char payload[PACKSZ - sizeof(enum pack_type)];
} packet;
int read_to_buff(Uint32 *buff, Uint32 *src, Uint32 sz)
{
if(sz%32){
puts("error size must be div by 32");
return 0;
}
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
*(buff + i) = SDLNet_Read32(src + i);
}
return 1;
}
int write_to_buff(Uint32 *buff, Uint32 *src, Uint32 sz)
{
if(sz%32){
puts("error size must be div by 32");
return 0;
}
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
SDLNet_Write32(*(src + i), (buff + i));
}
return 1;
}
#endif

36
net/Makefile Executable file
View File

@@ -0,0 +1,36 @@
#Troy Rosin tjr760 11200680
CC =gcc
CFLAGS =-g
CPPFLAGS =-c -g -Wall -pedantic
LDFLAGS =-lSDL2 -lSDL2_net
BIN =build/bin/
LIB =build/lib/
OBJ =build/obj/
DIRS =$(BIN) $(LIB) $(OBJ)
debug ?=on
ifeq ($(debug),on)
CPPFLAGS += -Ddebug
endif
BINARIES = client server
all : $(BINARIES)
clean :
rm -rf build/ lib/ bin/ $(BINARIES)
$(DIRS) :
mkdir -p $@
$(BIN)% : $(OBJ)%.o | $(BIN)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
$(OBJ)%.o : %.c net.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< -I.
$(BINARIES) : % : $(BIN)%
ln -sf $< $@

1
net/client Symbolic link
View File

@@ -0,0 +1 @@
build/bin/client

View File

@@ -3,15 +3,16 @@
#include <SDL2/SDL_net.h>
#include <SDL2/SDL.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <mydebug.h>
#include <net.h>
#define SECS 1
char buf[PACKSZ];
char pack[PACKSZ];
packet msg;
int main(void)
{
@@ -37,16 +38,44 @@ int main(void)
exit(1);
}
ret = SDLNet_TCP_Recv(sock, pack, PACKSZ);
if(ret <=0 )
{
printf("error in TCP Recv: %s\n", SDLNet_GetError());
}
PRINT_M("starting");
read_to_buff(buf, pack, PACKSZ);
msg.type = G_STATE;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
PRINT_M("packed up!");
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
printf("got msg: %s\n", buf);
msg.type = S_STATE;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
msg.type = HOSTS_REQ;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
msg.type = NEW_HOST;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
msg.type = JOIN_REQ;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
msg.type = U_NAME;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
SDLNet_TCP_Close(sock);
SDLNet_Quit();

View File

@@ -9,7 +9,7 @@
#define PACKSZ 1024
enum pack_type{
G_STATE, HOSTS_REQ, NEW_HOST, JOIN_REQ
G_STATE, S_STATE, HOSTS_REQ, NEW_HOST, JOIN_REQ, U_NAME
};
typedef struct packet{
@@ -18,28 +18,28 @@ typedef struct packet{
} packet;
int read_to_buff(char *buff, char *src, Uint32 sz)
int read_to_buff(Uint32 *buff, Uint32 *src, Uint32 sz)
{
if(sz%32){
puts("error size must be div by 32");
return 0;
}
for(Uint32 i = 0; i < sz ; i += 4)
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
*((Uint32*)(buff + i)) = SDLNet_Read32((Uint32 *)(src + i));
*(buff + i) = SDLNet_Read32(src + i);
}
return 1;
}
int write_to_buff(char *buff, char *src, Uint32 sz)
int write_to_buff(Uint32 *buff, Uint32 *src, Uint32 sz)
{
if(sz%32){
puts("error size must be div by 32");
return 0;
}
for(Uint32 i = 0; i < sz; i += 4)
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
SDLNet_Write32(*((Uint32*)(src + i)), (Uint32*)(buff + i));
SDLNet_Write32(*(src + i), (buff + i));
}
return 1;
}

1
net/server Symbolic link
View File

@@ -0,0 +1 @@
build/bin/server

View File

@@ -7,24 +7,25 @@
#include <mydebug.h>
#include <net.h>
#define MAX_SOCKS 4
char buff[PACKSZ];
char pack[PACKSZ], pack2[PACKSZ];
int main(void)
void game_server()
{
IPaddress *ip, _ip, *cip;
TCPsocket sock, csock;
TCPsocket sock_arr[MAX_SOCKS];
ip = &_ip;
SDLNet_SocketSet socks;
int i;
Uint16 c_port;
Uint32 c_ip;
strcpy(pack, "testing Yolo fambro!");
int flag = 1;
int ret = 0;
SDL_Init(0);
@@ -43,45 +44,86 @@ int main(void)
exit(1);
}
int flag = 1;
int ret = 0;
socks = SDLNet_AllocSocketSet(MAX_SOCKS);
for(i = 0; i < MAX_SOCKS; ++i)
{
sock_arr[i] = NULL;
}
while(flag)
{
csock = SDLNet_TCP_Accept(sock);
if(!csock)
{
//printf("TCP Accept error: %s\n", SDLNet_GetError());
}
else {
flag = 0;
cip = SDLNet_TCP_GetPeerAddress(csock);
if(!cip)
{
printf("error getting client ip addr: %s\n", SDLNet_GetError());
}
else
{
puts("we got a new connection!");
printf("sending: [%s]\n", pack);
write_to_buff(buff, pack, PACKSZ);
ret = SDLNet_TCP_Send(csock, buff, PACKSZ);
if(ret < PACKSZ)
{
printf("TCP Send error: %s\n", SDLNet_GetError());
if(!csock && SDLNet_CheckSockets(socks, 0) > 0 ){
//no new connection and there is updates on socks
for(i = 0; i < MAX_SOCKS; ++i)
{//check each sock for data and handle it
csock = sock_arr[i];
if(csock == NULL){continue;}
if(SDLNet_SocketReady(csock)){
ret = SDLNet_TCP_Recv(csock, pack, PACKSZ);
if(ret < PACKSZ){
printf("TCP recv error: %s\n", SDLNet_GetError());
sock_arr[i] = NULL;
SDLNet_TCP_DelSocket(socks, csock);
SDLNet_TCP_Close(csock);
continue;
}
read_to_buff((Uint32*)buff, (Uint32*)pack, PACKSZ);
switch(((packet*)buff)->type)
{
case G_STATE:
PRINT_M("got to get state case");
break;
case S_STATE:
PRINT_M("got to set state case");
break;
case HOSTS_REQ:
PRINT_M("got to hosts request state");
break;
case NEW_HOST:
PRINT_M("got to new host state");
break;
case JOIN_REQ:
PRINT_M("got to join request state");
break;
case U_NAME:
PRINT_M("got to update name state");
flag = 0;
break;
}
}
read_to_buff(pack2, buff, PACKSZ);
printf("sent: [%s]\n", pack2);
}
}
else if (csock) {
cip = SDLNet_TCP_GetPeerAddress(csock);
if(!cip){
printf("get peer addr error: %s\n", SDLNet_GetError());
}
else {
PRINT_M("we got a new connection!");
if(SDLNet_TCP_AddSocket(socks, csock) <= 0){
printf("add sock to set error: %s\n", SDLNet_GetError());
}
for(i = 0; i < MAX_SOCKS; ++i){
if(sock_arr[i] == NULL){ sock_arr[i] = csock; break; }
}
}
}
return;
}
SDLNet_TCP_Close(sock);
SDLNet_Quit();
SDL_Quit();
puts("donzo");
}
int main(void)
{
game_server();
puts("donzo");
return 0;
}

132
server.c Normal file
View File

@@ -0,0 +1,132 @@
//troy Rosin
#include <SDL2/SDL_net.h>
#include <SDL2/SDL.h>
#include <string.h>
#include <stdio.h>
#include <mydebug.h>
#include <net.h>
#define MAX_SOCKS 4
char buff[PACKSZ];
char pack[PACKSZ], pack2[PACKSZ];
void game_server()
{
IPaddress *ip, _ip, *cip;
TCPsocket sock, csock;
TCPsocket sock_arr[MAX_SOCKS];
ip = &_ip;
SDLNet_SocketSet socks;
int i;
Uint16 c_port;
Uint32 c_ip;
int flag = 1;
int ret = 0;
SDL_Init(0);
SDLNet_Init();
if(SDLNet_ResolveHost(ip, NULL, PORT) == -1)
{
printf("SDLNet_resolvehost error: %s\n", SDLNet_GetError());
exit(1);
}
sock = SDLNet_TCP_Open(ip);
if(!sock)
{
printf("SDL_Net_TCP_Open error: %s\n", SDLNet_GetError());
exit(1);
}
socks = SDLNet_AllocSocketSet(MAX_SOCKS);
for(i = 0; i < MAX_SOCKS; ++i)
{
sock_arr[i] = NULL;
}
while(flag)
{
csock = SDLNet_TCP_Accept(sock);
if(!csock && SDLNet_CheckSockets(socks, 0) > 0 ){
//no new connection and there is updates on socks
for(i = 0; i < MAX_SOCKS; ++i)
{//check each sock for data and handle it
csock = sock_arr[i];
if(csock == NULL){continue;}
if(SDLNet_SocketReady(csock)){
ret = SDLNet_TCP_Recv(csock, pack, PACKSZ);
if(ret < PACKSZ){
printf("TCP recv error: %s\n", SDLNet_GetError());
sock_arr[i] = NULL;
SDLNet_TCP_DelSocket(socks, csock);
SDLNet_TCP_Close(csock);
continue;
}
read_to_buff((Uint32*)buff, (Uint32*)pack, PACKSZ);
switch(((packet*)buff)->type)
{
case G_STATE:
PRINT_M("got to get state case");
break;
case S_STATE:
PRINT_M("got to set state case");
break;
case HOSTS_REQ:
PRINT_M("got to hosts request state");
break;
case NEW_HOST:
PRINT_M("got to new host state");
break;
case JOIN_REQ:
PRINT_M("got to join request state");
break;
case U_NAME:
PRINT_M("got to update name state");
flag = 0;
break;
}
}
}
}
else if (csock) {
cip = SDLNet_TCP_GetPeerAddress(csock);
if(!cip){
printf("get peer addr error: %s\n", SDLNet_GetError());
}
else {
PRINT_M("we got a new connection!");
if(SDLNet_TCP_AddSocket(socks, csock) <= 0){
printf("add sock to set error: %s\n", SDLNet_GetError());
}
for(i = 0; i < MAX_SOCKS; ++i){
if(sock_arr[i] == NULL){ sock_arr[i] = csock; break; }
}
}
}
return;
}
SDLNet_TCP_Close(sock);
SDLNet_Quit();
SDL_Quit();
}
int main(void)
{
game_server();
puts("donzo");
return 0;
}