2016-05-28 18:54:45 +02:00
|
|
|
#pragma region Copyright (c) 2014-2016 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 23:27:08 +02:00
|
|
|
#ifndef DISABLE_NETWORK
|
|
|
|
|
2016-06-02 10:13:25 +02:00
|
|
|
#include "network.h"
|
2016-05-28 18:54:45 +02:00
|
|
|
#include "NetworkConnection.h"
|
|
|
|
#include "../core/String.hpp"
|
|
|
|
#include <SDL.h>
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include "../localisation/localisation.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t NETWORK_DISCONNECT_REASON_BUFFER_SIZE = 256;
|
|
|
|
|
|
|
|
NetworkConnection::NetworkConnection()
|
|
|
|
{
|
|
|
|
ResetLastPacketTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkConnection::~NetworkConnection()
|
|
|
|
{
|
2016-06-01 23:58:21 +02:00
|
|
|
delete Socket;
|
2016-05-28 20:03:57 +02:00
|
|
|
if (_lastDisconnectReason)
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
delete[] _lastDisconnectReason;
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int NetworkConnection::ReadPacket()
|
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
if (InboundPacket.transferred < sizeof(InboundPacket.size))
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
|
|
|
// read packet size
|
2016-06-01 23:58:21 +02:00
|
|
|
void * buffer = &((char*)&InboundPacket.size)[InboundPacket.transferred];
|
|
|
|
size_t bufferLength = sizeof(InboundPacket.size) - InboundPacket.transferred;
|
|
|
|
size_t readBytes;
|
|
|
|
NETWORK_READPACKET status = Socket->ReceiveData(buffer, bufferLength, &readBytes);
|
|
|
|
if (status != NETWORK_READPACKET_SUCCESS)
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
2016-06-01 23:58:21 +02:00
|
|
|
return status;
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
2016-06-01 23:58:21 +02:00
|
|
|
|
2016-05-28 20:03:57 +02:00
|
|
|
InboundPacket.transferred += readBytes;
|
|
|
|
if (InboundPacket.transferred == sizeof(InboundPacket.size))
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
2016-06-01 23:58:21 +02:00
|
|
|
InboundPacket.size = Convert::NetworkToHost(InboundPacket.size);
|
2016-05-28 20:03:57 +02:00
|
|
|
if (InboundPacket.size == 0) // Can't have a size 0 packet
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
|
|
|
return NETWORK_READPACKET_DISCONNECTED;
|
|
|
|
}
|
2016-05-28 20:03:57 +02:00
|
|
|
InboundPacket.data->resize(InboundPacket.size);
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// read packet data
|
2016-05-28 20:03:57 +02:00
|
|
|
if (InboundPacket.data->capacity() > 0)
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
2016-06-01 23:58:21 +02:00
|
|
|
void * buffer = &InboundPacket.GetData()[InboundPacket.transferred - sizeof(InboundPacket.size)];
|
|
|
|
size_t bufferLength = sizeof(InboundPacket.size) + InboundPacket.size - InboundPacket.transferred;
|
|
|
|
size_t readBytes;
|
|
|
|
NETWORK_READPACKET status = Socket->ReceiveData(buffer, bufferLength, &readBytes);
|
|
|
|
if (status != NETWORK_READPACKET_SUCCESS)
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
2016-06-01 23:58:21 +02:00
|
|
|
return status;
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
2016-06-01 23:58:21 +02:00
|
|
|
|
2016-05-28 20:03:57 +02:00
|
|
|
InboundPacket.transferred += readBytes;
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
2016-05-28 20:03:57 +02:00
|
|
|
if (InboundPacket.transferred == sizeof(InboundPacket.size) + InboundPacket.size)
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
_lastPacketTime = SDL_GetTicks();
|
2016-05-28 18:54:45 +02:00
|
|
|
return NETWORK_READPACKET_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NETWORK_READPACKET_MORE_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetworkConnection::SendPacket(NetworkPacket& packet)
|
|
|
|
{
|
2016-06-01 23:58:21 +02:00
|
|
|
uint16 sizen = Convert::HostToNetwork(packet.size);
|
2016-05-28 18:54:45 +02:00
|
|
|
std::vector<uint8> tosend;
|
|
|
|
tosend.reserve(sizeof(sizen) + packet.size);
|
|
|
|
tosend.insert(tosend.end(), (uint8*)&sizen, (uint8*)&sizen + sizeof(sizen));
|
|
|
|
tosend.insert(tosend.end(), packet.data->begin(), packet.data->end());
|
2016-06-01 23:58:21 +02:00
|
|
|
|
|
|
|
const void * buffer = &tosend[packet.transferred];
|
|
|
|
size_t bufferSize = tosend.size() - packet.transferred;
|
2016-06-10 00:04:02 +02:00
|
|
|
size_t sent = Socket->SendData(buffer, bufferSize);
|
|
|
|
if (sent > 0)
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
2016-06-10 00:04:02 +02:00
|
|
|
packet.transferred += sent;
|
2016-06-01 23:58:21 +02:00
|
|
|
}
|
2016-06-10 00:04:02 +02:00
|
|
|
if (packet.transferred == tosend.size())
|
2016-06-01 23:58:21 +02:00
|
|
|
{
|
2016-06-10 00:04:02 +02:00
|
|
|
return true;
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
2016-06-10 00:04:02 +02:00
|
|
|
return false;
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkConnection::QueuePacket(std::unique_ptr<NetworkPacket> packet, bool front)
|
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
if (AuthStatus == NETWORK_AUTH_OK || !packet->CommandRequiresAuth())
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
|
|
|
packet->size = (uint16)packet->data->size();
|
|
|
|
if (front)
|
|
|
|
{
|
2016-09-13 00:59:22 +02:00
|
|
|
// If the first packet was already partially sent add new packet to second position
|
|
|
|
if (_outboundPackets.size() > 0 && _outboundPackets.front()->transferred > 0)
|
|
|
|
{
|
|
|
|
auto it = _outboundPackets.begin();
|
|
|
|
it++; // Second position
|
|
|
|
_outboundPackets.insert(it, std::move(packet));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_outboundPackets.push_front(std::move(packet));
|
|
|
|
}
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
_outboundPackets.push_back(std::move(packet));
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkConnection::SendQueuedPackets()
|
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
while (_outboundPackets.size() > 0 && SendPacket(*(_outboundPackets.front()).get()))
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
_outboundPackets.remove(_outboundPackets.front());
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkConnection::ResetLastPacketTime()
|
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
_lastPacketTime = SDL_GetTicks();
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NetworkConnection::ReceivedPacketRecently()
|
|
|
|
{
|
|
|
|
#ifndef DEBUG
|
2016-05-28 20:03:57 +02:00
|
|
|
if (SDL_TICKS_PASSED(SDL_GetTicks(), _lastPacketTime + 7000))
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-28 20:03:57 +02:00
|
|
|
const utf8 * NetworkConnection::GetLastDisconnectReason() const
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
return this->_lastDisconnectReason;
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 20:03:57 +02:00
|
|
|
void NetworkConnection::SetLastDisconnectReason(const utf8 * src)
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
|
|
|
if (src == nullptr)
|
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
if (_lastDisconnectReason)
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
delete[] _lastDisconnectReason;
|
|
|
|
_lastDisconnectReason = nullptr;
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-28 20:03:57 +02:00
|
|
|
if (_lastDisconnectReason == nullptr)
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
2016-05-28 20:03:57 +02:00
|
|
|
_lastDisconnectReason = new utf8[NETWORK_DISCONNECT_REASON_BUFFER_SIZE];
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
2016-05-28 20:03:57 +02:00
|
|
|
String::Set(_lastDisconnectReason, NETWORK_DISCONNECT_REASON_BUFFER_SIZE, src);
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 20:03:57 +02:00
|
|
|
void NetworkConnection::SetLastDisconnectReason(const rct_string_id string_id, void *args)
|
2016-05-28 18:54:45 +02:00
|
|
|
{
|
|
|
|
char buffer[NETWORK_DISCONNECT_REASON_BUFFER_SIZE];
|
|
|
|
format_string(buffer, string_id, args);
|
2016-05-28 20:03:57 +02:00
|
|
|
SetLastDisconnectReason(buffer);
|
2016-05-28 18:54:45 +02:00
|
|
|
}
|
2016-05-28 23:27:08 +02:00
|
|
|
|
|
|
|
#endif
|