• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Return-To-The-Roots / s25client / 25494921209

07 May 2026 12:10PM UTC coverage: 50.237% (+0.005%) from 50.232%
25494921209

push

github

web-flow
Merge pull request #1923 from DevOpsOfChaos/sidequest/direct-ip-wrong-password-message

0 of 2 new or added lines in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

23084 of 45950 relevant lines covered (50.24%)

43318.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.56
/libs/s25main/network/GameServer.cpp
1
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
2
//
3
// SPDX-License-Identifier: GPL-2.0-or-later
4

5
#include "GameServer.h"
6
#include "Debug.h"
7
#include "GameMessage.h"
8
#include "GameMessage_GameCommand.h"
9
#include "GameServerPlayer.h"
10
#include "GlobalGameSettings.h"
11
#include "JoinPlayerInfo.h"
12
#include "RTTR_Version.h"
13
#include "RttrConfig.h"
14
#include "Savegame.h"
15
#include "Settings.h"
16
#include "commonDefines.h"
17
#include "files.h"
18
#include "helpers/containerUtils.h"
19
#include "helpers/mathFuncs.h"
20
#include "helpers/random.h"
21
#include "network/CreateServerInfo.h"
22
#include "network/GameMessages.h"
23
#include "random/randomIO.h"
24
#include "gameTypes/LanGameInfo.h"
25
#include "gameTypes/TeamTypes.h"
26
#include "gameData/GameConsts.h"
27
#include "gameData/LanDiscoveryCfg.h"
28
#include "gameData/MaxPlayers.h"
29
#include "gameData/PortraitConsts.h"
30
#include "liblobby/LobbyClient.h"
31
#include "libsiedler2/ArchivItem_Map.h"
32
#include "libsiedler2/ArchivItem_Map_Header.h"
33
#include "libsiedler2/prototypen.h"
34
#include "s25util/SocketSet.h"
35
#include "s25util/colors.h"
36
#include "s25util/utf8.h"
37
#include <boost/container/static_vector.hpp>
38
#include <boost/filesystem.hpp>
39
#include <boost/nowide/convert.hpp>
40
#include <boost/nowide/fstream.hpp>
41
#include <helpers/chronoIO.h>
42
#include <iomanip>
43
#include <iterator>
44
#include <mygettext/mygettext.h>
45

46
struct GameServer::AsyncLog
47
{
48
    uint8_t playerId;
49
    bool done;
50
    AsyncChecksum checksum;
51
    std::string addData;
52
    std::vector<RandomEntry> randEntries;
53
    AsyncLog(uint8_t playerId, AsyncChecksum checksum) : playerId(playerId), done(false), checksum(checksum) {}
×
54
};
55

56
GameServer::ServerConfig::ServerConfig()
×
57
{
58
    Clear();
×
59
}
×
60

61
void GameServer::ServerConfig::Clear()
×
62
{
63
    servertype = ServerType::Local;
×
64
    gamename.clear();
×
65
    password.clear();
×
66
    port = 0;
×
67
    ipv6 = false;
×
68
}
×
69

70
GameServer::CountDown::CountDown() : isActive(false), remainingSecs(0) {}
×
71

72
void GameServer::CountDown::Start(unsigned timeInSec)
×
73
{
74
    isActive = true;
×
75
    remainingSecs = timeInSec;
×
76
    lasttime = SteadyClock::now();
×
77
}
×
78

79
void GameServer::CountDown::Stop()
×
80
{
81
    isActive = false;
×
82
}
×
83

84
bool GameServer::CountDown::Update()
×
85
{
86
    RTTR_Assert(isActive);
×
87
    SteadyClock::time_point curTime = SteadyClock::now();
×
88

89
    // Check if 1s has passed
90
    if(curTime - lasttime < std::chrono::seconds(1))
×
91
        return false;
×
92
    if(remainingSecs == 0)
×
93
    {
94
        Stop();
×
95
        return true;
×
96
    }
97
    // 1s has passed -> Reduce remaining time
98
    lasttime = curTime;
×
99
    remainingSecs--;
×
100
    return true;
×
101
}
102

103
GameServer::GameServer() : skiptogf(0), state(ServerState::Stopped), currentGF(0), lanAnnouncer(LAN_DISCOVERY_CFG) {}
×
104

105
GameServer::~GameServer()
×
106
{
107
    Stop();
×
108
}
×
109

110
///////////////////////////////////////////////////////////////////////////////
111
// Spiel hosten
112
bool GameServer::Start(const CreateServerInfo& csi, const MapDescription& map, const std::string& hostPw)
×
113
{
114
    Stop();
×
115

116
    // Name, Password und Kartenname kopieren
117
    config.gamename = csi.gameName;
×
118
    config.hostPassword = hostPw;
×
119
    config.password = csi.password;
×
120
    config.servertype = csi.type;
×
121
    config.port = csi.port;
×
122
    config.ipv6 = csi.ipv6;
×
123
    mapinfo.type = map.map_type;
×
124
    mapinfo.filepath = map.map_path;
×
125

126
    // Maps, Random-Maps, Savegames - Header laden und relevante Informationen rausschreiben (Map-Titel, Spieleranzahl)
127
    switch(mapinfo.type)
×
128
    {
129
        // Altes S2-Mapformat von BB
130
        case MapType::OldMap:
×
131
        {
132
            libsiedler2::Archiv map;
×
133

134
            // Karteninformationen laden
135
            if(libsiedler2::loader::LoadMAP(mapinfo.filepath, map, true) != 0)
×
136
            {
137
                LOG.write("GameServer::Start: ERROR: Map %1%, couldn't load header!\n") % mapinfo.filepath;
×
138
                return false;
×
139
            }
140
            const libsiedler2::ArchivItem_Map_Header& header =
141
              checkedCast<const libsiedler2::ArchivItem_Map*>(map.get(0))->getHeader();
×
142

143
            playerInfos.resize(header.getNumPlayers());
×
144
            mapinfo.title = s25util::ansiToUTF8(header.getName());
×
145
            ggs_.LoadSettings();
×
146
            currentGF = 0;
×
147
        }
148
        break;
×
149
        // Gespeichertes Spiel
150
        case MapType::Savegame:
×
151
        {
152
            Savegame save;
×
153

154
            if(!save.Load(mapinfo.filepath, SaveGameDataToLoad::HeaderAndSettings))
×
155
                return false;
×
156

157
            // Spieleranzahl
158
            playerInfos.resize(save.GetNumPlayers());
×
159
            mapinfo.title = save.GetMapName();
×
160

161
            for(unsigned i = 0; i < playerInfos.size(); ++i)
×
162
            {
163
                playerInfos[i] = JoinPlayerInfo(save.GetPlayer(i));
×
164
                // If it was a human we make it free, so someone can join
165
                if(playerInfos[i].ps == PlayerState::Occupied)
×
166
                    playerInfos[i].ps = PlayerState::Free;
×
167
            }
168

169
            ggs_ = save.ggs;
×
170
            currentGF = save.start_gf;
×
171
        }
172
        break;
×
173
    }
174

175
    if(playerInfos.empty())
×
176
    {
177
        LOG.write("Map %1% has no players!\n") % mapinfo.filepath;
×
178
        return false;
×
179
    }
180
    if(playerInfos.size() > MAX_PLAYERS)
×
181
    {
182
        LOG.write("Map %1% has too many players! (%1% > %2%)\n") % mapinfo.filepath % playerInfos.size() % MAX_PLAYERS;
×
183
        return false;
×
184
    }
185
    if(!mapinfo.mapData.CompressFromFile(mapinfo.filepath, &mapinfo.mapChecksum))
×
186
        return false;
×
187

188
    if(map.lua_path.has_value() && !bfs::is_regular_file(*map.lua_path))
×
189
        return false;
×
190

191
    bfs::path luaFilePath = map.lua_path.get_value_or(bfs::path(mapinfo.filepath).replace_extension("lua"));
×
192
    if(bfs::is_regular_file(luaFilePath))
×
193
    {
194
        if(!mapinfo.luaData.CompressFromFile(luaFilePath, &mapinfo.luaChecksum))
×
195
            return false;
×
196
        mapinfo.luaFilepath = luaFilePath;
×
197
    } else
198
        RTTR_Assert(mapinfo.luaFilepath.empty() && mapinfo.luaChecksum == 0);
×
199

200
    if(!mapinfo.verifySize())
×
201
    {
202
        LOG.write("Map %1% is to large!\n") % mapinfo.filepath;
×
203
        return false;
×
204
    }
205

206
    // ab in die Konfiguration
207
    state = ServerState::Config;
×
208

209
    // und das socket in listen-modus schicken
210
    if(!serversocket.Listen(config.port, config.ipv6, csi.use_upnp))
×
211
    {
212
        LOG.write("GameServer::Start: ERROR: Listening on port %d failed!\n") % config.port;
×
213
        LOG.writeLastError("Fehler");
×
214
        return false;
×
215
    }
216

217
    if(config.servertype == ServerType::LAN)
×
218
        lanAnnouncer.Start();
×
219
    else if(config.servertype == ServerType::Lobby)
×
220
    {
221
        LOBBYCLIENT.AddServer(config.gamename, mapinfo.title, (!config.password.empty()), config.port);
×
222
        LOBBYCLIENT.AddListener(this);
×
223
    }
224
    AnnounceStatusChange();
×
225

226
    return true;
×
227
}
228

