SDL_net is working

This commit is contained in:
Kayne Ruse
2013-09-09 12:15:26 +10:00
parent 6c5197f3f2
commit 1dfeabf195
7 changed files with 277 additions and 51 deletions
-25
View File
@@ -1,25 +0,0 @@
/* Copyright: (c) Kayne Ruse 2013
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "client_manager.hpp"
ClientManager ClientManager::instance;
-44
View File
@@ -1,44 +0,0 @@
/* Copyright: (c) Kayne Ruse 2013
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef CLIENTMANAGER_HPP_
#define CLIENTMANAGER_HPP_
#include <list>
class ClientManager {
private:
ClientManager() = default;
~ClientManager() = default;
static ClientManager instance;
public:
static ClientManager* GetInstance() { return &instance; }
private:
struct ClientEntry {
int index;
};
std::list<ClientEntry> list;
};
#endif
+1 -1
View File
@@ -1,7 +1,7 @@
#config
COMMONDIR+=../common
COMMON+=../libcommon.a
LIB+=$(COMMON) -lmingw32 -lSDLmain -lSDL -llua -lsqlite3
LIB+=$(COMMON) -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(COMMONDIR))
CFLAGS+=-DDEBUG $(addprefix -I,$(COMMONDIR))
+13 -9
View File
@@ -21,11 +21,14 @@
*/
#include "server_application.hpp"
#include "network_packet.hpp"
#include <stdexcept>
#include <iostream>
#include <string>
#include <fstream>
int ClientEntry::indexCounter = 0;
ServerApplication ServerApplication::instance;
ServerApplication::ServerApplication() {
@@ -37,6 +40,8 @@ ServerApplication::~ServerApplication() {
}
void ServerApplication::Init(int argc, char** argv) {
//TODO: proper command line option parsing
//Check thread safety
if (!sqlite3_threadsafe()) {
throw(std::runtime_error("Cannot run without thread safety"));
@@ -53,14 +58,14 @@ void ServerApplication::Init(int argc, char** argv) {
std::cout << "SDL initialized" << std::endl;
}
//Init lua
if (!(luaState = luaL_newstate())) {
throw(std::runtime_error("Failed to create the lua state"));
//Init SDL_net
if (SDLNet_Init()) {
throw(std::runtime_error("Failed to init SDL_net"));
}
else {
std::cout << "lua initialized" << std::endl;
std::cout << "SDL_net initialized" << std::endl;
}
luaL_openlibs(luaState);
networkUtil.Open(8895, 1024);
//Init SQL
std::string dbname = (argc > 1) ? argv[1] : argv[0];
@@ -73,8 +78,6 @@ void ServerApplication::Init(int argc, char** argv) {
}
//Run setup scripts
luaL_dofile(luaState, "rsc/setup_server.lua");
std::ifstream is("rsc/setup_server.sql");
if (!is.is_open()) {
throw(std::runtime_error("Failed to run database setup script"));
@@ -90,11 +93,12 @@ void ServerApplication::Init(int argc, char** argv) {
}
void ServerApplication::Loop() {
//TODO
//
}
void ServerApplication::Quit() {
sqlite3_close_v2(database);
lua_close(luaState);
networkUtil.Close();
SDLNet_Quit();
SDL_Quit();
}
+14 -2
View File
@@ -22,10 +22,20 @@
#ifndef SERVERAPPLICATION_HPP_
#define SERVERAPPLICATION_HPP_
#include "lua/lua.hpp"
#include "udp_network_utility.hpp"
#include "sqlite3/sqlite3.h"
#include "SDL/SDL.h"
#include <list>
//hold the info about the clients
struct ClientEntry {
static int indexCounter;
int index = indexCounter++;
IPaddress add = {0, 0};
};
//The main application class
class ServerApplication {
private:
@@ -43,8 +53,10 @@ public:
private:
bool running = true;
lua_State* luaState = nullptr;
sqlite3* database = nullptr;
UDPNetworkUtility networkUtil;
std::list<ClientEntry> clientEntries;
};
#endif