Merge pull request #249 from apachano/feat-enet-client_threading

Feat enet client threading
develop
Alexandre Plateau 2020-06-20 14:50:57 +02:00 committed by GitHub
commit f0cfc2a127
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 9 deletions

View File

@ -31,6 +31,7 @@
#include <Common/Input.hpp>
#include <Common/Network/Host.hpp>
#include <Common/Util/BlockingQueue.hpp>
#include <thread>
namespace phx::client
{
@ -40,10 +41,14 @@ namespace phx::client
Network(std::ostringstream& chat);
~Network();
void tick();
private:
void run();
void kill() { m_running = false; };
public:
void start();
void stop();
private:
/**
* @brief Actions taken when a state is received
*
@ -68,6 +73,7 @@ namespace phx::client
*/
void parseMessage(phx::net::Packet& packet);
public:
/**
* @brief Sends a state packet to a client
*
@ -88,5 +94,6 @@ namespace phx::client
bool m_running;
phx::net::Host* m_client;
std::ostringstream& m_chat;
std::thread* m_thread = nullptr;
};
} // namespace phx::client::net

View File

@ -246,6 +246,7 @@ void Game::onAttach()
m_chat->registerCallback(rawEcho);
m_network = new client::Network(m_chat->cout);
m_network->start();
m_player = new Player(m_registry);
m_player->registerAPI(m_modManager);
@ -296,6 +297,7 @@ void Game::onAttach()
void Game::onDetach()
{
m_network->stop();
delete m_world;
delete m_player;
delete m_camera;
@ -395,11 +397,6 @@ void Game::onEvent(events::Event& e)
void Game::tick(float dt)
{
// std::cout << "tick";
// LOG_WARNING("MAINLOOP") << "WTF";
/// @TODO convert this into thread
m_network->tick();
// temp, will change in the future, based on game time
static math::vec3 lightdir(0.f, -1.f, 0.f);
static float time = 0.f;

View File

@ -61,9 +61,33 @@ Network::Network(std::ostringstream& chat) : m_chat(chat)
m_client->poll(5000_ms);
}
Network::~Network() {}
Network::~Network()
{
if (m_running)
stop();
delete m_client;
}
void Network::tick() { m_client->poll(); }
void Network::run()
{
while (m_running)
{
m_client->poll();
}
}
void Network::start()
{
m_running = true;
m_thread = new std::thread(&Network::run, this);
}
void Network::stop()
{
m_running = false;
m_thread->join();
delete m_thread;
}
void Network::parseEvent(phx::net::Packet& packet)
{

View File

@ -57,6 +57,10 @@ void registerUnusedAPI(cms::ModManager* manager)
[](std::string uniqueName) {});
manager->registerFunction("core.input.registerCallback",
[](int input, sol::function f) {});
manager->registerFunction(
"audio.loadMP3",
[=](const std::string& uniqueName, const std::string& filePath) {});
manager->registerFunction("audio.play", [=](sol::table source) {});
}
void Server::run()