diff --git a/README.md b/README.md index 57f079b..e496780 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,15 @@ Tortuga is a 3/4 top down multiplayer RPG set in a large archipelago occupied by ## Libraries * [SDL](http://www.libsdl.org/) - Simple DirectMedia Layer API -* [SDL_net](http://www.libsdl.org/projects/SDL_net/) - SDL's networking extension; source included under libs/SDL_net +* [SDL_net](http://www.libsdl.org/projects/SDL_net/) - SDL's networking extension; modified source included under libs/SDL_net * [Codebase](https://github.com/Ratstail91/Codebase) - files included locally; modifications will be included upstream further into this project. ## Instructions -This project uses C++11, which is available via GNU (or MinGW) 4.7, or Visual Studio 2012. Personally, I'm using MinGW 4.7.2. I'm trying to keep this as IDE agnostic as possible, so if you use an IDE, please add it's files to .gitignore. You can read more details on the GitHub wiki [here](https://github.com/Ratstail91/Tortuga/wiki). +* This project uses C++11, which is available via GNU (or MinGW) 4.7, or Visual Studio 2012. Personally, I'm using MinGW 4.7.2. +* You need to build the library in the common/ directroy before any others. +* I'm trying to keep this as IDE agnostic as possible, so if you use an IDE, please add it's files to .gitignore. +* You can read more details on the GitHub wiki [here](https://github.com/Ratstail91/Tortuga/wiki). ## Copyright diff --git a/client/makefile b/client/makefile index c2427f2..109dd77 100644 --- a/client/makefile +++ b/client/makefile @@ -35,5 +35,3 @@ clean: $(RM) *.o *.a *.exe rebuild: clean all - -unit: diff --git a/common/udp_network_utility.cpp b/common/udp_network_utility.cpp new file mode 100644 index 0000000..4209634 --- /dev/null +++ b/common/udp_network_utility.cpp @@ -0,0 +1,106 @@ +#include "udp_network_utility.hpp" + +#include + +void UDPNetworkUtility::Open(int port, int packSize) { + if (!(socket = SDLNet_UDP_Open(port))) { + Close(); + throw(std::runtime_error("Failed to open a UDP socket")); + } + + if (!(packOut = SDLNet_AllocPacket(packSize))) { + Close(); + throw(std::runtime_error("Failed to allocate the out packet")); + } + + if (!(packIn = SDLNet_AllocPacket(packSize))) { + Close(); + throw(std::runtime_error("Failed to allocate the in packet")); + } +} + +void UDPNetworkUtility::Close() { + SDLNet_UDP_Close(socket); + SDLNet_FreePacket(packOut); + SDLNet_FreePacket(packIn); + socket = nullptr; + packOut = nullptr; + packIn = nullptr; +} + +int UDPNetworkUtility::Bind(const char* ip, int port, int channel) { + IPaddress add; + if (SDLNet_ResolveHost(&add, ip, port) == -1) { + throw(std::runtime_error("Failed to resolve a host")); + } + + return Bind(&add, channel); +} + +int UDPNetworkUtility::Bind(IPaddress* add, int channel) { + int ret = SDLNet_UDP_Bind(socket, channel, add); + + if (ret == -1) { + throw(std::runtime_error("Failed to bind to a channel")); + } + + return ret; +} + +void UDPNetworkUtility::Unbind(int channel) { + SDLNet_UDP_Unbind(socket, channel); +} + +int UDPNetworkUtility::Send(const char* ip, int port, void* data, int len) { + IPaddress add; + if (SDLNet_ResolveHost(&add, ip, port) == -1) { + throw(std::runtime_error("Failed to resolve a host")); + } + + Send(&add, data, len); +} + +int UDPNetworkUtility::Send(IPaddress* add, void* data, int len) { + if (len > packOut->maxlen) { + throw(std::runtime_error("Failed to copy the data into the packet")); + } + memset(packOut->data, 0, packOut->maxlen); + memcpy(packOut->data, data, len); + packOut->len = len; + packOut->address = *add; + + int ret = SDLNet_UDP_Send(socket, -1, packOut); + + if (ret <= 0) { + throw(std::runtime_error("Failed to send a packet")); + } + + return ret; +} + +int UDPNetworkUtility::Send(int channel, void* data, int len) { + if (len > packOut->maxlen) { + throw(std::runtime_error("Failed to copy the data into the packet")); + } + memset(packOut->data, 0, packOut->maxlen); + memcpy(packOut->data, data, len); + packOut->len = len; + + int ret = SDLNet_UDP_Send(socket, channel, packOut); + + if (ret <= 0) { + throw(std::runtime_error("Failed to send a packet")); + } + + return ret; +} + +int UDPNetworkUtility::Receive() { + int ret = SDLNet_UDP_Recv(socket, packIn); + + if (ret < 0) { + throw(std::runtime_error("Unknown network error occured")); + } + + return ret; +} diff --git a/common/udp_network_utility.hpp b/common/udp_network_utility.hpp index 16af210..1a1679f 100644 --- a/common/udp_network_utility.hpp +++ b/common/udp_network_utility.hpp @@ -15,19 +15,21 @@ public: int Bind(const char* ip, int port) { Bind(ip, port, -1); } - int Bind(IPaddress add) { + 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); + int Bind(IPaddress* add, int channel); void Unbind(int channel); IPaddress* GetIPAddress(int channel) { - return SDLNet_GetPeerAddress(socket, channel); + return SDLNet_UDP_GetPeerAddress(socket, channel); } + int Send(const char* ip, int port, void* data, int len); + int Send(IPaddress* add, void* data, int len); int Send(int channel, void* data, int len); int Receive(); @@ -37,12 +39,15 @@ public: void* GetInData() const { return reinterpret_cast(packIn->data); }; - void* GetOutPacket() const { + UDPpacket* GetOutPacket() const { return packOut; } - void* GetInPacket() const { + UDPpacket* GetInPacket() const { return packIn; } + UDPsocket GetSocket() const { + return socket; + } private: UDPsocket socket = nullptr; UDPpacket* packOut = nullptr; diff --git a/libs/SDL_net/makefile b/libs/SDL_net/makefile index 0c460ce..e3fde37 100644 --- a/libs/SDL_net/makefile +++ b/libs/SDL_net/makefile @@ -1,12 +1,37 @@ -SRC=$(wildcard *.c) -OBJ=$(SRC:.c=.o) -OUT = libSDL_net.a +#config +INCLUDES= +CFLAGS+=$(addprefix -I,$(INCLUDES)) +LIB= -all: $(OBJ) +#source +SRC=$(wildcard *.c) + +#objects +OBJDIR=obj +OBJ=$(addprefix $(OBJDIR)/,$(SRC:.c=.o)) + +#output +OUTDIR=out +OUT=$(addprefix $(OUTDIR)/,libSDL_net.a) + +#targets +all: $(OBJ) $(OUT) ar -crs $(OUT) $(OBJ) -$(%.o): %.c - $(CC) -c -o $@ $< +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< clean: - $(RM) *.o *.a + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/server/makefile b/server/makefile index 4ae339d..b287ac6 100644 --- a/server/makefile +++ b/server/makefile @@ -14,8 +14,13 @@ OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o)) OUTDIR=out #targets -all: $(OBJ) $(OUT) - $(CXX) $(CXXFLAGS) -o $(OUTDIR)/server main.cpp $(OBJ) $(LIB) +all: server unit + +server: $(OBJ) $(OUT) + $(CXX) $(CXXFLAGS) -o $(OUTDIR)/$@ main.cpp $(OBJ) $(LIB) + +unit: $(OBJ) $(OUT) + $(CXX) $(CXXFLAGS) -o $(OUTDIR)/$@ unit.cpp $(OBJ) $(LIB) $(OBJ): | $(OBJDIR) @@ -33,7 +38,4 @@ $(OBJDIR)/%.o: %.cpp clean: $(RM) *.o *.a *.exe -rebuild: clean all unit - -unit: $(OBJ) $(OUT) - $(CXX) $(CXXFLAGS) -o $(OUTDIR)\unit unit.cpp $(OBJ) $(LIB) \ No newline at end of file +rebuild: clean all diff --git a/server/server.cpp b/server/server.cpp index cf195e3..2ab5607 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -6,7 +6,11 @@ using namespace std; void Server::Init() { + if (SDLNet_Init()) { + throw(runtime_error("Failed to initialize SDL_net")); + } config.Load("config.cfg"); + netUtil.Open(config.Integer("port"), 512); running = true; } @@ -17,15 +21,19 @@ void Server::Proc() { HandleOutput(); //debug - running = false; +// running = false; } } void Server::Quit() { - // + netUtil.Close(); + SDLNet_Quit(); } void Server::HandleInput() { + while(netUtil.Receive()) { + cout << reinterpret_cast(netUtil.GetInData()) << endl; + } //accept new connections //accept updates from the clients //read the updates from the clients into internal containers diff --git a/server/server.hpp b/server/server.hpp index ad45e55..6f81e13 100644 --- a/server/server.hpp +++ b/server/server.hpp @@ -1,10 +1,9 @@ #ifndef SERVER_HPP_ #define SERVER_HPP_ +#include "udp_network_utility.hpp" #include "config_utility.hpp" -#include - class Server { public: Server() = default; @@ -20,6 +19,7 @@ public: private: bool running = false; ConfigUtility config; + UDPNetworkUtility netUtil; }; #endif diff --git a/server/unit.cpp b/server/unit.cpp index 211536d..e2be63f 100644 --- a/server/unit.cpp +++ b/server/unit.cpp @@ -1,9 +1,26 @@ +#include "udp_network_utility.hpp" + +#include "SDL_net/SDL_net.h" + #include +#include using namespace std; -//receive any amount of info and print it - int main(int, char**) { + if (SDLNet_Init()) { + cerr << "Failed to init SDL_net" << endl; + return -1; + } + UDPNetworkUtility netUtil; + + netUtil.Open(0, 512); + + string s = "Hello world"; + netUtil.Send("127.0.0.1", 2000, (void*)s.c_str(), s.length()); + + netUtil.Close(); + + SDLNet_Quit(); return 0; } \ No newline at end of file