2014-07-09 22:29:07 +00:00
|
|
|
#include "stdafx.h"
|
2015-07-02 03:17:14 +00:00
|
|
|
#include <thread>
|
|
|
|
using std::thread;
|
|
|
|
|
|
|
|
#include "MessageManager.h"
|
2014-07-09 22:29:07 +00:00
|
|
|
#include "GameClient.h"
|
2014-07-09 23:05:07 +00:00
|
|
|
#include "Console.h"
|
2015-07-02 03:17:14 +00:00
|
|
|
#include "../Utilities/Socket.h"
|
|
|
|
#include "ClientConnectionData.h"
|
2016-02-06 04:14:27 +00:00
|
|
|
#include "GameClientConnection.h"
|
2014-07-09 22:29:07 +00:00
|
|
|
|
|
|
|
unique_ptr<GameClient> GameClient::Instance;
|
|
|
|
|
|
|
|
GameClient::GameClient()
|
|
|
|
{
|
2015-07-02 03:17:14 +00:00
|
|
|
MessageManager::RegisterNotificationListener(this);
|
2014-07-09 22:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GameClient::~GameClient()
|
|
|
|
{
|
|
|
|
_stop = true;
|
|
|
|
_clientThread->join();
|
2015-07-02 03:17:14 +00:00
|
|
|
MessageManager::UnregisterNotificationListener(this);
|
2014-07-09 22:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GameClient::Connected()
|
|
|
|
{
|
|
|
|
if(Instance) {
|
|
|
|
return Instance->_connected;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 03:17:14 +00:00
|
|
|
void GameClient::Connect(shared_ptr<ClientConnectionData> connectionData)
|
2014-07-09 22:29:07 +00:00
|
|
|
{
|
|
|
|
Instance.reset(new GameClient());
|
2015-07-02 03:17:14 +00:00
|
|
|
Instance->PrivateConnect(connectionData);
|
2014-07-09 22:29:07 +00:00
|
|
|
Instance->_clientThread.reset(new thread(&GameClient::Exec, Instance.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameClient::Disconnect()
|
|
|
|
{
|
|
|
|
Instance.reset();
|
|
|
|
}
|
|
|
|
|
2015-07-02 03:17:14 +00:00
|
|
|
void GameClient::PrivateConnect(shared_ptr<ClientConnectionData> connectionData)
|
2014-07-09 22:29:07 +00:00
|
|
|
{
|
|
|
|
_stop = false;
|
|
|
|
_socket.reset(new Socket());
|
2015-07-02 03:17:14 +00:00
|
|
|
if(_socket->Connect(connectionData->Host.c_str(), connectionData->Port)) {
|
|
|
|
_connection.reset(new GameClientConnection(_socket, connectionData));
|
2014-07-09 22:29:07 +00:00
|
|
|
_connected = true;
|
|
|
|
} else {
|
2015-07-11 12:27:22 +00:00
|
|
|
MessageManager::DisplayMessage("Net Play", "Could not connect to server.");
|
2014-07-09 22:29:07 +00:00
|
|
|
_connected = false;
|
|
|
|
_socket.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameClient::Exec()
|
|
|
|
{
|
|
|
|
if(_connected) {
|
|
|
|
while(!_stop) {
|
|
|
|
if(!_connection->ConnectionError()) {
|
|
|
|
_connection->ProcessMessages();
|
|
|
|
_connection->SendInput();
|
|
|
|
} else {
|
|
|
|
_connected = false;
|
|
|
|
_socket.reset();
|
|
|
|
_connection.reset();
|
|
|
|
break;
|
|
|
|
}
|
2014-07-10 02:02:46 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(1));
|
2014-07-09 22:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-26 22:11:00 +00:00
|
|
|
void GameClient::ProcessNotification(ConsoleNotificationType type, void* parameter)
|
2014-07-09 22:29:07 +00:00
|
|
|
{
|
|
|
|
if(type == ConsoleNotificationType::GameLoaded && std::this_thread::get_id() != _clientThread->get_id()) {
|
|
|
|
GameClient::Disconnect();
|
|
|
|
}
|
2016-02-06 04:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t GameClient::GetControllerState(uint8_t port)
|
|
|
|
{
|
2016-02-06 20:33:45 +00:00
|
|
|
if(Instance && Instance->_connection) {
|
|
|
|
return Instance->_connection->GetControllerState(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameClient::SelectController(uint8_t port)
|
|
|
|
{
|
|
|
|
if(Instance && Instance->_connection) {
|
|
|
|
Instance->_connection->SelectController(port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t GameClient::GetAvailableControllers()
|
|
|
|
{
|
|
|
|
if(Instance && Instance->_connection) {
|
|
|
|
return Instance->_connection->GetAvailableControllers();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t GameClient::GetControllerPort()
|
|
|
|
{
|
|
|
|
if(Instance && Instance->_connection) {
|
|
|
|
return Instance->_connection->GetControllerPort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GameConnection::SpectatorPort;
|
2014-07-09 22:29:07 +00:00
|
|
|
}
|