mirror of
https://github.com/jwetzell/QControlKit.git
synced 2026-08-03 06:27:59 +00:00
62 lines
1.6 KiB
C#
62 lines
1.6 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace SharpOSC
|
|
{
|
|
public static class SlipFrame
|
|
{
|
|
public static readonly byte END = 0xc0;
|
|
public static readonly byte ESC = 0xdb;
|
|
public static readonly byte ESC_END = 0xDC;
|
|
public static readonly byte ESC_ESC = 0xDD;
|
|
|
|
public static List<byte[]> Decode(byte[] data)
|
|
{
|
|
List<byte[]> messages = new List<byte[]>();
|
|
|
|
List<byte> buffer = new List<byte>();
|
|
for (int i = 0; i < data.Length; i++)
|
|
{
|
|
if (data[i] == END && buffer.Count > 0)
|
|
{
|
|
messages.Add(buffer.ToArray());
|
|
buffer.Clear();
|
|
}
|
|
else if (data[i] != END)
|
|
{
|
|
buffer.Add(data[i]);
|
|
}
|
|
}
|
|
|
|
return messages;
|
|
}
|
|
|
|
public static byte[] Encode(byte[] data)
|
|
{
|
|
List<byte> slipData = new List<byte>();
|
|
|
|
byte[] esc_end = { ESC, ESC_END };
|
|
byte[] esc_esc = { ESC, ESC_ESC };
|
|
byte[] end = { END };
|
|
|
|
int length = data.Length;
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
if (data[i] == END)
|
|
{
|
|
slipData.AddRange(esc_end);
|
|
}
|
|
else if (data[i] == ESC)
|
|
{
|
|
slipData.AddRange(esc_esc);
|
|
}
|
|
else
|
|
{
|
|
slipData.Add(data[i]);
|
|
}
|
|
}
|
|
slipData.AddRange(end);
|
|
return slipData.ToArray();
|
|
}
|
|
}
|
|
}
|