Files
UnrealEngineUWP/Engine/Source/Runtime/NetworkFileSystem/Private/NetworkFileServerHttp.cpp
Chris Babcock dc647b9547 Copying //UE4/Dev-Mobile to //UE4/Main (Source: //UE4/Dev-Mobile @ 3600060)
#rb none
#lockdown nick.penwarden

==========================
MAJOR FEATURES + CHANGES
==========================

Change 3292215 on 2017/02/08 by Nick.Shin

	HTML5 emscripten: wasm and wbegl2 support

	- emscripten toolchain

	#jira UEPLAT-1437  Switch [to] web assembly
	#rb none

Change 3293994 on 2017/02/09 by Nick.Shin

	HTML5 emscripten: wasm and webgl2 support

	- OSX toolchain

	#jira UEPLAT-1437  Switch [to] web assembly
	#rb none

Change 3317951 on 2017/02/22 by Nick.Shin

	HTML5 emscripten: wasm & webgl2 support - RC1

	- emscripten toolchain

	WARNING: emscripten/incoming/source/include/libc/bit (the file) might need to be deleted first

	#jira UEMOB-263  Switch [to] web assembly
	#jira UEMOB-201  Support ES3 / WebGL2 in HTML5
	#rb none

Change 3318669 on 2017/02/23 by Nick.Shin

	HTML5 emscripten: wasm & webgl2 support - RC1

	- OSX toolchain

	#jira UEMOB-263  Switch [to] web assembly
	#jira UEMOB-201  Support ES3 / WebGL2 in HTML5
	#rb none

Change 3462146 on 2017/05/26 by Nick.Shin

	HTML5 - merge from Release-4.16 to Dev-Mobile

	#jira none
	#rb none
	#rnx

Change 3504996 on 2017/06/22 by Cosmin.Sulea

	UEMOB-362 - Add per-texture and per-format compression quality override settings
	#rb Dmitriy.Dyomin
	#jira UEMOB-362
	#codereview Dmitriy.Dyomin
	#codereview Jack.Porter

Change 3505056 on 2017/06/22 by Cosmin.Sulea

	Back out changelist 3504996 - due to errors generated in xboxOne, PS4 and Switch versions
	#rb none

Change 3508049 on 2017/06/23 by Nick.Shin

	HTML5 toolchain notes corrections

	#jira none
	#rb none
	#rnx

Change 3508663 on 2017/06/24 by Nick.Shin

	HTML5LaunchHelper.exe on linux - redo

	- it seems that i need to also check-in the exe and pdb file instead of having CIS make and checking-in them itself...
	- modified c# program to output a version number to help track which version of HTML5LaunchHelper is running...

	#jira UE-45302  HTML5LaunchHelper.exe hosts the files in the current working directory on Linux
	#rnx
	#rb none

Change 3509210 on 2017/06/26 by Dmitriy.Dyomin

	 ExposureScale will be applied during tonemap pass when MobileHDR is on
	#rb jack.porter
	#codereview Allan.Bentham

Change 3511058 on 2017/06/27 by Cosmin.Sulea

	UEMOB-362 - Add per-texture and per-format compression quality override settings - resubmitted
	#rb Dmitriy.Dyomin
	#jira UEMOB-362
	#codereview Dmitriy.Dyomin

Change 3511069 on 2017/06/27 by Jack.Porter

	PS4, XboxOne and Switch fixes for changes to ITextureFormat interface
	#rb Dmitriy.Dyomin
	#jira UEMOB-362

Change 3513028 on 2017/06/28 by Jack.Porter

	Merging //UE4/Dev-Main to Dev-Mobile (//UE4/Dev-Mobile)
	#rb None

Change 3517409 on 2017/06/30 by Jack.Porter

	Merging //UE4/Dev-Main to Dev-Mobile (//UE4/Dev-Mobile)
	#rb None

Change 3517730 on 2017/06/30 by Cosmin.Sulea

	UEMOB-328 - Improve handling of iOS signing key on remote Mac system keychain when using remote toolchain
	#rb Jack.Porter
	#jira UEMOB-328
	#codereview: peter.sauerbrei

