Skip to content

Commit 06beb1c

Browse files
JasonAtClockworkkistz
authored andcommitted
Unreal SDK failing to handle fragmented messages (clockworklabs#4002)
# Description of Changes Updated the handler from OnRawMessage to OnBinaryMessage as OnRawMessage seems to have a bug that won't report the BytesRemaining for incomplete messages. OnBinaryMessage properly returns a bool flag like how the C# implementation works. # API and ABI breaking changes N/A # Expected complexity level and risk 2 - Underlying framework change on how messages are handled # Testing Blackholio was no longer working and is back in working order; however Unreal test framework has been broken due to the work on Result<> as the companion PR has not been merged. - [x] Rebuilt and ran Unreal Blackholio
1 parent 4d065f2 commit 06beb1c

2 files changed

Lines changed: 17 additions & 22 deletions

File tree

sdks/unreal/src/SpacetimeDbSdk/Source/SpacetimeDbSdk/Private/Connection/Websocket.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void UWebsocketManager::Connect(const FString& ServerUrl)
8181
WebSocket->OnConnected().AddUObject(this, &UWebsocketManager::HandleConnected);
8282
WebSocket->OnConnectionError().AddUObject(this, &UWebsocketManager::HandleConnectionError);
8383
WebSocket->OnMessage().AddUObject(this, &UWebsocketManager::HandleMessageReceived);
84-
WebSocket->OnRawMessage().AddUObject(this, &UWebsocketManager::HandleBinaryMessageReceived);
84+
WebSocket->OnBinaryMessage().AddUObject(this, &UWebsocketManager::HandleBinaryMessageReceived);
8585
WebSocket->OnClosed().AddUObject(this, &UWebsocketManager::HandleClosed);
8686

8787
UE_LOG(LogTemp, Log, TEXT("UWebsocketManager::Connect: Connecting to %s..."), *ServerUrl);
@@ -173,7 +173,7 @@ void UWebsocketManager::HandleMessageReceived(const FString& Message)
173173
OnMessageReceived.Broadcast(Message);
174174
}
175175

176-
void UWebsocketManager::HandleBinaryMessageReceived(const void* Data, SIZE_T Size, SIZE_T BytesRemaining)
176+
void UWebsocketManager::HandleBinaryMessageReceived(const void* Data, SIZE_T Size, bool bIsLastFragment)
177177
{
178178
if (Size == 0)
179179
{
@@ -183,30 +183,25 @@ void UWebsocketManager::HandleBinaryMessageReceived(const void* Data, SIZE_T Siz
183183
// Handle binary messages, which may be fragmented
184184
const uint8* Bytes = static_cast<const uint8*>(Data);
185185

186-
if (IncompleteMessage.Num() > 0 && !bAwaitingBinaryFragments)
187-
{
188-
UE_LOG(LogTemp, Error, TEXT("Received binary fragment while previous data pending"));
189-
}
190-
191-
// Append new incoming bytes to any incomplete message
186+
// Append this fragment to our buffer
192187
IncompleteMessage.Append(Bytes, Size);
193188

194-
if (BytesRemaining > 0)
189+
// If this is the last fragment, we have the complete message
190+
if (bIsLastFragment)
191+
{
192+
// We have the complete message
193+
TArray<uint8> MessageBytes = IncompleteMessage;
194+
IncompleteMessage.Reset();
195+
bAwaitingBinaryFragments = false;
196+
197+
// Forward the complete binary payload to listeners.
198+
OnBinaryMessageReceived.Broadcast(MessageBytes);
199+
}
200+
else
195201
{
196-
// Still expecting more fragments
202+
// More fragments are coming
197203
bAwaitingBinaryFragments = true;
198-
return;
199204
}
200-
201-
// Final fragment received, reset and process
202-
bAwaitingBinaryFragments = false;
203-
204-
TArray<uint8> MessageBytes = IncompleteMessage;
205-
IncompleteMessage.Reset();
206-
207-
// Forward the complete binary payload to listeners.
208-
OnBinaryMessageReceived.Broadcast(MessageBytes);
209-
210205
}
211206

212207
void UWebsocketManager::HandleClosed(int32 StatusCode, const FString& Reason, bool bWasClean)

sdks/unreal/src/SpacetimeDbSdk/Source/SpacetimeDbSdk/Public/Connection/Websocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class SPACETIMEDBSDK_API UWebsocketManager : public UObject
106106
/** Handler for incoming text messages */
107107
void HandleMessageReceived(const FString& Message);
108108
/** Handler for incoming binary messages */
109-
void HandleBinaryMessageReceived(const void* Data, SIZE_T Size, SIZE_T BytesRemaining);
109+
void HandleBinaryMessageReceived(const void* Data, SIZE_T Size, bool bIsLastFragment);
110110
/** Handler for socket close */
111111
void HandleClosed(int32 StatusCode, const FString& Reason, bool bWasClean);
112112

0 commit comments

Comments
 (0)