Skip to content

Commit

Permalink
Fix HandleWatchMessage (googleforgames#2977)
Browse files Browse the repository at this point in the history
* Fix HandleWatchMessage

* Additional fix for non-zero terminated strings.
  • Loading branch information
tvandijck authored Feb 22, 2023
1 parent fefd7c8 commit 140740b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions sdks/unreal/Agones/Source/Agones/Private/AgonesComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "WebSockets/Public/IWebSocket.h"
#include "WebSockets/Public/WebSocketsModule.h"

DEFINE_LOG_CATEGORY_STATIC(LogAgones, Log, Log);

UAgonesComponent::UAgonesComponent()
{
PrimaryComponentTick.bCanEverTick = false;
Expand Down Expand Up @@ -206,6 +208,7 @@ void UAgonesComponent::WatchGameServer(const FGameServerDelegate WatchDelegate)
!JsonObject->TryGetObjectField(TEXT("result"), ResultObject) ||
!ResultObject->IsValid())
{
UE_LOG(LogAgones, Error, TEXT("Failed to parse json: %s"), *JsonString);
return;
}

Expand All @@ -221,22 +224,22 @@ void UAgonesComponent::WatchGameServer(const FGameServerDelegate WatchDelegate)

void UAgonesComponent::HandleWatchMessage(const void* Data, SIZE_T Size, SIZE_T BytesRemaining)
{
if (BytesRemaining > 0)
if (BytesRemaining <= 0 && WatchMessageBuffer.IsEmpty())
{
WatchMessageBuffer.Append(UTF8_TO_TCHAR(static_cast<const UTF8CHAR*>(Data)), Size);
FUTF8ToTCHAR Message(static_cast<const UTF8CHAR*>(Data), Size);
DeserializeAndBroadcastWatch(FString(Message.Length(), Message.Get()));
return;
}

FString const Message = FString(Size, UTF8_TO_TCHAR(static_cast<const UTF8CHAR*>(Data)));

// If the LHS of FString + is empty, it just uses the RHS directly so there's no copy here with an empty buffer.
DeserializeAndBroadcastWatch(WatchMessageBuffer + Message);

// Faster to check and then empty vs blindly emptying - normal case is that the buffer is already empty
if (!WatchMessageBuffer.IsEmpty())
WatchMessageBuffer.Insert(static_cast<const UTF8CHAR*>(Data), Size, WatchMessageBuffer.Num());
if (BytesRemaining > 0)
{
WatchMessageBuffer.Empty();
return;
}

FUTF8ToTCHAR Message(WatchMessageBuffer.GetData(), WatchMessageBuffer.Num());
DeserializeAndBroadcastWatch(FString(Message.Length(), Message.Get()));
WatchMessageBuffer.Empty();
}

void UAgonesComponent::SetLabel(
Expand Down
2 changes: 1 addition & 1 deletion sdks/unreal/Agones/Source/Agones/Public/AgonesComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class AGONES_API UAgonesComponent final : public UActorComponent

TSharedPtr<IWebSocket> WatchWebSocket;

FString WatchMessageBuffer;
TArray<UTF8CHAR> WatchMessageBuffer;

TArray<FGameServerDelegate> WatchGameServerCallbacks;

Expand Down

0 comments on commit 140740b

Please sign in to comment.