using System.Collections.Generic; using System; namespace MLAgents.SideChannels { /// /// Side channel for managing raw bytes of data. It is up to the clients of this side channel /// to interpret the messages. /// public class RawBytesChannel : SideChannel { List m_MessagesReceived = new List(); /// /// RawBytesChannel provides a way to exchange raw byte arrays between Unity and Python. /// /// The identifier for the RawBytesChannel. Must be /// the same on Python and Unity. public RawBytesChannel(Guid channelId) { ChannelId = channelId; } /// protected override void OnMessageReceived(IncomingMessage msg) { m_MessagesReceived.Add(msg.GetRawBytes()); } /// /// Sends the byte array message to the Python side channel. The message will be sent /// alongside the simulation step. /// /// The byte array of data to send to Python. public void SendRawBytes(byte[] data) { using (var msg = new OutgoingMessage()) { msg.SetRawBytes(data); QueueMessageToSend(msg); } } /// /// Gets the messages that were sent by python since the last call to /// GetAndClearReceivedMessages. /// /// a list of byte array messages that Python has sent. public IList GetAndClearReceivedMessages() { var result = new List(); result.AddRange(m_MessagesReceived); m_MessagesReceived.Clear(); return result; } /// /// Gets the messages that were sent by python since the last call to /// GetAndClearReceivedMessages. Note that the messages received will not /// be cleared with a call to GetReceivedMessages. /// /// a list of byte array messages that Python has sent. public IList GetReceivedMessages() { var result = new List(); result.AddRange(m_MessagesReceived); return result; } } }