mirror of
https://github.com/jwetzell/qController.git
synced 2026-08-06 16:03:48 +00:00
rever to old TCP method with new decode logic
This commit is contained in:
@@ -13,7 +13,6 @@
|
||||
<package id="Splat" version="14.1.17" targetFramework="monoandroid10.0" />
|
||||
<package id="Square.OkHttp3" version="4.9.2.1" targetFramework="monoandroid10.0" />
|
||||
<package id="Square.OkIO" version="2.10.0.1" targetFramework="monoandroid10.0" />
|
||||
<package id="SuperSimpleTcp" version="3.0.5" targetFramework="monoandroid12.0" />
|
||||
<package id="System.AppContext" version="4.3.0" targetFramework="monoandroid90" />
|
||||
<package id="System.Collections" version="4.3.0" targetFramework="monoandroid90" />
|
||||
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="monoandroid90" />
|
||||
|
||||
@@ -468,9 +468,6 @@
|
||||
<Reference Include="Xamarin.AndroidX.Concurrent.Futures">
|
||||
<HintPath>..\packages\Xamarin.AndroidX.Concurrent.Futures.1.1.0.5\lib\monoandroid90\Xamarin.AndroidX.Concurrent.Futures.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="SuperSimpleTcp">
|
||||
<HintPath>..\packages\SuperSimpleTcp.3.0.5\lib\netstandard2.1\SuperSimpleTcp.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="MainActivity.cs" />
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="xamarinios10" />
|
||||
<package id="Serilog" version="2.12.0" targetFramework="xamarinios10" />
|
||||
<package id="Serilog.Sinks.Xamarin" version="1.0.0" targetFramework="xamarinios10" />
|
||||
<package id="SuperSimpleTcp" version="3.0.5" targetFramework="xamarinios10" />
|
||||
<package id="System.AppContext" version="4.3.0" targetFramework="xamarinios10" />
|
||||
<package id="System.Collections" version="4.3.0" targetFramework="xamarinios10" />
|
||||
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="xamarinios10" />
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
<DefineConstants>DEBUG;ENABLE_TEST_CLOUD;</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<CodesignKey>iPhone Developer: Joel Wetzell (5E8CU66YWB)</CodesignKey>
|
||||
<CodesignKey>iPhone Developer</CodesignKey>
|
||||
<MtouchDebug>true</MtouchDebug>
|
||||
<MtouchNoSymbolStrip>true</MtouchNoSymbolStrip>
|
||||
<MtouchFastDev>true</MtouchFastDev>
|
||||
@@ -30,7 +30,7 @@
|
||||
<MtouchArch>x86_64</MtouchArch>
|
||||
<MtouchHttpClientHandler>HttpClientHandler</MtouchHttpClientHandler>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<CodesignProvision>qController Dev</CodesignProvision>
|
||||
<CodesignProvision>Automatic</CodesignProvision>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhone' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@@ -152,9 +152,6 @@
|
||||
<HintPath>..\packages\Xamarin.Forms.5.0.0.2515\lib\Xamarin.iOS10\Xamarin.Forms.Platform.iOS.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Numerics" />
|
||||
<Reference Include="SuperSimpleTcp">
|
||||
<HintPath>..\packages\SuperSimpleTcp.3.0.5\lib\netstandard2.1\SuperSimpleTcp.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="mscorlib" />
|
||||
<Reference Include="System.Numerics.Vectors" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -5,7 +5,6 @@ using System.Text;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using Serilog;
|
||||
using SuperSimpleTcp;
|
||||
|
||||
namespace SharpOSC
|
||||
{
|
||||
@@ -20,8 +19,6 @@ namespace SharpOSC
|
||||
|
||||
public class TCPClient
|
||||
{
|
||||
private ILogger _log = Log.Logger.ForContext<TCPClient>();
|
||||
|
||||
public int Port
|
||||
{
|
||||
get { return _port; }
|
||||
@@ -35,10 +32,10 @@ namespace SharpOSC
|
||||
public delegate void MessageReceivedHandler(object source, MessageEventArgs args);
|
||||
public event MessageReceivedHandler MessageReceived;
|
||||
|
||||
private Thread receivingThread;
|
||||
|
||||
string _address;
|
||||
|
||||
SimpleTcpClient tcpClient;
|
||||
|
||||
TcpClient client;
|
||||
|
||||
|
||||
public TCPClient(string address, int port)
|
||||
@@ -49,81 +46,28 @@ namespace SharpOSC
|
||||
|
||||
public bool Connect()
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
tcpClient = new SimpleTcpClient(Address, Port);
|
||||
tcpClient.Events.Connected += ClientConnected;
|
||||
tcpClient.Events.DataReceived += DataReceived;
|
||||
tcpClient.Events.Disconnected += ClientDisconneted;
|
||||
tcpClient.Logger += TCPLog;
|
||||
tcpClient.Connect();
|
||||
client = new TcpClient(Address, Port);
|
||||
receivingThread = new Thread(ReceiveLoop);
|
||||
receivingThread.Start();
|
||||
|
||||
Log.Debug($"[tcpclient] connected to <{Address}:{Port}>");
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.Error(e.Message);
|
||||
Log.Error(e.Message);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void TCPLog(string obj)
|
||||
{
|
||||
_log.Verbose($"{obj}");
|
||||
}
|
||||
|
||||
private void ClientDisconneted(object sender, ConnectionEventArgs e)
|
||||
{
|
||||
_log.Verbose($"{e.IpPort} client disconnected: {e.Reason}");
|
||||
Close();
|
||||
}
|
||||
|
||||
private void DataReceived(object sender, DataReceivedEventArgs e)
|
||||
{
|
||||
|
||||
_log.Verbose($"Raw Data Received contents: {Encoding.UTF8.GetString(e.Data.Array, 0, e.Data.Count)}");
|
||||
_log.Verbose($"Raw Data Received size: {e.Data.Count}");
|
||||
List<byte[]> messages = SlipFrame.Decode(e.Data.Array);
|
||||
_log.Verbose($"Slip decoded {messages.Count} osc messages");
|
||||
foreach (var message in messages)
|
||||
{
|
||||
_log.Verbose($"Raw message contents: {Encoding.UTF8.GetString(message, 0, message.Length)}");
|
||||
try
|
||||
{
|
||||
OscPacket packet = OscPacket.GetPacket(message);
|
||||
OscMessage responseMessage = (OscMessage)packet;
|
||||
if (packet == null)
|
||||
{
|
||||
_log.Error("packet is null");
|
||||
}
|
||||
|
||||
if (responseMessage == null)
|
||||
{
|
||||
_log.Error("responeMessage is null");
|
||||
}
|
||||
|
||||
_log.Debug($"OSC Message Received: {responseMessage.Address}");
|
||||
OnMessageReceived(responseMessage);
|
||||
_log.Debug($"After OnMessageReceived Event");
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Error($"Exception parsing OSC message: {ex.ToString()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ClientConnected(object sender, ConnectionEventArgs e)
|
||||
{
|
||||
_log.Debug($"connected to <{Address}:{Port}>");
|
||||
}
|
||||
|
||||
public void Send(byte[] message)
|
||||
{
|
||||
byte[] slipData = SlipFrame.Encode(message);
|
||||
tcpClient.Send(slipData.ToArray());
|
||||
NetworkStream netStream = client.GetStream();
|
||||
netStream.Write(slipData.ToArray(), 0, slipData.ToArray().Length);
|
||||
}
|
||||
|
||||
public void Send(OscPacket packet)
|
||||
@@ -136,25 +80,75 @@ namespace SharpOSC
|
||||
{
|
||||
get
|
||||
{
|
||||
if (tcpClient == null)
|
||||
if (client == null)
|
||||
return false;
|
||||
else
|
||||
return tcpClient.IsConnected;
|
||||
return client.Connected;
|
||||
}
|
||||
}
|
||||
|
||||
public void ReceiveLoop()
|
||||
{
|
||||
while (client != null && client.Connected)
|
||||
{
|
||||
Receive();
|
||||
}
|
||||
//Log.Debug("[tcpclient] - ReceiveLoop has exited");
|
||||
}
|
||||
|
||||
public void Receive()
|
||||
{
|
||||
Random random = new Random();
|
||||
int num = random.Next(1000);
|
||||
try
|
||||
{
|
||||
NetworkStream netStream = client.GetStream();
|
||||
netStream.ReadTimeout = 250;
|
||||
List<byte> responseData = new List<byte>();
|
||||
if (netStream.CanRead)
|
||||
{
|
||||
//var watch = System.Diagnostics.Stopwatch.StartNew();
|
||||
byte[] buffer = new byte[256];
|
||||
|
||||
int bytesRead = 0;
|
||||
int reads = 0;
|
||||
do
|
||||
{
|
||||
bytesRead = netStream.Read(buffer, 0, buffer.Length);
|
||||
responseData.AddRange(buffer);
|
||||
reads += 1;
|
||||
Thread.Sleep(1);
|
||||
//Log.Debug("Thread " + num + ": Bytes read: " + bytesRead + " - " + Encoding.UTF8.GetString(buffer));
|
||||
} while (netStream.DataAvailable);
|
||||
|
||||
List<byte[]> messages = SlipFrame.Decode(responseData.ToArray());
|
||||
|
||||
foreach (byte[] message in messages)
|
||||
{
|
||||
OscPacket packet = OscPacket.GetPacket(message);
|
||||
OscMessage responseMessage = (OscMessage)packet;
|
||||
//watch.Stop();
|
||||
//Console.WriteLine($"TCPCLient - message receive took {watch.ElapsedMilliseconds}ms and {reads} reads");
|
||||
OnMessageReceived(responseMessage);
|
||||
}
|
||||
//Console.WriteLine("Raw TCP In: " + System.Text.Encoding.UTF8.GetString(responseData.ToArray()));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//Console.WriteLine("TCPSENDER - Receive Exception: " + e.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
if (tcpClient != null)
|
||||
if (client != null)
|
||||
{
|
||||
tcpClient.Events.Connected -= ClientConnected;
|
||||
tcpClient.Events.DataReceived -= DataReceived;
|
||||
tcpClient.Events.Disconnected -= ClientDisconneted;
|
||||
if (tcpClient.IsConnected)
|
||||
if (client.Connected)
|
||||
{
|
||||
_log.Debug($"closing connection to {Address}");
|
||||
tcpClient.Disconnect();
|
||||
tcpClient.Dispose();
|
||||
Log.Debug($"[tcpClient] closing connection to {Address}");
|
||||
client.GetStream().Close();
|
||||
client.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,6 @@
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<PackageReference Include="Xamarin.Essentials" Version="1.7.3" />
|
||||
<PackageReference Include="Serilog" Version="2.12.0" />
|
||||
<PackageReference Include="SuperSimpleTcp" Version="3.0.5" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="App.xaml" />
|
||||
|
||||
Reference in New Issue
Block a user