Skip to content

SideChannel helpers #3596

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

Merged
merged 11 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,17 @@ public void RegisterSideChannel(SideChannel sideChannel)
"side channels of the same id.", channelId));
}

// Process any messages that we've already received for this channel ID.
var numMessages = m_CachedMessages.Count;
for (int i = 0; i < numMessages; i++)
{
var cachedMessage = m_CachedMessages.Dequeue();
if (channelId == cachedMessage.ChannelId)
{
sideChannel.OnMessageReceived(cachedMessage.Message);
using (var incomingMsg = new IncomingMessage(cachedMessage.Message))
{
sideChannel.OnMessageReceived(incomingMsg);
}
}
else
{
Expand Down Expand Up @@ -581,7 +585,10 @@ public static void ProcessSideChannelData(Dictionary<Guid, SideChannel> sideChan
var cachedMessage = m_CachedMessages.Dequeue();
if (sideChannels.ContainsKey(cachedMessage.ChannelId))
{
sideChannels[cachedMessage.ChannelId].OnMessageReceived(cachedMessage.Message);
using (var incomingMsg = new IncomingMessage(cachedMessage.Message))
{
sideChannels[cachedMessage.ChannelId].OnMessageReceived(incomingMsg);
}
}
else
{
Expand Down Expand Up @@ -618,7 +625,10 @@ public static void ProcessSideChannelData(Dictionary<Guid, SideChannel> sideChan
}
if (sideChannels.ContainsKey(channelId))
{
sideChannels[channelId].OnMessageReceived(message);
using (var incomingMsg = new IncomingMessage(message))
{
sideChannels[channelId].OnMessageReceived(incomingMsg);
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.IO;
using System;
using UnityEngine;

Expand All @@ -20,27 +19,21 @@ public EngineConfigurationChannel()
}

/// <inheritdoc/>
public override void OnMessageReceived(byte[] data)
public override void OnMessageReceived(IncomingMessage msg)
{
using (var memStream = new MemoryStream(data))
{
using (var binaryReader = new BinaryReader(memStream))
{
var width = binaryReader.ReadInt32();
var height = binaryReader.ReadInt32();
var qualityLevel = binaryReader.ReadInt32();
var timeScale = binaryReader.ReadSingle();
var targetFrameRate = binaryReader.ReadInt32();
var width = msg.ReadInt32();
var height = msg.ReadInt32();
var qualityLevel = msg.ReadInt32();
var timeScale = msg.ReadFloat32();
var targetFrameRate = msg.ReadInt32();

timeScale = Mathf.Clamp(timeScale, 1, 100);
timeScale = Mathf.Clamp(timeScale, 1, 100);

Screen.SetResolution(width, height, false);
QualitySettings.SetQualityLevel(qualityLevel, true);
Time.timeScale = timeScale;
Time.captureFramerate = 60;
Application.targetFrameRate = targetFrameRate;
}
}
Screen.SetResolution(width, height, false);
QualitySettings.SetQualityLevel(qualityLevel, true);
Time.timeScale = timeScale;
Time.captureFramerate = 60;
Application.targetFrameRate = targetFrameRate;
}
}
}
69 changes: 20 additions & 49 deletions com.unity.ml-agents/Runtime/SideChannels/FloatPropertiesChannel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System;
using System.Text;

namespace MLAgents.SideChannels
{
Expand Down Expand Up @@ -32,38 +30,40 @@ public class FloatPropertiesChannel : SideChannel
}

/// <inheritdoc/>
public override void OnMessageReceived(byte[] data)
public override void OnMessageReceived(IncomingMessage msg)
{
var kv = DeserializeMessage(data);
m_FloatProperties[kv.Key] = kv.Value;
if (m_RegisteredActions.ContainsKey(kv.Key))
{
m_RegisteredActions[kv.Key].Invoke(kv.Value);
}
var key = msg.ReadString();
var value = msg.ReadFloat32();

m_FloatProperties[key] = value;

Action<float> action;
m_RegisteredActions.TryGetValue(key, out action);
action?.Invoke(value);
}

/// <inheritdoc/>
public void SetProperty(string key, float value)
{
m_FloatProperties[key] = value;
QueueMessageToSend(SerializeMessage(key, value));
if (m_RegisteredActions.ContainsKey(key))
using (var msgOut = new OutgoingMessage())
{
m_RegisteredActions[key].Invoke(value);
msgOut.WriteString(key);
msgOut.WriteFloat32(value);
QueueMessageToSend(msgOut);
}

Action<float> action;
m_RegisteredActions.TryGetValue(key, out action);
action?.Invoke(value);
}

/// <inheritdoc/>
public float GetPropertyWithDefault(string key, float defaultValue)
{
if (m_FloatProperties.ContainsKey(key))
{
return m_FloatProperties[key];
}
else
{
return defaultValue;
}
float valueOut;
bool hasKey = m_FloatProperties.TryGetValue(key, out valueOut);
return hasKey ? valueOut : defaultValue;
}

/// <inheritdoc/>
Expand All @@ -77,34 +77,5 @@ public IList<string> ListProperties()
{
return new List<string>(m_FloatProperties.Keys);
}

static KeyValuePair<string, float> DeserializeMessage(byte[] data)
{
using (var memStream = new MemoryStream(data))
{
using (var binaryReader = new BinaryReader(memStream))
{
var keyLength = binaryReader.ReadInt32();
var key = Encoding.ASCII.GetString(binaryReader.ReadBytes(keyLength));
var value = binaryReader.ReadSingle();
return new KeyValuePair<string, float>(key, value);
}
}
}

static byte[] SerializeMessage(string key, float value)
{
using (var memStream = new MemoryStream())
{
using (var binaryWriter = new BinaryWriter(memStream))
{
var stringEncoded = Encoding.ASCII.GetBytes(key);
binaryWriter.Write(stringEncoded.Length);
binaryWriter.Write(stringEncoded);
binaryWriter.Write(value);
return memStream.ToArray();
}
}
}
}
}
101 changes: 101 additions & 0 deletions com.unity.ml-agents/Runtime/SideChannels/IncomingMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System.Collections.Generic;
using System;
using System.IO;
using System.Text;

namespace MLAgents.SideChannels
{
/// <summary>
/// Utility class for reading the data sent to the SideChannel.
/// </summary>
public class IncomingMessage : IDisposable
{
byte[] m_Data;
Stream m_Stream;
BinaryReader m_Reader;

/// <summary>
/// Construct an IncomingMessage from the byte array.
/// </summary>
/// <param name="data"></param>
public IncomingMessage(byte[] data)
{
m_Data = data;
m_Stream = new MemoryStream(data);
m_Reader = new BinaryReader(m_Stream);
}

/// <summary>
/// Read a boolan value from the message.
/// </summary>
/// <returns></returns>
public bool ReadBoolean()
{
return m_Reader.ReadBoolean();
}

/// <summary>
/// Read an integer value from the message.
/// </summary>
/// <returns></returns>
public int ReadInt32()
{
return m_Reader.ReadInt32();
}

/// <summary>
/// Read a float value from the message.
/// </summary>
/// <returns></returns>
public float ReadFloat32()
{
return m_Reader.ReadSingle();
}

/// <summary>
/// Read a string value from the message.
/// </summary>
/// <returns></returns>
public string ReadString()
{
var strLength = ReadInt32();
var str = Encoding.ASCII.GetString(m_Reader.ReadBytes(strLength));
return str;
}

/// <summary>
/// Reads a list of floats from the message. The length of the list is stored in the message.
/// </summary>
/// <returns></returns>
public IList<float> ReadFloatList()
{
var len = ReadInt32();
var output = new float[len];
for (var i = 0; i < len; i++)
{
output[i] = ReadFloat32();
}

return output;
}

/// <summary>
/// Gets the original data of the message. Note that this will return all of the data,
/// even if part of it has already been read.
/// </summary>
/// <returns></returns>
public byte[] GetRawBytes()
{
return m_Data;
}

/// <summary>
/// Clean up the internal storage.
/// </summary>
public void Dispose()
{
m_Reader?.Dispose();
m_Stream?.Dispose();
}
}
}
11 changes: 11 additions & 0 deletions com.unity.ml-agents/Runtime/SideChannels/IncomingMessage.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading