Merge 1.10.0-beta1
This commit is contained in:
@@ -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,12 +29,12 @@
|
||||
#include "network_base.h"
|
||||
#include "network_client.h"
|
||||
#include "../core/backup_type.hpp"
|
||||
#include "../newgrf_revisions.hpp"
|
||||
|
||||
#include "../thread.h"
|
||||
#include "table/strings.h"
|
||||
|
||||
#include "../town.h"
|
||||
#include "network_func.h"
|
||||
#include "../newgrf_revisions.hpp"
|
||||
|
||||
#include "../safeguards.h"
|
||||
/* This file handles all the client-commands */
|
||||
@@ -50,7 +48,7 @@ static const uint32 OPENTTD_NEWGRF_REVISION_MASK = (1 << 19) - 1;
|
||||
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.
|
||||
@@ -58,10 +56,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.
|
||||
@@ -86,14 +91,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);
|
||||
@@ -115,11 +120,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;
|
||||
}
|
||||
@@ -144,9 +149,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;
|
||||
}
|
||||
|
||||
@@ -154,7 +159,7 @@ ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s) : Netwo
|
||||
ClientNetworkGameSocketHandler::~ClientNetworkGameSocketHandler()
|
||||
{
|
||||
assert(ClientNetworkGameSocketHandler::my_client == this);
|
||||
ClientNetworkGameSocketHandler::my_client = NULL;
|
||||
ClientNetworkGameSocketHandler::my_client = nullptr;
|
||||
|
||||
delete this->savegame;
|
||||
}
|
||||
@@ -302,7 +307,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;
|
||||
@@ -321,9 +326,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;
|
||||
/** Server revision to use when fooling revision check */
|
||||
const char *_network_join_server_revision = NULL;
|
||||
|
||||
@@ -558,7 +563,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;
|
||||
}
|
||||
|
||||
|
||||
@@ -567,7 +572,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)
|
||||
{
|
||||
@@ -601,7 +606,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();
|
||||
@@ -644,7 +649,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);
|
||||
@@ -747,7 +752,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);
|
||||
@@ -833,7 +838,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();
|
||||
|
||||
@@ -851,7 +856,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);
|
||||
@@ -862,7 +867,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);
|
||||
@@ -876,7 +881,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);
|
||||
@@ -889,12 +894,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;
|
||||
@@ -920,7 +925,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 */
|
||||
@@ -985,7 +990,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;
|
||||
}
|
||||
@@ -1000,7 +1005,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();
|
||||
@@ -1009,7 +1014,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) {
|
||||
@@ -1042,7 +1047,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(Packet *p)
|
||||
ci = ci_to;
|
||||
}
|
||||
|
||||
if (ci != NULL) {
|
||||
if (ci != nullptr) {
|
||||
if (strncmp(msg, "synccmuser", 10) == 0) SyncCMUser(msg);
|
||||
else NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), self_send, name, msg, data);
|
||||
}
|
||||
@@ -1056,8 +1061,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;
|
||||
}
|
||||
|
||||
@@ -1073,8 +1078,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);
|
||||
@@ -1095,7 +1100,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);
|
||||
}
|
||||
|
||||
@@ -1165,7 +1170,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;
|
||||
@@ -1270,7 +1275,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);
|
||||
|
||||
@@ -1291,7 +1296,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) {
|
||||
@@ -1375,4 +1380,4 @@ void SyncCMUser(const char *msg) {
|
||||
_novarole = (role >= 50);
|
||||
DEBUG(net, 1, "CityMania user synchronized: %u %u", user_id, role);
|
||||
}
|
||||
#endif /* ENABLE_NETWORK */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user