jellyfin/MediaBrowser.Common/Net/HttpServer.cs
LukePulverenti Luke Pulverenti luke pulverenti f1e668bad8 More code cleanups
2012-09-11 15:37:14 -04:00

40 lines
1.2 KiB
C#

using System;
using System.Net;
using System.Reactive.Linq;
namespace MediaBrowser.Common.Net
{
public class HttpServer : IObservable<HttpListenerContext>, IDisposable
{
private readonly HttpListener _listener;
private readonly IObservable<HttpListenerContext> _stream;
public HttpServer(string url)
{
_listener = new HttpListener();
_listener.Prefixes.Add(url);
_listener.Start();
_stream = ObservableHttpContext();
}
private IObservable<HttpListenerContext> ObservableHttpContext()
{
return Observable.Create<HttpListenerContext>(obs =>
Observable.FromAsync(() => _listener.GetContextAsync())
.Subscribe(obs))
.Repeat()
.Retry()
.Publish()
.RefCount();
}
public void Dispose()
{
_listener.Stop();
}
public IDisposable Subscribe(IObserver<HttpListenerContext> observer)
{
return _stream.Subscribe(observer);
}
}
}