What am I doing wrong?
This commit is contained in:
+9
-1
@@ -10,6 +10,7 @@ using namespace std;
|
|||||||
|
|
||||||
Server::Server() {
|
Server::Server() {
|
||||||
running = true;
|
running = true;
|
||||||
|
config.Load("config.cfg");
|
||||||
}
|
}
|
||||||
|
|
||||||
Server::~Server() {
|
Server::~Server() {
|
||||||
@@ -20,7 +21,7 @@ void Server::Init() {
|
|||||||
if (SDLNet_Init()) {
|
if (SDLNet_Init()) {
|
||||||
throw(runtime_error("Failed to init SDL_net"));
|
throw(runtime_error("Failed to init SDL_net"));
|
||||||
}
|
}
|
||||||
netMgr.Init(100);
|
netMgr.Init(config.Integer("port"), config.Integer("maxplayers"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::Proc() {
|
void Server::Proc() {
|
||||||
@@ -28,6 +29,9 @@ void Server::Proc() {
|
|||||||
HandleInput();
|
HandleInput();
|
||||||
UpdateWorld();
|
UpdateWorld();
|
||||||
HandleOutput();
|
HandleOutput();
|
||||||
|
|
||||||
|
//debug
|
||||||
|
running = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +42,11 @@ void Server::Quit() {
|
|||||||
|
|
||||||
void Server::HandleInput() {
|
void Server::HandleInput() {
|
||||||
//accept new connections
|
//accept new connections
|
||||||
|
netMgr.AcceptConnections();
|
||||||
//accept updates from the clients
|
//accept updates from the clients
|
||||||
|
netMgr.CheckSockets();
|
||||||
|
//read the updates from the clients into internal containers
|
||||||
|
//...
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::UpdateWorld() {
|
void Server::UpdateWorld() {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define SERVER_HPP_
|
#define SERVER_HPP_
|
||||||
|
|
||||||
#include "tcp_network_manager.hpp"
|
#include "tcp_network_manager.hpp"
|
||||||
|
#include "config_utility.hpp"
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
public:
|
public:
|
||||||
@@ -18,6 +19,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
bool running;
|
bool running;
|
||||||
TCPNetworkManager netMgr;
|
TCPNetworkManager netMgr;
|
||||||
|
ConfigUtility config;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,22 +1,34 @@
|
|||||||
#include "tcp_network_manager.hpp"
|
#include "tcp_network_manager.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
TCPNetworkManager::TCPNetworkManager() {
|
TCPNetworkManager::TCPNetworkManager() {
|
||||||
maxConnections = currentConnections = 0;
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
TCPNetworkManager::~TCPNetworkManager() {
|
TCPNetworkManager::~TCPNetworkManager() {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPNetworkManager::Init(int maxConnections) {
|
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() {
|
void TCPNetworkManager::Quit() {
|
||||||
//
|
SDLNet_FreeSocketSet(clientSocks);
|
||||||
|
SDLNet_TCP_Close(sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
int TCPNetworkManager::AcceptConnection() {
|
int TCPNetworkManager::AcceptConnections() {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,10 +56,10 @@ int TCPNetworkManager::CloseSocket(int index) {
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
int TCPNetworkManager::GetMaxConnections() {
|
int TCPNetworkManager::GetMaxConnections() const {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
int TCPNetworkManager::GetCurrentConnections() {
|
int TCPNetworkManager::GetCurrentConnections() const {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ public:
|
|||||||
TCPNetworkManager();
|
TCPNetworkManager();
|
||||||
~TCPNetworkManager();
|
~TCPNetworkManager();
|
||||||
|
|
||||||
void Init(int maxConnections);
|
void Init(Uint16 port, int maxSockets);
|
||||||
void Quit();
|
void Quit();
|
||||||
|
|
||||||
int AcceptConnection();
|
int AcceptConnections();
|
||||||
int CheckSockets();
|
int CheckSockets();
|
||||||
|
|
||||||
int Send(int index, const void* data, int len);
|
int Send(int index, const void* data, int len);
|
||||||
@@ -21,12 +21,11 @@ public:
|
|||||||
TCPsocket GetSocket(int index);
|
TCPsocket GetSocket(int index);
|
||||||
int CloseSocket(int index);
|
int CloseSocket(int index);
|
||||||
|
|
||||||
int GetMaxConnections();
|
int GetMaxConnections() const;
|
||||||
int GetCurrentConnections();
|
int GetCurrentConnections() const;
|
||||||
private:
|
private:
|
||||||
TCPsocket serverSocket;
|
TCPsocket sock;
|
||||||
SDLNet_SocketSet socketSet;
|
SDLNet_SocketSet clientSocks;
|
||||||
int maxConnections, currentConnections;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user