/*Troy Rosin*/ #include #include #include #include #include #include #include #include #include #include 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; } 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; }