diff options
| author | Luke <luke.pulverenti@gmail.com> | 2015-04-14 00:43:41 -0400 |
|---|---|---|
| committer | Luke <luke.pulverenti@gmail.com> | 2015-04-14 00:43:41 -0400 |
| commit | 935de313d58c7a7ba792345c16cfd1c1aad09a78 (patch) | |
| tree | 2e9334986de5864b00d4901f031b5de6a970305e /MediaBrowser.Server.Implementations/EntryPoints | |
| parent | 71a4f2761e784513ae2f3dda03aa549903808ebb (diff) | |
| parent | bd253399c2f1913c544c93fd6927dee37f8add2f (diff) | |
Merge pull request #1079 from MediaBrowser/dev
3.0.5582.0
Diffstat (limited to 'MediaBrowser.Server.Implementations/EntryPoints')
| -rw-r--r-- | MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs | 55 | ||||
| -rw-r--r-- | MediaBrowser.Server.Implementations/EntryPoints/LibraryChangedNotifier.cs | 52 |
2 files changed, 65 insertions, 42 deletions
diff --git a/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs b/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs index 5c69db3e1..966e0a3e4 100644 --- a/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs +++ b/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Controller; +using System.Linq; +using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Plugins; using MediaBrowser.Model.Logging; @@ -47,7 +48,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints void _config_ConfigurationUpdated(object sender, EventArgs e) { _config.ConfigurationUpdated -= _config_ConfigurationUpdated; - + if (!string.Equals(_lastConfigIdentifier, GetConfigIdentifier(), StringComparison.OrdinalIgnoreCase)) { if (_isStarted) @@ -88,7 +89,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints NatUtility.UnhandledException += NatUtility_UnhandledException; NatUtility.StartDiscovery(); - _timer = new Timer(s => _createdRules = new List<string>(), null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5)); + _timer = new Timer(s => _createdRules = new List<string>(), null, TimeSpan.FromMinutes(3), TimeSpan.FromMinutes(3)); _lastConfigIdentifier = GetConfigIdentifier(); @@ -119,7 +120,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints CreateRules(device); } - catch (Exception) + catch (Exception ex) { // I think it could be a good idea to log the exception because // you are using permanent portmapping here (never expire) and that means that next time @@ -128,7 +129,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints // It also can fail with others like 727-ExternalPortOnlySupportsWildcard, 728-NoPortMapsAvailable // and those errors (upnp errors) could be useful for diagnosting. - //_logger.ErrorException("Error creating port forwarding rules", ex); + _logger.ErrorException("Error creating port forwarding rules", ex); } } @@ -138,25 +139,55 @@ namespace MediaBrowser.Server.Implementations.EntryPoints // On some systems the device discovered event seems to fire repeatedly // This check will help ensure we're not trying to port map the same device over and over + List<Mapping> currentMappings = null; + + try + { + currentMappings = device.GetAllMappings().ToList(); + } + catch (NotSupportedException) + { + } + var address = device.LocalAddress.ToString(); if (!_createdRules.Contains(address)) { _createdRules.Add(address); - CreatePortMap(device, _appHost.HttpPort, _config.Configuration.PublicPort); - CreatePortMap(device, _appHost.HttpsPort, _config.Configuration.PublicHttpsPort); + CreatePortMap(device, currentMappings, _appHost.HttpPort, _config.Configuration.PublicPort); + CreatePortMap(device, currentMappings, _appHost.HttpsPort, _config.Configuration.PublicHttpsPort); } } - private void CreatePortMap(INatDevice device, int privatePort, int publicPort) + private void CreatePortMap(INatDevice device, List<Mapping> currentMappings, int privatePort, int publicPort) { - _logger.Debug("Creating port map on port {0}", privatePort); + var hasMapping = false; + + if (currentMappings != null) + { + hasMapping = currentMappings.Any(i => i.PublicPort == publicPort && i.PrivatePort == privatePort); + } + else + { + try + { + var mapping = device.GetSpecificMapping(Protocol.Tcp, publicPort); + hasMapping = mapping != null; + } + catch (NotSupportedException) + { + } + } - device.CreatePortMap(new Mapping(Protocol.Tcp, privatePort, publicPort) + if (!hasMapping) { - Description = _appHost.Name - }); + _logger.Debug("Creating port map on port {0}", privatePort); + device.CreatePortMap(new Mapping(Protocol.Tcp, privatePort, publicPort) + { + Description = _appHost.Name + }); + } } // As I said before, this method will be never invoked. You can remove it. diff --git a/MediaBrowser.Server.Implementations/EntryPoints/LibraryChangedNotifier.cs b/MediaBrowser.Server.Implementations/EntryPoints/LibraryChangedNotifier.cs index 090f1e756..03daa4c2a 100644 --- a/MediaBrowser.Server.Implementations/EntryPoints/LibraryChangedNotifier.cs +++ b/MediaBrowser.Server.Implementations/EntryPoints/LibraryChangedNotifier.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Channels; +using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Plugins; using MediaBrowser.Controller.Session; @@ -69,7 +70,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints /// <param name="e">The <see cref="ItemChangeEventArgs"/> instance containing the event data.</param> void libraryManager_ItemAdded(object sender, ItemChangeEventArgs e) { - if (e.Item.LocationType == LocationType.Virtual) + if (!FilterItem(e.Item)) { return; } @@ -102,7 +103,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints /// <param name="e">The <see cref="ItemChangeEventArgs"/> instance containing the event data.</param> void libraryManager_ItemUpdated(object sender, ItemChangeEventArgs e) { - if (e.Item.LocationType == LocationType.Virtual) + if (!FilterItem(e.Item)) { return; } @@ -130,7 +131,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints /// <param name="e">The <see cref="ItemChangeEventArgs"/> instance containing the event data.</param> void libraryManager_ItemRemoved(object sender, ItemChangeEventArgs e) { - if (e.Item.LocationType == LocationType.Virtual) + if (!FilterItem(e.Item)) { return; } @@ -243,39 +244,39 @@ namespace MediaBrowser.Server.Implementations.EntryPoints { var user = _userManager.GetUserById(userId); - var collections = user.RootFolder.GetChildren(user, true).ToList(); - - var allRecursiveChildren = user.RootFolder - .GetRecursiveChildren(user) - .Select(i => i.Id) - .Distinct() - .ToDictionary(i => i); - return new LibraryUpdateInfo { - ItemsAdded = itemsAdded.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, collections, allRecursiveChildren)).Select(i => i.Id.ToString("N")).Distinct().ToList(), + ItemsAdded = itemsAdded.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user)).Select(i => i.Id.ToString("N")).Distinct().ToList(), - ItemsUpdated = itemsUpdated.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, collections, allRecursiveChildren)).Select(i => i.Id.ToString("N")).Distinct().ToList(), + ItemsUpdated = itemsUpdated.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user)).Select(i => i.Id.ToString("N")).Distinct().ToList(), - ItemsRemoved = itemsRemoved.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, collections, allRecursiveChildren, true)).Select(i => i.Id.ToString("N")).Distinct().ToList(), + ItemsRemoved = itemsRemoved.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, true)).Select(i => i.Id.ToString("N")).Distinct().ToList(), - FoldersAddedTo = foldersAddedTo.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, collections, allRecursiveChildren)).Select(i => i.Id.ToString("N")).Distinct().ToList(), + FoldersAddedTo = foldersAddedTo.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user)).Select(i => i.Id.ToString("N")).Distinct().ToList(), - FoldersRemovedFrom = foldersRemovedFrom.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, collections, allRecursiveChildren)).Select(i => i.Id.ToString("N")).Distinct().ToList() + FoldersRemovedFrom = foldersRemovedFrom.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user)).Select(i => i.Id.ToString("N")).Distinct().ToList() }; } + private bool FilterItem(BaseItem item) + { + if (item.LocationType == LocationType.Virtual) + { + return false; + } + + return !(item is IChannelItem); + } + /// <summary> /// Translates the physical item to user library. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="item">The item.</param> /// <param name="user">The user.</param> - /// <param name="collections">The collections.</param> - /// <param name="allRecursiveChildren">All recursive children.</param> /// <param name="includeIfNotFound">if set to <c>true</c> [include if not found].</param> /// <returns>IEnumerable{``0}.</returns> - private IEnumerable<T> TranslatePhysicalItemToUserLibrary<T>(T item, User user, IEnumerable<BaseItem> collections, Dictionary<Guid, Guid> allRecursiveChildren, bool includeIfNotFound = false) + private IEnumerable<T> TranslatePhysicalItemToUserLibrary<T>(T item, User user, bool includeIfNotFound = false) where T : BaseItem { // If the physical root changed, return the user root @@ -284,17 +285,8 @@ namespace MediaBrowser.Server.Implementations.EntryPoints return new[] { user.RootFolder as T }; } - // Need to find what user collection folder this belongs to - if (item.Parent is AggregateFolder) - { - if (item.LocationType == LocationType.FileSystem) - { - return collections.Where(i => i.PhysicalLocations.Contains(item.Path)).Cast<T>(); - } - } - // Return it only if it's in the user's library - if (includeIfNotFound || allRecursiveChildren.ContainsKey(item.Id) || (item.Parents.Any(i => i is BasePluginFolder) && item.IsVisibleStandalone(user))) + if (includeIfNotFound || item.IsVisibleStandalone(user)) { return new[] { item }; } |
