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

198 lines
5.9 KiB
C++
Raw Normal View History

2017-06-01 21:55:10 +02:00
#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
2016-05-28 22:27:08 +01:00
#ifndef DISABLE_NETWORK
2016-06-02 10:13:25 +02:00
#include "network.h"
#include "NetworkConnection.h"
#include "../core/String.hpp"
extern "C"
{
#include "../localisation/localisation.h"
#include "../platform/platform.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;
2016-05-28 19:03:57 +01:00
if (_lastDisconnectReason)
{
2016-05-28 19:03:57 +01:00
delete[] _lastDisconnectReason;
}
}
2017-01-04 22:17:08 +01:00
sint32 NetworkConnection::ReadPacket()
{
2017-01-12 17:45:48 +00:00
if (InboundPacket.BytesTransferred < sizeof(InboundPacket.Size))
{
// read packet size
2017-01-12 17:45:48 +00:00
void * buffer = &((char*)&InboundPacket.Size)[InboundPacket.BytesTransferred];
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)
{
2017-01-12 17:45:48 +00:00
void * buffer = &InboundPacket.GetData()[InboundPacket.BytesTransferred - sizeof(InboundPacket.Size)];
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)
{
2017-01-12 17:45:48 +00:00
uint16 sizen = Convert::HostToNetwork(packet.Size);
std::vector<uint8> tosend;
2017-01-12 17:45:48 +00:00
tosend.reserve(sizeof(sizen) + packet.Size);
tosend.insert(tosend.end(), (uint8*)&sizen, (uint8*)&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
2017-01-12 17:45:48 +00:00
const void * buffer = &tosend[packet.BytesTransferred];
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-01-12 17:45:48 +00:00
if (packet.BytesTransferred == tosend.size())
{
2016-06-01 22:58:21 +01:00
return true;
}
2016-06-09 16:04:02 -06:00
return false;
}
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())
{
2017-01-12 17:45:48 +00:00
packet->Size = (uint16)packet->Data->size();
if (front)
{
// If the first packet was already partially sent add new packet to second position
2017-01-12 17:45:48 +00:00
if (_outboundPackets.size() > 0 && _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()
{
2016-05-28 19:03:57 +01:00
while (_outboundPackets.size() > 0 && SendPacket(*(_outboundPackets.front()).get()))
{
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()
{
#ifndef DEBUG
2017-03-26 22:30:23 +01:00
if (platform_get_ticks() > _lastPacketTime + 7000)
{
return false;
}
#endif
return true;
}
2016-05-28 19:03:57 +01:00
const utf8 * NetworkConnection::GetLastDisconnectReason() const
{
2016-05-28 19:03:57 +01:00
return this->_lastDisconnectReason;
}
2016-05-28 19:03:57 +01:00
void NetworkConnection::SetLastDisconnectReason(const utf8 * src)
{
if (src == nullptr)
{
2016-05-28 19:03:57 +01:00
if (_lastDisconnectReason)
{
2016-05-28 19:03:57 +01:00
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);
}
2016-05-28 19:03:57 +01: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