141 lines
No EOL
4.2 KiB
C++
141 lines
No EOL
4.2 KiB
C++
#include "stdafx.h"
|
|
#include "MessageManager.h"
|
|
#include "GameServerConnection.h"
|
|
#include "HandShakeMessage.h"
|
|
#include "InputDataMessage.h"
|
|
#include "MovieDataMessage.h"
|
|
#include "GameInformationMessage.h"
|
|
#include "SaveStateMessage.h"
|
|
#include "Console.h"
|
|
#include "ControlManager.h"
|
|
#include "ClientConnectionData.h"
|
|
#include "EmulationSettings.h"
|
|
#include "StandardController.h"
|
|
|
|
GameServerConnection* GameServerConnection::_netPlayDevices[4] = { nullptr,nullptr,nullptr, nullptr };
|
|
|
|
GameServerConnection::GameServerConnection(shared_ptr<Socket> socket, int controllerPort) : GameConnection(socket, nullptr)
|
|
{
|
|
//Server-side connection
|
|
_controllerPort = controllerPort;
|
|
MessageManager::RegisterNotificationListener(this);
|
|
}
|
|
|
|
GameServerConnection::~GameServerConnection()
|
|
{
|
|
if(_connectionData) {
|
|
MessageManager::DisplayToast("Net Play", _connectionData->PlayerName + " (Player " + std::to_string(_controllerPort + 1) + ") disconnected.", _connectionData->AvatarData, _connectionData->AvatarSize);
|
|
} else {
|
|
MessageManager::DisplayMessage("Net Play", "Player " + std::to_string(_controllerPort + 1) + " disconnected.");
|
|
}
|
|
|
|
UnregisterNetPlayDevice(this);
|
|
MessageManager::UnregisterNotificationListener(this);
|
|
}
|
|
|
|
void GameServerConnection::SendGameInformation()
|
|
{
|
|
Console::Pause();
|
|
GameInformationMessage gameInfo(Console::GetROMPath(), Console::GetCrc32(), _controllerPort, EmulationSettings::CheckFlag(EmulationFlags::Paused));
|
|
SendNetMessage(gameInfo);
|
|
SaveStateMessage saveState;
|
|
SendNetMessage(saveState);
|
|
Console::Resume();
|
|
}
|
|
|
|
void GameServerConnection::SendMovieData(uint8_t state, uint8_t port)
|
|
{
|
|
if(_handshakeCompleted) {
|
|
MovieDataMessage message(state, port);
|
|
SendNetMessage(message);
|
|
}
|
|
}
|
|
|
|
void GameServerConnection::PushState(uint32_t state)
|
|
{
|
|
if(_inputData.size() == 0 || state != _inputData.back()) {
|
|
_inputData.push_back(state);
|
|
}
|
|
}
|
|
|
|
uint32_t GameServerConnection::GetState()
|
|
{
|
|
size_t inputBufferSize = _inputData.size();
|
|
uint32_t stateData = 0;
|
|
if(inputBufferSize > 0) {
|
|
stateData = _inputData.front();
|
|
if(inputBufferSize > 1) {
|
|
//Always keep the last one the client sent, it will be used until a new one is received
|
|
_inputData.pop_front();
|
|
}
|
|
}
|
|
return stateData;
|
|
}
|
|
|
|
void GameServerConnection::ProcessMessage(NetMessage* message)
|
|
{
|
|
switch(message->GetType()) {
|
|
case MessageType::HandShake:
|
|
//Send the game's current state to the client and register the controller
|
|
if(((HandShakeMessage*)message)->IsValid()) {
|
|
Console::Pause();
|
|
_connectionData.reset(new ClientConnectionData("", 0, ((HandShakeMessage*)message)->GetPlayerName(), ((HandShakeMessage*)message)->GetAvatarData(), ((HandShakeMessage*)message)->GetAvatarSize()));
|
|
|
|
MessageManager::DisplayToast("Net Play", _connectionData->PlayerName + " (Player " + std::to_string(_controllerPort + 1) + ") connected.", _connectionData->AvatarData, _connectionData->AvatarSize);
|
|
|
|
if(Console::GetROMPath().size() > 0) {
|
|
SendGameInformation();
|
|
}
|
|
|
|
_handshakeCompleted = true;
|
|
RegisterNetPlayDevice(this, _controllerPort);
|
|
Console::Resume();
|
|
}
|
|
break;
|
|
case MessageType::InputData:
|
|
PushState(((InputDataMessage*)message)->GetInputState());
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void GameServerConnection::ProcessNotification(ConsoleNotificationType type, void* parameter)
|
|
{
|
|
switch(type) {
|
|
case ConsoleNotificationType::GamePaused:
|
|
case ConsoleNotificationType::GameLoaded:
|
|
case ConsoleNotificationType::GameResumed:
|
|
case ConsoleNotificationType::GameReset:
|
|
case ConsoleNotificationType::StateLoaded:
|
|
case ConsoleNotificationType::CheatAdded:
|
|
case ConsoleNotificationType::FdsDiskChanged:
|
|
case ConsoleNotificationType::ConfigChanged:
|
|
SendGameInformation();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void GameServerConnection::RegisterNetPlayDevice(GameServerConnection* device, uint8_t port)
|
|
{
|
|
GameServerConnection::_netPlayDevices[port] = device;
|
|
}
|
|
|
|
void GameServerConnection::UnregisterNetPlayDevice(GameServerConnection* device)
|
|
{
|
|
if(device != nullptr) {
|
|
for(int i = 0; i < 4; i++) {
|
|
if(GameServerConnection::_netPlayDevices[i] == device) {
|
|
GameServerConnection::_netPlayDevices[i] = nullptr;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
GameServerConnection* GameServerConnection::GetNetPlayDevice(uint8_t port)
|
|
{
|
|
return GameServerConnection::_netPlayDevices[port];
|
|
} |