Ticket #3575: wip.v1.patch

File wip.v1.patch, 14.7 KB (added by elexis, 8 years ago)

(This was the mentioned wip patch from november 2015 on which the patch above was based on)

  • binaries/data/mods/public/gui/gamesetup/gamesetup_mp.js

     
     1const g_DefaultPort = 20595;
     2
    13var g_IsConnecting = false;
    24var g_GameType; // "server" or "client"
    35var g_ServerName = "";
    46
    57var g_IsRejoining = false;
    function init(attribs)  
    1315    switch (attribs.multiplayerGameType)
    1416    {
    1517    case "join":
    1618        if(Engine.HasXmppClient())
    1719        {
    18             if (startJoin(attribs.name, attribs.ip))
     20            if (startJoin(attribs.name, attribs.ip, attribs.port || g_DefaultPort))
    1921                switchSetupPage("pageJoin", "pageConnecting");
    2022        }
    2123        else
    2224        {
    2325            Engine.GetGUIObjectByName("pageJoin").hidden = false;
    function startHost(playername, servernam  
    198200                Engine.GetGUIObjectByName("hostFeedback").caption = translate("Game name already in use.");
    199201                return false;
    200202            }
    201203        }
    202204    }
     205    // TODO: should we allow regular users to host under different ports?
    203206    try
    204207    {
    205208        if (g_userRating)
    206             Engine.StartNetworkHost(playername + " (" + g_userRating + ")");
     209            Engine.StartNetworkHost(playername + " (" + g_userRating + ")", 777);
    207210        else
    208             Engine.StartNetworkHost(playername);
     211            Engine.StartNetworkHost(playername, 777);
    209212    }
    210213    catch (e)
    211214    {
    212215        cancelSetup();
    213216        messageBox(400, 200,
    function startHost(playername, servernam  
    222225    if (Engine.HasXmppClient())
    223226        Engine.LobbySetPlayerPresence("playing");
    224227    return true;
    225228}
    226229
    227 function startJoin(playername, ip)
     230function startJoin(playername, ip, port)
    228231{
    229232    try
    230233    {
    231234        if (g_userRating)
    232             Engine.StartNetworkJoin(playername + " (" + g_userRating + ")", ip);
     235            Engine.StartNetworkJoin(playername + " (" + g_userRating + ")", ip, port);
    233236        else
    234             Engine.StartNetworkJoin(playername, ip);
     237            Engine.StartNetworkJoin(playername, ip, port);
    235238    }
    236239    catch (e)
    237240    {
    238241        cancelSetup();
    239242        messageBox(400, 200,
    function startJoin(playername, ip)  
    249252        Engine.LobbySetPlayerPresence("playing");
    250253    else {
    251254        // Only save the player name and host address if they're valid and we're not in the lobby
    252255        Engine.ConfigDB_CreateValue("user", "playername", playername);
    253256        Engine.ConfigDB_CreateValue("user", "multiplayerserver", ip);
     257        Engine.ConfigDB_CreateValue("user", "multiplayerport", port);
    254258        Engine.ConfigDB_WriteFile("user", "config/user.cfg");
    255259    }
    256260    return true;
    257261}
    258262
  • binaries/data/mods/public/gui/gamesetup/gamesetup_mp.xml

     
    4343                <action on="Load">
    4444                    this.caption = Engine.ConfigDB_GetValue("user", "multiplayerserver")
    4545                </action>
    4646            </object>
    4747
     48            <object type="text" size="20 120 50% 150" style="ModernLabelText" text_align="right">
     49                <translatableAttribute id="caption">Server Port:</translatableAttribute>
     50            </object>
     51
     52            <object name="joinPort" type="input" size="50%+10 120 100%-20 144" style="ModernInput">
     53                <action on="Load">
     54                    this.caption = Engine.ConfigDB_GetValue("user", "multiplayerport");
     55                </action>
     56            </object>
     57
    4858            <object hotkey="confirm" type="button" size="50%+5 100%-45 100%-18 100%-17" style="ModernButtonRed">
    4959                <translatableAttribute id="caption">Continue</translatableAttribute>
    5060                <action on="Press">
    5161                    var joinPlayerName = Engine.GetGUIObjectByName("joinPlayerName").caption;
    5262                    var joinServer = Engine.GetGUIObjectByName("joinServer").caption;
    53                     if (startJoin(joinPlayerName, joinServer))
     63                    var joinPort = Engine.GetGUIObjectByName("joinPort").caption;
     64                    if (startJoin(joinPlayerName, joinServer, joinPort))
    5465                        switchSetupPage("pageJoin", "pageConnecting");
    5566                </action>
    5667            </object>
    5768        </object>
    5869
  • binaries/data/mods/public/gui/lobby/lobby.js

    function joinSelectedGame()  
    614614    if (gamesBox.selected >= 0)
    615615    {
    616616        var g = gamesBox.list_data[gamesBox.selected];
    617617        var sname = g_Name;
    618618        var sip = g_GameList[g].ip;
     619        var sport= g_GameList[g].port;
    619620
    620621        // TODO: What about valid host names?
    621622        // Check if it looks like an ip address
    622623        if (sip.split('.').length != 4)
    623624        {
    624625            addChatMessage({ "from": "system", "text": sprintf(translate("This game's address '%(ip)s' does not appear to be valid."), { ip: sip }) });
    625626            return;
    626627        }
    627628
    628629        // Open Multiplayer connection window with join option.
    629         Engine.PushGuiPage("page_gamesetup_mp.xml", { multiplayerGameType: "join", name: sname, ip: sip, rating: g_userRating });
     630        Engine.PushGuiPage("page_gamesetup_mp.xml", {
     631            "multiplayerGameType": "join",
     632            "name": sname,
     633            "ip": sip,
     634            "port": sport,
     635            "rating": g_userRating
     636        });
    630637    }
    631638}
    632639
    633640/**
    634641 * Start the hosting process.
  • source/gui/scripting/ScriptFunctions.cpp

    void SetNetworkGameAttributes(ScriptInte  
    299299    JS::RootedValue attribs(cx, attribs1);
    300300
    301301    g_NetServer->UpdateGameAttributes(&attribs, *(pCxPrivate->pScriptInterface));
    302302}
    303303
    304 void StartNetworkHost(ScriptInterface::CxPrivate* pCxPrivate, std::wstring playerName)
     304void StartNetworkHost(ScriptInterface::CxPrivate* pCxPrivate, std::wstring playerName, unsigned int serverPort)
    305305{
    306306    ENSURE(!g_NetClient);
    307307    ENSURE(!g_NetServer);
    308308    ENSURE(!g_Game);
    309309
    310310    g_NetServer = new CNetServer();
    311     if (!g_NetServer->SetupConnection())
     311    if (!g_NetServer->SetupConnection(serverPort))
    312312    {
    313313        pCxPrivate->pScriptInterface->ReportError("Failed to start server");
    314314        SAFE_DELETE(g_NetServer);
    315315        return;
    316316    }
    317317
    318318    g_Game = new CGame();
    319319    g_NetClient = new CNetClient(g_Game);
    320320    g_NetClient->SetUserName(playerName);
    321321
    322     if (!g_NetClient->SetupConnection("127.0.0.1"))
     322    if (!g_NetClient->SetupConnection("127.0.0.1", serverPort))
    323323    {
    324324        pCxPrivate->pScriptInterface->ReportError("Failed to connect to server");
    325325        SAFE_DELETE(g_NetClient);
    326326        SAFE_DELETE(g_Game);
    327327    }
    328328}
    329329
    330 void StartNetworkJoin(ScriptInterface::CxPrivate* pCxPrivate, std::wstring playerName, std::string serverAddress)
     330void StartNetworkJoin(ScriptInterface::CxPrivate* pCxPrivate, std::wstring playerName, std::string serverAddress, unsigned int serverPort)
    331331{
    332332    ENSURE(!g_NetClient);
    333333    ENSURE(!g_NetServer);
    334334    ENSURE(!g_Game);
    335335
    336336    g_Game = new CGame();
    337337    g_NetClient = new CNetClient(g_Game);
    338338    g_NetClient->SetUserName(playerName);
    339     if (!g_NetClient->SetupConnection(serverAddress))
     339    if (!g_NetClient->SetupConnection(serverAddress, serverPort))
    340340    {
    341341        pCxPrivate->pScriptInterface->ReportError("Failed to connect to server");
    342342        SAFE_DELETE(g_NetClient);
    343343        SAFE_DELETE(g_Game);
    344344    }
    void GuiScriptingInit(ScriptInterface& s  
    958958
    959959    // Network / game setup functions
    960960    scriptInterface.RegisterFunction<void, &StartNetworkGame>("StartNetworkGame");
    961961    scriptInterface.RegisterFunction<void, JS::HandleValue, int, &StartGame>("StartGame");
    962962    scriptInterface.RegisterFunction<void, &Script_EndGame>("EndGame");
    963     scriptInterface.RegisterFunction<void, std::wstring, &StartNetworkHost>("StartNetworkHost");
    964     scriptInterface.RegisterFunction<void, std::wstring, std::string, &StartNetworkJoin>("StartNetworkJoin");
     963    scriptInterface.RegisterFunction<void, std::wstring, unsigned int, &StartNetworkHost>("StartNetworkHost");
     964    scriptInterface.RegisterFunction<void, std::wstring, std::string, unsigned int, &StartNetworkJoin>("StartNetworkJoin");
    965965    scriptInterface.RegisterFunction<void, &DisconnectNetworkGame>("DisconnectNetworkGame");
    966966    scriptInterface.RegisterFunction<JS::Value, &PollNetworkClient>("PollNetworkClient");
    967967    scriptInterface.RegisterFunction<void, JS::HandleValue, &SetNetworkGameAttributes>("SetNetworkGameAttributes");
    968968    scriptInterface.RegisterFunction<void, int, std::string, &AssignNetworkPlayer>("AssignNetworkPlayer");
    969969    scriptInterface.RegisterFunction<void, std::string, int, &SetNetworkPlayerStatus>("SetNetworkPlayerStatus");
  • source/network/NetClient.cpp

    void CNetClient::SetUserName(const CStrW  
    139139    ENSURE(!m_Session); // must be called before we start the connection
    140140
    141141    m_UserName = username;
    142142}
    143143
    144 bool CNetClient::SetupConnection(const CStr& server)
     144bool CNetClient::SetupConnection(const CStr& server, u16 port)
    145145{
    146146    CNetClientSession* session = new CNetClientSession(*this);
    147     bool ok = session->Connect(PS_DEFAULT_PORT, server);
     147    bool ok = session->Connect(port, server);
    148148    SetAndOwnSession(session);
    149149    return ok;
    150150}
    151151
    152152void CNetClient::SetAndOwnSession(CNetClientSession* session)
  • source/network/NetClient.h

    public:  
    9191    /**
    9292     * Set up a connection to the remote networked server.
    9393     * @param server IP address or host name to connect to
    9494     * @return true on success, false on connection failure
    9595     */
    96     bool SetupConnection(const CStr& server);
     96    bool SetupConnection(const CStr& server, u16 port);
    9797
    9898    /**
    9999     * Destroy the connection to the server.
    100100     * This client probably cannot be used again.
    101101     */
  • source/network/NetServer.cpp

    CNetServerWorker::~CNetServerWorker()  
    161161    }
    162162
    163163    delete m_ServerTurnManager;
    164164}
    165165
    166 bool CNetServerWorker::SetupConnection()
     166bool CNetServerWorker::SetupConnection(u16 port)
    167167{
    168168    ENSURE(m_State == SERVER_STATE_UNCONNECTED);
    169169    ENSURE(!m_Host);
    170170
    171171    // Bind to default host
    172172    ENetAddress addr;
    173173    addr.host = ENET_HOST_ANY;
    174     addr.port = PS_DEFAULT_PORT;
     174    addr.port = port;
    175175
    176176    // Create ENet server
    177177    m_Host = enet_host_create(&addr, MAX_CLIENTS, CHANNEL_COUNT, 0, 0);
    178178    if (!m_Host)
    179179    {
    CNetServer::CNetServer(int autostartPlay  
    11611161CNetServer::~CNetServer()
    11621162{
    11631163    delete m_Worker;
    11641164}
    11651165
    1166 bool CNetServer::SetupConnection()
     1166bool CNetServer::SetupConnection(u16 port)
    11671167{
    1168     return m_Worker->SetupConnection();
     1168    return m_Worker->SetupConnection(port);
    11691169}
    11701170
    11711171void CNetServer::AssignPlayer(int playerID, const CStr& guid)
    11721172{
    11731173    CScopeLock lock(m_Worker->m_WorkerMutex);
  • source/network/NetServer.h

    public:  
    111111    /**
    112112     * Begin listening for network connections.
    113113     * This function is synchronous (it won't return until the connection is established).
    114114     * @return true on success, false on error (e.g. port already in use)
    115115     */
    116     bool SetupConnection();
     116    bool SetupConnection(u16 port);
    117117
    118118    /**
    119119     * Call from the GUI to update the player assignments.
    120120     * The given GUID will be (re)assigned to the given player ID.
    121121     * Any player currently using that ID will be unassigned.
    private:  
    197197
    198198    /**
    199199     * Begin listening for network connections.
    200200     * @return true on success, false on error (e.g. port already in use)
    201201     */
    202     bool SetupConnection();
     202    bool SetupConnection(u16 port);
    203203
    204204    /**
    205205     * Call from the GUI to update the player assignments.
    206206     * The given GUID will be (re)assigned to the given player ID.
    207207     * Any player currently using that ID will be unassigned.
  • source/network/tests/test_Net.h

     
    2222#include "lib/external_libraries/libsdl.h"
    2323#include "lib/tex/tex.h"
    2424#include "network/NetServer.h"
    2525#include "network/NetClient.h"
    2626#include "network/NetTurnManager.h"
     27#include "network/NetMessage.h"
     28#include "network/NetMessages.h"
    2729#include "ps/CLogger.h"
    2830#include "ps/Game.h"
    2931#include "ps/Filesystem.h"
    3032#include "ps/Loader.h"
    3133#include "ps/XML/Xeromyces.h"
    public:  
    6971        return true;
    7072    }
    7173
    7274    void connect(CNetServer& server, const std::vector<CNetClient*>& clients)
    7375    {
    74         TS_ASSERT(server.SetupConnection());
     76        TS_ASSERT(server.SetupConnection(PS_DEFAULT_PORT));
    7577        for (size_t j = 0; j < clients.size(); ++j)
    76             TS_ASSERT(clients[j]->SetupConnection("127.0.0.1"));
     78            TS_ASSERT(clients[j]->SetupConnection("127.0.0.1", PS_DEFAULT_PORT));
    7779
    7880        for (size_t i = 0; ; ++i)
    7981        {
    8082//          debug_printf(".");
    8183            for (size_t j = 0; j < clients.size(); ++j)
    public:  
    271273        CGame client2BGame(true);
    272274        CNetClient client2B(&client2BGame);
    273275        client2B.SetUserName(L"bob");
    274276        clients.push_back(&client2B);
    275277
    276         TS_ASSERT(client2B.SetupConnection("127.0.0.1"));
     278        TS_ASSERT(client2B.SetupConnection("127.0.0.1", PS_DEFAULT_PORT));
    277279
    278280        for (size_t i = 0; ; ++i)
    279281        {
    280282            debug_printf("[%u]\n", client2B.GetCurrState());
    281283            client2B.Poll();
  • source/ps/GameSetup/GameSetup.cpp

     
    4747#include "gui/scripting/ScriptFunctions.h"
    4848#include "i18n/L10n.h"
    4949#include "maths/MathUtil.h"
    5050#include "network/NetServer.h"
    5151#include "network/NetClient.h"
     52#include "network/NetMessage.h"
     53#include "network/NetMessages.h"
    5254
    5355#include "ps/CConsole.h"
    5456#include "ps/CLogger.h"
    5557#include "ps/ConfigDB.h"
    5658#include "ps/Filesystem.h"
    bool Autostart(const CmdLineArgs& args)  
    14341436
    14351437        g_NetServer = new CNetServer(maxPlayers);
    14361438
    14371439        g_NetServer->UpdateGameAttributes(&attrs, scriptInterface);
    14381440
    1439         bool ok = g_NetServer->SetupConnection();
     1441        bool ok = g_NetServer->SetupConnection(PS_DEFAULT_PORT);
    14401442        ENSURE(ok);
    14411443
    14421444        g_NetClient = new CNetClient(g_Game);
    14431445        g_NetClient->SetUserName(userName);
    1444         g_NetClient->SetupConnection("127.0.0.1");
     1446        g_NetClient->SetupConnection("127.0.0.1", PS_DEFAULT_PORT);
    14451447    }
    14461448    else if (args.Has("autostart-client"))
    14471449    {
    14481450        InitPs(true, L"page_loading.xml", &scriptInterface, mpInitData);
    14491451
    bool Autostart(const CmdLineArgs& args)  
    14521454
    14531455        CStr ip = args.Get("autostart-client");
    14541456        if (ip.empty())
    14551457            ip = "127.0.0.1";
    14561458
    1457         bool ok = g_NetClient->SetupConnection(ip);
     1459        bool ok = g_NetClient->SetupConnection(ip, PS_DEFAULT_PORT);
    14581460        ENSURE(ok);
    14591461    }
    14601462    else
    14611463    {
    14621464        g_Game->SetPlayerID(1);