229
unsigned GameServer::GetNumFilledSlots() const
×
230
{
231
    unsigned numFilled = 0;
×
232
    for(const JoinPlayerInfo& player : playerInfos)
×
233
    {
234
        if(player.ps != PlayerState::Free)
×
235
            ++numFilled;
×
236
    }
237
    return numFilled;
×
238
}
239

240
void GameServer::AnnounceStatusChange()
×
241
{
242
    if(config.servertype == ServerType::LAN)
×
243
    {
244
        LanGameInfo info;
×
245
        info.name = config.gamename;
×
246
        info.hasPwd = !config.password.empty();
×
247
        info.map = mapinfo.title;
×
248
        info.curNumPlayers = GetNumFilledSlots();
×
249
        info.maxNumPlayers = playerInfos.size();
×
250
        info.port = config.port;
×
251
        info.isIPv6 = config.ipv6;
×
252
        info.version = rttr::version::GetReadableVersion();
×
253
        info.revision = rttr::version::GetRevision();
×
254
        Serializer ser;
×
255
        info.Serialize(ser);
×
256
        lanAnnouncer.SetPayload(ser.GetData(), ser.GetLength());
×
257
    } else if(config.servertype == ServerType::Lobby)
×
258
    {
259
        if(LOBBYCLIENT.IsIngame())
×
260
            LOBBYCLIENT.UpdateServerNumPlayers(GetNumFilledSlots(), playerInfos.size());
×
261
    }
262
}
×
263

264
void GameServer::LC_Status_Error(const std::string& /*error*/)
×
265
{
266
    // Error during adding of server to lobby -> Stop
267
    Stop();
×
268
}
×
269

270
void GameServer::LC_Created()
×
271
{
272
    // All good -> Don't listen anymore
273
    LOBBYCLIENT.RemoveListener(this);
×
274
    AnnounceStatusChange();
×
275
}
×
276

277
void GameServer::Run()
×
278
{
279
    if(state == ServerState::Stopped)
×
280
        return;
×
281

282
    // auf tote Clients prüfen
283
    ClientWatchDog();
×
284

285
    // auf neue Clients warten
286
    if(state == ServerState::Config)
×
287
        RunStateConfig();
×
288
    else if(state == ServerState::Loading)
×
289
        RunStateLoading();
×
290
    else if(state == ServerState::Game)
×
291
        RunStateGame();
×
292

293
    // post zustellen
294
    FillPlayerQueues();
×
295

296
    // Execute messages
297
    for(GameServerPlayer& player : networkPlayers)
×
298
    {
299
        // Ignore kicked players
300
        if(!player.socket.isValid())
×
301
            continue;
×
302
        player.executeMsgs(*this);
×
303
    }
304
    // Send afterwards as most messages are relayed which should be done as fast as possible
305
    for(GameServerPlayer& player : networkPlayers)
×
306
    {
307
        // Ignore kicked players
308
        if(!player.socket.isValid())
×
309
            continue;
×
310
        player.sendMsgs(10);
×
311
    }
312
    helpers::erase_if(networkPlayers, [](const auto& player) { return !player.socket.isValid(); });
×
313

314
    lanAnnouncer.Run();
×
315
}
316

317
void GameServer::RunStateConfig()
×
318
{
319
    WaitForClients();
×
320
    if(countdown.IsActive() && countdown.Update())
×
321
    {
322
        // nun echt starten
323
        if(!countdown.IsActive())
×
324
        {
325
            if(!StartGame())
×
326
            {
327
                Stop();
×
328
                return;
×
329
            }
330
        } else
331
            SendToAll(GameMessage_Countdown(countdown.GetRemainingSecs()));
×
332
    }
333
}
334

335
void GameServer::RunStateLoading()
×
336
{
337
    if(!nwfInfo.isReady())
×
338
    {
339
        if(SteadyClock::now() - loadStartTime > std::chrono::seconds(LOAD_TIMEOUT))
×
340
        {
341
            for(const NWFPlayerInfo& player : nwfInfo.getPlayerInfos())
×
342
            {
343
                if(player.isLagging)
×
344
                    KickPlayer(player.id, KickReason::PingTimeout, __LINE__);
×
345
            }
346
        }
347
        return;
×
348
    }
349
    LOG.write("SERVER: Game loaded by all players after %1%\n")
×
350
      % helpers::withUnit(std::chrono::duration_cast<std::chrono::seconds>(SteadyClock::now() - loadStartTime));
×
351
    // The first NWF is ready. Server has to set up "missing" commands so every future command is for the correct NWF as
352
    // specified with cmdDelay. We have commands for NWF 0. When clients execute this they will send the commands for
353
    // NWF cmdDelay. So commands for NWF 1..cmdDelay-1 are missing. Do this here and before the NWFDone is sent,
354
    // otherwise we might get them in a wrong order when messages are sent asynchronously
355
    for(unsigned i = 1; i < nwfInfo.getCmdDelay(); i++)
×
356
    {
357
        for(const NWFPlayerInfo& player : nwfInfo.getPlayerInfos())
×
358
        {
359
            GameMessage_GameCommand msg(player.id, nwfInfo.getPlayerCmds(player.id).checksum,
×
360
                                        std::vector<gc::GameCommandPtr>());
×
361
            SendToAll(msg);
×
362
            nwfInfo.addPlayerCmds(player.id, msg.cmds);
×
363
        }
364
    }
365

366
    NWFServerInfo serverInfo = nwfInfo.getServerInfo();
×
367
    // Send cmdDelay NWFDone messages
368
    // First send the OK for NWF 0 which is also the game ready command
369
    // Note: Do not store. It already is in NWFInfo
370
    SendToAll(GameMessage_Server_NWFDone(serverInfo.gf, serverInfo.newGFLen, serverInfo.nextNWF));
×
371
    RTTR_Assert(framesinfo.nwf_length > 0);
×
372
    // Then the remaining OKs for the commands sent above
373
    for(unsigned i = 1; i < nwfInfo.getCmdDelay(); i++)
×
374
    {
375
        serverInfo.gf = serverInfo.nextNWF;
×
376
        serverInfo.nextNWF += framesinfo.nwf_length;
×
377
        SendNWFDone(serverInfo);
×
378
    }
379

380
    // And go!
381
    framesinfo.lastTime = FramesInfo::UsedClock::now();
×
382
    state = ServerState::Game;
×
383
}
384

385
void GameServer::RunStateGame()
×
386
{
387
    if(!framesinfo.isPaused)
×
388
        ExecuteGameFrame();
×
389
}
×
390

391
void GameServer::Stop()
×
392
{
393
    if(state == ServerState::Stopped)
×
394
        return;
×
395

396
    // player verabschieden
397
    playerInfos.clear();
×
398
    networkPlayers.clear();
×
399

400
    // aufräumen
401
    framesinfo.Clear();
×
402
    config.Clear();
×
403
    mapinfo.Clear();
×
404
    countdown.Stop();
×
405

406
    // laden dicht machen
407
    serversocket.Close();
×
408
    // clear jump target
409
    skiptogf = 0;
×
410

411
    // clear async logs
412
    asyncLogs.clear();
×
413

414
    lanAnnouncer.Stop();
×
415

416
    if(LOBBYCLIENT.IsLoggedIn()) // steht die Lobbyverbindung noch?
×
417
        LOBBYCLIENT.DeleteServer();
×
418
    LOBBYCLIENT.RemoveListener(this);
×
419

420
    // status
421
    state = ServerState::Stopped;
×
422
    LOG.write("server state changed to stop\n");
×
423
}
424

