!I 1937152 from //dev_game_hunt/consoles_candidate: !B Remove allocations of unnecess...
[CRYENGINE.git] / Code / CryPlugins / CryLobby / Module / DurangoLive / CryDurangoLiveLobby.cpp
blobcf865e63fba82032e5d0713a120cd3a54c5e72ab
1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 #include "StdAfx.h"
4 #include "CryDurangoLiveLobby.h"
5 #include <CryCore/TypeInfo_impl.h>
7 #if USE_DURANGOLIVE
9 using namespace ABI::Windows::Foundation;
10 using namespace ABI::Windows::Storage::Streams;
11 using namespace Microsoft::WRL;
12 using namespace ABI::Windows::Xbox::Networking;
13 using namespace ABI::Windows::Xbox::System;
15 #define DURANGO_DEFAULT_PORT 49150
17 #define TDN_GET_USER_PRIVILEGES_RESULT 0
19 ECryLobbyError CryDurangoLiveLobbyGetErrorFromDurangoLive(HRESULT error)
21 NetLog("[Lobby] eCLE_InternalError: Durango Live Error %d %x", error, error);
22 return eCLE_InternalError;
25 CCryDurangoLiveLobbyService::CCryDurangoLiveLobbyService(CCryLobby* pLobby, ECryLobbyService service)
26 : CCryLobbyService(pLobby, service)
27 , m_titleID(0)
31 CCryDurangoLiveLobbyService::~CCryDurangoLiveLobbyService()
35 ECryLobbyError CCryDurangoLiveLobbyService::Initialise(ECryLobbyServiceFeatures features, CryLobbyServiceCallback pCB)
37 ECryLobbyError ret = CCryLobbyService::Initialise(features, pCB);
39 if ((ret == eCLE_Success) && (m_pTCPServiceFactory == NULL) && (features & eCLSO_TCPService))
41 m_pTCPServiceFactory = new CCryTCPServiceFactory();
43 if (m_pTCPServiceFactory)
45 ret = m_pTCPServiceFactory->Initialise("Scripts/Network/TCPServices.xml", CCryTCPServiceFactory::k_defaultMaxSockets);
47 else
49 return eCLE_OutOfMemory;
53 if (features & eCLSO_Base)
55 SConfigurationParams neededInfo[2] = {
56 { CLCC_LIVE_TITLE_ID, { NULL }
57 },{ CLCC_LIVE_SERVICE_CONFIG_ID, { NULL }
61 m_pLobby->GetConfigurationInformation(neededInfo, CRY_ARRAY_COUNT(neededInfo));
63 m_titleID = neededInfo[0].m_32;
64 m_serviceConfigId.Set(reinterpret_cast<LPCWSTR>(neededInfo[1].m_pData));
67 #if USE_CRY_MATCHMAKING
68 if ((ret == eCLE_Success) && (m_pMatchmaking == NULL) && (features & eCLSO_Matchmaking))
70 m_pMatchmaking = new CCryDurangoLiveMatchMaking(m_pLobby, this, m_service);
72 if (m_pMatchmaking)
74 ret = m_pMatchmaking->Initialise();
76 else
78 ret = eCLE_OutOfMemory;
81 #endif // USE_CRY_MATCHMAKING
83 #if USE_CRY_ONLINE_STORAGE
84 if ((ret == eCLE_Success) && (m_pOnlineStorage == NULL) && (features & eCLSO_OnlineStorage))
86 m_pOnlineStorage = new CCryDurangoLiveOnlineStorage(m_pLobby, this);
87 if (m_pOnlineStorage)
89 ret = m_pOnlineStorage->Initialise();
91 else
93 ret = eCLE_OutOfMemory;
96 #endif // USE_CRY_ONLINE_STORAGE
98 if ((ret == eCLE_Success) && (m_pLobbyUI == NULL) && (features & eCLSO_LobbyUI))
100 m_pLobbyUI = new CCryDurangoLiveLobbyUI(m_pLobby, this);
102 if (m_pLobbyUI)
104 ret = m_pLobbyUI->Initialise();
106 else
108 ret = eCLE_OutOfMemory;
112 if (ret == eCLE_Success)
114 if (features & eCLSO_Base)
116 m_pLobby->SetServicePacketEnd(m_service, eDurangoLivePT_EndType);
119 else
121 Terminate(features, NULL);
124 if (pCB)
126 pCB(ret, m_pLobby, m_service);
129 NetLog("[Lobby] Durango Live Initialise error %d", ret);
131 return ret;
134 ECryLobbyError CCryDurangoLiveLobbyService::Terminate(ECryLobbyServiceFeatures features, CryLobbyServiceCallback pCB)
136 ECryLobbyError ret = CCryLobbyService::Terminate(features, pCB);
138 if (m_pTCPServiceFactory && (features & eCLSO_TCPService))
140 ECryLobbyError error = m_pTCPServiceFactory->Terminate(false);
141 m_pTCPServiceFactory = NULL;
143 if (ret == eCLE_Success)
145 ret = error;
149 #if USE_CRY_MATCHMAKING
150 if (m_pMatchmaking && (features & eCLSO_Matchmaking))
152 ECryLobbyError error = m_pMatchmaking->Terminate();
153 m_pMatchmaking = NULL;
155 if (ret == eCLE_Success)
157 ret = error;
160 #endif // USE_CRY_MATCHMAKING
162 #if USE_CRY_ONLINE_STORAGE
163 if (m_pOnlineStorage && (features & eCLSO_OnlineStorage))
165 ECryLobbyError error = m_pOnlineStorage->Terminate();
166 m_pOnlineStorage = NULL;
168 if (ret == eCLE_Success)
170 ret = error;
173 #endif // USE_CRY_ONLINE_STORAGE
175 if (m_pLobbyUI && (features & eCLSO_LobbyUI))
177 ECryLobbyError error = m_pLobbyUI->Terminate();
178 m_pLobbyUI = NULL;
180 if (ret == eCLE_Success)
182 ret = error;
186 if (pCB)
188 pCB(ret, m_pLobby, m_service);
191 return ret;
194 void CCryDurangoLiveLobbyService::Tick(CTimeValue tv)
196 if (m_pLobby->MutexTryLock())
198 #if USE_CRY_MATCHMAKING
199 if (m_pMatchmaking)
201 m_pMatchmaking->Tick(tv);
203 #endif // USE_CRY_MATCHMAKING
205 #if USE_CRY_ONLINE_STORAGE
206 if (m_pOnlineStorage)
208 m_pOnlineStorage->Tick(tv);
210 #endif // USE_CRY_ONLINE_STORAGE
212 if (m_pLobbyUI)
214 m_pLobbyUI->Tick(tv);
217 m_pLobby->MutexUnlock();
220 if (m_pTCPServiceFactory)
222 m_pTCPServiceFactory->Tick(tv);
226 void CCryDurangoLiveLobbyService::OnPacket(const TNetAddress& addr, CCryLobbyPacket* pPacket)
228 #if USE_CRY_MATCHMAKING
229 if (m_pMatchmaking)
231 m_pMatchmaking->OnPacket(addr, pPacket);
233 #endif // USE_CRY_MATCHMAKING
236 void CCryDurangoLiveLobbyService::OnError(const TNetAddress& addr, ESocketError error, CryLobbySendID sendID)
238 #if USE_CRY_MATCHMAKING
239 if (m_pMatchmaking)
241 m_pMatchmaking->OnError(addr, error, sendID);
243 #endif // USE_CRY_MATCHMAKING
246 void CCryDurangoLiveLobbyService::OnSendComplete(const TNetAddress& addr, CryLobbySendID sendID)
248 #if USE_CRY_MATCHMAKING
249 if (m_pMatchmaking)
251 m_pMatchmaking->OnSendComplete(addr, sendID);
253 #endif // USE_CRY_MATCHMAKING
256 CryUserID CCryDurangoLiveLobbyService::GetUserID(uint32 user)
258 return CryUserInvalidID;
261 void CCryDurangoLiveLobbyService::StartTaskRunning(CryLobbyServiceTaskID lsTaskID)
263 LOBBY_AUTO_LOCK;
265 STask* pTask = GetTask(lsTaskID);
267 if (pTask->used)
269 pTask->running = true;
271 switch (pTask->startedTask)
273 case eT_GetUserPrivileges:
274 StartGetUserPrivileges(lsTaskID);
275 break;
280 void CCryDurangoLiveLobbyService::StopTaskRunning(CryLobbyServiceTaskID lsTaskID)
282 STask* pTask = GetTask(lsTaskID);
284 if (pTask->used)
286 pTask->running = false;
288 TO_GAME_FROM_LOBBY(&CCryDurangoLiveLobbyService::EndTask, this, lsTaskID);
292 void CCryDurangoLiveLobbyService::EndTask(CryLobbyServiceTaskID lsTaskID)
294 LOBBY_AUTO_LOCK;
296 STask* pTask = GetTask(lsTaskID);
298 if (pTask->used)
300 if (pTask->pCB)
302 switch (pTask->startedTask)
304 case eT_GetUserPrivileges:
305 ((CryLobbyPrivilegeCallback)pTask->pCB)(pTask->lTaskID, pTask->error, pTask->dataNum[TDN_GET_USER_PRIVILEGES_RESULT], pTask->pCBArg);
306 break;
310 if (pTask->error != eCLE_Success)
312 NetLog("[Lobby] Lobby Service EndTask %d Result %d", pTask->startedTask, pTask->error);
315 FreeTask(lsTaskID);
319 ECryLobbyError CCryDurangoLiveLobbyService::GetUserPrivileges(uint32 user, CryLobbyTaskID* pTaskID, CryLobbyPrivilegeCallback pCB, void* pCBArg)
321 ECryLobbyError error;
322 CryLobbyServiceTaskID tid;
324 LOBBY_AUTO_LOCK;
326 error = StartTask(eT_GetUserPrivileges, user, false, &tid, pTaskID, (void*)pCB, pCBArg);
328 if (error == eCLE_Success)
330 FROM_GAME_TO_LOBBY(&CCryDurangoLiveLobbyService::StartTaskRunning, this, tid);
333 return error;
336 void CCryDurangoLiveLobbyService::StartGetUserPrivileges(CryLobbyServiceTaskID lsTaskID)
338 STask* pTask = GetTask(lsTaskID);
339 uint32 result = 0;
341 if (m_pLobby->GetInternalSocket(m_service))
344 else
346 UpdateTaskError(lsTaskID, eCLE_InternalError);
349 pTask->dataNum[TDN_GET_USER_PRIVILEGES_RESULT] = result;
351 StopTaskRunning(lsTaskID);
354 ECryLobbyError CCryDurangoLiveLobbyService::GetSystemTime(uint32 user, SCrySystemTime* pSystemTime)
356 memset(pSystemTime, 0, sizeof(SCrySystemTime));
358 ECryLobbyError error = eCLE_Success;
359 SYSTEMTIME systemTime;
360 ::GetSystemTime(&systemTime);
362 pSystemTime->m_Year = systemTime.wYear;
363 pSystemTime->m_Month = static_cast<uint8>(systemTime.wMonth);
364 pSystemTime->m_Day = static_cast<uint8>(systemTime.wDay);
365 pSystemTime->m_Hour = static_cast<uint8>(systemTime.wHour);
366 pSystemTime->m_Minute = static_cast<uint8>(systemTime.wMinute);
367 pSystemTime->m_Second = static_cast<uint8>(systemTime.wSecond);
369 return error;
372 void CCryDurangoLiveLobbyService::GetSocketPorts(uint16& connectPort, uint16& listenPort)
374 connectPort = DURANGO_DEFAULT_PORT;
375 listenPort = DURANGO_DEFAULT_PORT;
378 #endif // USE_DURANGOLIVE