added being able to create a host on the game server now need to work on joining that game
This commit is contained in:
97
net.c
97
net.c
@@ -2,7 +2,6 @@
|
||||
|
||||
#include <SDL_net.h>
|
||||
#include <SDLnetsys.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <mydebug.h>
|
||||
@@ -12,15 +11,95 @@
|
||||
#include <main.h>
|
||||
#include <net.h>
|
||||
|
||||
IPaddress ser_ip;
|
||||
TCPsocket ser_sock;
|
||||
SDLNet_SocketSet sset;
|
||||
|
||||
char my_buff[PACKSZ]; //always client bit ordering
|
||||
char net_buff[PACKSZ]; //always in net bit ordering
|
||||
|
||||
packet to_send, received;
|
||||
|
||||
int init_server_connection()
|
||||
{
|
||||
static int connected = 0;
|
||||
if(connected){ return 1; }
|
||||
SDLNet_Init();
|
||||
if(SDLNet_ResolveHost(&ser_ip, SERVER, PORT) == -1){
|
||||
printf("SDLNet resolve host, in init_server connection, error,\n%s\n",
|
||||
SDLNet_GetError());
|
||||
return 0;
|
||||
}
|
||||
ser_sock = SDLNet_TCP_Open(&ser_ip);
|
||||
if(!ser_sock){
|
||||
printf("SDLNet tcp open, in init_server connection, error,\n%s\n",
|
||||
SDLNet_GetError());
|
||||
return 0;
|
||||
}
|
||||
sset = SDLNet_AllocSocketSet(1);
|
||||
SDLNet_TCP_AddSocket(sset, ser_sock);
|
||||
|
||||
connected = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static TCPsocket tcpscok = NULL;
|
||||
static UDPsocket udpsock = NULL;
|
||||
static SDLNet_SocketSet socketset = NULL;
|
||||
static UDPpacket **packets = NULL;
|
||||
static struct {
|
||||
int active;
|
||||
Uint8 name[MAX_NAMESZ];
|
||||
} people[MAX_PLAYERS];
|
||||
|
||||
int new_host(char lobby_name[MAX_NAMESZ])
|
||||
{
|
||||
int ret;
|
||||
if(!init_server_connection()){
|
||||
return 0;
|
||||
}
|
||||
strcpy(to_send.payload, lobby_name);
|
||||
to_send.type = NEW_HOST;
|
||||
write_for_sending((Uint32*)net_buff, (Uint32*)&to_send, PACKSZ);
|
||||
SDLNet_TCP_Send(ser_sock, net_buff, PACKSZ);
|
||||
|
||||
//wait for a response
|
||||
while(!SDLNet_CheckSockets(sset, 0)){}
|
||||
|
||||
PRINT_M(HAVE DATA TO GET)
|
||||
|
||||
ret = SDLNet_TCP_Recv(ser_sock, net_buff, PACKSZ);
|
||||
if(ret < PACKSZ){ return 0; }
|
||||
|
||||
PRINT_M(GOT FULL PACK)
|
||||
|
||||
read_from_packet((Uint32*)&received, (Uint32*)net_buff, PACKSZ);
|
||||
|
||||
if(received.type != NEW_HOST){ return 0; }
|
||||
|
||||
PRINT_M(NEW_HOST CREATED!)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
//takes packet in host bit ordering and puts in buff with client bit ordering
|
||||
int read_from_packet(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;
|
||||
}
|
||||
|
||||
//takes buffer in client bit order and packs into packet with net bit ordering
|
||||
int write_for_sending(Uint32 *pack, 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), (pack + i));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user