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

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