Mesen-SX/Core/HandShakeMessage.h

61 lines
1.6 KiB
C
Raw Normal View History

2019-10-20 20:05:39 -04:00
#pragma once
#include "stdafx.h"
#include "NetMessage.h"
#include "../Utilities/sha1.h"
class HandShakeMessage : public NetMessage
{
private:
static constexpr int CurrentVersion = 100; //Use 100+ to distinguish from Mesen
uint32_t _emuVersion = 0;
uint32_t _protocolVersion = CurrentVersion;
string _playerName;
string _hashedPassword;
2019-10-20 20:05:39 -04:00
bool _spectator = false;
protected:
void Serialize(Serializer &s) override
2019-10-20 20:05:39 -04:00
{
s.Stream(_emuVersion, _protocolVersion, _playerName, _hashedPassword, _spectator);
2019-10-20 20:05:39 -04:00
}
public:
HandShakeMessage(void* buffer, uint32_t length) : NetMessage(buffer, length) {}
2019-10-20 20:05:39 -04:00
HandShakeMessage(string playerName, string hashedPassword, bool spectator, uint32_t emuVersion) : NetMessage(MessageType::HandShake)
2019-10-20 20:05:39 -04:00
{
_emuVersion = emuVersion;
_protocolVersion = HandShakeMessage::CurrentVersion;
_playerName = playerName;
_hashedPassword = hashedPassword;
2019-10-20 20:05:39 -04:00
_spectator = spectator;
}
string GetPlayerName()
{
return _playerName;
2019-10-20 20:05:39 -04:00
}
bool IsValid(uint32_t emuVersion)
{
return _protocolVersion == CurrentVersion && _emuVersion == emuVersion;
}
bool CheckPassword(string serverPassword, string connectionHash)
{
return GetPasswordHash(serverPassword, connectionHash) == string(_hashedPassword);
}
bool IsSpectator()
{
return _spectator;
}
static string GetPasswordHash(string serverPassword, string connectionHash)
{
string saltedPassword = serverPassword + connectionHash;
vector<uint8_t> dataToHash = vector<uint8_t>(saltedPassword.c_str(), saltedPassword.c_str() + saltedPassword.size());
2019-10-20 20:05:39 -04:00
return SHA1::GetHash(dataToHash);
}
};