4 * This file is part of OpenTTD.
5 * OpenTTD 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, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file network_client.cpp Client part of the network protocol. */
14 #include "../stdafx.h"
15 #include "network_gui.h"
16 #include "../saveload/saveload.h"
17 #include "../saveload/saveload_filter.h"
18 #include "../command_func.h"
19 #include "../console_func.h"
20 #include "../strings_func.h"
21 #include "../window_func.h"
22 #include "../company_func.h"
23 #include "../company_base.h"
24 #include "../company_gui.h"
25 #include "../core/random_func.hpp"
26 #include "../date_func.h"
27 #include "../gfx_func.h"
31 #include "network_base.h"
32 #include "network_client.h"
33 #include "../core/backup_type.hpp"
35 #include "table/strings.h"
37 #include "../safeguards.h"
39 /* This file handles all the client-commands */
42 /** Read some packets, and when do use that data as initial load filter. */
43 struct PacketReader
: LoadFilter
{
44 static const size_t CHUNK
= 32 * 1024; ///< 32 KiB chunks of memory.
46 AutoFreeSmallVector
<byte
*, 16> blocks
; ///< Buffer with blocks of allocated memory.
47 byte
*buf
; ///< Buffer we're going to write to/read from.
48 byte
*bufe
; ///< End of the buffer we write to/read from.
49 byte
**block
; ///< The block we're reading from/writing to.
50 size_t written_bytes
; ///< The total number of bytes we've written.
51 size_t read_bytes
; ///< The total number of read bytes.
53 /** Initialise everything. */
54 PacketReader() : LoadFilter(NULL
), buf(NULL
), bufe(NULL
), block(NULL
), written_bytes(0), read_bytes(0)
59 * Add a packet to this buffer.
60 * @param p The packet to add.
62 void AddPacket(const Packet
*p
)
64 assert(this->read_bytes
== 0);
66 size_t in_packet
= p
->size
- p
->pos
;
67 size_t to_write
= min((size_t)(this->bufe
- this->buf
), in_packet
);
68 const byte
*pbuf
= p
->buffer
+ p
->pos
;
70 this->written_bytes
+= in_packet
;
72 memcpy(this->buf
, pbuf
, to_write
);
73 this->buf
+= to_write
;
76 /* Did everything fit in the current chunk, then we're done. */
77 if (to_write
== in_packet
) return;
79 /* Allocate a new chunk and add the remaining data. */
81 to_write
= in_packet
- to_write
;
82 this->buf
= *this->blocks
.Append() = CallocT
<byte
>(CHUNK
);
83 this->bufe
= this->buf
+ CHUNK
;
85 memcpy(this->buf
, pbuf
, to_write
);
86 this->buf
+= to_write
;
89 /* virtual */ size_t Read(byte
*rbuf
, size_t size
)
91 /* Limit the amount to read to whatever we still have. */
92 size_t ret_size
= size
= min(this->written_bytes
- this->read_bytes
, size
);
93 this->read_bytes
+= ret_size
;
94 const byte
*rbufe
= rbuf
+ ret_size
;
96 while (rbuf
!= rbufe
) {
97 if (this->buf
== this->bufe
) {
98 this->buf
= *this->block
++;
99 this->bufe
= this->buf
+ CHUNK
;
102 size_t to_write
= min(this->bufe
- this->buf
, rbufe
- rbuf
);
103 memcpy(rbuf
, this->buf
, to_write
);
105 this->buf
+= to_write
;
111 /* virtual */ void Reset()
113 this->read_bytes
= 0;
115 this->block
= this->blocks
.Begin();
116 this->buf
= *this->block
++;
117 this->bufe
= this->buf
+ CHUNK
;
123 * Create a new socket for the client side of the game connection.
124 * @param s The socket to connect with.
126 ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s
) : NetworkGameSocketHandler(s
), savegame(NULL
), status(STATUS_INACTIVE
)
128 assert(ClientNetworkGameSocketHandler::my_client
== NULL
);
129 ClientNetworkGameSocketHandler::my_client
= this;
132 /** Clear whatever we assigned. */
133 ClientNetworkGameSocketHandler::~ClientNetworkGameSocketHandler()
135 assert(ClientNetworkGameSocketHandler::my_client
== this);
136 ClientNetworkGameSocketHandler::my_client
= NULL
;
138 delete this->savegame
;
141 NetworkRecvStatus
ClientNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status
)
143 assert(status
!= NETWORK_RECV_STATUS_OKAY
);
145 * Sending a message just before leaving the game calls cs->SendPackets.
146 * This might invoke this function, which means that when we close the
147 * connection after cs->SendPackets we will close an already closed
148 * connection. This handles that case gracefully without having to make
149 * that code any more complex or more aware of the validity of the socket.
151 if (this->sock
== INVALID_SOCKET
) return status
;
153 DEBUG(net
, 1, "Closed client connection %d", this->client_id
);
155 this->SendPackets(true);
157 /* Wait a number of ticks so our leave message can reach the server.
158 * This is especially needed for Windows servers as they seem to get
159 * the "socket is closed" message before receiving our leave message,
160 * which would trigger the server to close the connection as well. */
161 CSleep(3 * MILLISECONDS_PER_TICK
);
163 delete this->GetInfo();
170 * Handle an error coming from the client side.
171 * @param res The "error" that happened.
173 void ClientNetworkGameSocketHandler::ClientError(NetworkRecvStatus res
)
175 /* First, send a CLIENT_ERROR to the server, so he knows we are
176 * disconnection (and why!) */
177 NetworkErrorCode errorno
;
179 /* We just want to close the connection.. */
180 if (res
== NETWORK_RECV_STATUS_CLOSE_QUERY
) {
181 this->NetworkSocketHandler::CloseConnection();
182 this->CloseConnection(res
);
185 DeleteWindowById(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
190 case NETWORK_RECV_STATUS_DESYNC
: errorno
= NETWORK_ERROR_DESYNC
; break;
191 case NETWORK_RECV_STATUS_SAVEGAME
: errorno
= NETWORK_ERROR_SAVEGAME_FAILED
; break;
192 case NETWORK_RECV_STATUS_NEWGRF_MISMATCH
: errorno
= NETWORK_ERROR_NEWGRF_MISMATCH
; break;
193 default: errorno
= NETWORK_ERROR_GENERAL
; break;
196 /* This means we fucked up and the server closed the connection */
197 if (res
!= NETWORK_RECV_STATUS_SERVER_ERROR
&& res
!= NETWORK_RECV_STATUS_SERVER_FULL
&&
198 res
!= NETWORK_RECV_STATUS_SERVER_BANNED
) {
202 _switch_mode
= SM_MENU
;
203 this->CloseConnection(res
);
209 * Check whether we received/can send some data from/to the server and
210 * when that's the case handle it appropriately.
211 * @return true when everything went okay.
213 /* static */ bool ClientNetworkGameSocketHandler::Receive()
215 if (my_client
->CanSendReceive()) {
216 NetworkRecvStatus res
= my_client
->ReceivePackets();
217 if (res
!= NETWORK_RECV_STATUS_OKAY
) {
218 /* The client made an error of which we can not recover.
219 * Close the connection and drop back to the main menu. */
220 my_client
->ClientError(res
);
227 /** Send the packets of this socket handler. */
228 /* static */ void ClientNetworkGameSocketHandler::Send()
230 my_client
->SendPackets();
231 my_client
->CheckConnection();
235 * Actual game loop for the client.
236 * @return Whether everything went okay, or not.
238 /* static */ bool ClientNetworkGameSocketHandler::GameLoop()
242 NetworkExecuteLocalCommandQueue();
244 extern void StateGameLoop();
247 /* Check if we are in sync! */
248 if (_sync_frame
!= 0) {
249 if (_sync_frame
== _frame_counter
) {
250 #ifdef NETWORK_SEND_DOUBLE_SEED
251 if (_sync_seed_1
!= _random
.state
[0] || _sync_seed_2
!= _random
.state
[1]) {
253 if (_sync_seed_1
!= _random
.state
[0]) {
255 NetworkError(STR_NETWORK_ERROR_DESYNC
);
256 DEBUG(desync
, 1, "sync_err: %08x; %02x", _date
, _date_fract
);
257 DEBUG(net
, 0, "Sync error detected!");
258 my_client
->ClientError(NETWORK_RECV_STATUS_DESYNC
);
262 /* If this is the first time we have a sync-frame, we
263 * need to let the server know that we are ready and at the same
264 * frame as he is.. so we can start playing! */
265 if (_network_first_time
) {
266 _network_first_time
= false;
271 } else if (_sync_frame
< _frame_counter
) {
272 DEBUG(net
, 1, "Missed frame for sync-test (%d / %d)", _sync_frame
, _frame_counter
);
281 /** Our client's connection. */
282 ClientNetworkGameSocketHandler
* ClientNetworkGameSocketHandler::my_client
= NULL
;
284 /** Last frame we performed an ack. */
285 static uint32 last_ack_frame
;
287 /** One bit of 'entropy' used to generate a salt for the company passwords. */
288 static uint32 _password_game_seed
;
289 /** The other bit of 'entropy' used to generate a salt for the company passwords. */
290 static char _password_server_id
[NETWORK_SERVER_ID_LENGTH
];
292 /** Maximum number of companies of the currently joined server. */
293 static uint8 _network_server_max_companies
;
294 /** Maximum number of spectators of the currently joined server. */
295 static uint8 _network_server_max_spectators
;
297 /** Who would we like to join as. */
298 CompanyID _network_join_as
;
300 /** Login password from -p argument */
301 const char *_network_join_server_password
= NULL
;
302 /** Company password from -P argument */
303 const char *_network_join_company_password
= NULL
;
305 /** Make sure the server ID length is the same as a md5 hash. */
306 assert_compile(NETWORK_SERVER_ID_LENGTH
== 16 * 2 + 1);
310 * DEF_CLIENT_SEND_COMMAND has no parameters
313 /** Query the server for company information. */
314 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendCompanyInformationQuery()
316 my_client
->status
= STATUS_COMPANY_INFO
;
317 _network_join_status
= NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO
;
318 SetWindowDirty(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
320 Packet
*p
= new Packet(PACKET_CLIENT_COMPANY_INFO
);
321 my_client
->SendPacket(p
);
322 return NETWORK_RECV_STATUS_OKAY
;
325 /** Tell the server we would like to join. */
326 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendJoin()
328 my_client
->status
= STATUS_JOIN
;
329 _network_join_status
= NETWORK_JOIN_STATUS_AUTHORIZING
;
330 SetWindowDirty(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
332 Packet
*p
= new Packet(PACKET_CLIENT_JOIN
);
333 p
->Send_string(_openttd_revision
);
334 p
->Send_uint32(_openttd_newgrf_version
);
335 p
->Send_string(_settings_client
.network
.client_name
); // Client name
336 p
->Send_uint8 (_network_join_as
); // PlayAs
337 p
->Send_uint8 (NETLANG_ANY
); // Language
338 my_client
->SendPacket(p
);
339 return NETWORK_RECV_STATUS_OKAY
;
342 /** Tell the server we got all the NewGRFs. */
343 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendNewGRFsOk()
345 Packet
*p
= new Packet(PACKET_CLIENT_NEWGRFS_CHECKED
);
346 my_client
->SendPacket(p
);
347 return NETWORK_RECV_STATUS_OKAY
;
351 * Set the game password as requested.
352 * @param password The game password.
354 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendGamePassword(const char *password
)
356 Packet
*p
= new Packet(PACKET_CLIENT_GAME_PASSWORD
);
357 p
->Send_string(password
);
358 my_client
->SendPacket(p
);
359 return NETWORK_RECV_STATUS_OKAY
;
363 * Set the company password as requested.
364 * @param password The company password.
366 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendCompanyPassword(const char *password
)
368 Packet
*p
= new Packet(PACKET_CLIENT_COMPANY_PASSWORD
);
369 p
->Send_string(GenerateCompanyPasswordHash(password
, _password_server_id
, _password_game_seed
));
370 my_client
->SendPacket(p
);
371 return NETWORK_RECV_STATUS_OKAY
;
374 /** Request the map from the server. */
375 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendGetMap()
377 my_client
->status
= STATUS_MAP_WAIT
;
379 Packet
*p
= new Packet(PACKET_CLIENT_GETMAP
);
380 my_client
->SendPacket(p
);
381 return NETWORK_RECV_STATUS_OKAY
;
384 /** Tell the server we received the complete map. */
385 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendMapOk()
387 my_client
->status
= STATUS_ACTIVE
;
389 Packet
*p
= new Packet(PACKET_CLIENT_MAP_OK
);
390 my_client
->SendPacket(p
);
391 return NETWORK_RECV_STATUS_OKAY
;
394 /** Send an acknowledgement from the server's ticks. */
395 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendAck()
397 Packet
*p
= new Packet(PACKET_CLIENT_ACK
);
399 p
->Send_uint32(_frame_counter
);
400 p
->Send_uint8 (my_client
->token
);
401 my_client
->SendPacket(p
);
402 return NETWORK_RECV_STATUS_OKAY
;
406 * Send a command to the server.
407 * @param cp The command to send.
409 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendCommand(const CommandPacket
*cp
)
411 Packet
*p
= new Packet(PACKET_CLIENT_COMMAND
);
412 my_client
->NetworkGameSocketHandler::SendCommand(p
, cp
);
414 my_client
->SendPacket(p
);
415 return NETWORK_RECV_STATUS_OKAY
;
418 /** Send a chat-packet over the network */
419 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendChat(NetworkAction action
, DestType type
, int dest
, const char *msg
, int64 data
)
421 Packet
*p
= new Packet(PACKET_CLIENT_CHAT
);
423 p
->Send_uint8 (action
);
424 p
->Send_uint8 (type
);
425 p
->Send_uint32(dest
);
427 p
->Send_uint64(data
);
429 my_client
->SendPacket(p
);
430 return NETWORK_RECV_STATUS_OKAY
;
433 /** Send an error-packet over the network */
434 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendError(NetworkErrorCode errorno
)
436 Packet
*p
= new Packet(PACKET_CLIENT_ERROR
);
438 p
->Send_uint8(errorno
);
439 my_client
->SendPacket(p
);
440 return NETWORK_RECV_STATUS_OKAY
;
444 * Tell the server that we like to change the password of the company.
445 * @param password The new password.
447 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendSetPassword(const char *password
)
449 Packet
*p
= new Packet(PACKET_CLIENT_SET_PASSWORD
);
451 p
->Send_string(GenerateCompanyPasswordHash(password
, _password_server_id
, _password_game_seed
));
452 my_client
->SendPacket(p
);
453 return NETWORK_RECV_STATUS_OKAY
;
457 * Tell the server that we like to change the name of the client.
458 * @param name The new name.
460 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendSetName(const char *name
)
462 Packet
*p
= new Packet(PACKET_CLIENT_SET_NAME
);
464 p
->Send_string(name
);
465 my_client
->SendPacket(p
);
466 return NETWORK_RECV_STATUS_OKAY
;
470 * Tell the server we would like to quit.
472 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendQuit()
474 Packet
*p
= new Packet(PACKET_CLIENT_QUIT
);
476 my_client
->SendPacket(p
);
477 return NETWORK_RECV_STATUS_OKAY
;
481 * Send a console command.
482 * @param pass The password for the remote command.
483 * @param command The actual command.
485 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendRCon(const char *pass
, const char *command
)
487 Packet
*p
= new Packet(PACKET_CLIENT_RCON
);
488 p
->Send_string(pass
);
489 p
->Send_string(command
);
490 my_client
->SendPacket(p
);
491 return NETWORK_RECV_STATUS_OKAY
;
495 * Ask the server to move us.
496 * @param company The company to move to.
497 * @param password The password of the company to move to.
499 NetworkRecvStatus
ClientNetworkGameSocketHandler::SendMove(CompanyID company
, const char *password
)
501 Packet
*p
= new Packet(PACKET_CLIENT_MOVE
);
502 p
->Send_uint8(company
);
503 p
->Send_string(GenerateCompanyPasswordHash(password
, _password_server_id
, _password_game_seed
));
504 my_client
->SendPacket(p
);
505 return NETWORK_RECV_STATUS_OKAY
;
509 * Check whether the client is actually connected (and in the game).
510 * @return True when the client is connected.
512 bool ClientNetworkGameSocketHandler::IsConnected()
514 return my_client
!= NULL
&& my_client
->status
== STATUS_ACTIVE
;
519 * Receiving functions
520 * DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p
523 extern bool SafeLoad(const char *filename
, SaveLoadOperation fop
, DetailedFileType dft
, GameMode newgm
, Subdirectory subdir
, struct LoadFilter
*lf
= NULL
);
525 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_FULL(Packet
*p
)
527 /* We try to join a server which is full */
528 ShowErrorMessage(STR_NETWORK_ERROR_SERVER_FULL
, INVALID_STRING_ID
, WL_CRITICAL
);
529 DeleteWindowById(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
531 return NETWORK_RECV_STATUS_SERVER_FULL
;
534 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_BANNED(Packet
*p
)
536 /* We try to join a server where we are banned */
537 ShowErrorMessage(STR_NETWORK_ERROR_SERVER_BANNED
, INVALID_STRING_ID
, WL_CRITICAL
);
538 DeleteWindowById(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
540 return NETWORK_RECV_STATUS_SERVER_BANNED
;
543 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO(Packet
*p
)
545 if (this->status
!= STATUS_COMPANY_INFO
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
547 byte company_info_version
= p
->Recv_uint8();
549 if (!this->HasClientQuit() && company_info_version
== NETWORK_COMPANY_INFO_VERSION
) {
550 /* We have received all data... (there are no more packets coming) */
551 if (!p
->Recv_bool()) return NETWORK_RECV_STATUS_CLOSE_QUERY
;
553 CompanyID current
= (Owner
)p
->Recv_uint8();
554 if (current
>= MAX_COMPANIES
) return NETWORK_RECV_STATUS_CLOSE_QUERY
;
556 NetworkCompanyInfo
*company_info
= GetLobbyCompanyInfo(current
);
557 if (company_info
== NULL
) return NETWORK_RECV_STATUS_CLOSE_QUERY
;
559 p
->Recv_string(company_info
->company_name
, sizeof(company_info
->company_name
));
560 company_info
->inaugurated_year
= p
->Recv_uint32();
561 company_info
->company_value
= p
->Recv_uint64();
562 company_info
->money
= p
->Recv_uint64();
563 company_info
->income
= p
->Recv_uint64();
564 company_info
->performance
= p
->Recv_uint16();
565 company_info
->use_password
= p
->Recv_bool();
566 for (uint i
= 0; i
< NETWORK_VEH_END
; i
++) {
567 company_info
->num_vehicle
[i
] = p
->Recv_uint16();
569 for (uint i
= 0; i
< NETWORK_VEH_END
; i
++) {
570 company_info
->num_station
[i
] = p
->Recv_uint16();
572 company_info
->ai
= p
->Recv_bool();
574 p
->Recv_string(company_info
->clients
, sizeof(company_info
->clients
));
576 SetWindowDirty(WC_NETWORK_WINDOW
, WN_NETWORK_WINDOW_LOBBY
);
578 return NETWORK_RECV_STATUS_OKAY
;
581 return NETWORK_RECV_STATUS_CLOSE_QUERY
;
584 /* This packet contains info about the client (playas and name)
585 * as client we save this in NetworkClientInfo, linked via 'client_id'
586 * which is always an unique number on a server. */
587 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(Packet
*p
)
589 NetworkClientInfo
*ci
;
590 ClientID client_id
= (ClientID
)p
->Recv_uint32();
591 CompanyID playas
= (CompanyID
)p
->Recv_uint8();
592 char name
[NETWORK_NAME_LENGTH
];
594 p
->Recv_string(name
, sizeof(name
));
596 if (this->status
< STATUS_AUTHORIZED
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
597 if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST
;
599 ci
= NetworkClientInfo::GetByClientID(client_id
);
601 if (playas
== ci
->client_playas
&& strcmp(name
, ci
->client_name
) != 0) {
602 /* Client name changed, display the change */
603 NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE
, CC_DEFAULT
, false, ci
->client_name
, name
);
604 } else if (playas
!= ci
->client_playas
) {
605 /* The client changed from client-player..
606 * Do not display that for now */
609 /* Make sure we're in the company the server tells us to be in,
610 * for the rare case that we get moved while joining. */
611 if (client_id
== _network_own_client_id
) SetLocalCompany(!Company::IsValidID(playas
) ? COMPANY_SPECTATOR
: playas
);
613 ci
->client_playas
= playas
;
614 strecpy(ci
->client_name
, name
, lastof(ci
->client_name
));
616 SetWindowDirty(WC_CLIENT_LIST
, 0);
618 return NETWORK_RECV_STATUS_OKAY
;
621 /* There are at most as many ClientInfo as ClientSocket objects in a
622 * server. Having more info than a server can have means something
623 * has gone wrong somewhere, i.e. the server has more info than it
624 * has actual clients. That means the server is feeding us an invalid
625 * state. So, bail out! This server is broken. */
626 if (!NetworkClientInfo::CanAllocateItem()) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
628 /* We don't have this client_id yet, find an empty client_id, and put the data there */
629 ci
= new NetworkClientInfo(client_id
);
630 ci
->client_playas
= playas
;
631 if (client_id
== _network_own_client_id
) this->SetInfo(ci
);
633 strecpy(ci
->client_name
, name
, lastof(ci
->client_name
));
635 SetWindowDirty(WC_CLIENT_LIST
, 0);
637 return NETWORK_RECV_STATUS_OKAY
;
640 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_ERROR(Packet
*p
)
642 static const StringID network_error_strings
[] = {
643 STR_NETWORK_ERROR_LOSTCONNECTION
, // NETWORK_ERROR_GENERAL
644 STR_NETWORK_ERROR_LOSTCONNECTION
, // NETWORK_ERROR_DESYNC
645 STR_NETWORK_ERROR_LOSTCONNECTION
, // NETWORK_ERROR_SAVEGAME_FAILED
646 STR_NETWORK_ERROR_LOSTCONNECTION
, // NETWORK_ERROR_CONNECTION_LOST
647 STR_NETWORK_ERROR_LOSTCONNECTION
, // NETWORK_ERROR_ILLEGAL_PACKET
648 STR_NETWORK_ERROR_LOSTCONNECTION
, // NETWORK_ERROR_NEWGRF_MISMATCH
649 STR_NETWORK_ERROR_SERVER_ERROR
, // NETWORK_ERROR_NOT_AUTHORIZED
650 STR_NETWORK_ERROR_SERVER_ERROR
, // NETWORK_ERROR_NOT_EXPECTED
651 STR_NETWORK_ERROR_WRONG_REVISION
, // NETWORK_ERROR_WRONG_REVISION
652 STR_NETWORK_ERROR_LOSTCONNECTION
, // NETWORK_ERROR_NAME_IN_USE
653 STR_NETWORK_ERROR_WRONG_PASSWORD
, // NETWORK_ERROR_WRONG_PASSWORD
654 STR_NETWORK_ERROR_SERVER_ERROR
, // NETWORK_ERROR_COMPANY_MISMATCH
655 STR_NETWORK_ERROR_KICKED
, // NETWORK_ERROR_KICKED
656 STR_NETWORK_ERROR_CHEATER
, // NETWORK_ERROR_CHEATER
657 STR_NETWORK_ERROR_SERVER_FULL
, // NETWORK_ERROR_FULL
658 STR_NETWORK_ERROR_TOO_MANY_COMMANDS
, // NETWORK_ERROR_TOO_MANY_COMMANDS
659 STR_NETWORK_ERROR_TIMEOUT_PASSWORD
, // NETWORK_ERROR_TIMEOUT_PASSWORD
660 STR_NETWORK_ERROR_TIMEOUT_COMPUTER
, // NETWORK_ERROR_TIMEOUT_COMPUTER
661 STR_NETWORK_ERROR_TIMEOUT_MAP
, // NETWORK_ERROR_TIMEOUT_MAP
662 STR_NETWORK_ERROR_TIMEOUT_JOIN
, // NETWORK_ERROR_TIMEOUT_JOIN
664 assert_compile(lengthof(network_error_strings
) == NETWORK_ERROR_END
);
666 NetworkErrorCode error
= (NetworkErrorCode
)p
->Recv_uint8();
668 StringID err
= STR_NETWORK_ERROR_LOSTCONNECTION
;
669 if (error
< (ptrdiff_t)lengthof(network_error_strings
)) err
= network_error_strings
[error
];
671 ShowErrorMessage(err
, INVALID_STRING_ID
, WL_CRITICAL
);
673 DeleteWindowById(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
675 return NETWORK_RECV_STATUS_SERVER_ERROR
;
678 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS(Packet
*p
)
680 if (this->status
!= STATUS_JOIN
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
682 uint grf_count
= p
->Recv_uint8();
683 NetworkRecvStatus ret
= NETWORK_RECV_STATUS_OKAY
;
686 for (; grf_count
> 0; grf_count
--) {
688 this->ReceiveGRFIdentifier(p
, &c
);
690 /* Check whether we know this GRF */
691 const GRFConfig
*f
= FindGRFConfig(c
.grfid
, FGCM_EXACT
, c
.md5sum
);
693 /* We do not know this GRF, bail out of initialization */
694 char buf
[sizeof(c
.md5sum
) * 2 + 1];
695 md5sumToString(buf
, lastof(buf
), c
.md5sum
);
696 DEBUG(grf
, 0, "NewGRF %08X not found; checksum %s", BSWAP32(c
.grfid
), buf
);
697 ret
= NETWORK_RECV_STATUS_NEWGRF_MISMATCH
;
701 if (ret
== NETWORK_RECV_STATUS_OKAY
) {
702 /* Start receiving the map */
703 return SendNewGRFsOk();
706 /* NewGRF mismatch, bail out */
707 ShowErrorMessage(STR_NETWORK_ERROR_NEWGRF_MISMATCH
, INVALID_STRING_ID
, WL_CRITICAL
);
711 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_NEED_GAME_PASSWORD(Packet
*p
)
713 if (this->status
< STATUS_JOIN
|| this->status
>= STATUS_AUTH_GAME
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
714 this->status
= STATUS_AUTH_GAME
;
716 const char *password
= _network_join_server_password
;
717 if (!StrEmpty(password
)) {
718 return SendGamePassword(password
);
721 ShowNetworkNeedPassword(NETWORK_GAME_PASSWORD
);
723 return NETWORK_RECV_STATUS_OKAY
;
726 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_NEED_COMPANY_PASSWORD(Packet
*p
)
728 if (this->status
< STATUS_JOIN
|| this->status
>= STATUS_AUTH_COMPANY
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
729 this->status
= STATUS_AUTH_COMPANY
;
731 _password_game_seed
= p
->Recv_uint32();
732 p
->Recv_string(_password_server_id
, sizeof(_password_server_id
));
733 if (this->HasClientQuit()) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
735 const char *password
= _network_join_company_password
;
736 if (!StrEmpty(password
)) {
737 return SendCompanyPassword(password
);
740 ShowNetworkNeedPassword(NETWORK_COMPANY_PASSWORD
);
742 return NETWORK_RECV_STATUS_OKAY
;
745 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_WELCOME(Packet
*p
)
747 if (this->status
< STATUS_JOIN
|| this->status
>= STATUS_AUTHORIZED
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
748 this->status
= STATUS_AUTHORIZED
;
750 _network_own_client_id
= (ClientID
)p
->Recv_uint32();
752 /* Initialize the password hash salting variables, even if they were previously. */
753 _password_game_seed
= p
->Recv_uint32();
754 p
->Recv_string(_password_server_id
, sizeof(_password_server_id
));
756 /* Start receiving the map */
760 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_WAIT(Packet
*p
)
762 /* We set the internal wait state when requesting the map. */
763 if (this->status
!= STATUS_MAP_WAIT
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
765 /* But... only now we set the join status to waiting, instead of requesting. */
766 _network_join_status
= NETWORK_JOIN_STATUS_WAITING
;
767 _network_join_waiting
= p
->Recv_uint8();
768 SetWindowDirty(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
770 return NETWORK_RECV_STATUS_OKAY
;
773 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_BEGIN(Packet
*p
)
775 if (this->status
< STATUS_AUTHORIZED
|| this->status
>= STATUS_MAP
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
776 this->status
= STATUS_MAP
;
778 if (this->savegame
!= NULL
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
780 this->savegame
= new PacketReader();
782 _frame_counter
= _frame_counter_server
= _frame_counter_max
= p
->Recv_uint32();
784 _network_join_bytes
= 0;
785 _network_join_bytes_total
= 0;
787 _network_join_status
= NETWORK_JOIN_STATUS_DOWNLOADING
;
788 SetWindowDirty(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
790 return NETWORK_RECV_STATUS_OKAY
;
793 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_SIZE(Packet
*p
)
795 if (this->status
!= STATUS_MAP
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
796 if (this->savegame
== NULL
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
798 _network_join_bytes_total
= p
->Recv_uint32();
799 SetWindowDirty(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
801 return NETWORK_RECV_STATUS_OKAY
;
804 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DATA(Packet
*p
)
806 if (this->status
!= STATUS_MAP
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
807 if (this->savegame
== NULL
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
809 /* We are still receiving data, put it to the file */
810 this->savegame
->AddPacket(p
);
812 _network_join_bytes
= (uint32
)this->savegame
->written_bytes
;
813 SetWindowDirty(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
815 return NETWORK_RECV_STATUS_OKAY
;
818 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet
*p
)
820 if (this->status
!= STATUS_MAP
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
821 if (this->savegame
== NULL
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
823 _network_join_status
= NETWORK_JOIN_STATUS_PROCESSING
;
824 SetWindowDirty(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
827 * Make sure everything is set for reading.
829 * We need the local copy and reset this->savegame because when
830 * loading fails the network gets reset upon loading the intro
831 * game, which would cause us to free this->savegame twice.
833 LoadFilter
*lf
= this->savegame
;
834 this->savegame
= NULL
;
837 /* The map is done downloading, load it */
838 ClearErrorMessages();
839 bool load_success
= SafeLoad(NULL
, SLO_LOAD
, DFT_GAME_FILE
, GM_NORMAL
, NO_DIRECTORY
, lf
);
841 /* Long savegame loads shouldn't affect the lag calculation! */
842 this->last_packet
= _realtime_tick
;
845 DeleteWindowById(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
846 ShowErrorMessage(STR_NETWORK_ERROR_SAVEGAMEERROR
, INVALID_STRING_ID
, WL_CRITICAL
);
847 return NETWORK_RECV_STATUS_SAVEGAME
;
849 /* If the savegame has successfully loaded, ALL windows have been removed,
850 * only toolbar/statusbar and gamefield are visible */
852 /* Say we received the map and loaded it correctly! */
855 /* New company/spectator (invalid company) or company we want to join is not active
856 * Switch local company to spectator and await the server's judgement */
857 if (_network_join_as
== COMPANY_NEW_COMPANY
|| !Company::IsValidID(_network_join_as
)) {
858 SetLocalCompany(COMPANY_SPECTATOR
);
860 if (_network_join_as
!= COMPANY_SPECTATOR
) {
861 /* We have arrived and ready to start playing; send a command to make a new company;
862 * the server will give us a client-id and let us in */
863 _network_join_status
= NETWORK_JOIN_STATUS_REGISTERING
;
864 ShowJoinStatusWindow();
865 NetworkSendCommand(0, 0, 0, CMD_COMPANY_CTRL
, NULL
, NULL
, _local_company
);
868 /* take control over an existing company */
869 SetLocalCompany(_network_join_as
);
872 return NETWORK_RECV_STATUS_OKAY
;
875 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_FRAME(Packet
*p
)
877 if (this->status
!= STATUS_ACTIVE
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
879 _frame_counter_server
= p
->Recv_uint32();
880 _frame_counter_max
= p
->Recv_uint32();
881 #ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
882 /* Test if the server supports this option
883 * and if we are at the frame the server is */
884 if (p
->pos
+ 1 < p
->size
) {
885 _sync_frame
= _frame_counter_server
;
886 _sync_seed_1
= p
->Recv_uint32();
887 #ifdef NETWORK_SEND_DOUBLE_SEED
888 _sync_seed_2
= p
->Recv_uint32();
892 /* Receive the token. */
893 if (p
->pos
!= p
->size
) this->token
= p
->Recv_uint8();
895 DEBUG(net
, 5, "Received FRAME %d", _frame_counter_server
);
897 /* Let the server know that we received this frame correctly
898 * We do this only once per day, to save some bandwidth ;) */
899 if (!_network_first_time
&& last_ack_frame
< _frame_counter
) {
900 last_ack_frame
= _frame_counter
+ DAY_TICKS
;
901 DEBUG(net
, 4, "Sent ACK at %d", _frame_counter
);
905 return NETWORK_RECV_STATUS_OKAY
;
908 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_SYNC(Packet
*p
)
910 if (this->status
!= STATUS_ACTIVE
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
912 _sync_frame
= p
->Recv_uint32();
913 _sync_seed_1
= p
->Recv_uint32();
914 #ifdef NETWORK_SEND_DOUBLE_SEED
915 _sync_seed_2
= p
->Recv_uint32();
918 return NETWORK_RECV_STATUS_OKAY
;
921 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_COMMAND(Packet
*p
)
923 if (this->status
!= STATUS_ACTIVE
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
926 const char *err
= this->ReceiveCommand(p
, &cp
);
927 cp
.frame
= p
->Recv_uint32();
928 cp
.my_cmd
= p
->Recv_bool();
931 IConsolePrintF(CC_ERROR
, "WARNING: %s from server, dropping...", err
);
932 return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
935 this->incoming_queue
.Append(&cp
);
937 return NETWORK_RECV_STATUS_OKAY
;
940 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(Packet
*p
)
942 if (this->status
!= STATUS_ACTIVE
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
944 char name
[NETWORK_NAME_LENGTH
], msg
[NETWORK_CHAT_LENGTH
];
945 const NetworkClientInfo
*ci
= NULL
, *ci_to
;
947 NetworkAction action
= (NetworkAction
)p
->Recv_uint8();
948 ClientID client_id
= (ClientID
)p
->Recv_uint32();
949 bool self_send
= p
->Recv_bool();
950 p
->Recv_string(msg
, NETWORK_CHAT_LENGTH
);
951 int64 data
= p
->Recv_uint64();
953 ci_to
= NetworkClientInfo::GetByClientID(client_id
);
954 if (ci_to
== NULL
) return NETWORK_RECV_STATUS_OKAY
;
956 /* Did we initiate the action locally? */
959 case NETWORK_ACTION_CHAT_CLIENT
:
960 /* For speaking to client we need the client-name */
961 seprintf(name
, lastof(name
), "%s", ci_to
->client_name
);
962 ci
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
965 /* For speaking to company or giving money, we need the company-name */
966 case NETWORK_ACTION_GIVE_MONEY
:
967 if (!Company::IsValidID(ci_to
->client_playas
)) return NETWORK_RECV_STATUS_OKAY
;
969 case NETWORK_ACTION_CHAT_COMPANY
: {
970 StringID str
= Company::IsValidID(ci_to
->client_playas
) ? STR_COMPANY_NAME
: STR_NETWORK_SPECTATORS
;
971 SetDParam(0, ci_to
->client_playas
);
973 GetString(name
, str
, lastof(name
));
974 ci
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
978 default: return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
981 /* Display message from somebody else */
982 seprintf(name
, lastof(name
), "%s", ci_to
->client_name
);
987 NetworkTextMessage(action
, GetDrawStringCompanyColour(ci
->client_playas
), self_send
, name
, msg
, data
);
989 return NETWORK_RECV_STATUS_OKAY
;
992 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_ERROR_QUIT(Packet
*p
)
994 if (this->status
< STATUS_AUTHORIZED
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
996 ClientID client_id
= (ClientID
)p
->Recv_uint32();
998 NetworkClientInfo
*ci
= NetworkClientInfo::GetByClientID(client_id
);
1000 NetworkTextMessage(NETWORK_ACTION_LEAVE
, CC_DEFAULT
, false, ci
->client_name
, NULL
, GetNetworkErrorMsg((NetworkErrorCode
)p
->Recv_uint8()));
1004 SetWindowDirty(WC_CLIENT_LIST
, 0);
1006 return NETWORK_RECV_STATUS_OKAY
;
1009 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_QUIT(Packet
*p
)
1011 if (this->status
< STATUS_AUTHORIZED
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
1013 ClientID client_id
= (ClientID
)p
->Recv_uint32();
1015 NetworkClientInfo
*ci
= NetworkClientInfo::GetByClientID(client_id
);
1017 NetworkTextMessage(NETWORK_ACTION_LEAVE
, CC_DEFAULT
, false, ci
->client_name
, NULL
, STR_NETWORK_MESSAGE_CLIENT_LEAVING
);
1020 DEBUG(net
, 0, "Unknown client (%d) is leaving the game", client_id
);
1023 SetWindowDirty(WC_CLIENT_LIST
, 0);
1025 /* If we come here it means we could not locate the client.. strange :s */
1026 return NETWORK_RECV_STATUS_OKAY
;
1029 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_JOIN(Packet
*p
)
1031 if (this->status
< STATUS_AUTHORIZED
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
1033 ClientID client_id
= (ClientID
)p
->Recv_uint32();
1035 NetworkClientInfo
*ci
= NetworkClientInfo::GetByClientID(client_id
);
1037 NetworkTextMessage(NETWORK_ACTION_JOIN
, CC_DEFAULT
, false, ci
->client_name
);
1040 SetWindowDirty(WC_CLIENT_LIST
, 0);
1042 return NETWORK_RECV_STATUS_OKAY
;
1045 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_SHUTDOWN(Packet
*p
)
1047 /* Only when we're trying to join we really
1048 * care about the server shutting down. */
1049 if (this->status
>= STATUS_JOIN
) {
1050 ShowErrorMessage(STR_NETWORK_MESSAGE_SERVER_SHUTDOWN
, INVALID_STRING_ID
, WL_CRITICAL
);
1053 return NETWORK_RECV_STATUS_SERVER_ERROR
;
1056 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_NEWGAME(Packet
*p
)
1058 /* Only when we're trying to join we really
1059 * care about the server shutting down. */
1060 if (this->status
>= STATUS_JOIN
) {
1061 /* To throttle the reconnects a bit, every clients waits its
1062 * Client ID modulo 16. This way reconnects should be spread
1064 _network_reconnect
= _network_own_client_id
% 16;
1065 ShowErrorMessage(STR_NETWORK_MESSAGE_SERVER_REBOOT
, INVALID_STRING_ID
, WL_CRITICAL
);
1068 return NETWORK_RECV_STATUS_SERVER_ERROR
;
1071 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_RCON(Packet
*p
)
1073 if (this->status
< STATUS_AUTHORIZED
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
1075 TextColour colour_code
= (TextColour
)p
->Recv_uint16();
1076 if (!IsValidConsoleColour(colour_code
)) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
1078 char rcon_out
[NETWORK_RCONCOMMAND_LENGTH
];
1079 p
->Recv_string(rcon_out
, sizeof(rcon_out
));
1081 IConsolePrint(colour_code
, rcon_out
);
1083 return NETWORK_RECV_STATUS_OKAY
;
1086 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_MOVE(Packet
*p
)
1088 if (this->status
< STATUS_AUTHORIZED
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
1090 /* Nothing more in this packet... */
1091 ClientID client_id
= (ClientID
)p
->Recv_uint32();
1092 CompanyID company_id
= (CompanyID
)p
->Recv_uint8();
1094 if (client_id
== 0) {
1095 /* definitely an invalid client id, debug message and do nothing. */
1096 DEBUG(net
, 0, "[move] received invalid client index = 0");
1097 return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
1100 const NetworkClientInfo
*ci
= NetworkClientInfo::GetByClientID(client_id
);
1101 /* Just make sure we do not try to use a client_index that does not exist */
1102 if (ci
== NULL
) return NETWORK_RECV_STATUS_OKAY
;
1104 /* if not valid player, force spectator, else check player exists */
1105 if (!Company::IsValidID(company_id
)) company_id
= COMPANY_SPECTATOR
;
1107 if (client_id
== _network_own_client_id
) {
1108 SetLocalCompany(company_id
);
1111 return NETWORK_RECV_STATUS_OKAY
;
1114 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_CONFIG_UPDATE(Packet
*p
)
1116 if (this->status
< STATUS_ACTIVE
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
1118 _network_server_max_companies
= p
->Recv_uint8();
1119 _network_server_max_spectators
= p
->Recv_uint8();
1121 return NETWORK_RECV_STATUS_OKAY
;
1124 NetworkRecvStatus
ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_UPDATE(Packet
*p
)
1126 if (this->status
< STATUS_ACTIVE
) return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
1128 _network_company_passworded
= p
->Recv_uint16();
1129 SetWindowClassesDirty(WC_COMPANY
);
1131 return NETWORK_RECV_STATUS_OKAY
;
1135 * Check the connection's state, i.e. is the connection still up?
1137 void ClientNetworkGameSocketHandler::CheckConnection()
1139 /* Only once we're authorized we can expect a steady stream of packets. */
1140 if (this->status
< STATUS_AUTHORIZED
) return;
1142 /* It might... sometimes occur that the realtime ticker overflows. */
1143 if (_realtime_tick
< this->last_packet
) this->last_packet
= _realtime_tick
;
1145 /* Lag is in milliseconds; 5 seconds are roughly twice the
1146 * server's "you're slow" threshold (1 game day). */
1147 uint lag
= (_realtime_tick
- this->last_packet
) / 1000;
1148 if (lag
< 5) return;
1150 /* 20 seconds are (way) more than 4 game days after which
1151 * the server will forcefully disconnect you. */
1153 this->NetworkGameSocketHandler::CloseConnection();
1154 ShowErrorMessage(STR_NETWORK_ERROR_LOSTCONNECTION
, INVALID_STRING_ID
, WL_CRITICAL
);
1158 /* Prevent showing the lag message every tick; just update it when needed. */
1159 static uint last_lag
= 0;
1160 if (last_lag
== lag
) return;
1164 ShowErrorMessage(STR_NETWORK_ERROR_CLIENT_GUI_LOST_CONNECTION_CAPTION
, STR_NETWORK_ERROR_CLIENT_GUI_LOST_CONNECTION
, WL_INFO
);
1168 /** Is called after a client is connected to the server */
1169 void NetworkClient_Connected()
1171 /* Set the frame-counter to 0 so nothing happens till we are ready */
1173 _frame_counter_server
= 0;
1175 /* Request the game-info */
1176 MyClient::SendJoin();
1180 * Send a remote console command.
1181 * @param password The password.
1182 * @param command The command to execute.
1184 void NetworkClientSendRcon(const char *password
, const char *command
)
1186 MyClient::SendRCon(password
, command
);
1190 * Notify the server of this client wanting to be moved to another company.
1191 * @param company_id id of the company the client wishes to be moved to.
1192 * @param pass the password, is only checked on the server end if a password is needed.
1195 void NetworkClientRequestMove(CompanyID company_id
, const char *pass
)
1197 MyClient::SendMove(company_id
, pass
);
1201 * Move the clients of a company to the spectators.
1202 * @param cid The company to move the clients of.
1204 void NetworkClientsToSpectators(CompanyID cid
)
1206 Backup
<CompanyByte
> cur_company(_current_company
, FILE_LINE
);
1207 /* If our company is changing owner, go to spectators */
1208 if (cid
== _local_company
) SetLocalCompany(COMPANY_SPECTATOR
);
1210 NetworkClientInfo
*ci
;
1211 FOR_ALL_CLIENT_INFOS(ci
) {
1212 if (ci
->client_playas
!= cid
) continue;
1213 NetworkTextMessage(NETWORK_ACTION_COMPANY_SPECTATOR
, CC_DEFAULT
, false, ci
->client_name
);
1214 ci
->client_playas
= COMPANY_SPECTATOR
;
1217 cur_company
.Restore();
1221 * Send the server our name.
1223 void NetworkUpdateClientName()
1225 NetworkClientInfo
*ci
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
1227 if (ci
== NULL
) return;
1229 /* Don't change the name if it is the same as the old name */
1230 if (strcmp(ci
->client_name
, _settings_client
.network
.client_name
) != 0) {
1231 if (!_network_server
) {
1232 MyClient::SendSetName(_settings_client
.network
.client_name
);
1234 if (NetworkFindName(_settings_client
.network
.client_name
, lastof(_settings_client
.network
.client_name
))) {
1235 NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE
, CC_DEFAULT
, false, ci
->client_name
, _settings_client
.network
.client_name
);
1236 strecpy(ci
->client_name
, _settings_client
.network
.client_name
, lastof(ci
->client_name
));
1237 NetworkUpdateClientInfo(CLIENT_ID_SERVER
);
1244 * Send a chat message.
1245 * @param action The action associated with the message.
1246 * @param type The destination type.
1247 * @param dest The destination index, be it a company index or client id.
1248 * @param msg The actual message.
1249 * @param data Arbitrary extra data.
1251 void NetworkClientSendChat(NetworkAction action
, DestType type
, int dest
, const char *msg
, int64 data
)
1253 MyClient::SendChat(action
, type
, dest
, msg
, data
);
1257 * Set/Reset company password on the client side.
1258 * @param password Password to be set.
1260 void NetworkClientSetCompanyPassword(const char *password
)
1262 MyClient::SendSetPassword(password
);
1266 * Tell whether the client has team members where he/she can chat to.
1267 * @param cio client to check members of.
1268 * @return true if there is at least one team member.
1270 bool NetworkClientPreferTeamChat(const NetworkClientInfo
*cio
)
1272 /* Only companies actually playing can speak to team. Eg spectators cannot */
1273 if (!_settings_client
.gui
.prefer_teamchat
|| !Company::IsValidID(cio
->client_playas
)) return false;
1275 const NetworkClientInfo
*ci
;
1276 FOR_ALL_CLIENT_INFOS(ci
) {
1277 if (ci
->client_playas
== cio
->client_playas
&& ci
!= cio
) return true;
1284 * Check if max_companies has been reached on the server (local check only).
1285 * @return true if the max value has been reached or exceeded, false otherwise.
1287 bool NetworkMaxCompaniesReached()
1289 return Company::GetNumItems() >= (_network_server
? _settings_client
.network
.max_companies
: _network_server_max_companies
);
1293 * Check if max_spectatos has been reached on the server (local check only).
1294 * @return true if the max value has been reached or exceeded, false otherwise.
1296 bool NetworkMaxSpectatorsReached()
1298 return NetworkSpectatorCount() >= (_network_server
? _settings_client
.network
.max_spectators
: _network_server_max_spectators
);
1301 #endif /* ENABLE_NETWORK */