diff --git a/client_test b/client_test deleted file mode 120000 index c712ab8..0000000 --- a/client_test +++ /dev/null @@ -1 +0,0 @@ -build/bin/client_test \ No newline at end of file diff --git a/game b/game deleted file mode 120000 index 5aa2eea..0000000 --- a/game +++ /dev/null @@ -1 +0,0 @@ -build/bin/game \ No newline at end of file diff --git a/include/net.h b/include/net.h index 126c6bc..3d02337 100644 --- a/include/net.h +++ b/include/net.h @@ -4,7 +4,8 @@ #define __netcode_h__ #define PORT 36911 - + +void init_network(); #endif diff --git a/net/client.c b/net/client.c new file mode 100644 index 0000000..7e5dd59 --- /dev/null +++ b/net/client.c @@ -0,0 +1,63 @@ +//troy Rosin + +#include +#include +#include +#include +#include +#include + + + +char buf[PACKSZ]; +char pack[PACKSZ]; + + +int main(void) +{ + + IPaddress ip; + TCPsocket sock; + int ret = 0; + + SDL_Init(0); + + SDLNet_Init(); + + if(SDLNet_ResolveHost(&ip, SERVER, 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); + } + + ret = SDLNet_TCP_Recv(sock, pack, PACKSZ); + if(ret <=0 ) + { + printf("error in TCP Recv: %s\n", SDLNet_GetError()); + } + + read_to_buff(buf, pack, PACKSZ); + + printf("got msg: %s\n", buf); + + + SDLNet_TCP_Close(sock); + SDLNet_Quit(); + SDL_Quit(); + + puts("donzo"); + + + return 0; +} + + + + diff --git a/net/mydebug.h b/net/mydebug.h new file mode 100644 index 0000000..0eaa36b --- /dev/null +++ b/net/mydebug.h @@ -0,0 +1,84 @@ +/*for debugging print statments and timers +* Troy Rosin TJR760 11200680 +* compile with -Ddebug and the prints will be there +* otherwise nothing wil appear +* +* to use with makefile add the following +* +* debug ?=off +* ifeq ($(debug),on) +* CPPFLAGS += -Ddebug +* endif +* +* then use "make debug=on" to make if you want the debug messages +* just use make to build normally +* +*/ + +#include +#include + +#ifdef debug +/*prints the name of the function that was called passed as an arg */ +#define PRINT_C(FUNC)\ + puts(#FUNC" was called"); + + +/*prints the message wanted followed by a \n no "" needed*/ +#define PRINT_M(MSG)\ + puts(#MSG); + + +/*prints the name of a variable holding a pointer and its value*/ +#define PRINT_P(PTR)\ + printf(#PTR" has value %p\n", (void*)PTR); + + +/*prints the varilble name holding the int and the value +* Type is how you want it to print ie l, d, ld, u, f... +* just pass the field type printf expects without the '%' +*/ +#define PRINT_INT(VAR, TYPE)\ + printf(#VAR" has value %"#TYPE"\n", VAR); + + +/*starts a timer so you can see how long fuctions can run for +* you must end the timer within the same scope as the start +* the id can be anything its only used to differentiate between +* timers +* T_START must be placed at the end of variable definitions to comply with C90 +* standards. if not using C90 go nuts +*/ +#define T_START(ID)\ + struct timespec ID ## __d_timer_s__;\ + struct timespec ID ## __d_timer_e__;\ + clock_gettime(CLOCK_MONOTONIC_RAW, &ID ## __d_timer_s__); + + + +/*checks how long since the timer began and prints the seconds and nanoseconds +* since. Can be called multiple times if needed +*/ +#define T_END(ID)\ + clock_gettime(CLOCK_MONOTONIC_RAW, &ID ## __d_timer_e__);\ + printf("timer "#ID" took:\n%ld second(s) total\n%ld nanoseconds total\n",\ + ((ID ## __d_timer_e__.tv_sec * 1000000000 + ID ## __d_timer_e__.tv_nsec - \ + ID ## __d_timer_s__.tv_sec * 1000000000 + ID ## __d_timer_s__.tv_nsec) - \ + ((ID ## __d_timer_e__.tv_sec * 1000000000 + ID ## __d_timer_e__.tv_nsec-\ + ID ## __d_timer_s__.tv_sec * 1000000000 + ID ## __d_timer_s__.tv_nsec) \ + % 1000000000)) / 1000000000 , \ + ID ## __d_timer_e__.tv_sec * 1000000000 + ID ## __d_timer_e__.tv_nsec -\ + ID ## __d_timer_s__.tv_sec * 1000000000 + ID ## __d_timer_s__.tv_nsec ); + + + +#else +/*if debug was not defined we insert nothing there*/ +#define PRINT_C(X) {} +#define PRINT_M(X) {} +#define PRINT_P(X) {} +#define PRINT_INT(X,Y) {} +#define T_START(X) {} +#define T_END(X) {} + +#endif diff --git a/net/net.h b/net/net.h new file mode 100644 index 0000000..9f43c60 --- /dev/null +++ b/net/net.h @@ -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, HOSTS_REQ, NEW_HOST, JOIN_REQ +}; + +typedef struct packet{ + enum pack_type type; + char payload[PACKSZ - sizeof(enum pack_type)]; +} packet; + + +int read_to_buff(char *buff, char *src, Uint32 sz) +{ + if(sz%32){ + puts("error size must be div by 32"); + return 0; + } + for(Uint32 i = 0; i < sz ; i += 4) + { + *((Uint32*)(buff + i)) = SDLNet_Read32((Uint32 *)(src + i)); + } + return 1; +} + +int write_to_buff(char *buff, char *src, Uint32 sz) +{ + if(sz%32){ + puts("error size must be div by 32"); + return 0; + } + for(Uint32 i = 0; i < sz; i += 4) + { + SDLNet_Write32(*((Uint32*)(src + i)), (Uint32*)(buff + i)); + } + return 1; +} + + +#endif diff --git a/net/server.c b/net/server.c new file mode 100644 index 0000000..092d5a0 --- /dev/null +++ b/net/server.c @@ -0,0 +1,90 @@ +//troy Rosin + +#include +#include +#include +#include +#include +#include + + + +char buff[PACKSZ]; +char pack[PACKSZ], pack2[PACKSZ]; + + + +int main(void) +{ + + IPaddress *ip, _ip, *cip; + TCPsocket sock, csock; + ip = &_ip; + + Uint16 c_port; + Uint32 c_ip; + + strcpy(pack, "testing Yolo fambro!"); + + 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); + } + + int flag = 1; + int ret = 0; + 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()); + } + read_to_buff(pack2, buff, PACKSZ); + printf("sent: [%s]\n", pack2); + } + } + } + + SDLNet_TCP_Close(sock); + SDLNet_Quit(); + SDL_Quit(); + + puts("donzo"); + + + return 0; +} + + + + diff --git a/net/site b/net/site new file mode 100644 index 0000000..301f0b8 --- /dev/null +++ b/net/site @@ -0,0 +1 @@ +www.libsdl.org/projects/old/SDL_net/docs/ diff --git a/server_test b/server_test deleted file mode 120000 index 5a7da28..0000000 --- a/server_test +++ /dev/null @@ -1 +0,0 @@ -build/bin/server_test \ No newline at end of file