00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifdef ENABLE_NETWORK
00013
00014 #include "../stdafx.h"
00015 #include "../strings_func.h"
00016 #include "../date_func.h"
00017 #include "network_admin.h"
00018 #include "network_server.h"
00019 #include "network_udp.h"
00020 #include "network_base.h"
00021 #include "../console_func.h"
00022 #include "../company_base.h"
00023 #include "../command_func.h"
00024 #include "../saveload/saveload.h"
00025 #include "../saveload/saveload_filter.h"
00026 #include "../station_base.h"
00027 #include "../genworld.h"
00028 #include "../company_func.h"
00029 #include "../company_gui.h"
00030 #include "../window_func.h"
00031 #include "../roadveh.h"
00032 #include "../order_backup.h"
00033 #include "../core/pool_func.hpp"
00034 #include "../core/random_func.hpp"
00035 #include "../rev.h"
00036
00037
00038
00039
00040 DECLARE_POSTFIX_INCREMENT(ClientID)
00042 static ClientID _network_client_id = CLIENT_ID_FIRST;
00043
00045 assert_compile(MAX_CLIENT_SLOTS > MAX_CLIENTS);
00046 assert_compile(NetworkClientSocketPool::MAX_SIZE == MAX_CLIENT_SLOTS);
00047
00048 NetworkClientSocketPool _networkclientsocket_pool("NetworkClientSocket");
00049 INSTANTIATE_POOL_METHODS(NetworkClientSocket)
00050
00052 template SocketList TCPListenHandler<ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED>::sockets;
00053
00055 struct PacketWriter : SaveFilter {
00056 ServerNetworkGameSocketHandler *cs;
00057 Packet *current;
00058 size_t total_size;
00059
00064 PacketWriter(ServerNetworkGameSocketHandler *cs) : SaveFilter(NULL), cs(cs), current(NULL), total_size(0)
00065 {
00066 this->cs->savegame_mutex = ThreadMutex::New();
00067 }
00068
00070 ~PacketWriter()
00071 {
00072
00073 if (this->cs != NULL) {
00074 if (this->cs->savegame_mutex != NULL) this->cs->savegame_mutex->BeginCritical();
00075 this->cs->savegame = NULL;
00076 if (this->cs->savegame_mutex != NULL) this->cs->savegame_mutex->EndCritical();
00077
00078 delete this->cs->savegame_mutex;
00079 this->cs->savegame_mutex = NULL;
00080 }
00081
00082 delete this->current;
00083 }
00084
00086 void AppendQueue()
00087 {
00088 if (this->current == NULL) return;
00089
00090 Packet **p = &this->cs->savegame_packets;
00091 while (*p != NULL) {
00092 p = &(*p)->next;
00093 }
00094 *p = this->current;
00095
00096 this->current = NULL;
00097 }
00098
00099 void Write(byte *buf, size_t size)
00100 {
00101
00102 if (this->cs == NULL) SlError(STR_NETWORK_ERROR_LOSTCONNECTION);
00103
00104 if (this->current == NULL) this->current = new Packet(PACKET_SERVER_MAP_DATA);
00105
00106 if (this->cs->savegame_mutex != NULL) this->cs->savegame_mutex->BeginCritical();
00107
00108 byte *bufe = buf + size;
00109 while (buf != bufe) {
00110 size_t to_write = min(SEND_MTU - this->current->size, bufe - buf);
00111 memcpy(this->current->buffer + this->current->size, buf, to_write);
00112 this->current->size += (PacketSize)to_write;
00113 buf += to_write;
00114
00115 if (this->current->size == SEND_MTU) {
00116 this->AppendQueue();
00117 if (buf != bufe) this->current = new Packet(PACKET_SERVER_MAP_DATA);
00118 }
00119 }
00120
00121 if (this->cs->savegame_mutex != NULL) this->cs->savegame_mutex->EndCritical();
00122
00123 this->total_size += size;
00124 }
00125
00126 void Finish()
00127 {
00128
00129 if (this->cs == NULL) SlError(STR_NETWORK_ERROR_LOSTCONNECTION);
00130
00131 if (this->cs->savegame_mutex != NULL) this->cs->savegame_mutex->BeginCritical();
00132
00133
00134 this->AppendQueue();
00135
00136
00137 this->current = new Packet(PACKET_SERVER_MAP_DONE);
00138 this->AppendQueue();
00139
00140
00141 Packet *p = new Packet(PACKET_SERVER_MAP_SIZE);
00142 p->Send_uint32((uint32)this->total_size);
00143 this->cs->NetworkTCPSocketHandler::SendPacket(p);
00144
00145 if (this->cs->savegame_mutex != NULL) this->cs->savegame_mutex->EndCritical();
00146 }
00147 };
00148
00149
00154 ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s)
00155 {
00156 this->status = STATUS_INACTIVE;
00157 this->client_id = _network_client_id++;
00158 this->receive_limit = _settings_client.network.bytes_per_frame_burst;
00159
00160
00161
00162
00163 assert_compile(NetworkClientSocketPool::MAX_SIZE == NetworkClientInfoPool::MAX_SIZE);
00164 }
00165
00169 ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler()
00170 {
00171 if (_redirect_console_to_client == this->client_id) _redirect_console_to_client = INVALID_CLIENT_ID;
00172 OrderBackup::ResetUser(this->client_id);
00173
00174 if (this->savegame_mutex != NULL) this->savegame_mutex->BeginCritical();
00175 if (this->savegame != NULL) this->savegame->cs = NULL;
00176 if (this->savegame_mutex != NULL) this->savegame_mutex->EndCritical();
00177
00178
00179
00180
00181
00182 WaitTillSaved();
00183 ProcessAsyncSaveFinish();
00184
00185 while (this->savegame_packets != NULL) {
00186 Packet *p = this->savegame_packets->next;
00187 delete this->savegame_packets;
00188 this->savegame_packets = p;
00189 }
00190
00191 delete this->savegame_mutex;
00192 }
00193
00194 Packet *ServerNetworkGameSocketHandler::ReceivePacket()
00195 {
00196
00197
00198 if (this->receive_limit <= 0) return NULL;
00199
00200
00201
00202 Packet *p = this->NetworkTCPSocketHandler::ReceivePacket();
00203 if (p != NULL) this->receive_limit -= p->size;
00204 return p;
00205 }
00206
00207 void ServerNetworkGameSocketHandler::SendPacket(Packet *packet)
00208 {
00209 if (this->savegame_mutex != NULL) this->savegame_mutex->BeginCritical();
00210 this->NetworkTCPSocketHandler::SendPacket(packet);
00211 if (this->savegame_mutex != NULL) this->savegame_mutex->EndCritical();
00212 }
00213
00214 NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status)
00215 {
00216 assert(status != NETWORK_RECV_STATUS_OKAY);
00217
00218
00219
00220
00221
00222
00223
00224 if (this->sock == INVALID_SOCKET) return status;
00225
00226 if (status != NETWORK_RECV_STATUS_CONN_LOST && !this->HasClientQuit() && this->status >= STATUS_AUTHORIZED) {
00227
00228 char client_name[NETWORK_CLIENT_NAME_LENGTH];
00229 NetworkClientSocket *new_cs;
00230
00231 this->GetClientName(client_name, sizeof(client_name));
00232
00233 NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, NULL, STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST);
00234
00235
00236 FOR_ALL_CLIENT_SOCKETS(new_cs) {
00237 if (new_cs->status > STATUS_AUTHORIZED && this != new_cs) {
00238 new_cs->SendErrorQuit(this->client_id, NETWORK_ERROR_CONNECTION_LOST);
00239 }
00240 }
00241 }
00242
00243 NetworkAdminClientError(this->client_id, NETWORK_ERROR_CONNECTION_LOST);
00244 DEBUG(net, 1, "Closed client connection %d", this->client_id);
00245
00246
00247 if (this->status >= STATUS_AUTHORIZED) _network_game_info.clients_on--;
00248 extern byte _network_clients_connected;
00249 _network_clients_connected--;
00250
00251 DeleteWindowById(WC_CLIENT_LIST_POPUP, this->client_id);
00252 SetWindowDirty(WC_CLIENT_LIST, 0);
00253
00254 this->SendPackets(true);
00255
00256 delete this->GetInfo();
00257 delete this;
00258
00259 return status;
00260 }
00261
00266 bool ServerNetworkGameSocketHandler::AllowConnection()
00267 {
00268 extern byte _network_clients_connected;
00269 bool accept = _network_clients_connected < MAX_CLIENTS && _network_game_info.clients_on < _settings_client.network.max_clients;
00270
00271
00272
00273 assert_compile(NetworkClientSocketPool::MAX_SIZE == MAX_CLIENTS + 1);
00274 assert(ServerNetworkGameSocketHandler::CanAllocateItem());
00275 return accept;
00276 }
00277
00279 void ServerNetworkGameSocketHandler::Send()
00280 {
00281 NetworkClientSocket *cs;
00282 FOR_ALL_CLIENT_SOCKETS(cs) {
00283 if (cs->writable) {
00284 if (cs->SendPackets() != SPS_CLOSED && cs->status == STATUS_MAP) {
00285
00286 cs->SendMap();
00287 }
00288 }
00289 }
00290 }
00291
00292 static void NetworkHandleCommandQueue(NetworkClientSocket *cs);
00293
00294
00295
00296
00297
00298
00299 NetworkRecvStatus ServerNetworkGameSocketHandler::SendClientInfo(NetworkClientInfo *ci)
00300 {
00301 if (ci->client_id != INVALID_CLIENT_ID) {
00302 Packet *p = new Packet(PACKET_SERVER_CLIENT_INFO);
00303 p->Send_uint32(ci->client_id);
00304 p->Send_uint8 (ci->client_playas);
00305 p->Send_string(ci->client_name);
00306
00307 this->SendPacket(p);
00308 }
00309 return NETWORK_RECV_STATUS_OKAY;
00310 }
00311
00312 NetworkRecvStatus ServerNetworkGameSocketHandler::SendCompanyInfo()
00313 {
00314
00315 NetworkCompanyStats company_stats[MAX_COMPANIES];
00316 NetworkPopulateCompanyStats(company_stats);
00317
00318
00319 char clients[MAX_COMPANIES][NETWORK_CLIENTS_LENGTH];
00320 NetworkClientSocket *csi;
00321 memset(clients, 0, sizeof(clients));
00322
00323
00324 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
00325 if (ci != NULL && Company::IsValidID(ci->client_playas)) {
00326 strecpy(clients[ci->client_playas], ci->client_name, lastof(clients[ci->client_playas]));
00327 }
00328
00329 FOR_ALL_CLIENT_SOCKETS(csi) {
00330 char client_name[NETWORK_CLIENT_NAME_LENGTH];
00331
00332 ((ServerNetworkGameSocketHandler*)csi)->GetClientName(client_name, sizeof(client_name));
00333
00334 ci = csi->GetInfo();
00335 if (ci != NULL && Company::IsValidID(ci->client_playas)) {
00336 if (!StrEmpty(clients[ci->client_playas])) {
00337 strecat(clients[ci->client_playas], ", ", lastof(clients[ci->client_playas]));
00338 }
00339
00340 strecat(clients[ci->client_playas], client_name, lastof(clients[ci->client_playas]));
00341 }
00342 }
00343
00344
00345
00346 Company *company;
00347 Packet *p;
00348
00349 FOR_ALL_COMPANIES(company) {
00350 p = new Packet(PACKET_SERVER_COMPANY_INFO);
00351
00352 p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
00353 p->Send_bool (true);
00354 this->SendCompanyInformation(p, company, &company_stats[company->index]);
00355
00356 if (StrEmpty(clients[company->index])) {
00357 p->Send_string("<none>");
00358 } else {
00359 p->Send_string(clients[company->index]);
00360 }
00361
00362 this->SendPacket(p);
00363 }
00364
00365 p = new Packet(PACKET_SERVER_COMPANY_INFO);
00366
00367 p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
00368 p->Send_bool (false);
00369
00370 this->SendPacket(p);
00371 return NETWORK_RECV_STATUS_OKAY;
00372 }
00373
00374 NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode error)
00375 {
00376 char str[100];
00377 Packet *p = new Packet(PACKET_SERVER_ERROR);
00378
00379 p->Send_uint8(error);
00380 this->SendPacket(p);
00381
00382 StringID strid = GetNetworkErrorMsg(error);
00383 GetString(str, strid, lastof(str));
00384
00385
00386 if (this->status > STATUS_AUTHORIZED) {
00387 NetworkClientSocket *new_cs;
00388 char client_name[NETWORK_CLIENT_NAME_LENGTH];
00389
00390 this->GetClientName(client_name, sizeof(client_name));
00391
00392 DEBUG(net, 1, "'%s' made an error and has been disconnected. Reason: '%s'", client_name, str);
00393
00394 NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, NULL, strid);
00395
00396 FOR_ALL_CLIENT_SOCKETS(new_cs) {
00397 if (new_cs->status > STATUS_AUTHORIZED && new_cs != this) {
00398
00399
00400 if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION) {
00401 error = NETWORK_ERROR_ILLEGAL_PACKET;
00402 }
00403 new_cs->SendErrorQuit(this->client_id, error);
00404 }
00405 }
00406
00407 NetworkAdminClientError(this->client_id, error);
00408 } else {
00409 DEBUG(net, 1, "Client %d made an error and has been disconnected. Reason: '%s'", this->client_id, str);
00410 }
00411
00412
00413 return this->CloseConnection(NETWORK_RECV_STATUS_SERVER_ERROR);
00414 }
00415
00416 NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGRFCheck()
00417 {
00418 Packet *p = new Packet(PACKET_SERVER_CHECK_NEWGRFS);
00419 const GRFConfig *c;
00420 uint grf_count = 0;
00421
00422 for (c = _grfconfig; c != NULL; c = c->next) {
00423 if (!HasBit(c->flags, GCF_STATIC)) grf_count++;
00424 }
00425
00426 p->Send_uint8 (grf_count);
00427 for (c = _grfconfig; c != NULL; c = c->next) {
00428 if (!HasBit(c->flags, GCF_STATIC)) this->SendGRFIdentifier(p, &c->ident);
00429 }
00430
00431 this->SendPacket(p);
00432 return NETWORK_RECV_STATUS_OKAY;
00433 }
00434
00435 NetworkRecvStatus ServerNetworkGameSocketHandler::SendNeedGamePassword()
00436 {
00437
00438 if (this->status >= STATUS_AUTH_GAME) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET);
00439
00440 this->status = STATUS_AUTH_GAME;
00441
00442 Packet *p = new Packet(PACKET_SERVER_NEED_GAME_PASSWORD);
00443 this->SendPacket(p);
00444 return NETWORK_RECV_STATUS_OKAY;
00445 }
00446
00447 NetworkRecvStatus ServerNetworkGameSocketHandler::SendNeedCompanyPassword()
00448 {
00449
00450 if (this->status >= STATUS_AUTH_COMPANY) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET);
00451
00452 this->status = STATUS_AUTH_COMPANY;
00453
00454 Packet *p = new Packet(PACKET_SERVER_NEED_COMPANY_PASSWORD);
00455 p->Send_uint32(_settings_game.game_creation.generation_seed);
00456 p->Send_string(_settings_client.network.network_id);
00457 this->SendPacket(p);
00458 return NETWORK_RECV_STATUS_OKAY;
00459 }
00460
00461 NetworkRecvStatus ServerNetworkGameSocketHandler::SendWelcome()
00462 {
00463 Packet *p;
00464 NetworkClientSocket *new_cs;
00465
00466
00467 if (this->status >= STATUS_AUTHORIZED) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET);
00468
00469 this->status = STATUS_AUTHORIZED;
00470 _network_game_info.clients_on++;
00471
00472 p = new Packet(PACKET_SERVER_WELCOME);
00473 p->Send_uint32(this->client_id);
00474 p->Send_uint32(_settings_game.game_creation.generation_seed);
00475 p->Send_string(_settings_client.network.network_id);
00476 this->SendPacket(p);
00477
00478
00479 FOR_ALL_CLIENT_SOCKETS(new_cs) {
00480 if (new_cs != this && new_cs->status > STATUS_AUTHORIZED) {
00481 this->SendClientInfo(new_cs->GetInfo());
00482 }
00483 }
00484
00485 return this->SendClientInfo(NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER));
00486 }
00487
00488 NetworkRecvStatus ServerNetworkGameSocketHandler::SendWait()
00489 {
00490 int waiting = 0;
00491 NetworkClientSocket *new_cs;
00492 Packet *p;
00493
00494
00495 FOR_ALL_CLIENT_SOCKETS(new_cs) {
00496 if (new_cs->status != STATUS_MAP_WAIT) continue;
00497 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++;
00498 }
00499
00500 p = new Packet(PACKET_SERVER_WAIT);
00501 p->Send_uint8(waiting);
00502 this->SendPacket(p);
00503 return NETWORK_RECV_STATUS_OKAY;
00504 }
00505
00506
00507 NetworkRecvStatus ServerNetworkGameSocketHandler::SendMap()
00508 {
00509 static uint sent_packets;
00510
00511 if (this->status < STATUS_AUTHORIZED) {
00512
00513 return this->SendError(NETWORK_ERROR_NOT_AUTHORIZED);
00514 }
00515
00516 if (this->status == STATUS_AUTHORIZED) {
00517 this->savegame = new PacketWriter(this);
00518
00519
00520 Packet *p = new Packet(PACKET_SERVER_MAP_BEGIN);
00521 p->Send_uint32(_frame_counter);
00522 this->SendPacket(p);
00523
00524 NetworkSyncCommandQueue(this);
00525 this->status = STATUS_MAP;
00526
00527 this->last_frame = _frame_counter;
00528 this->last_frame_server = _frame_counter;
00529
00530 sent_packets = 4;
00531
00532
00533 if (SaveWithFilter(this->savegame, true) != SL_OK) usererror("network savedump failed");
00534 }
00535
00536 if (this->status == STATUS_MAP) {
00537 if (this->savegame_mutex != NULL) this->savegame_mutex->BeginCritical();
00538
00539 bool last_packet = false;
00540
00541 for (uint i = 0; i < sent_packets && this->savegame_packets != NULL; i++) {
00542 Packet *p = this->savegame_packets;
00543 last_packet = p->buffer[2] == PACKET_SERVER_MAP_DONE;
00544
00545
00546 this->savegame_packets = p->next;
00547 p->next = NULL;
00548 this->NetworkTCPSocketHandler::SendPacket(p);
00549
00550 if (last_packet) {
00551
00552 break;
00553 }
00554 }
00555
00556 if (this->savegame_mutex != NULL) this->savegame_mutex->EndCritical();
00557
00558 if (last_packet) {
00559
00560 WaitTillSaved();
00561
00562
00563
00564 this->status = STATUS_DONE_MAP;
00565
00566
00567 NetworkClientSocket *new_cs;
00568 NetworkClientSocket *best = NULL;
00569 FOR_ALL_CLIENT_SOCKETS(new_cs) {
00570 if (new_cs->status == STATUS_MAP_WAIT) {
00571 if (best == NULL || 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)) {
00572 best = new_cs;
00573 }
00574 }
00575 }
00576
00577
00578 if (best != NULL) {
00579
00580 best->status = STATUS_AUTHORIZED;
00581 best->SendMap();
00582
00583
00584 FOR_ALL_CLIENT_SOCKETS(new_cs) {
00585 if (new_cs->status == STATUS_MAP_WAIT) new_cs->SendWait();
00586 }
00587 }
00588 }
00589
00590 switch (this->SendPackets()) {
00591 case SPS_CLOSED:
00592 return NETWORK_RECV_STATUS_CONN_LOST;
00593
00594 case SPS_ALL_SENT:
00595
00596 if (this->savegame_packets != NULL) sent_packets *= 2;
00597 break;
00598
00599 case SPS_PARTLY_SENT:
00600
00601 break;
00602
00603 case SPS_NONE_SENT:
00604
00605 if (sent_packets > 1) sent_packets /= 2;
00606 break;
00607 }
00608 }
00609 return NETWORK_RECV_STATUS_OKAY;
00610 }
00611
00612 NetworkRecvStatus ServerNetworkGameSocketHandler::SendJoin(ClientID client_id)
00613 {
00614 Packet *p = new Packet(PACKET_SERVER_JOIN);
00615
00616 p->Send_uint32(client_id);
00617
00618 this->SendPacket(p);
00619 return NETWORK_RECV_STATUS_OKAY;
00620 }
00621
00622
00623 NetworkRecvStatus ServerNetworkGameSocketHandler::SendFrame()
00624 {
00625 Packet *p = new Packet(PACKET_SERVER_FRAME);
00626 p->Send_uint32(_frame_counter);
00627 p->Send_uint32(_frame_counter_max);
00628 #ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
00629 p->Send_uint32(_sync_seed_1);
00630 #ifdef NETWORK_SEND_DOUBLE_SEED
00631 p->Send_uint32(_sync_seed_2);
00632 #endif
00633 #endif
00634
00635
00636 if (this->last_token == 0) {
00637 this->last_token = InteractiveRandomRange(UINT8_MAX - 1) + 1;
00638 p->Send_uint8(this->last_token);
00639 }
00640
00641 this->SendPacket(p);
00642 return NETWORK_RECV_STATUS_OKAY;
00643 }
00644
00645 NetworkRecvStatus ServerNetworkGameSocketHandler::SendSync()
00646 {
00647 Packet *p = new Packet(PACKET_SERVER_SYNC);
00648 p->Send_uint32(_frame_counter);
00649 p->Send_uint32(_sync_seed_1);
00650
00651 #ifdef NETWORK_SEND_DOUBLE_SEED
00652 p->Send_uint32(_sync_seed_2);
00653 #endif
00654 this->SendPacket(p);
00655 return NETWORK_RECV_STATUS_OKAY;
00656 }
00657
00658 NetworkRecvStatus ServerNetworkGameSocketHandler::SendCommand(const CommandPacket *cp)
00659 {
00660 Packet *p = new Packet(PACKET_SERVER_COMMAND);
00661
00662 this->NetworkGameSocketHandler::SendCommand(p, cp);
00663 p->Send_uint32(cp->frame);
00664 p->Send_bool (cp->my_cmd);
00665
00666 this->SendPacket(p);
00667 return NETWORK_RECV_STATUS_OKAY;
00668 }
00669
00670 NetworkRecvStatus ServerNetworkGameSocketHandler::SendChat(NetworkAction action, ClientID client_id, bool self_send, const char *msg, int64 data)
00671 {
00672 if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_OKAY;
00673
00674 Packet *p = new Packet(PACKET_SERVER_CHAT);
00675
00676 p->Send_uint8 (action);
00677 p->Send_uint32(client_id);
00678 p->Send_bool (self_send);
00679 p->Send_string(msg);
00680 p->Send_uint64(data);
00681
00682 this->SendPacket(p);
00683 return NETWORK_RECV_STATUS_OKAY;
00684 }
00685
00686 NetworkRecvStatus ServerNetworkGameSocketHandler::SendErrorQuit(ClientID client_id, NetworkErrorCode errorno)
00687 {
00688 Packet *p = new Packet(PACKET_SERVER_ERROR_QUIT);
00689
00690 p->Send_uint32(client_id);
00691 p->Send_uint8 (errorno);
00692
00693 this->SendPacket(p);
00694 return NETWORK_RECV_STATUS_OKAY;
00695 }
00696
00697 NetworkRecvStatus ServerNetworkGameSocketHandler::SendQuit(ClientID client_id)
00698 {
00699 Packet *p = new Packet(PACKET_SERVER_QUIT);
00700
00701 p->Send_uint32(client_id);
00702
00703 this->SendPacket(p);
00704 return NETWORK_RECV_STATUS_OKAY;
00705 }
00706
00707 NetworkRecvStatus ServerNetworkGameSocketHandler::SendShutdown()
00708 {
00709 Packet *p = new Packet(PACKET_SERVER_SHUTDOWN);
00710 this->SendPacket(p);
00711 return NETWORK_RECV_STATUS_OKAY;
00712 }
00713
00714 NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGame()
00715 {
00716 Packet *p = new Packet(PACKET_SERVER_NEWGAME);
00717 this->SendPacket(p);
00718 return NETWORK_RECV_STATUS_OKAY;
00719 }
00720
00721 NetworkRecvStatus ServerNetworkGameSocketHandler::SendRConResult(uint16 colour, const char *command)
00722 {
00723 Packet *p = new Packet(PACKET_SERVER_RCON);
00724
00725 p->Send_uint16(colour);
00726 p->Send_string(command);
00727 this->SendPacket(p);
00728 return NETWORK_RECV_STATUS_OKAY;
00729 }
00730
00731 NetworkRecvStatus ServerNetworkGameSocketHandler::SendMove(ClientID client_id, CompanyID company_id)
00732 {
00733 Packet *p = new Packet(PACKET_SERVER_MOVE);
00734
00735 p->Send_uint32(client_id);
00736 p->Send_uint8(company_id);
00737 this->SendPacket(p);
00738 return NETWORK_RECV_STATUS_OKAY;
00739 }
00740
00741 NetworkRecvStatus ServerNetworkGameSocketHandler::SendCompanyUpdate()
00742 {
00743 Packet *p = new Packet(PACKET_SERVER_COMPANY_UPDATE);
00744
00745 p->Send_uint16(_network_company_passworded);
00746 this->SendPacket(p);
00747 return NETWORK_RECV_STATUS_OKAY;
00748 }
00749
00750 NetworkRecvStatus ServerNetworkGameSocketHandler::SendConfigUpdate()
00751 {
00752 Packet *p = new Packet(PACKET_SERVER_CONFIG_UPDATE);
00753
00754 p->Send_uint8(_settings_client.network.max_companies);
00755 p->Send_uint8(_settings_client.network.max_spectators);
00756 this->SendPacket(p);
00757 return NETWORK_RECV_STATUS_OKAY;
00758 }
00759
00760
00761
00762
00763
00764
00765 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_COMPANY_INFO)
00766 {
00767 return this->SendCompanyInfo();
00768 }
00769
00770 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_NEWGRFS_CHECKED)
00771 {
00772 if (this->status != STATUS_NEWGRFS_CHECK) {
00773
00774 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
00775 }
00776
00777 NetworkClientInfo *ci = this->GetInfo();
00778
00779
00780 if (!StrEmpty(_settings_client.network.server_password)) {
00781 return this->SendNeedGamePassword();
00782 }
00783
00784 if (Company::IsValidID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) {
00785 return this->SendNeedCompanyPassword();
00786 }
00787
00788 return this->SendWelcome();
00789 }
00790
00791 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_JOIN)
00792 {
00793 if (this->status != STATUS_INACTIVE) {
00794
00795 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
00796 }
00797
00798 char name[NETWORK_CLIENT_NAME_LENGTH];
00799 CompanyID playas;
00800 NetworkLanguage client_lang;
00801 char client_revision[NETWORK_REVISION_LENGTH];
00802
00803 p->Recv_string(client_revision, sizeof(client_revision));
00804
00805
00806 if (!IsNetworkCompatibleVersion(client_revision)) {
00807
00808 return this->SendError(NETWORK_ERROR_WRONG_REVISION);
00809 }
00810
00811 p->Recv_string(name, sizeof(name));
00812 playas = (Owner)p->Recv_uint8();
00813 client_lang = (NetworkLanguage)p->Recv_uint8();
00814
00815 if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
00816
00817
00818 switch (playas) {
00819 case COMPANY_NEW_COMPANY:
00820 if (Company::GetNumItems() >= _settings_client.network.max_companies) {
00821 return this->SendError(NETWORK_ERROR_FULL);
00822 }
00823 break;
00824 case COMPANY_SPECTATOR:
00825 if (NetworkSpectatorCount() >= _settings_client.network.max_spectators) {
00826 return this->SendError(NETWORK_ERROR_FULL);
00827 }
00828 break;
00829 default:
00830 if (!Company::IsValidHumanID(playas)) {
00831 return this->SendError(NETWORK_ERROR_COMPANY_MISMATCH);
00832 }
00833 break;
00834 }
00835
00836
00837 if (StrEmpty(name)) strecpy(name, "Player", lastof(name));
00838
00839 if (!NetworkFindName(name)) {
00840
00841 return this->SendError(NETWORK_ERROR_NAME_IN_USE);
00842 }
00843
00844 assert(NetworkClientInfo::CanAllocateItem());
00845 NetworkClientInfo *ci = new NetworkClientInfo(this->client_id);
00846 this->SetInfo(ci);
00847 ci->join_date = _date;
00848 strecpy(ci->client_name, name, lastof(ci->client_name));
00849 ci->client_playas = playas;
00850 ci->client_lang = client_lang;
00851 DEBUG(desync, 1, "client: %08x; %02x; %02x; %04x", _date, _date_fract, (int)ci->client_playas, ci->index);
00852
00853
00854 if (Company::IsValidID(playas)) _network_company_states[playas].months_empty = 0;
00855
00856 this->status = STATUS_NEWGRFS_CHECK;
00857
00858 if (_grfconfig == NULL) {
00859
00860 return this->NetworkPacketReceive_PACKET_CLIENT_NEWGRFS_CHECKED_command(NULL);
00861 }
00862
00863 return this->SendNewGRFCheck();
00864 }
00865
00866 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_GAME_PASSWORD)
00867 {
00868 if (this->status != STATUS_AUTH_GAME) {
00869 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
00870 }
00871
00872 char password[NETWORK_PASSWORD_LENGTH];
00873 p->Recv_string(password, sizeof(password));
00874
00875
00876 if (!StrEmpty(_settings_client.network.server_password) &&
00877 strcmp(password, _settings_client.network.server_password) != 0) {
00878
00879 return this->SendError(NETWORK_ERROR_WRONG_PASSWORD);
00880 }
00881
00882 const NetworkClientInfo *ci = this->GetInfo();
00883 if (Company::IsValidID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) {
00884 return this->SendNeedCompanyPassword();
00885 }
00886
00887
00888 return this->SendWelcome();
00889 }
00890
00891 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_COMPANY_PASSWORD)
00892 {
00893 if (this->status != STATUS_AUTH_COMPANY) {
00894 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
00895 }
00896
00897 char password[NETWORK_PASSWORD_LENGTH];
00898 p->Recv_string(password, sizeof(password));
00899
00900
00901
00902
00903 CompanyID playas = this->GetInfo()->client_playas;
00904 if (Company::IsValidID(playas) && !StrEmpty(_network_company_states[playas].password) &&
00905 strcmp(password, _network_company_states[playas].password) != 0) {
00906
00907 return this->SendError(NETWORK_ERROR_WRONG_PASSWORD);
00908 }
00909
00910 return this->SendWelcome();
00911 }
00912
00913 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_GETMAP)
00914 {
00915 NetworkClientSocket *new_cs;
00916
00917
00918
00919
00920
00921
00922
00923
00924 if (HasBit(_openttd_newgrf_version, 19)) {
00925 if (_openttd_newgrf_version != p->Recv_uint32()) {
00926
00927
00928 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
00929 }
00930 } else if (p->size != 3) {
00931
00932
00933 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
00934 }
00935
00936
00937
00938 if (this->status < STATUS_AUTHORIZED || this->HasClientQuit()) {
00939 return this->SendError(NETWORK_ERROR_NOT_AUTHORIZED);
00940 }
00941
00942
00943 FOR_ALL_CLIENT_SOCKETS(new_cs) {
00944 if (new_cs->status == STATUS_MAP) {
00945
00946 this->status = STATUS_MAP_WAIT;
00947 return this->SendWait();
00948 }
00949 }
00950
00951
00952 return this->SendMap();
00953 }
00954
00955 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_MAP_OK)
00956 {
00957
00958 if (this->status == STATUS_DONE_MAP && !this->HasClientQuit()) {
00959 char client_name[NETWORK_CLIENT_NAME_LENGTH];
00960 NetworkClientSocket *new_cs;
00961
00962 this->GetClientName(client_name, sizeof(client_name));
00963
00964 NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, client_name, NULL, this->client_id);
00965
00966
00967
00968 this->status = STATUS_PRE_ACTIVE;
00969 NetworkHandleCommandQueue(this);
00970 this->SendFrame();
00971 this->SendSync();
00972
00973
00974
00975 this->last_frame = _frame_counter;
00976 this->last_frame_server = _frame_counter;
00977
00978 FOR_ALL_CLIENT_SOCKETS(new_cs) {
00979 if (new_cs->status > STATUS_AUTHORIZED) {
00980 new_cs->SendClientInfo(this->GetInfo());
00981 new_cs->SendJoin(this->client_id);
00982 }
00983 }
00984
00985 NetworkAdminClientInfo(this, true);
00986
00987
00988 this->SendConfigUpdate();
00989
00990
00991 return this->SendCompanyUpdate();
00992 }
00993
00994
00995 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
00996 }
00997
01002 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_COMMAND)
01003 {
01004
01005
01006 if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
01007 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
01008 }
01009
01010 if (this->incoming_queue.Count() >= _settings_client.network.max_commands_in_queue) {
01011 return this->SendError(NETWORK_ERROR_TOO_MANY_COMMANDS);
01012 }
01013
01014 CommandPacket cp;
01015 const char *err = this->ReceiveCommand(p, &cp);
01016
01017 if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
01018
01019 NetworkClientInfo *ci = this->GetInfo();
01020
01021 if (err != NULL) {
01022 IConsolePrintF(CC_ERROR, "WARNING: %s from client %d (IP: %s).", err, ci->client_id, this->GetClientIP());
01023 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
01024 }
01025
01026
01027 if ((GetCommandFlags(cp.cmd) & CMD_SERVER) && ci->client_id != CLIENT_ID_SERVER) {
01028 IConsolePrintF(CC_ERROR, "WARNING: server only command from: client %d (IP: %s), kicking...", ci->client_id, this->GetClientIP());
01029 return this->SendError(NETWORK_ERROR_KICKED);
01030 }
01031
01032 if ((GetCommandFlags(cp.cmd) & CMD_SPECTATOR) == 0 && !Company::IsValidID(cp.company) && ci->client_id != CLIENT_ID_SERVER) {
01033 IConsolePrintF(CC_ERROR, "WARNING: spectator issueing command from client %d (IP: %s), kicking...", ci->client_id, this->GetClientIP());
01034 return this->SendError(NETWORK_ERROR_KICKED);
01035 }
01036
01042 if (!(cp.cmd == CMD_COMPANY_CTRL && cp.p1 == 0 && ci->client_playas == COMPANY_NEW_COMPANY) && ci->client_playas != cp.company) {
01043 IConsolePrintF(CC_ERROR, "WARNING: client %d (IP: %s) tried to execute a command as company %d, kicking...",
01044 ci->client_playas + 1, this->GetClientIP(), cp.company + 1);
01045 return this->SendError(NETWORK_ERROR_COMPANY_MISMATCH);
01046 }
01047
01048 if (cp.cmd == CMD_COMPANY_CTRL) {
01049 if (cp.p1 != 0 || cp.company != COMPANY_SPECTATOR) {
01050 return this->SendError(NETWORK_ERROR_CHEATER);
01051 }
01052
01053
01054 if (Company::GetNumItems() >= _settings_client.network.max_companies) {
01055 NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_CLIENT, ci->client_id, "cannot create new company, server full", CLIENT_ID_SERVER);
01056 return NETWORK_RECV_STATUS_OKAY;
01057 }
01058 }
01059
01060 if (GetCommandFlags(cp.cmd) & CMD_CLIENT_ID) cp.p2 = this->client_id;
01061
01062 this->incoming_queue.Append(&cp);
01063 return NETWORK_RECV_STATUS_OKAY;
01064 }
01065
01066 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_ERROR)
01067 {
01068
01069
01070 NetworkClientSocket *new_cs;
01071 char str[100];
01072 char client_name[NETWORK_CLIENT_NAME_LENGTH];
01073 NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8();
01074
01075
01076 if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
01077 return this->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
01078 }
01079
01080 this->GetClientName(client_name, sizeof(client_name));
01081
01082 StringID strid = GetNetworkErrorMsg(errorno);
01083 GetString(str, strid, lastof(str));
01084
01085 DEBUG(net, 2, "'%s' reported an error and is closing its connection (%s)", client_name, str);
01086
01087 NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, NULL, strid);
01088
01089 FOR_ALL_CLIENT_SOCKETS(new_cs) {
01090 if (new_cs->status > STATUS_AUTHORIZED) {
01091 new_cs->SendErrorQuit(this->client_id, errorno);
01092 }
01093 }
01094
01095 NetworkAdminClientError(this->client_id, errorno);
01096
01097 return this->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
01098 }
01099
01100 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_QUIT)
01101 {
01102
01103
01104 NetworkClientSocket *new_cs;
01105 char client_name[NETWORK_CLIENT_NAME_LENGTH];
01106
01107
01108 if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
01109 return this->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
01110 }
01111
01112 this->GetClientName(client_name, sizeof(client_name));
01113
01114 NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, NULL, STR_NETWORK_MESSAGE_CLIENT_LEAVING);
01115
01116 FOR_ALL_CLIENT_SOCKETS(new_cs) {
01117 if (new_cs->status > STATUS_AUTHORIZED && new_cs != this) {
01118 new_cs->SendQuit(this->client_id);
01119 }
01120 }
01121
01122 NetworkAdminClientQuit(this->client_id);
01123
01124 return this->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
01125 }
01126
01127 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_ACK)
01128 {
01129 if (this->status < STATUS_AUTHORIZED) {
01130
01131 return this->SendError(NETWORK_ERROR_NOT_AUTHORIZED);
01132 }
01133
01134 uint32 frame = p->Recv_uint32();
01135
01136
01137 if (this->status == STATUS_PRE_ACTIVE) {
01138
01139 if (frame + DAY_TICKS < _frame_counter) return NETWORK_RECV_STATUS_OKAY;
01140
01141
01142 this->status = STATUS_ACTIVE;
01143 this->last_token_frame = _frame_counter;
01144
01145
01146 IConsoleCmdExec("exec scripts/on_server_connect.scr 0");
01147 }
01148
01149
01150 uint8 token = p->Recv_uint8();
01151 if (token == this->last_token) {
01152
01153
01154
01155
01156
01157
01158
01159
01160 this->last_token_frame = _frame_counter;
01161
01162 this->last_token = 0;
01163 }
01164
01165
01166 this->last_frame = frame;
01167
01168 this->last_frame_server = _frame_counter;
01169 return NETWORK_RECV_STATUS_OKAY;
01170 }
01171
01172
01173
01174 void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const char *msg, ClientID from_id, int64 data, bool from_admin)
01175 {
01176 NetworkClientSocket *cs;
01177 const NetworkClientInfo *ci, *ci_own, *ci_to;
01178
01179 switch (desttype) {
01180 case DESTTYPE_CLIENT:
01181
01182 if ((ClientID)dest == CLIENT_ID_SERVER) {
01183 ci = NetworkClientInfo::GetByClientID(from_id);
01184
01185 if (ci != NULL) {
01186 NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), false, ci->client_name, msg, data);
01187
01188 if (_settings_client.network.server_admin_chat) {
01189 NetworkAdminChat(action, desttype, from_id, msg, data, from_admin);
01190 }
01191 }
01192 } else {
01193
01194 FOR_ALL_CLIENT_SOCKETS(cs) {
01195 if (cs->client_id == (ClientID)dest) {
01196 cs->SendChat(action, from_id, false, msg, data);
01197 break;
01198 }
01199 }
01200 }
01201
01202
01203 if (from_id != (ClientID)dest) {
01204 if (from_id == CLIENT_ID_SERVER) {
01205 ci = NetworkClientInfo::GetByClientID(from_id);
01206 ci_to = NetworkClientInfo::GetByClientID((ClientID)dest);
01207 if (ci != NULL && ci_to != NULL) {
01208 NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), true, ci_to->client_name, msg, data);
01209 }
01210 } else {
01211 FOR_ALL_CLIENT_SOCKETS(cs) {
01212 if (cs->client_id == from_id) {
01213 cs->SendChat(action, (ClientID)dest, true, msg, data);
01214 break;
01215 }
01216 }
01217 }
01218 }
01219 break;
01220 case DESTTYPE_TEAM: {
01221
01222 bool show_local = true;
01223
01224 ci_to = NULL;
01225 FOR_ALL_CLIENT_SOCKETS(cs) {
01226 ci = cs->GetInfo();
01227 if (ci != NULL && ci->client_playas == (CompanyID)dest) {
01228 cs->SendChat(action, from_id, false, msg, data);
01229 if (cs->client_id == from_id) show_local = false;
01230 ci_to = ci;
01231 }
01232 }
01233
01234
01235 if (_local_company == (CompanyID)dest && _settings_client.network.server_admin_chat) {
01236 NetworkAdminChat(action, desttype, from_id, msg, data, from_admin);
01237 }
01238
01239 ci = NetworkClientInfo::GetByClientID(from_id);
01240 ci_own = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
01241 if (ci != NULL && ci_own != NULL && ci_own->client_playas == dest) {
01242 NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), false, ci->client_name, msg, data);
01243 if (from_id == CLIENT_ID_SERVER) show_local = false;
01244 ci_to = ci_own;
01245 }
01246
01247
01248 if (ci_to == NULL) break;
01249
01250
01251 if (ci != NULL && show_local) {
01252 if (from_id == CLIENT_ID_SERVER) {
01253 char name[NETWORK_NAME_LENGTH];
01254 StringID str = Company::IsValidID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
01255 SetDParam(0, ci_to->client_playas);
01256 GetString(name, str, lastof(name));
01257 NetworkTextMessage(action, GetDrawStringCompanyColour(ci_own->client_playas), true, name, msg, data);
01258 } else {
01259 FOR_ALL_CLIENT_SOCKETS(cs) {
01260 if (cs->client_id == from_id) {
01261 cs->SendChat(action, ci_to->client_id, true, msg, data);
01262 }
01263 }
01264 }
01265 }
01266 break;
01267 }
01268 default:
01269 DEBUG(net, 0, "[server] received unknown chat destination type %d. Doing broadcast instead", desttype);
01270
01271 case DESTTYPE_BROADCAST:
01272 FOR_ALL_CLIENT_SOCKETS(cs) {
01273 cs->SendChat(action, from_id, false, msg, data);
01274 }
01275
01276 NetworkAdminChat(action, desttype, from_id, msg, data, from_admin);
01277
01278 ci = NetworkClientInfo::GetByClientID(from_id);
01279 if (ci != NULL) {
01280 NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), false, ci->client_name, msg, data);
01281 }
01282 break;
01283 }
01284 }
01285
01286 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_CHAT)
01287 {
01288 if (this->status < STATUS_AUTHORIZED) {
01289
01290 return this->SendError(NETWORK_ERROR_NOT_AUTHORIZED);
01291 }
01292
01293 NetworkAction action = (NetworkAction)p->Recv_uint8();
01294 DestType desttype = (DestType)p->Recv_uint8();
01295 int dest = p->Recv_uint32();
01296 char msg[NETWORK_CHAT_LENGTH];
01297
01298 p->Recv_string(msg, NETWORK_CHAT_LENGTH);
01299 int64 data = p->Recv_uint64();
01300
01301 NetworkClientInfo *ci = this->GetInfo();
01302 switch (action) {
01303 case NETWORK_ACTION_GIVE_MONEY:
01304 if (!Company::IsValidID(ci->client_playas)) break;
01305
01306 case NETWORK_ACTION_CHAT:
01307 case NETWORK_ACTION_CHAT_CLIENT:
01308 case NETWORK_ACTION_CHAT_COMPANY:
01309 NetworkServerSendChat(action, desttype, dest, msg, this->client_id, data);
01310 break;
01311 default:
01312 IConsolePrintF(CC_ERROR, "WARNING: invalid chat action from client %d (IP: %s).", ci->client_id, this->GetClientIP());
01313 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
01314 }
01315 return NETWORK_RECV_STATUS_OKAY;
01316 }
01317
01318 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_SET_PASSWORD)
01319 {
01320 if (this->status != STATUS_ACTIVE) {
01321
01322 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
01323 }
01324
01325 char password[NETWORK_PASSWORD_LENGTH];
01326 const NetworkClientInfo *ci;
01327
01328 p->Recv_string(password, sizeof(password));
01329 ci = this->GetInfo();
01330
01331 NetworkServerSetCompanyPassword(ci->client_playas, password);
01332 return NETWORK_RECV_STATUS_OKAY;
01333 }
01334
01335 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_SET_NAME)
01336 {
01337 if (this->status != STATUS_ACTIVE) {
01338
01339 return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
01340 }
01341
01342 char client_name[NETWORK_CLIENT_NAME_LENGTH];
01343 NetworkClientInfo *ci;
01344
01345 p->Recv_string(client_name, sizeof(client_name));
01346 ci = this->GetInfo();
01347
01348 if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
01349
01350 if (ci != NULL) {
01351
01352 if (NetworkFindName(client_name)) {
01353 NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, client_name);
01354 strecpy(ci->client_name, client_name, lastof(ci->client_name));
01355 NetworkUpdateClientInfo(ci->client_id);
01356 }
01357 }
01358 return NETWORK_RECV_STATUS_OKAY;
01359 }
01360
01361 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_RCON)
01362 {
01363 if (this->status != STATUS_ACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
01364
01365 char pass[NETWORK_PASSWORD_LENGTH];
01366 char command[NETWORK_RCONCOMMAND_LENGTH];
01367
01368 if (StrEmpty(_settings_client.network.rcon_password)) return NETWORK_RECV_STATUS_OKAY;
01369
01370 p->Recv_string(pass, sizeof(pass));
01371 p->Recv_string(command, sizeof(command));
01372
01373 if (strcmp(pass, _settings_client.network.rcon_password) != 0) {
01374 DEBUG(net, 0, "[rcon] wrong password from client-id %d", this->client_id);
01375 return NETWORK_RECV_STATUS_OKAY;
01376 }
01377
01378 DEBUG(net, 0, "[rcon] client-id %d executed: '%s'", this->client_id, command);
01379
01380 _redirect_console_to_client = this->client_id;
01381 IConsoleCmdExec(command);
01382 _redirect_console_to_client = INVALID_CLIENT_ID;
01383 return NETWORK_RECV_STATUS_OKAY;
01384 }
01385
01386 DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_MOVE)
01387 {
01388 if (this->status != STATUS_ACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
01389
01390 CompanyID company_id = (Owner)p->Recv_uint8();
01391
01392
01393 if (company_id != COMPANY_SPECTATOR && !Company::IsValidHumanID(company_id)) return NETWORK_RECV_STATUS_OKAY;
01394
01395
01396 if (company_id != COMPANY_SPECTATOR && !StrEmpty(_network_company_states[company_id].password)) {
01397
01398 char password[NETWORK_PASSWORD_LENGTH];
01399 p->Recv_string(password, sizeof(password));
01400
01401
01402 if (strcmp(password, _network_company_states[company_id].password) != 0) {
01403 DEBUG(net, 2, "[move] wrong password from client-id #%d for company #%d", this->client_id, company_id + 1);
01404 return NETWORK_RECV_STATUS_OKAY;
01405 }
01406 }
01407
01408
01409 NetworkServerDoMove(this->client_id, company_id);
01410 return NETWORK_RECV_STATUS_OKAY;
01411 }
01412
01420 void NetworkSocketHandler::SendCompanyInformation(Packet *p, const Company *c, const NetworkCompanyStats *stats, uint max_len)
01421 {
01422
01423 char company_name[NETWORK_COMPANY_NAME_LENGTH];
01424 SetDParam(0, c->index);
01425
01426 assert(max_len <= lengthof(company_name));
01427 GetString(company_name, STR_COMPANY_NAME, company_name + max_len - 1);
01428
01429
01430 Money income = 0;
01431 if (_cur_year - 1 == c->inaugurated_year) {
01432
01433 for (uint i = 0; i < lengthof(c->yearly_expenses[2]); i++) {
01434 income -= c->yearly_expenses[2][i];
01435 }
01436 } else {
01437 for (uint i = 0; i < lengthof(c->yearly_expenses[1]); i++) {
01438 income -= c->yearly_expenses[1][i];
01439 }
01440 }
01441
01442
01443 p->Send_uint8 (c->index);
01444 p->Send_string(company_name);
01445 p->Send_uint32(c->inaugurated_year);
01446 p->Send_uint64(c->old_economy[0].company_value);
01447 p->Send_uint64(c->money);
01448 p->Send_uint64(income);
01449 p->Send_uint16(c->old_economy[0].performance_history);
01450
01451
01452 p->Send_bool (!StrEmpty(_network_company_states[c->index].password));
01453
01454 for (uint i = 0; i < NETWORK_VEH_END; i++) {
01455 p->Send_uint16(stats->num_vehicle[i]);
01456 }
01457
01458 for (uint i = 0; i < NETWORK_VEH_END; i++) {
01459 p->Send_uint16(stats->num_station[i]);
01460 }
01461
01462 p->Send_bool(c->is_ai);
01463 }
01464
01469 void NetworkPopulateCompanyStats(NetworkCompanyStats *stats)
01470 {
01471 const Vehicle *v;
01472 const Station *s;
01473
01474 memset(stats, 0, sizeof(*stats) * MAX_COMPANIES);
01475
01476
01477 FOR_ALL_VEHICLES(v) {
01478 if (!Company::IsValidID(v->owner) || !v->IsPrimaryVehicle()) continue;
01479 byte type = 0;
01480 switch (v->type) {
01481 case VEH_TRAIN: type = NETWORK_VEH_TRAIN; break;
01482 case VEH_ROAD: type = RoadVehicle::From(v)->IsBus() ? NETWORK_VEH_BUS : NETWORK_VEH_LORRY; break;
01483 case VEH_AIRCRAFT: type = NETWORK_VEH_PLANE; break;
01484 case VEH_SHIP: type = NETWORK_VEH_SHIP; break;
01485 default: continue;
01486 }
01487 stats[v->owner].num_vehicle[type]++;
01488 }
01489
01490
01491 FOR_ALL_STATIONS(s) {
01492 if (Company::IsValidID(s->owner)) {
01493 NetworkCompanyStats *npi = &stats[s->owner];
01494
01495 if (s->facilities & FACIL_TRAIN) npi->num_station[NETWORK_VEH_TRAIN]++;
01496 if (s->facilities & FACIL_TRUCK_STOP) npi->num_station[NETWORK_VEH_LORRY]++;
01497 if (s->facilities & FACIL_BUS_STOP) npi->num_station[NETWORK_VEH_BUS]++;
01498 if (s->facilities & FACIL_AIRPORT) npi->num_station[NETWORK_VEH_PLANE]++;
01499 if (s->facilities & FACIL_DOCK) npi->num_station[NETWORK_VEH_SHIP]++;
01500 }
01501 }
01502 }
01503
01504
01505 void NetworkUpdateClientInfo(ClientID client_id)
01506 {
01507 NetworkClientSocket *cs;
01508 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
01509
01510 if (ci == NULL) return;
01511
01512 DEBUG(desync, 1, "client: %08x; %02x; %02x; %04x", _date, _date_fract, (int)ci->client_playas, client_id);
01513
01514 FOR_ALL_CLIENT_SOCKETS(cs) {
01515 cs->SendClientInfo(ci);
01516 }
01517
01518 NetworkAdminClientUpdate(ci);
01519 }
01520
01521
01522 static void NetworkCheckRestartMap()
01523 {
01524 if (_settings_client.network.restart_game_year != 0 && _cur_year >= _settings_client.network.restart_game_year) {
01525 DEBUG(net, 0, "Auto-restarting map. Year %d reached", _cur_year);
01526
01527 StartNewGameWithoutGUI(GENERATE_NEW_SEED);
01528 }
01529 }
01530
01531
01532
01533
01534
01535
01536 static void NetworkAutoCleanCompanies()
01537 {
01538 const NetworkClientInfo *ci;
01539 const Company *c;
01540 bool clients_in_company[MAX_COMPANIES];
01541 int vehicles_in_company[MAX_COMPANIES];
01542
01543 if (!_settings_client.network.autoclean_companies) return;
01544
01545 memset(clients_in_company, 0, sizeof(clients_in_company));
01546
01547
01548 FOR_ALL_CLIENT_INFOS(ci) {
01549 if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
01550 }
01551
01552 if (!_network_dedicated) {
01553 ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
01554 if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
01555 }
01556
01557 if (_settings_client.network.autoclean_novehicles != 0) {
01558 memset(vehicles_in_company, 0, sizeof(vehicles_in_company));
01559
01560 const Vehicle *v;
01561 FOR_ALL_VEHICLES(v) {
01562 if (!Company::IsValidID(v->owner) || !v->IsPrimaryVehicle()) continue;
01563 vehicles_in_company[v->owner]++;
01564 }
01565 }
01566
01567
01568 FOR_ALL_COMPANIES(c) {
01569
01570 if (c->is_ai) continue;
01571
01572 if (!clients_in_company[c->index]) {
01573
01574 _network_company_states[c->index].months_empty++;
01575
01576
01577 if (_settings_client.network.autoclean_unprotected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_unprotected && StrEmpty(_network_company_states[c->index].password)) {
01578
01579 DoCommandP(0, 2 | c->index << 16, CRR_AUTOCLEAN, CMD_COMPANY_CTRL);
01580 IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d with no password", c->index + 1);
01581 }
01582
01583 if (_settings_client.network.autoclean_protected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_protected && !StrEmpty(_network_company_states[c->index].password)) {
01584
01585 _network_company_states[c->index].password[0] = '\0';
01586 IConsolePrintF(CC_DEFAULT, "Auto-removed protection from company #%d", c->index + 1);
01587 _network_company_states[c->index].months_empty = 0;
01588 NetworkServerUpdateCompanyPassworded(c->index, false);
01589 }
01590
01591 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) {
01592
01593 DoCommandP(0, 2 | c->index << 16, CRR_AUTOCLEAN, CMD_COMPANY_CTRL);
01594 IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d with no vehicles", c->index + 1);
01595 }
01596 } else {
01597
01598 _network_company_states[c->index].months_empty = 0;
01599 }
01600 }
01601 }
01602
01603
01604
01605 bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH])
01606 {
01607 bool found_name = false;
01608 uint number = 0;
01609 char original_name[NETWORK_CLIENT_NAME_LENGTH];
01610
01611
01612 ttd_strlcpy(original_name, new_name, NETWORK_CLIENT_NAME_LENGTH);
01613
01614 while (!found_name) {
01615 const NetworkClientInfo *ci;
01616
01617 found_name = true;
01618 FOR_ALL_CLIENT_INFOS(ci) {
01619 if (strcmp(ci->client_name, new_name) == 0) {
01620
01621 found_name = false;
01622 break;
01623 }
01624 }
01625
01626 ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
01627 if (ci != NULL) {
01628 if (strcmp(ci->client_name, new_name) == 0) found_name = false;
01629 }
01630
01631 if (!found_name) {
01632
01633
01634
01635 if (number++ > MAX_CLIENTS) break;
01636 snprintf(new_name, NETWORK_CLIENT_NAME_LENGTH, "%s #%d", original_name, number);
01637 }
01638 }
01639
01640 return found_name;
01641 }
01642
01649 bool NetworkServerChangeClientName(ClientID client_id, const char *new_name)
01650 {
01651 NetworkClientInfo *ci;
01652
01653 FOR_ALL_CLIENT_INFOS(ci) {
01654 if (strcmp(ci->client_name, new_name) == 0) return false;
01655 }
01656
01657 ci = NetworkClientInfo::GetByClientID(client_id);
01658 if (ci == NULL) return false;
01659
01660 NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, true, ci->client_name, new_name);
01661
01662 strecpy(ci->client_name, new_name, lastof(ci->client_name));
01663
01664 NetworkUpdateClientInfo(client_id);
01665 return true;
01666 }
01667
01674 void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed)
01675 {
01676 if (!Company::IsValidHumanID(company_id)) return;
01677
01678 if (!already_hashed) {
01679 password = GenerateCompanyPasswordHash(password, _settings_client.network.network_id, _settings_game.game_creation.generation_seed);
01680 }
01681
01682 strecpy(_network_company_states[company_id].password, password, lastof(_network_company_states[company_id].password));
01683 NetworkServerUpdateCompanyPassworded(company_id, !StrEmpty(_network_company_states[company_id].password));
01684 }
01685
01686
01687 static void NetworkHandleCommandQueue(NetworkClientSocket *cs)
01688 {
01689 CommandPacket *cp;
01690 while ((cp = cs->outgoing_queue.Pop()) != NULL) {
01691 cs->SendCommand(cp);
01692 free(cp);
01693 }
01694 }
01695
01696
01697 void NetworkServer_Tick(bool send_frame)
01698 {
01699 NetworkClientSocket *cs;
01700 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
01701 bool send_sync = false;
01702 #endif
01703
01704 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
01705 if (_frame_counter >= _last_sync_frame + _settings_client.network.sync_freq) {
01706 _last_sync_frame = _frame_counter;
01707 send_sync = true;
01708 }
01709 #endif
01710
01711
01712
01713 FOR_ALL_CLIENT_SOCKETS(cs) {
01714
01715
01716 cs->receive_limit = min(cs->receive_limit + _settings_client.network.bytes_per_frame,
01717 _settings_client.network.bytes_per_frame_burst);
01718
01719
01720 if (cs->status == NetworkClientSocket::STATUS_ACTIVE) {
01721
01722 uint lag = NetworkCalculateLag(cs) / DAY_TICKS;
01723 if (lag > 0) {
01724 if (lag > 3) {
01725
01726
01727 IConsolePrintF(CC_ERROR, cs->last_packet + 3 * DAY_TICKS * MILLISECONDS_PER_TICK > _realtime_tick ?
01728
01729 "Client #%d is dropped because the client's game state is more than 4 game-days behind" :
01730
01731 "Client #%d is dropped because the client did not respond for more than 4 game-days",
01732 cs->client_id);
01733 cs->CloseConnection(NETWORK_RECV_STATUS_SERVER_ERROR);
01734 continue;
01735 }
01736
01737
01738
01739
01740
01741
01742 if (cs->lag_test == 0 && cs->last_packet + DAY_TICKS * MILLISECONDS_PER_TICK > _realtime_tick) {
01743 IConsolePrintF(CC_WARNING,"[%d] Client #%d is slow, try increasing [network.]frame_freq to a higher value!", _frame_counter, cs->client_id);
01744 cs->lag_test = 1;
01745 }
01746 } else {
01747 cs->lag_test = 0;
01748 }
01749 if (cs->last_frame_server - cs->last_token_frame >= 5 * DAY_TICKS) {
01750
01751 IConsolePrintF(CC_ERROR, "Client #%d is dropped because it fails to send valid acks", cs->client_id);
01752 cs->CloseConnection(NETWORK_RECV_STATUS_SERVER_ERROR);
01753 continue;
01754 }
01755 } else if (cs->status == NetworkClientSocket::STATUS_PRE_ACTIVE) {
01756 uint lag = NetworkCalculateLag(cs);
01757 if (lag > _settings_client.network.max_join_time) {
01758 IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks for him to join", cs->client_id, _settings_client.network.max_join_time);
01759 cs->CloseConnection(NETWORK_RECV_STATUS_SERVER_ERROR);
01760 continue;
01761 }
01762 } else if (cs->status == NetworkClientSocket::STATUS_INACTIVE) {
01763 uint lag = NetworkCalculateLag(cs);
01764 if (lag > 4 * DAY_TICKS) {
01765 IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks to start the joining process", cs->client_id, 4 * DAY_TICKS);
01766 cs->CloseConnection(NETWORK_RECV_STATUS_SERVER_ERROR);
01767 continue;
01768 }
01769 }
01770
01771 if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) {
01772
01773 NetworkHandleCommandQueue(cs);
01774
01775
01776 if (send_frame) cs->SendFrame();
01777
01778 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
01779
01780 if (send_sync) cs->SendSync();
01781 #endif
01782 }
01783 }
01784
01785
01786 NetworkUDPAdvertise();
01787 }
01788
01790 void NetworkServerYearlyLoop()
01791 {
01792 NetworkCheckRestartMap();
01793 NetworkAdminUpdate(ADMIN_FREQUENCY_ANUALLY);
01794 }
01795
01797 void NetworkServerMonthlyLoop()
01798 {
01799 NetworkAutoCleanCompanies();
01800 NetworkAdminUpdate(ADMIN_FREQUENCY_MONTHLY);
01801 if ((_cur_month % 3) == 0) NetworkAdminUpdate(ADMIN_FREQUENCY_QUARTERLY);
01802 }
01803
01805 void NetworkServerDailyLoop()
01806 {
01807 NetworkAdminUpdate(ADMIN_FREQUENCY_DAILY);
01808 if ((_date % 7) == 3) NetworkAdminUpdate(ADMIN_FREQUENCY_WEEKLY);
01809 }
01810
01815 const char *ServerNetworkGameSocketHandler::GetClientIP()
01816 {
01817 return this->client_address.GetHostname();
01818 }
01819
01820 void NetworkServerShowStatusToConsole()
01821 {
01822 static const char * const stat_str[] = {
01823 "inactive",
01824 "checking NewGRFs",
01825 "authorizing (server password)",
01826 "authorizing (company password)",
01827 "authorized",
01828 "waiting",
01829 "loading map",
01830 "map done",
01831 "ready",
01832 "active"
01833 };
01834 assert_compile(lengthof(stat_str) == NetworkClientSocket::STATUS_END);
01835
01836 NetworkClientSocket *cs;
01837 FOR_ALL_CLIENT_SOCKETS(cs) {
01838 NetworkClientInfo *ci = cs->GetInfo();
01839 if (ci == NULL) continue;
01840 uint lag = NetworkCalculateLag(cs);
01841 const char *status;
01842
01843 status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
01844 IConsolePrintF(CC_INFO, "Client #%1d name: '%s' status: '%s' frame-lag: %3d company: %1d IP: %s",
01845 cs->client_id, ci->client_name, status, lag,
01846 ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
01847 cs->GetClientIP());
01848 }
01849 }
01850
01854 void NetworkServerSendConfigUpdate()
01855 {
01856 NetworkClientSocket *cs;
01857
01858 FOR_ALL_CLIENT_SOCKETS(cs) {
01859 if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) cs->SendConfigUpdate();
01860 }
01861 }
01862
01863 void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded)
01864 {
01865 if (NetworkCompanyIsPassworded(company_id) == passworded) return;
01866
01867 SB(_network_company_passworded, company_id, 1, !!passworded);
01868 SetWindowClassesDirty(WC_COMPANY);
01869
01870 NetworkClientSocket *cs;
01871 FOR_ALL_CLIENT_SOCKETS(cs) {
01872 if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) cs->SendCompanyUpdate();
01873 }
01874
01875 NetworkAdminCompanyUpdate(Company::GetIfValid(company_id));
01876 }
01877
01884 void NetworkServerDoMove(ClientID client_id, CompanyID company_id)
01885 {
01886
01887 if (client_id == CLIENT_ID_SERVER && _network_dedicated) return;
01888
01889 NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
01890
01891
01892 if (ci->client_playas == company_id) return;
01893
01894 ci->client_playas = company_id;
01895
01896 if (client_id == CLIENT_ID_SERVER) {
01897 SetLocalCompany(company_id);
01898 } else {
01899 NetworkClientSocket *cs = NetworkClientSocket::GetByClientID(client_id);
01900
01901 if (cs->status < NetworkClientSocket::STATUS_AUTHORIZED) return;
01902 cs->SendMove(client_id, company_id);
01903 }
01904
01905
01906 NetworkUpdateClientInfo(client_id);
01907
01908 NetworkAction action = (company_id == COMPANY_SPECTATOR) ? NETWORK_ACTION_COMPANY_SPECTATOR : NETWORK_ACTION_COMPANY_JOIN;
01909 NetworkServerSendChat(action, DESTTYPE_BROADCAST, 0, "", client_id, company_id + 1);
01910 }
01911
01912 void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const char *string)
01913 {
01914 NetworkClientSocket::GetByClientID(client_id)->SendRConResult(colour_code, string);
01915 }
01916
01917 static void NetworkServerSendError(ClientID client_id, NetworkErrorCode error)
01918 {
01919 NetworkClientSocket::GetByClientID(client_id)->SendError(error);
01920 }
01921
01922 void NetworkServerKickClient(ClientID client_id)
01923 {
01924 if (client_id == CLIENT_ID_SERVER) return;
01925 NetworkServerSendError(client_id, NETWORK_ERROR_KICKED);
01926 }
01927
01928 uint NetworkServerKickOrBanIP(ClientID client_id, bool ban)
01929 {
01930 return NetworkServerKickOrBanIP(NetworkClientSocket::GetByClientID(client_id)->GetClientIP(), ban);
01931 }
01932
01933 uint NetworkServerKickOrBanIP(const char *ip, bool ban)
01934 {
01935
01936 if (ban) *_network_ban_list.Append() = strdup(ip);
01937
01938 uint n = 0;
01939
01940
01941 NetworkClientSocket *cs;
01942 FOR_ALL_CLIENT_SOCKETS(cs) {
01943 if (cs->client_id == CLIENT_ID_SERVER) continue;
01944 if (cs->client_address.IsInNetmask(const_cast<char *>(ip))) {
01945 NetworkServerKickClient(cs->client_id);
01946 n++;
01947 }
01948 }
01949
01950 return n;
01951 }
01952
01953 bool NetworkCompanyHasClients(CompanyID company)
01954 {
01955 const NetworkClientInfo *ci;
01956 FOR_ALL_CLIENT_INFOS(ci) {
01957 if (ci->client_playas == company) return true;
01958 }
01959 return false;
01960 }
01961
01962
01968 void ServerNetworkGameSocketHandler::GetClientName(char *client_name, size_t size) const
01969 {
01970 const NetworkClientInfo *ci = this->GetInfo();
01971
01972 if (ci == NULL || StrEmpty(ci->client_name)) {
01973 snprintf(client_name, size, "Client #%4d", this->client_id);
01974 } else {
01975 ttd_strlcpy(client_name, ci->client_name, size);
01976 }
01977 }
01978
01982 void NetworkPrintClients()
01983 {
01984 NetworkClientInfo *ci;
01985 FOR_ALL_CLIENT_INFOS(ci) {
01986 IConsolePrintF(CC_INFO, _network_server ? "Client #%1d name: '%s' company: %1d IP: %s" : "Client #%1d name: '%s' company: %1d",
01987 ci->client_id,
01988 ci->client_name,
01989 ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
01990 _network_server ? (ci->client_id == CLIENT_ID_SERVER ? "server" : NetworkClientSocket::GetByClientID(ci->client_id)->GetClientIP()) : "");
01991 }
01992 }
01993
01994 #endif