Files
UnrealEngineUWP/Engine/Source/Runtime/NetworkFileSystem/Private/NetworkFileServerHttp.cpp
Josh Adams cf651e0c63 Change 2898947 on 2016/03/08 by Mark.Satterthwaite
Move the Apple Driver Monitor stats into their own stat groups, DriverMonitor has the common stats, DriverMonitorAMD/Intel/Nvidia have the vendor/GPU specific stats. The Metal and OpenGL RHIs update the driver monitor stats group for the current GPU at the appropriate time.

Change 2898950 on 2016/03/08 by Mark.Satterthwaite

	More shader cache code documentation.

Change 2898952 on 2016/03/08 by Michael.Trepka

	Check GPU driver version and warn of bad drivers only on Windows

Change 2898964 on 2016/03/08 by Mark.Satterthwaite

	Only verify the vertex attribute layout for Metal in debug builds or when using development with the debug layer turned on. It reduces performance significantly and isn't all that helpful unless you are attempting to debug a mismatch.

Change 2898973 on 2016/03/08 by Mark.Satterthwaite

	Switch uniform buffers to managed memory on Mac as this is more appropriate for AMD & Nvidia GPUs.

Change 2898988 on 2016/03/08 by Mark.Satterthwaite

	Simplify MetalContext by having only one SubmitCommandsHint implementation.

Change 2899011 on 2016/03/08 by Mark.Satterthwaite

	Duplicate 4.11 CL #2898988:

	Proper fix for UE-25804 - we have to manually expand PF_G8 + SRGB to RGBA8_sRGB - this then fixes UE-27483.
	#jira UE-25804
	#jira UE-27483

Change 2899024 on 2016/03/08 by Mark.Satterthwaite

	Duplicate 4.11 CL #2887365 & CL #2887583:

	Allow InfiltratorDemoEditor under Metal to issue a query buffer reset without crashing - the function that switches to the new query buffer needs to reapply some of the draw-state so that future commands don't dereference nil.
	#jira UE-27513

	My earlier fix for UE-27513 overlooked various internal details that meant it wouldn't restore state correctly, would fail validation and could crash in a new place. This version will ensure that cached state is only reset when it is appropriate to do so and will restore it correct when doing a query buffer reset.
	#jira UE-27513

Change 2899418 on 2016/03/08 by Daniel.Lamb

	Added support for textboxes in the editor to convert uasset filenames into long package names. As this is more useful to the cooker and more portable for projects.
	#codereview Matt.Kuhlenschmidt
	#jira UE-27785

Change 2899419 on 2016/03/08 by Daniel.Lamb

	Added support for passing -opengl command through to launch on if the editor is started with it.
	#codereview Michael.Trepka

Change 2900846 on 2016/03/09 by Mark.Satterthwaite

	Reimplement Metal object lifetime tracking as stats in the stat-group, though the old system is maintained as a debug-only tool that could (and probably should) be extended to track over/under-release bugs. Currently the texture count will be distorted by texture SRVs so will need improvement but other stats should be reliable. In order to properly report the number of buffers the TResourcePool policy class must now define a FreeResource function, so I've added them to the appropriate places too.

Change 2900853 on 2016/03/09 by Mark.Satterthwaite

	Optimise away empty encoders that don't perform a clear operation on AMD & Intel, but not Nvidia or non-Mac Metal devices. This should slightly improve performance.

Change 2900927 on 2016/03/09 by Mark.Satterthwaite

	Implemented operation threshold submission of Metal command buffers to keep the GPU busier and not just idle waiting for the CPU. Whenever rhi.Metal.CommandBufferCommitThreshold is set to a value >0 and the current command buffer has >= draw/dispatch operations outstanding then the command-buffer will be committed at the next encoder boundary. The default value is 100 operations which is currently arbitrary and the feature can be disabled by setting the value to <= 0 in which case only explicit submissions will occur as previously.

Change 2901310 on 2016/03/09 by Mark.Satterthwaite

	Change OneColor clear shader setup so that it works with parallel encoding in Metal.

Change 2903002 on 2016/03/10 by Mark.Satterthwaite

	Instantiate the OneColor shaders once in Metal.

Change 2903274 on 2016/03/10 by Mark.Satterthwaite

	Remove more unnecessary parallel execution stalls from MetalRHI.

Change 2903402 on 2016/03/10 by Mark.Satterthwaite

	Implement Metal support for index buffer SRVs.

Change 2903419 on 2016/03/10 by Mark.Satterthwaite

	Always use Managed memory on Mac Metal for buffers.

