OpenTTD: src/network/network_client.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

network_client.cpp
Go to the documentation of this file.
1 /* $Id: network_client.cpp 27893 2017-08-13 18:38:42Z frosch $ */
2
3 /*
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/>.
8 */
9
12 #ifdef ENABLE_NETWORK
13
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"
28 #include "../error.h"
29 #include "../rev.h"
30 #include "network.h"
31 #include "network_base.h"
32 #include "network_client.h"
33 #include "../core/backup_type.hpp"
34
35 #include "table/strings.h"
36
37 #include "../safeguards.h"
38
39 /* This file handles all the client-commands */
40
41
43 struct PacketReader : LoadFilter {
44 static const size_t CHUNK = 32 * 1024;
45
46 AutoFreeSmallVector<byte *, 16> blocks;
47 byte *buf;
48 byte *bufe;
49 byte **block;
50 size_t written_bytes;
51 size_t read_bytes;
52
54 PacketReader() : LoadFilter(NULL), buf(NULL), bufe(NULL), block(NULL), written_bytes(0), read_bytes(0)
55 {
56 }
57
62 void AddPacket(const Packet *p)
63 {
64 assert(this->read_bytes == 0);
65
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;
69
70 this->written_bytes += in_packet;
71 if (to_write != 0) {
72 memcpy(this->buf, pbuf, to_write);
73 this->buf += to_write;
74 }
75
76 /* Did everything fit in the current chunk, then we're done. */
77 if (to_write == in_packet) return;
78
79 /* Allocate a new chunk and add the remaining data. */
80 pbuf += to_write;
81 to_write = in_packet - to_write;
82 this->buf = *this->blocks.Append() = CallocT<byte>(CHUNK);
83 this->bufe = this->buf + CHUNK;
84
85 memcpy(this->buf, pbuf, to_write);
86 this->buf += to_write;
87 }
88
89 /* virtual */ size_t Read(byte *rbuf, size_t size)
90 {
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;
95
96 while (rbuf != rbufe) {
97 if (this->buf == this->bufe) {
98 this->buf = *this->block++;
99 this->bufe = this->buf + CHUNK;
100 }
101
102 size_t to_write = min(this->bufe - this->buf, rbufe - rbuf);
103 memcpy(rbuf, this->buf, to_write);
104 rbuf += to_write;
105 this->buf += to_write;
106 }
107
108 return ret_size;
109 }
110
111 /* virtual */ void Reset()
112 {
113 this->read_bytes = 0;
114
115 this->block = this->blocks.Begin();
116 this->buf = *this->block++;
117 this->bufe = this->buf + CHUNK;
118 }
119 };
120
121
126 ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s), savegame(NULL), status(STATUS_INACTIVE)
127 {
128 assert(ClientNetworkGameSocketHandler::my_client == NULL);
129 ClientNetworkGameSocketHandler::my_client = this;
130 }
131
133 ClientNetworkGameSocketHandler::~ClientNetworkGameSocketHandler()
134 {
135 assert(ClientNetworkGameSocketHandler::my_client == this);
136 ClientNetworkGameSocketHandler::my_client = NULL;
137
138 delete this->savegame;
139 }
140
141 NetworkRecvStatus ClientNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status)
142 {
143 assert(status != NETWORK_RECV_STATUS_OKAY);
144 /*
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.
150 */
151 if (this->sock == INVALID_SOCKET) return status;
152
153 DEBUG(net, 1, "Closed client connection %d", this->client_id);
154
155 this->SendPackets(true);
156
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);
162
163 delete this->GetInfo();
164 delete this;
165
166 return status;
167 }
168
173 void ClientNetworkGameSocketHandler::ClientError(NetworkRecvStatus res)
174 {
175 /* First, send a CLIENT_ERROR to the server, so he knows we are
176 * disconnection (and why!) */
177 NetworkErrorCode errorno;
178
179 /* We just want to close the connection.. */
180 if (res == NETWORK_RECV_STATUS_CLOSE_QUERY) {
181 this->NetworkSocketHandler::CloseConnection();
182 this->CloseConnection(res);
183 _networking = false;
184
185 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
186 return;
187 }
188
189 switch (res) {
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;
194 }
195
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) {
199 SendError(errorno);
200 }
201
202 _switch_mode = SM_MENU;
203 this->CloseConnection(res);
204 _networking = false;
205 }
206
207
213 /* static */ bool ClientNetworkGameSocketHandler::Receive()
214 {
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);
221 return false;
222 }
223 }
224 return _networking;
225 }
226
228 /* static */ void ClientNetworkGameSocketHandler::Send()
229 {
230 my_client->SendPackets();
231 my_client->CheckConnection();
232 }
233
238 /* static */ bool ClientNetworkGameSocketHandler::GameLoop()
239 {
240 _frame_counter++;
241
242 NetworkExecuteLocalCommandQueue();
243
244 extern void StateGameLoop();
245 StateGameLoop();
246
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]) {
252 #else
253 if (_sync_seed_1 != _random.state[0]) {
254 #endif
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);
259 return false;
260 }
261
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;
267 SendAck();
268 }
269
270 _sync_frame = 0;
271 } else if (_sync_frame < _frame_counter) {
272 DEBUG(net, 1, "Missed frame for sync-test (%d / %d)", _sync_frame, _frame_counter);
273 _sync_frame = 0;
274 }
275 }
276
277 return true;
278 }
279
280
282 ClientNetworkGameSocketHandler * ClientNetworkGameSocketHandler::my_client = NULL;
283
285 static uint32 last_ack_frame;
286
288 static uint32 _password_game_seed;
290 static char _password_server_id[NETWORK_SERVER_ID_LENGTH];
291
293 static uint8 _network_server_max_companies;
295 static uint8 _network_server_max_spectators;
296
298 CompanyID _network_join_as;
299
301 const char *_network_join_server_password = NULL;
303 const char *_network_join_company_password = NULL;
304
306 assert_compile(NETWORK_SERVER_ID_LENGTH == 16 * 2 + 1);
307
308 /***********
309 * Sending functions
310 * DEF_CLIENT_SEND_COMMAND has no parameters
311 ************/
312
314 NetworkRecvStatus ClientNetworkGameSocketHandler::SendCompanyInformationQuery()
315 {
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);
319
320 Packet *p = new Packet(PACKET_CLIENT_COMPANY_INFO);
321 my_client->SendPacket(p);
322 return NETWORK_RECV_STATUS_OKAY;
323 }
324
326 NetworkRecvStatus ClientNetworkGameSocketHandler::SendJoin()
327 {
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);
331
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;
340 }
341
343 NetworkRecvStatus ClientNetworkGameSocketHandler::SendNewGRFsOk()
344 {
345 Packet *p = new Packet(PACKET_CLIENT_NEWGRFS_CHECKED);
346 my_client->SendPacket(p);
347 return NETWORK_RECV_STATUS_OKAY;
348 }
349
354 NetworkRecvStatus ClientNetworkGameSocketHandler::SendGamePassword(const char *password)
355 {
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;
360 }
361
366 NetworkRecvStatus ClientNetworkGameSocketHandler::SendCompanyPassword(const char *password)
367 {
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;
372 }
373
375 NetworkRecvStatus ClientNetworkGameSocketHandler::SendGetMap()
376 {
377 my_client->status = STATUS_MAP_WAIT;
378
379 Packet *p = new Packet(PACKET_CLIENT_GETMAP);
380 my_client->SendPacket(p);
381 return NETWORK_RECV_STATUS_OKAY;
382 }
383
385 NetworkRecvStatus ClientNetworkGameSocketHandler::SendMapOk()
386 {
387 my_client->status = STATUS_ACTIVE;
388
389 Packet *p = new Packet(PACKET_CLIENT_MAP_OK);
390 my_client->SendPacket(p);
391 return NETWORK_RECV_STATUS_OKAY;
392 }
393
395 NetworkRecvStatus ClientNetworkGameSocketHandler::SendAck()
396 {
397 Packet *p = new Packet(PACKET_CLIENT_ACK);
398
399 p->Send_uint32(_frame_counter);
400 p->Send_uint8 (my_client->token);
401 my_client->SendPacket(p);
402 return NETWORK_RECV_STATUS_OKAY;
403 }
404
409 NetworkRecvStatus ClientNetworkGameSocketHandler::SendCommand(const CommandPacket *cp)
410 {
411 Packet *p = new Packet(PACKET_CLIENT_COMMAND);
412 my_client->NetworkGameSocketHandler::SendCommand(p, cp);
413
414 my_client->SendPacket(p);
415 return NETWORK_RECV_STATUS_OKAY;
416 }
417
419 NetworkRecvStatus ClientNetworkGameSocketHandler::SendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data)
420 {
421 Packet *p = new Packet(PACKET_CLIENT_CHAT);
422
423 p->Send_uint8 (action);
424 p->Send_uint8 (type);
425 p->Send_uint32(dest);
426 p->Send_string(msg);
427 p->Send_uint64(data);
428
429 my_client->SendPacket(p);
430 return NETWORK_RECV_STATUS_OKAY;
431 }
432
434 NetworkRecvStatus ClientNetworkGameSocketHandler::SendError(NetworkErrorCode errorno)
435 {
436 Packet *p = new Packet(PACKET_CLIENT_ERROR);
437
438 p->Send_uint8(errorno);
439 my_client->SendPacket(p);
440 return NETWORK_RECV_STATUS_OKAY;
441 }
442
447 NetworkRecvStatus ClientNetworkGameSocketHandler::SendSetPassword(const char *password)
448 {
449 Packet *p = new Packet(PACKET_CLIENT_SET_PASSWORD);
450
451 p->Send_string(GenerateCompanyPasswordHash(password, _password_server_id, _password_game_seed));
452 my_client->SendPacket(p);
453 return NETWORK_RECV_STATUS_OKAY;
454 }
455
460 NetworkRecvStatus ClientNetworkGameSocketHandler::SendSetName(const char *name)
461 {
462 Packet *p = new Packet(PACKET_CLIENT_SET_NAME);
463
464 p->Send_string(name);
465 my_client->SendPacket(p);
466 return NETWORK_RECV_STATUS_OKAY;
467 }
468
472 NetworkRecvStatus ClientNetworkGameSocketHandler::SendQuit()
473 {
474 Packet *p = new Packet(PACKET_CLIENT_QUIT);
475
476 my_client->SendPacket(p);
477 return NETWORK_RECV_STATUS_OKAY;
478 }
479
485 NetworkRecvStatus ClientNetworkGameSocketHandler::SendRCon(const char *pass, const char *command)
486 {
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;
492 }
493
499 NetworkRecvStatus ClientNetworkGameSocketHandler::SendMove(CompanyID company, const char *password)
500 {
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;
506 }
507
512 bool ClientNetworkGameSocketHandler::IsConnected()
513 {
514 return my_client != NULL && my_client->status == STATUS_ACTIVE;
515 }
516
517
518 /***********
519 * Receiving functions
520 * DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p
521 ************/
522
523 extern bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL);
524
525 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_FULL(Packet *p)
526 {
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);
530
531 return NETWORK_RECV_STATUS_SERVER_FULL;
532 }
533
534 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_BANNED(Packet *p)
535 {
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);
539
540 return NETWORK_RECV_STATUS_SERVER_BANNED;
541 }
542
543 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO(Packet *p)
544 {
545 if (this->status != STATUS_COMPANY_INFO) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
546
547 byte company_info_version = p->Recv_uint8();
548
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;
552
553 CompanyID current = (Owner)p->Recv_uint8();
554 if (current >= MAX_COMPANIES) return NETWORK_RECV_STATUS_CLOSE_QUERY;
555
556 NetworkCompanyInfo *company_info = GetLobbyCompanyInfo(current);
557 if (company_info == NULL) return NETWORK_RECV_STATUS_CLOSE_QUERY;
558
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();
568 }
569 for (uint i = 0; i < NETWORK_VEH_END; i++) {
570 company_info->num_station[i] = p->Recv_uint16();
571 }
572 company_info->ai = p->Recv_bool();
573
574 p->Recv_string(company_info->clients, sizeof(company_info->clients));
575
576 SetWindowDirty(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY);
577
578 return NETWORK_RECV_STATUS_OKAY;
579 }
580
581 return NETWORK_RECV_STATUS_CLOSE_QUERY;
582 }
583
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)
588 {
589 NetworkClientInfo *ci;
590 ClientID client_id = (ClientID)p->Recv_uint32();
591 CompanyID playas = (CompanyID)p->Recv_uint8();
592 char name[NETWORK_NAME_LENGTH];
593
594 p->Recv_string(name, sizeof(name));
595
596 if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
597 if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
598
599 ci = NetworkClientInfo::GetByClientID(client_id);
600 if (ci != NULL) {
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 */
607 }
608
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);
612
613 ci->client_playas = playas;
614 strecpy(ci->client_name, name, lastof(ci->client_name));
615
616 SetWindowDirty(WC_CLIENT_LIST, 0);
617
618 return NETWORK_RECV_STATUS_OKAY;
619 }
620
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;
627
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);
632
633 strecpy(ci->client_name, name, lastof(ci->client_name));
634
635 SetWindowDirty(WC_CLIENT_LIST, 0);
636
637 return NETWORK_RECV_STATUS_OKAY;
638 }
639
640 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_ERROR(Packet *p)
641 {
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
663 };
664 assert_compile(lengthof(network_error_strings) == NETWORK_ERROR_END);
665
666 NetworkErrorCode error = (NetworkErrorCode)p->Recv_uint8();
667
668 StringID err = STR_NETWORK_ERROR_LOSTCONNECTION;
669 if (error < (ptrdiff_t)lengthof(network_error_strings)) err = network_error_strings[error];
670
671 ShowErrorMessage(err, INVALID_STRING_ID, WL_CRITICAL);
672
673 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
674
675 return NETWORK_RECV_STATUS_SERVER_ERROR;
676 }
677
678 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS(Packet *p)
679 {
680 if (this->status != STATUS_JOIN) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
681
682 uint grf_count = p->Recv_uint8();
683 NetworkRecvStatus ret = NETWORK_RECV_STATUS_OKAY;
684
685 /* Check all GRFs */
686 for (; grf_count > 0; grf_count--) {
687 GRFIdentifier c;
688 this->ReceiveGRFIdentifier(p, &c);
689
690 /* Check whether we know this GRF */
691 const GRFConfig *f = FindGRFConfig(c.grfid, FGCM_EXACT, c.md5sum);
692 if (f == NULL) {
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;
698 }
699 }
700
701 if (ret == NETWORK_RECV_STATUS_OKAY) {
702 /* Start receiving the map */
703 return SendNewGRFsOk();
704 }
705
706 /* NewGRF mismatch, bail out */
707 ShowErrorMessage(STR_NETWORK_ERROR_NEWGRF_MISMATCH, INVALID_STRING_ID, WL_CRITICAL);
708 return ret;
709 }
710
711 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_NEED_GAME_PASSWORD(Packet *p)
712 {
713 if (this->status < STATUS_JOIN || this->status >= STATUS_AUTH_GAME) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
714 this->status = STATUS_AUTH_GAME;
715
716 const char *password = _network_join_server_password;
717 if (!StrEmpty(password)) {
718 return SendGamePassword(password);
719 }
720
721 ShowNetworkNeedPassword(NETWORK_GAME_PASSWORD);
722
723 return NETWORK_RECV_STATUS_OKAY;
724 }
725
726 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p)
727 {
728 if (this->status < STATUS_JOIN || this->status >= STATUS_AUTH_COMPANY) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
729 this->status = STATUS_AUTH_COMPANY;
730
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;
734
735 const char *password = _network_join_company_password;
736 if (!StrEmpty(password)) {
737 return SendCompanyPassword(password);
738 }
739
740 ShowNetworkNeedPassword(NETWORK_COMPANY_PASSWORD);
741
742 return NETWORK_RECV_STATUS_OKAY;
743 }
744
745 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_WELCOME(Packet *p)
746 {
747 if (this->status < STATUS_JOIN || this->status >= STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
748 this->status = STATUS_AUTHORIZED;
749
750 _network_own_client_id = (ClientID)p->Recv_uint32();
751
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));
755
756 /* Start receiving the map */
757 return SendGetMap();
758 }
759
760 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_WAIT(Packet *p)
761 {
762 /* We set the internal wait state when requesting the map. */
763 if (this->status != STATUS_MAP_WAIT) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
764
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);
769
770 return NETWORK_RECV_STATUS_OKAY;
771 }
772
773 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_BEGIN(Packet *p)
774 {
775 if (this->status < STATUS_AUTHORIZED || this->status >= STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
776 this->status = STATUS_MAP;
777
778 if (this->savegame != NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
779
780 this->savegame = new PacketReader();
781
782 _frame_counter = _frame_counter_server = _frame_counter_max = p->Recv_uint32();
783
784 _network_join_bytes = 0;
785 _network_join_bytes_total = 0;
786
787 _network_join_status = NETWORK_JOIN_STATUS_DOWNLOADING;
788 SetWindowDirty(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
789
790 return NETWORK_RECV_STATUS_OKAY;
791 }
792
793 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_SIZE(Packet *p)
794 {
795 if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
796 if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
797
798 _network_join_bytes_total = p->Recv_uint32();
799 SetWindowDirty(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
800
801 return NETWORK_RECV_STATUS_OKAY;
802 }
803
804 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DATA(Packet *p)
805 {
806 if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
807 if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
808
809 /* We are still receiving data, put it to the file */
810 this->savegame->AddPacket(p);
811
812 _network_join_bytes = (uint32)this->savegame->written_bytes;
813 SetWindowDirty(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
814
815 return NETWORK_RECV_STATUS_OKAY;
816 }
817
818 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet *p)
819 {
820 if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
821 if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
822
823 _network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
824 SetWindowDirty(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
825
826 /*
827 * Make sure everything is set for reading.
828 *
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.
832 */
833 LoadFilter *lf = this->savegame;
834 this->savegame = NULL;
835 lf->Reset();
836
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);
840
841 /* Long savegame loads shouldn't affect the lag calculation! */
842 this->last_packet = _realtime_tick;
843
844 if (!load_success) {
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;
848 }
849 /* If the savegame has successfully loaded, ALL windows have been removed,
850 * only toolbar/statusbar and gamefield are visible */
851
852 /* Say we received the map and loaded it correctly! */
853 SendMapOk();
854
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);
859
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);
866 }
867 } else {
868 /* take control over an existing company */
869 SetLocalCompany(_network_join_as);
870 }
871
872 return NETWORK_RECV_STATUS_OKAY;
873 }
874
875 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_FRAME(Packet *p)
876 {
877 if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
878
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();
889 #endif
890 }
891 #endif
892 /* Receive the token. */
893 if (p->pos != p->size) this->token = p->Recv_uint8();
894
895 DEBUG(net, 5, "Received FRAME %d", _frame_counter_server);
896
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);
902 SendAck();
903 }
904
905 return NETWORK_RECV_STATUS_OKAY;
906 }
907
908 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_SYNC(Packet *p)
909 {
910 if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
911
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();
916 #endif
917
918 return NETWORK_RECV_STATUS_OKAY;
919 }
920
921 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_COMMAND(Packet *p)
922 {
923 if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
924
925 CommandPacket cp;
926 const char *err = this->ReceiveCommand(p, &cp);
927 cp.frame = p->Recv_uint32();
928 cp.my_cmd = p->Recv_bool();
929
930 if (err != NULL) {
931 IConsolePrintF(CC_ERROR, "WARNING: %s from server, dropping...", err);
932 return NETWORK_RECV_STATUS_MALFORMED_PACKET;
933 }
934
935 this->incoming_queue.Append(&cp);
936
937 return NETWORK_RECV_STATUS_OKAY;
938 }
939
940 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(Packet *p)
941 {
942 if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
943
944 char name[NETWORK_NAME_LENGTH], msg[NETWORK_CHAT_LENGTH];
945 const NetworkClientInfo *ci = NULL, *ci_to;
946
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();
952
953 ci_to = NetworkClientInfo::GetByClientID(client_id);
954 if (ci_to == NULL) return NETWORK_RECV_STATUS_OKAY;
955
956 /* Did we initiate the action locally? */
957 if (self_send) {
958 switch (action) {
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);
963 break;
964
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;
968 FALLTHROUGH;
969
970 case NETWORK_ACTION_CHAT_COMPANY: {
971 StringID str = Company::IsValidID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
972 SetDParam(0, ci_to->client_playas);
973
974 GetString(name, str, lastof(name));
975 ci = NetworkClientInfo::GetByClientID(_network_own_client_id);
976 break;
977 }
978
979 default: return NETWORK_RECV_STATUS_MALFORMED_PACKET;
980 }
981 } else {
982 /* Display message from somebody else */
983 seprintf(name, lastof(name), "%s", ci_to->client_name);
984 ci = ci_to;
985 }
986
987 if (ci != NULL) {
988 NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), self_send, name, msg, data);
989 }
990 return NETWORK_RECV_STATUS_OKAY;
991 }
992
993 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_ERROR_QUIT(Packet *p)
994 {
995 if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
996
997 ClientID client_id = (ClientID)p->Recv_uint32();
998
999 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
1000 if (ci != NULL) {
1001 NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, GetNetworkErrorMsg((NetworkErrorCode)p->Recv_uint8()));
1002 delete ci;
1003 }
1004
1005 SetWindowDirty(WC_CLIENT_LIST, 0);
1006
1007 return NETWORK_RECV_STATUS_OKAY;
1008 }
1009
1010 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_QUIT(Packet *p)
1011 {
1012 if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
1013
1014 ClientID client_id = (ClientID)p->Recv_uint32();
1015
1016 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
1017 if (ci != NULL) {
1018 NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, STR_NETWORK_MESSAGE_CLIENT_LEAVING);
1019 delete ci;
1020 } else {
1021 DEBUG(net, 0, "Unknown client (%d) is leaving the game", client_id);
1022 }
1023
1024 SetWindowDirty(WC_CLIENT_LIST, 0);
1025
1026 /* If we come here it means we could not locate the client.. strange :s */
1027 return NETWORK_RECV_STATUS_OKAY;
1028 }
1029
1030 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_JOIN(Packet *p)
1031 {
1032 if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
1033
1034 ClientID client_id = (ClientID)p->Recv_uint32();
1035
1036 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
1037 if (ci != NULL) {
1038 NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, ci->client_name);
1039 }
1040
1041 SetWindowDirty(WC_CLIENT_LIST, 0);
1042
1043 return NETWORK_RECV_STATUS_OKAY;
1044 }
1045
1046 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_SHUTDOWN(Packet *p)
1047 {
1048 /* Only when we're trying to join we really
1049 * care about the server shutting down. */
1050 if (this->status >= STATUS_JOIN) {
1051 ShowErrorMessage(STR_NETWORK_MESSAGE_SERVER_SHUTDOWN, INVALID_STRING_ID, WL_CRITICAL);
1052 }
1053
1054 return NETWORK_RECV_STATUS_SERVER_ERROR;
1055 }
1056
1057 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_NEWGAME(Packet *p)
1058 {
1059 /* Only when we're trying to join we really
1060 * care about the server shutting down. */
1061 if (this->status >= STATUS_JOIN) {
1062 /* To throttle the reconnects a bit, every clients waits its
1063 * Client ID modulo 16. This way reconnects should be spread
1064 * out a bit. */
1065 _network_reconnect = _network_own_client_id % 16;
1066 ShowErrorMessage(STR_NETWORK_MESSAGE_SERVER_REBOOT, INVALID_STRING_ID, WL_CRITICAL);
1067 }
1068
1069 return NETWORK_RECV_STATUS_SERVER_ERROR;
1070 }
1071
1072 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_RCON(Packet *p)
1073 {
1074 if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
1075
1076 TextColour colour_code = (TextColour)p->Recv_uint16();
1077 if (!IsValidConsoleColour(colour_code)) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
1078
1079 char rcon_out[NETWORK_RCONCOMMAND_LENGTH];
1080 p->Recv_string(rcon_out, sizeof(rcon_out));
1081
1082 IConsolePrint(colour_code, rcon_out);
1083
1084 return NETWORK_RECV_STATUS_OKAY;
1085 }
1086
1087 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MOVE(Packet *p)
1088 {
1089 if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
1090
1091 /* Nothing more in this packet... */
1092 ClientID client_id = (ClientID)p->Recv_uint32();
1093 CompanyID company_id = (CompanyID)p->Recv_uint8();
1094
1095 if (client_id == 0) {
1096 /* definitely an invalid client id, debug message and do nothing. */
1097 DEBUG(net, 0, "[move] received invalid client index = 0");
1098 return NETWORK_RECV_STATUS_MALFORMED_PACKET;
1099 }
1100
1101 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
1102 /* Just make sure we do not try to use a client_index that does not exist */
1103 if (ci == NULL) return NETWORK_RECV_STATUS_OKAY;
1104
1105 /* if not valid player, force spectator, else check player exists */
1106 if (!Company::IsValidID(company_id)) company_id = COMPANY_SPECTATOR;
1107
1108 if (client_id == _network_own_client_id) {
1109 SetLocalCompany(company_id);
1110 }
1111
1112 return NETWORK_RECV_STATUS_OKAY;
1113 }
1114
1115 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CONFIG_UPDATE(Packet *p)
1116 {
1117 if (this->status < STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
1118
1119 _network_server_max_companies = p->Recv_uint8();
1120 _network_server_max_spectators = p->Recv_uint8();
1121
1122 return NETWORK_RECV_STATUS_OKAY;
1123 }
1124
1125 NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_UPDATE(Packet *p)
1126 {
1127 if (this->status < STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
1128
1129 _network_company_passworded = p->Recv_uint16();
1130 SetWindowClassesDirty(WC_COMPANY);
1131
1132 return NETWORK_RECV_STATUS_OKAY;
1133 }
1134
1138 void ClientNetworkGameSocketHandler::CheckConnection()
1139 {
1140 /* Only once we're authorized we can expect a steady stream of packets. */
1141 if (this->status < STATUS_AUTHORIZED) return;
1142
1143 /* It might... sometimes occur that the realtime ticker overflows. */
1144 if (_realtime_tick < this->last_packet) this->last_packet = _realtime_tick;
1145
1146 /* Lag is in milliseconds; 5 seconds are roughly twice the
1147 * server's "you're slow" threshold (1 game day). */
1148 uint lag = (_realtime_tick - this->last_packet) / 1000;
1149 if (lag < 5) return;
1150
1151 /* 20 seconds are (way) more than 4 game days after which
1152 * the server will forcefully disconnect you. */
1153 if (lag > 20) {
1154 this->NetworkGameSocketHandler::CloseConnection();
1155 ShowErrorMessage(STR_NETWORK_ERROR_LOSTCONNECTION, INVALID_STRING_ID, WL_CRITICAL);
1156 return;
1157 }
1158
1159 /* Prevent showing the lag message every tick; just update it when needed. */
1160 static uint last_lag = 0;
1161 if (last_lag == lag) return;
1162
1163 last_lag = lag;
1164 SetDParam(0, lag);
1165 ShowErrorMessage(STR_NETWORK_ERROR_CLIENT_GUI_LOST_CONNECTION_CAPTION, STR_NETWORK_ERROR_CLIENT_GUI_LOST_CONNECTION, WL_INFO);
1166 }
1167
1168
1170 void NetworkClient_Connected()
1171 {
1172 /* Set the frame-counter to 0 so nothing happens till we are ready */
1173 _frame_counter = 0;
1174 _frame_counter_server = 0;
1175 last_ack_frame = 0;
1176 /* Request the game-info */
1177 MyClient::SendJoin();
1178 }
1179
1185 void NetworkClientSendRcon(const char *password, const char *command)
1186 {
1187 MyClient::SendRCon(password, command);
1188 }
1189
1196 void NetworkClientRequestMove(CompanyID company_id, const char *pass)
1197 {
1198 MyClient::SendMove(company_id, pass);
1199 }
1200
1205 void NetworkClientsToSpectators(CompanyID cid)
1206 {
1207 Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
1208 /* If our company is changing owner, go to spectators */
1209 if (cid == _local_company) SetLocalCompany(COMPANY_SPECTATOR);
1210
1211 NetworkClientInfo *ci;
1212 FOR_ALL_CLIENT_INFOS(ci) {
1213 if (ci->client_playas != cid) continue;
1214 NetworkTextMessage(NETWORK_ACTION_COMPANY_SPECTATOR, CC_DEFAULT, false, ci->client_name);
1215 ci->client_playas = COMPANY_SPECTATOR;
1216 }
1217
1218 cur_company.Restore();
1219 }
1220
1224 void NetworkUpdateClientName()
1225 {
1226 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(_network_own_client_id);
1227
1228 if (ci == NULL) return;
1229
1230 /* Don't change the name if it is the same as the old name */
1231 if (strcmp(ci->client_name, _settings_client.network.client_name) != 0) {
1232 if (!_network_server) {
1233 MyClient::SendSetName(_settings_client.network.client_name);
1234 } else {
1235 if (NetworkFindName(_settings_client.network.client_name, lastof(_settings_client.network.client_name))) {
1236 NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, _settings_client.network.client_name);
1237 strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
1238 NetworkUpdateClientInfo(CLIENT_ID_SERVER);
1239 }
1240 }
1241 }
1242 }
1243
1252 void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data)
1253 {
1254 MyClient::SendChat(action, type, dest, msg, data);
1255 }
1256
1261 void NetworkClientSetCompanyPassword(const char *password)
1262 {
1263 MyClient::SendSetPassword(password);
1264 }
1265
1271 bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio)
1272 {
1273 /* Only companies actually playing can speak to team. Eg spectators cannot */
1274 if (!_settings_client.gui.prefer_teamchat || !Company::IsValidID(cio->client_playas)) return false;
1275
1276 const NetworkClientInfo *ci;
1277 FOR_ALL_CLIENT_INFOS(ci) {
1278 if (ci->client_playas == cio->client_playas && ci != cio) return true;
1279 }
1280
1281 return false;
1282 }
1283
1288 bool NetworkMaxCompaniesReached()
1289 {
1290 return Company::GetNumItems() >= (_network_server ? _settings_client.network.max_companies : _network_server_max_companies);
1291 }
1292
1297 bool NetworkMaxSpectatorsReached()
1298 {
1299 return NetworkSpectatorCount() >= (_network_server ? _settings_client.network.max_spectators : _network_server_max_spectators);
1300 }
1301
1302 #endif /* ENABLE_NETWORK */