OpenTTD Source: src/network/network_server.cpp Source File

OpenTTD Source 12.0-beta1

network_server.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "../stdafx.h"
11 #include "../strings_func.h"
12 #include "../date_func.h"
13 #include "core/game_info.h"
14 #include "network_admin.h"
15 #include "network_server.h"
16 #include "network_udp.h"
17 #include "network_base.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 "../roadveh.h"
28 #include "../order_backup.h"
29 #include "../core/pool_func.hpp"
30 #include "../core/random_func.hpp"
31 #include "../rev.h"
32 #include <mutex>
33 #include <condition_variable>
34
35 #include "../safeguards.h"
36
37
38 /* This file handles all the server-commands */
39
40 DECLARE_POSTFIX_INCREMENT(ClientID)
42 static ClientID _network_client_id = CLIENT_ID_FIRST;
43
45 static_assert(MAX_CLIENT_SLOTS > MAX_CLIENTS);
47 static_assert(NetworkClientSocketPool::MAX_SIZE == MAX_CLIENT_SLOTS);
48
50 NetworkClientSocketPool _networkclientsocket_pool("NetworkClientSocket");
51 INSTANTIATE_POOL_METHODS(NetworkClientSocket)
52
54 template SocketList TCPListenHandler<ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED>::sockets;
55
57 struct PacketWriter : SaveFilter {
58 ServerNetworkGameSocketHandler *cs;
59 Packet *current;
60 size_t total_size;
61 Packet *packets;
62 std::mutex mutex;
63 std::condition_variable exit_sig;
64
69 PacketWriter(ServerNetworkGameSocketHandler *cs) : SaveFilter(nullptr), cs(cs), current(nullptr), total_size(0), packets(nullptr)
70 {
71 }
72
74 ~PacketWriter()
75 {
76 std::unique_lock<std::mutex> lock(this->mutex);
77
78 if (this->cs != nullptr) this->exit_sig.wait(lock);
79
80 /* This must all wait until the Destroy function is called. */
81
82 while (this->packets != nullptr) {
83 delete Packet::PopFromQueue(&this->packets);
84 }
85
86 delete this->current;
87 }
88
99 void Destroy()
100 {
101 std::unique_lock<std::mutex> lock(this->mutex);
102
103 this->cs = nullptr;
104
105 this->exit_sig.notify_all();
106 lock.unlock();
107
108 /* Make sure the saving is completely cancelled. Yes,
109 * we need to handle the save finish as well as the
110 * next connection might just be requesting a map. */
111 WaitTillSaved();
112 ProcessAsyncSaveFinish();
113 }
114
121 bool TransferToNetworkQueue(ServerNetworkGameSocketHandler *socket)
122 {
123 /* Unsafe check for the queue being empty or not. */
124 if (this->packets == nullptr) return false;
125
126 std::lock_guard<std::mutex> lock(this->mutex);
127
128 while (this->packets != nullptr) {
129 Packet *p = Packet::PopFromQueue(&this->packets);
130 bool last_packet = p->GetPacketType() == PACKET_SERVER_MAP_DONE;
131 socket->SendPacket(p);
132
133 if (last_packet) return true;
134 }
135
136 return false;
137 }
138
140 void AppendQueue()
141 {
142 if (this->current == nullptr) return;
143
144 Packet::AddToQueue(&this->packets, this->current);
145 this->current = nullptr;
146 }
147
149 void PrependQueue()
150 {
151 if (this->current == nullptr) return;
152
153 /* Reversed from AppendQueue so the queue gets added to the current one. */
154 Packet::AddToQueue(&this->current, this->packets);
155 this->packets = this->current;
156 this->current = nullptr;
157 }
158
159 void Write(byte *buf, size_t size) override
160 {
161 /* We want to abort the saving when the socket is closed. */
162 if (this->cs == nullptr) SlError(STR_NETWORK_ERROR_LOSTCONNECTION);
163
164 if (this->current == nullptr) this->current = new Packet(PACKET_SERVER_MAP_DATA, TCP_MTU);
165
166 std::lock_guard<std::mutex> lock(this->mutex);
167
168 byte *bufe = buf + size;
169 while (buf != bufe) {
170 size_t written = this->current->Send_bytes(buf, bufe);
171 buf += written;
172
173 if (!this->current->CanWriteToPacket(1)) {
174 this->AppendQueue();
175 if (buf != bufe) this->current = new Packet(PACKET_SERVER_MAP_DATA, TCP_MTU);
176 }
177 }
178
179 this->total_size += size;
180 }
181
182 void Finish() override
183 {
184 /* We want to abort the saving when the socket is closed. */
185 if (this->cs == nullptr) SlError(STR_NETWORK_ERROR_LOSTCONNECTION);
186
187 std::lock_guard<std::mutex> lock(this->mutex);
188
189 /* Make sure the last packet is flushed. */
190 this->AppendQueue();
191
192 /* Add a packet stating that this is the end to the queue. */
193 this->current = new Packet(PACKET_SERVER_MAP_DONE);
194 this->AppendQueue();
195
196 /* Fast-track the size to the client. */
197 this->current = new Packet(PACKET_SERVER_MAP_SIZE);
198 this->current->Send_uint32((uint32)this->total_size);
199 this->PrependQueue();
200 }
201 };
202
203
208 ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s)
209 {
210 this->status = STATUS_INACTIVE;
211 this->client_id = _network_client_id++;
212 this->receive_limit = _settings_client.network.bytes_per_frame_burst;
213
214 /* The Socket and Info pools need to be the same in size. After all,
215 * each Socket will be associated with at most one Info object. As
216 * such if the Socket was allocated the Info object can as well. */
217 static_assert(NetworkClientSocketPool::MAX_SIZE == NetworkClientInfoPool::MAX_SIZE);
218 }
219
223 ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler()
224 {
225 if (_redirect_console_to_client == this->client_id) _redirect_console_to_client = INVALID_CLIENT_ID;
226 OrderBackup::ResetUser(this->client_id);
227
228 if (this->savegame != nullptr) {
229 this->savegame->Destroy();
230 this->savegame = nullptr;
231 }
232 }
233
234 Packet *ServerNetworkGameSocketHandler::ReceivePacket()
235 {
236 /* Only allow receiving when we have some buffer free; this value
237 * can go negative, but eventually it will become positive again. */
238 if (this->receive_limit <= 0) return nullptr;
239
240 /* We can receive a packet, so try that and if needed account for
241 * the amount of received data. */
242 Packet *p = this->NetworkTCPSocketHandler::ReceivePacket();
243 if (p != nullptr) this->receive_limit -= p->Size();
244 return p;
245 }
246
247 NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status)
248 {
249 assert(status != NETWORK_RECV_STATUS_OKAY);
250 /*
251 * Sending a message just before leaving the game calls cs->SendPackets.
252 * This might invoke this function, which means that when we close the
253 * connection after cs->SendPackets we will close an already closed
254 * connection. This handles that case gracefully without having to make
255 * that code any more complex or more aware of the validity of the socket.
256 */
257 if (this->sock == INVALID_SOCKET) return status;
258
259 if (status != NETWORK_RECV_STATUS_CLIENT_QUIT && status != NETWORK_RECV_STATUS_SERVER_ERROR && !this->HasClientQuit() && this->status >= STATUS_AUTHORIZED) {
260 /* We did not receive a leave message from this client... */
261 std::string client_name = this->GetClientName();
262
263 NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST);
264
265 /* Inform other clients of this... strange leaving ;) */
266 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
267 if (new_cs->status > STATUS_AUTHORIZED && this != new_cs) {
268 new_cs->SendErrorQuit(this->client_id, NETWORK_ERROR_CONNECTION_LOST);
269 }
270 }
271 }
272
273 /* If we were transfering a map to this client, stop the savegame creation
274 * process and queue the next client to receive the map. */
275 if (this->status == STATUS_MAP) {
276 /* Ensure the saving of the game is stopped too. */
277 this->savegame->Destroy();
278 this->savegame = nullptr;
279
280 this->CheckNextClientToSendMap(this);
281 }
282
283 NetworkAdminClientError(this->client_id, NETWORK_ERROR_CONNECTION_LOST);
284 Debug(net, 3, "Closed client connection {}", this->client_id);
285
286 /* We just lost one client :( */
287 if (this->status >= STATUS_AUTHORIZED) _network_game_info.clients_on--;
288 extern byte _network_clients_connected;
289 _network_clients_connected--;
290
291 this->SendPackets(true);
292
293 delete this->GetInfo();
294 delete this;
295
296 InvalidateWindowData(WC_CLIENT_LIST, 0);
297
298 return status;
299 }
300
305 /* static */ bool ServerNetworkGameSocketHandler::AllowConnection()
306 {
307 extern byte _network_clients_connected;
308 bool accept = _network_clients_connected < MAX_CLIENTS && _network_game_info.clients_on < _settings_client.network.max_clients;
309
310 /* We can't go over the MAX_CLIENTS limit here. However, the
311 * pool must have place for all clients and ourself. */
312 static_assert(NetworkClientSocketPool::MAX_SIZE == MAX_CLIENTS + 1);
313 assert(!accept || ServerNetworkGameSocketHandler::CanAllocateItem());
314 return accept;
315 }
316
318 /* static */ void ServerNetworkGameSocketHandler::Send()
319 {
320 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
321 if (cs->writable) {
322 if (cs->SendPackets() != SPS_CLOSED && cs->status == STATUS_MAP) {
323 /* This client is in the middle of a map-send, call the function for that */
324 cs->SendMap();
325 }
326 }
327 }
328 }
329
330 static void NetworkHandleCommandQueue(NetworkClientSocket *cs);
331
332 /***********
333 * Sending functions
334 * DEF_SERVER_SEND_COMMAND has parameter: NetworkClientSocket *cs
335 ************/
336
341 NetworkRecvStatus ServerNetworkGameSocketHandler::SendClientInfo(NetworkClientInfo *ci)
342 {
343 if (ci->client_id != INVALID_CLIENT_ID) {
344 Packet *p = new Packet(PACKET_SERVER_CLIENT_INFO);
345 p->Send_uint32(ci->client_id);
346 p->Send_uint8 (ci->client_playas);
347 p->Send_string(ci->client_name);
348
349 this->SendPacket(p);
350 }
351 return NETWORK_RECV_STATUS_OKAY;
352 }
353
355 NetworkRecvStatus ServerNetworkGameSocketHandler::SendGameInfo()
356 {
357 Packet *p = new Packet(PACKET_SERVER_GAME_INFO, TCP_MTU);
358 SerializeNetworkGameInfo(p, GetCurrentNetworkServerGameInfo());
359
360 this->SendPacket(p);
361
362 return NETWORK_RECV_STATUS_OKAY;
363 }
364
370 NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode error, const std::string &reason)
371 {
372 Packet *p = new Packet(PACKET_SERVER_ERROR);
373
374 p->Send_uint8(error);
375 if (!reason.empty()) p->Send_string(reason);
376 this->SendPacket(p);
377
378 StringID strid = GetNetworkErrorMsg(error);
379
380 /* Only send when the current client was in game */
381 if (this->status > STATUS_AUTHORIZED) {
382 std::string client_name = this->GetClientName();
383
384 Debug(net, 1, "'{}' made an error and has been disconnected: {}", client_name, GetString(strid));
385
386 if (error == NETWORK_ERROR_KICKED && !reason.empty()) {
387 NetworkTextMessage(NETWORK_ACTION_KICKED, CC_DEFAULT, false, client_name, reason, strid);
388 } else {
389 NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", strid);
390 }
391
392 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
393 if (new_cs->status >= STATUS_AUTHORIZED && new_cs != this) {
394 /* Some errors we filter to a more general error. Clients don't have to know the real
395 * reason a joining failed. */
396 if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION) {
397 error = NETWORK_ERROR_ILLEGAL_PACKET;
398 }
399 new_cs->SendErrorQuit(this->client_id, error);
400 }
401 }
402
403 NetworkAdminClientError(this->client_id, error);
404 } else {
405 Debug(net, 1, "Client {} made an error and has been disconnected: {}", this->client_id, GetString(strid));
406 }
407
408 /* The client made a mistake, so drop the connection now! */
409 return this->CloseConnection(NETWORK_RECV_STATUS_SERVER_ERROR);
410 }
411
413 NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGRFCheck()
414 {
415 Packet *p = new Packet(PACKET_SERVER_CHECK_NEWGRFS, TCP_MTU);
416 const GRFConfig *c;
417 uint grf_count = 0;
418
419 for (c = _grfconfig; c != nullptr; c = c->next) {
420 if (!HasBit(c->flags, GCF_STATIC)) grf_count++;
421 }
422
423 p->Send_uint8 (grf_count);
424 for (c = _grfconfig; c != nullptr; c = c->next) {
425 if (!HasBit(c->flags, GCF_STATIC)) SerializeGRFIdentifier(p, &c->ident);
426 }
427
428 this->SendPacket(p);
429 return NETWORK_RECV_STATUS_OKAY;
430 }
431
433 NetworkRecvStatus ServerNetworkGameSocketHandler::SendNeedGamePassword()
434 {
435 /* Invalid packet when status is STATUS_AUTH_GAME or higher */
436 if (this->status >= STATUS_AUTH_GAME) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET);
437
438 this->status = STATUS_AUTH_GAME;
439 /* Reset 'lag' counters */
440 this->last_frame = this->last_frame_server = _frame_counter;
441
442 Packet *p = new Packet(PACKET_SERVER_NEED_GAME_PASSWORD);
443 this->SendPacket(p);
444 return NETWORK_RECV_STATUS_OKAY;
445 }
446
448 NetworkRecvStatus ServerNetworkGameSocketHandler::SendNeedCompanyPassword()
449 {
450 /* Invalid packet when status is STATUS_AUTH_COMPANY or higher */
451 if (this->status >= STATUS_AUTH_COMPANY) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET);
452
453 this->status = STATUS_AUTH_COMPANY;
454 /* Reset 'lag' counters */
455 this->last_frame = this->last_frame_server = _frame_counter;
456
457 Packet *p = new Packet(PACKET_SERVER_NEED_COMPANY_PASSWORD);
458 p->Send_uint32(_settings_game.game_creation.generation_seed);
459 p->Send_string(_settings_client.network.network_id);
460 this->SendPacket(p);
461 return NETWORK_RECV_STATUS_OKAY;
462 }
463
465 NetworkRecvStatus ServerNetworkGameSocketHandler::SendWelcome()
466 {
467 Packet *p;
468
469 /* Invalid packet when status is AUTH or higher */
470 if (this->status >= STATUS_AUTHORIZED) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET);
471
472 this->status = STATUS_AUTHORIZED;
473 /* Reset 'lag' counters */
474 this->last_frame = this->last_frame_server = _frame_counter;
475
476 _network_game_info.clients_on++;
477
478 p = new Packet(PACKET_SERVER_WELCOME);
479 p->Send_uint32(this->client_id);
480 p->Send_uint32(_settings_game.game_creation.generation_seed);
481 p->Send_string(_settings_client.network.network_id);
482 this->SendPacket(p);
483
484 /* Transmit info about all the active clients */
485 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
486 if (new_cs != this && new_cs->status >= STATUS_AUTHORIZED) {
487 this->SendClientInfo(new_cs->GetInfo());
488 }
489 }
490 /* Also send the info of the server */
491 return this->SendClientInfo(NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER));
492 }
493
495 NetworkRecvStatus ServerNetworkGameSocketHandler::SendWait()
496 {
497 int waiting = 1; // current player getting the map counts as 1
498 Packet *p;
499
500 /* Count how many clients are waiting in the queue, in front of you! */
501 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
502 if (new_cs->status != STATUS_MAP_WAIT) continue;
503 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++;
504 }
505
506 p = new Packet(PACKET_SERVER_WAIT);
507 p->Send_uint8(waiting);
508 this->SendPacket(p);
509 return NETWORK_RECV_STATUS_OKAY;
510 }
511
512 void ServerNetworkGameSocketHandler::CheckNextClientToSendMap(NetworkClientSocket *ignore_cs)
513 {
514 /* Find the best candidate for joining, i.e. the first joiner. */
515 NetworkClientSocket *best = nullptr;
516 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
517 if (ignore_cs == new_cs) continue;
518
519 if (new_cs->status == STATUS_MAP_WAIT) {
520 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)) {
521 best = new_cs;
522 }
523 }
524 }
525
526 /* Is there someone else to join? */
527 if (best != nullptr) {
528 /* Let the first start joining. */
529 best->status = STATUS_AUTHORIZED;
530 best->SendMap();
531
532 /* And update the rest. */
533 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
534 if (new_cs->status == STATUS_MAP_WAIT) new_cs->SendWait();
535 }
536 }
537 }
538
540 NetworkRecvStatus ServerNetworkGameSocketHandler::SendMap()
541 {
542 if (this->status < STATUS_AUTHORIZED) {
543 /* Illegal call, return error and ignore the packet */
544 return this->SendError(NETWORK_ERROR_NOT_AUTHORIZED);
545 }
546
547 if (this->status == STATUS_AUTHORIZED) {
548 this->savegame = new PacketWriter(this);
549
550 /* Now send the _frame_counter and how many packets are coming */
551 Packet *p = new Packet(PACKET_SERVER_MAP_BEGIN);
552 p->Send_uint32(_frame_counter);
553 this->SendPacket(p);
554
555 NetworkSyncCommandQueue(this);
556 this->status = STATUS_MAP;
557 /* Mark the start of download */
558 this->last_frame = _frame_counter;
559 this->last_frame_server = _frame_counter;
560
561 /* Make a dump of the current game */
562 if (SaveWithFilter(this->savegame, true) != SL_OK) usererror("network savedump failed");
563 }
564
565 if (this->status == STATUS_MAP) {
566 bool last_packet = this->savegame->TransferToNetworkQueue(this);
567 if (last_packet) {
568 /* Done reading, make sure saving is done as well */
569 this->savegame->Destroy();
570 this->savegame = nullptr;
571
572 /* Set the status to DONE_MAP, no we will wait for the client
573 * to send it is ready (maybe that happens like never ;)) */
574 this->status = STATUS_DONE_MAP;
575
576 this->CheckNextClientToSendMap();
577 }
578 }
579 return NETWORK_RECV_STATUS_OKAY;
580 }
581
586 NetworkRecvStatus ServerNetworkGameSocketHandler::SendJoin(ClientID client_id)
587 {
588 Packet *p = new Packet(PACKET_SERVER_JOIN);
589
590 p->Send_uint32(client_id);
591
592 this->SendPacket(p);
593 return NETWORK_RECV_STATUS_OKAY;
594 }
595
597 NetworkRecvStatus ServerNetworkGameSocketHandler::SendFrame()
598 {
599 Packet *p = new Packet(PACKET_SERVER_FRAME);
600 p->Send_uint32(_frame_counter);
601 p->Send_uint32(_frame_counter_max);
602 #ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
603 p->Send_uint32(_sync_seed_1);
604 #ifdef NETWORK_SEND_DOUBLE_SEED
605 p->Send_uint32(_sync_seed_2);
606 #endif
607 #endif
608
609 /* If token equals 0, we need to make a new token and send that. */
610 if (this->last_token == 0) {
611 this->last_token = InteractiveRandomRange(UINT8_MAX - 1) + 1;
612 p->Send_uint8(this->last_token);
613 }
614
615 this->SendPacket(p);
616 return NETWORK_RECV_STATUS_OKAY;
617 }
618
620 NetworkRecvStatus ServerNetworkGameSocketHandler::SendSync()
621 {
622 Packet *p = new Packet(PACKET_SERVER_SYNC);
623 p->Send_uint32(_frame_counter);
624 p->Send_uint32(_sync_seed_1);
625
626 #ifdef NETWORK_SEND_DOUBLE_SEED
627 p->Send_uint32(_sync_seed_2);
628 #endif
629 this->SendPacket(p);
630 return NETWORK_RECV_STATUS_OKAY;
631 }
632
637 NetworkRecvStatus ServerNetworkGameSocketHandler::SendCommand(const CommandPacket *cp)
638 {
639 Packet *p = new Packet(PACKET_SERVER_COMMAND);
640
641 this->NetworkGameSocketHandler::SendCommand(p, cp);
642 p->Send_uint32(cp->frame);
643 p->Send_bool (cp->my_cmd);
644
645 this->SendPacket(p);
646 return NETWORK_RECV_STATUS_OKAY;
647 }
648
657 NetworkRecvStatus ServerNetworkGameSocketHandler::SendChat(NetworkAction action, ClientID client_id, bool self_send, const std::string &msg, int64 data)
658 {
659 if (this->status < STATUS_PRE_ACTIVE) return NETWORK_RECV_STATUS_OKAY;
660
661 Packet *p = new Packet(PACKET_SERVER_CHAT);
662
663 p->Send_uint8 (action);
664 p->Send_uint32(client_id);
665 p->Send_bool (self_send);
666 p->Send_string(msg);
667 p->Send_uint64(data);
668
669 this->SendPacket(p);
670 return NETWORK_RECV_STATUS_OKAY;
671 }
672
678 NetworkRecvStatus ServerNetworkGameSocketHandler::SendErrorQuit(ClientID client_id, NetworkErrorCode errorno)
679 {
680 Packet *p = new Packet(PACKET_SERVER_ERROR_QUIT);
681
682 p->Send_uint32(client_id);
683 p->Send_uint8 (errorno);
684
685 this->SendPacket(p);
686 return NETWORK_RECV_STATUS_OKAY;
687 }
688
693 NetworkRecvStatus ServerNetworkGameSocketHandler::SendQuit(ClientID client_id)
694 {
695 Packet *p = new Packet(PACKET_SERVER_QUIT);
696
697 p->Send_uint32(client_id);
698
699 this->SendPacket(p);
700 return NETWORK_RECV_STATUS_OKAY;
701 }
702
704 NetworkRecvStatus ServerNetworkGameSocketHandler::SendShutdown()
705 {
706 Packet *p = new Packet(PACKET_SERVER_SHUTDOWN);
707 this->SendPacket(p);
708 return NETWORK_RECV_STATUS_OKAY;
709 }
710
712 NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGame()
713 {
714 Packet *p = new Packet(PACKET_SERVER_NEWGAME);
715 this->SendPacket(p);
716 return NETWORK_RECV_STATUS_OKAY;
717 }
718
724 NetworkRecvStatus ServerNetworkGameSocketHandler::SendRConResult(uint16 colour, const std::string &command)
725 {
726 Packet *p = new Packet(PACKET_SERVER_RCON);
727
728 p->Send_uint16(colour);
729 p->Send_string(command);
730 this->SendPacket(p);
731 return NETWORK_RECV_STATUS_OKAY;
732 }
733
739 NetworkRecvStatus ServerNetworkGameSocketHandler::SendMove(ClientID client_id, CompanyID company_id)
740 {
741 Packet *p = new Packet(PACKET_SERVER_MOVE);
742
743 p->Send_uint32(client_id);
744 p->Send_uint8(company_id);
745 this->SendPacket(p);
746 return NETWORK_RECV_STATUS_OKAY;
747 }
748
750 NetworkRecvStatus ServerNetworkGameSocketHandler::SendCompanyUpdate()
751 {
752 Packet *p = new Packet(PACKET_SERVER_COMPANY_UPDATE);
753
754 p->Send_uint16(_network_company_passworded);
755 this->SendPacket(p);
756 return NETWORK_RECV_STATUS_OKAY;
757 }
758
760 NetworkRecvStatus ServerNetworkGameSocketHandler::SendConfigUpdate()
761 {
762 Packet *p = new Packet(PACKET_SERVER_CONFIG_UPDATE);
763
764 p->Send_uint8(_settings_client.network.max_companies);
765 p->Send_string(_settings_client.network.server_name);
766 this->SendPacket(p);
767 return NETWORK_RECV_STATUS_OKAY;
768 }
769
770 /***********
771 * Receiving functions
772 * DEF_SERVER_RECEIVE_COMMAND has parameter: NetworkClientSocket *cs, Packet *p
773 ************/
774
775 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_INFO(Packet *p)
776 {
777 return this->SendGameInfo();
778 }
779
780 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_NEWGRFS_CHECKED(Packet *p)
781 {
782 if (this->status != STATUS_NEWGRFS_CHECK) {
783 /* Illegal call, return error and ignore the packet */
784 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
785 }
786
787 NetworkClientInfo *ci = this->GetInfo();
788
789 /* We now want a password from the client else we do not allow them in! */
790 if (!_settings_client.network.server_password.empty()) {
791 return this->SendNeedGamePassword();
792 }
793
794 if (Company::IsValidID(ci->client_playas) && !_network_company_states[ci->client_playas].password.empty()) {
795 return this->SendNeedCompanyPassword();
796 }
797
798 return this->SendWelcome();
799 }
800
801 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet *p)
802 {
803 if (this->status != STATUS_INACTIVE) {
804 /* Illegal call, return error and ignore the packet */
805 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
806 }
807
808 std::string client_revision = p->Recv_string(NETWORK_REVISION_LENGTH);
809 uint32 newgrf_version = p->Recv_uint32();
810
811 /* Check if the client has revision control enabled */
812 if (!IsNetworkCompatibleVersion(client_revision) || _openttd_newgrf_version != newgrf_version) {
813 /* Different revisions!! */
814 return this->SendError(NETWORK_ERROR_WRONG_REVISION);
815 }
816
817 std::string client_name = p->Recv_string(NETWORK_CLIENT_NAME_LENGTH);
818 CompanyID playas = (Owner)p->Recv_uint8();
819
820 if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CLIENT_QUIT;
821
822 /* join another company does not affect these values */
823 switch (playas) {
824 case COMPANY_NEW_COMPANY: // New company
825 if (Company::GetNumItems() >= _settings_client.network.max_companies) {
826 return this->SendError(NETWORK_ERROR_FULL);
827 }
828 break;
829 case COMPANY_SPECTATOR: // Spectator
830 break;
831 default: // Join another company (companies 1-8 (index 0-7))
832 if (!Company::IsValidHumanID(playas)) {
833 return this->SendError(NETWORK_ERROR_COMPANY_MISMATCH);
834 }
835 break;
836 }
837
838 if (!NetworkIsValidClientName(client_name)) {
839 /* An invalid client name was given. However, the client ensures the name
840 * is valid before it is sent over the network, so something went horribly
841 * wrong. This is probably someone trying to troll us. */
842 return this->SendError(NETWORK_ERROR_INVALID_CLIENT_NAME);
843 }
844
845 if (!NetworkMakeClientNameUnique(client_name)) { // Change name if duplicate
846 /* We could not create a name for this client */
847 return this->SendError(NETWORK_ERROR_NAME_IN_USE);
848 }
849
850 assert(NetworkClientInfo::CanAllocateItem());
851 NetworkClientInfo *ci = new NetworkClientInfo(this->client_id);
852 this->SetInfo(ci);
853 ci->join_date = _date;
854 ci->client_name = client_name;
855 ci->client_playas = playas;
856 Debug(desync, 1, "client: {:08x}; {:02x}; {:02x}; {:02x}", _date, _date_fract, (int)ci->client_playas, (int)ci->index);
857
858 /* Make sure companies to which people try to join are not autocleaned */
859 if (Company::IsValidID(playas)) _network_company_states[playas].months_empty = 0;
860
861 this->status = STATUS_NEWGRFS_CHECK;
862
863 if (_grfconfig == nullptr) {
864 /* Behave as if we received PACKET_CLIENT_NEWGRFS_CHECKED */
865 return this->Receive_CLIENT_NEWGRFS_CHECKED(nullptr);
866 }
867
868 return this->SendNewGRFCheck();
869 }
870
871 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_PASSWORD(Packet *p)
872 {
873 if (this->status != STATUS_AUTH_GAME) {
874 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
875 }
876
877 std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
878
879 /* Check game password. Allow joining if we cleared the password meanwhile */
880 if (!_settings_client.network.server_password.empty() &&
881 _settings_client.network.server_password.compare(password) != 0) {
882 /* Password is invalid */
883 return this->SendError(NETWORK_ERROR_WRONG_PASSWORD);
884 }
885
886 const NetworkClientInfo *ci = this->GetInfo();
887 if (Company::IsValidID(ci->client_playas) && !_network_company_states[ci->client_playas].password.empty()) {
888 return this->SendNeedCompanyPassword();
889 }
890
891 /* Valid password, allow user */
892 return this->SendWelcome();
893 }
894
895 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMPANY_PASSWORD(Packet *p)
896 {
897 if (this->status != STATUS_AUTH_COMPANY) {
898 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
899 }
900
901 std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
902
903 /* Check company password. Allow joining if we cleared the password meanwhile.
904 * Also, check the company is still valid - client could be moved to spectators
905 * in the middle of the authorization process */
906 CompanyID playas = this->GetInfo()->client_playas;
907 if (Company::IsValidID(playas) && !_network_company_states[playas].password.empty() &&
908 _network_company_states[playas].password.compare(password) != 0) {
909 /* Password is invalid */
910 return this->SendError(NETWORK_ERROR_WRONG_PASSWORD);
911 }
912
913 return this->SendWelcome();
914 }
915
916 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GETMAP(Packet *p)
917 {
918 /* The client was never joined.. so this is impossible, right?
919 * Ignore the packet, give the client a warning, and close the connection */
920 if (this->status < STATUS_AUTHORIZED || this->HasClientQuit()) {
921 return this->SendError(NETWORK_ERROR_NOT_AUTHORIZED);
922 }
923
924 /* Check if someone else is receiving the map */
925 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
926 if (new_cs->status == STATUS_MAP) {
927 /* Tell the new client to wait */
928 this->status = STATUS_MAP_WAIT;
929 return this->SendWait();
930 }
931 }
932
933 /* We receive a request to upload the map.. give it to the client! */
934 return this->SendMap();
935 }
936
937 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK(Packet *p)
938 {
939 /* Client has the map, now start syncing */
940 if (this->status == STATUS_DONE_MAP && !this->HasClientQuit()) {
941 std::string client_name = this->GetClientName();
942
943 NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, client_name, "", this->client_id);
944 InvalidateWindowData(WC_CLIENT_LIST, 0);
945
946 /* Mark the client as pre-active, and wait for an ACK
947 * so we know it is done loading and in sync with us */
948 this->status = STATUS_PRE_ACTIVE;
949 NetworkHandleCommandQueue(this);
950 this->SendFrame();
951 this->SendSync();
952
953 /* This is the frame the client receives
954 * we need it later on to make sure the client is not too slow */
955 this->last_frame = _frame_counter;
956 this->last_frame_server = _frame_counter;
957
958 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
959 if (new_cs->status >= STATUS_AUTHORIZED) {
960 new_cs->SendClientInfo(this->GetInfo());
961 new_cs->SendJoin(this->client_id);
962 }
963 }
964
965 NetworkAdminClientInfo(this, true);
966
967 /* also update the new client with our max values */
968 this->SendConfigUpdate();
969
970 /* quickly update the syncing client with company details */
971 return this->SendCompanyUpdate();
972 }
973
974 /* Wrong status for this packet, give a warning to client, and close connection */
975 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
976 }
977
982 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND(Packet *p)
983 {
984 /* The client was never joined.. so this is impossible, right?
985 * Ignore the packet, give the client a warning, and close the connection */
986 if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
987 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
988 }
989
990 if (this->incoming_queue.Count() >= _settings_client.network.max_commands_in_queue) {
991 return this->SendError(NETWORK_ERROR_TOO_MANY_COMMANDS);
992 }
993
994 CommandPacket cp;
995 const char *err = this->ReceiveCommand(p, &cp);
996
997 if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CLIENT_QUIT;
998
999 NetworkClientInfo *ci = this->GetInfo();
1000
1001 if (err != nullptr) {
1002 IConsolePrint(CC_WARNING, "Dropping client #{} (IP: {}) due to {}.", ci->client_id, this->GetClientIP(), err);
1003 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
1004 }
1005
1006
1007 if ((GetCommandFlags(cp.cmd) & CMD_SERVER) && ci->client_id != CLIENT_ID_SERVER) {
1008 IConsolePrint(CC_WARNING, "Kicking client #{} (IP: {}) due to calling a server only command {}.", ci->client_id, this->GetClientIP(), cp.cmd & CMD_ID_MASK);
1009 return this->SendError(NETWORK_ERROR_KICKED);
1010 }
1011
1012 if ((GetCommandFlags(cp.cmd) & CMD_SPECTATOR) == 0 && !Company::IsValidID(cp.company) && ci->client_id != CLIENT_ID_SERVER) {
1013 IConsolePrint(CC_WARNING, "Kicking client #{} (IP: {}) due to calling a non-spectator command {}.", ci->client_id, this->GetClientIP(), cp.cmd & CMD_ID_MASK);
1014 return this->SendError(NETWORK_ERROR_KICKED);
1015 }
1016
1022 if (!(cp.cmd == CMD_COMPANY_CTRL && cp.p1 == 0 && ci->client_playas == COMPANY_NEW_COMPANY) && ci->client_playas != cp.company) {
1023 IConsolePrint(CC_WARNING, "Kicking client #{} (IP: {}) due to calling a command as another company {}.",
1024 ci->client_playas + 1, this->GetClientIP(), cp.company + 1);
1025 return this->SendError(NETWORK_ERROR_COMPANY_MISMATCH);
1026 }
1027
1028 if (cp.cmd == CMD_COMPANY_CTRL) {
1029 if (cp.p1 != 0 || cp.company != COMPANY_SPECTATOR) {
1030 return this->SendError(NETWORK_ERROR_CHEATER);
1031 }
1032
1033 /* Check if we are full - else it's possible for spectators to send a CMD_COMPANY_CTRL and the company is created regardless of max_companies! */
1034 if (Company::GetNumItems() >= _settings_client.network.max_companies) {
1035 NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_CLIENT, ci->client_id, "cannot create new company, server full", CLIENT_ID_SERVER);
1036 return NETWORK_RECV_STATUS_OKAY;
1037 }
1038 }
1039
1040 if (GetCommandFlags(cp.cmd) & CMD_CLIENT_ID) cp.p2 = this->client_id;
1041
1042 this->incoming_queue.Append(&cp);
1043 return NETWORK_RECV_STATUS_OKAY;
1044 }
1045
1046 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet *p)
1047 {
1048 /* This packets means a client noticed an error and is reporting this
1049 * to us. Display the error and report it to the other clients */
1050 NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8();
1051
1052 /* The client was never joined.. thank the client for the packet, but ignore it */
1053 if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
1054 return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
1055 }
1056
1057 std::string client_name = this->GetClientName();
1058 StringID strid = GetNetworkErrorMsg(errorno);
1059
1060 Debug(net, 1, "'{}' reported an error and is closing its connection: {}", client_name, GetString(strid));
1061
1062 NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", strid);
1063
1064 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
1065 if (new_cs->status >= STATUS_AUTHORIZED) {
1066 new_cs->SendErrorQuit(this->client_id, errorno);
1067 }
1068 }
1069
1070 NetworkAdminClientError(this->client_id, errorno);
1071
1072 return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
1073 }
1074
1075 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_QUIT(Packet *p)
1076 {
1077 /* The client was never joined.. thank the client for the packet, but ignore it */
1078 if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
1079 return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
1080 }
1081
1082 /* The client wants to leave. Display this and report it to the other clients. */
1083 std::string client_name = this->GetClientName();
1084 NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", STR_NETWORK_MESSAGE_CLIENT_LEAVING);
1085
1086 for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
1087 if (new_cs->status >= STATUS_AUTHORIZED && new_cs != this) {
1088 new_cs->SendQuit(this->client_id);
1089 }
1090 }
1091
1092 NetworkAdminClientQuit(this->client_id);
1093
1094 return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
1095 }
1096
1097 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ACK(Packet *p)
1098 {
1099 if (this->status < STATUS_AUTHORIZED) {
1100 /* Illegal call, return error and ignore the packet */
1101 return this->SendError(NETWORK_ERROR_NOT_AUTHORIZED);
1102 }
1103
1104 uint32 frame = p->Recv_uint32();
1105
1106 /* The client is trying to catch up with the server */
1107 if (this->status == STATUS_PRE_ACTIVE) {
1108 /* The client is not yet caught up? */
1109 if (frame + DAY_TICKS < _frame_counter) return NETWORK_RECV_STATUS_OKAY;
1110
1111 /* Now it is! Unpause the game */
1112 this->status = STATUS_ACTIVE;
1113 this->last_token_frame = _frame_counter;
1114
1115 /* Execute script for, e.g. MOTD */
1116 IConsoleCmdExec("exec scripts/on_server_connect.scr 0");
1117 }
1118
1119 /* Get, and validate the token. */
1120 uint8 token = p->Recv_uint8();
1121 if (token == this->last_token) {
1122 /* We differentiate between last_token_frame and last_frame so the lag
1123 * test uses the actual lag of the client instead of the lag for getting
1124 * the token back and forth; after all, the token is only sent every
1125 * time we receive a PACKET_CLIENT_ACK, after which we will send a new
1126 * token to the client. If the lag would be one day, then we would not
1127 * be sending the new token soon enough for the new daily scheduled
1128 * PACKET_CLIENT_ACK. This would then register the lag of the client as
1129 * two days, even when it's only a single day. */
1130 this->last_token_frame = _frame_counter;
1131 /* Request a new token. */
1132 this->last_token = 0;
1133 }
1134
1135 /* The client received the frame, make note of it */
1136 this->last_frame = frame;
1137 /* With those 2 values we can calculate the lag realtime */
1138 this->last_frame_server = _frame_counter;
1139 return NETWORK_RECV_STATUS_OKAY;
1140 }
1141
1142
1153 void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const std::string &msg, ClientID from_id, int64 data, bool from_admin)
1154 {
1155 const NetworkClientInfo *ci, *ci_own, *ci_to;
1156
1157 switch (desttype) {
1158 case DESTTYPE_CLIENT:
1159 /* Are we sending to the server? */
1160 if ((ClientID)dest == CLIENT_ID_SERVER) {
1161 ci = NetworkClientInfo::GetByClientID(from_id);
1162 /* Display the text locally, and that is it */
1163 if (ci != nullptr) {
1164 NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), false, ci->client_name, msg, data);
1165
1166 if (_settings_client.network.server_admin_chat) {
1167 NetworkAdminChat(action, desttype, from_id, msg, data, from_admin);
1168 }
1169 }
1170 } else {
1171 /* Else find the client to send the message to */
1172 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1173 if (cs->client_id == (ClientID)dest) {
1174 cs->SendChat(action, from_id, false, msg, data);
1175 break;
1176 }
1177 }
1178 }
1179
1180 /* Display the message locally (so you know you have sent it) */
1181 if (from_id != (ClientID)dest) {
1182 if (from_id == CLIENT_ID_SERVER) {
1183 ci = NetworkClientInfo::GetByClientID(from_id);
1184 ci_to = NetworkClientInfo::GetByClientID((ClientID)dest);
1185 if (ci != nullptr && ci_to != nullptr) {
1186 NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), true, ci_to->client_name, msg, data);
1187 }
1188 } else {
1189 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1190 if (cs->client_id == from_id) {
1191 cs->SendChat(action, (ClientID)dest, true, msg, data);
1192 break;
1193 }
1194 }
1195 }
1196 }
1197 break;
1198 case DESTTYPE_TEAM: {
1199 /* If this is false, the message is already displayed on the client who sent it. */
1200 bool show_local = true;
1201 /* Find all clients that belong to this company */
1202 ci_to = nullptr;
1203 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1204 ci = cs->GetInfo();
1205 if (ci != nullptr && ci->client_playas == (CompanyID)dest) {
1206 cs->SendChat(action, from_id, false, msg, data);
1207 if (cs->client_id == from_id) show_local = false;
1208 ci_to = ci; // Remember a client that is in the company for company-name
1209 }
1210 }
1211
1212 /* if the server can read it, let the admin network read it, too. */
1213 if (_local_company == (CompanyID)dest && _settings_client.network.server_admin_chat) {
1214 NetworkAdminChat(action, desttype, from_id, msg, data, from_admin);
1215 }
1216
1217 ci = NetworkClientInfo::GetByClientID(from_id);
1218 ci_own = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
1219 if (ci != nullptr && ci_own != nullptr && ci_own->client_playas == dest) {
1220 NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), false, ci->client_name, msg, data);
1221 if (from_id == CLIENT_ID_SERVER) show_local = false;
1222 ci_to = ci_own;
1223 }
1224
1225 /* There is no such client */
1226 if (ci_to == nullptr) break;
1227
1228 /* Display the message locally (so you know you have sent it) */
1229 if (ci != nullptr && show_local) {
1230 if (from_id == CLIENT_ID_SERVER) {
1231 StringID str = Company::IsValidID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
1232 SetDParam(0, ci_to->client_playas);
1233 std::string name = GetString(str);
1234 NetworkTextMessage(action, GetDrawStringCompanyColour(ci_own->client_playas), true, name, msg, data);
1235 } else {
1236 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1237 if (cs->client_id == from_id) {
1238 cs->SendChat(action, ci_to->client_id, true, msg, data);
1239 }
1240 }
1241 }
1242 }
1243 break;
1244 }
1245 default:
1246 Debug(net, 1, "Received unknown chat destination type {}; doing broadcast instead", desttype);
1247 FALLTHROUGH;
1248
1249 case DESTTYPE_BROADCAST:
1250 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1251 cs->SendChat(action, from_id, false, msg, data);
1252 }
1253
1254 NetworkAdminChat(action, desttype, from_id, msg, data, from_admin);
1255
1256 ci = NetworkClientInfo::GetByClientID(from_id);
1257 if (ci != nullptr) {
1258 NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), false, ci->client_name, msg, data);
1259 }
1260 break;
1261 }
1262 }
1263
1264 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_CHAT(Packet *p)
1265 {
1266 if (this->status < STATUS_PRE_ACTIVE) {
1267 /* Illegal call, return error and ignore the packet */
1268 return this->SendError(NETWORK_ERROR_NOT_AUTHORIZED);
1269 }
1270
1271 NetworkAction action = (NetworkAction)p->Recv_uint8();
1272 DestType desttype = (DestType)p->Recv_uint8();
1273 int dest = p->Recv_uint32();
1274
1275 std::string msg = p->Recv_string(NETWORK_CHAT_LENGTH);
1276 int64 data = p->Recv_uint64();
1277
1278 NetworkClientInfo *ci = this->GetInfo();
1279 switch (action) {
1280 case NETWORK_ACTION_CHAT:
1281 case NETWORK_ACTION_CHAT_CLIENT:
1282 case NETWORK_ACTION_CHAT_COMPANY:
1283 NetworkServerSendChat(action, desttype, dest, msg, this->client_id, data);
1284 break;
1285 default:
1286 IConsolePrint(CC_WARNING, "Kicking client #{} (IP: {}) due to unknown chact action.", ci->client_id, this->GetClientIP());
1287 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
1288 }
1289 return NETWORK_RECV_STATUS_OKAY;
1290 }
1291
1292 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_SET_PASSWORD(Packet *p)
1293 {
1294 if (this->status != STATUS_ACTIVE) {
1295 /* Illegal call, return error and ignore the packet */
1296 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
1297 }
1298
1299 std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
1300 const NetworkClientInfo *ci = this->GetInfo();
1301
1302 NetworkServerSetCompanyPassword(ci->client_playas, password);
1303 return NETWORK_RECV_STATUS_OKAY;
1304 }
1305
1306 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_SET_NAME(Packet *p)
1307 {
1308 if (this->status != STATUS_ACTIVE) {
1309 /* Illegal call, return error and ignore the packet */
1310 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
1311 }
1312
1313 NetworkClientInfo *ci;
1314
1315 std::string client_name = p->Recv_string(NETWORK_CLIENT_NAME_LENGTH);
1316 ci = this->GetInfo();
1317
1318 if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CLIENT_QUIT;
1319
1320 if (ci != nullptr) {
1321 if (!NetworkIsValidClientName(client_name)) {
1322 /* An invalid client name was given. However, the client ensures the name
1323 * is valid before it is sent over the network, so something went horribly
1324 * wrong. This is probably someone trying to troll us. */
1325 return this->SendError(NETWORK_ERROR_INVALID_CLIENT_NAME);
1326 }
1327
1328 /* Display change */
1329 if (NetworkMakeClientNameUnique(client_name)) {
1330 NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, client_name);
1331 ci->client_name = client_name;
1332 NetworkUpdateClientInfo(ci->client_id);
1333 }
1334 }
1335 return NETWORK_RECV_STATUS_OKAY;
1336 }
1337
1338 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_RCON(Packet *p)
1339 {
1340 if (this->status != STATUS_ACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
1341
1342 if (_settings_client.network.rcon_password.empty()) return NETWORK_RECV_STATUS_OKAY;
1343
1344 std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
1345 std::string command = p->Recv_string(NETWORK_RCONCOMMAND_LENGTH);
1346
1347 if (_settings_client.network.rcon_password.compare(password) != 0) {
1348 Debug(net, 1, "[rcon] Wrong password from client-id {}", this->client_id);
1349 return NETWORK_RECV_STATUS_OKAY;
1350 }
1351
1352 Debug(net, 3, "[rcon] Client-id {} executed: {}", this->client_id, command);
1353
1354 _redirect_console_to_client = this->client_id;
1355 IConsoleCmdExec(command.c_str());
1356 _redirect_console_to_client = INVALID_CLIENT_ID;
1357 return NETWORK_RECV_STATUS_OKAY;
1358 }
1359
1360 NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_MOVE(Packet *p)
1361 {
1362 if (this->status != STATUS_ACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
1363
1364 CompanyID company_id = (Owner)p->Recv_uint8();
1365
1366 /* Check if the company is valid, we don't allow moving to AI companies */
1367 if (company_id != COMPANY_SPECTATOR && !Company::IsValidHumanID(company_id)) return NETWORK_RECV_STATUS_OKAY;
1368
1369 /* Check if we require a password for this company */
1370 if (company_id != COMPANY_SPECTATOR && !_network_company_states[company_id].password.empty()) {
1371 /* we need a password from the client - should be in this packet */
1372 std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
1373
1374 /* Incorrect password sent, return! */
1375 if (_network_company_states[company_id].password.compare(password) != 0) {
1376 Debug(net, 2, "Wrong password from client-id #{} for company #{}", this->client_id, company_id + 1);
1377 return NETWORK_RECV_STATUS_OKAY;
1378 }
1379 }
1380
1381 /* if we get here we can move the client */
1382 NetworkServerDoMove(this->client_id, company_id);
1383 return NETWORK_RECV_STATUS_OKAY;
1384 }
1385
1390 void NetworkPopulateCompanyStats(NetworkCompanyStats *stats)
1391 {
1392 memset(stats, 0, sizeof(*stats) * MAX_COMPANIES);
1393
1394 /* Go through all vehicles and count the type of vehicles */
1395 for (const Vehicle *v : Vehicle::Iterate()) {
1396 if (!Company::IsValidID(v->owner) || !v->IsPrimaryVehicle()) continue;
1397 byte type = 0;
1398 switch (v->type) {
1399 case VEH_TRAIN: type = NETWORK_VEH_TRAIN; break;
1400 case VEH_ROAD: type = RoadVehicle::From(v)->IsBus() ? NETWORK_VEH_BUS : NETWORK_VEH_LORRY; break;
1401 case VEH_AIRCRAFT: type = NETWORK_VEH_PLANE; break;
1402 case VEH_SHIP: type = NETWORK_VEH_SHIP; break;
1403 default: continue;
1404 }
1405 stats[v->owner].num_vehicle[type]++;
1406 }
1407
1408 /* Go through all stations and count the types of stations */
1409 for (const Station *s : Station::Iterate()) {
1410 if (Company::IsValidID(s->owner)) {
1411 NetworkCompanyStats *npi = &stats[s->owner];
1412
1413 if (s->facilities & FACIL_TRAIN) npi->num_station[NETWORK_VEH_TRAIN]++;
1414 if (s->facilities & FACIL_TRUCK_STOP) npi->num_station[NETWORK_VEH_LORRY]++;
1415 if (s->facilities & FACIL_BUS_STOP) npi->num_station[NETWORK_VEH_BUS]++;
1416 if (s->facilities & FACIL_AIRPORT) npi->num_station[NETWORK_VEH_PLANE]++;
1417 if (s->facilities & FACIL_DOCK) npi->num_station[NETWORK_VEH_SHIP]++;
1418 }
1419 }
1420 }
1421
1426 void NetworkUpdateClientInfo(ClientID client_id)
1427 {
1428 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
1429
1430 if (ci == nullptr) return;
1431
1432 Debug(desync, 1, "client: {:08x}; {:02x}; {:02x}; {:04x}", _date, _date_fract, (int)ci->client_playas, client_id);
1433
1434 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1435 if (cs->status >= ServerNetworkGameSocketHandler::STATUS_AUTHORIZED) {
1436 cs->SendClientInfo(ci);
1437 }
1438 }
1439
1440 NetworkAdminClientUpdate(ci);
1441 }
1442
1444 static void NetworkCheckRestartMap()
1445 {
1446 if (_settings_client.network.restart_game_year != 0 && _cur_year >= _settings_client.network.restart_game_year) {
1447 Debug(net, 3, "Auto-restarting map: year {} reached", _cur_year);
1448
1449 _settings_newgame.game_creation.generation_seed = GENERATE_NEW_SEED;
1450 switch(_file_to_saveload.abstract_ftype) {
1451 case FT_SAVEGAME:
1452 case FT_SCENARIO:
1453 _switch_mode = SM_LOAD_GAME;
1454 break;
1455
1456 case FT_HEIGHTMAP:
1457 _switch_mode = SM_START_HEIGHTMAP;
1458 break;
1459
1460 default:
1461 _switch_mode = SM_NEWGAME;
1462 }
1463 }
1464 }
1465
1472 static void NetworkAutoCleanCompanies()
1473 {
1474 bool clients_in_company[MAX_COMPANIES];
1475 int vehicles_in_company[MAX_COMPANIES];
1476
1477 if (!_settings_client.network.autoclean_companies) return;
1478
1479 memset(clients_in_company, 0, sizeof(clients_in_company));
1480
1481 /* Detect the active companies */
1482 for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
1483 if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
1484 }
1485
1486 if (!_network_dedicated) {
1487 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
1488 if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
1489 }
1490
1491 if (_settings_client.network.autoclean_novehicles != 0) {
1492 memset(vehicles_in_company, 0, sizeof(vehicles_in_company));
1493
1494 for (const Vehicle *v : Vehicle::Iterate()) {
1495 if (!Company::IsValidID(v->owner) || !v->IsPrimaryVehicle()) continue;
1496 vehicles_in_company[v->owner]++;
1497 }
1498 }
1499
1500 /* Go through all the companies */
1501 for (const Company *c : Company::Iterate()) {
1502 /* Skip the non-active once */
1503 if (c->is_ai) continue;
1504
1505 if (!clients_in_company[c->index]) {
1506 /* The company is empty for one month more */
1507 _network_company_states[c->index].months_empty++;
1508
1509 /* Is the company empty for autoclean_unprotected-months, and is there no protection? */
1510 if (_settings_client.network.autoclean_unprotected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_unprotected && _network_company_states[c->index].password.empty()) {
1511 /* Shut the company down */
1512 DoCommandP(0, CCA_DELETE | c->index << 16 | CRR_AUTOCLEAN << 24, 0, CMD_COMPANY_CTRL);
1513 IConsolePrint(CC_INFO, "Auto-cleaned company #{} with no password.", c->index + 1);
1514 }
1515 /* Is the company empty for autoclean_protected-months, and there is a protection? */
1516 if (_settings_client.network.autoclean_protected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_protected && !_network_company_states[c->index].password.empty()) {
1517 /* Unprotect the company */
1518 _network_company_states[c->index].password.clear();
1519 IConsolePrint(CC_INFO, "Auto-removed protection from company #{}.", c->index + 1);
1520 _network_company_states[c->index].months_empty = 0;
1521 NetworkServerUpdateCompanyPassworded(c->index, false);
1522 }
1523 /* Is the company empty for autoclean_novehicles-months, and has no vehicles? */
1524 if (_settings_client.network.autoclean_novehicles != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_novehicles && vehicles_in_company[c->index] == 0) {
1525 /* Shut the company down */
1526 DoCommandP(0, CCA_DELETE | c->index << 16 | CRR_AUTOCLEAN << 24, 0, CMD_COMPANY_CTRL);
1527 IConsolePrint(CC_INFO, "Auto-cleaned company #{} with no vehicles.", c->index + 1);
1528 }
1529 } else {
1530 /* It is not empty, reset the date */
1531 _network_company_states[c->index].months_empty = 0;
1532 }
1533 }
1534 }
1535
1541 bool NetworkMakeClientNameUnique(std::string &name)
1542 {
1543 bool is_name_unique = false;
1544 uint number = 0;
1545 std::string original_name = name;
1546
1547 while (!is_name_unique) {
1548 is_name_unique = true;
1549 for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
1550 if (ci->client_name.compare(name) == 0) {
1551 /* Name already in use */
1552 is_name_unique = false;
1553 break;
1554 }
1555 }
1556 /* Check if it is the same as the server-name */
1557 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
1558 if (ci != nullptr) {
1559 if (ci->client_name.compare(name) == 0) is_name_unique = false; // name already in use
1560 }
1561
1562 if (!is_name_unique) {
1563 /* Try a new name (<name> #1, <name> #2, and so on) */
1564 name = original_name + " #" + std::to_string(number);
1565
1566 /* The constructed client name is larger than the limit,
1567 * so... bail out as no valid name can be created. */
1568 if (name.size() >= NETWORK_CLIENT_NAME_LENGTH) return false;
1569 }
1570 }
1571
1572 return is_name_unique;
1573 }
1574
1581 bool NetworkServerChangeClientName(ClientID client_id, const std::string &new_name)
1582 {
1583 /* Check if the name's already in use */
1584 for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
1585 if (ci->client_name.compare(new_name) == 0) return false;
1586 }
1587
1588 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
1589 if (ci == nullptr) return false;
1590
1591 NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, true, ci->client_name, new_name);
1592
1593 ci->client_name = new_name;
1594
1595 NetworkUpdateClientInfo(client_id);
1596 return true;
1597 }
1598
1605 void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &password, bool already_hashed)
1606 {
1607 if (!Company::IsValidHumanID(company_id)) return;
1608
1609 if (already_hashed) {
1610 _network_company_states[company_id].password = password;
1611 } else {
1612 _network_company_states[company_id].password = GenerateCompanyPasswordHash(password, _settings_client.network.network_id, _settings_game.game_creation.generation_seed);
1613 }
1614
1615 NetworkServerUpdateCompanyPassworded(company_id, !_network_company_states[company_id].password.empty());
1616 }
1617
1622 static void NetworkHandleCommandQueue(NetworkClientSocket *cs)
1623 {
1624 CommandPacket *cp;
1625 while ((cp = cs->outgoing_queue.Pop()) != nullptr) {
1626 cs->SendCommand(cp);
1627 delete cp;
1628 }
1629 }
1630
1635 void NetworkServer_Tick(bool send_frame)
1636 {
1637 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
1638 bool send_sync = false;
1639 #endif
1640
1641 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
1642 if (_frame_counter >= _last_sync_frame + _settings_client.network.sync_freq) {
1643 _last_sync_frame = _frame_counter;
1644 send_sync = true;
1645 }
1646 #endif
1647
1648 /* Now we are done with the frame, inform the clients that they can
1649 * do their frame! */
1650 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1651 /* We allow a number of bytes per frame, but only to the burst amount
1652 * to be available for packet receiving at any particular time. */
1653 cs->receive_limit = std::min<size_t>(cs->receive_limit + _settings_client.network.bytes_per_frame,
1654 _settings_client.network.bytes_per_frame_burst);
1655
1656 /* Check if the speed of the client is what we can expect from a client */
1657 uint lag = NetworkCalculateLag(cs);
1658 switch (cs->status) {
1659 case NetworkClientSocket::STATUS_ACTIVE:
1660 if (lag > _settings_client.network.max_lag_time) {
1661 /* Client did still not report in within the specified limit. */
1662 IConsolePrint(CC_WARNING, cs->last_packet + std::chrono::milliseconds(lag * MILLISECONDS_PER_TICK) > std::chrono::steady_clock::now() ?
1663 /* A packet was received in the last three game days, so the client is likely lagging behind. */
1664 "Client #%u (IP: %s) is dropped because the client's game state is more than %d ticks behind." :
1665 /* No packet was received in the last three game days; sounds like a lost connection. */
1666 "Client #%u (IP: %s) is dropped because the client did not respond for more than %d ticks.",
1667 cs->client_id, cs->GetClientIP(), lag);
1668 cs->SendError(NETWORK_ERROR_TIMEOUT_COMPUTER);
1669 continue;
1670 }
1671
1672 /* Report once per time we detect the lag, and only when we
1673 * received a packet in the last 2 seconds. If we
1674 * did not receive a packet, then the client is not just
1675 * slow, but the connection is likely severed. Mentioning
1676 * frame_freq is not useful in this case. */
1677 if (lag > (uint)DAY_TICKS && cs->lag_test == 0 && cs->last_packet + std::chrono::seconds(2) > std::chrono::steady_clock::now()) {
1678 IConsolePrint(CC_WARNING, "[{}] Client #{} is slow, try increasing [network.]frame_freq to a higher value!", _frame_counter, cs->client_id);
1679 cs->lag_test = 1;
1680 }
1681
1682 if (cs->last_frame_server - cs->last_token_frame >= _settings_client.network.max_lag_time) {
1683 /* This is a bad client! It didn't send the right token back within time. */
1684 IConsolePrint(CC_WARNING, "Client #{} (IP: {}) is dropped because it fails to send valid acks.", cs->client_id, cs->GetClientIP());
1685 cs->SendError(NETWORK_ERROR_TIMEOUT_COMPUTER);
1686 continue;
1687 }
1688 break;
1689
1690 case NetworkClientSocket::STATUS_INACTIVE:
1691 case NetworkClientSocket::STATUS_NEWGRFS_CHECK:
1692 case NetworkClientSocket::STATUS_AUTHORIZED:
1693 /* NewGRF check and authorized states should be handled almost instantly.
1694 * So give them some lee-way, likewise for the query with inactive. */
1695 if (lag > _settings_client.network.max_init_time) {
1696 IConsolePrint(CC_WARNING, "Client #{} (IP: {}) is dropped because it took longer than {} ticks to start the joining process.", cs->client_id, cs->GetClientIP(), _settings_client.network.max_init_time);
1697 cs->SendError(NETWORK_ERROR_TIMEOUT_COMPUTER);
1698 continue;
1699 }
1700 break;
1701
1702 case NetworkClientSocket::STATUS_MAP_WAIT:
1703 /* Send every two seconds a packet to the client, to make sure
1704 * it knows the server is still there; just someone else is
1705 * still receiving the map. */
1706 if (std::chrono::steady_clock::now() > cs->last_packet + std::chrono::seconds(2)) {
1707 cs->SendWait();
1708 /* We need to reset the timer, as otherwise we will be
1709 * spamming the client. Strictly speaking this variable
1710 * tracks when we last received a packet from the client,
1711 * but as it is waiting, it will not send us any till we
1712 * start sending them data. */
1713 cs->last_packet = std::chrono::steady_clock::now();
1714 }
1715 break;
1716
1717 case NetworkClientSocket::STATUS_MAP:
1718 /* Downloading the map... this is the amount of time since starting the saving. */
1719 if (lag > _settings_client.network.max_download_time) {
1720 IConsolePrint(CC_WARNING, "Client #{} (IP: {}) is dropped because it took longer than {} ticks to download the map.", cs->client_id, cs->GetClientIP(), _settings_client.network.max_download_time);
1721 cs->SendError(NETWORK_ERROR_TIMEOUT_MAP);
1722 continue;
1723 }
1724 break;
1725
1726 case NetworkClientSocket::STATUS_DONE_MAP:
1727 case NetworkClientSocket::STATUS_PRE_ACTIVE:
1728 /* The map has been sent, so this is for loading the map and syncing up. */
1729 if (lag > _settings_client.network.max_join_time) {
1730 IConsolePrint(CC_WARNING, "Client #{} (IP: {}) is dropped because it took longer than {} ticks to join.", cs->client_id, cs->GetClientIP(), _settings_client.network.max_join_time);
1731 cs->SendError(NETWORK_ERROR_TIMEOUT_JOIN);
1732 continue;
1733 }
1734 break;
1735
1736 case NetworkClientSocket::STATUS_AUTH_GAME:
1737 case NetworkClientSocket::STATUS_AUTH_COMPANY:
1738 /* These don't block? */
1739 if (lag > _settings_client.network.max_password_time) {
1740 IConsolePrint(CC_WARNING, "Client #{} (IP: {}) is dropped because it took longer than {} ticks to enter the password.", cs->client_id, cs->GetClientIP(), _settings_client.network.max_password_time);
1741 cs->SendError(NETWORK_ERROR_TIMEOUT_PASSWORD);
1742 continue;
1743 }
1744 break;
1745
1746 case NetworkClientSocket::STATUS_END:
1747 /* Bad server/code. */
1748 NOT_REACHED();
1749 }
1750
1751 if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) {
1752 /* Check if we can send command, and if we have anything in the queue */
1753 NetworkHandleCommandQueue(cs);
1754
1755 /* Send an updated _frame_counter_max to the client */
1756 if (send_frame) cs->SendFrame();
1757
1758 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
1759 /* Send a sync-check packet */
1760 if (send_sync) cs->SendSync();
1761 #endif
1762 }
1763 }
1764 }
1765
1767 void NetworkServerYearlyLoop()
1768 {
1769 NetworkCheckRestartMap();
1770 NetworkAdminUpdate(ADMIN_FREQUENCY_ANUALLY);
1771 }
1772
1774 void NetworkServerMonthlyLoop()
1775 {
1776 NetworkAutoCleanCompanies();
1777 NetworkAdminUpdate(ADMIN_FREQUENCY_MONTHLY);
1778 if ((_cur_month % 3) == 0) NetworkAdminUpdate(ADMIN_FREQUENCY_QUARTERLY);
1779 }
1780
1782 void NetworkServerDailyLoop()
1783 {
1784 NetworkAdminUpdate(ADMIN_FREQUENCY_DAILY);
1785 if ((_date % 7) == 3) NetworkAdminUpdate(ADMIN_FREQUENCY_WEEKLY);
1786 }
1787
1792 const std::string &ServerNetworkGameSocketHandler::GetClientIP()
1793 {
1794 return this->client_address.GetHostname();
1795 }
1796
1798 void NetworkServerShowStatusToConsole()
1799 {
1800 static const char * const stat_str[] = {
1801 "inactive",
1802 "checking NewGRFs",
1803 "authorizing (server password)",
1804 "authorizing (company password)",
1805 "authorized",
1806 "waiting",
1807 "loading map",
1808 "map done",
1809 "ready",
1810 "active"
1811 };
1812 static_assert(lengthof(stat_str) == NetworkClientSocket::STATUS_END);
1813
1814 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1815 NetworkClientInfo *ci = cs->GetInfo();
1816 if (ci == nullptr) continue;
1817 uint lag = NetworkCalculateLag(cs);
1818 const char *status;
1819
1820 status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
1821 IConsolePrint(CC_INFO, "Client #{} name: '{}' status: '{}' frame-lag: {} company: {} IP: {}",
1822 cs->client_id, ci->client_name.c_str(), status, lag,
1823 ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
1824 cs->GetClientIP());
1825 }
1826 }
1827
1831 void NetworkServerSendConfigUpdate()
1832 {
1833 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1834 if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) cs->SendConfigUpdate();
1835 }
1836 }
1837
1839 void NetworkServerUpdateGameInfo()
1840 {
1841 if (_network_server) FillStaticNetworkServerGameInfo();
1842 }
1843
1849 void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded)
1850 {
1851 if (NetworkCompanyIsPassworded(company_id) == passworded) return;
1852
1853 SB(_network_company_passworded, company_id, 1, !!passworded);
1854 SetWindowClassesDirty(WC_COMPANY);
1855
1856 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1857 if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) cs->SendCompanyUpdate();
1858 }
1859
1860 NetworkAdminCompanyUpdate(Company::GetIfValid(company_id));
1861 }
1862
1869 void NetworkServerDoMove(ClientID client_id, CompanyID company_id)
1870 {
1871 /* Only allow non-dedicated servers and normal clients to be moved */
1872 if (client_id == CLIENT_ID_SERVER && _network_dedicated) return;
1873
1874 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
1875
1876 /* No need to waste network resources if the client is in the company already! */
1877 if (ci->client_playas == company_id) return;
1878
1879 ci->client_playas = company_id;
1880
1881 if (client_id == CLIENT_ID_SERVER) {
1882 SetLocalCompany(company_id);
1883 } else {
1884 NetworkClientSocket *cs = NetworkClientSocket::GetByClientID(client_id);
1885 /* When the company isn't authorized we can't move them yet. */
1886 if (cs->status < NetworkClientSocket::STATUS_AUTHORIZED) return;
1887 cs->SendMove(client_id, company_id);
1888 }
1889
1890 /* announce the client's move */
1891 NetworkUpdateClientInfo(client_id);
1892
1893 NetworkAction action = (company_id == COMPANY_SPECTATOR) ? NETWORK_ACTION_COMPANY_SPECTATOR : NETWORK_ACTION_COMPANY_JOIN;
1894 NetworkServerSendChat(action, DESTTYPE_BROADCAST, 0, "", client_id, company_id + 1);
1895
1896 InvalidateWindowData(WC_CLIENT_LIST, 0);
1897 }
1898
1905 void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const std::string &string)
1906 {
1907 NetworkClientSocket::GetByClientID(client_id)->SendRConResult(colour_code, string);
1908 }
1909
1915 void NetworkServerKickClient(ClientID client_id, const std::string &reason)
1916 {
1917 if (client_id == CLIENT_ID_SERVER) return;
1918 NetworkClientSocket::GetByClientID(client_id)->SendError(NETWORK_ERROR_KICKED, reason);
1919 }
1920
1927 uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, const std::string &reason)
1928 {
1929 return NetworkServerKickOrBanIP(NetworkClientSocket::GetByClientID(client_id)->GetClientIP(), ban, reason);
1930 }
1931
1938 uint NetworkServerKickOrBanIP(const std::string &ip, bool ban, const std::string &reason)
1939 {
1940 /* Add address to ban-list */
1941 if (ban) {
1942 bool contains = false;
1943 for (const auto &iter : _network_ban_list) {
1944 if (iter == ip) {
1945 contains = true;
1946 break;
1947 }
1948 }
1949 if (!contains) _network_ban_list.emplace_back(ip);
1950 }
1951
1952 uint n = 0;
1953
1954 /* There can be multiple clients with the same IP, kick them all but don't kill the server,
1955 * or the client doing the rcon. The latter can't be kicked because kicking frees closes
1956 * and subsequently free the connection related instances, which we would be reading from
1957 * and writing to after returning. So we would read or write data from freed memory up till
1958 * the segfault triggers. */
1959 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
1960 if (cs->client_id == CLIENT_ID_SERVER) continue;
1961 if (cs->client_id == _redirect_console_to_client) continue;
1962 if (cs->client_address.IsInNetmask(ip)) {
1963 NetworkServerKickClient(cs->client_id, reason);
1964 n++;
1965 }
1966 }
1967
1968 return n;
1969 }
1970
1976 bool NetworkCompanyHasClients(CompanyID company)
1977 {
1978 for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
1979 if (ci->client_playas == company) return true;
1980 }
1981 return false;
1982 }
1983
1984
1990 std::string ServerNetworkGameSocketHandler::GetClientName() const
1991 {
1992 const NetworkClientInfo *ci = this->GetInfo();
1993 if (ci != nullptr && !ci->client_name.empty()) return ci->client_name;
1994
1995 return fmt::format("Client #{}", this->client_id);
1996 }
1997
2001 void NetworkPrintClients()
2002 {
2003 for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
2004 if (_network_server) {
2005 IConsolePrint(CC_INFO, "Client #{} name: '{}' company: {} IP: {}",
2006 ci->client_id,
2007 ci->client_name,
2008 ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
2009 ci->client_id == CLIENT_ID_SERVER ? "server" : NetworkClientSocket::GetByClientID(ci->client_id)->GetClientIP());
2010 } else {
2011 IConsolePrint(CC_INFO, "Client #{} name: '{}' company: {}",
2012 ci->client_id,
2013 ci->client_name,
2014 ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0));
2015 }
2016 }
2017 }
2018
2024 void NetworkServerNewCompany(const Company *c, NetworkClientInfo *ci)
2025 {
2026 assert(c != nullptr);
2027
2028 if (!_network_server) return;
2029
2030 _network_company_states[c->index].months_empty = 0;
2031 _network_company_states[c->index].password.clear();
2032 NetworkServerUpdateCompanyPassworded(c->index, false);
2033
2034 if (ci != nullptr) {
2035 /* ci is nullptr when replaying, or for AIs. In neither case there is a client. */
2036 ci->client_playas = c->index;
2037 NetworkUpdateClientInfo(ci->client_id);
2038 NetworkSendCommand(0, 0, 0, CMD_RENAME_PRESIDENT, nullptr, ci->client_name, c->index);
2039 }
2040
2041 /* Announce new company on network. */
2042 NetworkAdminCompanyInfo(c, true);
2043
2044 if (ci != nullptr) {
2045 /* ci is nullptr when replaying, or for AIs. In neither case there is a client.
2046 We need to send Admin port update here so that they first know about the new company
2047 and then learn about a possibly joining client (see FS#6025) */
2048 NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, c->index + 1);
2049 }
2050 }
ServerNetworkGameSocketHandler::Receive_CLIENT_QUIT
NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p) override
The client is quitting the game.
Definition: network_server.cpp:1075
VEH_AIRCRAFT
@ VEH_AIRCRAFT
Aircraft vehicle type.
Definition: vehicle_type.h:27
NetworkServerKickOrBanIP
uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, const std::string &reason)
Ban, or kick, everyone joined from the given client's IP.
Definition: network_server.cpp:1927
SerializeNetworkGameInfo
void SerializeNetworkGameInfo(Packet *p, const NetworkServerGameInfo *info, bool send_newgrf_names)
Serializes the NetworkGameInfo struct to the packet.
Definition: game_info.cpp:185
NetworkServerSendChat
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.
Definition: network_server.cpp:1153
NetworkCompanyStats::num_station
uint16 num_station[NETWORK_VEH_END]
How many stations are there of this type?
Definition: network_type.h:67
NetworkCompanyStats
Simple calculated statistics of a company.
Definition: network_type.h:65
CommandContainer::cmd
uint32 cmd
command being executed.
Definition: command_type.h:483
PACKET_SERVER_GAME_INFO
@ PACKET_SERVER_GAME_INFO
Information about the server.
Definition: tcp_game.h:46
DestType
DestType
Destination of our chat messages.
Definition: network_type.h:89
CC_INFO
static const TextColour CC_INFO
Colour for information lines.
Definition: console_type.h:27
InvalidateWindowData
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-...
Definition: window.cpp:3218
NetworkSettings::max_password_time
uint16 max_password_time
maximum amount of time, in game ticks, a client may take to enter the password
Definition: settings_type.h:270
NETWORK_RECV_STATUS_CLIENT_QUIT
@ NETWORK_RECV_STATUS_CLIENT_QUIT
The connection is lost gracefully. Other clients are already informed of this leaving client.
Definition: core.h:27
_frame_counter
uint32 _frame_counter
The current frame.
Definition: network.cpp:70
GameCreationSettings::generation_seed
uint32 generation_seed
noise seed for world generation
Definition: settings_type.h:304
FT_SCENARIO
@ FT_SCENARIO
old or new scenario
Definition: fileio_type.h:19
PACKET_SERVER_ERROR
@ PACKET_SERVER_ERROR
Server sending an error message to the client.
Definition: tcp_game.h:39
Packet::Size
size_t Size() const
Get the number of bytes in the packet.
Definition: packet.cpp:249
NetworkServerSendConfigUpdate
void NetworkServerSendConfigUpdate()
Send Config Update.
Definition: network_server.cpp:1831
NetworkAdminUpdate
void NetworkAdminUpdate(AdminUpdateFrequency freq)
Send (push) updates to the admin network as they have registered for these updates.
Definition: network_admin.cpp:979
INVALID_CLIENT_ID
@ INVALID_CLIENT_ID
Client is not part of anything.
Definition: network_type.h:48
SM_START_HEIGHTMAP
@ SM_START_HEIGHTMAP
Load a heightmap and start a new game from it.
Definition: openttd.h:37
usererror
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:103
SerializeGRFIdentifier
void SerializeGRFIdentifier(Packet *p, const GRFIdentifier *grf)
Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet.
Definition: game_info.cpp:363
SM_LOAD_GAME
@ SM_LOAD_GAME
Load game, Play Scenario.
Definition: openttd.h:31
NetworkSettings::autoclean_unprotected
uint8 autoclean_unprotected
remove passwordless companies after this many months
Definition: settings_type.h:289
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:348
NetworkServerShowStatusToConsole
void NetworkServerShowStatusToConsole()
Show the status message of all clients on the console.
Definition: network_server.cpp:1798
NetworkClientInfo::client_name
std::string client_name
Name of the client.
Definition: network_base.h:26
NetworkCheckRestartMap
static void NetworkCheckRestartMap()
Check if we want to restart the map.
Definition: network_server.cpp:1444
PacketWriter::cs
ServerNetworkGameSocketHandler * cs
Socket we are associated with.
Definition: network_server.cpp:58
NetworkAdminChat
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).
Definition: network_admin.cpp:900
NetworkSettings::max_clients
uint8 max_clients
maximum amount of clients
Definition: settings_type.h:293
_cur_year
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:26
ServerNetworkGameSocketHandler::SendGameInfo
NetworkRecvStatus SendGameInfo()
Send the client information about the server.
Definition: network_server.cpp:355
lock
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:34
NetworkClientInfo::client_playas
CompanyID client_playas
As which company is this client playing (CompanyID)
Definition: network_base.h:27
ServerNetworkGameSocketHandler::SendChat
NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const std::string &msg, int64 data)
Send a chat message.
Definition: network_server.cpp:657
FACIL_TRUCK_STOP
@ FACIL_TRUCK_STOP
Station with truck stops.
Definition: station_type.h:53
ServerNetworkGameSocketHandler::STATUS_PRE_ACTIVE
@ STATUS_PRE_ACTIVE
The client is catching up the delayed frames.
Definition: network_server.h:60
Station
Station data structure.
Definition: station_base.h:447
Packet::Send_bytes
size_t Send_bytes(const byte *begin, const byte *end)
Send as many of the bytes as possible in the packet.
Definition: packet.cpp:196
CommandQueue::Count
uint Count() const
Get the number of items in the queue.
Definition: tcp_game.h:149
_date_fract
DateFract _date_fract
Fractional part of the day.
Definition: date.cpp:29
PACKET_SERVER_CONFIG_UPDATE
@ PACKET_SERVER_CONFIG_UPDATE
Some network configuration important to the client changed.
Definition: tcp_game.h:115
_network_server
bool _network_server
network-server is active
Definition: network.cpp:57
_network_company_passworded
CompanyMask _network_company_passworded
Bitmask of the password status of all companies.
Definition: network.cpp:79
CRR_AUTOCLEAN
@ CRR_AUTOCLEAN
The company is removed due to autoclean.
Definition: company_type.h:57
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition: network_type.h:99
CommandPacket::frame
uint32 frame
the frame in which this packet is executed
Definition: network_internal.h:112
SPS_CLOSED
@ SPS_CLOSED
The connection got closed.
Definition: tcp.h:25
NetworkGameSocketHandler::last_packet
std::chrono::steady_clock::time_point last_packet
Time we received the last frame.
Definition: tcp_game.h:505
WC_CLIENT_LIST
@ WC_CLIENT_LIST
Client list; Window numbers:
Definition: window_type.h:470
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
NetworkGameSocketHandler::SetInfo
void SetInfo(NetworkClientInfo *info)
Sets the client info for this socket handler.
Definition: tcp_game.h:520
ServerNetworkGameSocketHandler::Receive_CLIENT_SET_PASSWORD
NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p) override
Set the password for the clients current company: string The password.
Definition: network_server.cpp:1292
NETWORK_CHAT_LENGTH
static const uint NETWORK_CHAT_LENGTH
The maximum length of a chat message, in bytes including '\0'.
Definition: config.h:66
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
NetworkClientInfo::join_date
Date join_date
Gamedate the client has joined.
Definition: network_base.h:28
NetworkAutoCleanCompanies
static void NetworkAutoCleanCompanies()
Check if the server has autoclean_companies activated Two things happen: 1) If a company is not prote...
Definition: network_server.cpp:1472
ADMIN_FREQUENCY_DAILY
@ ADMIN_FREQUENCY_DAILY
The admin gets information about this on a daily basis.
Definition: tcp_admin.h:91
PACKET_SERVER_NEWGAME
@ PACKET_SERVER_NEWGAME
The server is preparing to start a new game.
Definition: tcp_game.h:118
NetworkPrintClients
void NetworkPrintClients()
Print all the clients to the console.
Definition: network_server.cpp:2001
SetLocalCompany
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...
Definition: company_cmd.cpp:102
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:39
ServerNetworkGameSocketHandler::receive_limit
size_t receive_limit
Amount of bytes that we can receive at this moment.
Definition: network_server.h:70
PacketWriter::Write
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: network_server.cpp:159
ServerNetworkGameSocketHandler::last_token_frame
uint32 last_token_frame
The last frame we received the right token.
Definition: network_server.h:67
MAX_CLIENTS
static const uint MAX_CLIENTS
How many clients can we have.
Definition: network_type.h:14
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
CommandContainer::p2
uint32 p2
parameter p2.
Definition: command_type.h:482
ServerNetworkGameSocketHandler::ReceivePacket
virtual Packet * ReceivePacket() override
Receives a packet for the given client.
Definition: network_server.cpp:234
NetworkGameSocketHandler::last_frame_server
uint32 last_frame_server
Last frame the server has executed.
Definition: tcp_game.h:503
DESTTYPE_TEAM
@ DESTTYPE_TEAM
Send message/notice to everyone playing the same company (Team)
Definition: network_type.h:91
GRFConfig::ident
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
Definition: newgrf_config.h:163
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
PACKET_SERVER_WAIT
@ PACKET_SERVER_WAIT
Server tells the client there are some people waiting for the map as well.
Definition: tcp_game.h:75
ServerNetworkGameSocketHandler::STATUS_MAP
@ STATUS_MAP
The client is downloading the map.
Definition: network_server.h:58
PACKET_SERVER_JOIN
@ PACKET_SERVER_JOIN
Tells clients that a new client has joined.
Definition: tcp_game.h:82
Company::IsValidHumanID
static bool IsValidHumanID(size_t index)
Is this company a valid company, not controlled by a NoAI program?
Definition: company_base.h:147
NetworkSyncCommandQueue
void NetworkSyncCommandQueue(NetworkClientSocket *cs)
Sync our local command queue to the command queue of the given socket.
Definition: network_command.cpp:178
ADMIN_FREQUENCY_MONTHLY
@ ADMIN_FREQUENCY_MONTHLY
The admin gets information about this on a monthly basis.
Definition: tcp_admin.h:93
ServerNetworkGameSocketHandler::STATUS_MAP_WAIT
@ STATUS_MAP_WAIT
The client is waiting as someone else is downloading the map.
Definition: network_server.h:57
NetworkServerMonthlyLoop
void NetworkServerMonthlyLoop()
Monthly "callback".
Definition: network_server.cpp:1774
NetworkSettings::autoclean_protected
uint8 autoclean_protected
remove the password from passworded companies after this many months
Definition: settings_type.h:290
WC_COMPANY
@ WC_COMPANY
Company view; Window numbers:
Definition: window_type.h:361
GetNetworkErrorMsg
StringID GetNetworkErrorMsg(NetworkErrorCode err)
Retrieve the string id of an internal error number.
Definition: network.cpp:297
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
_network_game_info
NetworkServerGameInfo _network_game_info
Information about our game.
Definition: game_info.cpp:37
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:221
NetworkCompanyState::months_empty
uint16 months_empty
How many months the company is empty.
Definition: network_type.h:74
_redirect_console_to_client
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
Definition: network.cpp:63
NETWORK_CLIENT_NAME_LENGTH
static const uint NETWORK_CLIENT_NAME_LENGTH
The maximum length of a client's name, in bytes including '\0'.
Definition: config.h:63
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
NetworkGameSocketHandler
Base socket handler for all TCP sockets.
Definition: tcp_game.h:153
Packet::Send_uint8
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:129
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:196
NetworkSendCommand
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const std::string &text, CompanyID company)
Prepare a DoCommand to be send over the network.
Definition: network_command.cpp:136
GENERATE_NEW_SEED
static const uint32 GENERATE_NEW_SEED
Create a new random seed.
Definition: genworld.h:24
ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK
NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p) override
Tell the server that we are done receiving/loading the map.
Definition: network_server.cpp:937
NetworkAdminClientQuit
void NetworkAdminClientQuit(ClientID client_id)
Notify the admin network that a client quit (if they have opt in for the respective update).
Definition: network_admin.cpp:824
CC_DEFAULT
static const TextColour CC_DEFAULT
Default colour of the console.
Definition: console_type.h:23
_frame_counter_max
uint32 _frame_counter_max
To where we may go with our clients.
Definition: network.cpp:69
NetworkServerSendRcon
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const std::string &string)
Send an rcon reply to the client.
Definition: network_server.cpp:1905
ServerNetworkGameSocketHandler::STATUS_ACTIVE
@ STATUS_ACTIVE
The client is active within in the game.
Definition: network_server.h:61
NetworkServer_Tick
void NetworkServer_Tick(bool send_frame)
This is called every tick if this is a _network_server.
Definition: network_server.cpp:1635
ServerNetworkGameSocketHandler::SendCommand
NetworkRecvStatus SendCommand(const CommandPacket *cp)
Send a command to the client to execute.
Definition: network_server.cpp:637
network_base.h
SmallMap< NetworkAddress, SOCKET >
PACKET_SERVER_MAP_SIZE
@ PACKET_SERVER_MAP_SIZE
Server tells the client what the (compressed) size of the map is.
Definition: tcp_game.h:77
FillStaticNetworkServerGameInfo
void FillStaticNetworkServerGameInfo()
Fill a NetworkServerGameInfo structure with the static content, or things that are so static they can...
Definition: game_info.cpp:123
NetworkAdminClientInfo
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).
Definition: network_admin.cpp:795
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:576
PacketWriter::Finish
void Finish() override
Prepare everything to finish writing the savegame.
Definition: network_server.cpp:182
Packet::Send_uint32
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:150
Packet::AddToQueue
static void AddToQueue(Packet **queue, Packet *packet)
Add the given Packet to the end of the queue of packets.
Definition: packet.cpp:59
PACKET_SERVER_MAP_DONE
@ PACKET_SERVER_MAP_DONE
Server tells it has just sent the last bits of the map to the client.
Definition: tcp_game.h:79
SpecializedStation< Station, false >::Iterate
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
Definition: base_station_base.h:270
Packet::Recv_string
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...
Definition: packet.cpp:380
NetworkTCPSocketHandler::ReceivePacket
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:144
NetworkSettings::max_download_time
uint16 max_download_time
maximum amount of time, in game ticks, a client may take to download the map
Definition: settings_type.h:269
CommandQueue::Append
void Append(CommandPacket *p)
Append a CommandPacket at the end of the queue.
Definition: network_command.cpp:57
Packet::PopFromQueue
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...
Definition: packet.cpp:71
NetworkMakeClientNameUnique
bool NetworkMakeClientNameUnique(std::string &name)
Check whether a name is unique, and otherwise try to make it unique.
Definition: network_server.cpp:1541
Pool::MAX_SIZE
static constexpr size_t MAX_SIZE
Make template parameter accessible from outside.
Definition: pool_type.hpp:85
DECLARE_POSTFIX_INCREMENT
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
Definition: enum_type.hpp:14
NetworkClientInfo::GetByClientID
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it's client-identifier.
Definition: network.cpp:112
ADMIN_FREQUENCY_QUARTERLY
@ ADMIN_FREQUENCY_QUARTERLY
The admin gets information about this on a quarterly basis.
Definition: tcp_admin.h:94
ServerNetworkGameSocketHandler::Receive_CLIENT_COMPANY_PASSWORD
NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p) override
Send a password to the server to authorize uint8 Password type (see NetworkPasswordType).
Definition: network_server.cpp:895
PacketWriter::PrependQueue
void PrependQueue()
Prepend the current packet to the queue.
Definition: network_server.cpp:149
CommandPacket::company
CompanyID company
company that is executing the command
Definition: network_internal.h:111
FileToSaveLoad::abstract_ftype
AbstractFileType abstract_ftype
Abstract type of file (scenario, heightmap, etc).
Definition: saveload.h:355
FACIL_BUS_STOP
@ FACIL_BUS_STOP
Station with bus stops.
Definition: station_type.h:54
ServerNetworkGameSocketHandler::Receive_CLIENT_NEWGRFS_CHECKED
NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p) override
Tell the server that we have the required GRFs.
Definition: network_server.cpp:780
ServerNetworkGameSocketHandler::GetClientName
std::string GetClientName() const
Get the name of the client, if the user did not send it yet, Client ID is used.
Definition: network_server.cpp:1990
TCPListenHandler
Template for TCP listeners.
Definition: tcp_listen.h:28
ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler
~ServerNetworkGameSocketHandler()
Clear everything related to this client.
Definition: network_server.cpp:223
CommandContainer::p1
uint32 p1
parameter p1.
Definition: command_type.h:481
COMPANY_NEW_COMPANY
@ COMPANY_NEW_COMPANY
The client wants a new company.
Definition: company_type.h:34
NetworkSettings::server_admin_chat
bool server_admin_chat
allow private chat for the server to be distributed to the admin network
Definition: settings_type.h:275
ServerNetworkGameSocketHandler::STATUS_INACTIVE
@ STATUS_INACTIVE
The client is not connected nor active.
Definition: network_server.h:52
PacketWriter::TransferToNetworkQueue
bool TransferToNetworkQueue(ServerNetworkGameSocketHandler *socket)
Transfer all packets from here to the network's queue while holding the lock on our mutex.
Definition: network_server.cpp:121
ServerNetworkGameSocketHandler::Send
static void Send()
Send the packets for the server sockets.
Definition: network_server.cpp:318
NetworkCompanyStats::num_vehicle
uint16 num_vehicle[NETWORK_VEH_END]
How many vehicles are there of this type?
Definition: network_type.h:66
Packet::Recv_uint32
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:335
PACKET_SERVER_NEED_GAME_PASSWORD
@ PACKET_SERVER_NEED_GAME_PASSWORD
Server requests the (hashed) game password.
Definition: tcp_game.h:64
PacketWriter
Writing a savegame directly to a number of packets.
Definition: network_server.cpp:57
NetworkServerUpdateCompanyPassworded
void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded)
Tell that a particular company is (not) passworded.
Definition: network_server.cpp:1849
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition: network_internal.h:107
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
ServerNetworkGameSocketHandler::SendSync
NetworkRecvStatus SendSync()
Request the client to sync.
Definition: network_server.cpp:620
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:155
PACKET_SERVER_SHUTDOWN
@ PACKET_SERVER_SHUTDOWN
The server is shutting down.
Definition: tcp_game.h:119
DoCommandP
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:541
PACKET_SERVER_NEED_COMPANY_PASSWORD
@ PACKET_SERVER_NEED_COMPANY_PASSWORD
Server requests the (hashed) company password.
Definition: tcp_game.h:66
ServerNetworkGameSocketHandler::SendConfigUpdate
NetworkRecvStatus SendConfigUpdate()
Send an update about the max company/spectator counts.
Definition: network_server.cpp:760
SM_NEWGAME
@ SM_NEWGAME
New Game --> 'Random game'.
Definition: openttd.h:27
ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_INFO
NetworkRecvStatus Receive_CLIENT_GAME_INFO(Packet *p) override
Request game information.
Definition: network_server.cpp:775
NetworkHandleCommandQueue
static void NetworkHandleCommandQueue(NetworkClientSocket *cs)
Handle the command-queue of a socket.
Definition: network_server.cpp:1622
NetworkSettings::bytes_per_frame
uint16 bytes_per_frame
how many bytes may, over a long period, be received per frame?
Definition: settings_type.h:265
NETWORK_RECV_STATUS_SERVER_ERROR
@ NETWORK_RECV_STATUS_SERVER_ERROR
The server told us we made an error.
Definition: core.h:29
PACKET_SERVER_COMPANY_UPDATE
@ PACKET_SERVER_COMPANY_UPDATE
Information (password) of a company changed.
Definition: tcp_game.h:114
GenerateCompanyPasswordHash
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.
Definition: network.cpp:175
GRFConfig::flags
uint8 flags
NOSAVE: GCF_Flags, bitset.
Definition: newgrf_config.h:173
NetworkSettings::restart_game_year
Year restart_game_year
year the server restarts
Definition: settings_type.h:294
SB
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.
Definition: bitmath_func.hpp:58
ClientID
ClientID
'Unique' identifier to be given to clients
Definition: network_type.h:47
PACKET_SERVER_BANNED
@ PACKET_SERVER_BANNED
The server has banned you.
Definition: tcp_game.h:35
ServerNetworkGameSocketHandler::SendMove
NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id)
Tell that a client moved to another company.
Definition: network_server.cpp:739
ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN
NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p) override
Try to join the server: string OpenTTD revision (norev000 if no revision).
Definition: network_server.cpp:801
ServerNetworkGameSocketHandler::SendJoin
NetworkRecvStatus SendJoin(ClientID client_id)
Tell that a client joined.
Definition: network_server.cpp:586
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:53
_sync_seed_1
uint32 _sync_seed_1
Seed to compare during sync checks.
Definition: network.cpp:73
ServerNetworkGameSocketHandler::SendFrame
NetworkRecvStatus SendFrame()
Tell the client that they may run to a particular frame.
Definition: network_server.cpp:597
ServerNetworkGameSocketHandler::SendRConResult
NetworkRecvStatus SendRConResult(uint16 colour, const std::string &command)
Send the result of a console action.
Definition: network_server.cpp:724
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
CMD_CLIENT_ID
@ CMD_CLIENT_ID
set p2 with the ClientID of the sending client.
Definition: command_type.h:398
PacketWriter::total_size
size_t total_size
Total size of the compressed savegame.
Definition: network_server.cpp:60
NetworkCompanyState::password
std::string password
The password for the company.
Definition: network_type.h:73
Packet::Send_uint16
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:139
NetworkTCPSocketHandler::SendPacket
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Definition: tcp.cpp:81
CLIENT_ID_SERVER
@ CLIENT_ID_SERVER
Servers always have this ID.
Definition: network_type.h:49
_network_company_states
NetworkCompanyState * _network_company_states
Statistics about some companies.
Definition: network.cpp:61
ServerNetworkGameSocketHandler::SendCompanyUpdate
NetworkRecvStatus SendCompanyUpdate()
Send an update about the company password states.
Definition: network_server.cpp:750
ServerNetworkGameSocketHandler::SendWelcome
NetworkRecvStatus SendWelcome()
Send the client a welcome message with some basic information.
Definition: network_server.cpp:465
_network_client_id
static ClientID _network_client_id
The identifier counter for new clients (is never decreased)
Definition: network_server.cpp:42
ServerNetworkGameSocketHandler::Receive_CLIENT_MOVE
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...
Definition: network_server.cpp:1360
CommandPacket::my_cmd
bool my_cmd
did the command originate from "me"
Definition: network_internal.h:113
ServerNetworkGameSocketHandler::SendError
NetworkRecvStatus SendError(NetworkErrorCode error, const std::string &reason={})
Send an error to the client, and close its connection.
Definition: network_server.cpp:370
ServerNetworkGameSocketHandler::status
ClientStatus status
Status of this client.
Definition: network_server.h:68
NetworkPopulateCompanyStats
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats)
Populate the company stats.
Definition: network_server.cpp:1390
TCP_MTU
static const uint16 TCP_MTU
Number of bytes we can pack in a single TCP packet.
Definition: config.h:47
FACIL_DOCK
@ FACIL_DOCK
Station with a dock.
Definition: station_type.h:56
network_server.h
_network_dedicated
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:59
ServerNetworkGameSocketHandler::savegame
struct PacketWriter * savegame
Writer used to write the savegame.
Definition: network_server.h:72
Packet
Internal entity of a packet.
Definition: packet.h:44
NetworkSettings::max_join_time
uint16 max_join_time
maximum amount of time, in game ticks, a client may take to sync up during joining
Definition: settings_type.h:268
FT_SAVEGAME
@ FT_SAVEGAME
old or new savegame
Definition: fileio_type.h:18
ServerNetworkGameSocketHandler::SendClientInfo
NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci)
Send the client information about a client.
Definition: network_server.cpp:341
NetworkGameSocketHandler::ReceiveCommand
const char * ReceiveCommand(Packet *p, CommandPacket *cp)
Receives a command from the network.
Definition: network_command.cpp:295
GetCommandFlags
CommandFlags GetCommandFlags(uint32 cmd)
Definition: command.cpp:393
ADMIN_FREQUENCY_ANUALLY
@ ADMIN_FREQUENCY_ANUALLY
The admin gets information about this on a yearly basis.
Definition: tcp_admin.h:95
NETWORK_REVISION_LENGTH
static const uint NETWORK_REVISION_LENGTH
The maximum length of the revision, in bytes including '\0'.
Definition: config.h:60
ServerNetworkGameSocketHandler::STATUS_DONE_MAP
@ STATUS_DONE_MAP
The client has downloaded the map.
Definition: network_server.h:59
NetworkCompanyIsPassworded
bool NetworkCompanyIsPassworded(CompanyID company_id)
Check if the company we want to join requires a password.
Definition: network.cpp:211
ServerNetworkGameSocketHandler::SendQuit
NetworkRecvStatus SendQuit(ClientID client_id)
Tell the client another client quit.
Definition: network_server.cpp:693
NetworkClientInfo::client_id
ClientID client_id
Client identifier (same as ClientState->client_id)
Definition: network_base.h:25
PACKET_SERVER_MOVE
@ PACKET_SERVER_MOVE
Server tells everyone that someone is moved to another company.
Definition: tcp_game.h:109
NetworkServerDailyLoop
void NetworkServerDailyLoop()
Daily "callback".
Definition: network_server.cpp:1782
NETWORK_PASSWORD_LENGTH
static const uint NETWORK_PASSWORD_LENGTH
The maximum length of the password, in bytes including '\0' (must be >= NETWORK_SERVER_ID_LENGTH)
Definition: config.h:61
GetCurrentNetworkServerGameInfo
const NetworkServerGameInfo * GetCurrentNetworkServerGameInfo()
Get the NetworkServerGameInfo structure with the latest information of the server.
Definition: game_info.cpp:143
PACKET_SERVER_FULL
@ PACKET_SERVER_FULL
The server is full and has no place for you.
Definition: tcp_game.h:34
IsNetworkCompatibleVersion
bool IsNetworkCompatibleVersion(std::string_view other)
Checks whether the given version string is compatible with our version.
Definition: game_info.cpp:90
Packet::Send_bool
void Send_bool(bool data)
Package a boolean in the packet.
Definition: packet.cpp:120
SlError
void NORETURN SlError(StringID string, const char *extra_msg)
Error handler.
Definition: saveload.cpp:332
NetworkAdminCompanyUpdate
void NetworkAdminCompanyUpdate(const Company *company)
Notify the admin network of company updates.
Definition: network_admin.cpp:873
NetworkSettings::server_name
std::string server_name
name of the server
Definition: settings_type.h:279
ServerNetworkGameSocketHandler::SendErrorQuit
NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno)
Tell the client another client quit with an error.
Definition: network_server.cpp:678
NetworkServerNewCompany
void NetworkServerNewCompany(const Company *c, NetworkClientInfo *ci)
Perform all the server specific administration of a new company.
Definition: network_server.cpp:2024
ServerNetworkGameSocketHandler
Class for handling the server side of the game connection.
Definition: network_server.h:24
NetworkGameSocketHandler::GetInfo
NetworkClientInfo * GetInfo() const
Gets the client info of this socket handler.
Definition: tcp_game.h:530
NetworkSettings::rcon_password
std::string rcon_password
password for rconsole (server side)
Definition: settings_type.h:281
_switch_mode
SwitchMode _switch_mode
The next mainloop command.
Definition: gfx.cpp:46
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
PacketWriter::AppendQueue
void AppendQueue()
Append the current packet to the queue.
Definition: network_server.cpp:140
NetworkSocketHandler::HasClientQuit
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
Definition: core.h:68
ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND
NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p) override
The client has done a command and wants us to handle it.
Definition: network_server.cpp:982
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:99
NETWORK_RCONCOMMAND_LENGTH
static const uint NETWORK_RCONCOMMAND_LENGTH
The maximum length of a rconsole command, in bytes including '\0'.
Definition: config.h:64
NetworkServerDoMove
void NetworkServerDoMove(ClientID client_id, CompanyID company_id)
Handle the tid-bits of moving a client from one company to another.
Definition: network_server.cpp:1869
Pool::PoolItem<&_vehicle_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
ServerNetworkGameSocketHandler::GetClientIP
const std::string & GetClientIP()
Get the IP address/hostname of the connected client.
Definition: network_server.cpp:1792
Pool
Base class for all pools.
Definition: pool_type.hpp:81
ServerNetworkGameSocketHandler::Receive_CLIENT_SET_NAME
NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p) override
Gives the client a new name: string New name of the client.
Definition: network_server.cpp:1306
PACKET_SERVER_ERROR_QUIT
@ PACKET_SERVER_ERROR_QUIT
A server tells that a client has hit an error and did quit.
Definition: tcp_game.h:125
Pool::PoolItem<&_company_pool >::GetNumItems
static size_t GetNumItems()
Returns number of valid items in the pool.
Definition: pool_type.hpp:367
Packet::GetPacketType
PacketType GetPacketType() const
Get the PacketType from this packet.
Definition: packet.cpp:287
ServerNetworkGameSocketHandler::SendNewGame
NetworkRecvStatus SendNewGame()
Tell the client we're starting a new game.
Definition: network_server.cpp:712
FACIL_TRAIN
@ FACIL_TRAIN
Station with train station.
Definition: station_type.h:52
CMD_ID_MASK
@ CMD_ID_MASK
mask for the command ID
Definition: command_type.h:382
network_udp.h
PacketWriter::~PacketWriter
~PacketWriter()
Make sure everything is cleaned up.
Definition: network_server.cpp:74
Packet::CanWriteToPacket
bool CanWriteToPacket(size_t bytes_to_write)
Is it safe to write to the packet, i.e.
Definition: packet.cpp:99
SpecializedVehicle< RoadVehicle, Type >::From
static RoadVehicle * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition: vehicle_base.h:1164
Packet::Send_uint64
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
Definition: packet.cpp:163
SaveFilter
Interface for filtering a savegame till it is written.
Definition: saveload_filter.h:60
GetDrawStringCompanyColour
TextColour GetDrawStringCompanyColour(CompanyID company)
Get the colour for DrawString-subroutines which matches the colour of the company.
Definition: company_cmd.cpp:131
NetworkSettings::bytes_per_frame_burst
uint16 bytes_per_frame_burst
how many bytes may, over a short period, be received?
Definition: settings_type.h:266
COMPANY_SPECTATOR
@ COMPANY_SPECTATOR
The client is spectating.
Definition: company_type.h:35
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
_network_clients_connected
byte _network_clients_connected
The amount of clients connected.
Definition: network.cpp:84
_file_to_saveload
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition: saveload.cpp:63
GRFConfig::next
struct GRFConfig * next
NOSAVE: Next item in the linked list.
Definition: newgrf_config.h:183
ServerNetworkGameSocketHandler::STATUS_NEWGRFS_CHECK
@ STATUS_NEWGRFS_CHECK
The client is checking NewGRFs.
Definition: network_server.h:53
SaveWithFilter
SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded)
Save the game using a (writer) filter.
Definition: saveload.cpp:3062
ServerNetworkGameSocketHandler::AllowConnection
static bool AllowConnection()
Whether an connection is allowed or not at this moment.
Definition: network_server.cpp:305
ServerNetworkGameSocketHandler::Receive_CLIENT_GETMAP
NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p) override
Request the map from the server.
Definition: network_server.cpp:916
NetworkSettings::server_password
std::string server_password
password for joining this server
Definition: settings_type.h:280
NetworkSettings::max_init_time
uint16 max_init_time
maximum amount of time, in game ticks, a client may take to initiate joining
Definition: settings_type.h:267
NetworkAddress::GetHostname
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition: address.cpp:23
PACKET_SERVER_SYNC
@ PACKET_SERVER_SYNC
Server tells the client what the random state should be.
Definition: tcp_game.h:93
Pool::PoolItem<&_networkclientsocket_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:307
NetworkServerKickClient
void NetworkServerKickClient(ClientID client_id, const std::string &reason)
Kick a single client.
Definition: network_server.cpp:1915
ServerNetworkGameSocketHandler::SendMap
NetworkRecvStatus SendMap()
This sends the map to the client.
Definition: network_server.cpp:540
FT_HEIGHTMAP
@ FT_HEIGHTMAP
heightmap file
Definition: fileio_type.h:20
game_info.h
PACKET_SERVER_MAP_BEGIN
@ PACKET_SERVER_MAP_BEGIN
Server tells the client that it is beginning to send the map.
Definition: tcp_game.h:76
Packet::Recv_uint8
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:306
ServerNetworkGameSocketHandler::SendNewGRFCheck
NetworkRecvStatus SendNewGRFCheck()
Send the check for the NewGRFs.
Definition: network_server.cpp:413
ServerNetworkGameSocketHandler::STATUS_AUTH_COMPANY
@ STATUS_AUTH_COMPANY
The client is authorizing with company password.
Definition: network_server.h:55
CMD_COMPANY_CTRL
@ CMD_COMPANY_CTRL
used in multiplayer to create a new companies etc.
Definition: command_type.h:281
PACKET_SERVER_CHAT
@ PACKET_SERVER_CHAT
Server distributing the message of a client (or itself).
Definition: tcp_game.h:101
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
PACKET_SERVER_RCON
@ PACKET_SERVER_RCON
Response of the executed command on the server.
Definition: tcp_game.h:105
PACKET_SERVER_QUIT
@ PACKET_SERVER_QUIT
A server tells that a client has quit.
Definition: tcp_game.h:123
ServerNetworkGameSocketHandler::SendNeedGamePassword
NetworkRecvStatus SendNeedGamePassword()
Request the game password.
Definition: network_server.cpp:433
NetworkGameSocketHandler::incoming_queue
CommandQueue incoming_queue
The command-queue awaiting handling.
Definition: tcp_game.h:504
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:132
NetworkGameSocketHandler::client_id
ClientID client_id
Client identifier.
Definition: tcp_game.h:501
_grfconfig
GRFConfig * _grfconfig
First item in list of current GRF set up.
Definition: newgrf_config.cpp:171
PacketWriter::PacketWriter
PacketWriter(ServerNetworkGameSocketHandler *cs)
Create the packet writer.
Definition: network_server.cpp:69
Packet::Send_string
void Send_string(const std::string_view data)
Sends a string over the network.
Definition: packet.cpp:181
MILLISECONDS_PER_TICK
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
Definition: gfx_type.h:310
NetworkServerChangeClientName
bool NetworkServerChangeClientName(ClientID client_id, const std::string &new_name)
Change the client name of the given client.
Definition: network_server.cpp:1581
PACKET_SERVER_COMMAND
@ PACKET_SERVER_COMMAND
Server distributes a command to (all) the clients.
Definition: tcp_game.h:97
ServerNetworkGameSocketHandler::Receive_CLIENT_ACK
NetworkRecvStatus Receive_CLIENT_ACK(Packet *p) override
Tell the server we are done with this frame: uint32 Current frame counter of the client.
Definition: network_server.cpp:1097
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
NetworkAdminClientUpdate
void NetworkAdminClientUpdate(const NetworkClientInfo *ci)
Notify the admin network of a client update (if they did opt in for the respective update).
Definition: network_admin.cpp:811
PACKET_SERVER_FRAME
@ PACKET_SERVER_FRAME
Server tells the client what frame it is in, and thus to where the client may progress.
Definition: tcp_game.h:91
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
_network_ban_list
StringList _network_ban_list
The banned clients.
Definition: network.cpp:67
ClientSettings::network
NetworkSettings network
settings related to the network
Definition: settings_type.h:594
Packet::Recv_uint64
uint64 Recv_uint64()
Read a 64 bits integer from the packet.
Definition: packet.cpp:352
OrderBackup::ResetUser
static void ResetUser(uint32 user)
Reset an user's OrderBackup if needed.
Definition: order_backup.cpp:164
NetworkSettings::autoclean_novehicles
uint8 autoclean_novehicles
remove companies with no vehicles after this many months
Definition: settings_type.h:291
DESTTYPE_BROADCAST
@ DESTTYPE_BROADCAST
Send message/notice to all clients (All)
Definition: network_type.h:90
PacketWriter::mutex
std::mutex mutex
Mutex for making threaded saving safe.
Definition: network_server.cpp:62
ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler
ServerNetworkGameSocketHandler(SOCKET s)
Create a new socket for the server side of the game connection.
Definition: network_server.cpp:208
ServerNetworkGameSocketHandler::Receive_CLIENT_RCON
NetworkRecvStatus Receive_CLIENT_RCON(Packet *p) override
Send an RCon command to the server: string RCon password.
Definition: network_server.cpp:1338
RoadVehicle::IsBus
bool IsBus() const
Check whether a roadvehicle is a bus.
Definition: roadveh_cmd.cpp:79
ServerNetworkGameSocketHandler::client_address
NetworkAddress client_address
IP-address of the client (so they can be banned)
Definition: network_server.h:73
IConsoleCmdExec
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
Execute a given command passed to us.
Definition: console.cpp:302
ProcessAsyncSaveFinish
void ProcessAsyncSaveFinish()
Handle async save finishes.
Definition: saveload.cpp:408
PacketWriter::Destroy
void Destroy()
Begin the destruction of this packet writer.
Definition: network_server.cpp:99
NetworkSettings::max_lag_time
uint16 max_lag_time
maximum amount of time, in game ticks, a client may be lagging behind the server
Definition: settings_type.h:271
ServerNetworkGameSocketHandler::Receive_CLIENT_CHAT
NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p) override
Sends a chat-packet to the server: uint8 ID of the action (see NetworkAction).
Definition: network_server.cpp:1264
PACKET_SERVER_CLIENT_INFO
@ PACKET_SERVER_CLIENT_INFO
Server sends you information about a client.
Definition: tcp_game.h:71
NETWORK_RECV_STATUS_OKAY
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition: core.h:23
NetworkGameSocketHandler::SendCommand
void SendCommand(Packet *p, const CommandPacket *cp)
Sends a command over the network.
Definition: network_command.cpp:320
CCA_DELETE
@ CCA_DELETE
Delete a company.
Definition: company_type.h:67
ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_PASSWORD
NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p) override
Send a password to the server to authorize: uint8 Password type (see NetworkPasswordType).
Definition: network_server.cpp:871
ADMIN_FREQUENCY_WEEKLY
@ ADMIN_FREQUENCY_WEEKLY
The admin gets information about this on a weekly basis.
Definition: tcp_admin.h:92
DESTTYPE_CLIENT
@ DESTTYPE_CLIENT
Send message/notice to only a certain client (Private)
Definition: network_type.h:92
NetworkServerUpdateGameInfo
void NetworkServerUpdateGameInfo()
Update the server's NetworkServerGameInfo due to changes in settings.
Definition: network_server.cpp:1839
PACKET_SERVER_WELCOME
@ PACKET_SERVER_WELCOME
Server welcomes you and gives you your ClientID.
Definition: tcp_game.h:70
ServerNetworkGameSocketHandler::SendShutdown
NetworkRecvStatus SendShutdown()
Tell the client we're shutting down.
Definition: network_server.cpp:704
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
Pool::PoolItem<&_company_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:326
FACIL_AIRPORT
@ FACIL_AIRPORT
Station with an airport.
Definition: station_type.h:55
CMD_SERVER
@ CMD_SERVER
the command can only be initiated by the server
Definition: command_type.h:391
CMD_SPECTATOR
@ CMD_SPECTATOR
the command may be initiated by a spectator
Definition: command_type.h:392
ServerNetworkGameSocketHandler::last_token
byte last_token
The last random token we did send to verify the client is listening.
Definition: network_server.h:66
PacketWriter::exit_sig
std::condition_variable exit_sig
Signal for threaded destruction of this packet writer.
Definition: network_server.cpp:63
network_admin.h
NetworkServerGameInfo::clients_on
byte clients_on
Current count of clients on server.
Definition: game_info.h:103
NetworkSettings::max_commands_in_queue
uint16 max_commands_in_queue
how many commands may there be in the incoming queue before dropping the connection?
Definition: settings_type.h:264
CC_WARNING
static const TextColour CC_WARNING
Colour for warning lines.
Definition: console_type.h:25
MAX_CLIENT_SLOTS
static const uint MAX_CLIENT_SLOTS
The number of slots; must be at least 1 more than MAX_CLIENTS.
Definition: network_type.h:21
NetworkServerSetCompanyPassword
void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &password, bool already_hashed)
Set/Reset a company password on the server end.
Definition: network_server.cpp:1605
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
Company
Definition: company_base.h:115
_cur_month
Month _cur_month
Current month (0..11)
Definition: date.cpp:27
ServerNetworkGameSocketHandler::SendNeedCompanyPassword
NetworkRecvStatus SendNeedCompanyPassword()
Request the company password.
Definition: network_server.cpp:448
PACKET_SERVER_CHECK_NEWGRFS
@ PACKET_SERVER_CHECK_NEWGRFS
Server sends NewGRF IDs and MD5 checksums for the client to check.
Definition: tcp_game.h:60
NetworkServerYearlyLoop
void NetworkServerYearlyLoop()
Yearly "callback".
Definition: network_server.cpp:1767
SetWindowClassesDirty
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition: window.cpp:3146
CMD_RENAME_PRESIDENT
@ CMD_RENAME_PRESIDENT
change the president name
Definition: command_type.h:247
SL_OK
@ SL_OK
completed successfully
Definition: saveload.h:346
NetworkGameSocketHandler::last_frame
uint32 last_frame
Last frame we have executed.
Definition: tcp_game.h:502
_networkclientsocket_pool
NetworkClientSocketPool _networkclientsocket_pool("NetworkClientSocket")
Make very sure the preconditions given in network_type.h are actually followed.
DAY_TICKS
static const int DAY_TICKS
1 day is 74 ticks; _date_fract used to be uint16 and incremented by 885.
Definition: date_type.h:28
PacketWriter::current
Packet * current
The packet we're currently writing to.
Definition: network_server.cpp:59
_last_sync_frame
uint32 _last_sync_frame
Used in the server to store the last time a sync packet was sent to clients.
Definition: network.cpp:71
NetworkAdminCompanyInfo
void NetworkAdminCompanyInfo(const Company *company, bool new_company)
Notify the admin network of company details.
Definition: network_admin.cpp:852
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition: network_type.h:118
NetworkClientInfo
Container for all information known about a client.
Definition: network_base.h:24
NetworkIsValidClientName
bool NetworkIsValidClientName(const std::string_view client_name)
Check whether the given client name is deemed valid for use in network games.
Definition: network_client.cpp:1239
ServerNetworkGameSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override
Close the network connection due to the given status.
Definition: network_server.cpp:247
ServerNetworkGameSocketHandler::STATUS_AUTH_GAME
@ STATUS_AUTH_GAME
The client is authorizing with game (server) password.
Definition: network_server.h:54
NetworkCompanyHasClients
bool NetworkCompanyHasClients(CompanyID company)
Check whether a particular company has clients.
Definition: network_server.cpp:1976
PacketWriter::packets
Packet * packets
Packet queue of the savegame; send these "slowly" to the client.
Definition: network_server.cpp:61
_settings_newgame
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
Definition: settings.cpp:54
NetworkAdminClientError
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).
Definition: network_admin.cpp:838
GCF_STATIC
@ GCF_STATIC
GRF file is used statically (can be used in any MP game)
Definition: newgrf_config.h:25
NetworkSettings::sync_freq
uint16 sync_freq
how often do we check whether we are still in-sync
Definition: settings_type.h:261
ServerNetworkGameSocketHandler::SendWait
NetworkRecvStatus SendWait()
Tell the client that its put in a waiting queue.
Definition: network_server.cpp:495
NetworkSettings::autoclean_companies
bool autoclean_companies
automatically remove companies that are not in use
Definition: settings_type.h:288
PACKET_SERVER_MAP_DATA
@ PACKET_SERVER_MAP_DATA
Server sends bits of the map to the client.
Definition: tcp_game.h:78
NetworkSettings::network_id
std::string network_id
network ID for servers
Definition: settings_type.h:287
NetworkSettings::max_companies
uint8 max_companies
maximum amount of companies
Definition: settings_type.h:292
ServerNetworkGameSocketHandler::STATUS_AUTHORIZED
@ STATUS_AUTHORIZED
The client is authorized.
Definition: network_server.h:56
CLIENT_ID_FIRST
@ CLIENT_ID_FIRST
The first client ID.
Definition: network_type.h:50
NETWORK_RECV_STATUS_MALFORMED_PACKET
@ NETWORK_RECV_STATUS_MALFORMED_PACKET
We apparently send a malformed packet.
Definition: core.h:28
NetworkUpdateClientInfo
void NetworkUpdateClientInfo(ClientID client_id)
Send updated client info of a particular client.
Definition: network_server.cpp:1426
ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR
NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p) override
The client made an error and is quitting the game.
Definition: network_server.cpp:1046
IConsolePrint
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.
Definition: console.cpp:94