jellyfin/MediaBrowser.Common/Net/HttpServer.cs

40 lines
1.2 KiB
C#
Raw Normal View History

2012-07-12 06:55:27 +00:00
using System;
using System.Net;
using System.Reactive.Linq;
namespace MediaBrowser.Common.Net
2012-07-12 06:55:27 +00:00
{
public class HttpServer : IObservable<HttpListenerContext>, IDisposable
2012-07-12 06:55:27 +00:00
{
private readonly HttpListener listener;
private readonly IObservable<HttpListenerContext> stream;
2012-07-12 06:55:27 +00:00
public HttpServer(string url)
{
listener = new HttpListener();
listener.Prefixes.Add(url);
listener.Start();
stream = ObservableHttpContext();
}
private IObservable<HttpListenerContext> ObservableHttpContext()
2012-07-12 06:55:27 +00:00
{
return Observable.Create<HttpListenerContext>(obs =>
Observable.FromAsync<HttpListenerContext>(() => listener.GetContextAsync())
2012-07-12 06:55:27 +00:00
.Subscribe(obs))
.Repeat()
.Retry()
.Publish()
.RefCount();
}
public void Dispose()
{
listener.Stop();
}
public IDisposable Subscribe(IObserver<HttpListenerContext> observer)
2012-07-12 06:55:27 +00:00
{
return stream.Subscribe(observer);
}
}
}