Files
UnrealEngineUWP/Engine/Source/Runtime/Sockets/Private/Unix/SocketSubsystemUnix.cpp

209 lines
5.7 KiB
C++
Raw Normal View History

// Copyright Epic Games, Inc. All Rights Reserved.
#include "Unix/SocketSubsystemUnix.h"
Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340) #lockdown Nick.Penwarden #rb none ========================== MAJOR FEATURES + CHANGES ========================== Change 3209340 on 2016/11/23 by Ben.Marsh Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h. Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms. * Every header now includes everything it needs to compile. * There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first. * There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h. * Every .cpp file includes its matching .h file first. * This helps validate that each header is including everything it needs to compile. * No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more. * You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there. * There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible. * No engine code explicitly includes a precompiled header any more. * We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies. * PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files. Tool used to generate this transform is at Engine\Source\Programs\IncludeTool. [CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00
#include "Misc/CommandLine.h"
#include "SocketSubsystemModule.h"
#include "BSDSockets/IPAddressBSD.h"
#include "Unix/SocketsUnix.h"
#include <ifaddrs.h>
#include <net/if.h>
FSocketSubsystemUnix* FSocketSubsystemUnix::SocketSingleton = NULL;
FName CreateSocketSubsystem( FSocketSubsystemModule& SocketSubsystemModule )
{
FName SubsystemName(TEXT("UNIX"));
// Create and register our singleton factor with the main online subsystem for easy access
FSocketSubsystemUnix* SocketSubsystem = FSocketSubsystemUnix::Create();
FString Error;
if (SocketSubsystem->Init(Error))
{
SocketSubsystemModule.RegisterSocketSubsystem(SubsystemName, SocketSubsystem);
return SubsystemName;
}
else
{
FSocketSubsystemUnix::Destroy();
return NAME_None;
}
}
void DestroySocketSubsystem( FSocketSubsystemModule& SocketSubsystemModule )
{
SocketSubsystemModule.UnregisterSocketSubsystem(FName(TEXT("UNIX")));
FSocketSubsystemUnix::Destroy();
}
/**
* Singleton interface for the Android socket subsystem
* @return the only instance of the Android socket subsystem
*/
FSocketSubsystemUnix* FSocketSubsystemUnix::Create()
{
if (SocketSingleton == NULL)
{
SocketSingleton = new FSocketSubsystemUnix();
}
return SocketSingleton;
}
/**
* Destroy the singleton Android socket subsystem
*/
void FSocketSubsystemUnix::Destroy()
{
if (SocketSingleton != NULL)
{
SocketSingleton->Shutdown();
delete SocketSingleton;
SocketSingleton = NULL;
}
}
/**
* Does Unix platform initialization of the sockets library
*
* @param Error a string that is filled with error information
*
* @return TRUE if initialized ok, FALSE otherwise
*/
bool FSocketSubsystemUnix::Init(FString& Error)
{
return true;
}
/**
* Performs Android specific socket clean up
*/
void FSocketSubsystemUnix::Shutdown(void)
{
}
/**
* @return Whether the device has a properly configured network device or not
*/
bool FSocketSubsystemUnix::HasNetworkDevice()
{
// @TODO: implement
return true;
}
FSocket* FSocketSubsystemUnix::CreateSocket(const FName& SocketType, const FString& SocketDescription, const FName& ProtocolType)
{
FSocketBSD* NewSocket = (FSocketBSD*)FSocketSubsystemBSD::CreateSocket(SocketType, SocketDescription, ProtocolType);
if (NewSocket != nullptr)
{
NewSocket->SetIPv6Only(false);
}
else
{
UE_LOG(LogSockets, Warning, TEXT("Failed to create socket %s [%s]"), *SocketType.ToString(), *SocketDescription);
}
return NewSocket;
}
bool FSocketSubsystemUnix::GetLocalAdapterAddresses(TArray<TSharedPtr<FInternetAddr>>& OutAddresses)
{
ifaddrs* Interfaces = NULL;
int InterfaceQueryRet = getifaddrs(&Interfaces);
UE_LOG(LogSockets, Verbose, TEXT("Querying net interfaces returned: %d"), InterfaceQueryRet);
const bool bDisableIPv6 = CVarDisableIPv6.GetValueOnAnyThread() == 1;
if (InterfaceQueryRet == 0)
{
// Loop through linked list of interfaces
for (ifaddrs* Travel = Interfaces; Travel != NULL; Travel = Travel->ifa_next)
{
// Skip over empty data sets.
if (Travel->ifa_addr == NULL)
{
continue;
}
uint16 AddrFamily = Travel->ifa_addr->sa_family;
// Find any up and non-loopback addresses
if ((Travel->ifa_flags & IFF_UP) != 0 &&
(Travel->ifa_flags & IFF_LOOPBACK) == 0 &&
(AddrFamily == AF_INET || (!bDisableIPv6 && AddrFamily == AF_INET6)))
{
TSharedRef<FInternetAddrBSD> NewAddress = MakeShareable(new FInternetAddrBSD(this));
NewAddress->SetIp(*((sockaddr_storage*)Travel->ifa_addr));
uint32 AddressInterface = ntohl(if_nametoindex(Travel->ifa_name));
NewAddress->SetScopeId(AddressInterface);
OutAddresses.Add(NewAddress);
UE_LOG(LogSockets, Verbose, TEXT("Added address %s on interface %d"), *(NewAddress->ToString(false)), AddressInterface);
}
}
freeifaddrs(Interfaces);
}
else
{
UE_LOG(LogSockets, Warning, TEXT("getifaddrs returned result %d"), InterfaceQueryRet);
return false;
}
return (OutAddresses.Num() > 0);
}
FSocketBSD* FSocketSubsystemUnix::InternalBSDSocketFactory(SOCKET Socket, ESocketType SocketType, const FString& SocketDescription, const FName& SocketProtocol)
{
return new FSocketUnix(Socket, SocketType, SocketDescription, SocketProtocol, this);
}
TUniquePtr<FRecvMulti> FSocketSubsystemUnix::CreateRecvMulti(int32 MaxNumPackets, int32 MaxPacketSize, ERecvMultiFlags Flags)
{
#if PLATFORM_HAS_BSD_SOCKET_FEATURE_RECVMMSG
return MakeUnique<FUnixRecvMulti>(this, MaxNumPackets, MaxPacketSize, Flags);
#endif
return nullptr;
}
bool FSocketSubsystemUnix::IsSocketRecvMultiSupported() const
{
#if PLATFORM_HAS_BSD_SOCKET_FEATURE_RECVMMSG
return true;
#endif
return false;
}
double FSocketSubsystemUnix::TranslatePacketTimestamp(const FPacketTimestamp& Timestamp, ETimestampTranslation Translation)
{
double ReturnVal = 0.0;
const bool bDeltaOnly = Translation == ETimestampTranslation::TimeDelta;
if (Translation == ETimestampTranslation::LocalTimestamp || bDeltaOnly)
{
// Unfortunately, the packet timestamp is platform-specific, using CLOCK_REALTIME in this case,
// whereas FPlatformTime may select from a variety of incompatible clocks.
// So, the only safe option is to return the time difference instead.
struct timespec CurPlatformTime;
clock_gettime(CLOCK_REALTIME, &CurPlatformTime);
FTimespan CurPlatformTimespan((CurPlatformTime.tv_sec * ETimespan::TicksPerSecond) +
(CurPlatformTime.tv_nsec / ETimespan::NanosecondsPerTick));
ReturnVal = (CurPlatformTimespan - Timestamp.Timestamp).GetTotalSeconds();
if (!bDeltaOnly)
{
ReturnVal = FPlatformTime::Seconds() - ReturnVal;
}
}
else
{
UE_LOG(LogSockets, Warning, TEXT("TranslatePacketTimestamp: Unknown ETimestampTranslation type: %i"), (uint32)Translation);
}
return ReturnVal;
}