Change 3517757 on 2017/06/30 by Cosmin.Sulea

	UE-46245 - Building with remote toolchain does not use Project Setting for iOS signing identity which can cause signing errors
	#rb Jack.Porter
	#jira UE-46245
	#codereview: peter.sauerbrei

Change 3518149 on 2017/06/30 by Adrian.Chelu

	UE-43035 Tilt axis for X and Z are not consistent between Android and iOS devices
	#rb Jack.Porter
	#jira UE-46245
	#codereview: Chris Babcock <chris.babcock@epicgames.com>

Change 3524242 on 2017/07/06 by Nick.Shin

	HTML5 - refraction shader

	note: this CL also contains fixes to webgl2 [float4 vs half2] and a [% vs Mod()] material custom function changes to some TM-ShaderModels shaders
	specifically: fixes to and similar with: DitherTemporalAA

	#jria UE-46434  No Refraction in QA Game TM-Shadermodels HTML5
	#rb none
	#rn
	#codereview jack.porter dmitriy.dyomin

Change 3535295 on 2017/07/13 by Allan.Bentham

	#jira UEMOB-390
	Add Android cpu stats.
	add 'stat AndroidCPU' to android's console spinner UI.
	increase GetCPUState's core count support to 16.
	#jira UE-45888
	Use cvar value to limit android cpu stat update rate.
	#rb none

Change 3535306 on 2017/07/13 by Allan.Bentham

	Add missing pragma once
	#rb none

Change 3537047 on 2017/07/13 by Ben.Marsh

	Fixing case of iOS directories, pt1

	#rb none

Change 3537051 on 2017/07/13 by Ben.Marsh

	Fixing case of iOS directories, pt2

	#rb none

Change 3537373 on 2017/07/14 by Allan.Bentham

	Add scope level android egl error verification.
	work around minor issue with invalid egl config property.
	#rb chris.babcock

Change 3541735 on 2017/07/18 by Allan.Bentham

	Add 'sustained performance mode' support for API 24+ devices.
	#jira UEMOB-386
	#rb chris.babcock

Change 3543001 on 2017/07/18 by Sorin.Gradinaru

	#jira UE-45766 Improved Virtual Keyboard cannot receive non-English characters.

	- for Android, add an native EditBox above the virtual keyboard to receive the text and pass it to the object from the slate

	#rb Chris.Babcock

Change 3554399 on 2017/07/25 by Nick.Shin

	STATS disabled for non multi-threaded platforms

	#jira UE-47485  ( Pri:1 - 4.18 )  Crash running Stat Command test in TM-Core on Firefox
	#rnx
	#rb none

Change 3554402 on 2017/07/25 by Nick.Shin

	STATS TaskGraph disabled for non multi-threaded platforms

	#jira UE-47486  ( Pri:1 - 4.18 )  QAGame hard locks on Firefox when triggering Task Graph Benchmark test
	#rb none
	#rnx

Change 3556957 on 2017/07/26 by Nick.Shin

	HTML5 - WASM enabled by default - part 1 -- commenting out asmjs stuff

	begin sunsetting ASM.JS

	note to self: CL#3462146 "backout" asmjs

	#jira UEMOB-416  WASM enabled by default
	#rnx
	#rb none

Change 3557654 on 2017/07/26 by Nick.Shin

	HTML5 - WASM enabled by default - part 2 -- remove asmjs code

	sunsetting ASM.JS

	note to self: CL#3462146 "backout" asmjs

	#jira UEMOB-416  WASM enabled by default
	#rn
	#rb none

Change 3557910 on 2017/07/27 by Jack.Porter

	Support Client configuration when packaging in the editor
	#jira UE-39973
	#rb Dmitriy.Dyomin

Change 3557917 on 2017/07/27 by Jack.Porter

	Missing file from CL 3557910
	#rb trivial

