当前位置: 首页>>代码示例>>C++>>正文


C++ CNetServerSession::SetUserName方法代码示例

本文整理汇总了C++中CNetServerSession::SetUserName方法的典型用法代码示例。如果您正苦于以下问题:C++ CNetServerSession::SetUserName方法的具体用法?C++ CNetServerSession::SetUserName怎么用?C++ CNetServerSession::SetUserName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CNetServerSession的用法示例。


在下文中一共展示了CNetServerSession::SetUserName方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: OnAuthenticate

bool CNetServerWorker::OnAuthenticate(void* context, CFsmEvent* event)
{
	ENSURE(event->GetType() == (uint)NMT_AUTHENTICATE);

	CNetServerSession* session = (CNetServerSession*)context;
	CNetServerWorker& server = session->GetServer();

	// Prohibit joins while the game is loading
	if (server.m_State == SERVER_STATE_LOADING)
	{
		LOGMESSAGE("Refused connection while the game is loading");
		session->Disconnect(NDR_SERVER_LOADING);
		return true;
	}

	CAuthenticateMessage* message = (CAuthenticateMessage*)event->GetParamRef();
	CStrW username = server.DeduplicatePlayerName(SanitisePlayerName(message->m_Name));

	// Optionally allow observers to join after the game has started
	bool observerLateJoin = false;
	ScriptInterface& scriptInterface = server.GetScriptInterface();
	JSContext* cx = scriptInterface.GetContext();
	JSAutoRequest rq(cx);
	JS::RootedValue settings(cx);
	scriptInterface.GetProperty(server.m_GameAttributes.get(), "settings", &settings);
	if (scriptInterface.HasProperty(settings, "ObserverLateJoin"))
		scriptInterface.GetProperty(settings, "ObserverLateJoin", observerLateJoin);

	// If the game has already started, only allow rejoins
	bool isRejoining = false;
	if (server.m_State != SERVER_STATE_PREGAME)
	{
		// Search for an old disconnected player of the same name
		// (TODO: if GUIDs were stable, we should use them instead)
		isRejoining =
			observerLateJoin ||
			std::find_if(
				server.m_PlayerAssignments.begin(), server.m_PlayerAssignments.end(),
				[&username] (const std::pair<CStr, PlayerAssignment>& pair)
				{ return !pair.second.m_Enabled && pair.second.m_Name == username; })
			!= server.m_PlayerAssignments.end();

		// Players who weren't already in the game are not allowed to join now that it's started
		if (!isRejoining)
		{
			LOGMESSAGE("Refused connection after game start from not-previously-known user \"%s\"", utf8_from_wstring(username));
			session->Disconnect(NDR_SERVER_ALREADY_IN_GAME);
			return true;
		}
	}

	// TODO: check server password etc?

	u32 newHostID = server.m_NextHostID++;

	session->SetUserName(username);
	session->SetGUID(message->m_GUID);
	session->SetHostID(newHostID);

	CAuthenticateResultMessage authenticateResult;
	authenticateResult.m_Code = isRejoining ? ARC_OK_REJOINING : ARC_OK;
	authenticateResult.m_HostID = newHostID;
	authenticateResult.m_Message = L"Logged in";
	session->SendMessage(&authenticateResult);

	server.OnUserJoin(session);

	if (isRejoining)
	{
		// Request a copy of the current game state from an existing player,
		// so we can send it on to the new player

		// Assume session 0 is most likely the local player, so they're
		// the most efficient client to request a copy from
		CNetServerSession* sourceSession = server.m_Sessions.at(0);
		sourceSession->GetFileTransferer().StartTask(
			shared_ptr<CNetFileReceiveTask>(new CNetFileReceiveTask_ServerRejoin(server, newHostID))
		);

		session->SetNextState(NSS_JOIN_SYNCING);
	}

	return true;
}
开发者ID:UnitecProgra3,项目名称:0ad,代码行数:84,代码来源:NetServer.cpp


注:本文中的CNetServerSession::SetUserName方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。