using System.Collections.Generic;
using System.IO;
using MLAPI.Serialization.Pooled;
namespace MLAPI.Profiling
{
///
/// The type of Tick
///
public enum TickType
{
///
/// Event tick. During EventTick NetworkVars are flushed etc
///
Event,
///
/// Receive tick. During ReceiveTick data is received from the transport
///
Receive,
///
/// Send tick. During Send data is sent from Transport queue
///
Send
}
///
/// A tick in used for the Profiler
///
public class ProfilerTick
{
///
/// The events that occured during this tick
///
public readonly List Events = new List();
///
/// Writes the current ProfilerTick to the stream
///
/// The stream containing
public void SerializeToStream(Stream stream)
{
using (var writer = PooledNetworkWriter.Get(stream))
{
writer.WriteUInt16Packed((ushort)Events.Count);
for (int i = 0; i < Events.Count; i++)
{
Events[i].SerializeToStream(stream);
}
}
}
///
/// Creates a ProfilerTick from data in the provided stream
///
/// The stream containing the ProfilerTick data
/// The ProfilerTick with data read from the stream
public static ProfilerTick FromStream(Stream stream)
{
var tick = new ProfilerTick();
using (var reader = PooledNetworkReader.Get(stream))
{
ushort count = reader.ReadUInt16Packed();
for (int i = 0; i < count; i++)
{
tick.Events.Add(TickEvent.FromStream(stream));
}
return tick;
}
}
internal void EndEvent()
{
for (int i = Events.Count - 1; i >= 0; i--)
{
if (!Events[i].Closed)
{
Events[i].Closed = true;
return;
}
}
}
internal void StartEvent(TickType type, uint bytes, string channelName, string messageType)
{
var tickEvent = new TickEvent()
{
Bytes = bytes,
ChannelName = string.IsNullOrEmpty(channelName) ? "NONE" : channelName,
MessageType = string.IsNullOrEmpty(messageType) ? "NONE" : messageType,
EventType = type,
Closed = false
};
Events.Add(tickEvent);
}
///
/// The type of tick
///
public TickType Type;
///
/// The frame the tick executed on
///
public int Frame;
///
/// The id of the tick
///
public int EventId;
///
/// The amount of bytes that were sent and / or received during this tick
///
public uint Bytes
{
get
{
uint bytes = 0;
for (int i = 0; i < Events.Count; i++)
{
bytes += Events[i].Bytes;
}
return bytes;
}
}
}
///
/// A event that can occur during a Event
///
public class TickEvent
{
///
/// The type of evenmt
///
public TickType EventType;
///
/// The amount of bytes sent or received
///
public uint Bytes;
///
/// The name of the channel
///
public string ChannelName;
///
/// The message type
///
public string MessageType;
///
/// Whether or not the event is closed
///
public bool Closed;
///
/// Writes the TickEvent data to the stream
///
/// The stream to write the TickEvent data to
public void SerializeToStream(Stream stream)
{
using (var writer = PooledNetworkWriter.Get(stream))
{
writer.WriteByte((byte)EventType);
writer.WriteUInt32Packed(Bytes);
writer.WriteStringPacked(ChannelName);
writer.WriteStringPacked(MessageType);
writer.WriteBool(Closed);
}
}
///
/// Creates a TickEvent from data in the provided stream
///
/// The stream containing the TickEvent data
/// The TickEvent with data read from the stream
public static TickEvent FromStream(Stream stream)
{
using (var reader = PooledNetworkReader.Get(stream))
{
var tickEvent = new TickEvent
{
EventType = (TickType)reader.ReadByte(),
Bytes = reader.ReadUInt32Packed(),
ChannelName = reader.ReadStringPacked(),
MessageType = reader.ReadStringPacked(),
Closed = reader.ReadBool()
};
return tickEvent;
}
}
}
}