diff --git a/docs/pseudocode.txt b/docs/pseudocode.txt index 4c66e79..7355565 100644 --- a/docs/pseudocode.txt +++ b/docs/pseudocode.txt @@ -16,6 +16,7 @@ Player: [graphical stuff] position velocity + avatarName ------------------------- @@ -34,7 +35,7 @@ PlayerManager: ------------------------- -Rememer: Top down programming/K.I.S.S. +Remember: Top down programming/K.I.S.S. KeyDown: up: @@ -52,3 +53,14 @@ end ------------------------- +TCP systems + +TCPNetworkManager: + Init //opens the TCP server socket, and allocates the socket set + Quit //close the TCP server socket, and free the socket set + AcceptConnections //accept new connections + Send //send to a specific client, closing the connection if an error occured + SendAll //send to all clients + CheckSockets //non-blocking + GetSocket() //get a specific socket + total connections in the packet \ No newline at end of file diff --git a/server/main.cpp b/server/main.cpp new file mode 100644 index 0000000..3e762ec --- /dev/null +++ b/server/main.cpp @@ -0,0 +1,26 @@ +#include "server.hpp" + +#include +#include + +using namespace std; + +int main(int, char**) { +#ifdef DEBUG + cout << "Beginning server" << endl; +#endif + Server app; + try { + app.Init(); + app.Proc(); + app.Quit(); + } + catch(exception& e) { + cerr << "Fatal error: " << e.what() << endl; + return 1; + } +#ifdef DEBUG + cout << "Clean exit" << endl; +#endif + return 0; +} diff --git a/server/makefile b/server/makefile index 09f6683..60a4629 100644 --- a/server/makefile +++ b/server/makefile @@ -1,19 +1,38 @@ -CXXFLAGS+=-std=c++11 -DFLAGS=-DDEBUG +#config +CXXFLAGS+=-std=c++11 -DDEBUG LIB=-lmingw32 -lSDL_net -lSDLmain -lSDL -lwsock32 -liphlpapi -OBJ= -SRC=server.cpp -all: debug +#objects +OBJDIR=obj +OBJ=$(addprefix $(OBJDIR)/,main.o) -release: $(OBJ) - $(CXX) $(CXXFLAGS) $(SRC) $(OBJ) $(LIB) +#output +OUTDIR=out +OUT=$(addprefix $(OUTDIR)/,a) -debug: $(OBJ) - $(CXX) $(CXXFLAGS) $(DFLAGS) $(SRC) $(OBJ) $(LIB) +#source +SRC=server.cpp tcp_network_manager.cpp + +#targets +all: $(OBJ) $(OUT) + $(CXX) $(CXXFLAGS) -o $(OUT) $(SRC) $(OBJ) $(LIB) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $(@) $< clean: - -$(RM) *.o *.a *.exe + $(RM) *.o *.a *.exe + +rebuild: clean all unit: - $(CXX) $(CXXFLAGS) unit.cpp diff --git a/server/server.cpp b/server/server.cpp index fcf4a0d..2e1ca0e 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -1,8 +1,33 @@ +#include "server.hpp" + #include "SDL/SDL.h" #include "SDL_net/SDL_net.h" -#include +#include -int SDL_main(int, char**) { - return 0; -} \ No newline at end of file +using namespace std; + +Server::Server() { + // +} + +Server::~Server() { + // +} + +void Server::Init() { + if (SDLNet_Init()) { + throw(runtime_error("Failed to init SDL_net")); + } +} + +void Server::Proc() { + bool running = true; + while(running) { + // + } +} + +void Server::Quit() { + SDLNet_Quit(); +} diff --git a/server/server.hpp b/server/server.hpp new file mode 100644 index 0000000..e9a7bc2 --- /dev/null +++ b/server/server.hpp @@ -0,0 +1,15 @@ +#ifndef SERVER_HPP_ +#define SERVER_HPP_ + +class Server { +public: + Server(); + ~Server(); + + void Init(); + void Proc(); + void Quit(); +private: +}; + +#endif diff --git a/server/tcp_network_manager.cpp b/server/tcp_network_manager.cpp new file mode 100644 index 0000000..8b94885 --- /dev/null +++ b/server/tcp_network_manager.cpp @@ -0,0 +1,2 @@ +#include "tcp_network_manager.hpp" + diff --git a/server/tcp_network_manager.hpp b/server/tcp_network_manager.hpp new file mode 100644 index 0000000..9202554 --- /dev/null +++ b/server/tcp_network_manager.hpp @@ -0,0 +1,32 @@ +#ifndef TCPNETWORKMANAGER_HPP_ +#define TCPNETWORKMANAGER_HPP_ + +#include "SDL_net/SDL_net.h" + +class TCPNetworkManager { +public: + TCPNetworkManager(); + ~TCPNetworkManager(); + + void Init(int maxConnections); + void Quit(); + + int AcceptConnections(); + int CheckSockets(); + + int Send(int index, const void* data, int len); + int SendAll(const void* data, int len); + + int OpenSocket(const char* host, int port); + TCPsocket GetSocket(int index); + int CloseSocket(int index); + + int GetMaxConnections(); + int GetCurrentConnections(); +private: + TCPsocket serverSocket; + SDLNet_SocketSet socketSet; + int maxConnections, currentConnections; +}; + +#endif