Mesen-SX/Core/NetMessage.h

58 lines
1 KiB
C
Raw Normal View History

2019-10-20 20:05:39 -04:00
#pragma once
#include "stdafx.h"
#include "MessageType.h"
#include "SaveStateManager.h"
2019-10-20 20:05:39 -04:00
#include "../Utilities/Socket.h"
#include "../Utilities/Serializer.h"
2019-10-20 20:05:39 -04:00
class NetMessage
{
protected:
MessageType _type;
stringstream _receivedData;
2019-10-20 20:05:39 -04:00
NetMessage(MessageType type)
{
_type = type;
}
NetMessage(void* buffer, uint32_t length)
{
_type = (MessageType)((uint8_t*)buffer)[0];
_receivedData.write((char*)buffer + 1, length - 1);
2019-10-20 20:05:39 -04:00
}
public:
virtual ~NetMessage()
{
2019-10-20 20:05:39 -04:00
}
void Initialize()
{
Serializer s(_receivedData, SaveStateManager::FileFormatVersion);
Serialize(s);
2019-10-20 20:05:39 -04:00
}
MessageType GetType()
{
return _type;
}
void Send(Socket &socket)
2019-10-20 20:05:39 -04:00
{
Serializer s(SaveStateManager::FileFormatVersion);
Serialize(s);
2019-10-20 20:05:39 -04:00
stringstream out;
s.Save(out);
2019-10-20 20:05:39 -04:00
string data = out.str();
uint32_t messageLength = (uint32_t)data.size() + 1;
data = string((char*)&messageLength, 4) + (char)_type + data;
socket.Send((char*)data.c_str(), (int)data.size(), 0);
2019-10-20 20:05:39 -04:00
}
protected:
virtual void Serialize(Serializer &s) = 0;
2019-10-20 20:05:39 -04:00
};