425
// Check if there are players that have not been assigned a team but only a random team.
426
// Those players are assigned a team now optionally trying to balance the number of players per team.
427
// Returns true iff players have been assigned.
428
bool GameServer::assignPlayersOfRandomTeams(std::vector<JoinPlayerInfo>& playerInfos)
28✔
429
{
430
    static_assert(NUM_TEAMS == 4, "Expected exactly 4 playable teams!");
431
    RTTR_Assert(playerInfos.size() <= MAX_PLAYERS);
28✔
432

433
    using boost::container::static_vector;
434
    using PlayerIndex = unsigned;
435
    using TeamIndex = unsigned;
436
    const auto teamIdxToTeam = [](const TeamIndex teamNum) {
93✔
437
        RTTR_Assert(teamNum < NUM_TEAMS);
93✔
438
        return Team(static_cast<TeamIndex>(Team::Team1) + teamNum);
93✔
439
    };
440

441
    std::array<unsigned, NUM_TEAMS> numPlayersInTeam{};
28✔
442
    struct AssignPlayer
443
    {
444
        PlayerIndex player;
445
        static_vector<TeamIndex, NUM_TEAMS> possibleTeams;
446
        TeamIndex chosenTeam = 0;
447
    };
448

449
    static_vector<AssignPlayer, MAX_PLAYERS> playersToAssign;
×
450
    auto rng = helpers::getRandomGenerator();
28✔
451

452
    bool playerWasAssigned = false;
28✔
453

454
    // Assign (fully) random teams, count players in team and sort into playersToAssign
455
    for(PlayerIndex player = 0; player < playerInfos.size(); ++player)
184✔
456
    {
457
        auto& playerInfo = playerInfos[player];
156✔
458
        if(playerInfo.team == Team::Random)
156✔
459
        {
460
            const TeamIndex randTeam = std::uniform_int_distribution<TeamIndex>{0, NUM_TEAMS - 1u}(rng);
2✔
461
            playerInfo.team = teamIdxToTeam(randTeam);
2✔
462
            playerWasAssigned = true;
2✔
463
        }
464
        switch(playerInfo.team)
156✔
465
        {
466
            case Team::Team1: ++numPlayersInTeam[0]; break;
28✔
467
            case Team::Team2: ++numPlayersInTeam[1]; break;
7✔
468
            case Team::Team3: ++numPlayersInTeam[2]; break;
24✔
469
            case Team::Team4: ++numPlayersInTeam[3]; break;
4✔
470
            case Team::Random1To2: playersToAssign.emplace_back(AssignPlayer{player, {0, 1}}); break;
69✔
471
            case Team::Random1To3: playersToAssign.emplace_back(AssignPlayer{player, {0, 1, 2}}); break;
162✔
472
            case Team::Random1To4: playersToAssign.emplace_back(AssignPlayer{player, {0, 1, 2, 3}}); break;
42✔
473
            case Team::Random: RTTR_Assert(false); break;
×
474
            case Team::None: break;
2✔
475
        }
476
    }
477

478
    // To make the teams as even as possible we start to assign the most constrained players first
479
    helpers::sort(playersToAssign, [](const AssignPlayer& lhs, const AssignPlayer& rhs) {
28✔
480
        return lhs.possibleTeams.size() < rhs.possibleTeams.size();
414✔
481
    });
482

483
    // Put each player into a random team with the currently least amount of players using the possible teams only
484
    for(AssignPlayer& player : playersToAssign)
266✔
485
    {
486
        // Determine the teams with the minima size for the currently possible teams and choose one randomly
487
        unsigned minNextTeamSize = std::numeric_limits<unsigned>::max();
91✔
488
        static_vector<TeamIndex, NUM_TEAMS> teamsForNextPlayer;
×
489
        for(const TeamIndex team : player.possibleTeams)
801✔
490
        {
491
            if(minNextTeamSize > numPlayersInTeam[team])
264✔
492
            {
493
                teamsForNextPlayer.clear();
494
                teamsForNextPlayer.push_back(team);
495
                minNextTeamSize = numPlayersInTeam[team];
135✔
496
            } else if(minNextTeamSize == numPlayersInTeam[team])
129✔
497
                teamsForNextPlayer.push_back(team);
498
        }
499
        player.chosenTeam = helpers::getRandomElement(rng, teamsForNextPlayer);
91✔
500

501
        ++numPlayersInTeam[player.chosenTeam];
91✔
502
        playerWasAssigned = true;
91✔
503
    }
504
    // Now the teams are as even as possible and the uneven team(s) is a random one within the constraints
505
    // To have some more randomness we swap players within their constraints
506
    std::shuffle(playersToAssign.begin(), playersToAssign.end(), rng);
28✔
507
    for(auto it = playersToAssign.begin(); it != playersToAssign.end(); ++it)
210✔
508
    {
509
        // Search for a random player with which we can swap, including ourselfes
510
        // Go only forward to avoid back-swapping
511
        static_vector<decltype(it), MAX_PLAYERS> possibleSwapTargets;
×
512
        for(auto it2 = it; it2 != playersToAssign.end(); ++it2)
555✔
513
        {
514
            if(helpers::contains(it->possibleTeams, it2->chosenTeam)
464✔
515
               && helpers::contains(it2->possibleTeams, it->chosenTeam))
672✔
516
                possibleSwapTargets.push_back(it2);
517
        }
518
        const auto itSwapTarget = helpers::getRandomElement(rng, possibleSwapTargets);
91✔
519
        std::swap(it->chosenTeam, itSwapTarget->chosenTeam);
182✔
520
        playerInfos[it->player].team = teamIdxToTeam(it->chosenTeam);
182✔
521
    }
522

523
    return playerWasAssigned;
56✔
524
}
525

526
bool GameServer::StartGame()
×
527
{
528
    lanAnnouncer.Stop();
×
529

530
    // Finalize the team selection for unassigned players.
531
    if(assignPlayersOfRandomTeams(playerInfos))
×
532
        SendToAll(GameMessage_Player_List(playerInfos));
×
533

534
    // Bei Savegames wird der Startwert von den Clients aus der Datei gelesen!
535
    unsigned random_init;
536
    if(mapinfo.type == MapType::Savegame)
×
537
        random_init = 0;
×
538
    else
539
        random_init = static_cast<unsigned>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
×
540

541
    nwfInfo.init(currentGF, 3);
×
542

543
    // Send start first, then load the rest
544
    SendToAll(GameMessage_Server_Start(random_init, nwfInfo.getNextNWF(), nwfInfo.getCmdDelay()));
×
545
    LOG.writeToFile("SERVER >>> BROADCAST: NMS_SERVER_START(%d)\n") % random_init;
×
546

547
    // Höchsten Ping ermitteln
548
    unsigned highest_ping = 0;
×
549
    for(const JoinPlayerInfo& player : playerInfos)
×
550
    {
551
        if(player.ps == PlayerState::Occupied)
×
552
        {
553
            if(player.ping > highest_ping)
×
554
                highest_ping = player.ping;
×
555
        }
556
    }
557

558
    framesinfo.gfLengthReq = framesinfo.gf_length = SPEED_GF_LENGTHS[ggs_.speed];
×
559

560
    // NetworkFrame-Länge bestimmen, je schlechter (also höher) die Pings, desto länger auch die Framelänge
561
    framesinfo.nwf_length = CalcNWFLenght(FramesInfo::milliseconds32_t(highest_ping));
×
562

563
    LOG.write("SERVER: Using gameframe length of %1%\n") % helpers::withUnit(framesinfo.gf_length);
×
564
    LOG.write("SERVER: Using networkframe length of %1% GFs (%2%)\n") % framesinfo.nwf_length
×
565
      % helpers::withUnit(framesinfo.nwf_length * framesinfo.gf_length);
×
566

567
    for(unsigned id = 0; id < playerInfos.size(); id++)
×
568
    {
569
        if(playerInfos[id].isUsed())
×
570
            nwfInfo.addPlayer(id);
×
571
    }
572

573
    // Add server info so nwfInfo can be ready but do NOT send it yet, as we wait for the player commands before sending
574
    // the done msg
575
    nwfInfo.addServerInfo(NWFServerInfo(currentGF, framesinfo.gf_length / FramesInfo::milliseconds32_t(1),
×
576
                                        currentGF + framesinfo.nwf_length));
×
577

578
    state = ServerState::Loading;
×
579
    loadStartTime = SteadyClock::now();
×
580

581
    return true;
×
582
}
583

584
unsigned GameServer::CalcNWFLenght(FramesInfo::milliseconds32_t minDuration) const
×
585
{
586
    constexpr unsigned maxNumGF = 20;
×
587
    for(unsigned i = 1; i < maxNumGF; ++i)
×
588
    {
589
        if(i * framesinfo.gf_length >= minDuration)
×
590
            return i;
×
591
    }
592
    return maxNumGF;
×
593
}
594

595
void GameServer::SendNWFDone(const NWFServerInfo& info)
×
596
{
597
    nwfInfo.addServerInfo(info);
×
598
    SendToAll(GameMessage_Server_NWFDone(info.gf, info.newGFLen, info.nextNWF));
×
599
}
×
600

601
void GameServer::SendToAll(const GameMessage& msg)
×
602
{
603
    for(GameServerPlayer& player : networkPlayers)
×
604
    {
605
        // ist der Slot Belegt, dann Nachricht senden
606
        if(player.isActive())
×
607
            player.sendMsgAsync(msg.clone());
×
608
    }
609
}
×
610

611
void GameServer::KickPlayer(uint8_t playerId, KickReason cause, uint32_t param)
×
612
{
613
    if(playerId >= playerInfos.size())
×
614
        return;
×
615
    JoinPlayerInfo& playerInfo = playerInfos[playerId];
×
616
    GameServerPlayer* player = GetNetworkPlayer(playerId);
×
617
    if(player)
×
618
        player->closeConnection();
×
619
    // Non-existing or connecting player
620
    if(!playerInfo.isUsed())
×
621
        return;
×
622
    playerInfo.ps = PlayerState::Free;
×
623

624
    SendToAll(GameMessage_Player_Kicked(playerId, cause, param));
×
625

626
    // If we are ingame, replace by KI
627
    if(state == ServerState::Game || state == ServerState::Loading)
×
628
    {
629
        playerInfo.ps = PlayerState::AI;
×
630
        playerInfo.aiInfo = AI::Info(AI::Type::Dummy);
×
631
    } else
632
        CancelCountdown();
×
633

634
    AnnounceStatusChange();
×
635
    LOG.writeToFile("SERVER >>> BROADCAST: NMS_PLAYERKICKED(%d,%d,%d)\n") % unsigned(playerId) % unsigned(cause)
×
636
      % unsigned(param);
×
637
}
638

