Files
UnrealEngineUWP/Engine/Source/Runtime/Sockets/Private/Linux/SocketSubsystemLinux.cpp
Ben Marsh 4ba423868f 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

157 lines
3.8 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#include "Linux/SocketSubsystemLinux.h"
#include "Misc/CommandLine.h"
#include "SocketSubsystemModule.h"
#include "IPAddress.h"
#include <net/if.h>
FSocketSubsystemLinux* FSocketSubsystemLinux::SocketSingleton = NULL;
FName CreateSocketSubsystem( FSocketSubsystemModule& SocketSubsystemModule )
{
FName SubsystemName(TEXT("LINUX"));
// Create and register our singleton factor with the main online subsystem for easy access
FSocketSubsystemLinux* SocketSubsystem = FSocketSubsystemLinux::Create();
FString Error;
if (SocketSubsystem->Init(Error))
{
SocketSubsystemModule.RegisterSocketSubsystem(SubsystemName, SocketSubsystem);
return SubsystemName;
}
else
{
FSocketSubsystemLinux::Destroy();
return NAME_None;
}
}
void DestroySocketSubsystem( FSocketSubsystemModule& SocketSubsystemModule )
{
SocketSubsystemModule.UnregisterSocketSubsystem(FName(TEXT("LINUX")));
FSocketSubsystemLinux::Destroy();
}
/**
* Singleton interface for the Android socket subsystem
* @return the only instance of the Android socket subsystem
*/
FSocketSubsystemLinux* FSocketSubsystemLinux::Create()
{
if (SocketSingleton == NULL)
{
SocketSingleton = new FSocketSubsystemLinux();
}
return SocketSingleton;
}
/**
* Destroy the singleton Android socket subsystem
*/
void FSocketSubsystemLinux::Destroy()
{
if (SocketSingleton != NULL)
{
SocketSingleton->Shutdown();
delete SocketSingleton;
SocketSingleton = NULL;
}
}
/**
* Does Linux platform initialization of the sockets library
*
* @param Error a string that is filled with error information
*
* @return TRUE if initialized ok, FALSE otherwise
*/
bool FSocketSubsystemLinux::Init(FString& Error)
{
return true;
}
/**
* Performs Android specific socket clean up
*/
void FSocketSubsystemLinux::Shutdown(void)
{
}
/**
* @return Whether the device has a properly configured network device or not
*/
bool FSocketSubsystemLinux::HasNetworkDevice()
{
// @TODO: implement
return true;
}
TSharedRef<FInternetAddr> FSocketSubsystemLinux::GetLocalHostAddr(FOutputDevice& Out, bool& bCanBindAll)
{
// get parent address first
TSharedRef<FInternetAddr> Addr = FSocketSubsystemBSD::GetLocalHostAddr(Out, bCanBindAll);
// If the address is not a loopback one (or none), return it.
uint32 ParentIp = 0;
Addr->GetIp(ParentIp); // will return in host order
if (ParentIp != 0 && (ParentIp & 0xff000000) != 0x7f000000)
{
return Addr;
}
// If superclass got the address from command line, honor that override
TCHAR Home[256]=TEXT("");
if (FParse::Value(FCommandLine::Get(),TEXT("MULTIHOME="),Home,ARRAY_COUNT(Home)))
{
TSharedRef<FInternetAddr> TempAddr = CreateInternetAddr();
bool bIsValid = false;
TempAddr->SetIp(Home, bIsValid);
if (bIsValid)
{
return Addr;
}
}
// we need to go deeper... (see http://unixhelp.ed.ac.uk/CGI/man-cgi?netdevice+7)
int TempSocket = socket(PF_INET, SOCK_STREAM, 0);
if (TempSocket)
{
ifreq IfReqs[8];
ifconf IfConfig;
FMemory::Memzero(IfConfig);
IfConfig.ifc_req = IfReqs;
IfConfig.ifc_len = sizeof(IfReqs);
int Result = ioctl(TempSocket, SIOCGIFCONF, &IfConfig);
if (Result == 0)
{
for (int32 IdxReq = 0; IdxReq < ARRAY_COUNT(IfReqs); ++IdxReq)
{
// grab the first non-loobpack one which is up
int ResultFlags = ioctl(TempSocket, SIOCGIFFLAGS, &IfReqs[IdxReq]);
if (ResultFlags == 0 &&
(IfReqs[IdxReq].ifr_flags & IFF_UP) &&
(IfReqs[IdxReq].ifr_flags & IFF_LOOPBACK) == 0)
{
int32 NetworkAddr = reinterpret_cast<sockaddr_in *>(&IfReqs[IdxReq].ifr_addr)->sin_addr.s_addr;
Addr->SetIp(ntohl(NetworkAddr));
break;
}
}
}
else
{
int ErrNo = errno;
UE_LOG(LogSockets, Warning, TEXT("ioctl( ,SIOGCIFCONF, ) failed, errno=%d (%s)"), ErrNo, ANSI_TO_TCHAR(strerror(ErrNo)));
}
close(TempSocket);
}
return Addr;
}