You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
Please note that file comments had no purpose in nearly all cases and just added visual clutter. The two files that had meaningful file comments had their comments moved into the corresponding classes. There are still hundreds of file comments left in other files that will be removed over time. Also cleaned up some random stuff along the way: - relative paths to public headers within the same module are no longer necessary (automatically discovered by UBT now) - header guards are deprecated, use #pragma once instead (all compilers support it now) - space between multiple template brackets is no longer required (all compilers support >> now) - NULL to nullptr, OVERRIDE to override - spelling errors, whitespace, line breaks [CL 2104067 by Max Preussner in Main branch]
315 lines
6.5 KiB
C++
315 lines
6.5 KiB
C++
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include "SessionServicesPrivatePCH.h"
|
|
|
|
|
|
/* FSessionManager structors
|
|
*****************************************************************************/
|
|
|
|
FSessionManager::FSessionManager( const IMessageBusRef& InMessageBus )
|
|
: MessageBusPtr(InMessageBus)
|
|
{
|
|
// fill in the owner array
|
|
FString Filter;
|
|
|
|
if (FParse::Value(FCommandLine::Get(), TEXT("SessionFilter="), Filter))
|
|
{
|
|
// Allow support for -SessionFilter=Filter1+Filter2+Filter3
|
|
int32 PlusIdx = Filter.Find(TEXT("+"));
|
|
|
|
while (PlusIdx != INDEX_NONE)
|
|
{
|
|
FString Owner = Filter.Left(PlusIdx);
|
|
FilteredOwners.Add(Owner);
|
|
Filter = Filter.Right(Filter.Len() - (PlusIdx + 1));
|
|
PlusIdx = Filter.Find(TEXT("+"));
|
|
}
|
|
|
|
FilteredOwners.Add(Filter);
|
|
}
|
|
|
|
// connect to message bus
|
|
MessageEndpoint = FMessageEndpoint::Builder("FSessionManager", InMessageBus)
|
|
.Handling<FEngineServicePong>(this, &FSessionManager::HandleEnginePongMessage)
|
|
.Handling<FSessionServicePong>(this, &FSessionManager::HandleSessionPongMessage);
|
|
|
|
// initialize ticker
|
|
TickDelegate = FTickerDelegate::CreateRaw(this, &FSessionManager::HandleTicker);
|
|
FTicker::GetCoreTicker().AddTicker(TickDelegate, 1.f);
|
|
|
|
SendPing();
|
|
}
|
|
|
|
|
|
FSessionManager::~FSessionManager( )
|
|
{
|
|
FTicker::GetCoreTicker().RemoveTicker(TickDelegate);
|
|
}
|
|
|
|
|
|
/* ISessionManager interface
|
|
*****************************************************************************/
|
|
|
|
void FSessionManager::AddOwner( const FString& InOwner )
|
|
{
|
|
FilteredOwners.Add(InOwner);
|
|
}
|
|
|
|
|
|
void FSessionManager::GetSelectedInstances( TArray<ISessionInstanceInfoPtr>& OutInstances) const
|
|
{
|
|
if (SelectedSession.IsValid())
|
|
{
|
|
SelectedSession->GetInstances(OutInstances);
|
|
|
|
for (int32 Index = OutInstances.Num() - 1; Index >= 0 ; --Index)
|
|
{
|
|
if (DeselectedInstances.Contains(OutInstances[Index]))
|
|
{
|
|
OutInstances.RemoveAtSwap(Index);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void FSessionManager::GetSessions( TArray<ISessionInfoPtr>& OutSessions ) const
|
|
{
|
|
OutSessions.Empty(Sessions.Num());
|
|
|
|
for (TMap<FGuid, TSharedPtr<FSessionInfo> >::TConstIterator It(Sessions); It; ++It)
|
|
{
|
|
OutSessions.Add(It.Value());
|
|
}
|
|
}
|
|
|
|
|
|
void FSessionManager::RemoveOwner( const FString& InOwner )
|
|
{
|
|
FilteredOwners.Remove(InOwner);
|
|
RefreshSessions();
|
|
}
|
|
|
|
|
|
bool FSessionManager::SelectSession( const ISessionInfoPtr& Session )
|
|
{
|
|
if (Session != SelectedSession)
|
|
{
|
|
if (!Session.IsValid() || Sessions.Contains(Session->GetSessionId()))
|
|
{
|
|
bool CanSelect = true;
|
|
|
|
CanSelectSessionDelegate.Broadcast(Session, CanSelect);
|
|
|
|
if (CanSelect)
|
|
{
|
|
SelectedSession = Session;
|
|
|
|
SelectedSessionChangedDelegate.Broadcast(Session);
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool FSessionManager::SetInstanceSelected( const ISessionInstanceInfoPtr& Instance, bool Selected )
|
|
{
|
|
if (Instance->GetOwnerSession() == SelectedSession)
|
|
{
|
|
if (Selected)
|
|
{
|
|
if (DeselectedInstances.Remove(Instance) > 0)
|
|
{
|
|
InstanceSelectionChangedDelegate.Broadcast();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!DeselectedInstances.Contains(Instance))
|
|
{
|
|
DeselectedInstances.Add(Instance);
|
|
InstanceSelectionChangedDelegate.Broadcast();
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
/* FSessionManager implementation
|
|
*****************************************************************************/
|
|
|
|
void FSessionManager::FindExpiredSessions( const FDateTime& Now )
|
|
{
|
|
bool Dirty = false;
|
|
|
|
for (TMap<FMessageAddress, TSharedPtr<FSessionInfo> >::TIterator It(Sessions); It; ++It)
|
|
{
|
|
if (Now > It.Value()->GetLastUpdateTime() + FTimespan::FromSeconds(10.0))
|
|
{
|
|
It.RemoveCurrent();
|
|
Dirty = true;
|
|
}
|
|
}
|
|
|
|
if (Dirty)
|
|
{
|
|
SessionsUpdatedDelegate.Broadcast();
|
|
}
|
|
}
|
|
|
|
|
|
bool FSessionManager::IsValidOwner( const FString& Owner )
|
|
{
|
|
if (Owner == FPlatformProcess::UserName(false))
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
for (int32 Index = 0; Index < FilteredOwners.Num(); ++Index)
|
|
{
|
|
if (FilteredOwners[Index] == Owner)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
void FSessionManager::RefreshSessions()
|
|
{
|
|
bool Dirty = false;
|
|
|
|
for (TMap<FMessageAddress, TSharedPtr<FSessionInfo> >::TIterator It(Sessions); It; ++It)
|
|
{
|
|
if (!IsValidOwner(It.Value()->GetSessionOwner()))
|
|
{
|
|
It.RemoveCurrent();
|
|
|
|
Dirty = true;
|
|
}
|
|
}
|
|
|
|
if (Dirty)
|
|
{
|
|
SessionsUpdatedDelegate.Broadcast();
|
|
}
|
|
}
|
|
|
|
|
|
void FSessionManager::SendPing( )
|
|
{
|
|
if (MessageEndpoint.IsValid())
|
|
{
|
|
MessageEndpoint->Publish(new FEngineServicePing(), EMessageScope::Network);
|
|
MessageEndpoint->Publish(new FSessionServicePing(), EMessageScope::Network);
|
|
}
|
|
|
|
LastPingTime = FDateTime::Now();
|
|
}
|
|
|
|
|
|
/* FSessionManager event handlers
|
|
*****************************************************************************/
|
|
|
|
void FSessionManager::HandleEnginePongMessage( const FEngineServicePong& Message, const IMessageContextRef& Context )
|
|
{
|
|
if (!Message.SessionId.IsValid())
|
|
{
|
|
return;
|
|
}
|
|
|
|
// update instance
|
|
TSharedPtr<FSessionInfo> Session = Sessions.FindRef(Message.SessionId);
|
|
|
|
if (Session.IsValid())
|
|
{
|
|
Session->UpdateFromMessage(Message, Context);
|
|
SessionInstanceUpdatedDelegate.Broadcast();
|
|
}
|
|
}
|
|
|
|
|
|
void FSessionManager::HandleInstanceDiscovered( const ISessionInfoRef& Session, const ISessionInstanceInfoRef& Instance )
|
|
{
|
|
if (Session == SelectedSession)
|
|
{
|
|
InstanceSelectionChangedDelegate.Broadcast();
|
|
}
|
|
}
|
|
|
|
|
|
void FSessionManager::HandleLogReceived( const ISessionInfoRef& Session, const ISessionInstanceInfoRef& Instance, const FSessionLogMessageRef& Message )
|
|
{
|
|
if (Session == SelectedSession)
|
|
{
|
|
LogReceivedDelegate.Broadcast(Session, Instance, Message);
|
|
}
|
|
}
|
|
|
|
|
|
void FSessionManager::HandleSessionPongMessage( const FSessionServicePong& Message, const IMessageContextRef& Context )
|
|
{
|
|
if (!Message.SessionId.IsValid())
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Message.Standalone && !IsValidOwner(Message.SessionOwner))
|
|
{
|
|
return;
|
|
}
|
|
|
|
IMessageBusPtr MessageBus = MessageBusPtr.Pin();
|
|
|
|
if (!MessageBus.IsValid())
|
|
{
|
|
return;
|
|
}
|
|
|
|
// update session
|
|
TSharedPtr<FSessionInfo>& Session = Sessions.FindOrAdd(Message.SessionId);
|
|
|
|
if (Session.IsValid())
|
|
{
|
|
Session->UpdateFromMessage(Message, Context);
|
|
}
|
|
else
|
|
{
|
|
Session = MakeShareable(new FSessionInfo(Message.SessionId, MessageBus.ToSharedRef()));
|
|
Session->OnInstanceDiscovered().AddSP(this, &FSessionManager::HandleInstanceDiscovered);
|
|
Session->OnLogReceived().AddSP(this, &FSessionManager::HandleLogReceived);
|
|
Session->UpdateFromMessage(Message, Context);
|
|
|
|
SessionsUpdatedDelegate.Broadcast();
|
|
}
|
|
}
|
|
|
|
|
|
bool FSessionManager::HandleTicker( float DeltaTime )
|
|
{
|
|
FDateTime Now = FDateTime::UtcNow();
|
|
|
|
// FindExpiredSessions(Now);
|
|
|
|
if (Now >= LastPingTime + FTimespan::FromSeconds(2.5))
|
|
{
|
|
SendPing();
|
|
}
|
|
|
|
return true;
|
|
}
|