From b3691a341c1f4813787d851e8f6f95309e825962 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 19 May 2013 04:25:35 +1000 Subject: [PATCH] Started working on a simple UDP wrapper --- docs/networking outline.txt | 32 +++++++++++++++++---- libs/SDL_net/SDL_net.h | 14 ++++----- libs/SDL_net/SDLnet.c | 2 +- libs/SDL_net/makefile | 12 ++++++++ server/udp_network_utility.hpp | 52 ++++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 libs/SDL_net/makefile create mode 100644 server/udp_network_utility.hpp diff --git a/docs/networking outline.txt b/docs/networking outline.txt index 9c53a41..64fc81e 100644 --- a/docs/networking outline.txt +++ b/docs/networking outline.txt @@ -17,12 +17,32 @@ using ';' delimited commands, how can I get it to work? ------------------------- -UDPNetworkManager: //don't worry at this stage - Open(port) - Close() +UDPNetworkUtility: + void Open(port, packSize) + void Close() - Send(ip, port) - Receive() + //bind to an available channel + int Bind(ip, port) + int Bind(address) + + //bind to a certain channel + int Bind(ip, port, channel) + int Bind(address, channel) + void Unbind(channel) + + int Send(channel, data, len) + int Receive() + + GetIPAddress(channel) GetOutData() - GetInData() \ No newline at end of file + GetInData() + + GetOutPacket() + GetInPacket() + + UDPsocket socket + UDPpacket packOut + UDPpacket packIn +end + diff --git a/libs/SDL_net/SDL_net.h b/libs/SDL_net/SDL_net.h index 9a0dad8..9258f86 100644 --- a/libs/SDL_net/SDL_net.h +++ b/libs/SDL_net/SDL_net.h @@ -24,10 +24,10 @@ #ifndef _SDL_NET_H #define _SDL_NET_H -#include "SDL.h" -#include "SDL_endian.h" -#include "SDL_version.h" -#include "begin_code.h" +#include "SDL/SDL.h" +#include "SDL/SDL_endian.h" +#include "SDL/SDL_version.h" +#include "SDL/begin_code.h" @@ -157,9 +157,9 @@ extern DECLSPEC void SDLCALL SDLNet_TCP_Close(TCPsocket sock); /***********************************************************************/ /* The maximum channels on a a UDP socket */ -#define SDLNET_MAX_UDPCHANNELS 32 +#define SDLNET_MAX_UDPCHANNELS 150 /* The maximum addresses bound to a single UDP socket channel */ -#define SDLNET_MAX_UDPADDRESSES 4 +#define SDLNET_MAX_UDPADDRESSES 1 typedef struct _UDPsocket *UDPsocket; typedef struct { @@ -447,6 +447,6 @@ do \ #ifdef __cplusplus } #endif -#include "close_code.h" +#include "SDL/close_code.h" #endif /* _SDL_NET_H */ diff --git a/libs/SDL_net/SDLnet.c b/libs/SDL_net/SDLnet.c index a081f70..310c0cd 100644 --- a/libs/SDL_net/SDLnet.c +++ b/libs/SDL_net/SDLnet.c @@ -23,7 +23,7 @@ #include -#include "SDL_endian.h" +#include "SDL/SDL_endian.h" #include "SDLnetsys.h" #include "SDL_net.h" diff --git a/libs/SDL_net/makefile b/libs/SDL_net/makefile new file mode 100644 index 0000000..0c460ce --- /dev/null +++ b/libs/SDL_net/makefile @@ -0,0 +1,12 @@ +SRC=$(wildcard *.c) +OBJ=$(SRC:.c=.o) +OUT = libSDL_net.a + +all: $(OBJ) + ar -crs $(OUT) $(OBJ) + +$(%.o): %.c + $(CC) -c -o $@ $< + +clean: + $(RM) *.o *.a diff --git a/server/udp_network_utility.hpp b/server/udp_network_utility.hpp new file mode 100644 index 0000000..16af210 --- /dev/null +++ b/server/udp_network_utility.hpp @@ -0,0 +1,52 @@ +#ifndef UDPNETWORKUTILITY_HPP_ +#define UDPNETWORKUTILITY_HPP_ + +#include "SDL_net/SDL_net.h" + +class UDPNetworkUtility { +public: + UDPNetworkUtility() = default; + ~UDPNetworkUtility() = default; + + void Open(int port, int packSize); + void Close(); + + //bind to an available channel + int Bind(const char* ip, int port) { + Bind(ip, port, -1); + } + int Bind(IPaddress add) { + Bind(add, -1); + } + + //bind to certain channel + int Bind(const char* ip, int port, int channel); + int Bind(IPaddress add, int channel); + void Unbind(int channel); + + IPaddress* GetIPAddress(int channel) { + return SDLNet_GetPeerAddress(socket, channel); + } + + int Send(int channel, void* data, int len); + int Receive(); + + void* GetOutData() const { + return reinterpret_cast(packOut->data); + }; + void* GetInData() const { + return reinterpret_cast(packIn->data); + }; + void* GetOutPacket() const { + return packOut; + } + void* GetInPacket() const { + return packIn; + } +private: + UDPsocket socket = nullptr; + UDPpacket* packOut = nullptr; + UDPpacket* packIn = nullptr; +}; + +#endif