Changed my mind about networking system structure
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#include "client.hpp"
|
||||
|
||||
Client::Client(int id, TCPsocket) {
|
||||
//
|
||||
}
|
||||
@@ -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
|
||||
+2
-2
@@ -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)
|
||||
|
||||
+5
-5
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
#include "tcp_network_manager.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
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 {
|
||||
//
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user