639
// testet, ob in der Verbindungswarteschlange Clients auf Verbindung warten
640
void GameServer::ClientWatchDog()
×
641
{
642
    SocketSet set;
×
643
    set.Clear();
×
644

645
    // sockets zum set hinzufügen
646
    for(GameServerPlayer& player : networkPlayers)
×
647
        set.Add(player.socket);
×
648

649
    // auf fehler prüfen
650
    if(set.Select(0, 2) > 0)
×
651
    {
652
        for(const GameServerPlayer& player : networkPlayers)
×
653
        {
654
            if(set.InSet(player.socket))
×
655
            {
656
                LOG.write(_("SERVER: Error on socket of player %1%, bye bye!\n")) % player.playerId;
×
657
                KickPlayer(player.playerId, KickReason::ConnectionLost, __LINE__);
×
658
            }
659
        }
660
    }
661

662
    for(GameServerPlayer& player : networkPlayers)
×
663
    {
664
        if(player.hasTimedOut())
×
665
        {
666
            LOG.write(_("SERVER: Reserved slot %1% freed due to timeout\n")) % player.playerId;
×
667
            KickPlayer(player.playerId, KickReason::PingTimeout, __LINE__);
×
668
        } else
669
            player.doPing();
×
670
    }
671
}
×
672

673
void GameServer::ExecuteGameFrame()
×
674
{
675
    RTTR_Assert(state == ServerState::Game);
×
676

677
    FramesInfo::UsedClock::time_point currentTime = FramesInfo::UsedClock::now();
×
678
    FramesInfo::milliseconds32_t passedTime =
679
      std::chrono::duration_cast<FramesInfo::milliseconds32_t>(currentTime - framesinfo.lastTime);
×
680

681
    // prüfen ob GF vergangen
682
    if(passedTime >= framesinfo.gf_length || skiptogf > currentGF)
×
683
    {
684
        // NWF vergangen?
685
        if(currentGF == nwfInfo.getNextNWF())
×
686
        {
687
            if(CheckForLaggingPlayers())
×
688
            {
689
                // Check for kicking every second
690
                static FramesInfo::UsedClock::time_point lastLagKickTime;
691
                if(currentTime - lastLagKickTime >= std::chrono::seconds(1))
×
692
                {
693
                    lastLagKickTime = currentTime;
×
694
                    CheckAndKickLaggingPlayers();
×
695
                }
696
                // Skip the rest
697
                return;
×
698
            } else
699
                ExecuteNWF();
×
700
        }
701
        // Advance GF
702
        ++currentGF;
×
703
        // Normally we set lastTime = curTime (== lastTime + passedTime) where passedTime is ideally 1 GF
704
        // But we might got called late, so we advance the time by 1 GF anyway so in that case we execute the next GF a
705
        // bit earlier. Exception: We lag many GFs behind, then we advance by the full passedTime - 1 GF which means we
706
        // are now only 1 GF behind and execute that on the next call
707
        if(passedTime <= 4 * framesinfo.gf_length)
×
708
            passedTime = framesinfo.gf_length;
×
709
        else
710
            passedTime -= framesinfo.gf_length;
×
711
        framesinfo.lastTime += passedTime;
×
712
    }
713
}
714

715
void GameServer::ExecuteNWF()
×
716
{
717
    // Check for asyncs
718
    if(CheckForAsync())
×
719
    {
720
        // Pause game
721
        RTTR_Assert(!framesinfo.isPaused);
×
722
        SetPaused(true);
×
723

724
        // Notify players
725
        std::vector<unsigned> checksumHashes;
×
726
        checksumHashes.reserve(networkPlayers.size());
×
727
        for(const GameServerPlayer& player : networkPlayers)
×
728
            checksumHashes.push_back(nwfInfo.getPlayerCmds(player.playerId).checksum.getHash());
×
729
        SendToAll(GameMessage_Server_Async(checksumHashes));
×
730

731
        // Request async logs
732
        for(GameServerPlayer& player : networkPlayers)
×
733
        {
734
            asyncLogs.push_back(AsyncLog(player.playerId, nwfInfo.getPlayerCmds(player.playerId).checksum));
×
735
            player.sendMsgAsync(new GameMessage_GetAsyncLog());
×
736
        }
737
    }
738
    const NWFServerInfo serverInfo = nwfInfo.getServerInfo();
×
739
    RTTR_Assert(serverInfo.gf == currentGF);
×
740
    RTTR_Assert(serverInfo.nextNWF > currentGF);
×
741
    // First save old values
742
    unsigned lastNWF = nwfInfo.getLastNWF();
×
743
    FramesInfo::milliseconds32_t oldGFLen = framesinfo.gf_length;
×
744
    nwfInfo.execute(framesinfo);
×
745
    if(oldGFLen != framesinfo.gf_length)
×
746
    {
747
        LOG.write(_("SERVER: At GF %1%: Speed changed from %2% to %3%. NWF %4%\n")) % currentGF
×
748
          % helpers::withUnit(oldGFLen) % helpers::withUnit(framesinfo.gf_length) % framesinfo.nwf_length;
×
749
    }
750
    NWFServerInfo newInfo(lastNWF, framesinfo.gfLengthReq / FramesInfo::milliseconds32_t(1),
×
751
                          lastNWF + framesinfo.nwf_length);
×
752
    if(framesinfo.gfLengthReq != framesinfo.gf_length)
×
753
    {
754
        // Speed will change, adjust nwf length so the time will stay constant
755
        using namespace std::chrono;
756
        using MsDouble = duration<double, std::milli>;
757
        double newNWFLen =
758
          framesinfo.nwf_length * framesinfo.gf_length / duration_cast<MsDouble>(framesinfo.gfLengthReq);
×
759
        newInfo.nextNWF = lastNWF + std::max(1u, helpers::iround<unsigned>(newNWFLen));
×
760
    }
761
    SendNWFDone(newInfo);
×
762
}
×
763

764
bool GameServer::CheckForAsync()
×
765
{
766
    if(networkPlayers.empty())
×
767
        return false;
×
768
    bool isAsync = false;
×
769
    const AsyncChecksum& refChecksum = nwfInfo.getPlayerCmds(networkPlayers.front().playerId).checksum;
×
770
    for(const GameServerPlayer& player : networkPlayers)
×
771
    {
772
        const AsyncChecksum& curChecksum = nwfInfo.getPlayerCmds(player.playerId).checksum;
×
773

774
        // Checksummen nicht gleich?
775
        if(curChecksum != refChecksum)
×
776
        {
777
            LOG.write(_("Async at GF %1% of player %2% vs %3%. Checksums:\n%4%\n%5%\n\n")) % currentGF % player.playerId
×
778
              % networkPlayers.front().playerId % curChecksum % refChecksum;
×
779
            isAsync = true;
×
780
        }
781
    }
782
    return isAsync;
×
783
}
784

785
void GameServer::CheckAndKickLaggingPlayers()
×
786
{
787
    for(const GameServerPlayer& player : networkPlayers)
×
788
    {
789
        const unsigned timeOut = player.getLagTimeOut();
×
790
        if(timeOut == 0)
×
791
            KickPlayer(player.playerId, KickReason::PingTimeout, __LINE__);
×
792
        else if(timeOut <= 30
×
793
                && (timeOut % 5 == 0
×
794
                    || timeOut < 5)) // Notify every 5s if max 30s are remaining, if less than 5s notify every second
×
795
            LOG.write(_("SERVER: Kicking player %1% in %2% seconds\n")) % player.playerId % timeOut;
×
796
    }
797
}
×
798

799
bool GameServer::CheckForLaggingPlayers()
×
800
{
801
    if(nwfInfo.isReady())
×
802
        return false;
×
803
    for(GameServerPlayer& player : networkPlayers)
×
804
    {
805
        if(nwfInfo.getPlayerInfo(player.playerId).isLagging)
×
806
            player.setLagging();
×
807
    }
808
    return true;
×
809
}
810

811
// testet, ob in der Verbindungswarteschlange Clients auf Verbindung warten
812
void GameServer::WaitForClients()
×
813
{
814
    SocketSet set;
×
815

816
    set.Add(serversocket);
×
817
    if(set.Select(0, 0) > 0)
×
818
    {
819
        RTTR_Assert(set.InSet(serversocket));
×
820
        Socket socket = serversocket.Accept();
×
821

822
        // Verbindung annehmen
823
        if(!socket.isValid())
×
824
            return;
×
825

826
        unsigned newPlayerId = GameMessageWithPlayer::NO_PLAYER_ID;
×
827
        // Geeigneten Platz suchen
828
        for(unsigned playerId = 0; playerId < playerInfos.size(); ++playerId)
×
829
        {
830
            if(playerInfos[playerId].ps == PlayerState::Free && !GetNetworkPlayer(playerId))
×
831
            {
832
                networkPlayers.push_back(GameServerPlayer(playerId, socket));
×
833
                newPlayerId = playerId;
×
834
                break;
×
835
            }
836
        }
837

838
        GameMessage_Player_Id msg(newPlayerId);
×
839
        MessageHandler::send(socket, msg);
×
840

841
        // war kein platz mehr frei, wenn ja dann verbindung trennen?
842
        if(newPlayerId == 0xFFFFFFFF)
×
843
            socket.Close();
×
844
    }
845
}
846

847
// füllt die warteschlangen mit "paketen"
848
void GameServer::FillPlayerQueues()
×
849
{
850
    SocketSet set;
×
851
    bool msgReceived = false;
×
852

853
    // erstmal auf Daten überprüfen
854
    do
×
855
    {
856
        // sockets zum set hinzufügen
857
        for(const GameServerPlayer& player : networkPlayers)
×
858
            set.Add(player.socket);
×
859

860
        msgReceived = false;
×
861

862
        // ist eines der Sockets im Set lesbar?
863
        if(set.Select(0, 0) > 0)
×
864
        {
865
            for(GameServerPlayer& player : networkPlayers)
×
866
            {
867
                if(set.InSet(player.socket))
×
868
                {
869
                    // nachricht empfangen
870
                    if(!player.receiveMsgs())
×
871
                    {
872
                        LOG.write(_("SERVER: Receiving Message for player %1% failed, kicking...\n")) % player.playerId;
×
873
                        KickPlayer(player.playerId, KickReason::ConnectionLost, __LINE__);
×
874
                    } else
875
                        msgReceived = true;
×
876
                }
877
            }
878
        }
879
    } while(msgReceived);
880
}
×
881

