Update to 1.10.0-beta1

This commit is contained in:
dP
2019-10-31 22:24:28 +03:00
parent b84a475e14
commit 599ccf0c2b
1470 changed files with 354219 additions and 16795 deletions

View File

@@ -9,8 +9,6 @@
/** @file network_client.cpp Client part of the network protocol. */
#ifdef ENABLE_NETWORK
#include "../stdafx.h"
#include "network_gui.h"
#include "../saveload/saveload.h"
@@ -31,6 +29,7 @@
#include "network_base.h"
#include "network_client.h"
#include "../core/backup_type.hpp"
#include "../thread.h"
#include "table/strings.h"
@@ -43,7 +42,7 @@
struct PacketReader : LoadFilter {
static const size_t CHUNK = 32 * 1024; ///< 32 KiB chunks of memory.
AutoFreeSmallVector<byte *, 16> blocks; ///< Buffer with blocks of allocated memory.
std::vector<byte *> blocks; ///< Buffer with blocks of allocated memory.
byte *buf; ///< Buffer we're going to write to/read from.
byte *bufe; ///< End of the buffer we write to/read from.
byte **block; ///< The block we're reading from/writing to.
@@ -51,10 +50,17 @@ struct PacketReader : LoadFilter {
size_t read_bytes; ///< The total number of read bytes.
/** Initialise everything. */
PacketReader() : LoadFilter(NULL), buf(NULL), bufe(NULL), block(NULL), written_bytes(0), read_bytes(0)
PacketReader() : LoadFilter(nullptr), buf(nullptr), bufe(nullptr), block(nullptr), written_bytes(0), read_bytes(0)
{
}
~PacketReader() override
{
for (auto p : this->blocks) {
free(p);
}
}
/**
* Add a packet to this buffer.
* @param p The packet to add.
@@ -79,14 +85,14 @@ struct PacketReader : LoadFilter {
/* Allocate a new chunk and add the remaining data. */
pbuf += to_write;
to_write = in_packet - to_write;
this->buf = *this->blocks.Append() = CallocT<byte>(CHUNK);
this->blocks.push_back(this->buf = CallocT<byte>(CHUNK));
this->bufe = this->buf + CHUNK;
memcpy(this->buf, pbuf, to_write);
this->buf += to_write;
}
/* virtual */ size_t Read(byte *rbuf, size_t size)
size_t Read(byte *rbuf, size_t size) override
{
/* Limit the amount to read to whatever we still have. */
size_t ret_size = size = min(this->written_bytes - this->read_bytes, size);
@@ -108,11 +114,11 @@ struct PacketReader : LoadFilter {
return ret_size;
}
/* virtual */ void Reset()
void Reset() override
{
this->read_bytes = 0;
this->block = this->blocks.Begin();
this->block = this->blocks.data();
this->buf = *this->block++;
this->bufe = this->buf + CHUNK;
}
@@ -137,9 +143,9 @@ void ClientNetworkEmergencySave()
* Create a new socket for the client side of the game connection.
* @param s The socket to connect with.
*/
ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s), savegame(NULL), status(STATUS_INACTIVE)
ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s), savegame(nullptr), status(STATUS_INACTIVE)
{
assert(ClientNetworkGameSocketHandler::my_client == NULL);
assert(ClientNetworkGameSocketHandler::my_client == nullptr);
ClientNetworkGameSocketHandler::my_client = this;
}
@@ -147,7 +153,7 @@ ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s) : Netwo
ClientNetworkGameSocketHandler::~ClientNetworkGameSocketHandler()
{
assert(ClientNetworkGameSocketHandler::my_client == this);
ClientNetworkGameSocketHandler::my_client = NULL;
ClientNetworkGameSocketHandler::my_client = nullptr;
delete this->savegame;
}
@@ -295,7 +301,7 @@ void ClientNetworkGameSocketHandler::ClientError(NetworkRecvStatus res)
/** Our client's connection. */
ClientNetworkGameSocketHandler * ClientNetworkGameSocketHandler::my_client = NULL;
ClientNetworkGameSocketHandler * ClientNetworkGameSocketHandler::my_client = nullptr;
/** Last frame we performed an ack. */
static uint32 last_ack_frame;
@@ -314,9 +320,9 @@ static uint8 _network_server_max_spectators;
CompanyID _network_join_as;
/** Login password from -p argument */
const char *_network_join_server_password = NULL;
const char *_network_join_server_password = nullptr;
/** Company password from -P argument */
const char *_network_join_company_password = NULL;
const char *_network_join_company_password = nullptr;
/** Make sure the server ID length is the same as a md5 hash. */
assert_compile(NETWORK_SERVER_ID_LENGTH == 16 * 2 + 1);
@@ -527,7 +533,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendMove(CompanyID company, co
*/
bool ClientNetworkGameSocketHandler::IsConnected()
{
return my_client != NULL && my_client->status == STATUS_ACTIVE;
return my_client != nullptr && my_client->status == STATUS_ACTIVE;
}
@@ -536,7 +542,7 @@ bool ClientNetworkGameSocketHandler::IsConnected()
* DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p
************/
extern bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL);
extern bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = nullptr);
NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_FULL(Packet *p)
{
@@ -570,7 +576,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO(Pa
if (current >= MAX_COMPANIES) return NETWORK_RECV_STATUS_CLOSE_QUERY;
NetworkCompanyInfo *company_info = GetLobbyCompanyInfo(current);
if (company_info == NULL) return NETWORK_RECV_STATUS_CLOSE_QUERY;
if (company_info == nullptr) return NETWORK_RECV_STATUS_CLOSE_QUERY;
p->Recv_string(company_info->company_name, sizeof(company_info->company_name));
company_info->inaugurated_year = p->Recv_uint32();
@@ -613,7 +619,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(Pac
if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
ci = NetworkClientInfo::GetByClientID(client_id);
if (ci != NULL) {
if (ci != nullptr) {
if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
/* Client name changed, display the change */
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, name);
@@ -708,7 +714,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS(P
/* Check whether we know this GRF */
const GRFConfig *f = FindGRFConfig(c.grfid, FGCM_EXACT, c.md5sum);
if (f == NULL) {
if (f == nullptr) {
/* We do not know this GRF, bail out of initialization */
char buf[sizeof(c.md5sum) * 2 + 1];
md5sumToString(buf, lastof(buf), c.md5sum);
@@ -794,7 +800,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_BEGIN(Packe
if (this->status < STATUS_AUTHORIZED || this->status >= STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
this->status = STATUS_MAP;
if (this->savegame != NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
if (this->savegame != nullptr) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
this->savegame = new PacketReader();
@@ -812,7 +818,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_BEGIN(Packe
NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_SIZE(Packet *p)
{
if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
if (this->savegame == nullptr) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
_network_join_bytes_total = p->Recv_uint32();
SetWindowDirty(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
@@ -823,7 +829,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_SIZE(Packet
NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DATA(Packet *p)
{
if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
if (this->savegame == nullptr) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
/* We are still receiving data, put it to the file */
this->savegame->AddPacket(p);
@@ -837,7 +843,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DATA(Packet
NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet *p)
{
if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
if (this->savegame == nullptr) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
_network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
SetWindowDirty(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
@@ -850,12 +856,12 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet
* game, which would cause us to free this->savegame twice.
*/
LoadFilter *lf = this->savegame;
this->savegame = NULL;
this->savegame = nullptr;
lf->Reset();
/* The map is done downloading, load it */
ClearErrorMessages();
bool load_success = SafeLoad(NULL, SLO_LOAD, DFT_GAME_FILE, GM_NORMAL, NO_DIRECTORY, lf);
bool load_success = SafeLoad(nullptr, SLO_LOAD, DFT_GAME_FILE, GM_NORMAL, NO_DIRECTORY, lf);
/* Long savegame loads shouldn't affect the lag calculation! */
this->last_packet = _realtime_tick;
@@ -881,7 +887,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet
* the server will give us a client-id and let us in */
_network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
ShowJoinStatusWindow();
NetworkSendCommand(0, CCA_NEW, 0, CMD_COMPANY_CTRL, NULL, NULL, _local_company);
NetworkSendCommand(0, CCA_NEW, 0, CMD_COMPANY_CTRL, nullptr, nullptr, _local_company);
}
} else {
/* take control over an existing company */
@@ -946,7 +952,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_COMMAND(Packet
cp.frame = p->Recv_uint32();
cp.my_cmd = p->Recv_bool();
if (err != NULL) {
if (err != nullptr) {
IConsolePrintF(CC_ERROR, "WARNING: %s from server, dropping...", err);
return NETWORK_RECV_STATUS_MALFORMED_PACKET;
}
@@ -961,7 +967,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(Packet *p)
if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
char name[NETWORK_NAME_LENGTH], msg[NETWORK_CHAT_LENGTH];
const NetworkClientInfo *ci = NULL, *ci_to;
const NetworkClientInfo *ci = nullptr, *ci_to;
NetworkAction action = (NetworkAction)p->Recv_uint8();
ClientID client_id = (ClientID)p->Recv_uint32();
@@ -970,7 +976,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(Packet *p)
int64 data = p->Recv_uint64();
ci_to = NetworkClientInfo::GetByClientID(client_id);
if (ci_to == NULL) return NETWORK_RECV_STATUS_OKAY;
if (ci_to == nullptr) return NETWORK_RECV_STATUS_OKAY;
/* Did we initiate the action locally? */
if (self_send) {
@@ -1003,7 +1009,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(Packet *p)
ci = ci_to;
}
if (ci != NULL) {
if (ci != nullptr) {
NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), self_send, name, msg, data);
}
return NETWORK_RECV_STATUS_OKAY;
@@ -1016,8 +1022,8 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_ERROR_QUIT(Pack
ClientID client_id = (ClientID)p->Recv_uint32();
NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
if (ci != NULL) {
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, GetNetworkErrorMsg((NetworkErrorCode)p->Recv_uint8()));
if (ci != nullptr) {
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, nullptr, GetNetworkErrorMsg((NetworkErrorCode)p->Recv_uint8()));
delete ci;
}
@@ -1033,8 +1039,8 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_QUIT(Packet *p)
ClientID client_id = (ClientID)p->Recv_uint32();
NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
if (ci != NULL) {
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, STR_NETWORK_MESSAGE_CLIENT_LEAVING);
if (ci != nullptr) {
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, nullptr, STR_NETWORK_MESSAGE_CLIENT_LEAVING);
delete ci;
} else {
DEBUG(net, 0, "Unknown client (%d) is leaving the game", client_id);
@@ -1053,7 +1059,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_JOIN(Packet *p)
ClientID client_id = (ClientID)p->Recv_uint32();
NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
if (ci != NULL) {
if (ci != nullptr) {
NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, ci->client_name);
}
@@ -1123,7 +1129,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MOVE(Packet *p)
const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
/* Just make sure we do not try to use a client_index that does not exist */
if (ci == NULL) return NETWORK_RECV_STATUS_OKAY;
if (ci == nullptr) return NETWORK_RECV_STATUS_OKAY;
/* if not valid player, force spectator, else check player exists */
if (!Company::IsValidID(company_id)) company_id = COMPANY_SPECTATOR;
@@ -1226,7 +1232,7 @@ void NetworkClientRequestMove(CompanyID company_id, const char *pass)
*/
void NetworkClientsToSpectators(CompanyID cid)
{
Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
Backup<CompanyID> cur_company(_current_company, FILE_LINE);
/* If our company is changing owner, go to spectators */
if (cid == _local_company) SetLocalCompany(COMPANY_SPECTATOR);
@@ -1247,7 +1253,7 @@ void NetworkUpdateClientName()
{
NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(_network_own_client_id);
if (ci == NULL) return;
if (ci == nullptr) return;
/* Don't change the name if it is the same as the old name */
if (strcmp(ci->client_name, _settings_client.network.client_name) != 0) {
@@ -1320,5 +1326,3 @@ bool NetworkMaxSpectatorsReached()
{
return NetworkSpectatorCount() >= (_network_server ? _settings_client.network.max_spectators : _network_server_max_spectators);
}
#endif /* ENABLE_NETWORK */