Skip to content

Commit

Permalink
v3.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
shells-dw committed Mar 28, 2023
1 parent d53c726 commit 05687cb
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 149 deletions.
3 changes: 2 additions & 1 deletion Helper/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace streamdeck_totalmix
{
using System;
using System.Collections.Generic;
using System.Net;

public static class Globals
{
Expand All @@ -14,7 +15,7 @@ public static class Globals
public static Int32 channelCount;

// Globals for the device connection data, filled at start, read from then on
public static String interfaceIp;
public static IPAddress interfaceIp;
public static Int32 interfacePort;
public static Int32 interfaceSendPort;
public static Int32 interfaceBackgroundPort;
Expand Down
52 changes: 36 additions & 16 deletions Helper/HelperFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,51 @@ public static Boolean CheckForTotalMix()
while (!Globals.commandConnection && !Globals.backgroundConnection)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "CheckForTotalMix()");
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endPoints = properties.GetActiveUdpListeners();
foreach (IPEndPoint e in endPoints)

if (IPAddress.IsLoopback(Globals.interfaceIp) == true)
{
if (e.Port == Globals.interfacePort)

IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endPoints = properties.GetActiveUdpListeners();
foreach (IPEndPoint e in endPoints)
{
if (e.Port == Globals.interfacePort && IPAddress.IsLoopback(e.Address) == false)
{
Globals.commandConnection = true;
Logger.Instance.LogMessage(TracingLevel.INFO, "Globals.commandConnection = true");
if (!Globals.mirroringRequested)
{
GetChannelCount();
return false;
}
}
if (e.Port == Globals.interfaceBackgroundPort && IPAddress.IsLoopback(e.Address) == false)
{
Globals.backgroundConnection = true;
Logger.Instance.LogMessage(TracingLevel.INFO, "Globals.backgroundConnection = true");
}
}
}
else
{

Globals.commandConnection = true;
Logger.Instance.LogMessage(TracingLevel.INFO, "Globals.commandConnection = true");
Logger.Instance.LogMessage(TracingLevel.INFO, "LAN IP! Globals.commandConnection = true");
if (!Globals.mirroringRequested)
{
GetChannelCount();
return false;
}
}
if (e.Port == Globals.interfaceBackgroundPort)
{

Globals.backgroundConnection = true;
Logger.Instance.LogMessage(TracingLevel.INFO, "Globals.backgroundConnection = true");
}
if (Globals.commandConnection && Globals.backgroundConnection)
{
Task.Run(() => UpdateDeviceSettingDict());
GetChannelCount();
return true;
}
Logger.Instance.LogMessage(TracingLevel.INFO, "LAN IP! Globals.backgroundConnection = true");

}
if (Globals.commandConnection && Globals.backgroundConnection)
{
Task.Run(() => UpdateDeviceSettingDict());
GetChannelCount();
return true;
}
if (!Globals.backgroundConnection && Globals.mirroringRequested)
{
Expand Down
240 changes: 120 additions & 120 deletions Helper/OSCCommunicator/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,156 +11,156 @@ namespace streamdeck_totalmix
internal class Listener
{
// assign variables
static OscReceiver _receiver;
static OscReceiver _receiver;


// main listener Task
public static Task Listen(String bus, String address, Single value)
{
try
{
// Create the receiver
_receiver = new OscReceiver(Globals.interfaceBackgroundSendPort);
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: new OscReceiver: " + ex.Message);
}

// Connect the receiver
try
{
// connect to the socket
_receiver.Connect();
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: receiver.Connect(): " + ex.Message);
_receiver.Dispose();
_receiver = null;
Task.FromException(ex);
}
// main listener Task
public static Task Listen(String bus, String address, Single value)
{
try
{
// Create the receiver
_receiver = new OscReceiver(Globals.interfaceBackgroundSendPort);
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: new OscReceiver: " + ex.Message);
}

try
{
// Start the listen thread
Sender.Send(address, value, Globals.interfaceIp, Globals.interfaceBackgroundPort).Wait();
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: Sender.Send(): " + ex.Message);
}
// Connect the receiver
try
{
// connect to the socket
_receiver.Connect();
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: receiver.Connect(): " + ex.Message);
_receiver.Dispose();
_receiver = null;
Task.FromException(ex);
}

try
{
Task.Run(() => ListenLoop(bus)).Wait(5000);
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: Listenloop(): " + ex.Message);
}
// close the Reciver
_receiver.Close();
try
{
// Start the listen thread
Sender.Send(address, value, Globals.interfaceIp, Globals.interfaceBackgroundPort).Wait();
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: Sender.Send(): " + ex.Message);
}

if (_receiver != null)
{
// dispose of the reciever
_receiver.Dispose();
_receiver = null;
}
return Task.CompletedTask;
}
try
{
Task.Run(() => ListenLoop(bus)).Wait(5000);
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: Listenloop(): " + ex.Message);
}
// close the Reciver
_receiver.Close();

private static Task ListenLoop(object bus)
if (_receiver != null)
{
// dispose of the reciever
_receiver.Dispose();
_receiver = null;
}
return Task.CompletedTask;
}

private static Task ListenLoop(object bus)
{
try
{
while (_receiver.State != OscSocketState.Closed)
{
try
// making sure to add the key if it doesn't exist already (we need it anyway and don't like exceptions)
if (!Globals.bankSettings.ContainsKey($"{bus}"))
{
Globals.bankSettings.Add($"{bus}", new Dictionary<String, String>());
}
// if we are in a state to recieve
if (_receiver != null && _receiver.State == OscSocketState.Connected)
{
while (_receiver.State != OscSocketState.Closed)
// get the next message
// this will block until one arrives or the socket is closed
// OscPacket packet = receiver.Receive();
OscPacket packet;

try
{
// making sure to add the key if it doesn't exist already (we need it anyway and don't like exceptions)
if (!Globals.bankSettings.ContainsKey($"{bus}"))
{
Globals.bankSettings.Add($"{bus}", new Dictionary<String, String>());
}
// if we are in a state to recieve
if (_receiver != null && _receiver.State == OscSocketState.Connected)
while (_receiver.TryReceive(out packet) == true)
{
// get the next message
// this will block until one arrives or the socket is closed
// OscPacket packet = receiver.Receive();
OscPacket packet;
// Logger.Instance.LogMessage(TracingLevel.INFO, "while (receiver.TryReceive(out packet) == true _ before");
// we're expecting only bundles at this time, so define the received packet as bundle
OscBundle bundle = packet as OscBundle;

try
if (packet.Error == OscPacketError.None)
{
while (_receiver.TryReceive(out packet) == true)
try
{
// Logger.Instance.LogMessage(TracingLevel.INFO, "while (receiver.TryReceive(out packet) == true _ before");
// we're expecting only bundles at this time, so define the received packet as bundle
OscBundle bundle = packet as OscBundle;

if (packet.Error == OscPacketError.None)
// add every received bundle to the Global Dict
for (var i = 0; i < bundle.Count; i++)
{
try
Match heartbeat = Regex.Match(((Rug.Osc.OscMessage)bundle[i]).Address.ToString(), @"^\/$");
if (heartbeat.Success == true)
{
// add every received bundle to the Global Dict
for (var i = 0; i < bundle.Count; i++)
{
Match heartbeat = Regex.Match(((Rug.Osc.OscMessage)bundle[i]).Address.ToString(), @"^\/$");
if (heartbeat.Success == true)
{
return Task.CompletedTask;
}
Match uninterestingValues = Regex.Match(((Rug.Osc.OscMessage)bundle[i]).Address.ToString(), "^.{3}(?>label|select)");
if (uninterestingValues.Success == false)
{
Globals.bankSettings[$"{bus}"][$"{((Rug.Osc.OscMessage)bundle[i]).Address}"] = ((Rug.Osc.OscMessage)bundle[i])[0].ToString();
if (((Rug.Osc.OscMessage)bundle[i]).Address == $"/1/micgain{Globals.channelCount}Val")
{
return Task.CompletedTask;
}
}
}
return Task.CompletedTask;
}
catch (Exception ex)
Match uninterestingValues = Regex.Match(((Rug.Osc.OscMessage)bundle[i]).Address.ToString(), "^.{3}(?>label|select)");
if (uninterestingValues.Success == false)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: Error inside main function - bundle might be null: " + packet.ErrorMessage);
return Task.FromException(ex);

Globals.bankSettings[$"{bus}"][$"{((Rug.Osc.OscMessage)bundle[i]).Address}"] = ((Rug.Osc.OscMessage)bundle[i])[0].ToString();
if (((Rug.Osc.OscMessage)bundle[i]).Address == $"/1/micgain{Globals.channelCount}Val")
{
return Task.CompletedTask;
}
}
}
else
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: Error reading packet: " + packet.Error);
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: Error reading packet: " + packet.ErrorMessage);
return Task.CompletedTask;
}
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: Error inside main function - bundle might be null: " + packet.ErrorMessage);
return Task.FromException(ex);

// Logger.Instance.LogMessage(TracingLevel.INFO, "while (receiver.TryReceive(out packet) == true _ after");
}
}
catch (Exception ex)
else
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: while (receiver.TryReceive(out packet) == true): " + ex.Message);
Task.FromException(ex);
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: Error reading packet: " + packet.Error);
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: Error reading packet: " + packet.ErrorMessage);
return Task.CompletedTask;
}

// Logger.Instance.LogMessage(TracingLevel.INFO, "while (receiver.TryReceive(out packet) == true _ after");
}
}
return Task.CompletedTask;
}

// TODO
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: catch main try: " + ex.Message);
// if the socket was connected when this happens
// then do something useful with it
if (_receiver != null && _receiver.State == OscSocketState.Connected)
catch (Exception ex)
{
// something useful
return Task.CompletedTask;
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: while (receiver.TryReceive(out packet) == true): " + ex.Message);
Task.FromException(ex);
}
}
}
return Task.CompletedTask;
}