882
bool GameServer::OnGameMessage(const GameMessage_Pong& msg)
×
883
{
884
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
885
    if(player)
×
886
    {
887
        unsigned ping = player->calcPingTime();
×
888
        if(ping == 0u)
×
889
            return true;
×
890
        playerInfos[msg.senderPlayerID].ping = ping;
×
891
        SendToAll(GameMessage_Player_Ping(msg.senderPlayerID, ping));
×
892
    }
893
    return true;
×
894
}
895

896
bool GameServer::OnGameMessage(const GameMessage_Server_Type& msg)
×
897
{
898
    if(state != ServerState::Config)
×
899
    {
900
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
901
        return true;
×
902
    }
903

904
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
905
    if(!player)
×
906
        return true;
×
907

908
    auto typeok = GameMessage_Server_TypeOK::StatusCode::Ok;
×
909
    if(msg.type != config.servertype)
×
910
        typeok = GameMessage_Server_TypeOK::StatusCode::InvalidServerType;
×
911
    else if(msg.revision != rttr::version::GetRevision())
×
912
        typeok = GameMessage_Server_TypeOK::StatusCode::WrongVersion;
×
913

914
    player->sendMsg(GameMessage_Server_TypeOK(typeok, rttr::version::GetRevision()));
×
915

916
    if(typeok != GameMessage_Server_TypeOK::StatusCode::Ok)
×
917
        KickPlayer(msg.senderPlayerID, KickReason::ConnectionLost, __LINE__);
×
918
    return true;
×
919
}
920

921
bool GameServer::OnGameMessage(const GameMessage_Server_Password& msg)
×
922
{
923
    if(state != ServerState::Config)
×
924
    {
925
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
926
        return true;
×
927
    }
928

929
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
930
    if(!player)
×
931
        return true;
×
932

933
    std::string passwordok = (config.password == msg.password ? "true" : "false");
×
934
    if(msg.password == config.hostPassword)
×
935
    {
936
        passwordok = "true";
×
937
        playerInfos[msg.senderPlayerID].isHost = true;
×
938
    } else
939
        playerInfos[msg.senderPlayerID].isHost = false;
×
940

941
    if(passwordok == "false")
×
942
    {
NEW
943
        player->sendMsg(GameMessage_Server_Password(passwordok));
×
944
        KickPlayer(msg.senderPlayerID, KickReason::WrongPassword, __LINE__);
×
945
    } else
NEW
946
        player->sendMsgAsync(new GameMessage_Server_Password(passwordok));
×
UNCOV
947
    return true;
×
948
}
949

950
bool GameServer::OnGameMessage(const GameMessage_Chat& msg)
×
951
{
952
    int playerID = GetTargetPlayer(msg);
×
953
    if(playerID >= 0)
×
954
        SendToAll(GameMessage_Chat(playerID, msg.destination, msg.text));
×
955
    return true;
×
956
}
957

958
bool GameServer::OnGameMessage(const GameMessage_Player_State& msg)
×
959
{
960
    // `Occupied` is set only when a real player went through the join protocol
961
    // See `GameMessage_Map_Checksum` handling
962
    if(state != ServerState::Config || msg.ps == PlayerState::Occupied)
×
963
    {
964
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
965
        return true;
×
966
    }
967

968
    int playerID = GetTargetPlayer(msg);
×
969
    if(playerID < 0)
×
970
        return true;
×
971
    JoinPlayerInfo& player = playerInfos[playerID];
×
972
    const PlayerState oldPs = player.ps;
×
973
    if(playerID != msg.senderPlayerID)
×
974
    {
975
        // Remove the player currently occupying the target slot, if any
976
        if(GetNetworkPlayer(playerID))
×
977
            KickPlayer(playerID, KickReason::NoCause, __LINE__);
×
978

979
        if(mapinfo.type == MapType::Savegame)
×
980
        {
981
            // For savegames we cannot set anyone on a locked slot as the player does not exist on the map
982
            if(player.ps != PlayerState::Locked)
×
983
            {
984
                // And we don't lock!
985
                player.ps = msg.ps == PlayerState::Locked ? PlayerState::Free : msg.ps;
×
986
                player.aiInfo = msg.aiInfo;
×
987
            }
988
        } else
989
        {
990
            player.ps = msg.ps;
×
991
            player.aiInfo = msg.aiInfo;
×
992
        }
993
        // Free slots on single player games always get occupied by AI
994
        if(player.ps == PlayerState::Free && config.servertype == ServerType::Local)
×
995
        {
996
            player.ps = PlayerState::AI;
×
997
            player.aiInfo = AI::Info(AI::Type::Default);
×
998
        }
999
    }
1000
    // Even when nothing changed we send the data because the other players might have expected a change
1001

1002
    if(player.ps == PlayerState::AI)
×
1003
    {
1004
        player.SetAIName(playerID);
×
1005
        SendToAll(GameMessage_Player_Name(playerID, player.name));
×
1006
    }
1007
    // If slot is filled, check current color
1008
    if(player.isUsed())
×
1009
    {
1010
        // Ensure a unique color only when initially filling the slot
1011
        CheckAndSetColor(playerID, player.color, oldPs == PlayerState::Free || oldPs == PlayerState::Locked);
×
1012
    }
1013
    SendToAll(GameMessage_Player_State(playerID, player.ps, player.aiInfo));
×
1014

1015
    if(oldPs != player.ps)
×
1016
        player.isReady = (player.ps == PlayerState::AI);
×
1017
    SendToAll(GameMessage_Player_Ready(playerID, player.isReady));
×
1018
    PlayerDataChanged(playerID);
×
1019
    AnnounceStatusChange();
×
1020
    return true;
×
1021
}
1022

1023
bool GameServer::OnGameMessage(const GameMessage_Player_Name& msg)
×
1024
{
1025
    if(state != ServerState::Config)
×
1026
    {
1027
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1028
        return true;
×
1029
    }
1030
    int playerID = GetTargetPlayer(msg);
×
1031
    if(playerID < 0)
×
1032
        return true;
×
1033

1034
    LOG.writeToFile("CLIENT%d >>> SERVER: NMS_PLAYER_NAME(%s)\n") % playerID % msg.playername;
×
1035

1036
    playerInfos[playerID].name = msg.playername;
×
1037
    SendToAll(GameMessage_Player_Name(playerID, msg.playername));
×
1038
    PlayerDataChanged(playerID);
×
1039

1040
    return true;
×
1041
}
1042

1043
bool GameServer::OnGameMessage(const GameMessage_Player_Portrait& msg)
×
1044
{
1045
    if(state != ServerState::Config)
×
1046
    {
1047
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1048
        return true;
×
1049
    }
1050
    int playerID = GetTargetPlayer(msg);
×
1051
    if(playerID < 0)
×
1052
        return true;
×
1053
    if(msg.playerPortraitIndex >= Portraits.size())
×
1054
        return true;
×
1055

1056
    LOG.writeToFile("CLIENT%d >>> SERVER: NMS_PLAYER_PORTRAIT(%u)\n") % playerID % msg.playerPortraitIndex;
×
1057
    playerInfos[playerID].portraitIndex = msg.playerPortraitIndex;
×
1058
    SendToAll(GameMessage_Player_Portrait(playerID, msg.playerPortraitIndex));
×
1059
    PlayerDataChanged(playerID);
×
1060

1061
    return true;
×
1062
}
1063

1064
bool GameServer::OnGameMessage(const GameMessage_Player_Nation& msg)
×
1065
{
1066
    if(state != ServerState::Config)
×
1067
    {
1068
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1069
        return true;
×
1070
    }
1071
    int playerID = GetTargetPlayer(msg);
×
1072
    if(playerID < 0)
×
1073
        return true;
×
1074

1075
    playerInfos[playerID].nation = msg.nation;
×
1076

1077
    SendToAll(GameMessage_Player_Nation(playerID, msg.nation));
×
1078
    PlayerDataChanged(playerID);
×
1079
    return true;
×
1080
}
1081

1082
bool GameServer::OnGameMessage(const GameMessage_Player_Team& msg)
×
1083
{
1084
    if(state != ServerState::Config)
×
1085
    {
1086
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1087
        return true;
×
1088
    }
1089
    int playerID = GetTargetPlayer(msg);
×
1090
    if(playerID < 0)
×
1091
        return true;
×
1092

1093
    playerInfos[playerID].team = msg.team;
×
1094

1095
    SendToAll(GameMessage_Player_Team(playerID, msg.team));
×
1096
    PlayerDataChanged(playerID);
×
1097
    return true;
×
1098
}
1099

1100
bool GameServer::OnGameMessage(const GameMessage_Player_Color& msg)
×
1101
{
1102
    if(state != ServerState::Config)
×
1103
    {
1104
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1105
        return true;
×
1106
    }
1107
    int playerID = GetTargetPlayer(msg);
×
1108
    if(playerID < 0)
×
1109
        return true;
×
1110

1111
    CheckAndSetColor(playerID, msg.color, false);
×
1112
    PlayerDataChanged(playerID);
×
1113
    return true;
×
1114
}
1115

