Skip to content

Commit

Permalink
Deprecated device expiration/update timers (no status polling).
Browse files Browse the repository at this point in the history
  • Loading branch information
genemars committed Aug 30, 2023
1 parent 9383f1b commit f848c38
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 221 deletions.
61 changes: 1 addition & 60 deletions UPnP/LifeTimeMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ public event LifeTimeHandler OnExpired
/// </summary>
public LifeTimeMonitor()
{
SafeNotifyTimer.SetTimer(5000, OnTimedEvent);
//SafeNotifyTimer.OnElapsed += new SafeTimer.TimeElapsedHandler(OnTimedEvent);
//SafeNotifyTimer.AutoReset = false;
//OpenSource.Utilities.InstanceTracker.Add(this);
}

Expand All @@ -81,40 +78,13 @@ private void OnTimedEvent(SafeTimer_SINGLE sender)
}
foreach (object obj in eventList)
OnExpiredEvent.Fire(this, obj);
lock (MonitorLock)
{
if (MonitorList.Count > 0)
{
TimeSpan nextEventTime = ((DateTime)MonitorList.GetKey(0)).Subtract(DateTime.Now);
if (nextEventTime.TotalMilliseconds <= 0)
{
sender.SetTimer(1, OnTimedEvent);
}
else
{
sender.SetTimer((int)nextEventTime.TotalMilliseconds, OnTimedEvent);
}
/*
TimeSpan nextEventTime = ((DateTime)MonitorList.GetKey(0)).Subtract(DateTime.Now);
if (nextEventTime.TotalMilliseconds<=0)
{
SafeNotifyTimer.Interval = 1;
}
else
{
SafeNotifyTimer.Interval = (int)nextEventTime.TotalMilliseconds;
}
SafeNotifyTimer.Start();
*/
}
}

}

public void Clear()
{
lock (MonitorLock)
{
//this.SafeNotifyTimer.Stop();
MonitorList.Clear();
}
}
Expand All @@ -133,39 +103,10 @@ public bool Remove(object obj)
{
if (MonitorList.ContainsValue(obj) == true)
{
//SafeNotifyTimer.Stop();
//NotifyTimer.Stop();
RetVal = true;
MonitorList.RemoveAt(MonitorList.IndexOfValue(obj));

if (MonitorList.Count > 0)
{
TimeSpan nextEventTime = ((DateTime)MonitorList.GetKey(0)).Subtract(DateTime.Now);
if (nextEventTime.TotalMilliseconds <= 0)
{
SafeNotifyTimer.SetTimer(1, OnTimedEvent);
}
else
{
SafeNotifyTimer.SetTimer((int)nextEventTime.TotalMilliseconds, OnTimedEvent);
}
/*
TimeSpan nextEventTime = ((DateTime)MonitorList.GetKey(0)).Subtract(DateTime.Now);
if (nextEventTime.TotalMilliseconds<=0)
{
SafeNotifyTimer.Interval = 1;
}
else
{
SafeNotifyTimer.Interval = (int)nextEventTime.TotalMilliseconds;
}
SafeNotifyTimer.Start();
*/
}
}
}
//OnTimedEvent(this,null);
// OnTimedEvent();
return RetVal;
}

Expand Down
159 changes: 35 additions & 124 deletions UPnP/SSDP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ private class SearchStruct
private Hashtable sessions = new Hashtable();
private Hashtable usessions = new Hashtable();

private SafeTimer_SINGLE NotifyTimer = new SafeTimer_SINGLE();

/// <summary>
/// Constructs a new SSDP Server
/// </summary>
Expand All @@ -93,22 +91,13 @@ private class SearchStruct
public SSDP(int Expiration)
{
//OpenSource.Utilities.InstanceTracker.Add(this);
LifeTimeHandler = new LifeTimeMonitor.LifeTimeHandler(SearchTimerSink);
LifeTimeHandler = SearchTimerSink;
SearchTimer.OnExpired += LifeTimeHandler;

SSDP_EXPIRATION = Expiration;
if (SSDP_EXPIRATION < 5)
SSDP_EXPIRATION = 5;

__NotifyCheck(NotifyTimer);

/*
int MinVal = (int)((double)SSDP_EXPIRATION * 0.25 * 1000);
int MaxVal = (int)((double)SSDP_EXPIRATION * 0.45 * 1000);
NotifyTimer.Interval = RND.Next(MinVal, MaxVal);
NotifyTimer.AutoReset = true;
NotifyTimer.Start();
*/
NetInfo = new NetworkInfo();
SetupSessions();
}
Expand Down Expand Up @@ -154,10 +143,6 @@ public void Dispose()

