Download

Multithreading

Hello! I have to implement a networking component for my game that is to receive constant data stream trough UDP. The issue here is that there is little to no information on multithreading. All I have is the engine source (I am not too good with reading tons of code like that yet), the documentation that explains FRunnable and FRunnableThread and a single tutorial by Rama. No synchronization details on the classes, nothing. Is there any source of information on the topic other than the ones that I listed?

That’s another area. As I’m kind of in the same learning process as you. You have to learn about networking now and the terms. just like a class members and methods now you go study networks and learn the terms

I have implemented the UDP classes and they work. I have tested them already. I have also recreated the concept in C#, but the UE4 way is rather complicated. No tutorials, not much documentation and with their custom wrappers it makes things almost impossible for people that don’t have experience with going and learning trough the source code itself.

Rama actually wrote that he might will look into exactly that problem as soon as he has enough time for it :slight_smile:

If anyone could provide a satisfying solution for this, then its him.
I really look forward to it, also to the code which is available to learn :smiley:

I have implemented threads for a custom plugin just yesterday, so here is an example.

I have not tested it yet though, so be warned!!


class ALTERNATEPHYSICS_API APPhysicsEngineThread : public FRunnable
{
public:
	typedef std::shared_ptr<APPhysicsEngineThread> Pointer;
protected:
	bool bShouldRun;

	double timePrevious;
	double timeCurrent;
	float deltaTime;
public:
	APPhysicsEngineThread();

	void UpdateDeltaTime();
	bool Init() override;
	uint32 Run() override;
	void Stop() override;

};



APPhysicsEngineThread::APPhysicsEngineThread()
{

}

bool APPhysicsEngineThread::Init()
{
	bShouldRun = true;

	timePrevious = FPlatformTime::Seconds();
	timeCurrent = FPlatformTime::Seconds();

	return true;
}
void APPhysicsEngineThread::UpdateDeltaTime()
{
	timePrevious = timeCurrent;
	timeCurrent = FPlatformTime::Seconds();

	deltaTime = (float)(timeCurrent - timePrevious);
}
uint32 APPhysicsEngineThread::Run()
{
	while (bShouldRun == true)
	{
		// just pause it a view milliseconds
		FPlatformProcess::Sleep(0.005f);
	}
	FPlatformTime::Seconds();

	return 0;
}
void APPhysicsEngineThread::Stop()
{
	bShouldRun = false;
}



APPhysicsEngineThread::Pointer ptrPhysicsEngineThread = APPhysicsEngineThread::Pointer(new APPhysicsEngineThread());

FRunnableThread* ptrThread = FRunnableThread::Create(ptrPhysicsEngineThread.get(), TEXT("AlteratePhysicsThread"));

I believe its a little more complicated then this in order to get something like an UDP listener Thread-Safe :wink:

I don’t quite understand ?

You create a seperate thread and a data structure that holds all you incoming network messages. This data structure is locked by a FCriticalSection object and your UDP thread just reads incoming pakets and writes them into this data structure.
You can make this system that works in the background available to blueprints easily. Below is a piece of code that should give you and idea on how this works. I have done something similar for our backend server connection.



#pragma once

#include "Engine.h"
#include "Networking.h"
//#include "BlueprintFunctionLibrary.generated.h"

#include "ASNetworkBP.generated.h"

UCLASS()
class ALTERNATESERVER_API UASNetworkBP : public UBlueprintFunctionLibrary
{
	GENERATED_UCLASS_BODY()

public:
	UFUNCTION(BlueprintCallable, meta=(DisplayName = "ASNetwork::Startup" ), Category = "Alternate|Server")
		static void Startup();

	UFUNCTION(BlueprintCallable, meta = (DisplayName = "ASNetwork::Shutdown"), Category = "Alternate|Server")
		static void Shutdown();

	UFUNCTION(BlueprintCallable, meta = (DisplayName = "ASNetwork::Update"), Category = "Alternate|Server")
		static void Update(float DeltaTime);

};

Startup() sets up your network code
Shutdown() closes all connections