Rearranged the logic
This commit is contained in:
@@ -1,30 +0,0 @@
|
|||||||
#include "application.hpp"
|
|
||||||
|
|
||||||
Application::Application() {
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
Application::~Application() {
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
void Application::Init() {
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
//disable this for debugging
|
|
||||||
running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Application::Loop() {
|
|
||||||
while(running) {
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Application::Quit() {
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
void Application::NewRoom() {
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
#ifndef APPLICATION_HPP_
|
|
||||||
#define APPLICATION_HPP_
|
|
||||||
|
|
||||||
#include "threading.hpp"
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class Application {
|
|
||||||
public:
|
|
||||||
Application();
|
|
||||||
~Application();
|
|
||||||
|
|
||||||
void Init();
|
|
||||||
void Loop();
|
|
||||||
void Quit();
|
|
||||||
|
|
||||||
bool GetRunning() const { return running; }
|
|
||||||
private:
|
|
||||||
void NewRoom(/* args */);
|
|
||||||
|
|
||||||
std::vector<roomHandle> rooms;
|
|
||||||
bool running = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,16 +1,23 @@
|
|||||||
#include "threading.hpp"
|
#include "base_room.hpp"
|
||||||
|
|
||||||
|
#include "SDL/SDL_thread.h"
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
BaseRoom::BaseRoom(std::map<std::string, std::string> args):
|
||||||
|
arguments(args)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
int roomThread(void* ptr) {
|
int roomThread(void* ptr) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
std::cout << "Opening room" << std::endl;
|
std::cout << "Opening room" << std::endl;
|
||||||
#endif
|
#endif
|
||||||
try {
|
try {
|
||||||
reinterpret_cast<Room*>(ptr)->Init();
|
reinterpret_cast<BaseRoom*>(ptr)->Init();
|
||||||
reinterpret_cast<Room*>(ptr)->Loop();
|
reinterpret_cast<BaseRoom*>(ptr)->Loop();
|
||||||
reinterpret_cast<Room*>(ptr)->Quit();
|
reinterpret_cast<BaseRoom*>(ptr)->Quit();
|
||||||
}
|
}
|
||||||
catch(std::exception& e) {
|
catch(std::exception& e) {
|
||||||
std::cerr << "Fatal room error: " << e.what() << std::endl;
|
std::cerr << "Fatal room error: " << e.what() << std::endl;
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef BASEROOM_HPP_
|
||||||
|
#define BASEROOM_HPP_
|
||||||
|
|
||||||
|
#include "mail_box.hpp"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class BaseRoom {
|
||||||
|
public:
|
||||||
|
BaseRoom(std::map<std::string, std::string> args);
|
||||||
|
~BaseRoom() = default;
|
||||||
|
|
||||||
|
virtual void Init() = 0;
|
||||||
|
virtual void Loop() = 0;
|
||||||
|
virtual void Quit() = 0;
|
||||||
|
|
||||||
|
bool SetRunning(bool b) { return running = b; }
|
||||||
|
bool GetRunning() const { return running; }
|
||||||
|
|
||||||
|
MailBox* GetMailBox() { return& mailBox; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::map<std::string, std::string> const arguments;
|
||||||
|
MailBox mailBox;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool running = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
int roomThread(void*);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
#include "mail_box.hpp"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
MailBox::MailBox() {
|
||||||
|
if (!(inLock = SDL_CreateMutex())) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Failed to create a MailBox mutex; ";
|
||||||
|
os << SDL_GetError();
|
||||||
|
throw(std::runtime_error(os.str()));
|
||||||
|
}
|
||||||
|
if (!(outLock = SDL_CreateMutex())) {
|
||||||
|
SDL_DestroyMutex(inLock);
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Failed to create a MailBox mutex; ";
|
||||||
|
os << SDL_GetError();
|
||||||
|
throw(std::runtime_error(os.str()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MailBox::~MailBox() {
|
||||||
|
SDL_DestroyMutex(inLock);
|
||||||
|
SDL_DestroyMutex(outLock);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string MailBox::PushIn(std::string msg) {
|
||||||
|
SDL_LockMutex(inLock);
|
||||||
|
input.push_back(msg);
|
||||||
|
SDL_UnlockMutex(inLock);
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string MailBox::PeekIn() {
|
||||||
|
std::string ret;
|
||||||
|
SDL_LockMutex(inLock);
|
||||||
|
if (input.size()) {
|
||||||
|
ret = input[0];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ret = "";
|
||||||
|
}
|
||||||
|
SDL_UnlockMutex(inLock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string MailBox::PopIn() {
|
||||||
|
std::string ret;
|
||||||
|
SDL_LockMutex(inLock);
|
||||||
|
if (input.size()) {
|
||||||
|
ret = input[0];
|
||||||
|
input.pop_front();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ret = "";
|
||||||
|
}
|
||||||
|
SDL_UnlockMutex(inLock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string MailBox::PushOut(std::string msg) {
|
||||||
|
SDL_LockMutex(outLock);
|
||||||
|
output.push_back(msg);
|
||||||
|
SDL_UnlockMutex(outLock);
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string MailBox::PeekOut() {
|
||||||
|
std::string ret;
|
||||||
|
SDL_LockMutex(outLock);
|
||||||
|
if (output.size()) {
|
||||||
|
ret = output[0];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ret = "";
|
||||||
|
}
|
||||||
|
SDL_UnlockMutex(outLock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string MailBox::PopOut() {
|
||||||
|
std::string ret;
|
||||||
|
SDL_LockMutex(outLock);
|
||||||
|
if (output.size()) {
|
||||||
|
ret = output[0];
|
||||||
|
output.pop_front();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ret = "";
|
||||||
|
}
|
||||||
|
SDL_UnlockMutex(outLock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef MAILBOX_HPP_
|
||||||
|
#define MAILBOX_HPP_
|
||||||
|
|
||||||
|
#include "SDL/SDL_thread.h"
|
||||||
|
|
||||||
|
#include <deque>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
//Thread safe mailbox
|
||||||
|
class MailBox {
|
||||||
|
public:
|
||||||
|
MailBox();
|
||||||
|
~MailBox();
|
||||||
|
|
||||||
|
std::string PushIn(std::string);
|
||||||
|
std::string PeekIn();
|
||||||
|
std::string PopIn();
|
||||||
|
std::string PushOut(std::string);
|
||||||
|
std::string PeekOut();
|
||||||
|
std::string PopOut();
|
||||||
|
private:
|
||||||
|
std::deque<std::string> input;
|
||||||
|
std::deque<std::string> output;
|
||||||
|
SDL_mutex* inLock = nullptr;
|
||||||
|
SDL_mutex* outLock = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
#include "application.hpp"
|
#include "server_application.hpp"
|
||||||
|
|
||||||
#include "SDL/SDL.h"
|
#include "SDL/SDL.h"
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ int main(int, char**) {
|
|||||||
cout << "Beginning server" << endl;
|
cout << "Beginning server" << endl;
|
||||||
#endif
|
#endif
|
||||||
try {
|
try {
|
||||||
Application app;
|
ServerApplication app;
|
||||||
app.Init();
|
app.Init();
|
||||||
app.Loop();
|
app.Loop();
|
||||||
app.Quit();
|
app.Quit();
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
#include "room.hpp"
|
|
||||||
|
|
||||||
Room::Room() {
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
Room::~Room() {
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
void Room::Init() {
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
//disable this for debugging
|
|
||||||
running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Room::Loop() {
|
|
||||||
while(running) {
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Room::Quit() {
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
#ifndef ROOM_HPP_
|
|
||||||
#define ROOM_HPP_
|
|
||||||
|
|
||||||
class Room {
|
|
||||||
public:
|
|
||||||
Room(/* args */);
|
|
||||||
~Room();
|
|
||||||
|
|
||||||
void Init();
|
|
||||||
void Loop();
|
|
||||||
void Quit();
|
|
||||||
|
|
||||||
bool GetRunning() const { return running; }
|
|
||||||
private:
|
|
||||||
bool running = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#include "server_application.hpp"
|
||||||
|
|
||||||
|
ServerApplication::ServerApplication() {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
ServerApplication::~ServerApplication() {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerApplication::Init() {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerApplication::Loop() {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerApplication::Quit() {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerApplication::OpenRoom(std::map<std::string, std::string> args) {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerApplication::CloseRoom(roomHandle) {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef SERVERAPPLICATION_HPP_
|
||||||
|
#define SERVERAPPLICATION_HPP_
|
||||||
|
|
||||||
|
#include "base_room.hpp"
|
||||||
|
|
||||||
|
#include "SDL/SDL_thread.h"
|
||||||
|
|
||||||
|
#include <list>
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct roomHandle {
|
||||||
|
SDL_Thread* thread = nullptr;
|
||||||
|
BaseRoom* room = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ServerApplication {
|
||||||
|
public:
|
||||||
|
ServerApplication();
|
||||||
|
~ServerApplication();
|
||||||
|
|
||||||
|
void Init();
|
||||||
|
void Loop();
|
||||||
|
void Quit();
|
||||||
|
|
||||||
|
bool SetRunning(bool b) { return running = b; }
|
||||||
|
bool GetRunning() const { return running; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void OpenRoom(std::map<std::string, std::string>);
|
||||||
|
void CloseRoom(roomHandle);
|
||||||
|
|
||||||
|
std::list<roomHandle> rooms;
|
||||||
|
bool running = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
#ifndef THREADING_HPP_
|
|
||||||
#define THREADING_HPP_
|
|
||||||
|
|
||||||
#include "room.hpp"
|
|
||||||
|
|
||||||
#include "SDL/SDL_thread.h"
|
|
||||||
|
|
||||||
struct roomHandle {
|
|
||||||
SDL_Thread* thread = nullptr;
|
|
||||||
Room* room = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
int roomThread(void*);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Reference in New Issue
Block a user