// TODO
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Listener: catch main try: " + ex.Message);
// if the socket was connected when this happens
// then do something useful with it
if (_receiver != null && _receiver.State == OscSocketState.Connected)
{
// something useful
return Task.CompletedTask;
}
}
return Task.CompletedTask;
}
}
}
10 changes: 4 additions & 6 deletions Helper/OSCCommunicator/Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ namespace streamdeck_totalmix
{
internal class Sender
{
public static Task Send(String name, Single value, String ip, Int32 port)
public static Task Send(String name, Single value, IPAddress ip, Int32 port)
{

OscSender sender = null;
try { sender = new OscSender(address: IPAddress.Loopback, localPort: 0, remotePort: port); }
try { sender = new OscSender(local: IPAddress.Any, localPort: 0, remote: ip, remotePort: port); }
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Sender: new OscSender: " + ex.Message);
sender.Dispose();
sender = null;
}
finally { sender = new OscSender(address: IPAddress.Loopback, localPort: 0, remotePort: port); }

// This is the ip address we are going to send to
IPAddress address = IPAddress.Parse(ip);
finally { sender = new OscSender(local: IPAddress.Any, localPort: 0, remote: ip, remotePort: port); }

try
{
Expand Down
Loading

0 comments on commit 05687cb

Please sign in to comment.