Esempio n. 1
0
void SessionServer::OnListSessionsRequest(const ListSessionsRequest&, const NetworkConnectionPtr& connection)
{
	// Fill in a message with a list of all the sessions and send it

	ListSessionsReply reply;

	reply.SetSessionCount(static_cast<int32>(m_sessions.size()));

	for (size_t i = 0; i < m_sessions.size(); ++i)
	{
		XSessionImplPtr currentSession = m_sessions[i];

		reply.SetSessionDescriptor((int32)i, currentSession->GetSessionDescription(connection->GetSocket()));
	}

	NetworkOutMessagePtr msg = connection->CreateMessage(MessageID::SessionControl);
	msg->Write(reply.ToJSONString());

	connection->Send(msg);
}
Esempio n. 2
0
void SessionServer::OnNewSessionRequest(const NewSessionRequest& request, const NetworkConnectionPtr& connection)
{
	std::string name = request.GetSessionName();

	XSessionImplPtr session;
	std::string failureReason;

	// Cannot create a session with a name that is too short
	if (name.length() < kMinSessionNameLength)
	{
		failureReason = "Session name must have at least " + std::to_string(kMinSessionNameLength) + " letters";
	}

	// Cannot create a session with a name that is too long
	else if (name.length() > kMaxSessionNameLength)
	{
		failureReason = "Session name cannot be more than " + std::to_string(kMaxSessionNameLength) + " letters";
	}
	else
	{
		// Check to make sure that the requested session name is not already taken
		for (size_t i = 0; i < m_sessions.size(); ++i)
		{
			if (m_sessions[i]->GetName() == name)
			{
				failureReason = "A session with that name already exists";
				break;
			}
		}
	}

	if (failureReason.empty())
	{
		session = CreateNewSession(name, request.GetSessionType());
	}
	
	// If the session was successfully created...
	if (session)
	{
		// Report success.
		std::string address = m_socketMgr->GetLocalAddressForRemoteClient(connection->GetSocket());
		uint16 port = session->GetPort();

		NewSessionReply reply(
			session->GetId(), 
			session->GetType(),
			name, 
			address, 
			port);

		NetworkOutMessagePtr response = connection->CreateMessage(MessageID::SessionControl);

		response->Write(reply.ToJSONString());

		connection->Send(response);
	}
	else
	{
		// Report failure
		NewSessionReply reply(failureReason);

		NetworkOutMessagePtr response = connection->CreateMessage(MessageID::SessionControl);

		response->Write(reply.ToJSONString());

		connection->Send(response);
	}
}