You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Common/Net/HttpServer.cs

40 lines
1.2 KiB

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