I'm sick of multithreading.

I'm also sick of bastards on the internet.
This commit is contained in:
Kayne Ruse
2013-12-30 14:11:44 +11:00
parent a494bfbb38
commit 071e0d9021
5 changed files with 0 additions and 282 deletions
-27
View File
@@ -21,8 +21,6 @@
*/ */
#include "server_application.hpp" #include "server_application.hpp"
#include "network_packet.hpp"
#include "utility.hpp" #include "utility.hpp"
#include <stdexcept> #include <stdexcept>
@@ -47,12 +45,6 @@ ServerApplication::~ServerApplication() {
void ServerApplication::Init(int argc, char** argv) { void ServerApplication::Init(int argc, char** argv) {
//TODO: proper command line option parsing //TODO: proper command line option parsing
//Check prerequisites
if (!sqlite3_threadsafe()) {
throw(runtime_error("Cannot run without thread safety"));
}
cout << "Thread safety confirmed" << endl;
//load config //load config
config.Load("rsc\\config.cfg"); config.Load("rsc\\config.cfg");
@@ -88,13 +80,6 @@ void ServerApplication::Init(int argc, char** argv) {
getline(is, script, '\0'); getline(is, script, '\0');
is.close(); is.close();
sqlite3_exec(database, script.c_str(), nullptr, nullptr, nullptr); sqlite3_exec(database, script.c_str(), nullptr, nullptr, nullptr);
//open the rooms
worldRoomMap.insert( pair<int, WorldRoom*>(worldRoomCounter++, new WorldRoom(playerMap)));
for (auto& it : worldRoomMap) {
it.second->OpenRoom();
}
} }
void ServerApplication::Loop() { void ServerApplication::Loop() {
@@ -119,13 +104,6 @@ void ServerApplication::Loop() {
} }
void ServerApplication::Quit() { void ServerApplication::Quit() {
//close the rooms
for (auto& it : worldRoomMap) {
it.second->CloseRoom();
delete it.second;
}
worldRoomMap.clear();
//members //members
network.Close(); network.Close();
@@ -136,11 +114,6 @@ void ServerApplication::Quit() {
} }
void ServerApplication::HandlePacket(NetworkPacket packet) { void ServerApplication::HandlePacket(NetworkPacket packet) {
//debgging
for (auto& it : worldRoomMap) {
it.second->GetInQueue()->PushBack(packet);
}
switch(packet.meta.type) { switch(packet.meta.type) {
case NetworkPacket::Type::BROADCAST_REQUEST: case NetworkPacket::Type::BROADCAST_REQUEST:
HandleBroadcastRequest(packet); HandleBroadcastRequest(packet);
-4
View File
@@ -37,8 +37,6 @@
#include "client.hpp" #include "client.hpp"
#include "player.hpp" #include "player.hpp"
#include "world_room.hpp"
//STL //STL
#include <map> #include <map>
#include <string> #include <string>
@@ -82,11 +80,9 @@ private:
//global lists //global lists
ClientMap clientMap; ClientMap clientMap;
PlayerMap playerMap; PlayerMap playerMap;
std::map<int, WorldRoom*> worldRoomMap;
int clientCounter = 0; int clientCounter = 0;
int playerCounter = 0; int playerCounter = 0;
int worldRoomCounter = 0;
}; };
#endif #endif
-92
View File
@@ -1,92 +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 THREADSAFEQUEUE_HPP_
#define THREADSAFEQUEUE_HPP_
#include "SDL/SDL_thread.h"
#include <deque>
#include <stdexcept>
/* This container is a thread safe reimplementation of std::queue.
*/
template<typename T, class Container = std::deque<T>>
class ThreadSafeQueue {
public:
ThreadSafeQueue() {
lock = SDL_CreateSemaphore(1);
if (!lock) {
throw(std::runtime_error("Failed to create ThreadSafeQueue::lock"));
}
}
~ThreadSafeQueue() {
SDL_SemWait(lock);
container.clear();
SDL_SemPost(lock);
SDL_DestroySemaphore(lock);
}
T PushBack(T t) {
SDL_SemWait(lock);
container.push_back(t);
SDL_SemPost(lock);
return t;
}
T PeekFront() {
T t;
SDL_SemWait(lock);
if (container.size() > 0) {
t = container[0];
}
SDL_SemPost(lock);
return t;
}
T PopFront() {
T t;
SDL_SemWait(lock);
if (container.size() > 0) {
t = container[0];
container.pop_front();
}
SDL_SemPost(lock);
return t;
}
int Size() {
//can't be sure if std::deque::size() is thread safe
int ret;
SDL_SemWait(lock);
ret = container.size();
SDL_SemPost(lock);
return ret;
}
private:
Container container;
SDL_sem* lock;
};
#endif
-94
View File
@@ -1,94 +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 "world_room.hpp"
#include <iostream>
#include <stdexcept>
using namespace std;
int worldRoomThread(void* argRoom) {
WorldRoom* room = reinterpret_cast<WorldRoom*>(argRoom);
try {
room->Init();
room->Loop();
room->Quit();
}
catch(exception& e) {
cerr << "Fatal room error: " << e.what() << endl;
return 1;
}
return 0;
}
WorldRoom::WorldRoom(PlayerMap const& argPlayerMap):
playerMap(argPlayerMap)
{
//
}
WorldRoom::~WorldRoom() {
//
}
void WorldRoom::OpenRoom() {
if (running) {
throw(std::runtime_error("Cannot open a room that is already running"));
}
running = true;
if (!(thread = SDL_CreateThread(worldRoomThread, this))) {
throw(std::runtime_error("Failed to open the room thread"));
}
}
void WorldRoom::CloseRoom() {
running = false;
SDL_WaitThread(thread, nullptr);
}
void WorldRoom::KillRoom() {
running = false;
SDL_KillThread(thread);
}
void WorldRoom::Init() {
//
}
void WorldRoom::Loop() {
while(running) {
while(networkInQueue.Size() > 0) {
HandlePacket(networkInQueue.PopFront());
}
SDL_Delay(10);
}
}
void WorldRoom::Quit() {
//
}
void WorldRoom::HandlePacket(NetworkPacket packet) {
cout << "packet received" << endl;
}
-65
View File
@@ -1,65 +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 WORLDROOM_HPP_
#define WORLDROOM_HPP_
#include "network_packet.hpp"
#include "thread_safe_queue.hpp"
#include "player.hpp"
#include "player_entity.hpp"
#include "SDL/SDL_thread.h"
#include <map>
class WorldRoom {
public:
WorldRoom(PlayerMap const&);
~WorldRoom();
void OpenRoom();
void CloseRoom();
void KillRoom();
ThreadSafeQueue<NetworkPacket>* GetInQueue() { return &networkInQueue; };
ThreadSafeQueue<NetworkPacket>* GetOutQueue() { return &networkOutQueue; };
private:
friend int worldRoomThread(void* arg);
void Init();
void Loop();
void Quit();
void HandlePacket(NetworkPacket);
SDL_Thread* thread = nullptr;
bool running = false;
ThreadSafeQueue<NetworkPacket> networkInQueue;
ThreadSafeQueue<NetworkPacket> networkOutQueue;
PlayerMap playerMap;
PlayerEntityMap playerEntityMap;
};
#endif