Skip to content

Commit

Permalink
Buffer Unity SDK ReceiveData when watching GameServer configuration c…
Browse files Browse the repository at this point in the history
…hanges. (#3872)
  • Loading branch information
ZeroParticle authored Jun 21, 2024
1 parent 2c60194 commit 1f6b2a0
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions sdks/unity/AgonesSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,37 @@ private void OnRequestCompleted(AsyncOperation _)
private class GameServerHandler : DownloadHandlerScript
{
private WatchGameServerCallback callback;
private StringBuilder stringBuilder;

public GameServerHandler(WatchGameServerCallback callback)
{
this.callback = callback;
this.stringBuilder = new StringBuilder();
}

protected override bool ReceiveData(byte[] data, int dataLength)
{
string json = Encoding.UTF8.GetString(data);
var dictionary = (Dictionary<string, object>) Json.Deserialize(json);
var gameServer = new GameServer(dictionary["result"] as Dictionary<string, object>);
this.callback(gameServer);
string dataString = Encoding.UTF8.GetString(data);
this.stringBuilder.Append(dataString);

string bufferString = stringBuilder.ToString();
int newlineIndex;

while ((newlineIndex = bufferString.IndexOf('\n')) >= 0)
{
string fullLine = bufferString.Substring(0, newlineIndex);
try
{
var dictionary = (Dictionary<string, object>) Json.Deserialize(fullLine);
var gameServer = new GameServer(dictionary["result"] as Dictionary<string, object>);
this.callback(gameServer);
}
catch (Exception ignore) {} // Ignore parse errors
bufferString = bufferString.Substring(newlineIndex + 1);
}

stringBuilder.Clear();
stringBuilder.Append(bufferString);
return true;
}
}
Expand Down

0 comments on commit 1f6b2a0

Please sign in to comment.