Added a class that client and server inherits from.

Class and server now share the same pointer i game.
You are now able to start a client and a server on the same computer.
This commit is contained in:
Jocke
2015-12-16 14:56:53 +01:00
parent a2d55feed1
commit 58a9d38e26
7 changed files with 64 additions and 23 deletions
+2 -2
View File
@@ -17,9 +17,9 @@
#include "Core/EKeyDown.h" #include "Core/EKeyDown.h"
#include "Core/EKeyUp.h" #include "Core/EKeyUp.h"
#include "Input/EInputCommand.h" #include "Input/EInputCommand.h"
#include "Network/Network.h"
class Client : public Network
class Client
{ {
public: public:
Client(); Client();
+19
View File
@@ -0,0 +1,19 @@
#ifndef Network_h__
#define Network_h__
#include "Core/World.h"
#include "Core/EventBroker.h"
#include "Network/Package.h"
class Network
{
public:
Network();
~Network();
virtual void Start(World* m_world, EventBroker *eventBroker);
virtual void Update();
protected:
};
#endif
+2 -1
View File
@@ -14,8 +14,9 @@
#include "Core/World.h" #include "Core/World.h"
#include "Core/EventBroker.h" #include "Core/EventBroker.h"
#include "Game/ECreatePlayer.h" #include "Game/ECreatePlayer.h"
#include "Network/Network.h"
class Server class Server : public Network
{ {
public: public:
Server(); Server();
+3 -3
View File
@@ -21,6 +21,7 @@
// Network // Network
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include "Network/Network.h"
#include "Network/Server.h" #include "Network/Server.h"
#include "Network/Client.h" #include "Network/Client.h"
@@ -50,9 +51,8 @@ private:
// Network methods // Network methods
void NetworkFunction(); void NetworkFunction();
Client m_Client; Network* m_ClientOrServer;
Server m_Server; bool m_IsClientOrServer = false;
bool m_IsClient = false;
EventRelay<Game, Events::InputCommand> m_EInputCommand; EventRelay<Game, Events::InputCommand> m_EInputCommand;
bool debugOnInputCommand(const Events::InputCommand& e); bool debugOnInputCommand(const Events::InputCommand& e);
+1 -1
View File
@@ -5,8 +5,8 @@ using namespace boost::asio::ip;
Client::Client() : m_Socket(m_IOService) Client::Client() : m_Socket(m_IOService)
{ {
// Set up network stream
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string("192.168.1.6"), 13); m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string("192.168.1.6"), 13);
// Set up network stream
m_NextSnapshot.InputForward = ""; m_NextSnapshot.InputForward = "";
m_NextSnapshot.InputRight = ""; m_NextSnapshot.InputRight = "";
} }
+13
View File
@@ -0,0 +1,13 @@
#include "Network/Network.h"
Network::Network()
{ }
Network::~Network()
{ }
void Network::Start(World * m_world, EventBroker * eventBroker)
{ }
void Network::Update()
{ }
+24 -16
View File
@@ -20,7 +20,7 @@ Game::Game(int argc, char* argv[])
m_Renderer = new Renderer(m_EventBroker); m_Renderer = new Renderer(m_EventBroker);
m_Renderer->SetFullscreen(m_Config->Get<bool>("Video.Fullscreen", false)); m_Renderer->SetFullscreen(m_Config->Get<bool>("Video.Fullscreen", false));
m_Renderer->SetVSYNC(m_Config->Get<bool>("Video.VSYNC", false)); m_Renderer->SetVSYNC(m_Config->Get<bool>("Video.VSYNC", false));
m_Renderer->SetResolution(Rectangle::Rectangle( m_Renderer->SetResolution(Rectangle::Rectangle(
0, 0,
0, 0,
m_Config->Get<int>("Video.Width", 1280), m_Config->Get<int>("Video.Width", 1280),
@@ -53,9 +53,9 @@ Game::Game(int argc, char* argv[])
m_SystemPipeline->AddSystem<RaptorCopterSystem>(); m_SystemPipeline->AddSystem<RaptorCopterSystem>();
m_SystemPipeline->AddSystem<PlayerSystem>(); m_SystemPipeline->AddSystem<PlayerSystem>();
m_SystemPipeline->AddSystem<EditorSystem>(); m_SystemPipeline->AddSystem<EditorSystem>();
// Invoke network // Invoke network
if(m_Config->Get<bool>("Networking.StartNetwork", false) == true) if (m_Config->Get<bool>("Networking.StartNetwork", false) == true)
boost::thread workerThread(&Game::NetworkFunction, this); boost::thread workerThread(&Game::NetworkFunction, this);
m_LastTime = glfwGetTime(); m_LastTime = glfwGetTime();
debugInitialize(); debugInitialize();
@@ -90,7 +90,9 @@ void Game::Tick()
m_EventBroker->Swap(); m_EventBroker->Swap();
// Update network // Update network
m_IsClient ? m_Client.Update() : m_Server.Update(); if (m_IsClientOrServer)
m_ClientOrServer->Update();
// Iterate through systems and update world! // Iterate through systems and update world!
m_SystemPipeline->Update(m_World, dt); m_SystemPipeline->Update(m_World, dt);
@@ -133,16 +135,22 @@ void Game::debugTick(double dt)
} }
void Game::NetworkFunction() void Game::NetworkFunction()
{ {
std::string inputMessage; std::string inputMessage;
std::cout << "Start client or server? (c/s)" << std::endl; std::cout << "Start client or server? (c/s)" << std::endl;
std::cin >> inputMessage; std::cin >> inputMessage;
if (inputMessage == "c" || inputMessage == "C") { if (inputMessage == "c" || inputMessage == "C") {
m_IsClient = true; m_IsClientOrServer = true;
m_Client.Start(m_World, m_EventBroker); m_ClientOrServer = new Client();
}
if (inputMessage == "s" || inputMessage == "S") {
m_IsClient = false;
m_Server.Start(m_World, m_EventBroker);
} }
if (inputMessage == "s" || inputMessage == "S") {
m_IsClientOrServer = true;
m_ClientOrServer = new Server();
}
m_ClientOrServer->Start(m_World, m_EventBroker);
// I don't think we are reaching this part of the code right now.
// When server or client is done set it to false.
m_IsClientOrServer = false;
// Destroy it! (with fire)
delete m_ClientOrServer;
} }