我是 rx 的新手,一直在使用 dot net 中的反应式扩展处理一些网络代码。我的问题是,当我通过提供的令牌触发取消时,我使用异步函数创建的 tcpClients 的可观察量没有像我预期的那样完成。这是我遇到问题的代码的简化版本:
public static class ListenerExtensions
{
public static IObservable<TcpClient> ToListenerObservable(
this IPEndPoint endpoint,
int backlog)
{
return new TcpListener(endpoint).ToListenerObservable(backlog);
}
public static IObservable<TcpClient> ToListenerObservable(
this TcpListener listener,
int backlog)
{
return Observable.Create<TcpClient>(async (observer, token) =>
{
listener.Start(backlog);
try
{
while (!token.IsCancellationRequested)
observer.OnNext(await Task.Run(() => listener.AcceptTcpClientAsync(), token));
//This never prints and onCompleted is never called.
Console.WriteLine("Completing..");
observer.OnCompleted();
}
catch (System.Exception error)
{
observer.OnError(error);
}
finally
{
//This is never executed and my progam exits without closing the listener.
Console.WriteLine("Stopping listener...");
listener.Stop();
}
});
}
}
class Program
{
static void Main(string[] args)
{
var home = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2323);
var cancellation = new CancellationTokenSource();
home.ToListenerObservable(10)
.Subscribe(
onNext: c => Console.WriteLine($"{c.Client.RemoteEndPoint} connected"),
onError: e => Console.WriteLine($"Error: {e.Message}"),
onCompleted: () => Console.WriteLine("Complete"), // Never happens
token: cancellation.Token);
Console.WriteLine("Any key to cancel");
Console.ReadKey();
cancellation.Cancel();
Thread.Sleep(1000);
}
}
如果我运行它并连接到 localhost:2323,我可以看到我得到了一系列连接的 tcpClient。但是,如果我触发取消令牌,程序将退出而不关闭侦听器并像我预期的那样发出 onCompleted 事件。我做错了什么?
避免编写太多try
/catch
代码并使用取消令牌总是好的。这是一种在不离开标准 Rx 运算符的情况下执行您正在执行的操作的方法。请不要,我无法完全测试此代码,因此可能仍需要一些调整。
试试这个:
var query = Observable.Create<TcpClient>(o =>
{
var home = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2323);
var listener = new TcpListener(home);
listener.Start();
return
Observable
.Defer(() => Observable.FromAsync(() => listener.AcceptTcpClientAsync()))
.Repeat()
.Subscribe(o);
});
var completer = new Subject<Unit>();
var subscription =
query
.TakeUntil(completer)
.Subscribe(
onNext: c => Console.WriteLine($"{c.Client.RemoteEndPoint} connected"),
onError: e => Console.WriteLine($"Error: {e.Message}"),
onCompleted: () => Console.WriteLine("Complete"));
Console.WriteLine("Enter to cancel");
Console.ReadLine();
completer.OnNext(Unit.Default);
Thread.Sleep(1000);
这里的关键是像取消令牌一样询问的completer
。它自然地完成订阅,不像subscription.Dispose()
没有OnCompleted
调用即可完成订阅。
所以事实证明我对这里的一些事情感到困惑。这篇文章帮助我走上了正轨。事实上,我最终从那里复制了代码。
public static class TaskCancellations
{
public static async Task<T> WithCancellation<T>(
this Task<T> task,
CancellationToken token)
{
var cancellation = new TaskCompletionSource<bool>();
using (token.Register(s =>
(s as TaskCompletionSource<bool>).TrySetResult(true), cancellation))
{
if (task != await Task.WhenAny(task, cancellation.Task))
throw new OperationCanceledException(token);
return await task;
}
}
}
并像这样将它与 tcplistener 一起使用:
public static IObservable<TcpClient> ToListenerObservable(
this TcpListener listener,
int backlog)
{
return Observable.Create<TcpClient>(async (observer, token) =>
{
listener.Start(backlog)
try
{
while (!token.IsCancellationRequested)
{
observer.OnNext(await listener.AcceptTcpClientAsync()
.WithCancellation(token));
}
}
catch (OperationCanceledException)
{
Console.WriteLine("Completing...");
observer.OnCompleted();
}
catch (System.Exception error)
{
observer.OnError(error);
}
finally
{
Console.WriteLine("Stopping listener...");
listener.Stop();
}
});
}
现在一切都按预期工作。