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

178 lines
5.4 KiB
C++
Raw Normal View History

/*****************************************************************************
2018-06-15 14:07:34 +02:00
* Copyright (c) 2014-2018 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"
# include "network.h"
constexpr size_t NETWORK_DISCONNECT_REASON_BUFFER_SIZE = 256;
NetworkConnection::NetworkConnection()
{
ResetLastPacketTime();
}
NetworkConnection::~NetworkConnection()
{
2016-06-01 22:58:21 +01:00
delete Socket;
2017-10-03 00:00:32 +02:00
delete[] _lastDisconnectReason;
}
int32_t NetworkConnection::ReadPacket()
{
2017-01-12 17:45:48 +00:00
if (InboundPacket.BytesTransferred < sizeof(InboundPacket.Size))
{
// read packet size
2018-06-22 23:02:47 +02:00
void* buffer = &((char*)&InboundPacket.Size)[InboundPacket.BytesTransferred];
2017-01-12 17:45:48 +00:00
size_t bufferLength = sizeof(InboundPacket.Size) - InboundPacket.BytesTransferred;
2016-06-01 22:58:21 +01:00
size_t readBytes;
NETWORK_READPACKET status = Socket->ReceiveData(buffer, bufferLength, &readBytes);
if (status != NETWORK_READPACKET_SUCCESS)
{
2016-06-01 22:58:21 +01:00
return status;
}
2016-11-13 20:17:49 +01:00
2017-01-12 17:45:48 +00:00
InboundPacket.BytesTransferred += readBytes;
if (InboundPacket.BytesTransferred == sizeof(InboundPacket.Size))
{
2017-01-12 17:45:48 +00:00
InboundPacket.Size = Convert::NetworkToHost(InboundPacket.Size);
if (InboundPacket.Size == 0) // Can't have a size 0 packet
{
return NETWORK_READPACKET_DISCONNECTED;
}
2017-01-12 17:45:48 +00:00
InboundPacket.Data->resize(InboundPacket.Size);
}
}
else
{
// read packet data
2017-01-12 17:45:48 +00:00
if (InboundPacket.Data->capacity() > 0)
{
2018-06-22 23:02:47 +02:00
void* buffer = &InboundPacket.GetData()[InboundPacket.BytesTransferred - sizeof(InboundPacket.Size)];
2017-01-12 17:45:48 +00:00
size_t bufferLength = sizeof(InboundPacket.Size) + InboundPacket.Size - InboundPacket.BytesTransferred;
2016-06-01 22:58:21 +01:00
size_t readBytes;
NETWORK_READPACKET status = Socket->ReceiveData(buffer, bufferLength, &readBytes);
if (status != NETWORK_READPACKET_SUCCESS)
{
2016-06-01 22:58:21 +01:00
return status;
}
2016-06-01 22:58:21 +01:00
2017-01-12 17:45:48 +00:00
InboundPacket.BytesTransferred += readBytes;
}
2017-01-12 17:45:48 +00:00
if (InboundPacket.BytesTransferred == sizeof(InboundPacket.Size) + InboundPacket.Size)
{
2017-03-26 22:30:23 +01:00
_lastPacketTime = platform_get_ticks();
return NETWORK_READPACKET_SUCCESS;
}
}
return NETWORK_READPACKET_MORE_DATA;
}
bool NetworkConnection::SendPacket(NetworkPacket& packet)
{
uint16_t sizen = Convert::HostToNetwork(packet.Size);
std::vector<uint8_t> tosend;
2017-01-12 17:45:48 +00:00
tosend.reserve(sizeof(sizen) + packet.Size);
tosend.insert(tosend.end(), (uint8_t*)&sizen, (uint8_t*)&sizen + sizeof(sizen));
2017-01-12 17:45:48 +00:00
tosend.insert(tosend.end(), packet.Data->begin(), packet.Data->end());
2016-06-01 22:58:21 +01:00
2018-06-22 23:02:47 +02:00
const void* buffer = &tosend[packet.BytesTransferred];
2017-01-12 17:45:48 +00:00
size_t bufferSize = tosend.size() - packet.BytesTransferred;
2016-06-09 16:04:02 -06:00
size_t sent = Socket->SendData(buffer, bufferSize);
if (sent > 0)
{
2017-01-12 17:45:48 +00:00
packet.BytesTransferred += sent;
2016-06-09 16:04:02 -06:00
}
2017-10-03 00:00:32 +02:00
return packet.BytesTransferred == tosend.size();
}
void NetworkConnection::QueuePacket(std::unique_ptr<NetworkPacket> packet, bool front)
{
2016-05-28 19:03:57 +01:00
if (AuthStatus == NETWORK_AUTH_OK || !packet->CommandRequiresAuth())
{
packet->Size = (uint16_t)packet->Data->size();
if (front)
{
// If the first packet was already partially sent add new packet to second position
2017-10-03 00:00:32 +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()
{
2017-10-03 00:00:32 +02:00
while (!_outboundPackets.empty() && SendPacket(*_outboundPackets.front()))
{
2016-05-28 19:03:57 +01:00
_outboundPackets.remove(_outboundPackets.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
#endif