Change 3559642 on 2017/07/27 by Nick.Shin

	STATS TaskGraph disabled for non multi-threaded platforms

	- both "LockFree stress test" and "task graph benchmark" are disabled - no multi-threading for WASM exist yet (note: ASM.JS has been sunsetted)

	- stat command crash "fixed" - but, font size are totally broken - i can look at this (much) later...

	- new bug: physx will crash on "gc and level load stress test" -- please bug this as a new jira

	#jira UE-47486  ( Pri:1 - 4.18 )  QAGame hard locks on Firefox when triggering Task Graph Benchmark test
	#rb none
	#rnx

Change 3565656 on 2017/07/31 by Dmitriy.Dyomin

	Added a way to lock level position in Word Composition
	#jira UE-47713
	#rb none

Change 3565757 on 2017/08/01 by Dmitriy.Dyomin

	compile fix
	#rb none

Change 3567446 on 2017/08/01 by Chris.Babcock

	Allow addElement and addElements to only insert once with once="true" attribute in UPL
	#jira UE-47951
	#ue4
	#android
	#rb Peter.Sauerbrei

Change 3567592 on 2017/08/01 by Chris.Babcock

	Use absolute path for repositories for Gradle
	#jira UE-47952
	#ue4
	#android
	#rb Tim.Lincoln

Change 3568690 on 2017/08/02 by Chris.Babcock

	Removed warnings for once attribute in UPL
	#ue4
	#android
	#rb none

Change 3569975 on 2017/08/02 by Chris.Babcock

	Add <baseBuildGradleAdditions> to UPL to allow additions to the root-level build.gradle
	#jira UE-47995
	#ue4
	#android
	#rb Tim.Lincoln

Change 3570117 on 2017/08/02 by Chris.Babcock

	Add <setBoolFromPropertyContains> to UPL
	- sets bool to true if string list in ini matches contains attribute
	#jira UE-47996
	#ue4
	#android
	#rb Jack.Porter

Change 3571552 on 2017/08/03 by Chris.Babcock

	Removed unneeded settings.gradle file (generated)
	#jira UE-48041
	#ue4
	#android
	#rb none

Change 3572224 on 2017/08/04 by Dmitriy.Dyomin

	Better selection tracking in world composition
	#rb none

Change 3573662 on 2017/08/04 by Nick.Shin

	HTML5 remove PreLoadMap "feature" (was only available/used with HTML5)

	- asyncronous loads are not allowed during UEngine::LoadMap()
	- the files/code will be repurposed for pakfile CHUNK support

	#jira UEMOB-425  HTML5 streaming content investigation (part 1 of 2)
	#rn
	#rb none

Change 3574471 on 2017/08/07 by Dmitriy.Dyomin

	Export ULevelStreamingKismet::LoadLevelInstance function
	#rb none

Change 3576262 on 2017/08/08 by Dmitriy.Dyomin

	Fixed: widget clipping issues in world composition
	#rb none

Change 3576845 on 2017/08/08 by Nick.Shin

	set HTML5LaunchHelper application's icon to UE4.ico

	#jira UE-19225 HTML5LaunchHelper application does not have an unreal icon
	#rb none
	#rnx

Change 3578313 on 2017/08/09 by Dmitriy.Dyomin

	Added: an RHI call to invalidate/clear cached state, RHIInvalidateCachedState
	#jira UEMOB-435
	#rb jack.porter

Change 3578364 on 2017/08/09 by Dmitriy.Dyomin

	Vertex Fog is disabled on mobile by default. If scene uses vertex fog - Mobile preview and device will show on screen message: PROJECT HAS VERTEX FOG ON MOBILE DISABLED
	This saves about 90 instructions in VS and a few in PS
	#jira UEMOB-166
	#rb jack.porter

Change 3578703 on 2017/08/09 by Nick.Shin

	set HTML5LaunchHelper application's icon to UE4.ico

	forgot to check in exe and pdb file

	#jira UE-19225 HTML5LaunchHelper application does not have an unreal icon
	#rb none
	#rnx

Change 3578961 on 2017/08/09 by Peter.Sauerbrei

	deprecate IOS 8 as the minimum OS supported.
	#jira UEMOB-429
	#rb chris.babcock

