Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer Unity SDK ReceiveData when watching for configuration changes #3872

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the input look like for this method? Is all the data expected to be on one line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fullLine should always be a single JSON message on a single line. grpc-gateway turns streams into newline delineated JSON messages and JSON encoding will escape any newlines in the data, so the only newlines we encounter will be the ones intentionally sent to break full JSON messages.

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
Loading