Files
OpenRCT2-Web/src/openrct2/network/NetworkConnection.cpp
T

240 lines
7.0 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
*
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
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2016-05-28 22:27:08 +01:00
#ifndef DISABLE_NETWORK
2018-07-21 16:17:06 +02:00
# include "NetworkConnection.h"
2018-07-21 16:17:06 +02:00
# include "../core/String.hpp"
# include "../localisation/Localisation.h"
# include "../platform/platform.h"
2019-05-05 16:54:16 +00:00
# include "Socket.h"
2018-07-21 16:17:06 +02:00
# include "network.h"
constexpr size_t NETWORK_DISCONNECT_REASON_BUFFER_SIZE = 256;
2020-08-02 23:34:02 +02:00
constexpr size_t NetworkBufferSize = 1024;
NetworkConnection::NetworkConnection()
{
ResetLastPacketTime();
}
NetworkConnection::~NetworkConnection()
{
2017-10-03 00:00:32 +02:00
delete[] _lastDisconnectReason;
}
int32_t NetworkConnection::ReadPacket()
{
2020-08-02 23:34:02 +02:00
size_t bytesRead = 0;
// Read packet header.
auto& header = InboundPacket.Header;
if (InboundPacket.BytesTransferred < sizeof(InboundPacket.Header))
{
2020-08-02 23:34:02 +02:00
const size_t missingLength = sizeof(header) - InboundPacket.BytesTransferred;
uint8_t* buffer = reinterpret_cast<uint8_t*>(&InboundPacket.Header);
NETWORK_READPACKET status = Socket->ReceiveData(buffer, missingLength, &bytesRead);
2016-06-01 22:58:21 +01:00
if (status != NETWORK_READPACKET_SUCCESS)
{
2016-06-01 22:58:21 +01:00
return status;
}
2016-11-13 20:17:49 +01:00
2020-08-02 23:34:02 +02:00
InboundPacket.BytesTransferred += bytesRead;
if (InboundPacket.BytesTransferred < sizeof(InboundPacket.Header))
{
2020-08-02 23:34:02 +02:00
// If still not enough data for header, keep waiting.
return NETWORK_READPACKET_MORE_DATA;
}
2020-08-02 23:34:02 +02:00
// Normalise values.
header.Size = Convert::NetworkToHost(header.Size);
header.Id = ByteSwapBE(header.Id);
2020-08-05 16:19:57 +02:00
// NOTE: For compatibility reasons for the master server we need to remove sizeof(Header.Id) from the size.
// Previously the Id field was not part of the header rather part of the body.
2020-08-02 23:34:02 +02:00
header.Size -= sizeof(header.Id);
// Fall-through: Read rest of packet.
}
2020-08-02 23:34:02 +02:00
// Read packet body.
{
2020-08-02 23:34:02 +02:00
const size_t missingLength = header.Size - (InboundPacket.BytesTransferred - sizeof(header));
uint8_t buffer[NetworkBufferSize];
if (missingLength > 0)
{
2020-08-02 23:34:02 +02:00
NETWORK_READPACKET status = Socket->ReceiveData(buffer, std::min(missingLength, NetworkBufferSize), &bytesRead);
2016-06-01 22:58:21 +01:00
if (status != NETWORK_READPACKET_SUCCESS)
{
2016-06-01 22:58:21 +01:00
return status;
}
2016-06-01 22:58:21 +01:00
2020-08-02 23:34:02 +02:00
InboundPacket.BytesTransferred += bytesRead;
InboundPacket.Write(buffer, bytesRead);
}
2020-08-02 23:34:02 +02:00
if (InboundPacket.Data.size() == header.Size)
{
2020-08-02 23:34:02 +02:00
// Received complete packet.
2017-03-26 22:30:23 +01:00
_lastPacketTime = platform_get_ticks();
2019-01-29 16:35:19 +01:00
RecordPacketStats(InboundPacket, false);
return NETWORK_READPACKET_SUCCESS;
}
}
2020-08-02 23:34:02 +02:00
return NETWORK_READPACKET_MORE_DATA;
}
bool NetworkConnection::SendPacket(NetworkPacket& packet)
{
2020-08-02 23:34:02 +02:00
auto header = packet.Header;
2016-06-01 22:58:21 +01:00
2020-08-02 23:34:02 +02:00
std::vector<uint8_t> buffer;
buffer.reserve(sizeof(header) + header.Size);
2020-08-05 16:19:57 +02:00
// NOTE: For compatibility reasons for the master server we need to add sizeof(Header.Id) to the size.
// Previously the Id field was not part of the header rather part of the body.
2020-08-02 23:34:02 +02:00
header.Size += sizeof(header.Id);
header.Size = Convert::HostToNetwork(header.Size);
header.Id = ByteSwapBE(header.Id);
buffer.insert(buffer.end(), reinterpret_cast<uint8_t*>(&header), reinterpret_cast<uint8_t*>(&header) + sizeof(header));
buffer.insert(buffer.end(), packet.Data.begin(), packet.Data.end());
size_t bufferSize = buffer.size() - packet.BytesTransferred;
size_t sent = Socket->SendData(buffer.data() + packet.BytesTransferred, bufferSize);
2016-06-09 16:04:02 -06:00
if (sent > 0)
{
2017-01-12 17:45:48 +00:00
packet.BytesTransferred += sent;
2016-06-09 16:04:02 -06:00
}
2019-01-29 16:35:19 +01:00
2020-08-02 23:34:02 +02:00
bool sendComplete = packet.BytesTransferred == buffer.size();
2019-01-29 16:35:19 +01:00
if (sendComplete)
{
RecordPacketStats(packet, true);
}
return sendComplete;
}
2020-08-02 22:07:47 +02:00
void NetworkConnection::QueuePacket(NetworkPacket&& packet, bool front)
{
2020-08-02 22:07:47 +02:00
if (AuthStatus == NETWORK_AUTH_OK || !packet.CommandRequiresAuth())
{
2020-08-02 23:34:02 +02:00
packet.Header.Size = static_cast<uint16_t>(packet.Data.size());
if (front)
{
// If the first packet was already partially sent add new packet to second position
2020-08-02 22:07:47 +02:00
if (!_outboundPackets.empty() && _outboundPackets.front().BytesTransferred > 0)
{
auto it = _outboundPackets.begin();
it++; // Second position
_outboundPackets.insert(it, std::move(packet));
}
else
{
_outboundPackets.push_front(std::move(packet));
}
}
else
{
2016-05-28 19:03:57 +01:00
_outboundPackets.push_back(std::move(packet));
}
}
}
void NetworkConnection::SendQueuedPackets()
{
2020-08-02 22:07:47 +02:00
while (!_outboundPackets.empty() && SendPacket(_outboundPackets.front()))
{
2020-08-02 22:07:47 +02:00
_outboundPackets.pop_front();
}
}
void NetworkConnection::ResetLastPacketTime()
{
2017-03-26 22:30:23 +01:00
_lastPacketTime = platform_get_ticks();
}
bool NetworkConnection::ReceivedPacketRecently()
{
2018-07-21 16:17:06 +02:00
# ifndef DEBUG
2017-03-26 22:30:23 +01:00
if (platform_get_ticks() > _lastPacketTime + 7000)
{
return false;
}
2018-07-21 16:17:06 +02:00
# endif
return true;
}
2018-06-22 23:02:47 +02:00
const utf8* NetworkConnection::GetLastDisconnectReason() const
{
2016-05-28 19:03:57 +01:00
return this->_lastDisconnectReason;
}
2018-06-22 23:02:47 +02:00
void NetworkConnection::SetLastDisconnectReason(const utf8* src)
{
if (src == nullptr)
{
delete[] _lastDisconnectReason;
_lastDisconnectReason = nullptr;
return;
}
2016-05-28 19:03:57 +01:00
if (_lastDisconnectReason == nullptr)
{
2016-05-28 19:03:57 +01:00
_lastDisconnectReason = new utf8[NETWORK_DISCONNECT_REASON_BUFFER_SIZE];
}
2016-05-28 19:03:57 +01:00
String::Set(_lastDisconnectReason, NETWORK_DISCONNECT_REASON_BUFFER_SIZE, src);
}
2018-06-22 23:02:47 +02:00
void NetworkConnection::SetLastDisconnectReason(const rct_string_id string_id, void* args)
{
char buffer[NETWORK_DISCONNECT_REASON_BUFFER_SIZE];
2016-09-25 21:24:29 -05:00
format_string(buffer, NETWORK_DISCONNECT_REASON_BUFFER_SIZE, string_id, args);
2016-05-28 19:03:57 +01:00
SetLastDisconnectReason(buffer);
}
2016-05-28 22:27:08 +01:00
2019-01-29 16:35:19 +01:00
void NetworkConnection::RecordPacketStats(const NetworkPacket& packet, bool sending)
{
2020-04-22 17:09:29 +02:00
uint32_t packetSize = static_cast<uint32_t>(packet.BytesTransferred);
uint32_t trafficGroup;
2019-01-29 16:35:19 +01:00
switch (packet.GetCommand())
{
case NetworkCommand::GameAction:
2019-01-29 16:35:19 +01:00
trafficGroup = NETWORK_STATISTICS_GROUP_COMMANDS;
break;
case NetworkCommand::Map:
2019-01-29 16:35:19 +01:00
trafficGroup = NETWORK_STATISTICS_GROUP_MAPDATA;
break;
default:
trafficGroup = NETWORK_STATISTICS_GROUP_BASE;
break;
2019-01-29 16:35:19 +01:00
}
if (sending)
{
Stats.bytesSent[trafficGroup] += packetSize;
Stats.bytesSent[NETWORK_STATISTICS_GROUP_TOTAL] += packetSize;
}
else
{
Stats.bytesReceived[trafficGroup] += packetSize;
Stats.bytesReceived[NETWORK_STATISTICS_GROUP_TOTAL] += packetSize;
}
}
2016-05-28 22:27:08 +01:00
#endif