Ticket #3575: wip.v1.patch
File wip.v1.patch, 14.7 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/gui/gamesetup/gamesetup_mp.js
1 const g_DefaultPort = 20595; 2 1 3 var g_IsConnecting = false; 2 4 var g_GameType; // "server" or "client" 3 5 var g_ServerName = ""; 4 6 5 7 var g_IsRejoining = false; … … function init(attribs) 13 15 switch (attribs.multiplayerGameType) 14 16 { 15 17 case "join": 16 18 if(Engine.HasXmppClient()) 17 19 { 18 if (startJoin(attribs.name, attribs.ip ))20 if (startJoin(attribs.name, attribs.ip, attribs.port || g_DefaultPort)) 19 21 switchSetupPage("pageJoin", "pageConnecting"); 20 22 } 21 23 else 22 24 { 23 25 Engine.GetGUIObjectByName("pageJoin").hidden = false; … … function startHost(playername, servernam 198 200 Engine.GetGUIObjectByName("hostFeedback").caption = translate("Game name already in use."); 199 201 return false; 200 202 } 201 203 } 202 204 } 205 // TODO: should we allow regular users to host under different ports? 203 206 try 204 207 { 205 208 if (g_userRating) 206 Engine.StartNetworkHost(playername + " (" + g_userRating + ")" );209 Engine.StartNetworkHost(playername + " (" + g_userRating + ")", 777); 207 210 else 208 Engine.StartNetworkHost(playername );211 Engine.StartNetworkHost(playername, 777); 209 212 } 210 213 catch (e) 211 214 { 212 215 cancelSetup(); 213 216 messageBox(400, 200, … … function startHost(playername, servernam 222 225 if (Engine.HasXmppClient()) 223 226 Engine.LobbySetPlayerPresence("playing"); 224 227 return true; 225 228 } 226 229 227 function startJoin(playername, ip )230 function startJoin(playername, ip, port) 228 231 { 229 232 try 230 233 { 231 234 if (g_userRating) 232 Engine.StartNetworkJoin(playername + " (" + g_userRating + ")", ip );235 Engine.StartNetworkJoin(playername + " (" + g_userRating + ")", ip, port); 233 236 else 234 Engine.StartNetworkJoin(playername, ip );237 Engine.StartNetworkJoin(playername, ip, port); 235 238 } 236 239 catch (e) 237 240 { 238 241 cancelSetup(); 239 242 messageBox(400, 200, … … function startJoin(playername, ip) 249 252 Engine.LobbySetPlayerPresence("playing"); 250 253 else { 251 254 // Only save the player name and host address if they're valid and we're not in the lobby 252 255 Engine.ConfigDB_CreateValue("user", "playername", playername); 253 256 Engine.ConfigDB_CreateValue("user", "multiplayerserver", ip); 257 Engine.ConfigDB_CreateValue("user", "multiplayerport", port); 254 258 Engine.ConfigDB_WriteFile("user", "config/user.cfg"); 255 259 } 256 260 return true; 257 261 } 258 262 -
binaries/data/mods/public/gui/gamesetup/gamesetup_mp.xml
43 43 <action on="Load"> 44 44 this.caption = Engine.ConfigDB_GetValue("user", "multiplayerserver") 45 45 </action> 46 46 </object> 47 47 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 48 58 <object hotkey="confirm" type="button" size="50%+5 100%-45 100%-18 100%-17" style="ModernButtonRed"> 49 59 <translatableAttribute id="caption">Continue</translatableAttribute> 50 60 <action on="Press"> 51 61 var joinPlayerName = Engine.GetGUIObjectByName("joinPlayerName").caption; 52 62 var joinServer = Engine.GetGUIObjectByName("joinServer").caption; 53 if (startJoin(joinPlayerName, joinServer)) 63 var joinPort = Engine.GetGUIObjectByName("joinPort").caption; 64 if (startJoin(joinPlayerName, joinServer, joinPort)) 54 65 switchSetupPage("pageJoin", "pageConnecting"); 55 66 </action> 56 67 </object> 57 68 </object> 58 69 -
binaries/data/mods/public/gui/lobby/lobby.js
function joinSelectedGame() 614 614 if (gamesBox.selected >= 0) 615 615 { 616 616 var g = gamesBox.list_data[gamesBox.selected]; 617 617 var sname = g_Name; 618 618 var sip = g_GameList[g].ip; 619 var sport= g_GameList[g].port; 619 620 620 621 // TODO: What about valid host names? 621 622 // Check if it looks like an ip address 622 623 if (sip.split('.').length != 4) 623 624 { 624 625 addChatMessage({ "from": "system", "text": sprintf(translate("This game's address '%(ip)s' does not appear to be valid."), { ip: sip }) }); 625 626 return; 626 627 } 627 628 628 629 // 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 }); 630 637 } 631 638 } 632 639 633 640 /** 634 641 * Start the hosting process. -
source/gui/scripting/ScriptFunctions.cpp
void SetNetworkGameAttributes(ScriptInte 299 299 JS::RootedValue attribs(cx, attribs1); 300 300 301 301 g_NetServer->UpdateGameAttributes(&attribs, *(pCxPrivate->pScriptInterface)); 302 302 } 303 303 304 void StartNetworkHost(ScriptInterface::CxPrivate* pCxPrivate, std::wstring playerName )304 void StartNetworkHost(ScriptInterface::CxPrivate* pCxPrivate, std::wstring playerName, unsigned int serverPort) 305 305 { 306 306 ENSURE(!g_NetClient); 307 307 ENSURE(!g_NetServer); 308 308 ENSURE(!g_Game); 309 309 310 310 g_NetServer = new CNetServer(); 311 if (!g_NetServer->SetupConnection( ))311 if (!g_NetServer->SetupConnection(serverPort)) 312 312 { 313 313 pCxPrivate->pScriptInterface->ReportError("Failed to start server"); 314 314 SAFE_DELETE(g_NetServer); 315 315 return; 316 316 } 317 317 318 318 g_Game = new CGame(); 319 319 g_NetClient = new CNetClient(g_Game); 320 320 g_NetClient->SetUserName(playerName); 321 321 322 if (!g_NetClient->SetupConnection("127.0.0.1" ))322 if (!g_NetClient->SetupConnection("127.0.0.1", serverPort)) 323 323 { 324 324 pCxPrivate->pScriptInterface->ReportError("Failed to connect to server"); 325 325 SAFE_DELETE(g_NetClient); 326 326 SAFE_DELETE(g_Game); 327 327 } 328 328 } 329 329 330 void StartNetworkJoin(ScriptInterface::CxPrivate* pCxPrivate, std::wstring playerName, std::string serverAddress )330 void StartNetworkJoin(ScriptInterface::CxPrivate* pCxPrivate, std::wstring playerName, std::string serverAddress, unsigned int serverPort) 331 331 { 332 332 ENSURE(!g_NetClient); 333 333 ENSURE(!g_NetServer); 334 334 ENSURE(!g_Game); 335 335 336 336 g_Game = new CGame(); 337 337 g_NetClient = new CNetClient(g_Game); 338 338 g_NetClient->SetUserName(playerName); 339 if (!g_NetClient->SetupConnection(serverAddress ))339 if (!g_NetClient->SetupConnection(serverAddress, serverPort)) 340 340 { 341 341 pCxPrivate->pScriptInterface->ReportError("Failed to connect to server"); 342 342 SAFE_DELETE(g_NetClient); 343 343 SAFE_DELETE(g_Game); 344 344 } … … void GuiScriptingInit(ScriptInterface& s 958 958 959 959 // Network / game setup functions 960 960 scriptInterface.RegisterFunction<void, &StartNetworkGame>("StartNetworkGame"); 961 961 scriptInterface.RegisterFunction<void, JS::HandleValue, int, &StartGame>("StartGame"); 962 962 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"); 965 965 scriptInterface.RegisterFunction<void, &DisconnectNetworkGame>("DisconnectNetworkGame"); 966 966 scriptInterface.RegisterFunction<JS::Value, &PollNetworkClient>("PollNetworkClient"); 967 967 scriptInterface.RegisterFunction<void, JS::HandleValue, &SetNetworkGameAttributes>("SetNetworkGameAttributes"); 968 968 scriptInterface.RegisterFunction<void, int, std::string, &AssignNetworkPlayer>("AssignNetworkPlayer"); 969 969 scriptInterface.RegisterFunction<void, std::string, int, &SetNetworkPlayerStatus>("SetNetworkPlayerStatus"); -
source/network/NetClient.cpp
void CNetClient::SetUserName(const CStrW 139 139 ENSURE(!m_Session); // must be called before we start the connection 140 140 141 141 m_UserName = username; 142 142 } 143 143 144 bool CNetClient::SetupConnection(const CStr& server )144 bool CNetClient::SetupConnection(const CStr& server, u16 port) 145 145 { 146 146 CNetClientSession* session = new CNetClientSession(*this); 147 bool ok = session->Connect( PS_DEFAULT_PORT, server);147 bool ok = session->Connect(port, server); 148 148 SetAndOwnSession(session); 149 149 return ok; 150 150 } 151 151 152 152 void CNetClient::SetAndOwnSession(CNetClientSession* session) -
source/network/NetClient.h
public: 91 91 /** 92 92 * Set up a connection to the remote networked server. 93 93 * @param server IP address or host name to connect to 94 94 * @return true on success, false on connection failure 95 95 */ 96 bool SetupConnection(const CStr& server );96 bool SetupConnection(const CStr& server, u16 port); 97 97 98 98 /** 99 99 * Destroy the connection to the server. 100 100 * This client probably cannot be used again. 101 101 */ -
source/network/NetServer.cpp
CNetServerWorker::~CNetServerWorker() 161 161 } 162 162 163 163 delete m_ServerTurnManager; 164 164 } 165 165 166 bool CNetServerWorker::SetupConnection( )166 bool CNetServerWorker::SetupConnection(u16 port) 167 167 { 168 168 ENSURE(m_State == SERVER_STATE_UNCONNECTED); 169 169 ENSURE(!m_Host); 170 170 171 171 // Bind to default host 172 172 ENetAddress addr; 173 173 addr.host = ENET_HOST_ANY; 174 addr.port = PS_DEFAULT_PORT;174 addr.port = port; 175 175 176 176 // Create ENet server 177 177 m_Host = enet_host_create(&addr, MAX_CLIENTS, CHANNEL_COUNT, 0, 0); 178 178 if (!m_Host) 179 179 { … … CNetServer::CNetServer(int autostartPlay 1161 1161 CNetServer::~CNetServer() 1162 1162 { 1163 1163 delete m_Worker; 1164 1164 } 1165 1165 1166 bool CNetServer::SetupConnection( )1166 bool CNetServer::SetupConnection(u16 port) 1167 1167 { 1168 return m_Worker->SetupConnection( );1168 return m_Worker->SetupConnection(port); 1169 1169 } 1170 1170 1171 1171 void CNetServer::AssignPlayer(int playerID, const CStr& guid) 1172 1172 { 1173 1173 CScopeLock lock(m_Worker->m_WorkerMutex); -
source/network/NetServer.h
public: 111 111 /** 112 112 * Begin listening for network connections. 113 113 * This function is synchronous (it won't return until the connection is established). 114 114 * @return true on success, false on error (e.g. port already in use) 115 115 */ 116 bool SetupConnection( );116 bool SetupConnection(u16 port); 117 117 118 118 /** 119 119 * Call from the GUI to update the player assignments. 120 120 * The given GUID will be (re)assigned to the given player ID. 121 121 * Any player currently using that ID will be unassigned. … … private: 197 197 198 198 /** 199 199 * Begin listening for network connections. 200 200 * @return true on success, false on error (e.g. port already in use) 201 201 */ 202 bool SetupConnection( );202 bool SetupConnection(u16 port); 203 203 204 204 /** 205 205 * Call from the GUI to update the player assignments. 206 206 * The given GUID will be (re)assigned to the given player ID. 207 207 * Any player currently using that ID will be unassigned. -
source/network/tests/test_Net.h
22 22 #include "lib/external_libraries/libsdl.h" 23 23 #include "lib/tex/tex.h" 24 24 #include "network/NetServer.h" 25 25 #include "network/NetClient.h" 26 26 #include "network/NetTurnManager.h" 27 #include "network/NetMessage.h" 28 #include "network/NetMessages.h" 27 29 #include "ps/CLogger.h" 28 30 #include "ps/Game.h" 29 31 #include "ps/Filesystem.h" 30 32 #include "ps/Loader.h" 31 33 #include "ps/XML/Xeromyces.h" … … public: 69 71 return true; 70 72 } 71 73 72 74 void connect(CNetServer& server, const std::vector<CNetClient*>& clients) 73 75 { 74 TS_ASSERT(server.SetupConnection( ));76 TS_ASSERT(server.SetupConnection(PS_DEFAULT_PORT)); 75 77 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)); 77 79 78 80 for (size_t i = 0; ; ++i) 79 81 { 80 82 // debug_printf("."); 81 83 for (size_t j = 0; j < clients.size(); ++j) … … public: 271 273 CGame client2BGame(true); 272 274 CNetClient client2B(&client2BGame); 273 275 client2B.SetUserName(L"bob"); 274 276 clients.push_back(&client2B); 275 277 276 TS_ASSERT(client2B.SetupConnection("127.0.0.1" ));278 TS_ASSERT(client2B.SetupConnection("127.0.0.1", PS_DEFAULT_PORT)); 277 279 278 280 for (size_t i = 0; ; ++i) 279 281 { 280 282 debug_printf("[%u]\n", client2B.GetCurrState()); 281 283 client2B.Poll(); -
source/ps/GameSetup/GameSetup.cpp
47 47 #include "gui/scripting/ScriptFunctions.h" 48 48 #include "i18n/L10n.h" 49 49 #include "maths/MathUtil.h" 50 50 #include "network/NetServer.h" 51 51 #include "network/NetClient.h" 52 #include "network/NetMessage.h" 53 #include "network/NetMessages.h" 52 54 53 55 #include "ps/CConsole.h" 54 56 #include "ps/CLogger.h" 55 57 #include "ps/ConfigDB.h" 56 58 #include "ps/Filesystem.h" … … bool Autostart(const CmdLineArgs& args) 1434 1436 1435 1437 g_NetServer = new CNetServer(maxPlayers); 1436 1438 1437 1439 g_NetServer->UpdateGameAttributes(&attrs, scriptInterface); 1438 1440 1439 bool ok = g_NetServer->SetupConnection( );1441 bool ok = g_NetServer->SetupConnection(PS_DEFAULT_PORT); 1440 1442 ENSURE(ok); 1441 1443 1442 1444 g_NetClient = new CNetClient(g_Game); 1443 1445 g_NetClient->SetUserName(userName); 1444 g_NetClient->SetupConnection("127.0.0.1" );1446 g_NetClient->SetupConnection("127.0.0.1", PS_DEFAULT_PORT); 1445 1447 } 1446 1448 else if (args.Has("autostart-client")) 1447 1449 { 1448 1450 InitPs(true, L"page_loading.xml", &scriptInterface, mpInitData); 1449 1451 … … bool Autostart(const CmdLineArgs& args) 1452 1454 1453 1455 CStr ip = args.Get("autostart-client"); 1454 1456 if (ip.empty()) 1455 1457 ip = "127.0.0.1"; 1456 1458 1457 bool ok = g_NetClient->SetupConnection(ip );1459 bool ok = g_NetClient->SetupConnection(ip, PS_DEFAULT_PORT); 1458 1460 ENSURE(ok); 1459 1461 } 1460 1462 else 1461 1463 { 1462 1464 g_Game->SetPlayerID(1);