started netcode playing with basic functionality
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
build/bin/client_test
|
|
||||||
@@ -4,7 +4,8 @@
|
|||||||
#define __netcode_h__
|
#define __netcode_h__
|
||||||
|
|
||||||
#define PORT 36911
|
#define PORT 36911
|
||||||
|
|
||||||
|
void init_network();
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
63
net/client.c
Normal file
63
net/client.c
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
//troy Rosin
|
||||||
|
|
||||||
|
#include <SDL2/SDL_net.h>
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <mydebug.h>
|
||||||
|
#include <net.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
84
net/mydebug.h
Normal file
84
net/mydebug.h
Normal file
@@ -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 <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#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
|
||||||
48
net/net.h
Normal file
48
net/net.h
Normal 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, 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
|
||||||
90
net/server.c
Normal file
90
net/server.c
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
//troy Rosin
|
||||||
|
|
||||||
|
#include <SDL2/SDL_net.h>
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <mydebug.h>
|
||||||
|
#include <net.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
build/bin/server_test
|
|
||||||
Reference in New Issue
Block a user