From f0d4ef06751b4ac552058c48dacaf1da8ed82281 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 4 May 2013 14:00:30 +1000 Subject: [PATCH] Changed my mind about networking system structure --- docs/server.txt | 28 +++++++++++++++ server/client.cpp | 5 +++ server/client.hpp | 21 +++++++++++ server/makefile | 4 +-- server/server.cpp | 10 +++--- server/server.hpp | 2 -- server/tcp_network_manager.cpp | 65 ---------------------------------- server/tcp_network_manager.hpp | 31 ---------------- 8 files changed, 61 insertions(+), 105 deletions(-) create mode 100644 server/client.cpp create mode 100644 server/client.hpp delete mode 100644 server/tcp_network_manager.cpp delete mode 100644 server/tcp_network_manager.hpp diff --git a/docs/server.txt b/docs/server.txt index 178efbc..2f15fa6 100644 --- a/docs/server.txt +++ b/docs/server.txt @@ -27,5 +27,33 @@ This outline is only the most basic example of the server's operation possible, class Client: ID --unique, incremental socket --connection socket + + GetID + GetSocket + GetIP --wrapper for GetPeerAddress + + --handle dropped connections by destroying this client + int Send(data, len); + int Recv(data, maxlen); + + --close the socket, and cleanup any other stuff + CloseSocket() + [stuff] [things] + +class ClientManager: + Init(max) + Quit() + + CheckNewClients() + Send(id, data, len) + SendAll(data, len) + Recv(id, data, maxlen) + + Get(id) + Close(id) + + GetRaw() --the internal container + + --iteratable diff --git a/server/client.cpp b/server/client.cpp new file mode 100644 index 0000000..f777fb3 --- /dev/null +++ b/server/client.cpp @@ -0,0 +1,5 @@ +#include "client.hpp" + +Client::Client(int id, TCPsocket) { + // +} diff --git a/server/client.hpp b/server/client.hpp new file mode 100644 index 0000000..7476e91 --- /dev/null +++ b/server/client.hpp @@ -0,0 +1,21 @@ +#ifndef CLIENT_HPP_ +#define CLIENT_HPP_ + +#include "SDL_net/SDL_net.h" + +class Client { +public: + Client(int id, TCPsocket); + + int GetID() const { + return id; + } + TCPsocket GetSocket() const { + return socket; + } +private: + int id; + TCPsocket socket; +}; + +#endif diff --git a/server/makefile b/server/makefile index 44466c8..c1fed57 100644 --- a/server/makefile +++ b/server/makefile @@ -4,14 +4,14 @@ LIB=-lmingw32 -lSDL_net -lSDLmain -lSDL -lwsock32 -liphlpapi #objects OBJDIR=obj -OBJ=$(addprefix $(OBJDIR)/,main.o config_utility.o) +OBJ=$(addprefix $(OBJDIR)/,main.o config_utility.o client.o) #output OUTDIR=out OUT=$(addprefix $(OUTDIR)/,a) #source -SRC=server.cpp tcp_network_manager.cpp +SRC=server.cpp #targets all: $(OBJ) $(OUT) diff --git a/server/server.cpp b/server/server.cpp index 7fc20c8..f14be06 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -21,7 +21,6 @@ void Server::Init() { if (SDLNet_Init()) { throw(runtime_error("Failed to init SDL_net")); } - netMgr.Init(config.Integer("port"), config.Integer("maxplayers")); } void Server::Proc() { @@ -36,21 +35,22 @@ void Server::Proc() { } void Server::Quit() { - netMgr.Quit(); SDLNet_Quit(); } void Server::HandleInput() { //accept new connections - netMgr.AcceptConnections(); + //... //accept updates from the clients - netMgr.CheckSockets(); + //... //read the updates from the clients into internal containers //... } void Server::UpdateWorld() { - //update internals + //update internals ie. + // ai + // loot drops } void Server::HandleOutput() { diff --git a/server/server.hpp b/server/server.hpp index c2a4ccc..25c2ad8 100644 --- a/server/server.hpp +++ b/server/server.hpp @@ -1,7 +1,6 @@ #ifndef SERVER_HPP_ #define SERVER_HPP_ -#include "tcp_network_manager.hpp" #include "config_utility.hpp" class Server { @@ -18,7 +17,6 @@ public: void HandleOutput(); private: bool running; - TCPNetworkManager netMgr; ConfigUtility config; }; diff --git a/server/tcp_network_manager.cpp b/server/tcp_network_manager.cpp deleted file mode 100644 index 25dc8dd..0000000 --- a/server/tcp_network_manager.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "tcp_network_manager.hpp" - -#include - -TCPNetworkManager::TCPNetworkManager() { - // -} - -TCPNetworkManager::~TCPNetworkManager() { - // -} - -void TCPNetworkManager::Init(Uint16 port, int maxSockets) { - IPaddress add; - if (SDLNet_ResolveHost(&add, nullptr, port)) { - throw(std::runtime_error("Failed to resolve the host")); - } - if (!(sock = SDLNet_TCP_Open(&add))) { - throw(std::runtime_error("Failed to create the server socket")); - } - if (!(clientSocks = SDLNet_AllocSocketSet(maxSockets))) { - throw(std::runtime_error("Failed to allocate the socket set")); - } -} - -void TCPNetworkManager::Quit() { - SDLNet_FreeSocketSet(clientSocks); - SDLNet_TCP_Close(sock); -} - -int TCPNetworkManager::AcceptConnections() { - // -} - -int TCPNetworkManager::CheckSockets() { - // -} - -int TCPNetworkManager::Send(int index, const void* data, int len) { - // -} - -int TCPNetworkManager::SendAll(const void* data, int len) { - // -} - -int TCPNetworkManager::OpenSocket(const char* host, int port) { - // -} - -TCPsocket TCPNetworkManager::GetSocket(int index) { - // -} - -int TCPNetworkManager::CloseSocket(int index) { - // -} - -int TCPNetworkManager::GetMaxConnections() const { - // -} - -int TCPNetworkManager::GetCurrentConnections() const { - // -} diff --git a/server/tcp_network_manager.hpp b/server/tcp_network_manager.hpp deleted file mode 100644 index 6a2ad72..0000000 --- a/server/tcp_network_manager.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef TCPNETWORKMANAGER_HPP_ -#define TCPNETWORKMANAGER_HPP_ - -#include "SDL_net/SDL_net.h" - -class TCPNetworkManager { -public: - TCPNetworkManager(); - ~TCPNetworkManager(); - - void Init(Uint16 port, int maxSockets); - void Quit(); - - int AcceptConnections(); - int CheckSockets(); - - int Send(int index, const void* data, int len); - int SendAll(const void* data, int len); - - int AddSocket(const char* host, int port); - TCPsocket GetSocket(int index); - int CloseSocket(int index); - - int GetMaxConnections() const; - int GetCurrentConnections() const; -private: - TCPsocket server; - SDLNet_SocketSet socketSet; -}; - -#endif