1116
bool GameServer::OnGameMessage(const GameMessage_Player_Ready& msg)
×
1117
{
1118
    if(state != ServerState::Config)
×
1119
    {
1120
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1121
        return true;
×
1122
    }
1123
    int playerID = GetTargetPlayer(msg);
×
1124
    if(playerID < 0)
×
1125
        return true;
×
1126

1127
    JoinPlayerInfo& player = playerInfos[playerID];
×
1128

1129
    player.isReady = msg.ready;
×
1130

1131
    // countdown ggf abbrechen
1132
    if(!player.isReady && countdown.IsActive())
×
1133
        CancelCountdown();
×
1134

1135
    SendToAll(GameMessage_Player_Ready(playerID, msg.ready));
×
1136
    return true;
×
1137
}
1138

1139
bool GameServer::OnGameMessage(const GameMessage_MapRequest& msg)
×
1140
{
1141
    if(state != ServerState::Config)
×
1142
    {
1143
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1144
        return true;
×
1145
    }
1146
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
1147
    if(!player)
×
1148
        return true;
×
1149

1150
    if(msg.requestInfo)
×
1151
    {
1152
        player->sendMsgAsync(new GameMessage_Map_Info(mapinfo.filepath.filename().string(), mapinfo.type,
×
1153
                                                      mapinfo.mapData.uncompressedLength, mapinfo.mapData.data.size(),
×
1154
                                                      mapinfo.luaData.uncompressedLength, mapinfo.luaData.data.size()));
×
1155
    } else if(player->isMapSending())
×
1156
    {
1157
        // Don't send again
1158
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1159
    } else
1160
    {
1161
        // Send map data
1162
        unsigned curPos = 0;
×
1163
        unsigned remainingSize = mapinfo.mapData.data.size();
×
1164
        while(remainingSize)
×
1165
        {
1166
            unsigned chunkSize = std::min(MAP_PART_SIZE, remainingSize);
×
1167

1168
            player->sendMsgAsync(new GameMessage_Map_Data(true, curPos, &mapinfo.mapData.data[curPos], chunkSize));
×
1169
            curPos += chunkSize;
×
1170
            remainingSize -= chunkSize;
×
1171
        }
1172

1173
        // And lua data (if there is any)
1174
        RTTR_Assert(mapinfo.luaFilepath.empty() == mapinfo.luaData.data.empty());
×
1175
        RTTR_Assert(mapinfo.luaData.data.empty() == (mapinfo.luaData.uncompressedLength == 0));
×
1176
        curPos = 0;
×
1177
        remainingSize = mapinfo.luaData.data.size();
×
1178
        while(remainingSize)
×
1179
        {
1180
            unsigned chunkSize = std::min(MAP_PART_SIZE, remainingSize);
×
1181

1182
            player->sendMsgAsync(new GameMessage_Map_Data(false, curPos, &mapinfo.luaData.data[curPos], chunkSize));
×
1183
            curPos += chunkSize;
×
1184
            remainingSize -= chunkSize;
×
1185
        }
1186
        // estimate time. max 60 chunks/s (currently limited by framerate), assume 50 (~25kb/s)
1187
        auto numChunks = (mapinfo.mapData.data.size() + mapinfo.luaData.data.size()) / MAP_PART_SIZE;
×
1188
        player->setMapSending(std::chrono::seconds(numChunks / 50 + 1));
×
1189
    }
1190
    return true;
×
1191
}
1192

1193
bool GameServer::OnGameMessage(const GameMessage_Map_Checksum& msg)
×
1194
{
1195
    if(state != ServerState::Config)
×
1196
    {
1197
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1198
        return true;
×
1199
    }
1200
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
1201
    if(!player)
×
1202
        return true;
×
1203

1204
    bool checksumok = (msg.mapChecksum == mapinfo.mapChecksum && msg.luaChecksum == mapinfo.luaChecksum);
×
1205

1206
    LOG.writeToFile("CLIENT%d >>> SERVER: NMS_MAP_CHECKSUM(%u) expected: %u, ok: %s\n") % unsigned(msg.senderPlayerID)
×
1207
      % msg.mapChecksum % mapinfo.mapChecksum % (checksumok ? "yes" : "no");
×
1208

1209
    // Send response. If map data was not sent yet, the client may retry
1210
    player->sendMsgAsync(new GameMessage_Map_ChecksumOK(checksumok, !player->isMapSending()));
×
1211

1212
    LOG.writeToFile("SERVER >>> CLIENT%d: NMS_MAP_CHECKSUM(%d)\n") % unsigned(msg.senderPlayerID) % checksumok;
×
1213

1214
    if(!checksumok)
×
1215
    {
1216
        if(player->isMapSending())
×
1217
            KickPlayer(msg.senderPlayerID, KickReason::WrongChecksum, __LINE__);
×
1218
    } else
1219
    {
1220
        JoinPlayerInfo& playerInfo = playerInfos[msg.senderPlayerID];
×
1221
        // Used? Then we got this twice or some error happened. Remove him
1222
        if(playerInfo.isUsed())
×
1223
            KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1224
        else
1225
        {
1226
            // Inform others about a new player
1227
            SendToAll(GameMessage_Player_New(msg.senderPlayerID, playerInfo.name));
×
1228

1229
            LOG.writeToFile("SERVER >>> BROADCAST: NMS_PLAYER_NEW(%d, %s)\n") % unsigned(msg.senderPlayerID)
×
1230
              % playerInfo.name;
×
1231

1232
            // Mark as used and assign a unique color
1233
            // Do this before sending the player list to avoid sending useless updates
1234
            playerInfo.ps = PlayerState::Occupied;
×
1235
            CheckAndSetColor(msg.senderPlayerID, playerInfo.color, true);
×
1236

1237
            // Send remaining data and mark as active
1238
            player->sendMsgAsync(new GameMessage_Server_Name(config.gamename));
×
1239
            player->sendMsgAsync(new GameMessage_Player_List(playerInfos));
×
1240
            player->sendMsgAsync(new GameMessage_GGSChange(ggs_));
×
1241
            player->setActive();
×
1242
        }
1243
        AnnounceStatusChange();
×
1244
    }
1245
    return true;
×
1246
}
1247

1248
// speed change message
1249
bool GameServer::OnGameMessage(const GameMessage_Speed& msg)
×
1250
{
1251
    if(state != ServerState::Game)
×
1252
    {
1253
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1254
        return true;
×
1255
    }
1256
    framesinfo.gfLengthReq = FramesInfo::milliseconds32_t(msg.gf_length);
×
1257
    return true;
×
1258
}
1259

1260
bool GameServer::OnGameMessage(const GameMessage_GameCommand& msg)
×
1261
{
1262
    int targetPlayerId = GetTargetPlayer(msg);
×
1263
    if((state != ServerState::Game && state != ServerState::Loading) || targetPlayerId < 0
×
1264
       || (state == ServerState::Loading && !msg.cmds.gcs.empty()))
×
1265
    {
1266
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1267
        return true;
×
1268
    }
1269

1270
    if(!nwfInfo.addPlayerCmds(targetPlayerId, msg.cmds))
×
1271
        return true; // Ignore
×
1272
    GameServerPlayer* player = GetNetworkPlayer(targetPlayerId);
×
1273
    if(player)
×
1274
        player->setNotLagging();
×
1275
    SendToAll(GameMessage_GameCommand(targetPlayerId, msg.cmds.checksum, msg.cmds.gcs));
×
1276

1277
    return true;
×
1278
}
1279

1280
bool GameServer::OnGameMessage(const GameMessage_AsyncLog& msg)
×
1281
{
1282
    if(state != ServerState::Game)
×
1283
    {
1284
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1285
        return true;
×
1286
    }
1287
    bool foundPlayer = false;
×
1288
    for(AsyncLog& log : asyncLogs)
×
1289
    {
1290
        if(log.playerId != msg.senderPlayerID)
×
1291
            continue;
×
1292
        RTTR_Assert(!log.done);
×
1293
        if(log.done)
×
1294
            return true;
×
1295
        foundPlayer = true;
×
1296
        log.addData += msg.addData;
×
1297
        log.randEntries.insert(log.randEntries.end(), msg.entries.begin(), msg.entries.end());
×
1298
        if(msg.last)
×
1299
        {
1300
            LOG.write(_("Received async logs from %1% (%2% entries).\n")) % unsigned(log.playerId)
×
1301
              % log.randEntries.size();
×
1302
            log.done = true;
×
1303
        }
1304
    }
1305
    if(!foundPlayer)
×
1306
    {
1307
        LOG.write(_("Received async log from %1%, but did not expect it!\n")) % unsigned(msg.senderPlayerID);
×
1308
        return true;
×
1309
    }
1310

1311
    // Check if we have all logs
1312
    for(const AsyncLog& log : asyncLogs)
×
1313
    {
1314
        if(!log.done)
×
1315
            return true;
×
1316
    }
1317

1318
    LOG.write(_("Async logs received completely.\n"));
×
1319

1320
    const bfs::path asyncFilePath = SaveAsyncLog();
×
1321
    if(!asyncFilePath.empty())
×
1322
        SendAsyncLog(asyncFilePath);
×
1323

1324
    // Kick all players that have a different checksum from the host
1325
    AsyncChecksum hostChecksum;
×
1326
    for(const AsyncLog& log : asyncLogs)
×
1327
    {
1328
        if(playerInfos.at(log.playerId).isHost)
×
1329
        {
1330
            hostChecksum = log.checksum;
×
1331
            break;
×
1332
        }
1333
    }
1334
    for(const AsyncLog& log : asyncLogs)
×
1335
    {
1336
        if(log.checksum != hostChecksum)
×
1337
            KickPlayer(log.playerId, KickReason::Async, __LINE__);
×
1338
    }
1339
    return true;
×
1340
}
1341