Change 2905206 on 2016/03/11 by Mark.Satterthwaite

	Worked around UE-27818 "ElementalDemo Causes Invalid Rendering on AMD GPUs" - recent changes to allow mesh particles to write to velocity leave a texture-buffer unbound & then use a uniform value & an if-branch to guard against access but AMD's Mac GL driver notices that the buffer is referenced in the shader but not bound & promptly tries to fallback to Apple's S/W renderer regardless of what the uniform value is. That's legal behaviour for an OpenGL implementation so the C++ code has been changed to allocate and write the current transforms into the buffer for OpenGL when they wouldn't otherwise be provided. This is sufficient to avoid the problem without affecting any other API.

Change 2906217 on 2016/03/11 by Nick.Shin

	re-enabled http network file server
	it was disabled in CL: #2790193

	#jira UE-22166 HTML5 Cook on the fly will launch and then close browser

Change 2908203 on 2016/03/14 by Michael.Trepka

	Merging //UE4/Dev-Main to Dev-Platform (//UE4/Dev-Platform). Everything but SSF lib.

Change 2908553 on 2016/03/14 by Mark.Satterthwaite

	Force a submit & wait in Metal when contexts are being destroyed to prevent kernel panics in drivers which continue to process the now abandoned command-queue and encounter invalid resources (because we destroy them on shutdown).

Change 2908595 on 2016/03/14 by Michael.Trepka

	Fixed iOS compile error in MetalUniformBuffer.cpp

	#codereview Mark.Satterthwaite

Change 2910106 on 2016/03/15 by Mark.Satterthwaite

	Use a dispatch_semaphore not an FEvent for Metal free-list synchronisation as the dispatch_worker threads can't be properly setup for FStats and this causes problems.

Change 2910107 on 2016/03/15 by Mark.Satterthwaite

	Fix Metal reporting of GPU memory through the RHI as it is in bytes, not MB.

Change 2910138 on 2016/03/15 by Mark.Satterthwaite

	Properly retain/release dispatch_semaphore for Metal command buffer completion block & allow uniform buffer creation on parallel encoding thread.

Change 2911735 on 2016/03/16 by Nick.Shin

	housekeeping

	removing extra and inconsistant whitespace as well as making tabs & spaces consistant

[CL 2936662 by Josh Adams in Main branch]
2016-04-07 12:59:50 -04:00

