Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/plugin-scoped'
Browse files Browse the repository at this point in the history
  • Loading branch information
getnamo committed Nov 11, 2017
2 parents 104894e + e4d618b commit f17df08
Show file tree
Hide file tree
Showing 10 changed files with 563 additions and 230 deletions.
2 changes: 1 addition & 1 deletion SocketIOClient.uplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 1,
"VersionName": "0.6.9",
"VersionName": "0.7.0",
"FriendlyName": "Socket.IO Client",
"Description": "Real-time networking library Socket.IO Client usable from blueprints and c++.",
"Category": "Networking",
Expand Down
3 changes: 1 addition & 2 deletions Source/SIOJson/Private/SIOJConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,5 +461,4 @@ void USIOJConvert::ReplaceJsonValueNamesWithMap(TSharedPtr<FJsonValue>& JsonValu
ReplaceJsonValueNamesWithMap(Item, KeyMap);
}
}
}

}
9 changes: 9 additions & 0 deletions Source/SIOJson/Public/SIOJConvert.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ class SIOJSON_API USIOJConvert : public UObject
static void SetTrimmedKeyMapForStruct(TSharedPtr<FTrimmedKeyMap>& InMap, UStruct* Struct);
static void SetTrimmedKeyMapForProp(TSharedPtr<FTrimmedKeyMap>& InMap, UProperty* ArrayInnerProp);
static void ReplaceJsonValueNamesWithMap(TSharedPtr<FJsonValue>& InValue, TSharedPtr<FTrimmedKeyMap> KeyMap);

template<typename T>
static FString EnumToString(const FString& enumName, const T value)
{
UEnum* pEnum = FindObject<UEnum>(ANY_PACKAGE, *enumName);
return *(pEnum ? pEnum->GetNameStringByIndex(static_cast<uint8>(value)) : "null");
}
};


9 changes: 1 addition & 8 deletions Source/SocketIOClient/Private/SIOLambdaRunnable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,5 @@ FGraphEventRef FSIOLambdaRunnable::RunShortLambdaOnGameThread(TFunction< void()>

void FSIOLambdaRunnable::ShutdownThreads()
{
/*for (auto Runnable : Runnables)
{
if (Runnable != nullptr)
{
delete Runnable;
}
Runnable = nullptr;
}*/

}
117 changes: 92 additions & 25 deletions Source/SocketIOClient/Private/SocketIOClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,141 @@

#define LOCTEXT_NAMESPACE "FSocketIOClientModule"

//struct

class FSocketIOClientModule : public ISocketIOClientModule
{
public:
virtual FSocketIONative* NewValidNativePointer() override;
void ReleaseNativePointer(FSocketIONative* PointerToRelease) override;
//virtual TSharedPtr<FSocketIONative> NewValidNativePointer() override;
virtual TSharedPtr<FSocketIONative> NewValidNativePointer() override;
virtual TSharedPtr<FSocketIONative> ValidSharedNativePointer(FString SharedId) override;
void ReleaseNativePointer(TSharedPtr<FSocketIONative> PointerToRelease) override;

/** IModuleInterface implementation */
virtual void StartupModule() override;
virtual void ShutdownModule() override;

private:
FCriticalSection DeleteSection;
TArray<FSocketIONative*> ModulePointers;

//All native pointers manages by the plugin
TArray<TSharedPtr<FSocketIONative>> PluginNativePointers;

//Shared pointers, these will typically be alive past game world lifecycles
TMap<FString, TSharedPtr<FSocketIONative>> SharedNativePointers;
TSet<TSharedPtr<FSocketIONative>> AllSharedPtrs; //reverse lookup

FThreadSafeBool bHasActiveNativePointers;
};


void FSocketIOClientModule::StartupModule()
{
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module

ModulePointers.Empty();
PluginNativePointers.Empty();
}

void FSocketIOClientModule::ShutdownModule()
{
// This function may be called during shutdown to clean up your module. For modules that support dynamic reloading,
// we call this function before unloading the module.
FScopeLock Lock(&DeleteSection);

ModulePointers.Empty();

/*for (auto& Pointer : ModulePointers)
/*
Ensure we call release pointers, this will catch all the plugin scoped
connections pointers which don't get auto-released between game worlds.
*/
auto AllActivePointers = PluginNativePointers;
for (auto& Pointer : AllActivePointers)
{
if (Pointer)
ReleaseNativePointer(Pointer);
}
AllActivePointers.Empty();

//Wait for all pointers to release
float Elapsed = 0.f;
while (bHasActiveNativePointers)
{
FPlatformProcess::Sleep(0.01f);
Elapsed += 0.01f;

//if it takes more than 5 seconds, just quit
if (Elapsed > 5.f)
{
delete Pointer;
Pointer = nullptr;
UE_LOG(SocketIOLog, Warning, TEXT("FSocketIOClientModule::ShutdownModule force quit due to long wait to quit."));
break;
}
}*/
}

//Native pointers will be automatically released by uninitialize components
PluginNativePointers.Empty();
}

FSocketIONative* FSocketIOClientModule::NewValidNativePointer()
TSharedPtr<FSocketIONative> FSocketIOClientModule::NewValidNativePointer()
{
FSocketIONative* NewPointer = new FSocketIONative;
ModulePointers.Add(NewPointer);
TSharedPtr<FSocketIONative> NewPointer = MakeShareable(new FSocketIONative);

PluginNativePointers.Add(NewPointer);

bHasActiveNativePointers = true;

return NewPointer;
}

void FSocketIOClientModule::ReleaseNativePointer(FSocketIONative* PointerToRelease)
TSharedPtr<FSocketIONative> FSocketIOClientModule::ValidSharedNativePointer(FString SharedId)
{
PointerToRelease->OnConnectedCallback = [PointerToRelease](const FString& SessionId)
//Found key? return it
if (SharedNativePointers.Contains(SharedId))
{
return SharedNativePointers[SharedId];
}
//Otherwise request a new id and return it
else
{
//If we're still connected, disconnect us
if (PointerToRelease)
TSharedPtr<FSocketIONative> NewNativePtr = NewValidNativePointer();
SharedNativePointers.Add(SharedId, NewNativePtr);
AllSharedPtrs.Add(NewNativePtr);
return NewNativePtr;
}
}

void FSocketIOClientModule::ReleaseNativePointer(TSharedPtr<FSocketIONative> PointerToRelease)
{
//Remove shared ptr references if any
if (AllSharedPtrs.Contains(PointerToRelease))
{
AllSharedPtrs.Remove(PointerToRelease);
for (auto& Pair : SharedNativePointers)
{
PointerToRelease->SyncDisconnect();
if (Pair.Value == PointerToRelease)
{
SharedNativePointers.Remove(Pair.Key);
break;
}
}
};
}

//Release the pointer on the background thread
FSIOLambdaRunnable::RunLambdaOnBackGroundThread([PointerToRelease, this]
{
FScopeLock Lock(&DeleteSection);

if (PointerToRelease)
if (PointerToRelease.IsValid())
{
delete PointerToRelease;
//Disconnect
if (PointerToRelease->bIsConnected)
{
PointerToRelease->SyncDisconnect();
}

//Last operation took a while, ensure it's still true
if (PointerToRelease.IsValid())
{
//Ensure only one thread at a time removes from array
FScopeLock Lock(&DeleteSection);
PluginNativePointers.Remove(PointerToRelease);

//Update our active status
bHasActiveNativePointers = PluginNativePointers.Num() > 0;
}
}
});
}
Expand Down

0 comments on commit f17df08

Please sign in to comment.