File size: 2,456 Bytes
05c9ac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.Collections.Generic;
using System;

namespace Unity.MLAgents.SideChannels
{
    /// <summary>
    /// Side channel for managing raw bytes of data. It is up to the clients of this side channel
    /// to interpret the messages.
    /// </summary>
    public class RawBytesChannel : SideChannel
    {
        List<byte[]> m_MessagesReceived = new List<byte[]>();

        /// <summary>
        /// RawBytesChannel provides a way to exchange raw byte arrays between Unity and Python.
        /// </summary>
        /// <param name="channelId"> The identifier for the RawBytesChannel. Must be
        /// the same on Python and Unity.</param>
        public RawBytesChannel(Guid channelId)
        {
            ChannelId = channelId;
        }

        /// <inheritdoc/>
        protected override void OnMessageReceived(IncomingMessage msg)
        {
            m_MessagesReceived.Add(msg.GetRawBytes());
        }

        /// <summary>
        /// Sends the byte array message to the Python side channel. The message will be sent
        /// alongside the simulation step.
        /// </summary>
        /// <param name="data"> The byte array of data to send to Python.</param>
        public void SendRawBytes(byte[] data)
        {
            using (var msg = new OutgoingMessage())
            {
                msg.SetRawBytes(data);
                QueueMessageToSend(msg);
            }
        }

        /// <summary>
        /// Gets the messages that were sent by python since the last call to
        /// GetAndClearReceivedMessages.
        /// </summary>
        /// <returns> a list of byte array messages that Python has sent.</returns>
        public IList<byte[]> GetAndClearReceivedMessages()
        {
            var result = new List<byte[]>();
            result.AddRange(m_MessagesReceived);
            m_MessagesReceived.Clear();
            return result;
        }

        /// <summary>
        /// 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.
        /// </summary>
        /// <returns> a list of byte array messages that Python has sent.</returns>
        public IList<byte[]> GetReceivedMessages()
        {
            var result = new List<byte[]>();
            result.AddRange(m_MessagesReceived);
            return result;
        }
    }
}