1342
bool GameServer::OnGameMessage(const GameMessage_RemoveLua& msg)
×
1343
{
1344
    if(state != ServerState::Config || !IsHost(msg.senderPlayerID))
×
1345
    {
1346
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1347
        return true;
×
1348
    }
1349
    mapinfo.luaFilepath.clear();
×
1350
    mapinfo.luaData.Clear();
×
1351
    mapinfo.luaChecksum = 0;
×
1352
    SendToAll(msg);
×
1353
    CancelCountdown();
×
1354
    return true;
×
1355
}
1356

1357
bool GameServer::OnGameMessage(const GameMessage_Countdown& msg)
×
1358
{
1359
    if(state != ServerState::Config || !IsHost(msg.senderPlayerID))
×
1360
    {
1361
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1362
        return true;
×
1363
    }
1364

1365
    NetworkPlayer* nwPlayer = GetNetworkPlayer(msg.senderPlayerID);
×
1366
    if(!nwPlayer || countdown.IsActive())
×
1367
        return true;
×
1368

1369
    if(!ArePlayersReady())
×
1370
        nwPlayer->sendMsgAsync(new GameMessage_CancelCountdown(true));
×
1371
    else
1372
    {
1373
        // Just to make sure update all player infos
1374
        SendToAll(GameMessage_Player_List(playerInfos));
×
1375
        // Start countdown (except its single player)
1376
        if(networkPlayers.size() > 1)
×
1377
        {
1378
            countdown.Start(msg.countdown);
×
1379
            SendToAll(GameMessage_Countdown(countdown.GetRemainingSecs()));
×
1380
            LOG.writeToFile("SERVER >>> Countdown started(%d)\n") % countdown.GetRemainingSecs();
×
1381
        } else if(!StartGame())
×
1382
            Stop();
×
1383
    }
1384

1385
    return true;
×
1386
}
1387

1388
bool GameServer::OnGameMessage(const GameMessage_CancelCountdown& msg)
×
1389
{
1390
    if(state != ServerState::Config || !IsHost(msg.senderPlayerID))
×
1391
    {
1392
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1393
        return true;
×
1394
    }
1395

1396
    CancelCountdown();
×
1397
    return true;
×
1398
}
1399

1400
bool GameServer::OnGameMessage(const GameMessage_Pause& msg)
×
1401
{
1402
    if(IsHost(msg.senderPlayerID))
×
1403
        SetPaused(msg.paused);
×
1404
    return true;
×
1405
}
1406

1407
bool GameServer::OnGameMessage(const GameMessage_SkipToGF& msg)
×
1408
{
1409
    if(IsHost(msg.senderPlayerID))
×
1410
    {
1411
        skiptogf = msg.targetGF;
×
1412
        SendToAll(msg);
×
1413
    }
1414
    return true;
×
1415
}
1416

1417
bool GameServer::OnGameMessage(const GameMessage_GGSChange& msg)
×
1418
{
1419
    if(state != ServerState::Config || !IsHost(msg.senderPlayerID))
×
1420
    {
1421
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1422
        return true;
×
1423
    }
1424
    ggs_ = msg.ggs;
×
1425
    SendToAll(msg);
×
1426
    CancelCountdown();
×
1427
    return true;
×
1428
}
1429

1430
void GameServer::CancelCountdown()
×
1431
{
1432
    if(!countdown.IsActive())
×
1433
        return;
×
1434
    // Countdown-Stop allen mitteilen
1435
    countdown.Stop();
×
1436
    SendToAll(GameMessage_CancelCountdown());
×
1437
    LOG.writeToFile("SERVER >>> BROADCAST: NMS_CANCELCOUNTDOWN\n");
×
1438
}
1439

1440
bool GameServer::ArePlayersReady() const
×
1441
{
1442
    // All slots filled and human players are ready
1443
    for(const JoinPlayerInfo& player : playerInfos)
×
1444
    {
1445
        if(player.ps == PlayerState::Free || (player.isHuman() && !player.isReady))
×
1446
            return false;
×
1447
    }
1448

1449
    // Ensure unique colors except for scripted maps where duplicate colors can be intentional
1450
    if(!mapinfo.luaFilepath.empty())
×
1451
    {
1452
        std::set<unsigned> takenColors;
×
1453
        for(const JoinPlayerInfo& player : playerInfos)
×
1454
        {
1455
            if(player.isUsed())
×
1456
            {
1457
                if(helpers::contains(takenColors, player.color))
×
1458
                    return false;
×
1459
                takenColors.insert(player.color);
×
1460
            }
1461
        }
1462
    }
1463
    return true;
×
1464
}
1465

1466
void GameServer::PlayerDataChanged(unsigned playerIdx)
×
1467
{
1468
    CancelCountdown();
×
1469
    JoinPlayerInfo& player = GetJoinPlayer(playerIdx);
×
1470
    if(player.ps != PlayerState::AI && player.isReady)
×
1471
    {
1472
        player.isReady = false;
×
1473
        SendToAll(GameMessage_Player_Ready(playerIdx, false));
×
1474
    }
1475
}
×
1476

1477
bfs::path GameServer::SaveAsyncLog()
×
1478
{
1479
    // Get the highest common counter number and start from there (remove all others)
1480
    unsigned maxCtr = 0;
×
1481
    for(const AsyncLog& log : asyncLogs)
×
1482
    {
1483
        if(!log.randEntries.empty() && log.randEntries[0].counter > maxCtr)
×
1484
            maxCtr = log.randEntries[0].counter;
×
1485
    }
1486
    // Number of entries = max(asyncLogs[0..n].randEntries.size())
1487
    unsigned numEntries = 0;
×
1488
    for(AsyncLog& log : asyncLogs)
×
1489
    {
1490
        auto it = helpers::find_if(log.randEntries, [maxCtr](const auto& e) { return e.counter == maxCtr; });
×
1491
        log.randEntries.erase(log.randEntries.begin(), it);
×
1492
        if(numEntries < log.randEntries.size())
×
1493
            numEntries = log.randEntries.size();
×
1494
    }
1495
    // No entries :(
1496
    if(numEntries == 0 || asyncLogs.size() < 2u)
×
1497
        return "";
×
1498

1499
    // count identical lines
1500
    unsigned numIdentical = 0;
×
1501
    for(unsigned i = 0; i < numEntries; i++)
×
1502
    {
1503
        bool isIdentical = true;
×
1504
        if(i >= asyncLogs[0].randEntries.size())
×
1505
            break;
×
1506
        const RandomEntry& refEntry = asyncLogs[0].randEntries[i];
×
1507
        for(const AsyncLog& log : asyncLogs)
×
1508
        {
1509
            if(i >= log.randEntries.size())
×
1510
            {
1511
                isIdentical = false;
×
1512
                break;
×
1513
            }
1514
            const RandomEntry& curEntry = log.randEntries[i];
×
1515
            if(curEntry.maxExcl != refEntry.maxExcl || curEntry.rngState != refEntry.rngState
×
1516
               || curEntry.objId != refEntry.objId)
×
1517
            {
1518
                isIdentical = false;
×
1519
                break;
×
1520
            }
1521
        }
1522
        if(isIdentical)
×
1523
            ++numIdentical;
×
1524
        else
1525
            break;
×
1526
    }
1527

1528
    LOG.write(_("There are %1% identical async log entries.\n")) % numIdentical;
×
1529

1530
    bfs::path filePath =
1531
      RTTRCONFIG.ExpandPath(s25::folders::logs) / (s25util::Time::FormatTime("async_%Y-%m-%d_%H-%i-%s") + "Server.log");
×
1532

1533
    // open async log
1534
    bnw::ofstream file(filePath);
×
1535

1536
    if(file)
×
1537
    {
1538
        file << "Map: " << mapinfo.title << std::endl;
×
1539
        file << std::setfill(' ');
×
1540
        for(const AsyncLog& log : asyncLogs)
×
1541
        {
1542
            const JoinPlayerInfo& plInfo = playerInfos.at(log.playerId);
×
1543
            file << "Player " << std::setw(2) << unsigned(log.playerId) << (plInfo.isHost ? '#' : ' ') << "\t\""
×
1544
                 << plInfo.name << '"' << std::endl;
×
1545
            file << "System info: " << log.addData << std::endl;
×
1546
            file << "\tChecksum: " << std::setw(0) << log.checksum << std::endl;
×
1547
        }
1548
        for(const AsyncLog& log : asyncLogs)
×
1549
            file << "Checksum " << std::setw(2) << unsigned(log.playerId) << std::setw(0) << ": " << log.checksum
×
1550
                 << std::endl;
×
1551

1552
        // print identical lines, they help in tracing the bug
1553
        for(unsigned i = 0; i < numIdentical; i++)
×
1554
            file << "[ I ]: " << asyncLogs[0].randEntries[i] << "\n";
×
1555
        for(unsigned i = numIdentical; i < numEntries; i++)
×
1556
        {
1557
            for(const AsyncLog& log : asyncLogs)
×
1558
            {
1559
                if(i < log.randEntries.size())
×
1560
                    file << "[C" << std::setw(2) << unsigned(log.playerId) << std::setw(0)
×
1561
                         << "]: " << log.randEntries[i] << '\n';
×
1562
            }
1563
        }
1564

1565
        LOG.write(_("Async log saved at %1%\n")) % filePath;
×
1566
        return filePath;
×
1567
    } else
1568
    {
1569
        LOG.write(_("Failed to save async log at %1%\n")) % filePath;
×
1570
        return "";
×
1571
    }
1572
}
1573

