2016-05-28 17:54:45 +01:00
|
|
|
/*****************************************************************************
|
2020-07-21 15:04:34 +02:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2016-05-28 17:54:45 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2016-05-28 17:54:45 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-05-28 17:54:45 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2016-05-28 22:27:08 +01:00
|
|
|
#ifndef DISABLE_NETWORK
|
|
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# include "NetworkPacket.h"
|
2016-05-28 17:54:45 +01:00
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# include "NetworkTypes.h"
|
2018-06-22 23:02:47 +02:00
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# include <memory>
|
2017-12-03 23:45:43 +01:00
|
|
|
|
2020-08-02 22:31:00 +02:00
|
|
|
NetworkPacket::NetworkPacket(NetworkCommand id)
|
2020-08-02 23:34:02 +02:00
|
|
|
: Header{ 0, id }
|
2020-08-02 22:31:00 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
uint8_t* NetworkPacket::GetData()
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2020-08-02 22:07:47 +02:00
|
|
|
return Data.data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const uint8_t* NetworkPacket::GetData() const
|
|
|
|
|
{
|
|
|
|
|
return Data.data();
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-28 18:24:40 +02:00
|
|
|
NetworkCommand NetworkPacket::GetCommand() const
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2020-08-02 23:34:02 +02:00
|
|
|
return Header.Id;
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NetworkPacket::Clear()
|
|
|
|
|
{
|
2017-01-12 17:45:48 +00:00
|
|
|
BytesTransferred = 0;
|
|
|
|
|
BytesRead = 0;
|
2020-08-02 22:07:47 +02:00
|
|
|
Data.clear();
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NetworkPacket::CommandRequiresAuth()
|
|
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
switch (GetCommand())
|
|
|
|
|
{
|
2020-07-28 18:24:40 +02:00
|
|
|
case NetworkCommand::Ping:
|
|
|
|
|
case NetworkCommand::Auth:
|
|
|
|
|
case NetworkCommand::Token:
|
|
|
|
|
case NetworkCommand::GameInfo:
|
|
|
|
|
case NetworkCommand::ObjectsList:
|
2021-02-05 22:27:15 +00:00
|
|
|
case NetworkCommand::Scripts:
|
2020-07-28 18:24:40 +02:00
|
|
|
case NetworkCommand::MapRequest:
|
|
|
|
|
case NetworkCommand::Heartbeat:
|
2018-06-22 23:02:47 +02:00
|
|
|
return false;
|
|
|
|
|
default:
|
|
|
|
|
return true;
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-02 22:07:47 +02:00
|
|
|
void NetworkPacket::Write(const void* bytes, size_t size)
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2020-08-02 22:07:47 +02:00
|
|
|
const uint8_t* src = reinterpret_cast<const uint8_t*>(bytes);
|
|
|
|
|
Data.insert(Data.end(), src, src + size);
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void NetworkPacket::WriteString(const utf8* string)
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2020-05-14 02:22:50 +02:00
|
|
|
Write(reinterpret_cast<const uint8_t*>(string), strlen(string) + 1);
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
const uint8_t* NetworkPacket::Read(size_t size)
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2020-08-02 23:34:02 +02:00
|
|
|
if (BytesRead + size > Header.Size)
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
uint8_t* data = &GetData()[BytesRead];
|
2017-01-12 17:45:48 +00:00
|
|
|
BytesRead += size;
|
2016-05-28 17:54:45 +01:00
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
const utf8* NetworkPacket::ReadString()
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
char* str = reinterpret_cast<char*>(&GetData()[BytesRead]);
|
2018-06-22 23:02:47 +02:00
|
|
|
char* strend = str;
|
2020-08-02 23:34:02 +02:00
|
|
|
while (BytesRead < Header.Size && *strend != 0)
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2017-01-12 17:45:48 +00:00
|
|
|
BytesRead++;
|
2016-05-28 17:54:45 +01:00
|
|
|
strend++;
|
|
|
|
|
}
|
|
|
|
|
if (*strend != 0)
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2017-01-12 17:45:48 +00:00
|
|
|
BytesRead++;
|
2016-05-28 17:54:45 +01:00
|
|
|
return str;
|
|
|
|
|
}
|
2016-05-28 22:27:08 +01:00
|
|
|
|
|
|
|
|
#endif
|