Documentation for the Unreal C++ Library
Loading...
Searching...
No Matches
PVROnlineSession.h
Go to the documentation of this file.
1// Copyright(c) 2023 PixoVR, LLC. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#if ENGINE_MAJOR_VERSION >= 5
7#include "Online/CoreOnline.h"
8#else
9#include "UObject/CoreOnline.h"
10#endif
11#include "Misc/ScopeLock.h"
12#include "OnlineSessionSettings.h"
13#include "OnlineDelegateMacros.h"
14#include "Interfaces/OnlineSessionInterface.h"
15#include "PVROBeacon.h"
16#include "PVROFindServerTask.h"
18#include "PVROPackage.h"
19
20class FPVROSubsystem;
21
22DECLARE_MULTICAST_DELEGATE(FOnFindSessionsStart);
23typedef FOnFindSessionsStart::FDelegate FOnFindSessionsStartDelegate;
24
25DECLARE_MULTICAST_DELEGATE(FOnJoinSessionStart);
26typedef FOnJoinSessionStart::FDelegate FOnJoinSessionStartDelegate;
27
34{
35private:
36
39
42
45 Subsystem(NULL),
47 {}
48
54 void TickLanTasks(float DeltaTime);
55
61 bool NeedsToAdvertise();
62
68 bool NeedsToAdvertise( FNamedOnlineSession& Session );
69
75 bool IsSessionJoinable( const FNamedOnlineSession& Session) const;
76
82 uint32 UpdateLANStatus();
83
93 uint32 JoinInternetSession(int32 PlayerNum, class FNamedOnlineSession* Session, const class FOnlineSession* SearchSession);
94
100 uint32 FindInternetSession(const TSharedRef<FOnlineSessionSearch>& SearchSettings);
101
107 uint32 FindLANSession(const TSharedRef<FOnlineSessionSearch>& SearchSettings);
108
114 uint32 FinalizeLANSearch();
115
123 void OnValidQueryPacketReceived(uint8* PacketData, int32 PacketLength, uint64 ClientNonce);
124
131 void OnValidResponsePacketReceived(uint8* PacketData, int32 PacketLength);
132
136 void OnLANSearchTimeout();
137
141 static void SetPortFromNetDriver(const FPVROSubsystem& Subsystem, const TSharedPtr<FOnlineSessionInfo>& SessionInfo);
142
146 bool IsHost(const FNamedOnlineSession& Session) const;
147
149
150 TSharedPtr<FUniqueNetIdPixoVR> GameServerPixoVRId;
151
152 mutable FCriticalSection SessionLock;
153
154 TArray<FNamedOnlineSession> Sessions;
155
156 TSharedPtr<FOnlineSessionSearch> CurrentSessionSearch;
157
161
162 FPVROnlineSession(class FPVROSubsystem* InSubsystem);
163
167 void Tick(float DeltaTime);
168
169 void HandleMessageReceived(const FString& InJsonMessage);
170
171 void CheckMatchesFound(TSharedPtr<FJsonObject>& JsonMessageObject);
172
173 void GetHostAndPort(int32& Host, int32& Port, const FString& HostIP, const FString& HostPort);
174
175 // IOnlineSession
176 class FNamedOnlineSession* AddNamedSession(FName SessionName, const FOnlineSessionSettings& SessionSettings) override
177 {
178 FScopeLock ScopeLock(&SessionLock);
179 return new (Sessions) FNamedOnlineSession(SessionName, SessionSettings);
180 }
181
182 class FNamedOnlineSession* AddNamedSession(FName SessionName, const FOnlineSession& Session) override
183 {
184 FScopeLock ScopeLock(&SessionLock);
185 return new (Sessions) FNamedOnlineSession(SessionName, Session);
186 }
187
192
198 void RegisterVoice(const FUniqueNetId& PlayerId);
199
205 void UnregisterVoice(const FUniqueNetId& PlayerId);
206
212 void RegisterLocalPlayers(class FNamedOnlineSession* Session);
213
219 void UnregisterLocalPlayers(class FNamedOnlineSession* Session);
220
221
226 FString GetCustomDedicatedServerName() const;
227
228 bool ClearSessionSearch(EOnlineAsyncTaskState::Type TaskState = EOnlineAsyncTaskState::Done);
229
230public:
231
233
234 virtual TSharedPtr<const FUniqueNetId> CreateSessionIdFromString(const FString& SessionIdStr) override;
235
237 void OnSessionListenPortChanged(int32 Port);
238
239 FNamedOnlineSession* GetFirstNamedSession()
240 {
241 FScopeLock ScopeLock(&SessionLock);
242 if (Sessions.Num() > 0)
243 {
244 return &Sessions[0];
245 }
246 return NULL;
247 }
248
249 FNamedOnlineSession* GetNamedSession(FName SessionName) override
250 {
251 FScopeLock ScopeLock(&SessionLock);
252 for (int32 SearchIndex = 0; SearchIndex < Sessions.Num(); SearchIndex++)
253 {
254 if (Sessions[SearchIndex].SessionName == SessionName)
255 {
256 return &Sessions[SearchIndex];
257 }
258 }
259 return NULL;
260 }
261
262 virtual void RemoveNamedSession(FName SessionName) override
263 {
264 FScopeLock ScopeLock(&SessionLock);
265 for (int32 SearchIndex = 0; SearchIndex < Sessions.Num(); SearchIndex++)
266 {
267 if (Sessions[SearchIndex].SessionName == SessionName)
268 {
269 Sessions.RemoveAtSwap(SearchIndex);
270 return;
271 }
272 }
273 }
274
275 virtual EOnlineSessionState::Type GetSessionState(FName SessionName) const override
276 {
277 FScopeLock ScopeLock(&SessionLock);
278 for (int32 SearchIndex = 0; SearchIndex < Sessions.Num(); SearchIndex++)
279 {
280 if (Sessions[SearchIndex].SessionName == SessionName)
281 {
282 return Sessions[SearchIndex].SessionState;
283 }
284 }
285
286 return EOnlineSessionState::NoSession;
287 }
288
289 virtual bool HasPresenceSession() override
290 {
291 FScopeLock ScopeLock(&SessionLock);
292 for (int32 SearchIndex = 0; SearchIndex < Sessions.Num(); SearchIndex++)
293 {
294 if (Sessions[SearchIndex].SessionSettings.bUsesPresence)
295 {
296 return true;
297 }
298 }
299
300 return false;
301 }
302
303 uint32 CreateInternetSession(int32 HostingPlayerNum, FNamedOnlineSession* Session);
304 uint32 CreateLANSession(int32 HostingPlayerNum, FNamedOnlineSession* Session);
305 uint32 DestroyInternetSession(FNamedOnlineSession* Session, const FOnDestroySessionCompleteDelegate& CompletionDelegate = FOnDestroySessionCompleteDelegate());
306 uint32 DestroyLANSession(FName SessionName, const FOnDestroySessionCompleteDelegate& CompletionDelegate = FOnDestroySessionCompleteDelegate());
307 uint32 EndInternetSession(FNamedOnlineSession* Session);
308
309 // IOnlineSession
310 virtual bool CreateSession(int32 HostingPlayerNum, FName SessionName, const FOnlineSessionSettings& NewSessionSettings) override;
311 virtual bool CreateSession(const FUniqueNetId& HostingPlayerId, FName SessionName, const FOnlineSessionSettings& NewSessionSettings) override;
312 virtual bool StartSession(FName SessionName) override;
313 virtual bool UpdateSession(FName SessionName, FOnlineSessionSettings& UpdatedSessionSettings, bool bShouldRefreshOnlineData = true) override;
314 virtual bool EndSession(FName SessionName) override;
315 virtual bool DestroySession(FName SessionName, const FOnDestroySessionCompleteDelegate& CompletionDelegate = FOnDestroySessionCompleteDelegate()) override;
316 virtual bool IsPlayerInSession(FName SessionName, const FUniqueNetId& UniqueId) override;
317 virtual bool StartMatchmaking(const TArray< TSharedRef<const FUniqueNetId> >& LocalPlayers, FName SessionName, const FOnlineSessionSettings& NewSessionSettings, TSharedRef<FOnlineSessionSearch>& SearchSettings) override;
318 virtual bool CancelMatchmaking(int32 SearchingPlayerNum, FName SessionName) override;
319 virtual bool CancelMatchmaking(const FUniqueNetId& SearchingPlayerId, FName SessionName) override;
320 virtual bool FindSessions(int32 SearchingPlayerNum, const TSharedRef<FOnlineSessionSearch>& SearchSettings) override;
321 virtual bool FindSessions(const FUniqueNetId& SearchingPlayerId, const TSharedRef<FOnlineSessionSearch>& SearchSettings) override;
322 virtual bool FindSessionById(const FUniqueNetId& SearchingUserId, const FUniqueNetId& SessionId, const FUniqueNetId& FriendId, const FOnSingleSessionResultCompleteDelegate& CompletionDelegate) override;
323 virtual bool CancelFindSessions() override;
324 virtual bool PingSearchResults(const FOnlineSessionSearchResult& SearchResult) override;
325 virtual bool JoinSession(int32 PlayerNum, FName SessionName, const FOnlineSessionSearchResult& DesiredSession) override;
326 virtual bool JoinSession(const FUniqueNetId& PlayerId, FName SessionName, const FOnlineSessionSearchResult& DesiredSession) override;
327 virtual bool FindFriendSession(int32 LocalUserNum, const FUniqueNetId& Friend) override;
328 virtual bool FindFriendSession(const FUniqueNetId& LocalUserId, const FUniqueNetId& Friend) override;
329 virtual bool FindFriendSession(const FUniqueNetId& LocalUserId, const TArray<TSharedRef<const FUniqueNetId>>& FriendList) override;
330 virtual bool SendSessionInviteToFriend(int32 LocalUserNum, FName SessionName, const FUniqueNetId& Friend) override;
331 virtual bool SendSessionInviteToFriend(const FUniqueNetId& LocalUserId, FName SessionName, const FUniqueNetId& Friend) override;
332 virtual bool SendSessionInviteToFriends(int32 LocalUserNum, FName SessionName, const TArray< TSharedRef<const FUniqueNetId> >& Friends) override;
333 virtual bool SendSessionInviteToFriends(const FUniqueNetId& LocalUserId, FName SessionName, const TArray< TSharedRef<const FUniqueNetId> >& Friends) override;
334 virtual bool GetResolvedConnectString(FName SessionName, FString& ConnectInfo, FName PortType) override;
335 virtual bool GetResolvedConnectString(const FOnlineSessionSearchResult& SearchResult, FName PortType, FString& ConnectInfo) override;
336 virtual FOnlineSessionSettings* GetSessionSettings(FName SessionName) override;
337 virtual bool RegisterPlayer(FName SessionName, const FUniqueNetId& PlayerId, bool bWasInvited) override;
338 virtual bool RegisterPlayers(FName SessionName, const TArray< TSharedRef<const FUniqueNetId> >& Players, bool bWasInvited = false) override;
339 virtual bool UnregisterPlayer(FName SessionName, const FUniqueNetId& PlayerId) override;
340 virtual bool UnregisterPlayers(FName SessionName, const TArray< TSharedRef<const FUniqueNetId> >& Players) override;
341 virtual void RegisterLocalPlayer(const FUniqueNetId& PlayerId, FName SessionName, const FOnRegisterLocalPlayerCompleteDelegate& Delegate) override;
342 virtual void UnregisterLocalPlayer(const FUniqueNetId& PlayerId, FName SessionName, const FOnUnregisterLocalPlayerCompleteDelegate& Delegate) override;
343 virtual int32 GetNumSessions() override;
344 virtual void DumpSessionState() override;
345
346public:
347
348 DEFINE_ONLINE_DELEGATE(OnFindSessionsStart);
349 DEFINE_ONLINE_DELEGATE(OnJoinSessionStart);
350};
351
352typedef TSharedPtr<FPVROnlineSession, ESPMode::ThreadSafe> FPVROnlineSessionPtr;
#define PACKAGE_SCOPE
Definition PVROPackage.h:12
FOnFindSessionsStart::FDelegate FOnFindSessionsStartDelegate
TSharedPtr< FPVROnlineSession, ESPMode::ThreadSafe > FPVROnlineSessionPtr
FOnJoinSessionStart::FDelegate FOnJoinSessionStartDelegate
DECLARE_MULTICAST_DELEGATE(FOnFindSessionsStart)
virtual bool HasPresenceSession() override
virtual bool PingSearchResults(const FOnlineSessionSearchResult &SearchResult) override
FString GetCustomDedicatedServerName() const
Returns the custom server name if specified as argument.
uint32 CreateInternetSession(int32 HostingPlayerNum, FNamedOnlineSession *Session)
uint32 CreateLANSession(int32 HostingPlayerNum, FNamedOnlineSession *Session)
FNamedOnlineSession * GetFirstNamedSession()
class FNamedOnlineSession * AddNamedSession(FName SessionName, const FOnlineSession &Session) override
virtual bool UnregisterPlayers(FName SessionName, const TArray< TSharedRef< const FUniqueNetId > > &Players) override
virtual bool EndSession(FName SessionName) override
virtual bool JoinSession(int32 PlayerNum, FName SessionName, const FOnlineSessionSearchResult &DesiredSession) override
virtual bool RegisterPlayers(FName SessionName, const TArray< TSharedRef< const FUniqueNetId > > &Players, bool bWasInvited=false) override
void GetHostAndPort(int32 &Host, int32 &Port, const FString &HostIP, const FString &HostPort)
void CheckMatchesFound(TSharedPtr< FJsonObject > &JsonMessageObject)
void OnSessionListenPortChanged(int32 Port)
void Tick(float DeltaTime)
void OnValidQueryPacketReceived(uint8 *PacketData, int32 PacketLength, uint64 ClientNonce)
DEFINE_ONLINE_DELEGATE(OnJoinSessionStart)
uint32 DestroyLANSession(FName SessionName, const FOnDestroySessionCompleteDelegate &CompletionDelegate=FOnDestroySessionCompleteDelegate())
FNamedOnlineSession * GetNamedSession(FName SessionName) override
void HandleMessageReceived(const FString &InJsonMessage)
DEFINE_ONLINE_DELEGATE(OnFindSessionsStart)
virtual bool FindSessions(int32 SearchingPlayerNum, const TSharedRef< FOnlineSessionSearch > &SearchSettings) override
virtual bool CancelFindSessions() override
TSharedPtr< FOnlineSessionSearch > CurrentSessionSearch
virtual TSharedPtr< const FUniqueNetId > CreateSessionIdFromString(const FString &SessionIdStr) override
bool ClearSessionSearch(EOnlineAsyncTaskState::Type TaskState=EOnlineAsyncTaskState::Done)
void CheckPendingSessionInvite()
virtual bool IsPlayerInSession(FName SessionName, const FUniqueNetId &UniqueId) override
TSharedPtr< FUniqueNetIdPixoVR > GameServerPixoVRId
uint32 FindInternetSession(const TSharedRef< FOnlineSessionSearch > &SearchSettings)
static void SetPortFromNetDriver(const FPVROSubsystem &Subsystem, const TSharedPtr< FOnlineSessionInfo > &SessionInfo)
uint32 EndInternetSession(FNamedOnlineSession *Session)
uint32 JoinInternetSession(int32 PlayerNum, class FNamedOnlineSession *Session, const class FOnlineSession *SearchSession)
virtual bool FindFriendSession(int32 LocalUserNum, const FUniqueNetId &Friend) override
void OnValidResponsePacketReceived(uint8 *PacketData, int32 PacketLength)
virtual bool StartSession(FName SessionName) override
class FNamedOnlineSession * AddNamedSession(FName SessionName, const FOnlineSessionSettings &SessionSettings) override
virtual bool DestroySession(FName SessionName, const FOnDestroySessionCompleteDelegate &CompletionDelegate=FOnDestroySessionCompleteDelegate()) override
virtual bool RegisterPlayer(FName SessionName, const FUniqueNetId &PlayerId, bool bWasInvited) override
virtual void RegisterLocalPlayer(const FUniqueNetId &PlayerId, FName SessionName, const FOnRegisterLocalPlayerCompleteDelegate &Delegate) override
virtual void RemoveNamedSession(FName SessionName) override
virtual FOnlineSessionSettings * GetSessionSettings(FName SessionName) override
void RegisterVoice(const FUniqueNetId &PlayerId)
FCriticalSection SessionLock
virtual bool GetResolvedConnectString(FName SessionName, FString &ConnectInfo, FName PortType) override
void UnregisterVoice(const FUniqueNetId &PlayerId)
virtual bool CreateSession(int32 HostingPlayerNum, FName SessionName, const FOnlineSessionSettings &NewSessionSettings) override
void UnregisterLocalPlayers(class FNamedOnlineSession *Session)
virtual bool FindSessionById(const FUniqueNetId &SearchingUserId, const FUniqueNetId &SessionId, const FUniqueNetId &FriendId, const FOnSingleSessionResultCompleteDelegate &CompletionDelegate) override
uint32 DestroyInternetSession(FNamedOnlineSession *Session, const FOnDestroySessionCompleteDelegate &CompletionDelegate=FOnDestroySessionCompleteDelegate())
bool IsHost(const FNamedOnlineSession &Session) const
virtual int32 GetNumSessions() override
virtual void UnregisterLocalPlayer(const FUniqueNetId &PlayerId, FName SessionName, const FOnUnregisterLocalPlayerCompleteDelegate &Delegate) override
virtual bool SendSessionInviteToFriend(int32 LocalUserNum, FName SessionName, const FUniqueNetId &Friend) override
virtual bool StartMatchmaking(const TArray< TSharedRef< const FUniqueNetId > > &LocalPlayers, FName SessionName, const FOnlineSessionSettings &NewSessionSettings, TSharedRef< FOnlineSessionSearch > &SearchSettings) override
TArray< FNamedOnlineSession > Sessions
virtual bool UnregisterPlayer(FName SessionName, const FUniqueNetId &PlayerId) override
virtual bool CancelMatchmaking(int32 SearchingPlayerNum, FName SessionName) override
FPVROSession LANSessionManager
void TickLanTasks(float DeltaTime)
virtual bool SendSessionInviteToFriends(int32 LocalUserNum, FName SessionName, const TArray< TSharedRef< const FUniqueNetId > > &Friends) override
uint32 FindLANSession(const TSharedRef< FOnlineSessionSearch > &SearchSettings)
bool IsSessionJoinable(const FNamedOnlineSession &Session) const
virtual void DumpSessionState() override
virtual bool UpdateSession(FName SessionName, FOnlineSessionSettings &UpdatedSessionSettings, bool bShouldRefreshOnlineData=true) override
void RegisterLocalPlayers(class FNamedOnlineSession *Session)
virtual EOnlineSessionState::Type GetSessionState(FName SessionName) const override
class FPVROSubsystem * Subsystem