aboutsummaryrefslogtreecommitdiff
path: root/Mono.Nat/Pmp
diff options
context:
space:
mode:
Diffstat (limited to 'Mono.Nat/Pmp')
-rw-r--r--Mono.Nat/Pmp/AsyncResults/PortMapAsyncResult.cs52
-rw-r--r--Mono.Nat/Pmp/PmpNatDevice.cs22
-rw-r--r--Mono.Nat/Pmp/PmpSearcher.cs (renamed from Mono.Nat/Pmp/Searchers/PmpSearcher.cs)49
3 files changed, 42 insertions, 81 deletions
diff --git a/Mono.Nat/Pmp/AsyncResults/PortMapAsyncResult.cs b/Mono.Nat/Pmp/AsyncResults/PortMapAsyncResult.cs
deleted file mode 100644
index c8ccf5435..000000000
--- a/Mono.Nat/Pmp/AsyncResults/PortMapAsyncResult.cs
+++ /dev/null
@@ -1,52 +0,0 @@
-//
-// Authors:
-// Ben Motmans <ben.motmans@gmail.com>
-//
-// Copyright (C) 2007 Ben Motmans
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-using System;
-
-namespace Mono.Nat.Pmp
-{
- internal class PortMapAsyncResult : AsyncResult
- {
- private Mapping mapping;
-
- internal PortMapAsyncResult (Mapping mapping, AsyncCallback callback, object asyncState)
- : base (callback, asyncState)
- {
- this.mapping = mapping;
- }
-
- internal PortMapAsyncResult (Protocol protocol, int port, int lifetime, AsyncCallback callback, object asyncState)
- : base (callback, asyncState)
- {
- this.mapping = new Mapping (protocol, port, port, lifetime);
- }
-
- internal Mapping Mapping
- {
- get { return mapping; }
- }
- }
-}
diff --git a/Mono.Nat/Pmp/PmpNatDevice.cs b/Mono.Nat/Pmp/PmpNatDevice.cs
index fb45b365b..2ef66fdbb 100644
--- a/Mono.Nat/Pmp/PmpNatDevice.cs
+++ b/Mono.Nat/Pmp/PmpNatDevice.cs
@@ -32,6 +32,7 @@ using System.Threading;
using System.Collections.Generic;
using System.Threading.Tasks;
using MediaBrowser.Model.Extensions;
+using MediaBrowser.Model.Logging;
namespace Mono.Nat.Pmp
{
@@ -39,11 +40,18 @@ namespace Mono.Nat.Pmp
{
private IPAddress localAddress;
private IPAddress publicAddress;
+ private ILogger _logger;
- internal PmpNatDevice(IPAddress localAddress, IPAddress publicAddress)
+ internal PmpNatDevice(IPAddress localAddress, IPAddress publicAddress, ILogger logger)
{
+ if (localAddress == null)
+ {
+ throw new ArgumentNullException("localAddress");
+ }
+
this.localAddress = localAddress;
this.publicAddress = publicAddress;
+ _logger = logger;
}
public override IPAddress LocalAddress
@@ -97,8 +105,7 @@ namespace Mono.Nat.Pmp
while (attempt < PmpConstants.RetryAttempts)
{
- await udpClient.SendAsync(buffer, buffer.Length,
- new IPEndPoint(LocalAddress, PmpConstants.ServerPort));
+ await udpClient.SendAsync(buffer, buffer.Length, new IPEndPoint(LocalAddress, PmpConstants.ServerPort));
if (attempt == 0)
{
@@ -125,9 +132,8 @@ namespace Mono.Nat.Pmp
mapping.Protocol,
mapping.PrivatePort,
e.Message);
- NatUtility.Log(message);
- var pmpException = e as MappingException;
- throw new MappingException(message, pmpException);
+ _logger.Debug(message);
+ throw e;
}
return mapping;
@@ -177,7 +183,7 @@ namespace Mono.Nat.Pmp
};
var errorMsg = errors[resultCode];
- NatUtility.Log("Error in CreatePortMapListen: " + errorMsg);
+ _logger.Debug("Error in CreatePortMapListen: " + errorMsg);
return;
}
@@ -192,7 +198,7 @@ namespace Mono.Nat.Pmp
}
catch (Exception ex)
{
- NatUtility.Logger.ErrorException("Error in CreatePortMapListen", ex);
+ _logger.ErrorException("Error in CreatePortMapListen", ex);
return;
}
}
diff --git a/Mono.Nat/Pmp/Searchers/PmpSearcher.cs b/Mono.Nat/Pmp/PmpSearcher.cs
index 2836071d0..180fb48d7 100644
--- a/Mono.Nat/Pmp/Searchers/PmpSearcher.cs
+++ b/Mono.Nat/Pmp/PmpSearcher.cs
@@ -37,33 +37,44 @@ using Mono.Nat.Pmp;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading.Tasks;
+using MediaBrowser.Model.Logging;
+using System.Linq;
namespace Mono.Nat
{
- internal class PmpSearcher : ISearcher
+ internal class PmpSearcher : ISearcher, IDisposable
{
- static PmpSearcher instance = new PmpSearcher();
+ private ILogger _logger;
-
- public static PmpSearcher Instance
- {
- get { return instance; }
- }
-
- private int timeout;
+ private int timeout = 250;
private DateTime nextSearch;
public event EventHandler<DeviceEventArgs> DeviceFound;
- public event EventHandler<DeviceEventArgs> DeviceLost;
- static PmpSearcher()
+ public PmpSearcher(ILogger logger)
{
+ _logger = logger;
+
CreateSocketsAndAddGateways();
}
- public static List<UdpClient> sockets;
- protected static Dictionary<UdpClient, List<IPEndPoint>> gatewayLists;
+ public void Dispose()
+ {
+ var list = sockets.ToList();
+ sockets.Clear();
+
+ foreach (var s in list)
+ {
+ using (s)
+ {
+ s.Close();
+ }
+ }
+ }
+
+ private List<UdpClient> sockets;
+ private Dictionary<UdpClient, List<IPEndPoint>> gatewayLists;
- internal static void CreateSocketsAndAddGateways()
+ private void CreateSocketsAndAddGateways()
{
sockets = new List<UdpClient>();
gatewayLists = new Dictionary<UdpClient, List<IPEndPoint>>();
@@ -137,11 +148,6 @@ namespace Mono.Nat
}
}
- PmpSearcher()
- {
- timeout = 250;
- }
-
public async void Search()
{
foreach (UdpClient s in sockets)
@@ -202,12 +208,13 @@ namespace Mono.Nat
return;
int errorcode = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(response, 2));
if (errorcode != 0)
- NatUtility.Log("Non zero error: {0}", errorcode);
+ _logger.Debug("Non zero error: {0}", errorcode);
IPAddress publicIp = new IPAddress(new byte[] { response[8], response[9], response[10], response[11] });
nextSearch = DateTime.Now.AddMinutes(5);
timeout = 250;
- OnDeviceFound(new DeviceEventArgs(new PmpNatDevice(endpoint.Address, publicIp)));
+
+ OnDeviceFound(new DeviceEventArgs(new PmpNatDevice(endpoint.Address, publicIp, _logger)));
}
public DateTime NextSearch