diff --git a/server/server_application.cpp b/server/server_application.cpp index fd45f0c..c914487 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -21,8 +21,6 @@ */ #include "server_application.hpp" -#include "network_packet.hpp" - #include "utility.hpp" #include @@ -47,12 +45,6 @@ ServerApplication::~ServerApplication() { void ServerApplication::Init(int argc, char** argv) { //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 config.Load("rsc\\config.cfg"); @@ -88,13 +80,6 @@ void ServerApplication::Init(int argc, char** argv) { getline(is, script, '\0'); is.close(); sqlite3_exec(database, script.c_str(), nullptr, nullptr, nullptr); - - //open the rooms - worldRoomMap.insert( pair(worldRoomCounter++, new WorldRoom(playerMap))); - - for (auto& it : worldRoomMap) { - it.second->OpenRoom(); - } } void ServerApplication::Loop() { @@ -119,13 +104,6 @@ void ServerApplication::Loop() { } void ServerApplication::Quit() { - //close the rooms - for (auto& it : worldRoomMap) { - it.second->CloseRoom(); - delete it.second; - } - worldRoomMap.clear(); - //members network.Close(); @@ -136,11 +114,6 @@ void ServerApplication::Quit() { } void ServerApplication::HandlePacket(NetworkPacket packet) { - //debgging - for (auto& it : worldRoomMap) { - it.second->GetInQueue()->PushBack(packet); - } - switch(packet.meta.type) { case NetworkPacket::Type::BROADCAST_REQUEST: HandleBroadcastRequest(packet); diff --git a/server/server_application.hpp b/server/server_application.hpp index 7ed59f3..073d480 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -37,8 +37,6 @@ #include "client.hpp" #include "player.hpp" -#include "world_room.hpp" - //STL #include #include @@ -82,11 +80,9 @@ private: //global lists ClientMap clientMap; PlayerMap playerMap; - std::map worldRoomMap; int clientCounter = 0; int playerCounter = 0; - int worldRoomCounter = 0; }; #endif diff --git a/server/thread_safe_queue.hpp b/server/thread_safe_queue.hpp deleted file mode 100644 index 37555ae..0000000 --- a/server/thread_safe_queue.hpp +++ /dev/null @@ -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 -#include - -/* This container is a thread safe reimplementation of std::queue. -*/ - -template> -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 diff --git a/server/world_room.cpp b/server/world_room.cpp deleted file mode 100644 index c6a65f6..0000000 --- a/server/world_room.cpp +++ /dev/null @@ -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 -#include - -using namespace std; - -int worldRoomThread(void* argRoom) { - WorldRoom* room = reinterpret_cast(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; -} diff --git a/server/world_room.hpp b/server/world_room.hpp deleted file mode 100644 index fffcee7..0000000 --- a/server/world_room.hpp +++ /dev/null @@ -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 - -class WorldRoom { -public: - WorldRoom(PlayerMap const&); - ~WorldRoom(); - - void OpenRoom(); - void CloseRoom(); - void KillRoom(); - - ThreadSafeQueue* GetInQueue() { return &networkInQueue; }; - ThreadSafeQueue* 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 networkInQueue; - ThreadSafeQueue networkOutQueue; - - PlayerMap playerMap; - PlayerEntityMap playerEntityMap; -}; - -#endif