mirror of
https://github.com/jwetzell/qController.git
synced 2026-08-15 12:23:59 +00:00
BIG TCP Overhaul and passcode start
- Switch to Serilog for console logging - TCPSender is now persistent (let's hope) so can be used with passcode protected workspaces - UDP connection works with passcodes just can't timeout - Passcode prompt is automatic for workspaces that are password protected
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Serilog;
|
||||
|
||||
namespace SharpOSC
|
||||
{
|
||||
@@ -27,8 +27,8 @@ namespace SharpOSC
|
||||
private static OscMessage parseMessage(byte[] msg)
|
||||
{
|
||||
int index = 0;
|
||||
//Console.WriteLine("Raw ASCII DATA: " + System.Text.Encoding.ASCII.GetString(msg));
|
||||
//Console.WriteLine("Raw UTF-8 DATA: " + System.Text.Encoding.UTF8.GetString(msg));
|
||||
//Log.Debug("Raw ASCII DATA: " + System.Text.Encoding.ASCII.GetString(msg));
|
||||
//Log.Debug("Raw UTF-8 DATA: " + System.Text.Encoding.UTF8.GetString(msg));
|
||||
string address = null;
|
||||
char[] types = new char[0];
|
||||
List<object> arguments = new List<object>();
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using Serilog;
|
||||
|
||||
namespace SharpOSC
|
||||
{
|
||||
@@ -46,20 +47,40 @@ namespace SharpOSC
|
||||
_address = address;
|
||||
}
|
||||
|
||||
public void Send(byte[] message)
|
||||
public void Connect()
|
||||
{
|
||||
client = new TcpClient(Address, Port);
|
||||
Log.Debug($"TCPSender - connect called for <{Address}:{Port}>");
|
||||
Thread receivingThread = new Thread(ReceiveLoop);
|
||||
receivingThread.Start();
|
||||
}
|
||||
|
||||
public void Send(byte[] message)
|
||||
{
|
||||
byte[] slipData = SlipEncode(message);
|
||||
NetworkStream netStream = client.GetStream();
|
||||
netStream.Write(slipData.ToArray(), 0, slipData.ToArray().Length);
|
||||
}
|
||||
|
||||
public async void Receive()
|
||||
public void Send(OscPacket packet)
|
||||
{
|
||||
byte[] data = packet.GetBytes();
|
||||
Send(data);
|
||||
}
|
||||
|
||||
public void ReceiveLoop()
|
||||
{
|
||||
while (client.Connected)
|
||||
{
|
||||
Receive();
|
||||
}
|
||||
Log.Debug("TCPSender - Receive Loop has exited for some reason");
|
||||
}
|
||||
|
||||
public void Receive()
|
||||
{
|
||||
Random random = new Random();
|
||||
int num = random.Next(1000);
|
||||
|
||||
OscMessage response = new OscMessage("/null");
|
||||
NetworkStream netStream = client.GetStream();
|
||||
try
|
||||
{
|
||||
@@ -67,49 +88,25 @@ namespace SharpOSC
|
||||
List<byte> responseData = new List<byte>();
|
||||
if (netStream.CanRead)
|
||||
{
|
||||
byte[] buffer = new byte[1024];
|
||||
byte[] buffer = new byte[256];
|
||||
|
||||
int bytesRead = 0;
|
||||
do
|
||||
{
|
||||
bytesRead = await netStream.ReadAsync(buffer, 0, buffer.Length);
|
||||
bytesRead = netStream.Read(buffer, 0, buffer.Length);
|
||||
responseData.AddRange(buffer);
|
||||
Thread.Sleep(1);
|
||||
//Console.WriteLine( "Thread " + num + ": Bytes read: " + bytesRead + " - " + Encoding.UTF8.GetString(buffer));
|
||||
Thread.Sleep(30);
|
||||
//Log.Debug("Thread " + num + ": Bytes read: " + bytesRead + " - " + Encoding.UTF8.GetString(buffer));
|
||||
} while (netStream.DataAvailable);
|
||||
|
||||
//Console.WriteLine("Raw TCP In: " + System.Text.Encoding.UTF8.GetString(responseData.ToArray()));
|
||||
response = (OscMessage)OscPacket.GetPacket(responseData.Skip(1).ToArray());
|
||||
//Log.Debug("Raw TCP In: " + System.Text.Encoding.UTF8.GetString(responseData.ToArray()));
|
||||
OscMessage response = (OscMessage)OscPacket.GetPacket(responseData.Skip(1).ToArray());
|
||||
OnMessageReceived(response);
|
||||
}
|
||||
} catch(Exception e)
|
||||
{
|
||||
Console.WriteLine("TCPSENDER - Receive Exception: " + e.ToString());
|
||||
//Log.Debug("TCPSENDER - Receive Exception: " + e.ToString());
|
||||
}
|
||||
OnMessageReceived(response);
|
||||
}
|
||||
|
||||
public void SendAndReceive(byte[] message)
|
||||
{
|
||||
|
||||
client = new TcpClient(Address, Port);
|
||||
byte[] slipData = SlipEncode(message);
|
||||
NetworkStream netStream = client.GetStream();
|
||||
netStream.Write(slipData.ToArray(), 0, slipData.ToArray().Length);
|
||||
Receive();
|
||||
}
|
||||
|
||||
public void SendAndReceive(OscPacket packet)
|
||||
{
|
||||
OscMessage msg = (OscMessage)packet;
|
||||
//Console.WriteLine("TCPSENDER - TCP Message Sent: " + msg.Address);
|
||||
byte[] data = packet.GetBytes();
|
||||
SendAndReceive(data);
|
||||
}
|
||||
|
||||
public void Send(OscPacket packet)
|
||||
{
|
||||
byte[] data = packet.GetBytes();
|
||||
Send(data);
|
||||
}
|
||||
|
||||
public byte[] SlipEncode(byte[] data)
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using Serilog;
|
||||
|
||||
namespace SharpOSC
|
||||
{
|
||||
@@ -96,10 +97,10 @@ namespace SharpOSC
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
Log.Debug(e.ToString());
|
||||
// If there is an error reading the packet, null is sent to the callback
|
||||
}
|
||||
//Console.WriteLine("Raw UDP In: " + System.Text.Encoding.ASCII.GetString(bytes));
|
||||
//Log.Debug("Raw UDP In: " + System.Text.Encoding.ASCII.GetString(bytes));
|
||||
OscPacketCallback(packet);
|
||||
}
|
||||
else
|
||||
@@ -150,7 +151,7 @@ namespace SharpOSC
|
||||
{
|
||||
byte[] bytes = queue.Dequeue();
|
||||
var packet = OscPacket.GetPacket(bytes);
|
||||
//Console.WriteLine("Raw UDP In: " + System.Text.Encoding.ASCII.GetString(bytes));
|
||||
//Log.Debug("Raw UDP In: " + System.Text.Encoding.ASCII.GetString(bytes));
|
||||
return packet;
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user