Ticket #3953: disable_timeouts_differently_v1.patch
File disable_timeouts_differently_v1.patch, 13.2 KB (added by , 8 years ago) |
---|
-
source/gui/scripting/ScriptFunctions.cpp
void StartNetworkHost(ScriptInterface::C 357 357 SAFE_DELETE(g_NetServer); 358 358 return; 359 359 } 360 360 361 361 g_Game = new CGame(); 362 g_NetClient = new CNetClient(g_Game );362 g_NetClient = new CNetClient(g_Game, true); 363 363 g_NetClient->SetUserName(playerName); 364 364 365 365 if (!g_NetClient->SetupConnection("127.0.0.1")) 366 366 { 367 367 pCxPrivate->pScriptInterface->ReportError("Failed to connect to server"); … … void StartNetworkJoin(ScriptInterface::C 375 375 ENSURE(!g_NetClient); 376 376 ENSURE(!g_NetServer); 377 377 ENSURE(!g_Game); 378 378 379 379 g_Game = new CGame(); 380 g_NetClient = new CNetClient(g_Game );380 g_NetClient = new CNetClient(g_Game, false); 381 381 g_NetClient->SetUserName(playerName); 382 382 if (!g_NetClient->SetupConnection(serverAddress)) 383 383 { 384 384 pCxPrivate->pScriptInterface->ReportError("Failed to connect to server"); 385 385 SAFE_DELETE(g_NetClient); -
source/network/NetClient.cpp
public: 64 64 65 65 private: 66 66 CNetClient& m_Client; 67 67 }; 68 68 69 CNetClient::CNetClient(CGame* game ) :69 CNetClient::CNetClient(CGame* game, bool disableTimeout) : 70 70 m_Session(NULL), 71 71 m_UserName(L"anonymous"), 72 72 m_GUID(ps_generate_guid()), m_HostID((u32)-1), m_ClientTurnManager(NULL), m_Game(game), 73 73 m_GameAttributes(game->GetSimulation2()->GetScriptInterface().GetContext()), 74 m_DisableTimeout(disableTimeout), 74 75 m_LastConnectionCheck(0) 75 76 { 76 77 m_Game->SetTurnManager(NULL); // delete the old local turn manager so we don't accidentally use it 77 78 78 79 void* context = this; … … void CNetClient::SetUserName(const CStrW 152 153 } 153 154 154 155 bool CNetClient::SetupConnection(const CStr& server) 155 156 { 156 157 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); 158 159 SetAndOwnSession(session); 159 160 return ok; 160 161 } 161 162 162 163 void CNetClient::SetAndOwnSession(CNetClientSession* session) … … bool CNetClient::OnHandshakeResponse(voi 465 466 466 467 CAuthenticateMessage authenticate; 467 468 authenticate.m_GUID = client->m_GUID; 468 469 authenticate.m_Name = client->m_UserName; 469 470 authenticate.m_Password = L""; // TODO 471 authenticate.m_DisableTimeout = client->m_DisableTimeout; 470 472 client->SendMessage(&authenticate); 471 473 472 474 return true; 473 475 } 474 476 -
source/network/NetClient.h
class CNetClient : public CFsm 63 63 public: 64 64 /** 65 65 * Construct a client associated with the given game object. 66 66 * The game must exist for the lifetime of this object. 67 67 */ 68 CNetClient(CGame* game );68 CNetClient(CGame* game, bool disableTimeout); 69 69 70 70 virtual ~CNetClient(); 71 71 72 72 /** 73 73 * We assume that adding a tracing function that's only called … … private: 237 237 CNetClientTurnManager* m_ClientTurnManager; 238 238 239 239 /// Unique-per-game identifier of this client, used to identify the sender of simulation commands 240 240 u32 m_HostID; 241 241 242 /// Whether to prevent the client of the host from timing out 243 bool m_DisableTimeout; 244 242 245 /// Latest copy of game setup attributes heard from the server 243 246 JS::PersistentRootedValue m_GameAttributes; 244 247 245 248 /// Latest copy of player assignments heard from the server 246 249 PlayerAssignmentMap m_PlayerAssignments; -
source/network/NetMessages.h
26 26 #include "ps/CStr.h" 27 27 #include "scriptinterface/ScriptVal.h" 28 28 29 29 #define PS_PROTOCOL_MAGIC 0x5073013f // 'P', 's', 0x01, '?' 30 30 #define PS_PROTOCOL_MAGIC_RESPONSE 0x50630121 // 'P', 'c', 0x01, '!' 31 #define PS_PROTOCOL_VERSION 0x0101001 1// Arbitrary protocol31 #define PS_PROTOCOL_VERSION 0x01010012 // Arbitrary protocol 32 32 #define PS_DEFAULT_PORT 0x5073 // 'P', 's' 33 33 34 34 // Defines the list of message types. The order of the list must not change. 35 35 // The message types having a negative value are used internally and not sent 36 36 // over the network. The message types used for network communication have … … END_NMT_CLASS() 110 110 111 111 START_NMT_CLASS_(Authenticate, NMT_AUTHENTICATE) 112 112 NMT_FIELD(CStr8, m_GUID) 113 113 NMT_FIELD(CStrW, m_Name) 114 114 NMT_FIELD(CStrW, m_Password) 115 NMT_FIELD_INT(m_DisableTimeout, u8, 1) 115 116 END_NMT_CLASS() 116 117 117 118 START_NMT_CLASS_(AuthenticateResult, NMT_AUTHENTICATE_RESULT) 118 119 NMT_FIELD_INT(m_Code, u32, 4) 119 120 NMT_FIELD_INT(m_HostID, u32, 2) -
source/network/NetServer.cpp
bool CNetServerWorker::RunStep() 484 484 485 485 // Set up a session object for this peer 486 486 487 487 CNetServerSession* session = new CNetServerSession(*this, event.peer); 488 488 489 // Prevent the local client of the host from timing out too quickly490 #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 #endif494 495 489 m_Sessions.push_back(session); 496 490 497 491 SetupSession(session); 498 492 499 493 ENSURE(event.peer->data == NULL); … … bool CNetServerWorker::HandleConnect(CNe 684 678 handshake.m_ProtocolVersion = PS_PROTOCOL_VERSION; 685 679 handshake.m_SoftwareVersion = PS_PROTOCOL_VERSION; 686 680 return session->SendMessage(&handshake); 687 681 } 688 682 689 void CNetServerWorker::OnUserJoin(CNetServerSession* session )683 void CNetServerWorker::OnUserJoin(CNetServerSession* session, bool disableTimeout) 690 684 { 691 685 AddPlayer(session->GetGUID(), session->GetUserName()); 692 686 693 if (m_HostGUID.empty() && session->GetIPAddress() == "127.0.0.1")687 if (m_HostGUID.empty()) 694 688 m_HostGUID = session->GetGUID(); 695 689 690 if (disableTimeout) 691 session->DisableTimeout(); 692 696 693 CGameSetupMessage gameSetupMessage(GetScriptInterface()); 697 694 gameSetupMessage.m_Data = m_GameAttributes.get(); 698 695 session->SendMessage(&gameSetupMessage); 699 696 700 697 CPlayerAssignmentMessage assignMessage; … … bool CNetServerWorker::OnAuthenticate(vo 1004 1001 authenticateResult.m_Code = isRejoining ? ARC_OK_REJOINING : ARC_OK; 1005 1002 authenticateResult.m_HostID = newHostID; 1006 1003 authenticateResult.m_Message = L"Logged in"; 1007 1004 session->SendMessage(&authenticateResult); 1008 1005 1009 server.OnUserJoin(session );1006 server.OnUserJoin(session, message->m_DisableTimeout); 1010 1007 1011 1008 if (isRejoining) 1012 1009 { 1013 1010 // Request a copy of the current game state from an existing player, 1014 1011 // so we can send it on to the new player -
source/network/NetServer.h
private: 260 260 void ClearAllPlayerReady(); 261 261 262 262 void SetupSession(CNetServerSession* session); 263 263 bool HandleConnect(CNetServerSession* session); 264 264 265 void OnUserJoin(CNetServerSession* session );265 void OnUserJoin(CNetServerSession* session, bool disableTimeout); 266 266 void OnUserLeave(CNetServerSession* session); 267 267 268 268 static bool OnClientHandshake(void* context, CFsmEvent* event); 269 269 static bool OnAuthenticate(void* context, CFsmEvent* event); 270 270 static bool OnInGame(void* context, CFsmEvent* event); -
source/network/NetSession.cpp
CNetClientSession::~CNetClientSession() 49 49 m_Host = NULL; 50 50 m_Server = NULL; 51 51 } 52 52 } 53 53 54 bool CNetClientSession::Connect(u16 port, const CStr& server )54 bool CNetClientSession::Connect(u16 port, const CStr& server, bool disableTimeout) 55 55 { 56 56 ENSURE(!m_Host); 57 57 ENSURE(!m_Server); 58 58 59 59 // Create ENet host … … bool CNetClientSession::Connect(u16 port 75 75 m_Host = host; 76 76 m_Server = peer; 77 77 78 78 // Prevent the local client of the host from timing out too quickly. 79 79 #if (ENET_VERSION >= ENET_VERSION_CREATE(1, 3, 4)) 80 if ( GetIPAddress() == "127.0.0.1")80 if (disableTimeout) 81 81 enet_peer_timeout(peer, 1, MAXIMUM_HOST_TIMEOUT, MAXIMUM_HOST_TIMEOUT); 82 82 #endif 83 83 84 84 m_Stats = new CNetStatsTable(m_Server); 85 85 if (CProfileViewer::IsInitialised()) … … bool CNetClientSession::SendMessage(cons 176 176 ENSURE(m_Host && m_Server); 177 177 178 178 return CNetHost::SendMessage(message, m_Server, "server"); 179 179 } 180 180 181 CStr CNetClientSession::GetIPAddress() const182 {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 190 181 u32 CNetClientSession::GetLastReceivedTime() const 191 182 { 192 183 if (!m_Server) 193 184 return 0; 194 185 … … void CNetServerSession::DisconnectNow(u3 249 240 250 241 bool CNetServerSession::SendMessage(const CNetMessage* message) 251 242 { 252 243 return m_Server.SendMessage(m_Peer, message); 253 244 } 245 246 void 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 68 68 69 69 public: 70 70 CNetClientSession(CNetClient& client); 71 71 ~CNetClientSession(); 72 72 73 bool Connect(u16 port, const CStr& server );73 bool Connect(u16 port, const CStr& server, bool disableTimeout); 74 74 75 75 /** 76 76 * Process queued incoming messages. 77 77 */ 78 78 void Poll(); … … public: 91 91 /** 92 92 * Send a message to the server. 93 93 */ 94 94 virtual bool SendMessage(const CNetMessage* message); 95 95 96 CStr GetIPAddress() const;97 98 96 /** 99 97 * Number of milliseconds since the most recent packet of the server was received. 100 98 */ 101 99 u32 GetLastReceivedTime() const; 102 100 … … public: 170 168 * The server will not receive any further messages sent via this session. 171 169 */ 172 170 void DisconnectNow(u32 reason); 173 171 174 172 /** 173 * Prevent the client of the host from timing out. 174 */ 175 void DisableTimeout(); 176 177 /** 175 178 * Send a message to the client. 176 179 */ 177 180 virtual bool SendMessage(const CNetMessage* message); 178 181 179 182 CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; } -
source/network/tests/test_Net.h
public: 150 150 151 151 JS::RootedValue attrs(cx); 152 152 scriptInterface.Eval("({mapType:'scenario',map:'maps/scenarios/Saharan Oases',mapPath:'maps/scenarios/',thing:'example'})", &attrs); 153 153 server.UpdateGameAttributes(&attrs, scriptInterface); 154 154 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); 158 158 159 159 clients.push_back(&client1); 160 160 clients.push_back(&client2); 161 161 clients.push_back(&client3); 162 162 … … public: 215 215 216 216 JS::RootedValue attrs(cx); 217 217 scriptInterface.Eval("({mapType:'scenario',map:'maps/scenarios/Saharan Oases',mapPath:'maps/scenarios/',thing:'example'})", &attrs); 218 218 server.UpdateGameAttributes(&attrs, scriptInterface); 219 219 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); 223 223 224 224 client1.SetUserName(L"alice"); 225 225 client2.SetUserName(L"bob"); 226 226 client3.SetUserName(L"charlie"); 227 227 … … public: 267 267 clients.erase(clients.begin()+1); 268 268 269 269 debug_printf("==== Connecting client 2B\n"); 270 270 271 271 CGame client2BGame(true); 272 CNetClient client2B(&client2BGame );272 CNetClient client2B(&client2BGame, false); 273 273 client2B.SetUserName(L"bob"); 274 274 clients.push_back(&client2B); 275 275 276 276 TS_ASSERT(client2B.SetupConnection("127.0.0.1")); 277 277 -
source/ps/GameSetup/GameSetup.cpp
bool Autostart(const CmdLineArgs& args) 1445 1445 g_NetServer->UpdateGameAttributes(&attrs, scriptInterface); 1446 1446 1447 1447 bool ok = g_NetServer->SetupConnection(); 1448 1448 ENSURE(ok); 1449 1449 1450 g_NetClient = new CNetClient(g_Game );1450 g_NetClient = new CNetClient(g_Game, true); 1451 1451 g_NetClient->SetUserName(userName); 1452 1452 g_NetClient->SetupConnection("127.0.0.1"); 1453 1453 } 1454 1454 else if (args.Has("autostart-client")) 1455 1455 { 1456 1456 InitPs(true, L"page_loading.xml", &scriptInterface, mpInitData); 1457 1457 1458 g_NetClient = new CNetClient(g_Game );1458 g_NetClient = new CNetClient(g_Game, false); 1459 1459 g_NetClient->SetUserName(userName); 1460 1460 1461 1461 CStr ip = args.Get("autostart-client"); 1462 1462 if (ip.empty()) 1463 1463 ip = "127.0.0.1";