Go to the documentation of this file.
10 #include "../stdafx.h"
11 #include "../strings_func.h"
12 #include "../date_func.h"
18 #include "../console_func.h"
19 #include "../company_base.h"
20 #include "../command_func.h"
21 #include "../saveload/saveload.h"
22 #include "../saveload/saveload_filter.h"
23 #include "../station_base.h"
24 #include "../genworld.h"
25 #include "../company_func.h"
26 #include "../company_gui.h"
27 #include "../company_cmd.h"
28 #include "../roadveh.h"
29 #include "../order_backup.h"
30 #include "../core/pool_func.hpp"
31 #include "../core/random_func.hpp"
32 #include "../company_cmd.h"
35 #include <condition_variable>
37 #include "../safeguards.h"
78 std::unique_lock<std::mutex>
lock(this->mutex);
80 if (this->cs !=
nullptr) this->exit_sig.wait(
lock);
84 while (this->packets !=
nullptr) {
103 std::unique_lock<std::mutex>
lock(this->mutex);
107 this->exit_sig.notify_all();
126 if (this->packets ==
nullptr)
return false;
128 std::lock_guard<std::mutex>
lock(this->mutex);
130 while (this->packets !=
nullptr) {
135 if (last_packet)
return true;
144 if (this->current ==
nullptr)
return;
147 this->current =
nullptr;
153 if (this->current ==
nullptr)
return;
157 this->packets = this->current;
158 this->current =
nullptr;
161 void Write(
byte *buf,
size_t size)
override
164 if (this->cs ==
nullptr)
SlError(STR_NETWORK_ERROR_LOSTCONNECTION);
168 std::lock_guard<std::mutex>
lock(this->mutex);
170 byte *bufe = buf + size;
171 while (buf != bufe) {
172 size_t written = this->current->
Send_bytes(buf, bufe);
181 this->total_size += size;
187 if (this->cs ==
nullptr)
SlError(STR_NETWORK_ERROR_LOSTCONNECTION);
189 std::lock_guard<std::mutex>
lock(this->mutex);
200 this->current->
Send_uint32((uint32)this->total_size);
201 this->PrependQueue();
259 if (this->
sock == INVALID_SOCKET)
return status;
265 NetworkTextMessage(NETWORK_ACTION_LEAVE,
CC_DEFAULT,
false, client_name,
"", STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST);
268 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
270 new_cs->SendErrorQuit(this->
client_id, NETWORK_ERROR_CONNECTION_LOST);
282 this->CheckNextClientToSendMap(
this);
322 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
386 Debug(net, 1,
"'{}' made an error and has been disconnected: {}", client_name, GetString(strid));
388 if (
error == NETWORK_ERROR_KICKED && !reason.empty()) {
389 NetworkTextMessage(NETWORK_ACTION_KICKED,
CC_DEFAULT,
false, client_name, reason, strid);
391 NetworkTextMessage(NETWORK_ACTION_LEAVE,
CC_DEFAULT,
false, client_name,
"", strid);
394 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
398 if (
error == NETWORK_ERROR_NOT_AUTHORIZED ||
error == NETWORK_ERROR_NOT_EXPECTED ||
error == NETWORK_ERROR_WRONG_REVISION) {
399 error = NETWORK_ERROR_ILLEGAL_PACKET;
401 new_cs->SendErrorQuit(this->
client_id, error);
407 Debug(net, 1,
"Client {} made an error and has been disconnected: {}", this->
client_id, GetString(strid));
487 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
503 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
505 if (new_cs->GetInfo()->join_date < this->GetInfo()->join_date || (new_cs->GetInfo()->join_date == this->GetInfo()->join_date && new_cs->client_id < this->client_id)) waiting++;
514 void ServerNetworkGameSocketHandler::CheckNextClientToSendMap(NetworkClientSocket *ignore_cs)
517 NetworkClientSocket *best =
nullptr;
518 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
519 if (ignore_cs == new_cs)
continue;
522 if (best ==
nullptr || best->GetInfo()->join_date > new_cs->GetInfo()->join_date || (best->GetInfo()->join_date == new_cs->GetInfo()->join_date && best->client_id > new_cs->client_id)) {
529 if (best !=
nullptr) {
535 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
546 return this->
SendError(NETWORK_ERROR_NOT_AUTHORIZED);
578 this->CheckNextClientToSendMap();
604 #ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
606 #ifdef NETWORK_SEND_DOUBLE_SEED
613 this->
last_token = InteractiveRandomRange(UINT8_MAX - 1) + 1;
628 #ifdef NETWORK_SEND_DOUBLE_SEED
808 return this->
SendError(NETWORK_ERROR_NOT_EXPECTED);
829 return this->
SendError(NETWORK_ERROR_NOT_EXPECTED);
834 return this->
SendError(NETWORK_ERROR_FULL);
843 return this->
SendError(NETWORK_ERROR_WRONG_REVISION);
855 return this->
SendError(NETWORK_ERROR_FULL);
862 return this->
SendError(NETWORK_ERROR_COMPANY_MISMATCH);
871 return this->
SendError(NETWORK_ERROR_INVALID_CLIENT_NAME);
876 return this->
SendError(NETWORK_ERROR_NAME_IN_USE);
903 return this->
SendError(NETWORK_ERROR_NOT_EXPECTED);
912 return this->
SendError(NETWORK_ERROR_WRONG_PASSWORD);
927 return this->
SendError(NETWORK_ERROR_NOT_EXPECTED);
939 return this->
SendError(NETWORK_ERROR_WRONG_PASSWORD);
949 if (this->status < STATUS_AUTHORIZED || this->
HasClientQuit()) {
950 return this->
SendError(NETWORK_ERROR_NOT_AUTHORIZED);
954 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
972 NetworkTextMessage(NETWORK_ACTION_JOIN,
CC_DEFAULT,
false, client_name,
"", this->
client_id);
989 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
991 new_cs->SendClientInfo(this->
GetInfo());
1006 return this->
SendError(NETWORK_ERROR_NOT_EXPECTED);
1017 if (this->status < STATUS_DONE_MAP || this->
HasClientQuit()) {
1018 return this->
SendError(NETWORK_ERROR_NOT_EXPECTED);
1022 return this->
SendError(NETWORK_ERROR_TOO_MANY_COMMANDS);
1032 if (err !=
nullptr) {
1034 return this->
SendError(NETWORK_ERROR_NOT_EXPECTED);
1040 return this->
SendError(NETWORK_ERROR_KICKED);
1045 return this->
SendError(NETWORK_ERROR_KICKED);
1057 return this->
SendError(NETWORK_ERROR_COMPANY_MISMATCH);
1062 return this->
SendError(NETWORK_ERROR_CHEATER);
1085 if (this->status < STATUS_DONE_MAP || this->
HasClientQuit()) {
1092 Debug(net, 1,
"'{}' reported an error and is closing its connection: {}", client_name, GetString(strid));
1094 NetworkTextMessage(NETWORK_ACTION_LEAVE,
CC_DEFAULT,
false, client_name,
"", strid);
1096 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
1098 new_cs->SendErrorQuit(this->
client_id, errorno);
1110 if (this->status < STATUS_DONE_MAP || this->
HasClientQuit()) {
1116 NetworkTextMessage(NETWORK_ACTION_LEAVE,
CC_DEFAULT,
false, client_name,
"", STR_NETWORK_MESSAGE_CLIENT_LEAVING);
1118 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
1133 return this->
SendError(NETWORK_ERROR_NOT_AUTHORIZED);
1195 if (ci !=
nullptr) {
1204 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1205 if (cs->client_id == (
ClientID)dest) {
1206 cs->SendChat(action, from_id,
false, msg, data);
1217 if (ci !=
nullptr && ci_to !=
nullptr) {
1221 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1222 if (cs->client_id == from_id) {
1223 cs->SendChat(action, (
ClientID)dest,
true, msg, data);
1232 bool show_local =
true;
1235 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1238 cs->SendChat(action, from_id,
false, msg, data);
1239 if (cs->client_id == from_id) show_local =
false;
1251 if (ci !=
nullptr && ci_own !=
nullptr && ci_own->
client_playas == dest) {
1258 if (ci_to ==
nullptr)
break;
1261 if (ci !=
nullptr && show_local) {
1265 std::string name = GetString(str);
1268 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1269 if (cs->client_id == from_id) {
1270 cs->SendChat(action, ci_to->
client_id,
true, msg, data);
1278 Debug(net, 1,
"Received unknown chat destination type {}; doing broadcast instead", desttype);
1282 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1283 cs->SendChat(action, from_id,
false, msg, data);
1289 if (ci !=
nullptr) {
1305 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1306 cs->SendExternalChat(source, colour, user, msg);
1308 NetworkTextMessage(NETWORK_ACTION_EXTERNAL_CHAT, colour,
false, user, msg, 0, source);
1315 return this->
SendError(NETWORK_ERROR_NOT_AUTHORIZED);
1327 case NETWORK_ACTION_CHAT:
1328 case NETWORK_ACTION_CHAT_CLIENT:
1329 case NETWORK_ACTION_CHAT_COMPANY:
1334 return this->
SendError(NETWORK_ERROR_NOT_EXPECTED);
1343 return this->
SendError(NETWORK_ERROR_NOT_EXPECTED);
1357 return this->
SendError(NETWORK_ERROR_NOT_EXPECTED);
1367 if (ci !=
nullptr) {
1372 return this->
SendError(NETWORK_ERROR_INVALID_CLIENT_NAME);
1395 Debug(net, 1,
"[rcon] Wrong password from client-id {}", this->
client_id);
1399 Debug(net, 3,
"[rcon] Client-id {} executed: {}", this->
client_id, command);
1423 Debug(net, 2,
"Wrong password from client-id #{} for company #{}", this->
client_id, company_id + 1);
1446 case VEH_TRAIN: type = NETWORK_VEH_TRAIN;
break;
1449 case VEH_SHIP: type = NETWORK_VEH_SHIP;
break;
1477 if (ci ==
nullptr)
return;
1481 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1483 cs->SendClientInfo(ci);
1494 Debug(net, 3,
"Auto-restarting map: year {} reached",
_cur_year);
1526 memset(clients_in_company, 0,
sizeof(clients_in_company));
1530 if (
Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] =
true;
1539 memset(vehicles_in_company, 0,
sizeof(vehicles_in_company));
1543 vehicles_in_company[v->owner]++;
1550 if (c->is_ai)
continue;
1552 if (!clients_in_company[c->index]) {
1590 bool is_name_unique =
false;
1591 std::string original_name = name;
1593 for (uint number = 1; !is_name_unique && number <=
MAX_CLIENTS; number++) {
1594 is_name_unique =
true;
1596 if (ci->client_name == name) {
1598 is_name_unique =
false;
1604 if (ci !=
nullptr) {
1605 if (ci->
client_name == name) is_name_unique =
false;
1608 if (!is_name_unique) {
1610 name = original_name +
" #" + std::to_string(number);
1618 return is_name_unique;
1631 if (ci->client_name.compare(new_name) == 0)
return false;
1635 if (ci ==
nullptr)
return false;
1655 if (already_hashed) {
1671 while ((cp = cs->outgoing_queue.Pop()) !=
nullptr) {
1672 cs->SendCommand(cp);
1683 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
1684 bool send_sync =
false;
1687 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
1696 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1703 uint lag = NetworkCalculateLag(cs);
1704 switch (cs->status) {
1705 case NetworkClientSocket::STATUS_ACTIVE:
1710 "Client #{} (IP: {}) is dropped because the client's game state is more than {} ticks behind." :
1712 "Client #{} (IP: {}) is dropped because the client did not respond for more than {} ticks.",
1713 cs->client_id, cs->GetClientIP(), lag);
1714 cs->SendError(NETWORK_ERROR_TIMEOUT_COMPUTER);
1723 if (lag > (uint)
DAY_TICKS && cs->lag_test == 0 && cs->last_packet + std::chrono::seconds(2) > std::chrono::steady_clock::now()) {
1730 IConsolePrint(
CC_WARNING,
"Client #{} (IP: {}) is dropped because it fails to send valid acks.", cs->client_id, cs->GetClientIP());
1731 cs->SendError(NETWORK_ERROR_TIMEOUT_COMPUTER);
1736 case NetworkClientSocket::STATUS_INACTIVE:
1737 case NetworkClientSocket::STATUS_NEWGRFS_CHECK:
1738 case NetworkClientSocket::STATUS_AUTHORIZED:
1743 cs->SendError(NETWORK_ERROR_TIMEOUT_COMPUTER);
1748 case NetworkClientSocket::STATUS_MAP_WAIT:
1752 if (std::chrono::steady_clock::now() > cs->last_packet + std::chrono::seconds(2)) {
1759 cs->last_packet = std::chrono::steady_clock::now();
1763 case NetworkClientSocket::STATUS_MAP:
1767 cs->SendError(NETWORK_ERROR_TIMEOUT_MAP);
1772 case NetworkClientSocket::STATUS_DONE_MAP:
1773 case NetworkClientSocket::STATUS_PRE_ACTIVE:
1777 cs->SendError(NETWORK_ERROR_TIMEOUT_JOIN);
1782 case NetworkClientSocket::STATUS_AUTH_GAME:
1783 case NetworkClientSocket::STATUS_AUTH_COMPANY:
1787 cs->SendError(NETWORK_ERROR_TIMEOUT_PASSWORD);
1792 case NetworkClientSocket::STATUS_END:
1797 if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) {
1802 if (send_frame) cs->SendFrame();
1804 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
1806 if (send_sync) cs->SendSync();
1846 static const char *
const stat_str[] = {
1849 "authorizing (server password)",
1850 "authorizing (company password)",
1858 static_assert(
lengthof(stat_str) == NetworkClientSocket::STATUS_END);
1860 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1862 if (ci ==
nullptr)
continue;
1863 uint lag = NetworkCalculateLag(cs);
1866 status = (cs->status < (ptrdiff_t)
lengthof(stat_str) ? stat_str[cs->status] :
"unknown");
1868 cs->client_id, ci->
client_name.c_str(), status, lag,
1879 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1880 if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) cs->SendConfigUpdate();
1902 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1903 if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) cs->SendCompanyUpdate();
1930 NetworkClientSocket *cs = NetworkClientSocket::GetByClientID(client_id);
1932 if (cs->status < NetworkClientSocket::STATUS_AUTHORIZED)
return;
1933 cs->SendMove(client_id, company_id);
1953 NetworkClientSocket::GetByClientID(client_id)->SendRConResult(colour_code,
string);
1964 NetworkClientSocket::GetByClientID(client_id)->SendError(NETWORK_ERROR_KICKED, reason);
1988 bool contains =
false;
2005 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
2008 if (cs->client_address.IsInNetmask(ip)) {
2025 if (ci->client_playas == company)
return true;
2041 return fmt::format(
"Client #{}", this->
client_id);
2055 ci->client_id ==
CLIENT_ID_SERVER ?
"server" : NetworkClientSocket::GetByClientID(ci->client_id)->GetClientIP());
2072 assert(c !=
nullptr);
2080 if (ci !=
nullptr) {
2090 if (ci !=
nullptr) {
NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p) override
The client is quitting the game.
@ VEH_AIRCRAFT
Aircraft vehicle type.
uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, const std::string &reason)
Ban, or kick, everyone joined from the given client's IP.
void SerializeNetworkGameInfo(Packet *p, const NetworkServerGameInfo *info, bool send_newgrf_names)
Serializes the NetworkGameInfo struct to the packet.
void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const std::string &msg, ClientID from_id, int64 data, bool from_admin)
Send an actual chat message.
uint16 num_station[NETWORK_VEH_END]
How many stations are there of this type?
Simple calculated statistics of a company.
CompanyCtrlAction
The action to do with CMD_COMPANY_CTRL.
@ PACKET_SERVER_GAME_INFO
Information about the server.
DestType
Destination of our chat messages.
static const TextColour CC_INFO
Colour for information lines.
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
uint16 max_password_time
maximum amount of time, in game ticks, a client may take to enter the password
@ NETWORK_RECV_STATUS_CLIENT_QUIT
The connection is lost gracefully. Other clients are already informed of this leaving client.
uint32 _frame_counter
The current frame.
uint32 generation_seed
noise seed for world generation
@ FT_SCENARIO
old or new scenario
@ PACKET_SERVER_ERROR
Server sending an error message to the client.
size_t Size() const
Get the number of bytes in the packet.
void NetworkServerSendConfigUpdate()
Send Config Update.
void NetworkAdminUpdate(AdminUpdateFrequency freq)
Send (push) updates to the admin network as they have registered for these updates.
@ INVALID_CLIENT_ID
Client is not part of anything.
@ SM_START_HEIGHTMAP
Load a heightmap and start a new game from it.
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
void SerializeGRFIdentifier(Packet *p, const GRFIdentifier *grf)
Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet.
@ SM_LOAD_GAME
Load game, Play Scenario.
uint8 autoclean_unprotected
remove passwordless companies after this many months
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
void NetworkServerShowStatusToConsole()
Show the status message of all clients on the console.
std::string client_name
Name of the client.
static void NetworkCheckRestartMap()
Check if we want to restart the map.
ServerNetworkGameSocketHandler * cs
Socket we are associated with.
void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_id, const std::string &msg, int64 data, bool from_admin)
Send chat to the admin network (if they did opt in for the respective update).
uint8 max_clients
maximum amount of clients
Year _cur_year
Current year, starting at 0.
NetworkRecvStatus SendGameInfo()
Send the client information about the server.
@ CCA_DELETE
Delete a company.
std::mutex lock
synchronization for playback status fields
CompanyID client_playas
As which company is this client playing (CompanyID)
NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const std::string &msg, int64 data)
Send a chat message.
@ FACIL_TRUCK_STOP
Station with truck stops.
@ STATUS_PRE_ACTIVE
The client is catching up the delayed frames.
size_t Send_bytes(const byte *begin, const byte *end)
Send as many of the bytes as possible in the packet.
uint Count() const
Get the number of items in the queue.
DateFract _date_fract
Fractional part of the day.
CommandFlags GetCommandFlags(Commands cmd)
@ PACKET_SERVER_CONFIG_UPDATE
Some network configuration important to the client changed.
bool _network_server
network-server is active
CompanyMask _network_company_passworded
Bitmask of the password status of all companies.
NetworkAction
Actions that can be used for NetworkTextMessage.
uint32 frame
the frame in which this packet is executed
@ SPS_CLOSED
The connection got closed.
std::chrono::steady_clock::time_point last_packet
Time we received the last frame.
@ WC_CLIENT_LIST
Client list; Window numbers:
Tindex index
Index of this pool item.
void SetInfo(NetworkClientInfo *info)
Sets the client info for this socket handler.
NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p) override
Set the password for the clients current company: string The password.
static const uint NETWORK_CHAT_LENGTH
The maximum length of a chat message, in bytes including '\0'.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Date join_date
Gamedate the client has joined.
static void NetworkAutoCleanCompanies()
Check if the server has autoclean_companies activated Two things happen: 1) If a company is not prote...
@ ADMIN_FREQUENCY_DAILY
The admin gets information about this on a daily basis.
@ PACKET_SERVER_NEWGAME
The server is preparing to start a new game.
void NetworkPrintClients()
Print all the clients to the console.
void SetLocalCompany(CompanyID new_company)
Sets the local company and updates the settings that are set on a per-company basis to reflect the co...
SOCKET sock
The socket currently connected to.
size_t receive_limit
Amount of bytes that we can receive at this moment.
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
uint32 last_token_frame
The last frame we received the right token.
static const uint MAX_CLIENTS
How many clients can we have.
static const char * GetName()
Get the name used by the listener.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
virtual Packet * ReceivePacket() override
Receives a packet for the given client.
uint32 last_frame_server
Last frame the server has executed.
@ DESTTYPE_TEAM
Send message/notice to everyone playing the same company (Team)
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
ClientSettings _settings_client
The current settings for this game.
@ PACKET_SERVER_WAIT
Server tells the client there are some people waiting for the map as well.
@ STATUS_MAP
The client is downloading the map.
@ PACKET_SERVER_JOIN
Tells clients that a new client has joined.
static bool IsValidHumanID(size_t index)
Is this company a valid company, not controlled by a NoAI program?
void NetworkSyncCommandQueue(NetworkClientSocket *cs)
Sync our local command queue to the command queue of the given socket.
@ ADMIN_FREQUENCY_MONTHLY
The admin gets information about this on a monthly basis.
@ STATUS_MAP_WAIT
The client is waiting as someone else is downloading the map.
void NetworkServerMonthlyLoop()
Monthly "callback".
uint8 autoclean_protected
remove the password from passworded companies after this many months
@ WC_COMPANY
Company view; Window numbers:
StringID GetNetworkErrorMsg(NetworkErrorCode err)
Retrieve the string id of an internal error number.
@ VEH_ROAD
Road vehicle type.
NetworkServerGameInfo _network_game_info
Information about our game.
uint16 months_empty
How many months the company is empty.
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
static const uint NETWORK_CLIENT_NAME_LENGTH
The maximum length of a client's name, in bytes including '\0'.
Owner
Enum for all companies/owners.
Base socket handler for all TCP sockets.
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
static const uint32 GENERATE_NEW_SEED
Create a new random seed.
NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p) override
Tell the server that we are done receiving/loading the map.
void NetworkAdminClientQuit(ClientID client_id)
Notify the admin network that a client quit (if they have opt in for the respective update).
Defines the traits of a command.
static const TextColour CC_DEFAULT
Default colour of the console.
static void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id)
Insert a client ID into the command data in a command packet.
uint32 _frame_counter_max
To where we may go with our clients.
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const std::string &string)
Send an rcon reply to the client.
@ STATUS_ACTIVE
The client is active within in the game.
void NetworkServer_Tick(bool send_frame)
This is called every tick if this is a _network_server.
NetworkRecvStatus SendCommand(const CommandPacket *cp)
Send a command to the client to execute.
@ PACKET_SERVER_MAP_SIZE
Server tells the client what the (compressed) size of the map is.
void FillStaticNetworkServerGameInfo()
Fill a NetworkServerGameInfo structure with the static content, or things that are so static they can...
void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client)
Notify the admin network of a new client (if they did opt in for the respective update).
GameCreationSettings game_creation
settings used during the creation of a game (map)
void Finish() override
Prepare everything to finish writing the savegame.
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
static void AddToQueue(Packet **queue, Packet *packet)
Add the given Packet to the end of the queue of packets.
@ PACKET_SERVER_MAP_DONE
Server tells it has just sent the last bits of the map to the client.
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
std::string Recv_string(size_t length, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads characters (bytes) from the packet until it finds a '\0', or reaches a maximum of length charac...
virtual Packet * ReceivePacket()
Receives a packet for the given client.
uint16 max_download_time
maximum amount of time, in game ticks, a client may take to download the map
void Append(CommandPacket *p)
Append a CommandPacket at the end of the queue.
static Packet * PopFromQueue(Packet **queue)
Pop the packet from the begin of the queue and set the begin of the queue to the second element in th...
bool NetworkMakeClientNameUnique(std::string &name)
Check whether a name is unique, and otherwise try to make it unique.
static constexpr size_t MAX_SIZE
Make template parameter accessible from outside.
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it's client-identifier.
@ ADMIN_FREQUENCY_QUARTERLY
The admin gets information about this on a quarterly basis.
NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p) override
Send a password to the server to authorize uint8 Password type (see NetworkPasswordType).
void PrependQueue()
Prepend the current packet to the queue.
CompanyID company
company that is executing the command
AbstractFileType abstract_ftype
Abstract type of file (scenario, heightmap, etc).
@ FACIL_BUS_STOP
Station with bus stops.
NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p) override
Tell the server that we have the required GRFs.
std::string GetClientName() const
Get the name of the client, if the user did not send it yet, Client ID is used.
Template for TCP listeners.
~ServerNetworkGameSocketHandler()
Clear everything related to this client.
void NetworkServerSendExternalChat(const std::string &source, TextColour colour, const std::string &user, const std::string &msg)
Send a chat message from external source.
@ COMPANY_NEW_COMPANY
The client wants a new company.
bool server_admin_chat
allow private chat for the server to be distributed to the admin network
@ STATUS_INACTIVE
The client is not connected nor active.
bool TransferToNetworkQueue(ServerNetworkGameSocketHandler *socket)
Transfer all packets from here to the network's queue while holding the lock on our mutex.
static void Send()
Send the packets for the server sockets.
uint16 num_vehicle[NETWORK_VEH_END]
How many vehicles are there of this type?
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
@ PACKET_SERVER_NEED_GAME_PASSWORD
Server requests the (hashed) game password.
Writing a savegame directly to a number of packets.
void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded)
Tell that a particular company is (not) passworded.
Everything we need to know about a command to be able to execute it.
Date _date
Current date in days (day counter)
NetworkRecvStatus SendSync()
Request the client to sync.
Information about GRF, used in the game and (part of it) in savegames.
@ PACKET_SERVER_SHUTDOWN
The server is shutting down.
@ PACKET_SERVER_NEED_COMPANY_PASSWORD
Server requests the (hashed) company password.
NetworkRecvStatus SendConfigUpdate()
Send an update about the max company/spectator counts.
@ SM_NEWGAME
New Game --> 'Random game'.
NetworkRecvStatus Receive_CLIENT_GAME_INFO(Packet *p) override
Request game information.
static void NetworkHandleCommandQueue(NetworkClientSocket *cs)
Handle the command-queue of a socket.
uint16 bytes_per_frame
how many bytes may, over a long period, be received per frame?
@ NETWORK_RECV_STATUS_SERVER_ERROR
The server told us we made an error.
@ PACKET_SERVER_COMPANY_UPDATE
Information (password) of a company changed.
std::string GenerateCompanyPasswordHash(const std::string &password, const std::string &password_server_id, uint32 password_game_seed)
Hash the given password using server ID and game seed.
uint8 flags
NOSAVE: GCF_Flags, bitset.
Year restart_game_year
year the server restarts
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
ClientID
'Unique' identifier to be given to clients
@ PACKET_SERVER_BANNED
The server has banned you.
NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id)
Tell that a client moved to another company.
NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p) override
Try to join the server: string OpenTTD revision (norev000 if no revision).
@ CMD_COMPANY_CTRL
used in multiplayer to create a new companies etc.
NetworkRecvStatus SendJoin(ClientID client_id)
Tell that a client joined.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
uint32 _sync_seed_1
Seed to compare during sync checks.
NetworkRecvStatus SendFrame()
Tell the client that they may run to a particular frame.
NetworkRecvStatus SendRConResult(uint16 colour, const std::string &command)
Send the result of a console action.
@ MAX_COMPANIES
Maximum number of companies.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
@ CMD_CLIENT_ID
set p2 with the ClientID of the sending client.
size_t total_size
Total size of the compressed savegame.
std::string password
The password for the company.
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
@ CLIENT_ID_SERVER
Servers always have this ID.
NetworkCompanyState * _network_company_states
Statistics about some companies.
NetworkRecvStatus SendExternalChat(const std::string &source, TextColour colour, const std::string &user, const std::string &msg)
Send a chat message from external source.
NetworkRecvStatus SendCompanyUpdate()
Send an update about the company password states.
NetworkRecvStatus SendWelcome()
Send the client a welcome message with some basic information.
static ClientID _network_client_id
The identifier counter for new clients (is never decreased)
NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p) override
Request the server to move this client into another company: uint8 ID of the company the client wants...
bool my_cmd
did the command originate from "me"
NetworkRecvStatus SendError(NetworkErrorCode error, const std::string &reason={})
Send an error to the client, and close its connection.
ClientStatus status
Status of this client.
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats)
Populate the company stats.
static const uint16 TCP_MTU
Number of bytes we can pack in a single TCP packet.
@ FACIL_DOCK
Station with a dock.
bool _network_dedicated
are we a dedicated server?
struct PacketWriter * savegame
Writer used to write the savegame.
Internal entity of a packet.
uint16 max_join_time
maximum amount of time, in game ticks, a client may take to sync up during joining
@ FT_SAVEGAME
old or new savegame
NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci)
Send the client information about a client.
const char * ReceiveCommand(Packet *p, CommandPacket *cp)
Receives a command from the network.
@ ADMIN_FREQUENCY_ANUALLY
The admin gets information about this on a yearly basis.
static const uint NETWORK_REVISION_LENGTH
The maximum length of the revision, in bytes including '\0'.
@ STATUS_DONE_MAP
The client has downloaded the map.
bool NetworkCompanyIsPassworded(CompanyID company_id)
Check if the company we want to join requires a password.
NetworkRecvStatus SendQuit(ClientID client_id)
Tell the client another client quit.
ClientID client_id
Client identifier (same as ClientState->client_id)
@ PACKET_SERVER_MOVE
Server tells everyone that someone is moved to another company.
void NetworkServerDailyLoop()
Daily "callback".
static const uint NETWORK_PASSWORD_LENGTH
The maximum length of the password, in bytes including '\0' (must be >= NETWORK_SERVER_ID_LENGTH)
const NetworkServerGameInfo * GetCurrentNetworkServerGameInfo()
Get the NetworkServerGameInfo structure with the latest information of the server.
@ PACKET_SERVER_FULL
The server is full and has no place for you.
bool IsNetworkCompatibleVersion(std::string_view other)
Checks whether the given version string is compatible with our version.
void Send_bool(bool data)
Package a boolean in the packet.
void NORETURN SlError(StringID string, const char *extra_msg)
Error handler.
void NetworkAdminCompanyUpdate(const Company *company)
Notify the admin network of company updates.
std::string server_name
name of the server
NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno)
Tell the client another client quit with an error.
void NetworkServerNewCompany(const Company *c, NetworkClientInfo *ci)
Perform all the server specific administration of a new company.
Class for handling the server side of the game connection.
NetworkClientInfo * GetInfo() const
Gets the client info of this socket handler.
std::string rcon_password
password for rconsole (server side)
SwitchMode _switch_mode
The next mainloop command.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
void AppendQueue()
Append the current packet to the queue.
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p) override
The client has done a command and wants us to handle it.
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
static const uint NETWORK_RCONCOMMAND_LENGTH
The maximum length of a rconsole command, in bytes including '\0'.
void NetworkServerDoMove(ClientID client_id, CompanyID company_id)
Handle the tid-bits of moving a client from one company to another.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
const std::string & GetClientIP()
Get the IP address/hostname of the connected client.
Base class for all pools.
NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p) override
Gives the client a new name: string New name of the client.
@ PACKET_SERVER_ERROR_QUIT
A server tells that a client has hit an error and did quit.
static size_t GetNumItems()
Returns number of valid items in the pool.
PacketType GetPacketType() const
Get the PacketType from this packet.
NetworkRecvStatus SendNewGame()
Tell the client we're starting a new game.
@ FACIL_TRAIN
Station with train station.
~PacketWriter()
Make sure everything is cleaned up.
bool CanWriteToPacket(size_t bytes_to_write)
Is it safe to write to the packet, i.e.
static RoadVehicle * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
Interface for filtering a savegame till it is written.
TextColour GetDrawStringCompanyColour(CompanyID company)
Get the colour for DrawString-subroutines which matches the colour of the company.
uint16 bytes_per_frame_burst
how many bytes may, over a short period, be received?
CommandDataBuffer data
command parameters.
@ COMPANY_SPECTATOR
The client is spectating.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
byte _network_clients_connected
The amount of clients connected.
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
struct GRFConfig * next
NOSAVE: Next item in the linked list.
@ STATUS_NEWGRFS_CHECK
The client is checking NewGRFs.
SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded)
Save the game using a (writer) filter.
static bool AllowConnection()
Whether an connection is allowed or not at this moment.
NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p) override
Request the map from the server.
std::string server_password
password for joining this server
uint16 max_init_time
maximum amount of time, in game ticks, a client may take to initiate joining
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
@ PACKET_SERVER_SYNC
Server tells the client what the random state should be.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
void NetworkServerKickClient(ClientID client_id, const std::string &reason)
Kick a single client.
NetworkRecvStatus SendMap()
This sends the map to the client.
@ FT_HEIGHTMAP
heightmap file
@ PACKET_SERVER_MAP_BEGIN
Server tells the client that it is beginning to send the map.
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
@ CRR_AUTOCLEAN
The company is removed due to autoclean.
NetworkRecvStatus SendNewGRFCheck()
Send the check for the NewGRFs.
@ STATUS_AUTH_COMPANY
The client is authorizing with company password.
@ PACKET_SERVER_CHAT
Server distributing the message of a client (or itself).
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
@ PACKET_SERVER_RCON
Response of the executed command on the server.
@ PACKET_SERVER_QUIT
A server tells that a client has quit.
NetworkRecvStatus SendNeedGamePassword()
Request the game password.
CommandQueue incoming_queue
The command-queue awaiting handling.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
ClientID client_id
Client identifier.
GRFConfig * _grfconfig
First item in list of current GRF set up.
PacketWriter(ServerNetworkGameSocketHandler *cs)
Create the packet writer.
void Send_string(const std::string_view data)
Sends a string over the network.
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
bool NetworkServerChangeClientName(ClientID client_id, const std::string &new_name)
Change the client name of the given client.
@ PACKET_SERVER_COMMAND
Server distributes a command to (all) the clients.
NetworkRecvStatus Receive_CLIENT_ACK(Packet *p) override
Tell the server we are done with this frame: uint32 Current frame counter of the client.
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
void NetworkAdminClientUpdate(const NetworkClientInfo *ci)
Notify the admin network of a client update (if they did opt in for the respective update).
@ PACKET_SERVER_FRAME
Server tells the client what frame it is in, and thus to where the client may progress.
#define lengthof(x)
Return the length of an fixed size array.
StringList _network_ban_list
The banned clients.
NetworkSettings network
settings related to the network
uint64 Recv_uint64()
Read a 64 bits integer from the packet.
static void ResetUser(uint32 user)
Reset an user's OrderBackup if needed.
uint8 autoclean_novehicles
remove companies with no vehicles after this many months
@ DESTTYPE_BROADCAST
Send message/notice to all clients (All)
std::mutex mutex
Mutex for making threaded saving safe.
ServerNetworkGameSocketHandler(SOCKET s)
Create a new socket for the server side of the game connection.
NetworkRecvStatus Receive_CLIENT_RCON(Packet *p) override
Send an RCon command to the server: string RCon password.
bool IsBus() const
Check whether a roadvehicle is a bus.
NetworkAddress client_address
IP-address of the client (so they can be banned)
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
Execute a given command passed to us.
void ProcessAsyncSaveFinish()
Handle async save finishes.
void Destroy()
Begin the destruction of this packet writer.
uint16 max_lag_time
maximum amount of time, in game ticks, a client may be lagging behind the server
NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p) override
Sends a chat-packet to the server: uint8 ID of the action (see NetworkAction).
@ PACKET_SERVER_CLIENT_INFO
Server sends you information about a client.
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
void SendCommand(Packet *p, const CommandPacket *cp)
Sends a command over the network.
NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p) override
Send a password to the server to authorize: uint8 Password type (see NetworkPasswordType).
@ ADMIN_FREQUENCY_WEEKLY
The admin gets information about this on a weekly basis.
@ DESTTYPE_CLIENT
Send message/notice to only a certain client (Private)
void NetworkServerUpdateGameInfo()
Update the server's NetworkServerGameInfo due to changes in settings.
@ PACKET_SERVER_WELCOME
Server welcomes you and gives you your ClientID.
NetworkRecvStatus SendShutdown()
Tell the client we're shutting down.
@ VEH_TRAIN
Train vehicle type.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
@ FACIL_AIRPORT
Station with an airport.
@ CMD_SERVER
the command can only be initiated by the server
@ CMD_SPECTATOR
the command may be initiated by a spectator
@ CCA_NEW
Create a new company.
byte last_token
The last random token we did send to verify the client is listening.
std::condition_variable exit_sig
Signal for threaded destruction of this packet writer.
byte clients_on
Current count of clients on server.
uint16 max_commands_in_queue
how many commands may there be in the incoming queue before dropping the connection?
static const TextColour CC_WARNING
Colour for warning lines.
static const uint MAX_CLIENT_SLOTS
The number of slots; must be at least 1 more than MAX_CLIENTS.
void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &password, bool already_hashed)
Set/Reset a company password on the server end.
@ VEH_SHIP
Ship vehicle type.
Month _cur_month
Current month (0..11)
@ PACKET_SERVER_EXTERNAL_CHAT
Server distributing the message from external source.
NetworkRecvStatus SendNeedCompanyPassword()
Request the company password.
@ PACKET_SERVER_CHECK_NEWGRFS
Server sends NewGRF IDs and MD5 checksums for the client to check.
void NetworkServerYearlyLoop()
Yearly "callback".
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
@ SL_OK
completed successfully
uint32 last_frame
Last frame we have executed.
NetworkClientSocketPool _networkclientsocket_pool("NetworkClientSocket")
Make very sure the preconditions given in network_type.h are actually followed.
static const int DAY_TICKS
1 day is 74 ticks; _date_fract used to be uint16 and incremented by 885.
Packet * current
The packet we're currently writing to.
uint32 _last_sync_frame
Used in the server to store the last time a sync packet was sent to clients.
void NetworkAdminCompanyInfo(const Company *company, bool new_company)
Notify the admin network of company details.
NetworkErrorCode
The error codes we send around in the protocols.
Container for all information known about a client.
bool NetworkIsValidClientName(const std::string_view client_name)
Check whether the given client name is deemed valid for use in network games.
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override
Close the network connection due to the given status.
@ STATUS_AUTH_GAME
The client is authorizing with game (server) password.
bool NetworkCompanyHasClients(CompanyID company)
Check whether a particular company has clients.
Packet * packets
Packet queue of the savegame; send these "slowly" to the client.
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
void NetworkAdminClientError(ClientID client_id, NetworkErrorCode error_code)
Notify the admin network of a client error (if they have opt in for the respective update).
@ GCF_STATIC
GRF file is used statically (can be used in any MP game)
uint16 sync_freq
how often do we check whether we are still in-sync
NetworkRecvStatus SendWait()
Tell the client that its put in a waiting queue.
bool autoclean_companies
automatically remove companies that are not in use
@ PACKET_SERVER_MAP_DATA
Server sends bits of the map to the client.
std::string network_id
network ID for servers
uint8 max_companies
maximum amount of companies
@ STATUS_AUTHORIZED
The client is authorized.
@ CLIENT_ID_FIRST
The first client ID.
@ NETWORK_RECV_STATUS_MALFORMED_PACKET
We apparently send a malformed packet.
void NetworkUpdateClientInfo(ClientID client_id)
Send updated client info of a particular client.
Commands cmd
command being executed.
NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p) override
The client made an error and is quitting the game.
void IConsolePrint(TextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.