487 lines
13 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#if ENABLE_HTTP_FOR_NFS
#include "NetworkFileSystemPrivatePCH.h"
#include "NetworkFileServerHttp.h"
class FNetworkFileServerClientConnectionHTTP : public FNetworkFileServerClientConnection
{
public:
FNetworkFileServerClientConnectionHTTP(const FFileRequestDelegate& InFileRequestDelegate,
const FRecompileShadersDelegate& InRecompileShadersDelegate, const TArray<ITargetPlatform*>& InActiveTargetPlatforms )
: FNetworkFileServerClientConnection( InFileRequestDelegate,InRecompileShadersDelegate,InActiveTargetPlatforms)
{
}
bool SendPayload( TArray<uint8> &Out )
{
// Make Boundaries between payloads, add a visual marker for easier debugging.
uint32 Marker = 0xDeadBeef;
uint32 Size = Out.Num();
OutBuffer.Append((uint8*)&Marker,sizeof(uint32));
OutBuffer.Append((uint8*)&Size,sizeof(uint32));
OutBuffer.Append(Out);
return true;
}
private:
TArray<uint8>& GetOutBuffer() { return OutBuffer; }
void ResetBuffer() { OutBuffer.Reset(); }
TArray<uint8> OutBuffer;
friend class FNetworkFileServerHttp;
};
//////////////////////////////////////////////////////////////////////////
// LibWebsockets specific structs.
// a object of this type is associated by libwebsocket to every http session.
struct PerSessionData
{
// data being received.
TArray<uint8> In;
// data being sent out.
TArray<uint8> Out;
};
// protocol array.
static struct libwebsocket_protocols Protocols[] = {
/* first protocol must always be HTTP handler */
{
"http-only", // name
FNetworkFileServerHttp::CallBack_HTTP, // callback
sizeof(PerSessionData), // per_session_data_size
15 * 1024,
15 * 1024
},
{
NULL, NULL, 0 /* End of list */
}
};
//////////////////////////////////////////////////////////////////////////
FNetworkFileServerHttp::FNetworkFileServerHttp(
int32 InPort,
const FFileRequestDelegate* InFileRequestDelegate,
const FRecompileShadersDelegate* InRecompileShadersDelegate,
const TArray<ITargetPlatform*>& InActiveTargetPlatforms
)
:ActiveTargetPlatforms(InActiveTargetPlatforms)
,Port(InPort)
{
if (Port < 0 )
{
Port = DEFAULT_HTTP_FILE_SERVING_PORT;
}
UE_LOG(LogFileServer, Warning, TEXT("Unreal Network Http File Server starting up..."));
if (InFileRequestDelegate && InFileRequestDelegate->IsBound())
{
FileRequestDelegate = *InFileRequestDelegate;
}
if (InRecompileShadersDelegate && InRecompileShadersDelegate->IsBound())
{
RecompileShadersDelegate = *InRecompileShadersDelegate;
}
StopRequested.Reset();
Ready.Reset();
// spin up the worker thread, this will block till Init has executed on the freshly spinned up thread, Ready will have appropriate value
// set by the end of this function.
WorkerThread = FRunnableThread::Create(this,TEXT("FNetworkFileServerHttp"), 8 * 1024, TPri_AboveNormal);
}
bool FNetworkFileServerHttp::IsItReadyToAcceptConnections(void) const
{
return (Ready.GetValue() != 0);
}
#if UE_BUILD_DEBUG
void libwebsocket_debugLog(int level, const char *line)
{
UE_LOG(LogFileServer, Warning, TEXT(" LibWebsocket: %s"), ANSI_TO_TCHAR(line));
}
#endif
bool FNetworkFileServerHttp::Init()
{
// setup log level.
#if UE_BUILD_DEBUG
lws_set_log_level( LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_DEBUG , libwebsocket_debugLog);
#endif
struct lws_context_creation_info Info;
memset(&Info,0,sizeof(lws_context_creation_info));
// look up libwebsockets.h for details.
Info.port = Port;
// we listen on all available interfaces.
Info.iface = NULL;
// serve only the http protocols.
Info.protocols = Protocols;
// no extensions
Info.extensions = NULL;
Info.gid = -1;
Info.uid = -1;
Info.options = 0;
// tack on this object.
Info.user = this;
Context = libwebsocket_create_context(&Info);
Port = Info.port;
if (Context == NULL) {
UE_LOG(LogFileServer, Fatal, TEXT(" Could not create a libwebsocket content for port : %d"), Port);
return false;
}
Ready.Set(true);
return true;
}
FString FNetworkFileServerHttp::GetSupportedProtocol() const
{
return FString("http");
}
bool FNetworkFileServerHttp::GetAddressList(TArray<TSharedPtr<FInternetAddr> >& OutAddresses) const
{
// if Init failed, its already too late.
ensure( Context != nullptr);
// we are listening to all local interfaces.
ISocketSubsystem::Get()->GetLocalAdapterAddresses(OutAddresses);
// Fix up ports.
for (int32 AddressIndex = 0; AddressIndex < OutAddresses.Num(); ++AddressIndex)
{
OutAddresses[AddressIndex]->SetPort(Port);
}
return true;
}
int32 FNetworkFileServerHttp::NumConnections() const
{
return RequestHandlers.Num();
}
void FNetworkFileServerHttp::Shutdown()
{
// Allow multiple calls to this function.
if ( WorkerThread )
{
WorkerThread->Kill(true); // Kill Nicely. Wait for everything to shutdown.
delete WorkerThread;
WorkerThread = NULL;
}
}
uint32 FNetworkFileServerHttp::Run()
{
UE_LOG(LogFileServer, Display, TEXT("Unreal Network File Http Server is ready for client connections on port %d"), Port);
// start servicing.
// service libwebsocket context.
while(!StopRequested.GetValue())
{
// service libwebsocket, have a slight delay so it doesn't spin on zero load.
libwebsocket_service(Context, 10);
libwebsocket_callback_on_writable_all_protocol(&Protocols[0]);
}
UE_LOG(LogFileServer, Display, TEXT("Unreal Network File Http Server is now Shutting down "));
return true;
}
// Called internally by FRunnableThread::Kill.
void FNetworkFileServerHttp::Stop()
{
StopRequested.Set(true);
}
void FNetworkFileServerHttp::Exit()
{
// let's start shutting down.
// fires a LWS_CALLBACK_PROTOCOL_DESTROY callback, we clean up after ourselves there.
libwebsocket_context_destroy(Context);
Context = NULL;
}
FNetworkFileServerHttp::~FNetworkFileServerHttp()
{
Shutdown();
// delete our request handlers.
for ( auto& Element : RequestHandlers)
{
delete Element.Value;
}
// make sure context has been already cleaned up.
check( Context == NULL );
}
FNetworkFileServerClientConnectionHTTP* FNetworkFileServerHttp::CreateNewConnection()
{
return new FNetworkFileServerClientConnectionHTTP(FileRequestDelegate,RecompileShadersDelegate,ActiveTargetPlatforms);
}
// Have a similar process function for the normal tcp connection.
void FNetworkFileServerHttp::Process(FArchive& In, TArray<uint8>&Out, FNetworkFileServerHttp* Server)
{
int loops = 0;
while(!In.AtEnd())
{
UE_LOG(LogFileServer, Log, TEXT("In %d "), loops++);
// Every Request has a Guid attached to it - similar to Web session IDs.
FGuid ClientGuid;
In << ClientGuid;
UE_LOG(LogFileServer, Log, TEXT("Recieved GUID %s"), *ClientGuid.ToString());
FNetworkFileServerClientConnectionHTTP* Connection = NULL;
if (Server->RequestHandlers.Contains(ClientGuid))
{
UE_LOG(LogFileServer, Log, TEXT("Picking up an existing handler" ));
Connection = Server->RequestHandlers[ClientGuid];
}
else
{
UE_LOG(LogFileServer, Log, TEXT("Creating a handler" ));
Connection = Server->CreateNewConnection();
Server->RequestHandlers.Add(ClientGuid,Connection);
}
Connection->ProcessPayload(In);
Out.Append(Connection->GetOutBuffer());
Connection->ResetBuffer();
}
}
// This static function handles all callbacks coming in and when context is services via libwebsocket_service
// return value of -1, closes the connection.
int FNetworkFileServerHttp::CallBack_HTTP(
struct libwebsocket_context *Context,
struct libwebsocket *Wsi,
enum libwebsocket_callback_reasons Reason,
void *User,
void *In,
size_t Len)
{
PerSessionData* BufferInfo = (PerSessionData*)User;
FNetworkFileServerHttp* Server = (FNetworkFileServerHttp*)libwebsocket_context_user(Context);
switch (Reason)
{
case LWS_CALLBACK_HTTP:
// hang on to socket even if there's no data for atleast 60 secs.
libwebsocket_set_timeout(Wsi, NO_PENDING_TIMEOUT, 60);
/* if it was not legal POST URL, let it continue and accept data */
if (!lws_hdr_total_length(Wsi, WSI_TOKEN_POST_URI))
{
char *requested_uri = (char *) In;
// client request the base page. e.g http://unrealfileserver:port/
// just return a banner, probably add some more information, e,g Version, Config, Game. etc.
if ( FCString::Strcmp(ANSI_TO_TCHAR(requested_uri), TEXT("/")) == 0 )
{
TCHAR Buffer[1024];
TCHAR ServerBanner[] = TEXT("<HTML>This is Unreal File Server</HTML>");
int x = FCString::Sprintf(
Buffer,
TEXT("HTTP/1.0 200 OK\x0d\x0a")
TEXT("Server: Unreal File Server\x0d\x0a")
TEXT("Connection: close\x0d\x0a")
TEXT("Content-Type: text/html; charset=utf-8\x0d\x0a")
TEXT("Content-Length: %u\x0d\x0a\x0d\x0a%s"),
FCString::Strlen(ServerBanner),
ServerBanner
);
// very small data being sent, its fine to just send.
libwebsocket_write(Wsi,(unsigned char*)TCHAR_TO_ANSI(Buffer),FCStringAnsi::Strlen(TCHAR_TO_ANSI(Buffer)), LWS_WRITE_HTTP);
}
else
{
// client has asked for a file. ( only html/js files are served.)
// what type is being served.
FString FilePath = FPaths::GameDir() / TEXT("Binaries/HTML5") + FString((ANSICHAR*)In);
TCHAR Mime[512];
if ( FilePath.Contains(".js"))
{
FCStringWide::Strcpy(Mime,TEXT("application/javascript;charset=UTF-8"));
}
else
{
FCStringWide::Strcpy(Mime,TEXT("text/html;charset=UTF-8"));
}
UE_LOG(LogFileServer, Warning, TEXT("HTTP Serving file %s with mime %s "), *FilePath, (Mime));
FString AbsoluteFilePath = FPaths::ConvertRelativePathToFull(FilePath);
AbsoluteFilePath.ReplaceInline(TEXT("/"),TEXT("\\"));
// we are going to read the complete file in memory and then serve it in batches.
// rather than reading and sending in batches because Unreal NFS servers are not running in memory
// constrained env and the added complexity is not worth it.
TArray<uint8> FileData;
FFileHelper::LoadFileToArray(FileData, *AbsoluteFilePath, FILEREAD_Silent);
if (FileData.Num() == 0)
{
// umm. we didn't find file, we should tell the client that we couldn't find it.
// send 404.
char Header[]= "HTTP/1.1 404 Not Found\x0d\x0a"
"Server: Unreal File Server\x0d\x0a"
"Connection: close\x0d\x0a";
libwebsocket_write(Wsi,(unsigned char*)Header,FCStringAnsi::Strlen(Header), LWS_WRITE_HTTP);
// chug along, client will close the connection.
break;
}
// file up the header.
TCHAR Header[1024];
int Length = 0;
if (FilePath.Contains("gz"))
{
Length = FCString::Sprintf(Header,
TEXT("HTTP/1.1 200 OK\x0d\x0a")
TEXT("Server: Unreal File Server\x0d\x0a")
TEXT("Connection: close\x0d\x0a")
TEXT("Content-Type: %s \x0d\x0a")
TEXT("Content-Encoding: gzip\x0d\x0a")
TEXT("Content-Length: %u\x0d\x0a\x0d\x0a"),
Mime, FileData.Num());
}
else
{
Length = FCString::Sprintf(Header,
TEXT("HTTP/1.1 200 OK\x0d\x0a")
TEXT("Server: Unreal File Server\x0d\x0a")
TEXT("Connection: close\x0d\x0a")
TEXT("Content-Type: %s \x0d\x0a")
TEXT("Content-Length: %u\x0d\x0a\x0d\x0a"),
Mime, FileData.Num());
}
// make space for the whole file in our out buffer.
BufferInfo->Out.Append((uint8*)TCHAR_TO_ANSI(Header),Length);
BufferInfo->Out.Append(FileData);
// we need to write back to the client, queue up a write callback.
libwebsocket_callback_on_writable(Context, Wsi);
}
}
else
{
// we got a post request!, queue up a write callback.
libwebsocket_callback_on_writable(Context, Wsi);
}
break;
case LWS_CALLBACK_HTTP_BODY:
{
// post data is coming in, push it on to our incoming buffer.
UE_LOG(LogFileServer, Log, TEXT("Incoming HTTP Partial Body Size %d, total size %d"),Len, Len+ BufferInfo->In.Num());
BufferInfo->In.Append((uint8*)In,Len);
// we received some data - update time out.
libwebsocket_set_timeout(Wsi, NO_PENDING_TIMEOUT, 60);
}
break;
case LWS_CALLBACK_HTTP_BODY_COMPLETION:
{
// we have all the post data from the client.
// create archives and process them.
UE_LOG(LogFileServer, Log, TEXT("Incoming HTTP total size %d"), BufferInfo->In.Num());
FMemoryReader Reader(BufferInfo->In);
TArray<uint8> Writer;
FNetworkFileServerHttp::Process(Reader,Writer,Server);
// even if we have 0 data to push, tell the client that we don't any data.
ANSICHAR Header[1024];
int Length = FCStringAnsi::Sprintf(
(ANSICHAR*)Header,
"HTTP/1.1 200 OK\x0d\x0a"
"Server: Unreal File Server\x0d\x0a"
"Connection: close\x0d\x0a"
"Content-Type: application/octet-stream \x0d\x0a"
"Content-Length: %u\x0d\x0a\x0d\x0a",
Writer.Num()
);
// Add Http Header
BufferInfo->Out.Append((uint8*)Header,Length);
// Add Binary Data.
BufferInfo->Out.Append(Writer);
// we have enqueued data increase timeout and push a writable callback.
libwebsocket_set_timeout(Wsi, NO_PENDING_TIMEOUT, 60);
libwebsocket_callback_on_writable(Context, Wsi);
}
break;
case LWS_CALLBACK_CLOSED_HTTP:
// client went away or
//clean up.
BufferInfo->In.Empty();
BufferInfo->Out.Empty();
break;
case LWS_CALLBACK_PROTOCOL_DESTROY:
// we are going away.
break;
case LWS_CALLBACK_HTTP_WRITEABLE:
// get rid of superfluous write callbacks.
if ( BufferInfo == NULL )
break;
// we have data o send out.
if (BufferInfo->Out.Num())
{
int SentSize = libwebsocket_write(Wsi,(unsigned char*)BufferInfo->Out.GetData(),BufferInfo->Out.Num(), LWS_WRITE_HTTP);
// get rid of the data that has been sent.
BufferInfo->Out.RemoveAt(0,SentSize);
}
break;
default:
break;
}
return 0;
}
#endif