Mesen/Core/GameClient.cpp
Souryo 850102bbdc Input: Added support for ~20 new peripherals (Incomplete, more fixes/changes to come)
Movies: Rewrote format to support all IO devices and console types
Netplay: Now supports all IO devices and console types
2017-11-19 23:08:23 -05:00

114 lines
2.7 KiB
C++

#include "stdafx.h"
#include <thread>
using std::thread;
#include "MessageManager.h"
#include "GameClient.h"
#include "Console.h"
#include "../Utilities/Socket.h"
#include "ClientConnectionData.h"
#include "GameClientConnection.h"
shared_ptr<GameClient> GameClient::_instance;
GameClient::GameClient()
{
_stop = false;
MessageManager::RegisterNotificationListener(this);
}
GameClient::~GameClient()
{
_stop = true;
if(_clientThread) {
_clientThread->join();
}
MessageManager::UnregisterNotificationListener(this);
}
bool GameClient::Connected()
{
shared_ptr<GameClient> instance = _instance;
return instance ? instance->_connected : false;
}
void GameClient::Connect(shared_ptr<ClientConnectionData> connectionData)
{
_instance.reset(new GameClient());
shared_ptr<GameClient> instance = _instance;
if(instance) {
instance->PrivateConnect(connectionData);
instance->_clientThread.reset(new thread(&GameClient::Exec, instance.get()));
}
}
void GameClient::Disconnect()
{
_instance.reset();
}
shared_ptr<GameClientConnection> GameClient::GetConnection()
{
shared_ptr<GameClient> instance = _instance;
return instance ? instance->_connection : nullptr;
}
void GameClient::PrivateConnect(shared_ptr<ClientConnectionData> connectionData)
{
_stop = false;
shared_ptr<Socket> socket(new Socket());
if(socket->Connect(connectionData->Host.c_str(), connectionData->Port)) {
_connection.reset(new GameClientConnection(socket, connectionData));
_connected = true;
} else {
MessageManager::DisplayMessage("NetPlay", "CouldNotConnect");
_connected = false;
}
}
void GameClient::Exec()
{
if(_connected) {
while(!_stop) {
if(!_connection->ConnectionError()) {
_connection->ProcessMessages();
_connection->SendInput();
} else {
_connected = false;
_connection->Shutdown();
_connection.reset();
break;
}
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(1));
}
}
}
void GameClient::ProcessNotification(ConsoleNotificationType type, void* parameter)
{
if(type == ConsoleNotificationType::GameLoaded && std::this_thread::get_id() != _clientThread->get_id()) {
GameClient::Disconnect();
}
}
void GameClient::SelectController(uint8_t port)
{
shared_ptr<GameClientConnection> connection = GetConnection();
if(connection) {
connection->SelectController(port);
}
}
uint8_t GameClient::GetAvailableControllers()
{
shared_ptr<GameClientConnection> connection = GetConnection();
return connection ? connection->GetAvailableControllers() : 0;
}
uint8_t GameClient::GetControllerPort()
{
shared_ptr<GameClientConnection> connection = GetConnection();
return connection ? connection->GetControllerPort() : GameConnection::SpectatorPort;
}