Added a brand new bug, I hate this branch

This commit is contained in:
Kayne Ruse
2014-03-10 23:15:57 +11:00
parent 5cf62f5517
commit 19c1b1197d
6 changed files with 58 additions and 315 deletions
-75
View File
@@ -1,75 +0,0 @@
/* Copyright: (c) Kayne Ruse 2014
*
* 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"
int ClientManager::HandleConnection(IPaddress add) {
ClientEntry c;
c.address = add;
clientMap[counter] = c;
return counter++;
}
int ClientManager::HandleDisconnection(int i) {
for (auto& it : clientMap) {
if (it.first == i) {
clientMap.erase(it.first);
return 0;
}
}
return -1;
}
void ClientManager::ForEach(std::function<void(Iterator)> fn) {
for(Iterator it = clientMap.begin(); it != clientMap.end(); it++) {
fn(it);
}
}
void ClientManager::EraseIf(std::function<bool(Iterator)> fn) {
for(Iterator it = clientMap.begin(); it != clientMap.end(); /* empty */) {
if(fn(it)) {
it = clientMap.erase(it);
}
else {
++it;
}
}
}
ClientEntry* ClientManager::GetClient(int i) {
for (auto& it : clientMap) {
if (it.first == i) {
return &it.second;
}
}
return nullptr;
}
ClientEntry* ClientManager::GetClient(IPaddress add) {
for (auto& it : clientMap) {
if (it.second.address.host == add.host &&
it.second.address.port == add.port) {
return &it.second;
}
}
return nullptr;
}
-57
View File
@@ -1,57 +0,0 @@
/* Copyright: (c) Kayne Ruse 2014
*
* 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 "SDL/SDL_net.h"
#include <functional>
#include <map>
struct ClientEntry {
IPaddress address;
};
class ClientManager {
public:
//clarity typedefs
typedef std::map<int, ClientEntry> Container;
typedef Container::iterator Iterator;
//returns the internal index
int HandleConnection(IPaddress);
int HandleDisconnection(int i);
//lambdas
void ForEach(std::function<void(Iterator)> fn);
void EraseIf(std::function<bool(Iterator)> fn);
//accessors
ClientEntry* GetClient(int i);
ClientEntry* GetClient(IPaddress);
int Size() { return clientMap.size(); }
private:
Container clientMap;
int counter = 0;
};
#endif
-70
View File
@@ -1,70 +0,0 @@
/* Copyright: (c) Kayne Ruse 2014
*
* 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 "player_manager.hpp"
int PlayerManager::HandlePlayerCreation(std::string name, std::string avatar) {
//
}
int PlayerManager::HandlePlayerDeletion(int uniqueID) {
//
}
int PlayerManager::HandlePlayerLoad(int uniqueID, int clientIndex) {
//
}
int PlayerManager::HandlePlayerUnload(int uniqueID) {
//
}
void PlayerManager::ForEach(std::function<void(Iterator)> fn) {
for(Iterator it = playerMap.begin(); it != playerMap.end(); it++) {
fn(it);
}
}
void PlayerManager::EraseIf(std::function<bool(Iterator)> fn) {
for(Iterator it = playerMap.begin(); it != playerMap.end(); /* empty */) {
if(fn(it)) {
it = playerMap.erase(it);
}
else {
++it;
}
}
}
PlayerEntry* PlayerManager::GetPlayer(int uniqueID) {
for (auto& it : playerMap) {
if (it.first == uniqueID) {
return &it.second;
}
}
return nullptr;
}
void PlayerManager::Update(double delta) {
for (auto& it : playerMap) {
it.second.position += it.second.motion * delta;
}
}
-71
View File
@@ -1,71 +0,0 @@
/* Copyright: (c) Kayne Ruse 2014
*
* 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 PLAYERMANAGER_HPP_
#define PLAYERMANAGER_HPP_
#include "vector2.hpp"
#include "sqlite3/sqlite3.h"
#include <functional>
#include <map>
#include <string>
struct PlayerEntry {
int clientIndex;
int mapIndex;
Vector2 position;
Vector2 motion;
};
class PlayerManager {
public:
//clarity typedefs
typedef std::map<int, PlayerEntry> Container;
typedef Container::iterator Iterator;
//These functions interact with the database
//*Deletion, *Load and *Unload returns: 0 success, -1 failure
//*Creation returns the uniqueID, but doesn't load
// that object; call *Load directly afterward
int HandlePlayerCreation (std::string name, std::string avatar);
int HandlePlayerDeletion (int uniqueID);
int HandlePlayerLoad (int uniqueID, int clientIndex);
int HandlePlayerUnload (int uniqueID);
//lambdas
void ForEach(std::function<void(Iterator)> fn);
void EraseIf(std::function<bool(Iterator)> fn);
//accessors
PlayerEntry* GetPlayer(int uniqueID);
sqlite3* SetDatabase(sqlite3* db) { return database = db; }
sqlite3* GetDatabase() { return database; }
//update each player's position
void Update(double delta);
private:
Container playerMap;
sqlite3* database = nullptr;
};
#endif
+40 -36
View File
@@ -74,7 +74,6 @@ void ServerApplication::Init(int argc, char** argv) {
if (ret != SQLITE_OK || !database) {
throw(runtime_error(string() + "Failed to initialize SQL: " + sqlite3_errmsg(database) ));
}
playerMgr.SetDatabase(database);
cout << "Initialized SQL" << endl;
//setup the database
@@ -126,7 +125,6 @@ void ServerApplication::Quit() {
//APIs
lua_close(luaState);
playerMgr.SetDatabase(nullptr);
sqlite3_close_v2(database);
network.Close();
SDLNet_Quit();
@@ -156,13 +154,13 @@ void ServerApplication::HandlePacket(NetworkPacket packet) {
HandleShutdown(packet);
break;
case NetworkPacket::Type::PLAYER_NEW:
// HandlePlayerNew(packet);
HandlePlayerNew(packet);
break;
case NetworkPacket::Type::PLAYER_DELETE:
// HandlePlayerDelete(packet);
HandlePlayerDelete(packet);
break;
case NetworkPacket::Type::PLAYER_UPDATE:
// HandlePlayerUpdate(packet);
HandlePlayerUpdate(packet);
break;
//handle errors
default:
@@ -188,35 +186,40 @@ void ServerApplication::HandleBroadcastRequest(NetworkPacket packet) {
void ServerApplication::HandleJoinRequest(NetworkPacket packet) {
//register the new client
int index = clientMgr.HandleConnection(packet.meta.srcAddress);
ClientEntry c;
c.address = packet.meta.srcAddress;
clientMap[clientCounter] = c;
//send the client their info
packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE;
packet.clientInfo.index = index;
char buffer[sizeof(NetworkPacket)];
packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE;
packet.clientInfo.index = clientCounter;
serialize(&packet, buffer);
network.Send(&clientMgr.GetClient(index)->address, buffer, sizeof(NetworkPacket));
network.Send(&clientMap[clientCounter].address, buffer, sizeof(NetworkPacket));
//finished this routine
cout << "connect, total: " << clientMgr.Size() << endl;
clientCounter++;
cout << "connect, total: " << clientMap.size() << endl;
}
void ServerApplication::HandleDisconnect(NetworkPacket packet) {
//disconnect the specified client
//TODO: authenticate who is disconnecting/kicking
char buffer[sizeof(NetworkPacket)];
serialize(&packet, buffer);
network.Send(&clientMgr.GetClient(packet.clientInfo.index)->address, buffer, sizeof(NetworkPacket));
clientMgr.HandleDisconnection(packet.clientInfo.index);
network.Send(&clientMap[packet.clientInfo.index].address, buffer, sizeof(NetworkPacket));
clientMap.erase(packet.clientInfo.index);
//delete players from all clients
NetworkPacket delPacket;
delPacket.meta.type = NetworkPacket::Type::PLAYER_DELETE;
playerMgr.EraseIf([&](PlayerManager::Iterator it) -> bool {
erase_if(playerMap, [&](std::pair<int, PlayerEntry> it) -> bool {
//find the internal players to delete
if (it->first == packet.clientInfo.index) {
delPacket.playerInfo.playerIndex = it->first;
if (it.first == packet.clientInfo.index) {
delPacket.playerInfo.playerIndex = it.first;
//send the delete player command to all clients
PumpPacket(delPacket);
return true;
@@ -225,7 +228,7 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) {
});
//finished this routine
cout << "disconnect, total: " << clientMgr.Size() << endl;
cout << "disconnect, total: " << clientMap.size() << endl;
}
void ServerApplication::HandleSynchronize(NetworkPacket packet) {
@@ -237,15 +240,15 @@ void ServerApplication::HandleSynchronize(NetworkPacket packet) {
//players
//TODO: replace these lambda functions with proper iteration members
newPacket.meta.type = NetworkPacket::Type::PLAYER_UPDATE;
playerMgr.ForEach([&](PlayerManager::Iterator it) {
newPacket.playerInfo.playerIndex = it->first;
// snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", it->second.handle.c_str());
// snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it->second.avatar.c_str());
newPacket.playerInfo.position = it->second.position;
newPacket.playerInfo.motion = it->second.motion;
for (auto& it : playerMap) {
newPacket.playerInfo.playerIndex = it.first;
snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
newPacket.playerInfo.position = it.second.position;
newPacket.playerInfo.motion = it.second.motion;
serialize(&newPacket, buffer);
network.Send(&clientMgr.GetClient(it->second.clientIndex)->address, buffer, sizeof(NetworkPacket));
});
network.Send(&clientMap[it.second.clientIndex].address, buffer, sizeof(NetworkPacket));
}
}
void ServerApplication::HandleShutdown(NetworkPacket packet) {
@@ -254,18 +257,19 @@ void ServerApplication::HandleShutdown(NetworkPacket packet) {
//disconnect all clients
packet.meta.type = NetworkPacket::Type::DISCONNECT;
clientMgr.ForEach([&](ClientManager::Iterator it) -> void {
this->network.Send(&it->second.address, &packet, sizeof(NetworkPacket));
});
for (auto& it : clientMap) {
network.Send(&it.second.address, &packet, sizeof(NetworkPacket));
}
//finished this routine
cout << "shutting down" << endl;
}
/*
void ServerApplication::HandlePlayerNew(NetworkPacket packet) {
//create the new player object
Player newPlayer;
PlayerEntry newPlayer;
newPlayer.clientIndex = packet.playerInfo.clientIndex;
newPlayer.mapIndex = 0;
newPlayer.handle = packet.playerInfo.handle;
newPlayer.avatar = packet.playerInfo.avatar;
newPlayer.position = {0,0};
@@ -292,7 +296,7 @@ void ServerApplication::HandlePlayerDelete(NetworkPacket packet) {
}
//delete players
erase_if(playerMap, [&](pair<int, Player> it) -> bool {
erase_if(playerMap, [&](pair<int, PlayerEntry> it) -> bool {
if (it.first == packet.playerInfo.playerIndex) {
NetworkPacket delPacket;
@@ -320,12 +324,12 @@ void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) {
PumpPacket(packet);
}
*/
void ServerApplication::PumpPacket(NetworkPacket packet) {
//I don't really like this, but it'll do for now
char buffer[sizeof(NetworkPacket)];
serialize(&packet, buffer);
clientMgr.ForEach([&](ClientManager::Iterator it) {
network.Send(&it->second.address, buffer, sizeof(NetworkPacket));
});
}
for (auto& it : clientMap) {
network.Send(&it.second.address, buffer, sizeof(NetworkPacket));
}
}
+18 -6
View File
@@ -32,10 +32,6 @@
#include "sqlite3/sqlite3.h"
#include "SDL/SDL.h"
//misc
#include "client_manager.hpp"
#include "player_manager.hpp"
//common
#include "config_utility.hpp"
#include "vector2.hpp"
@@ -44,6 +40,19 @@
#include <map>
#include <string>
struct ClientEntry {
IPaddress address;
};
struct PlayerEntry {
int clientIndex;
int mapIndex;
std::string handle;
std::string avatar;
Vector2 position;
Vector2 motion;
};
//The main application class
class ServerApplication {
public:
@@ -83,8 +92,11 @@ private:
bool running = true;
ConfigUtility config;
ClientManager clientMgr;
PlayerManager playerMgr;
std::map<int, ClientEntry> clientMap;
std::map<int, PlayerEntry> playerMap;
int clientCounter = 0;
int playerCounter = 0;
};
#endif