forked from mirrors/0ad
Initialize more members of CNetClient
The username and the jid have to be set before the client is connected. With this commit it's not possible to initialize them to late.
This commit is contained in:
@@ -65,9 +65,10 @@ constexpr u32 NETWORK_BAD_PING = DEFAULT_TURN_LENGTH * COMMAND_DELAY_MP / 2;
|
||||
|
||||
CNetClient *g_NetClient = NULL;
|
||||
|
||||
CNetClient::CNetClient(CGame* game) :
|
||||
CNetClient::CNetClient(CGame* game, const CStrW& username, const CStr& hostJID) :
|
||||
m_Session(NULL),
|
||||
m_UserName(L"anonymous"),
|
||||
m_UserName{username},
|
||||
m_HostJID{hostJID},
|
||||
m_HostID((u32)-1), m_ClientTurnManager(NULL), m_Game(game),
|
||||
m_LastConnectionCheck(0),
|
||||
m_ServerAddress(),
|
||||
@@ -154,18 +155,6 @@ void CNetClient::TraceMember(JSTracer *trc)
|
||||
JS::TraceEdge(trc, &guiMessage, "m_GuiMessageQueue");
|
||||
}
|
||||
|
||||
void CNetClient::SetUserName(const CStrW& username)
|
||||
{
|
||||
ENSURE(!m_Session); // must be called before we start the connection
|
||||
|
||||
m_UserName = username;
|
||||
}
|
||||
|
||||
void CNetClient::SetHostJID(const CStr& jid)
|
||||
{
|
||||
m_HostJID = jid;
|
||||
}
|
||||
|
||||
void CNetClient::SetGamePassword(const CStr& hashedPassword)
|
||||
{
|
||||
// Hash on top with the user's name, to make sure not all
|
||||
|
||||
@@ -74,8 +74,10 @@ public:
|
||||
/**
|
||||
* Construct a client associated with the given game object.
|
||||
* The game must exist for the lifetime of this object.
|
||||
* The user's name will be displayed to all players.
|
||||
* The JID of the host is needed for the secure lobby authentication.
|
||||
*/
|
||||
CNetClient(CGame* game);
|
||||
CNetClient(CGame* game, const CStrW& username = L"anonymous", const CStr& hostJID = {});
|
||||
|
||||
virtual ~CNetClient();
|
||||
|
||||
@@ -92,18 +94,6 @@ public:
|
||||
|
||||
void TraceMember(JSTracer *trc);
|
||||
|
||||
/**
|
||||
* Set the user's name that will be displayed to all players.
|
||||
* This must not be called after the connection setup.
|
||||
*/
|
||||
void SetUserName(const CStrW& username);
|
||||
|
||||
/**
|
||||
* Store the JID of the host.
|
||||
* This is needed for the secure lobby authentication.
|
||||
*/
|
||||
void SetHostJID(const CStr& jid);
|
||||
|
||||
void SetControllerSecret(const std::string& secret);
|
||||
|
||||
bool IsController() const { return m_IsController; }
|
||||
|
||||
@@ -105,13 +105,11 @@ void StartNetworkHost(const CStrW& playerName, const u16 serverPort, const CStr&
|
||||
g_NetServer->SetControllerSecret(secret);
|
||||
|
||||
g_Game = new CGame(storeReplay);
|
||||
g_NetClient = new CNetClient(g_Game);
|
||||
g_NetClient->SetUserName(playerName);
|
||||
const std::string hostJID{hasLobby ? g_XmppClient->GetJID() : ""};
|
||||
g_NetClient = new CNetClient(g_Game, playerName, hostJID);
|
||||
|
||||
if (hasLobby)
|
||||
{
|
||||
CStr hostJID = g_XmppClient->GetJID();
|
||||
|
||||
/**
|
||||
* Password security - we want 0 A.D. to protect players from malicious hosts. We assume that clients
|
||||
* might mistakenly send a personal password instead of the game password (e.g. enter their mail account's password on autopilot).
|
||||
@@ -130,7 +128,6 @@ void StartNetworkHost(const CStrW& playerName, const u16 serverPort, const CStr&
|
||||
*/
|
||||
CStr hashedPass = HashCryptographically(password, hostJID + password + PS_SERIALIZATION_VERSION);
|
||||
g_NetServer->SetPassword(hashedPass);
|
||||
g_NetClient->SetHostJID(hostJID);
|
||||
g_NetClient->SetGamePassword(hashedPass);
|
||||
}
|
||||
|
||||
@@ -152,8 +149,7 @@ void StartNetworkJoin(const CStrW& playerName, const CStr& serverAddress, u16 se
|
||||
ENSURE(!g_Game);
|
||||
|
||||
g_Game = new CGame(storeReplay);
|
||||
g_NetClient = new CNetClient(g_Game);
|
||||
g_NetClient->SetUserName(playerName);
|
||||
g_NetClient = new CNetClient(g_Game, playerName);
|
||||
g_NetClient->SetupServerData(serverAddress, serverPort);
|
||||
|
||||
if (!g_NetClient->SetupConnection(nullptr))
|
||||
@@ -178,9 +174,7 @@ void StartNetworkJoinLobby(const CStrW& playerName, const CStr& hostJID, const C
|
||||
|
||||
CStr hashedPass = HashCryptographically(password, hostJID + password + PS_SERIALIZATION_VERSION);
|
||||
g_Game = new CGame(true);
|
||||
g_NetClient = new CNetClient(g_Game);
|
||||
g_NetClient->SetUserName(playerName);
|
||||
g_NetClient->SetHostJID(hostJID);
|
||||
g_NetClient = new CNetClient(g_Game, playerName, hostJID);
|
||||
g_NetClient->SetGamePassword(hashedPass);
|
||||
g_NetClient->SetupConnectionViaLobby();
|
||||
}
|
||||
|
||||
@@ -252,13 +252,9 @@ public:
|
||||
|
||||
server.UpdateInitAttributes(&attrs, scriptInterface);
|
||||
|
||||
CNetClient client1(&client1Game);
|
||||
CNetClient client2(&client2Game);
|
||||
CNetClient client3(&client3Game);
|
||||
|
||||
client1.SetUserName(L"alice");
|
||||
client2.SetUserName(L"bob");
|
||||
client3.SetUserName(L"charlie");
|
||||
CNetClient client1(&client1Game, L"alice");
|
||||
CNetClient client2(&client2Game, L"bob");
|
||||
CNetClient client3(&client3Game, L"charlie");
|
||||
|
||||
clients.push_back(&client1);
|
||||
clients.push_back(&client2);
|
||||
@@ -313,8 +309,7 @@ public:
|
||||
debug_printf("==== Connecting client 2B\n");
|
||||
|
||||
CGame client2BGame(false);
|
||||
CNetClient client2B(&client2BGame);
|
||||
client2B.SetUserName(L"bob");
|
||||
CNetClient client2B(&client2BGame, L"bob");
|
||||
clients.push_back(&client2B);
|
||||
|
||||
client2B.SetupServerData("127.0.0.1", PS_DEFAULT_PORT);
|
||||
|
||||
Reference in New Issue
Block a user