Wrapper completed & tested; adjusted many systems
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -35,5 +35,3 @@ clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
|
||||
unit:
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
#include "udp_network_utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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<void*>(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;
|
||||
|
||||
+32
-7
@@ -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
|
||||
|
||||
+8
-6
@@ -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)
|
||||
rebuild: clean all
|
||||
|
||||
+10
-2
@@ -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<char*>(netUtil.GetInData()) << endl;
|
||||
}
|
||||
//accept new connections
|
||||
//accept updates from the clients
|
||||
//read the updates from the clients into internal containers
|
||||
|
||||
+2
-2
@@ -1,10 +1,9 @@
|
||||
#ifndef SERVER_HPP_
|
||||
#define SERVER_HPP_
|
||||
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "config_utility.hpp"
|
||||
|
||||
#include <list>
|
||||
|
||||
class Server {
|
||||
public:
|
||||
Server() = default;
|
||||
@@ -20,6 +19,7 @@ public:
|
||||
private:
|
||||
bool running = false;
|
||||
ConfigUtility config;
|
||||
UDPNetworkUtility netUtil;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+19
-2
@@ -1,9 +1,26 @@
|
||||
#include "udp_network_utility.hpp"
|
||||
|
||||
#include "SDL_net/SDL_net.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user