Skip to content

Commit

Permalink
Fixed UPnP device expiration/update timer.
Browse files Browse the repository at this point in the history
  • Loading branch information
genemars committed Aug 29, 2023
1 parent 111ed91 commit 9383f1b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 34 deletions.
21 changes: 16 additions & 5 deletions Test.UPnP/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ public static void Main(string[] args)

while (true)
{
//Thread.Sleep(1000);
//Thread.Sleep(5000);
//Console.WriteLine(".");

var s = Console.ReadLine();
if (s == "-" && controlPoint != null)
{
StopControlPoint(controlPoint);
}
if (s == "+")
else if (s == "+" && controlPoint == null)
{
StartControlPoint(controlPoint);
}
if (s == "s")
else if (s == "r" && controlPoint != null)
{
controlPoint.Rescan();
}
else if (s == "s")
{
// Simulate WeMo Switch
AddWeMoSwitch();
}

}
}

Expand Down Expand Up @@ -369,16 +375,21 @@ internal void RemoveMe(UPnPDevice _d)

public void Rescan()
{
Hashtable dt = this.deviceTable.Clone() as Hashtable;
lock (this.deviceTableLock)
{
IDictionaryEnumerator enumerator = this.deviceTable.GetEnumerator();
IDictionaryEnumerator enumerator = dt.GetEnumerator();
while (enumerator.MoveNext())
{
string key = (string) enumerator.Key;
this.deviceLifeTimeClock.Add(key, 20);
}
}
this.genericControlPoint.FindDeviceAsync(searchFilter);

if (this.genericControlPoint != null)
{
this.genericControlPoint.FindDeviceAsync(searchFilter);
}
}

internal void SSDPNotifySink(IPEndPoint source, IPEndPoint local, Uri LocationURL, bool IsAlive, string USN,
Expand Down
59 changes: 38 additions & 21 deletions UPnP/LifeTimeMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ limitations under the License.
*/

using System;
using System.Threading;
using System.Collections;
using OpenSource.Utilities;
using Intel.UPNP;
Expand Down Expand Up @@ -86,17 +85,26 @@ private void OnTimedEvent(SafeTimer_SINGLE sender)
{
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();
TimeSpan nextEventTime = ((DateTime)MonitorList.GetKey(0)).Subtract(DateTime.Now);
if (nextEventTime.TotalMilliseconds<=0)
{
SafeNotifyTimer.Interval = 1;
}
else
{
SafeNotifyTimer.Interval = (int)nextEventTime.TotalMilliseconds;
}
SafeNotifyTimer.Start();
*/
}
}
Expand Down Expand Up @@ -132,17 +140,26 @@ public bool Remove(object 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();
TimeSpan nextEventTime = ((DateTime)MonitorList.GetKey(0)).Subtract(DateTime.Now);
if (nextEventTime.TotalMilliseconds<=0)
{
SafeNotifyTimer.Interval = 1;
}
else
{
SafeNotifyTimer.Interval = (int)nextEventTime.TotalMilliseconds;
}
SafeNotifyTimer.Start();
*/
}
}
Expand Down
17 changes: 9 additions & 8 deletions UPnP/SSDP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ public SSDP(int Expiration)
SSDP_EXPIRATION = Expiration;
if (SSDP_EXPIRATION < 5)
SSDP_EXPIRATION = 5;
int MinVal = (int)((double)SSDP_EXPIRATION * 0.25 * 1000);
int MaxVal = (int)((double)SSDP_EXPIRATION * 0.45 * 1000);

__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();
Expand Down Expand Up @@ -156,8 +157,8 @@ 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();

IPAddress[] ips = NetInfo.GetLocalAddresses();
foreach (IPAddress addr in ips)
{
if (addr.AddressFamily == AddressFamily.InterNetwork || addr.ScopeId != 0)
Expand Down Expand Up @@ -195,7 +196,7 @@ private void SetupSessions()
try
{
// session.Client.IOControl(SIO_UDP_CONNRESET, inValue, outValue);
session.Client.Disconnect(true);
session.Client.Disconnect(false);
}
catch (SocketException ex)
{
Expand All @@ -217,7 +218,7 @@ private void SetupSessions()
try
{
// session.Client.IOControl(SIO_UDP_CONNRESET, inValue, outValue);
session.Client.Disconnect(true);
session.Client.Disconnect(false);
}
catch (SocketException ex)
{
Expand Down Expand Up @@ -265,7 +266,7 @@ private void SetupSessions()
try
{
// session.Client.IOControl(SIO_UDP_CONNRESET, inValue, outValue);
session.Client.Disconnect(true);
session.Client.Disconnect(false);
}
catch (SocketException ex)
{
Expand All @@ -287,7 +288,7 @@ private void SetupSessions()
try
{
// session.Client.IOControl(SIO_UDP_CONNRESET, inValue, outValue);
session.Client.Disconnect(true);
session.Client.Disconnect(false);
}
catch (SocketException ex)
{
Expand Down Expand Up @@ -356,7 +357,7 @@ private void __NotifyCheck(SafeTimer_SINGLE sender)
OnRefresh();
int MinVal = (int)((double)SSDP_EXPIRATION * 0.25 * 1000);
int MaxVal = (int)((double)SSDP_EXPIRATION * 0.45 * 1000);
sender.SetTimer(5000, __NotifyCheck);
sender.SetTimer(RND.Next(MinVal, MaxVal), __NotifyCheck);
//sender.SetTimer(RND.Next(MinVal, MaxVal), __NotifyCheck);
//NotifyTimer.Interval = RND.Next(MinVal, MaxVal);
}
Expand Down

0 comments on commit 9383f1b

Please sign in to comment.