Ticket #3953: disable_timeouts_differently_v2.patch

File disable_timeouts_differently_v2.patch, 16.9 KB (added by elexis, 8 years ago)

Renames the new variable to isLocalClient, replaces g_HostGUID and changes the other mentioned GetIPAddress function to not convert to string.

  • source/gui/scripting/ScriptFunctions.cpp

    void StartNetworkHost(ScriptInterface::C  
    357357        SAFE_DELETE(g_NetServer);
    358358        return;
    359359    }
    360360
    361361    g_Game = new CGame();
    362     g_NetClient = new CNetClient(g_Game);
     362    g_NetClient = new CNetClient(g_Game, true);
    363363    g_NetClient->SetUserName(playerName);
    364364
    365365    if (!g_NetClient->SetupConnection("127.0.0.1"))
    366366    {
    367367        pCxPrivate->pScriptInterface->ReportError("Failed to connect to server");
    void StartNetworkJoin(ScriptInterface::C  
    375375    ENSURE(!g_NetClient);
    376376    ENSURE(!g_NetServer);
    377377    ENSURE(!g_Game);
    378378
    379379    g_Game = new CGame();
    380     g_NetClient = new CNetClient(g_Game);
     380    g_NetClient = new CNetClient(g_Game, false);
    381381    g_NetClient->SetUserName(playerName);
    382382    if (!g_NetClient->SetupConnection(serverAddress))
    383383    {
    384384        pCxPrivate->pScriptInterface->ReportError("Failed to connect to server");
    385385        SAFE_DELETE(g_NetClient);
  • source/network/NetClient.cpp

    public:  
    6464
    6565private:
    6666    CNetClient& m_Client;
    6767};
    6868
    69 CNetClient::CNetClient(CGame* game) :
     69CNetClient::CNetClient(CGame* game, bool isLocalClient) :
    7070    m_Session(NULL),
    7171    m_UserName(L"anonymous"),
    7272    m_GUID(ps_generate_guid()), m_HostID((u32)-1), m_ClientTurnManager(NULL), m_Game(game),
    7373    m_GameAttributes(game->GetSimulation2()->GetScriptInterface().GetContext()),
     74    m_IsLocalClient(isLocalClient),
    7475    m_LastConnectionCheck(0)
    7576{
    7677    m_Game->SetTurnManager(NULL); // delete the old local turn manager so we don't accidentally use it
    7778
    7879    void* context = this;
    void CNetClient::SetUserName(const CStrW  
    152153}
    153154
    154155bool CNetClient::SetupConnection(const CStr& server)
    155156{
    156157    CNetClientSession* session = new CNetClientSession(*this);
    157     bool ok = session->Connect(PS_DEFAULT_PORT, server);
     158    bool ok = session->Connect(PS_DEFAULT_PORT, server, m_IsLocalClient);
    158159    SetAndOwnSession(session);
    159160    return ok;
    160161}
    161162
    162163void CNetClient::SetAndOwnSession(CNetClientSession* session)
    bool CNetClient::OnHandshakeResponse(voi  
    465466
    466467    CAuthenticateMessage authenticate;
    467468    authenticate.m_GUID = client->m_GUID;
    468469    authenticate.m_Name = client->m_UserName;
    469470    authenticate.m_Password = L""; // TODO
     471    authenticate.m_IsLocalClient = client->m_IsLocalClient;
    470472    client->SendMessage(&authenticate);
    471473
    472474    return true;
    473475}
    474476
  • source/network/NetClient.h

    class CNetClient : public CFsm  
    6363public:
    6464    /**
    6565     * Construct a client associated with the given game object.
    6666     * The game must exist for the lifetime of this object.
    6767     */
    68     CNetClient(CGame* game);
     68    CNetClient(CGame* game, bool isLocalClient);
    6969
    7070    virtual ~CNetClient();
    7171
    7272    /**
    7373     * We assume that adding a tracing function that's only called
    private:  
    237237    CNetClientTurnManager* m_ClientTurnManager;
    238238
    239239    /// Unique-per-game identifier of this client, used to identify the sender of simulation commands
    240240    u32 m_HostID;
    241241
     242    /// Whether to prevent the client of the host from timing out
     243    bool m_IsLocalClient;
     244
    242245    /// Latest copy of game setup attributes heard from the server
    243246    JS::PersistentRootedValue m_GameAttributes;
    244247
    245248    /// Latest copy of player assignments heard from the server
    246249    PlayerAssignmentMap m_PlayerAssignments;
  • source/network/NetMessages.h

     
    2626#include "ps/CStr.h"
    2727#include "scriptinterface/ScriptVal.h"
    2828
    2929#define PS_PROTOCOL_MAGIC               0x5073013f      // 'P', 's', 0x01, '?'
    3030#define PS_PROTOCOL_MAGIC_RESPONSE      0x50630121      // 'P', 'c', 0x01, '!'
    31 #define PS_PROTOCOL_VERSION             0x01010011      // Arbitrary protocol
     31#define PS_PROTOCOL_VERSION             0x01010012      // Arbitrary protocol
    3232#define PS_DEFAULT_PORT                 0x5073          // 'P', 's'
    3333
    3434// Defines the list of message types. The order of the list must not change.
    3535// The message types having a negative value are used internally and not sent
    3636// over the network. The message types used for network communication have
    END_NMT_CLASS()  
    110110
    111111START_NMT_CLASS_(Authenticate, NMT_AUTHENTICATE)
    112112    NMT_FIELD(CStr8, m_GUID)
    113113    NMT_FIELD(CStrW, m_Name)
    114114    NMT_FIELD(CStrW, m_Password)
     115    NMT_FIELD_INT(m_IsLocalClient, u8, 1)
    115116END_NMT_CLASS()
    116117
    117118START_NMT_CLASS_(AuthenticateResult, NMT_AUTHENTICATE_RESULT)
    118119    NMT_FIELD_INT(m_Code, u32, 4)
    119120    NMT_FIELD_INT(m_HostID, u32, 2)
  • source/network/NetServer.cpp

     
    2323#include "NetMessage.h"
    2424#include "NetSession.h"
    2525#include "NetStats.h"
    2626#include "NetTurnManager.h"
    2727
    28 #include "lib/external_libraries/enet.h"
    2928#include "ps/CLogger.h"
    3029#include "ps/ConfigDB.h"
    3130#include "scriptinterface/ScriptInterface.h"
    3231#include "scriptinterface/ScriptRuntime.h"
    3332#include "simulation2/Simulation2.h"
    private:  
    126125
    127126CNetServerWorker::CNetServerWorker(int autostartPlayers) :
    128127    m_AutostartPlayers(autostartPlayers),
    129128    m_Shutdown(false),
    130129    m_ScriptInterface(NULL),
    131     m_NextHostID(1), m_Host(NULL), m_HostGUID(), m_Stats(NULL),
     130    m_NextHostID(1), m_Host(NULL), m_Stats(NULL),
    132131    m_LastConnectionCheck(0)
    133132{
    134133    m_State = SERVER_STATE_UNCONNECTED;
    135134
    136135    m_ServerTurnManager = NULL;
    bool CNetServerWorker::RunStep()  
    484483
    485484        // Set up a session object for this peer
    486485
    487486        CNetServerSession* session = new CNetServerSession(*this, event.peer);
    488487
    489         // Prevent the local client of the host from timing out too quickly
    490 #if (ENET_VERSION >= ENET_VERSION_CREATE(1, 3, 4))
    491         if (session->GetIPAddress() == "127.0.0.1")
    492             enet_peer_timeout(event.peer, 0, MAXIMUM_HOST_TIMEOUT, MAXIMUM_HOST_TIMEOUT);
    493 #endif
    494 
    495488        m_Sessions.push_back(session);
    496489
    497490        SetupSession(session);
    498491
    499492        ENSURE(event.peer->data == NULL);
    bool CNetServerWorker::HandleConnect(CNe  
    688681
    689682void CNetServerWorker::OnUserJoin(CNetServerSession* session)
    690683{
    691684    AddPlayer(session->GetGUID(), session->GetUserName());
    692685
    693     if (m_HostGUID.empty() && session->GetIPAddress() == "127.0.0.1")
    694         m_HostGUID = session->GetGUID();
    695 
    696686    CGameSetupMessage gameSetupMessage(GetScriptInterface());
    697687    gameSetupMessage.m_Data = m_GameAttributes.get();
    698688    session->SendMessage(&gameSetupMessage);
    699689
    700690    CPlayerAssignmentMessage assignMessage;
    bool CNetServerWorker::KickPlayer(const  
    790780    // Find the user with that name
    791781    std::vector<CNetServerSession*>::iterator it = std::find_if(m_Sessions.begin(), m_Sessions.end(),
    792782        [&](CNetServerSession* session) { return session->GetUserName() == playerName; });
    793783
    794784    // and return if no one or the host has that name
    795     if (it == m_Sessions.end() || (*it)->GetGUID() == m_HostGUID)
     785    if (it == m_Sessions.end() || (*it)->IsLocalClient())
    796786        return false;
    797787
    798788    if (ban)
    799789    {
    800790        // Remember name
    801791        if (std::find(m_BannedPlayers.begin(), m_BannedPlayers.end(), playerName) == m_BannedPlayers.end())
    802792            m_BannedPlayers.push_back(playerName);
    803793
    804794        // Remember IP address
    805         CStr ipAddress = (*it)->GetIPAddress();
    806         if (!ipAddress.empty() && std::find(m_BannedIPs.begin(), m_BannedIPs.end(), ipAddress) == m_BannedIPs.end())
     795        enet_uint32 ipAddress = (*it)->GetIPAddress();
     796        if (std::find(m_BannedIPs.begin(), m_BannedIPs.end(), ipAddress) == m_BannedIPs.end())
    807797            m_BannedIPs.push_back(ipAddress);
    808798    }
    809799
    810800    // Disconnect that user
    811801    (*it)->Disconnect(ban ? NDR_BANNED : NDR_KICKED);
    bool CNetServerWorker::OnAuthenticate(vo  
    997987    u32 newHostID = server.m_NextHostID++;
    998988
    999989    session->SetUserName(username);
    1000990    session->SetGUID(message->m_GUID);
    1001991    session->SetHostID(newHostID);
     992    session->SetLocalClient(message->m_IsLocalClient);
    1002993
    1003994    CAuthenticateResultMessage authenticateResult;
    1004995    authenticateResult.m_Code = isRejoining ? ARC_OK_REJOINING : ARC_OK;
    1005996    authenticateResult.m_HostID = newHostID;
    1006997    authenticateResult.m_Message = L"Logged in";
  • source/network/NetServer.h

     
    2020
    2121#include "NetFileTransfer.h"
    2222#include "NetHost.h"
    2323
    2424#include "lib/config2.h"
     25#include "lib/external_libraries/enet.h"
    2526#include "ps/ThreadUtil.h"
    2627#include "scriptinterface/ScriptVal.h"
    2728
    2829#include <vector>
    2930
    private:  
    311312    NetServerState m_State;
    312313
    313314    CStrW m_ServerName;
    314315    CStrW m_WelcomeMessage;
    315316
    316     std::vector<CStr> m_BannedIPs;
     317    std::vector<enet_uint32> m_BannedIPs;
    317318    std::vector<CStrW> m_BannedPlayers;
    318319
    319320    u32 m_NextHostID;
    320321
    321322    CNetServerTurnManager* m_ServerTurnManager;
    322323
    323     CStr m_HostGUID;
    324 
    325324    /**
    326325     * A copy of all simulation commands received so far, indexed by
    327326     * turn number, to simplify support for rejoining etc.
    328327     * TODO: verify this doesn't use too much RAM.
    329328     */
  • source/network/NetSession.cpp

     
    1919#include "NetSession.h"
    2020#include "NetClient.h"
    2121#include "NetServer.h"
    2222#include "NetMessage.h"
    2323#include "NetStats.h"
    24 #include "lib/external_libraries/enet.h"
    2524#include "ps/CLogger.h"
    2625#include "scriptinterface/ScriptInterface.h"
    2726
    2827const u32 NETWORK_WARNING_TIMEOUT = 4000;
    2928
    CNetClientSession::~CNetClientSession()  
    4948        m_Host = NULL;
    5049        m_Server = NULL;
    5150    }
    5251}
    5352
    54 bool CNetClientSession::Connect(u16 port, const CStr& server)
     53bool CNetClientSession::Connect(u16 port, const CStr& server, bool isLocalClient)
    5554{
    5655    ENSURE(!m_Host);
    5756    ENSURE(!m_Server);
    5857
    5958    // Create ENet host
    bool CNetClientSession::Connect(u16 port  
    7574    m_Host = host;
    7675    m_Server = peer;
    7776
    7877    // Prevent the local client of the host from timing out too quickly.
    7978#if (ENET_VERSION >= ENET_VERSION_CREATE(1, 3, 4))
    80     if (GetIPAddress() == "127.0.0.1")
     79    if (isLocalClient)
    8180        enet_peer_timeout(peer, 1, MAXIMUM_HOST_TIMEOUT, MAXIMUM_HOST_TIMEOUT);
    8281#endif
    8382
    8483    m_Stats = new CNetStatsTable(m_Server);
    8584    if (CProfileViewer::IsInitialised())
    bool CNetClientSession::SendMessage(cons  
    176175    ENSURE(m_Host && m_Server);
    177176
    178177    return CNetHost::SendMessage(message, m_Server, "server");
    179178}
    180179
    181 CStr CNetClientSession::GetIPAddress() const
    182 {
    183     char ipAddress[256] = "";
    184     if (enet_address_get_host_ip(&m_Server->address, ipAddress, ARRAY_SIZE(ipAddress)) < 0)
    185         LOGMESSAGE("Could not get IP address of the server!");
    186 
    187     return ipAddress;
    188 }
    189 
    190180u32 CNetClientSession::GetLastReceivedTime() const
    191181{
    192182    if (!m_Server)
    193183        return 0;
    194184
    u32 CNetClientSession::GetMeanRTT() cons  
    208198CNetServerSession::CNetServerSession(CNetServerWorker& server, ENetPeer* peer) :
    209199    m_Server(server), m_FileTransferer(this), m_Peer(peer)
    210200{
    211201}
    212202
    213 CStr CNetServerSession::GetIPAddress() const
     203enet_uint32 CNetServerSession::GetIPAddress() const
    214204{
    215     char ipAddress[256] = "";
    216     if (enet_address_get_host_ip(&m_Peer->address, ipAddress, ARRAY_SIZE(ipAddress)) < 0)
    217         LOGMESSAGE("Could not get IP address of a client!");
    218 
    219     return ipAddress;
     205    return m_Peer->address.host;
    220206}
    221207
    222208u32 CNetServerSession::GetLastReceivedTime() const
    223209{
    224210    if (!m_Peer)
    void CNetServerSession::DisconnectNow(u3  
    249235
    250236bool CNetServerSession::SendMessage(const CNetMessage* message)
    251237{
    252238    return m_Server.SendMessage(m_Peer, message);
    253239}
     240
     241bool CNetServerSession::IsLocalClient()
     242{
     243    return m_IsLocalClient;
     244}
     245
     246void CNetServerSession::SetLocalClient(bool isLocalClient)
     247{
     248    m_IsLocalClient = isLocalClient;
     249
     250    if (!isLocalClient)
     251        return;
     252
     253    // Prevent the local client of the host from timing out too quickly
     254#if (ENET_VERSION >= ENET_VERSION_CREATE(1, 3, 4))
     255    enet_peer_timeout(m_Peer, 0, MAXIMUM_HOST_TIMEOUT, MAXIMUM_HOST_TIMEOUT);
     256#endif
     257}
  • source/network/NetSession.h

     
    1919#define NETSESSION_H
    2020
    2121#include "network/fsm.h"
    2222#include "network/NetFileTransfer.h"
    2323#include "network/NetHost.h"
     24#include "lib/external_libraries/enet.h"
    2425#include "ps/CStr.h"
    2526#include "scriptinterface/ScriptVal.h"
    2627
    2728/**
    2829 * Report the peer if we didn't receive a packet after this time (milliseconds).
    class CNetClientSession : public INetSes  
    6869
    6970public:
    7071    CNetClientSession(CNetClient& client);
    7172    ~CNetClientSession();
    7273
    73     bool Connect(u16 port, const CStr& server);
     74    bool Connect(u16 port, const CStr& server, bool isLocalClient);
    7475
    7576    /**
    7677     * Process queued incoming messages.
    7778     */
    7879    void Poll();
    public:  
    9192    /**
    9293     * Send a message to the server.
    9394     */
    9495    virtual bool SendMessage(const CNetMessage* message);
    9596
    96     CStr GetIPAddress() const;
    97 
    9897    /**
    9998     * Number of milliseconds since the most recent packet of the server was received.
    10099     */
    101100    u32 GetLastReceivedTime() const;
    102101
    public:  
    142141    void SetUserName(const CStrW& name) { m_UserName = name; }
    143142
    144143    u32 GetHostID() const { return m_HostID; }
    145144    void SetHostID(u32 id) { m_HostID = id; }
    146145
    147     CStr GetIPAddress() const;
     146    enet_uint32 GetIPAddress() const;
    148147
    149148    /**
    150149     * Number of milliseconds since the latest packet of that client was received.
    151150     */
    152151    u32 GetLastReceivedTime() const;
    public:  
    170169     * The server will not receive any further messages sent via this session.
    171170     */
    172171    void DisconnectNow(u32 reason);
    173172
    174173    /**
     174     * Whether this client is running in the same process as the server.
     175     */
     176    bool IsLocalClient();
     177
     178    /**
     179     * Prevent timeouts for the client running in the same process as the server.
     180     */
     181    void SetLocalClient(bool isLocalClient);
     182
     183    /**
    175184     * Send a message to the client.
    176185     */
    177186    virtual bool SendMessage(const CNetMessage* message);
    178187
    179188    CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
    private:  
    186195    ENetPeer* m_Peer;
    187196
    188197    CStr m_GUID;
    189198    CStrW m_UserName;
    190199    u32 m_HostID;
     200
     201    bool m_IsLocalClient;
    191202};
    192203
    193204#endif  // NETSESSION_H
  • source/network/tests/test_Net.h

    public:  
    150150
    151151        JS::RootedValue attrs(cx);
    152152        scriptInterface.Eval("({mapType:'scenario',map:'maps/scenarios/Saharan Oases',mapPath:'maps/scenarios/',thing:'example'})", &attrs);
    153153        server.UpdateGameAttributes(&attrs, scriptInterface);
    154154
    155         CNetClient client1(&client1Game);
    156         CNetClient client2(&client2Game);
    157         CNetClient client3(&client3Game);
     155        CNetClient client1(&client1Game, false);
     156        CNetClient client2(&client2Game, false);
     157        CNetClient client3(&client3Game, false);
    158158
    159159        clients.push_back(&client1);
    160160        clients.push_back(&client2);
    161161        clients.push_back(&client3);
    162162
    public:  
    215215
    216216        JS::RootedValue attrs(cx);
    217217        scriptInterface.Eval("({mapType:'scenario',map:'maps/scenarios/Saharan Oases',mapPath:'maps/scenarios/',thing:'example'})", &attrs);
    218218        server.UpdateGameAttributes(&attrs, scriptInterface);
    219219
    220         CNetClient client1(&client1Game);
    221         CNetClient client2(&client2Game);
    222         CNetClient client3(&client3Game);
     220        CNetClient client1(&client1Game, false);
     221        CNetClient client2(&client2Game, false);
     222        CNetClient client3(&client3Game, false);
    223223
    224224        client1.SetUserName(L"alice");
    225225        client2.SetUserName(L"bob");
    226226        client3.SetUserName(L"charlie");
    227227
    public:  
    267267        clients.erase(clients.begin()+1);
    268268
    269269        debug_printf("==== Connecting client 2B\n");
    270270
    271271        CGame client2BGame(true);
    272         CNetClient client2B(&client2BGame);
     272        CNetClient client2B(&client2BGame, false);
    273273        client2B.SetUserName(L"bob");
    274274        clients.push_back(&client2B);
    275275
    276276        TS_ASSERT(client2B.SetupConnection("127.0.0.1"));
    277277
  • source/ps/GameSetup/GameSetup.cpp

    bool Autostart(const CmdLineArgs& args)  
    14451445        g_NetServer->UpdateGameAttributes(&attrs, scriptInterface);
    14461446
    14471447        bool ok = g_NetServer->SetupConnection();
    14481448        ENSURE(ok);
    14491449
    1450         g_NetClient = new CNetClient(g_Game);
     1450        g_NetClient = new CNetClient(g_Game, true);
    14511451        g_NetClient->SetUserName(userName);
    14521452        g_NetClient->SetupConnection("127.0.0.1");
    14531453    }
    14541454    else if (args.Has("autostart-client"))
    14551455    {
    14561456        InitPs(true, L"page_loading.xml", &scriptInterface, mpInitData);
    14571457
    1458         g_NetClient = new CNetClient(g_Game);
     1458        g_NetClient = new CNetClient(g_Game, false);
    14591459        g_NetClient->SetUserName(userName);
    14601460
    14611461        CStr ip = args.Get("autostart-client");
    14621462        if (ip.empty())
    14631463            ip = "127.0.0.1";