File size: 3,126 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | using System.Collections.Generic;
using System;
using System.IO;
using System.Text;
namespace Unity.MLAgents.SideChannels
{
/// <summary>
/// Utility class for forming the data that is sent to the SideChannel.
/// </summary>
public class OutgoingMessage : IDisposable
{
BinaryWriter m_Writer;
MemoryStream m_Stream;
/// <summary>
/// Create a new empty OutgoingMessage.
/// </summary>
public OutgoingMessage()
{
m_Stream = new MemoryStream();
m_Writer = new BinaryWriter(m_Stream);
}
/// <summary>
/// Clean up the internal storage.
/// </summary>
public void Dispose()
{
m_Writer?.Dispose();
m_Stream?.Dispose();
}
/// <summary>
/// Write a boolean value to the message.
/// </summary>
/// <param name="b"></param>
public void WriteBoolean(bool b)
{
m_Writer.Write(b);
}
/// <summary>
/// Write an interger value to the message.
/// </summary>
/// <param name="i"></param>
public void WriteInt32(int i)
{
m_Writer.Write(i);
}
/// <summary>
/// Write a float values to the message.
/// </summary>
/// <param name="f"></param>
public void WriteFloat32(float f)
{
m_Writer.Write(f);
}
/// <summary>
/// Write a string value to the message.
/// </summary>
/// <param name="s"></param>
public void WriteString(string s)
{
var stringEncoded = Encoding.ASCII.GetBytes(s);
m_Writer.Write(stringEncoded.Length);
m_Writer.Write(stringEncoded);
}
/// <summary>
/// Write a list or array of floats to the message.
/// </summary>
/// <param name="floatList"></param>
public void WriteFloatList(IList<float> floatList)
{
WriteInt32(floatList.Count);
foreach (var f in floatList)
{
WriteFloat32(f);
}
}
/// <summary>
/// Overwrite the message with a specific byte array.
/// </summary>
/// <param name="data"></param>
public void SetRawBytes(byte[] data)
{
// Reset first. Set the length to zero so that if there's more data than we're going to
// write, we don't have any of the original data.
m_Stream.Seek(0, SeekOrigin.Begin);
m_Stream.SetLength(0);
// Then append the data. Increase the capacity if needed (but don't shrink it).
m_Stream.Capacity = (m_Stream.Capacity < data.Length) ? data.Length : m_Stream.Capacity;
m_Stream.Write(data, 0, data.Length);
}
/// <summary>
/// Read the byte array of the message.
/// </summary>
/// <returns></returns>
internal byte[] ToByteArray()
{
return m_Stream.ToArray();
}
}
}
|