2016-12-08 08:52:44 -05:00
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
2014-03-14 14:13:41 -04:00
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 "NetworkFileServer.h"
# include "HAL/RunnableThread.h"
# include "Misc/OutputDeviceRedirector.h"
# include "IPAddress.h"
# include "Sockets.h"
# include "SocketSubsystem.h"
# include "NetworkMessage.h"
# include "NetworkFileSystemLog.h"
2014-06-12 17:02:52 -04:00
# include "NetworkFileServerConnection.h"
class FNetworkFileServerClientConnectionThreaded
: public FNetworkFileServerClientConnection
, protected FRunnable
{
public :
2017-06-15 17:45:03 -04:00
FNetworkFileServerClientConnectionThreaded ( FSocket * InSocket , const FNetworkFileDelegateContainer * NetworkFileDelegates , const TArray < ITargetPlatform * > & InActiveTargetPlatforms )
: FNetworkFileServerClientConnection ( NetworkFileDelegates , InActiveTargetPlatforms )
2014-06-12 17:02:52 -04:00
, Socket ( InSocket )
{
Running . Set ( true ) ;
StopRequested . Reset ( ) ;
2017-06-15 17:45:03 -04:00
2014-06-12 17:02:52 -04:00
# if UE_BUILD_DEBUG
2017-06-15 17:45:03 -04:00
// this thread needs more space in debug builds as it tries to log messages and such
const static uint32 NetworkFileServerThreadSize = 2 * 1024 * 1024 ;
2014-06-12 17:02:52 -04:00
# else
2017-06-15 17:45:03 -04:00
const static uint32 NetworkFileServerThreadSize = 1 * 1024 * 1024 ;
2014-06-12 17:02:52 -04:00
# endif
2017-06-15 17:45:03 -04:00
WorkerThread = FRunnableThread : : Create ( this , TEXT ( " FNetworkFileServerClientConnection " ) , NetworkFileServerThreadSize , TPri_AboveNormal ) ;
2014-06-12 17:02:52 -04:00
}
2015-04-01 07:20:55 -04:00
virtual bool Init ( ) override
2014-06-12 17:02:52 -04:00
{
return true ;
}
2015-04-01 07:20:55 -04:00
virtual uint32 Run ( ) override
2014-06-12 17:02:52 -04:00
{
while ( ! StopRequested . GetValue ( ) )
{
2014-08-07 12:57:57 -04:00
// read a header and payload pair
FArrayReader Payload ;
if ( ! FNFSMessageHeader : : ReceivePayload ( Payload , FSimpleAbstractSocket_FSocket ( Socket ) ) )
break ;
// now process the contents of the payload
2014-08-08 19:46:54 -04:00
if ( ! FNetworkFileServerClientConnection : : ProcessPayload ( Payload ) )
2014-08-07 12:57:57 -04:00
{
// give the processing of the payload a chance to terminate the connection
// failed to process message
UE_LOG ( LogFileServer , Warning , TEXT ( " Unable to process payload terminating connection " ) ) ;
break ;
}
}
2014-06-12 17:02:52 -04:00
2014-08-07 12:57:57 -04:00
return true ;
2014-06-12 17:02:52 -04:00
}
2014-08-08 19:46:54 -04:00
virtual bool SendPayload ( TArray < uint8 > & Out ) override
{
# if USE_MCSOCKET_FOR_NFS
return FNFSMessageHeader : : WrapAndSendPayload ( Out , FSimpleAbstractSocket_FMultichannelTCPSocket ( MCSocket , NFS_Channels : : Main ) ) ;
# else
return FNFSMessageHeader : : WrapAndSendPayload ( Out , FSimpleAbstractSocket_FSocket ( Socket ) ) ;
# endif
}
2015-04-01 07:20:55 -04:00
virtual void Stop ( ) override
2014-06-12 17:02:52 -04:00
{
StopRequested . Set ( true ) ;
}
2015-04-01 07:20:55 -04:00
virtual void Exit ( ) override
2014-06-12 17:02:52 -04:00
{
Socket - > Close ( ) ;
ISocketSubsystem : : Get ( ) - > DestroySocket ( Socket ) ;
Running . Set ( false ) ;
}
bool IsRunning ( )
{
return ( Running . GetValue ( ) ! = 0 ) ;
}
2015-01-20 10:51:54 -05:00
void GetAddress ( FInternetAddr & Addr )
2014-08-07 12:57:57 -04:00
{
Socket - > GetAddress ( Addr ) ;
}
2015-01-20 10:51:54 -05:00
void GetPeerAddress ( FInternetAddr & Addr )
{
Socket - > GetPeerAddress ( Addr ) ;
}
2014-06-12 17:02:52 -04:00
~ FNetworkFileServerClientConnectionThreaded ( )
{
WorkerThread - > Kill ( true ) ;
}
private :
FSocket * Socket ;
FThreadSafeCounter StopRequested ;
FThreadSafeCounter Running ;
FRunnableThread * WorkerThread ;
} ;
2014-03-14 14:13:41 -04:00
/* FNetworkFileServer constructors
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2017-06-15 17:45:03 -04:00
FNetworkFileServer : : FNetworkFileServer ( int32 InPort , FNetworkFileDelegateContainer InNetworkFileDelegateContainer ,
const TArray < ITargetPlatform * > & InActiveTargetPlatforms )
2014-06-12 17:02:52 -04:00
: ActiveTargetPlatforms ( InActiveTargetPlatforms )
2014-03-14 14:13:41 -04:00
{
2014-07-07 15:39:19 -04:00
if ( InPort < 0 )
{
InPort = DEFAULT_TCP_FILE_SERVING_PORT ;
}
2014-06-12 17:02:52 -04:00
Running . Set ( false ) ;
StopRequested . Set ( false ) ;
2014-03-14 14:13:41 -04:00
UE_LOG ( LogFileServer , Warning , TEXT ( " Unreal Network File Server starting up... " ) ) ;
2017-06-15 17:45:03 -04:00
NetworkFileDelegates = InNetworkFileDelegateContainer ;
2014-03-14 14:13:41 -04:00
// make sure sockets are going
ISocketSubsystem * SocketSubsystem = ISocketSubsystem : : Get ( ) ;
if ( ! SocketSubsystem )
{
UE_LOG ( LogFileServer , Error , TEXT ( " Could not get socket subsystem. " ) ) ;
}
else
{
// create a server TCP socket
Socket = SocketSubsystem - > CreateSocket ( NAME_Stream , TEXT ( " FNetworkFileServer tcp-listen " ) ) ;
if ( ! Socket )
{
UE_LOG ( LogFileServer , Error , TEXT ( " Could not create listen socket. " ) ) ;
}
else
{
// listen on any IP address
ListenAddr = SocketSubsystem - > GetLocalBindAddr ( * GLog ) ;
ListenAddr - > SetPort ( InPort ) ;
Socket - > SetReuseAddr ( ) ;
// bind to the address
if ( ! Socket - > Bind ( * ListenAddr ) )
{
UE_LOG ( LogFileServer , Warning , TEXT ( " Failed to bind listen socket %s in FNetworkFileServer " ) , * ListenAddr - > ToString ( true ) ) ;
}
// listen for connections
else if ( ! Socket - > Listen ( 16 ) )
{
UE_LOG ( LogFileServer , Warning , TEXT ( " Failed to listen on socket %s in FNetworkFileServer " ) , * ListenAddr - > ToString ( true ) ) ;
}
else
{
// set the port on the listen address to be the same as the port on the socket
int32 port = Socket - > GetPortNo ( ) ;
check ( ( InPort = = 0 & & port ! = 0 ) | | port = = InPort ) ;
ListenAddr - > SetPort ( port ) ;
// now create a thread to accept connections
2014-05-12 08:39:12 -04:00
Thread = FRunnableThread : : Create ( this , TEXT ( " FNetworkFileServer " ) , 8 * 1024 , TPri_AboveNormal ) ;
2014-03-14 14:13:41 -04:00
UE_LOG ( LogFileServer , Display , TEXT ( " Unreal Network File Server is ready for client connections on %s! " ) , * ListenAddr - > ToString ( true ) ) ;
}
}
}
2014-06-12 17:02:52 -04:00
}
2014-03-14 14:13:41 -04:00
FNetworkFileServer : : ~ FNetworkFileServer ( )
{
// Kill the running thread.
if ( Thread ! = NULL )
{
Thread - > Kill ( true ) ;
delete Thread ;
Thread = NULL ;
}
// We are done with the socket.
Socket - > Close ( ) ;
ISocketSubsystem : : Get ( ) - > DestroySocket ( Socket ) ;
Socket = NULL ;
}
/* FRunnable overrides
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
uint32 FNetworkFileServer : : Run ( )
{
2014-06-12 17:02:52 -04:00
Running . Set ( true ) ;
2014-03-14 14:13:41 -04:00
// go until requested to be done
2014-06-12 17:02:52 -04:00
while ( ! StopRequested . GetValue ( ) )
2014-03-14 14:13:41 -04:00
{
bool bReadReady = false ;
// clean up closed connections
for ( int32 ConnectionIndex = 0 ; ConnectionIndex < Connections . Num ( ) ; + + ConnectionIndex )
{
2014-06-12 17:02:52 -04:00
FNetworkFileServerClientConnectionThreaded * Connection = Connections [ ConnectionIndex ] ;
2014-03-14 14:13:41 -04:00
2014-06-12 17:02:52 -04:00
if ( ! Connection - > IsRunning ( ) )
2014-03-14 14:13:41 -04:00
{
UE_LOG ( LogFileServer , Display , TEXT ( " Client %s disconnected. " ) , * Connection - > GetDescription ( ) ) ;
Connections . RemoveAtSwap ( ConnectionIndex ) ;
delete Connection ;
}
}
// check for incoming connections
if ( Socket - > HasPendingConnection ( bReadReady ) & & bReadReady )
{
FSocket * ClientSocket = Socket - > Accept ( TEXT ( " Remote Console Connection " ) ) ;
if ( ClientSocket ! = NULL )
{
2015-01-20 10:51:54 -05:00
TSharedPtr < FInternetAddr > Addr = ISocketSubsystem : : Get ( PLATFORM_SOCKETSUBSYSTEM ) - > CreateInternetAddr ( ) ;
2014-08-07 12:57:57 -04:00
ClientSocket - > GetAddress ( * Addr ) ;
2015-01-20 10:51:54 -05:00
TSharedPtr < FInternetAddr > PeerAddr = ISocketSubsystem : : Get ( PLATFORM_SOCKETSUBSYSTEM ) - > CreateInternetAddr ( ) ;
ClientSocket - > GetPeerAddress ( * PeerAddr ) ;
2014-08-07 12:57:57 -04:00
for ( auto PreviousConnection : Connections )
{
TSharedPtr < FInternetAddr > PreviousAddr = ISocketSubsystem : : Get ( PLATFORM_SOCKETSUBSYSTEM ) - > CreateInternetAddr ( ) ; ;
PreviousConnection - > GetAddress ( * PreviousAddr ) ;
2015-01-20 10:51:54 -05:00
TSharedPtr < FInternetAddr > PreviousPeerAddr = ISocketSubsystem : : Get ( PLATFORM_SOCKETSUBSYSTEM ) - > CreateInternetAddr ( ) ; ;
PreviousConnection - > GetPeerAddress ( * PreviousPeerAddr ) ;
if ( ( * Addr = = * PreviousAddr ) & &
( * PeerAddr = = * PreviousPeerAddr ) )
2014-08-07 12:57:57 -04:00
{
// kill hte connection
PreviousConnection - > Stop ( ) ;
2015-01-20 10:51:54 -05:00
UE_LOG ( LogFileServer , Warning , TEXT ( " Killing client connection %s because new client connected from same address. " ) , * PreviousConnection - > GetDescription ( ) ) ;
2014-08-07 12:57:57 -04:00
}
}
2017-06-15 17:45:03 -04:00
FNetworkFileServerClientConnectionThreaded * Connection = new FNetworkFileServerClientConnectionThreaded ( ClientSocket , & NetworkFileDelegates , ActiveTargetPlatforms ) ;
2014-03-14 14:13:41 -04:00
Connections . Add ( Connection ) ;
UE_LOG ( LogFileServer , Display , TEXT ( " Client %s connected. " ) , * Connection - > GetDescription ( ) ) ;
}
}
FPlatformProcess : : Sleep ( 0.25f ) ;
}
return 0 ;
}
void FNetworkFileServer : : Exit ( )
{
// close all connections
for ( int32 ConnectionIndex = 0 ; ConnectionIndex < Connections . Num ( ) ; ConnectionIndex + + )
{
delete Connections [ ConnectionIndex ] ;
}
Connections . Empty ( ) ;
}
/* INetworkFileServer overrides
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2014-07-07 15:39:19 -04:00
FString FNetworkFileServer : : GetSupportedProtocol ( ) const
{
return FString ( " tcp " ) ;
}
2014-03-14 14:13:41 -04:00
bool FNetworkFileServer : : GetAddressList ( TArray < TSharedPtr < FInternetAddr > > & OutAddresses ) const
{
if ( ListenAddr . IsValid ( ) )
{
FString ListenAddressString = ListenAddr - > ToString ( true ) ;
if ( ListenAddressString . StartsWith ( TEXT ( " 0.0.0.0 " ) ) )
{
if ( ISocketSubsystem : : Get ( ) - > GetLocalAdapterAddresses ( OutAddresses ) )
{
for ( int32 AddressIndex = 0 ; AddressIndex < OutAddresses . Num ( ) ; + + AddressIndex )
{
OutAddresses [ AddressIndex ] - > SetPort ( ListenAddr - > GetPort ( ) ) ;
}
}
}
else
{
OutAddresses . Add ( ListenAddr ) ;
}
}
return ( OutAddresses . Num ( ) > 0 ) ;
2014-06-12 17:02:52 -04:00
}
2017-06-15 17:45:03 -04:00
2014-06-12 17:02:52 -04:00
bool FNetworkFileServer : : IsItReadyToAcceptConnections ( void ) const
{
return ( Running . GetValue ( ) ! = 0 ) ;
}
int32 FNetworkFileServer : : NumConnections ( void ) const
{
return Connections . Num ( ) ;
}
void FNetworkFileServer : : Shutdown ( void )
{
Stop ( ) ;
}