1574
void GameServer::SendAsyncLog(const bfs::path& asyncLogFilePath)
×
1575
{
1576
    if(SETTINGS.global.submitDebugData == SubmitDebugData::Yes
×
1577
#ifdef _WIN32
1578
       || (MessageBoxW(nullptr,
1579
                       boost::nowide::widen(_("The game clients are out of sync. Would you like to send debug "
1580
                                              "information to RttR to help us avoiding this in "
1581
                                              "the future? Thank you very much!"))
1582
                         .c_str(),
1583
                       boost::nowide::widen(_("Error")).c_str(),
1584
                       MB_YESNO | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND)
1585
           == IDYES)
1586
#endif
1587
    )
1588
    {
1589
        DebugInfo di;
×
1590
        LOG.write(_("Sending async logs %1%.\n")) % (di.SendAsyncLog(asyncLogFilePath) ? "succeeded" : "failed");
×
1591

1592
        di.SendReplay();
×
1593
    }
1594
}
×
1595

1596
void GameServer::CheckAndSetColor(unsigned playerIdx, unsigned newColor, bool ensureUnique)
×
1597
{
1598
    RTTR_Assert(playerIdx < playerInfos.size());
×
1599
    // If either of those fails we may not find a valid color!
1600
    static_assert(PLAYER_COLORS.size() >= MAX_PLAYERS, "Not enough player colors defined!");
1601
    RTTR_Assert(playerInfos.size() <= PLAYER_COLORS.size());
×
1602

1603
    JoinPlayerInfo& player = playerInfos[playerIdx];
×
1604
    RTTR_Assert(player.isUsed()); // Should only set colors for taken spots
×
1605

1606
    if(ensureUnique)
×
1607
    {
1608
        // Get colors used by other players
1609
        std::set<unsigned> takenColors;
×
1610
        for(unsigned p = 0; p < playerInfos.size(); ++p)
×
1611
        {
1612
            // Skip self
1613
            if(p == playerIdx)
×
1614
                continue;
×
1615

1616
            JoinPlayerInfo& otherPlayer = playerInfos[p];
×
1617
            if(otherPlayer.isUsed())
×
1618
                takenColors.insert(otherPlayer.color);
×
1619
        }
1620
        // Look for a unique color
1621
        int newColorIdx = JoinPlayerInfo::GetColorIdx(newColor);
×
1622
        while(helpers::contains(takenColors, newColor))
×
1623
            newColor = PLAYER_COLORS[(++newColorIdx) % PLAYER_COLORS.size()];
×
1624
    }
1625

1626
    if(player.color == newColor)
×
1627
        return;
×
1628

1629
    player.color = newColor;
×
1630

1631
    SendToAll(GameMessage_Player_Color(playerIdx, player.color));
×
1632
    LOG.writeToFile("SERVER >>> BROADCAST: NMS_PLAYER_TOGGLECOLOR(%d, %d)\n") % playerIdx % player.color;
×
1633
}
1634

1635
bool GameServer::OnGameMessage(const GameMessage_Player_Swap& msg)
×
1636
{
1637
    if(state != ServerState::Game && state != ServerState::Config)
×
1638
        return true;
×
1639
    int targetPlayer = GetTargetPlayer(msg);
×
1640
    if(targetPlayer < 0)
×
1641
        return true;
×
1642
    auto player1 = static_cast<uint8_t>(targetPlayer);
×
1643
    if(player1 == msg.player2 || msg.player2 >= playerInfos.size())
×
1644
        return true;
×
1645

1646
    SwapPlayer(player1, msg.player2);
×
1647
    return true;
×
1648
}
1649

1650
bool GameServer::OnGameMessage(const GameMessage_Player_SwapConfirm& msg)
×
1651
{
1652
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
1653
    if(!player)
×
1654
        return true;
×
1655
    for(auto it = player->getPendingSwaps().begin(); it != player->getPendingSwaps().end(); ++it)
×
1656
    {
1657
        if(it->first == msg.player && it->second == msg.player2)
×
1658
        {
1659
            player->getPendingSwaps().erase(it);
×
1660
            break;
×
1661
        }
1662
    }
1663
    return true;
×
1664
}
1665

1666
void GameServer::SwapPlayer(const uint8_t player1, const uint8_t player2)
×
1667
{
1668
    // TODO: Swapping the player messes up the ids because our IDs are indizes. Usually this works because we use the
1669
    // sender player ID for messages received by the server and set the right ID for messages sent by the server.
1670
    // However (currently only) the host may send messages for another player. Those will not get the adjusted ID till
1671
    // he gets the swap message. So there is a short time, where the messages may be executed for the wrong player.
1672
    // Idea: Use actual IDs for players in messages (unique)
1673
    if(state == ServerState::Config)
×
1674
    {
1675
        // Swap player during match-making
1676
        // Swap everything
1677
        using std::swap;
1678
        swap(playerInfos[player1], playerInfos[player2]);
×
1679
        // In savegames some things cannot be changed
1680
        if(mapinfo.type == MapType::Savegame)
×
1681
            playerInfos[player1].FixSwappedSaveSlot(playerInfos[player2]);
×
1682
    } else if(state == ServerState::Game)
×
1683
    {
1684
        // Ingame we can only switch to a KI
1685
        if(playerInfos[player1].ps != PlayerState::Occupied || playerInfos[player2].ps != PlayerState::AI)
×
1686
            return;
×
1687

1688
        LOG.write("GameServer::ChangePlayer %i - %i \n") % unsigned(player1) % unsigned(player2);
×
1689
        using std::swap;
1690
        swap(playerInfos[player2].ps, playerInfos[player1].ps);
×
1691
        swap(playerInfos[player2].aiInfo, playerInfos[player1].aiInfo);
×
1692
        swap(playerInfos[player2].isHost, playerInfos[player1].isHost);
×
1693
    }
1694
    // Change ids of network players (if any). Get both first!
1695
    GameServerPlayer* newPlayer = GetNetworkPlayer(player2);
×
1696
    GameServerPlayer* oldPlayer = GetNetworkPlayer(player1);
×
1697
    if(newPlayer)
×
1698
        newPlayer->playerId = player1;
×
1699
    if(oldPlayer)
×
1700
        oldPlayer->playerId = player2;
×
1701
    SendToAll(GameMessage_Player_Swap(player1, player2));
×
1702
    const auto pSwap = std::make_pair(player1, player2);
×
1703
    for(GameServerPlayer& player : networkPlayers)
×
1704
    {
1705
        if(!player.isActive())
×
1706
            continue;
×
1707
        player.getPendingSwaps().push_back(pSwap);
×
1708
    }
1709
}
1710

1711
GameServerPlayer* GameServer::GetNetworkPlayer(unsigned playerId)
×
1712
{
1713
    for(GameServerPlayer& player : networkPlayers)
×
1714
    {
1715
        if(player.playerId == playerId)
×
1716
            return &player;
×
1717
    }
1718
    return nullptr;
×
1719
}
1720

1721
void GameServer::SetPaused(bool paused)
×
1722
{
1723
    if(framesinfo.isPaused == paused)
×
1724
        return;
×
1725
    framesinfo.isPaused = paused;
×
1726
    SendToAll(GameMessage_Pause(framesinfo.isPaused));
×
1727
    for(GameServerPlayer& player : networkPlayers)
×
1728
        player.setNotLagging();
×
1729
}
1730

1731
JoinPlayerInfo& GameServer::GetJoinPlayer(unsigned playerIdx)
×
1732
{
1733
    return playerInfos.at(playerIdx);
×
1734
}
1735

1736
bool GameServer::IsHost(unsigned playerIdx) const
×
1737
{
1738
    return playerIdx < playerInfos.size() && playerInfos[playerIdx].isHost;
×
1739
}
1740

1741
int GameServer::GetTargetPlayer(const GameMessageWithPlayer& msg)
×
1742
{
1743
    if(msg.player != 0xFF)
×
1744
    {
1745
        if(msg.player < playerInfos.size() && (msg.player == msg.senderPlayerID || IsHost(msg.senderPlayerID)))
×
1746
        {
1747
            GameServerPlayer* networkPlayer = GetNetworkPlayer(msg.senderPlayerID);
×
1748
            if(networkPlayer->isActive())
×
1749
                return msg.player;
×
1750
            unsigned result = msg.player;
×
1751
            // Apply pending swaps
1752
            for(auto& pSwap : networkPlayer->getPendingSwaps()) //-V522
×
1753
            {
1754
                if(pSwap.first == result)
×
1755
                    result = pSwap.second;
×
1756
                else if(pSwap.second == result)
×
1757
                    result = pSwap.first;
×
1758
            }
1759
            return result;
×
1760
        }
1761
    } else if(msg.senderPlayerID < playerInfos.size())
×
1762
        return msg.senderPlayerID;
×
1763
    return -1;
×
1764
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc