Ticket #3953: disable_timeouts_differently_v1.patch

File disable_timeouts_differently_v1.patch, 13.2 KB (added by elexis, 8 years ago)
  • 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 disableTimeout) :
    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_DisableTimeout(disableTimeout),
    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_DisableTimeout);
    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_DisableTimeout = client->m_DisableTimeout;
    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 disableTimeout);
    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_DisableTimeout;
     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_DisableTimeout, 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

    bool CNetServerWorker::RunStep()  
    484484
    485485        // Set up a session object for this peer
    486486
    487487        CNetServerSession* session = new CNetServerSession(*this, event.peer);
    488488
    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 
    495489        m_Sessions.push_back(session);
    496490
    497491        SetupSession(session);
    498492
    499493        ENSURE(event.peer->data == NULL);
    bool CNetServerWorker::HandleConnect(CNe  
    684678    handshake.m_ProtocolVersion = PS_PROTOCOL_VERSION;
    685679    handshake.m_SoftwareVersion = PS_PROTOCOL_VERSION;
    686680    return session->SendMessage(&handshake);
    687681}
    688682
    689 void CNetServerWorker::OnUserJoin(CNetServerSession* session)
     683void CNetServerWorker::OnUserJoin(CNetServerSession* session, bool disableTimeout)
    690684{
    691685    AddPlayer(session->GetGUID(), session->GetUserName());
    692686
    693     if (m_HostGUID.empty() && session->GetIPAddress() == "127.0.0.1")
     687    if (m_HostGUID.empty())
    694688        m_HostGUID = session->GetGUID();
    695689
     690    if (disableTimeout)
     691        session->DisableTimeout();
     692
    696693    CGameSetupMessage gameSetupMessage(GetScriptInterface());
    697694    gameSetupMessage.m_Data = m_GameAttributes.get();
    698695    session->SendMessage(&gameSetupMessage);
    699696
    700697    CPlayerAssignmentMessage assignMessage;
    bool CNetServerWorker::OnAuthenticate(vo  
    10041001    authenticateResult.m_Code = isRejoining ? ARC_OK_REJOINING : ARC_OK;
    10051002    authenticateResult.m_HostID = newHostID;
    10061003    authenticateResult.m_Message = L"Logged in";
    10071004    session->SendMessage(&authenticateResult);
    10081005
    1009     server.OnUserJoin(session);
     1006    server.OnUserJoin(session, message->m_DisableTimeout);
    10101007
    10111008    if (isRejoining)
    10121009    {
    10131010        // Request a copy of the current game state from an existing player,
    10141011        // so we can send it on to the new player
  • source/network/NetServer.h

    private:  
    260260    void ClearAllPlayerReady();
    261261
    262262    void SetupSession(CNetServerSession* session);
    263263    bool HandleConnect(CNetServerSession* session);
    264264
    265     void OnUserJoin(CNetServerSession* session);
     265    void OnUserJoin(CNetServerSession* session, bool disableTimeout);
    266266    void OnUserLeave(CNetServerSession* session);
    267267
    268268    static bool OnClientHandshake(void* context, CFsmEvent* event);
    269269    static bool OnAuthenticate(void* context, CFsmEvent* event);
    270270    static bool OnInGame(void* context, CFsmEvent* event);
  • source/network/NetSession.cpp

    CNetClientSession::~CNetClientSession()  
    4949        m_Host = NULL;
    5050        m_Server = NULL;
    5151    }
    5252}
    5353
    54 bool CNetClientSession::Connect(u16 port, const CStr& server)
     54bool CNetClientSession::Connect(u16 port, const CStr& server, bool disableTimeout)
    5555{
    5656    ENSURE(!m_Host);
    5757    ENSURE(!m_Server);
    5858
    5959    // Create ENet host
    bool CNetClientSession::Connect(u16 port  
    7575    m_Host = host;
    7676    m_Server = peer;
    7777
    7878    // Prevent the local client of the host from timing out too quickly.
    7979#if (ENET_VERSION >= ENET_VERSION_CREATE(1, 3, 4))
    80     if (GetIPAddress() == "127.0.0.1")
     80    if (disableTimeout)
    8181        enet_peer_timeout(peer, 1, MAXIMUM_HOST_TIMEOUT, MAXIMUM_HOST_TIMEOUT);
    8282#endif
    8383
    8484    m_Stats = new CNetStatsTable(m_Server);
    8585    if (CProfileViewer::IsInitialised())
    bool CNetClientSession::SendMessage(cons  
    176176    ENSURE(m_Host && m_Server);
    177177
    178178    return CNetHost::SendMessage(message, m_Server, "server");
    179179}
    180180
    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 
    190181u32 CNetClientSession::GetLastReceivedTime() const
    191182{
    192183    if (!m_Server)
    193184        return 0;
    194185
    void CNetServerSession::DisconnectNow(u3  
    249240
    250241bool CNetServerSession::SendMessage(const CNetMessage* message)
    251242{
    252243    return m_Server.SendMessage(m_Peer, message);
    253244}
     245
     246void CNetServerSession::DisableTimeout()
     247{
     248    // Prevent the local client of the host from timing out too quickly
     249#if (ENET_VERSION >= ENET_VERSION_CREATE(1, 3, 4))
     250    enet_peer_timeout(m_Peer, 0, MAXIMUM_HOST_TIMEOUT, MAXIMUM_HOST_TIMEOUT);
     251#endif
     252}
  • source/network/NetSession.h

    class CNetClientSession : public INetSes  
    6868
    6969public:
    7070    CNetClientSession(CNetClient& client);
    7171    ~CNetClientSession();
    7272
    73     bool Connect(u16 port, const CStr& server);
     73    bool Connect(u16 port, const CStr& server, bool disableTimeout);
    7474
    7575    /**
    7676     * Process queued incoming messages.
    7777     */
    7878    void Poll();
    public:  
    9191    /**
    9292     * Send a message to the server.
    9393     */
    9494    virtual bool SendMessage(const CNetMessage* message);
    9595
    96     CStr GetIPAddress() const;
    97 
    9896    /**
    9997     * Number of milliseconds since the most recent packet of the server was received.
    10098     */
    10199    u32 GetLastReceivedTime() const;
    102100
    public:  
    170168     * The server will not receive any further messages sent via this session.
    171169     */
    172170    void DisconnectNow(u32 reason);
    173171
    174172    /**
     173     * Prevent the client of the host from timing out.
     174     */
     175    void DisableTimeout();
     176
     177    /**
    175178     * Send a message to the client.
    176179     */
    177180    virtual bool SendMessage(const CNetMessage* message);
    178181
    179182    CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
  • 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";