jellyfin/SocketHttpListener/Net/HttpListenerPrefixCollection.cs
Erwin de Haan ec1f5dc317 Mayor code cleanup
Add Argument*Exceptions now use proper nameof operators.

Added exception messages to quite a few Argument*Exceptions.

Fixed rethorwing to be proper syntax.

Added a ton of null checkes. (This is only a start, there are about 500 places that need proper null handling)

Added some TODOs to log certain exceptions.

Fix sln again.

Fixed all AssemblyInfo's and added proper copyright (where I could find them)

We live in *current year*.

Fixed the use of braces.

Fixed a ton of properties, and made a fair amount of functions static that should be and can be static.

Made more Methods that should be static static.

You can now use static to find bad functions!

Removed unused variable. And added one more proper XML comment.
2019-01-10 20:38:53 +01:00

98 lines
2.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
namespace SocketHttpListener.Net
{
public class HttpListenerPrefixCollection : ICollection<string>, IEnumerable<string>, IEnumerable
{
List<string> prefixes = new List<string>();
HttpListener listener;
private ILogger _logger;
internal HttpListenerPrefixCollection(ILogger logger, HttpListener listener)
{
_logger = logger;
this.listener = listener;
}
public int Count
{
get { return prefixes.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool IsSynchronized
{
get { return false; }
}
public void Add(string uriPrefix)
{
listener.CheckDisposed();
//ListenerPrefix.CheckUri(uriPrefix);
if (prefixes.Contains(uriPrefix))
return;
prefixes.Add(uriPrefix);
if (listener.IsListening)
HttpEndPointManager.AddPrefix(_logger, uriPrefix, listener);
}
public void Clear()
{
listener.CheckDisposed();
prefixes.Clear();
if (listener.IsListening)
HttpEndPointManager.RemoveListener(_logger, listener);
}
public bool Contains(string uriPrefix)
{
listener.CheckDisposed();
return prefixes.Contains(uriPrefix);
}
public void CopyTo(string[] array, int offset)
{
listener.CheckDisposed();
prefixes.CopyTo(array, offset);
}
public void CopyTo(Array array, int offset)
{
listener.CheckDisposed();
((ICollection)prefixes).CopyTo(array, offset);
}
public IEnumerator<string> GetEnumerator()
{
return prefixes.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return prefixes.GetEnumerator();
}
public bool Remove(string uriPrefix)
{
listener.CheckDisposed();
if (uriPrefix == null)
throw new ArgumentNullException(nameof(uriPrefix));
bool result = prefixes.Remove(uriPrefix);
if (result && listener.IsListening)
HttpEndPointManager.RemovePrefix(_logger, uriPrefix, listener);
return result;
}
}
}