Shared Memory Feature
I have added a shared memory protocol implementation to my custom UE4 library, ObjectDeliverer.
This allows data transfer using shared memory in addition to the previously implemented TCP/IP and UDP. However, shared memory is Windows Only…
How to Use
Since it’s implemented as a protocol for ObjectDeliverer, you can use it simply by switching the protocol passed to the ObjectDelivererManager’s Start method.
deliverer->Start(UProtocolFactory::CreateProtocolSharedMemory("memory_test", 1024),
UPacketRuleFactory::CreatePacketRuleSizeBody());
Pass the shared memory name and memory size to CreateProtocolSharedMemory
.
These two values must be the same as the counterpart exchanging data.
Specification
To use it as a data send/receive protocol, the content of the created shared memory is polled periodically. However, accessing the data every time increases processing load, so it’s handled with the following mechanism:
Block | Size (byte) | Content |
---|---|---|
Counter | 1 | Incremented each time data is written |
Size | 4 | Size of the data part |
Data | n | The written data |
During periodic polling, it first reads the first byte of the shared memory and checks if the counter value differs from the previous read.
If it differs, it determines that new data has been written, reads the next 4 bytes to confirm the data size, and then reads the data body.
Therefore, when exchanging data using shared memory with a process not using ObjectDeliverer, it needs to conform to this specification.
Sample
Using the shared memory protocol created this time, I made a sample like the movie below.
ObjectDelivererの共有メモリのテスト題材作成中。
— ayuma (@ayuma_x) January 29, 2019
WPFアプリが毎フレーム自分のウィンドウを共有メモリに書き込んで、それをUE4側が読み込みテクスチャを更新してる。#ObjectDeliverer #UE4 #UE4Study pic.twitter.com/uWY2QhHnYP
A WPF application writes its window display content to shared memory every frame, and the UE4 side receives the data using ObjectDeliverer.
Using the received data (pixel buffer), it updates the texture content, mirroring the WPF side on the UE4 display side.
Since it’s shared memory, it can only be used if both applications are on the same machine, but at a glance, it seems to synchronize without delay.
For reference, the WPF app created here is ↓.
Issue
When mirroring as described above for a long time, a phenomenon occurs where it crashes due to hitting the !IsUnreachable()
check in UObject::ProcessEvent
during shared memory reading.
It’s unusable unless this is fixed.
As a temporary fix, removing all uses of BlueprintNativeEvent
resolved the issue.
However, this prevents overriding in Blueprint, so I will consider that area again.