Change 3579319 on 2017/08/09 by Peter.Sauerbrei

	fixes for compile errors with Xcode 9 beta 4
	#rb none

Change 3579356 on 2017/08/09 by Peter.Sauerbrei

	modified minimum IOS to build with
	#rb chris.babcock

Change 3579687 on 2017/08/09 by Chris.Babcock

	Fix GoogleVR Gradle packaging
	#jira UE-48239
	#ue4
	#android
	#rb none

Change 3579921 on 2017/08/10 by Dmitriy.Dyomin

	GitHub 3670 : More zoom levels for World Composition (300)
	#contributedby: user37337
	#jira UE-45977
	#3670
	#rb none

Change 3580576 on 2017/08/10 by Peter.Sauerbrei

	detection of iPad Pro 10.5 and IPad Pro 12.9 (2nd Gen)
	#rb chris.babcock

Change 3580611 on 2017/08/10 by Chris.Babcock

	Set online provider back to GooglePlay and remove forcing IAP permission (contributed by umerov1999)
	#jira UE-48185
	#PR #3876
	#ue4
	#android
	#rb Peter.Sauerbrei

Change 3582166 on 2017/08/11 by Nick.Shin

	nuke PLATFORM_HTML5_WIN32


	PLATFORM_HTML5_WIN32 code removal tested successfully with (force rebuild and repackaging):

	* Win64 server (WindowsServer)
	* Win64 client (WindowsNoEditor)
	* HTML5 client

	all playing together via websocket net driver (i've attached a screen shot of this in jira)


	code changes touches: physics, audio and main build files


	#jira UEMOB-433  Remove Win32 SDL "HTML5 Simulator" code
	#rb ben.marsh
	#rnx
	#codereview josh.adams
	#fyi ori.cohen, aaron.mclera

Change 3582474 on 2017/08/11 by Chris.Babcock

	Don't use V2 signing for Gear VR APKs
	#jira UE-48354
	#ue4
	#android
	#rb Peter.Sauerbrei

Change 3582614 on 2017/08/11 by Chris.Babcock

	Filter out unneeded architectures from APK for Gradle builds
	#jira UE-48355
	#ue4
	#android
	#rb Peter.Sauerbrei

Change 3582923 on 2017/08/11 by Nick.Shin

	backport release 4.17 to dev-mobile

	#jira none
	#rb none
	#rnx

Change 3582924 on 2017/08/11 by Nick.Shin

	FNetworkFileServerHttp - error gracefully when port is already in use

	#jira UE-46409  [CrashReport] Assertion on Mac: Could not create a libwebsocket - FNetworkFileServerHttp::Init()
	#rnx
	#rb none

Change 3582925 on 2017/08/11 by Nick.Shin

	HTML5 - turn off pak file compression in favor of gzip packages

	#jira UE-46729  HTML5 - on shipping builds - turn off pak file compression in favor of gzip packages
	#rn
	#rb none

Change 3583943 on 2017/08/14 by Cosmin.Sulea

	UEMOB-363 - second iteration - Project wide texture quality control by texture group
	#rb Dmitriy Dyomin
	#jira UEMOB-363

Change 3583967 on 2017/08/14 by Cosmin.Sulea

	Back out changelist 3583943
	#rb none

Change 3584121 on 2017/08/14 by Peter.Sauerbrei

	fix for mac compile failure
	#rb none

Change 3587877 on 2017/08/15 by Peter.Sauerbrei

	josh's suggested fix is not working for Xcode 8.3, so brute forcing for now
	#rb none

Change 3588612 on 2017/08/15 by Peter.Sauerbrei

	Xcode 9 project compatbility updates
	#rb chris.babcock
	#codereview michael.trepka

Change 3589223 on 2017/08/15 by Dmitriy.Dyomin

	Fixed: bNavigationAutoUpdateEnabled was not always working when reopeinig the map
	Fixed: Navigation Build was not clearing some mesh tiles when bNavigationAutoUpdateEnabled is enabled
	Fixed: Streaming out a level in editor was not always updating NavMesh debug draw
	#rb lukasz.furman

Change 3589900 on 2017/08/16 by Dmitriy.Dyomin

	Support vulkan validation layers on Android, only in Debug and Development configuration (requires r.Vulkan.EnableValidation=1)
	#codereview chris.babcock, rolando.caloca
	#rb none

Change 3590592 on 2017/08/16 by Nick.Shin

	HTML5 emscripten 1.37.19 OSX

	#jira UE-47813
	#rb none
	#rn HTML5 emscripten 1.37.19 OSX

Change 3590597 on 2017/08/16 by Nick.Shin

	HTML5 emscripten 1.37.19 Linux

	#jira UE-47813
	#rb none
	#rn HTML5 emscripten 1.37.19 Linux

Change 3590624 on 2017/08/16 by Nick.Shin

	HTML5 emscripten 1.37.19 toolchain

	#jira UE-47813
	#rb none
	#rn HTML5 emscripten 1.37.19 toolchain

Change 3591720 on 2017/08/16 by Chris.Babcock

	Enable Gradle by default and add button to accept Android SDK license to project settings
	#jira UE-48519
	#ue4
	#android
	#rb Tim.Lincoln
	#fyi Peter.Sauerbrei

Change 3591998 on 2017/08/16 by Chris.Babcock

	Fix nonunity build
	#ue4
	#android
	#rb none

Change 3592407 on 2017/08/17 by Nick.Shin

	HTML5 emscripten 1.37.19 Win64

	#jira UE-47813
	#rb none
	#rn HTML5 emscripten 1.37.19 Win64

Change 3592479 on 2017/08/17 by Nick.Shin

	HTML5 3rd Party Libs - compiled with emscripten 1.37.19

	#jira UE-47813
	#rb none
	#rn HTML5 3rd Party Libs - compiled with emscripten 1.37.19 toolchain

Change 3592480 on 2017/08/17 by Nick.Shin

	HTML5 emscripten 1.37.19 toolchain Epic edits

	as well as setting UE4 HTML c# scripts to use new toolchain

	#jira UE-47813
	#rb none
	#rn HTML5 emscripten 1.37.19 toolchain Epic edits

Change 3592481 on 2017/08/17 by Nick.Shin

	HTML5 remove old emscripten toolchain

	#jira UE-47813
	#rb none
	#rn HTML5 remove old emscripten toolchain

Change 3592485 on 2017/08/17 by Nick.Shin

	HTML5 undo CanUseXGE - this might be breaking CIS for HTML5 builds...

	#jira UE-47813
	#rb none
	#rnx

Change 3592549 on 2017/08/17 by Dmitriy.Dyomin

	Added GetDiskTotalAndFreeSpace for IOS and Android
	#jira UE-46479
	#codereview chris.babcock, peter.sauerbrei
	#rb none

Change 3594045 on 2017/08/17 by Peter.Sauerbrei

	comment about potential failure case in the remote tool chain
	#rb none

Change 3594342 on 2017/08/17 by Peter.Sauerbrei

	Merging

	//UE4/Main/...

	to //UE4/Dev-Mobile/...

	#rb none

Change 3594920 on 2017/08/17 by Peter.Sauerbrei

	fix for non-unity builds (accidentally merged something incorrectly)
	#rb none

Change 3595347 on 2017/08/17 by Chris.Babcock

	merge fixes for Android
	#ue4
	#android
	#rb Peter.Sauerbrei
	#lockdown Peter.Sauerbrei

Change 3595752 on 2017/08/17 by Chris.Babcock

	Update Facebook plugin to support Gradle
	#jira UE-48569
	#ue4
	#android
	#fyi Josh.Markiewicz
	#rb none
	#lockdown Peter.Sauerbrei

Change 3595849 on 2017/08/17 by Chris.Babcock

	Fix issue with libovrplatformloader.so for non armv7 targets
	#jira UE-48533
	#ue4
	#android
	#rb none
	#lockdown Peter.Sauerbrei

Change 3596419 on 2017/08/18 by Peter.Sauerbrei

	fix for Mac Editor build failure
	#rb none

Change 3597023 on 2017/08/18 by Peter.Sauerbrei

	fix for game editor build failure
	#rb none

Change 3597032 on 2017/08/18 by Peter.Sauerbrei

	fix for app bundle id in Info-Editor.plist
	#rb none

Change 3597034 on 2017/08/18 by Peter.Sauerbrei

	put back the info.plist, found the real problem
	#rb none

Change 3597197 on 2017/08/18 by Peter.Sauerbrei

	pull Info.plist from the build products
	#rb none

[CL 3600450 by Chris Babcock in Main branch]
2017-08-21 15:05:19 -04:00

497 lines
14 KiB
C++

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#include "NetworkFileServerHttp.h"
#include "NetworkFileServerConnection.h"
#include "NetworkFileSystemLog.h"
#include "NetworkMessage.h"
#include "HAL/RunnableThread.h"
#include "SocketSubsystem.h"
#include "Misc/Paths.h"
#include "Misc/FileHelper.h"
#include "IPAddress.h"
#include "Serialization/MemoryReader.h"
#if ENABLE_HTTP_FOR_NFS
class FNetworkFileServerClientConnectionHTTP : public FNetworkFileServerClientConnection
{
public:
FNetworkFileServerClientConnectionHTTP(const FNetworkFileDelegateContainer* NetworkFileDelegates, const TArray<ITargetPlatform*>& InActiveTargetPlatforms )
: FNetworkFileServerClientConnection( NetworkFileDelegates, 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 lws_protocols Protocols[] = {
/* first protocol must always be HTTP handler */
{
"http-only", // name
FNetworkFileServerHttp::CallBack_HTTP, // callback
sizeof(PerSessionData), // per_session_data_size
15 * 1024, // rx_buffer_size
0, // id
NULL
},
{
NULL, NULL, 0, 0, 0, NULL /* End of list */
}
};
//////////////////////////////////////////////////////////////////////////
FNetworkFileServerHttp::FNetworkFileServerHttp(
int32 InPort,
FNetworkFileDelegateContainer InNetworkFileDelegateContainer,
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..."));
NetworkFileDelegates = InNetworkFileDelegateContainer;
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
inline void lws_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 , lws_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 = lws_create_context(&Info);
Port = Info.port;
if (Context == NULL) {
UE_LOG(LogFileServer, Error, TEXT(" Could not create a libwebsocket content.\n Port : %d is already in use.\n Exiting...\n"), 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.
lws_service(Context, 10);
lws_callback_on_writable_all_protocol(Context, &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.
lws_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(&NetworkFileDelegates,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 lws_service
// return value of -1, closes the connection.
int FNetworkFileServerHttp::CallBack_HTTP(
struct lws *Wsi,
enum lws_callback_reasons Reason,
void *User,
void *In,
size_t Len)
{
struct lws_context *Context = lws_get_context(Wsi);
PerSessionData* BufferInfo = (PerSessionData*)User;
FNetworkFileServerHttp* Server = (FNetworkFileServerHttp*)lws_context_user(Context);
switch (Reason)
{
case LWS_CALLBACK_HTTP:
// hang on to socket even if there's no data for atleast 60 secs.
lws_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.
lws_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::ProjectDir() / 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.
TCHAR Buffer[1024];
TCHAR ServerBanner[] = TEXT("<HTML>Not Found</HTML>");
int x = FCString::Sprintf(
Buffer,
TEXT("HTTP/1.0 404 Not Found\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
);
lws_write(Wsi,(unsigned char*)TCHAR_TO_ANSI(Buffer),FCStringAnsi::Strlen(TCHAR_TO_ANSI(Buffer)), 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.
lws_callback_on_writable(Wsi);
}
}
else
{
// we got a post request!, queue up a write callback.
lws_callback_on_writable(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.
lws_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.
lws_set_timeout(Wsi, NO_PENDING_TIMEOUT, 60);
lws_callback_on_writable(Wsi);
}
break;
case LWS_CALLBACK_CLOSED_HTTP:
if ( BufferInfo == NULL )
break;
// 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 = lws_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