added being able to create a host on the game server now need to work on joining that game

This commit is contained in:
2026-02-06 16:36:13 -06:00
parent 468ab94aa7
commit b2fa3052f3
23 changed files with 432 additions and 326 deletions

161
game_server.c Normal file
View File

@@ -0,0 +1,161 @@
//troy Rosin
#include <SDL2/SDL_net.h>
#include <SDL2/SDL.h>
#include <string.h>
#include <stdio.h>
#include <mydebug.h>
#include <net.h>
#include <game.h>
#define MAX_LOBBIES 16
host_info open_hosts[MAX_LOBBIES];
SDLNet_SocketSet host_socks;
extern char my_buff[PACKSZ];
extern char net_buff[PACKSZ];
packet ser_send, recved;
void new_host_setup();
TCPsocket sock, csock;
void game_server()
{
IPaddress *ip, _ip, *cip;
TCPsocket sock_arr[MAX_LOBBIES];
ip = &_ip;
SDLNet_SocketSet socks;
int i;
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_LOBBIES);
for(i = 0; i < MAX_LOBBIES; ++i)
{
open_hosts[i].name[0] = '\0';
sock_arr[i] = NULL;
}
//main server loop
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_LOBBIES; ++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, net_buff, 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_from_packet((Uint32*)&recved, (Uint32*)net_buff, PACKSZ);
switch(recved.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");
new_host_setup();
break;
case JOIN_REQ:
PRINT_M("got to join request state");
break;
case U_NAME:
PRINT_M("got to update name state");
break;
default:
printf("unknown packet type: %d\n", recved.type);
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_LOBBIES; ++i){
if(sock_arr[i] == NULL){ sock_arr[i] = csock; break; }
}
}
}
}
SDLNet_TCP_Close(sock);
SDLNet_Quit();
SDL_Quit();
}
int main(void)
{
game_server();
return 0;
}
void new_host_setup()
{
int i;
TCPsocket s = csock;
PRINT_M(SETUP NEW HOST)
for(i = 0; i < MAX_LOBBIES; ++i){
if(!*(open_hosts[i].name)){continue;}
if(!strcmp(recved.payload, open_hosts[i].name)){
PRINT_M(ALREAD HAVE THAT LOBBY NAME OPEN)
return;
}
}
PRINT_M(SENDING BACK THEY GOOD)
//no lobbies have that name
write_for_sending((Uint32*)net_buff, (Uint32*)&recved, PACKSZ);
SDLNet_TCP_Send(s, net_buff, PACKSZ);
return;
}