* SlipFrame.cs:

* TCPClient.cs: Split SLIP frame stuff to separate class

* QControlKit.csproj: Bump version number
This commit is contained in:
2022-02-17 17:45:59 -06:00
parent 19794f40a7
commit ff498f83d0
3 changed files with 64 additions and 56 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
<Authors>Joel Wetzell</Authors>
<Version>1.0.0-devel</Version>
<PackOnBuild>true</PackOnBuild>
<PackageVersion>0.0.23</PackageVersion>
<PackageVersion>0.0.24</PackageVersion>
<PackageId>QControlKit</PackageId>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Summary>Unofficial C# port of Figure53's QLabKit.objc</Summary>
+60
View File
@@ -0,0 +1,60 @@
using System.Collections.Generic;
namespace SharpOSC
{
public static class SlipFrame
{
static readonly byte END = 0xc0;
static readonly byte ESC = 0xdb;
static readonly byte ESC_END = 0xDC;
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();
}
}
}
+3 -55
View File
@@ -32,15 +32,11 @@ namespace SharpOSC
private Queue<OscPacket> SendQueue = new Queue<OscPacket>();
private Thread receivingThread;
private Thread sendThread;
string _address;
byte END = 0xc0;
byte ESC = 0xdb;
byte ESC_END = 0xDC;
byte ESC_ESC = 0xDD;
SimpleTcpClient simpleClient;
@@ -64,7 +60,7 @@ namespace SharpOSC
private void DataReceived(object sender, DataReceivedEventArgs e)
{
List<byte[]> messages = SlipDecode(e.Data);
List<byte[]> messages = SlipFrame.Decode(e.Data);
for(int i = 0; i < messages.Count; i++)
{
OscPacket packet = OscPacket.GetPacket(messages[i]);
@@ -122,7 +118,7 @@ namespace SharpOSC
public void Send(byte[] message)
{
byte[] slipData = SlipEncode(message);
byte[] slipData = SlipFrame.Encode(message);
simpleClient.Send(slipData);
}
@@ -143,54 +139,6 @@ namespace SharpOSC
}
}
public byte[] SlipEncode(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();
}
public List<byte[]> SlipDecode(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 void Close()
{
Log.Information("[tcpclient] Close() was requested");