private void SetupSessions()
{
const int SIO_UDP_CONNRESET = -1744830452;
byte[] inValue = new byte[] { 0, 0, 0, 0 }; // == false
byte[] outValue = new byte[] { 0, 0, 0, 0 }; // initialize to 0

IPAddress[] ips = NetInfo.GetLocalAddresses();
foreach (IPAddress addr in ips)
{
Expand All @@ -167,144 +152,81 @@ private void SetupSessions()
{
try
{

// IPV4
if (addr.AddressFamily == AddressFamily.InterNetwork)
{
UdpClient session = new UdpClient(AddressFamily.InterNetwork);
try
{
session.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}
catch (SocketException ex)
{
OpenSource.Utilities.EventLogger.Log(ex);
}
try
{
session.ExclusiveAddressUse = false;
}
catch (SocketException ex)
{
session.Client.Close();
OpenSource.Utilities.EventLogger.Log(ex);
}
if (!Utils.IsMono())
session.Client.Bind(new IPEndPoint(addr, 1900));
else

// TODO: commented because not working with dotnet >= 6
// only binding all addresses will make UDP work properly (event-driven)
// if (!Utils.IsMono())
// session.Client.Bind(new IPEndPoint(addr, 1900));
// else
session.Client.Bind(new IPEndPoint(IPAddress.Any, 1900));

session.EnableBroadcast = true;
session.JoinMulticastGroup(Utils.UpnpMulticastV4Addr, addr);
try
{
// session.Client.IOControl(SIO_UDP_CONNRESET, inValue, outValue);
session.Client.Disconnect(false);
}
catch (SocketException ex)
{
OpenSource.Utilities.EventLogger.Log(ex);
// session.Client.Close();
}
#if NET6_0_OR_GREATER
catch (NotSupportedException ex)
{
OpenSource.Utilities.EventLogger.Log(ex);
// session.Client.Close();
}
#endif
session.BeginReceive(new AsyncCallback(OnReceiveSink), new object[2] { session, new IPEndPoint(addr, ((IPEndPoint)session.Client.LocalEndPoint).Port) });
session.BeginReceive(OnReceiveSink, new object[2] { session, new IPEndPoint(addr, ((IPEndPoint)session.Client.LocalEndPoint).Port) });
sessions[addr] = session;

UdpClient usession = new UdpClient(AddressFamily.InterNetwork);
usession.Client.Bind(new IPEndPoint(addr, 0));
try
{
// session.Client.IOControl(SIO_UDP_CONNRESET, inValue, outValue);
session.Client.Disconnect(false);
}
catch (SocketException ex)
{
OpenSource.Utilities.EventLogger.Log(ex);
// usession.Client.Close();
}
#if NET6_0_OR_GREATER
catch (NotSupportedException ex)
{
OpenSource.Utilities.EventLogger.Log(ex);
// session.Client.Close();
}
#endif
usession.BeginReceive(new AsyncCallback(OnReceiveSink), new object[2] { usession, new IPEndPoint(addr, ((IPEndPoint)session.Client.LocalEndPoint).Port) });

usession.BeginReceive(OnReceiveSink, new object[2] { usession, new IPEndPoint(addr, ((IPEndPoint)session.Client.LocalEndPoint).Port) });
usessions[addr] = usession;
}

// IPV6
if (addr.AddressFamily == AddressFamily.InterNetworkV6)
{
UdpClient session = new UdpClient(AddressFamily.InterNetworkV6);
UdpClient sessionIPv6 = new UdpClient(AddressFamily.InterNetworkV6);
try
{
session.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sessionIPv6.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}
catch (SocketException ex)
{
OpenSource.Utilities.EventLogger.Log(ex);
}
try
{
session.ExclusiveAddressUse = false;
sessionIPv6.ExclusiveAddressUse = false;
}
catch (SocketException ex)
{
OpenSource.Utilities.EventLogger.Log(ex);
}
if (!Utils.IsMono())
session.Client.Bind(new IPEndPoint(addr, 1900));
else
session.Client.Bind(new IPEndPoint(IPAddress.IPv6Any, 1900));
session.EnableBroadcast = true;
// if (!Utils.IsMono())
// session.Client.Bind(new IPEndPoint(addr, 1900));
// else
sessionIPv6.Client.Bind(new IPEndPoint(IPAddress.IPv6Any, 1900));
sessionIPv6.EnableBroadcast = true;
if (addr.IsIPv6LinkLocal)
session.JoinMulticastGroup((int)addr.ScopeId, Utils.UpnpMulticastV6Addr2);
sessionIPv6.JoinMulticastGroup((int)addr.ScopeId, Utils.UpnpMulticastV6Addr2);
else
session.JoinMulticastGroup((int)addr.ScopeId, Utils.UpnpMulticastV6Addr1);
try
{
// session.Client.IOControl(SIO_UDP_CONNRESET, inValue, outValue);
session.Client.Disconnect(false);
}
catch (SocketException ex)
{
OpenSource.Utilities.EventLogger.Log(ex);
// session.Close();
}
#if NET6_0_OR_GREATER
catch (NotSupportedException ex)
{
OpenSource.Utilities.EventLogger.Log(ex);
// session.Client.Close();
}
#endif
session.BeginReceive(new AsyncCallback(OnReceiveSink), new object[2] { session, new IPEndPoint(addr, ((IPEndPoint)session.Client.LocalEndPoint).Port) });
sessions[addr] = session;
sessionIPv6.JoinMulticastGroup((int)addr.ScopeId, Utils.UpnpMulticastV6Addr1);

UdpClient usession = new UdpClient(AddressFamily.InterNetworkV6);
usession.Client.Bind(new IPEndPoint(addr, 0));
try
{
// session.Client.IOControl(SIO_UDP_CONNRESET, inValue, outValue);
session.Client.Disconnect(false);
}
catch (SocketException ex)
{
OpenSource.Utilities.EventLogger.Log(ex);
// usession.Close();
}
#if NET6_0_OR_GREATER
catch (NotSupportedException ex)
{
OpenSource.Utilities.EventLogger.Log(ex);
// session.Client.Close();
}
#endif
usession.BeginReceive(new AsyncCallback(OnReceiveSink), new object[2] { usession, new IPEndPoint(addr, ((IPEndPoint)session.Client.LocalEndPoint).Port) });
usessions[addr] = usession;
sessionIPv6.BeginReceive(OnReceiveSink, new object[2] { sessionIPv6, new IPEndPoint(addr, ((IPEndPoint)sessionIPv6.Client.LocalEndPoint).Port) });
sessions[addr] = sessionIPv6;

UdpClient usessionIPv6 = new UdpClient(AddressFamily.InterNetworkV6);
usessionIPv6.Client.Bind(new IPEndPoint(addr, 0));

usessionIPv6.BeginReceive(OnReceiveSink, new object[2] { usessionIPv6, new IPEndPoint(addr, ((IPEndPoint)sessionIPv6.Client.LocalEndPoint).Port) });
usessions[addr] = usessionIPv6;
}

}
catch (SocketException ex)
{
Expand Down Expand Up @@ -340,7 +262,7 @@ private void OnReceiveSink(IAsyncResult result)
{
OpenSource.Utilities.EventLogger.Log(ex);
}
session.BeginReceive(new AsyncCallback(OnReceiveSink), args);
session.BeginReceive(OnReceiveSink, args);
}
catch (Exception ex)
{
Expand All @@ -351,17 +273,6 @@ private void OnReceiveSink(IAsyncResult result)
}
}

private void __NotifyCheck(SafeTimer_SINGLE sender)
{
if (OnRefresh != null)
OnRefresh();
int MinVal = (int)((double)SSDP_EXPIRATION * 0.25 * 1000);
int MaxVal = (int)((double)SSDP_EXPIRATION * 0.45 * 1000);
sender.SetTimer(RND.Next(MinVal, MaxVal), __NotifyCheck);
//sender.SetTimer(RND.Next(MinVal, MaxVal), __NotifyCheck);
//NotifyTimer.Interval = RND.Next(MinVal, MaxVal);
}

/// <summary>
/// Parses a URL
/// </summary>
Expand Down
Loading

0 comments on commit f848c38

Please sign in to comment.