-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathMergedChannelReader.cs
More file actions
133 lines (113 loc) · 3.97 KB
/
MergedChannelReader.cs
File metadata and controls
133 lines (113 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Iviz.Msgs;
#if !NETSTANDARD2_0
using Nito.AsyncEx;
using System.Runtime.CompilerServices;
#endif
namespace Iviz.Roslib
{
/// <summary>
/// A subscriber queue that merges two or more <see cref="RosChannelReader{T}"/>
/// </summary>
public sealed class MergedChannelReader : IEnumerable<IMessage>
#if !NETSTANDARD2_0
, IAsyncEnumerable<IMessage>
#endif
{
readonly IRosChannelReader[] sources;
public MergedChannelReader(params IRosChannelReader[] sources)
{
if (sources == null)
{
BuiltIns.ThrowArgumentNull(nameof(sources));
}
if (sources.Length == 0)
{
throw new ArgumentException("Value cannot be an empty collection.", nameof(sources));
}
this.sources = sources;
}
public MergedChannelReader(IEnumerable<IRosChannelReader> sources)
{
if (sources == null)
{
BuiltIns.ThrowArgumentNull(nameof(sources));
}
this.sources = sources.ToArray();
if (this.sources.Length == 0)
{
throw new ArgumentException("Value cannot be an empty collection.", nameof(sources));
}
}
public IEnumerable<IMessage> ReadAll(CancellationToken token = default)
{
Task[] tasks = new Task[sources.Length];
for (int i = 0; i < sources.Length; i++)
{
tasks[i] = sources[i].WaitToReadAsync(token).AsTask();
}
while (true)
{
int readyTaskId = Task.WaitAny(tasks, token);
var readyTask = (Task<bool>) tasks[readyTaskId];
if (readyTask.IsCanceled)
{
throw new TaskCanceledException(readyTask);
}
if (!readyTask.Result)
{
throw new ObjectDisposedException($"{nameof(sources)}[{readyTaskId}]",
$"Channel {readyTaskId} has been disposed!");
}
yield return sources[readyTaskId].Read(token);
tasks[readyTaskId] = sources[readyTaskId].WaitToReadAsync(token).AsTask();
}
}
public IEnumerable<IMessage> TryReadAll()
{
return sources.SelectMany(source => source.TryReadAll());
}
public IEnumerator<IMessage> GetEnumerator()
{
return ReadAll().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public async IAsyncEnumerable<IMessage> ReadAllAsync([EnumeratorCancellation] CancellationToken token = default)
{
Task<bool>[] tasks = new Task<bool>[sources.Length];
for (int i = 0; i < sources.Length; i++)
{
tasks[i] = sources[i].WaitToReadAsync(token).AsTask();
}
while (true)
{
Task<bool> readyTask = await tasks.WhenAny(token);
for (int i = 0; i < tasks.Length; i++)
{
if (tasks[i] != readyTask)
{
continue;
}
if (!await readyTask)
{
throw new ObjectDisposedException($"{nameof(sources)}[{i}]", $"Channel {i} has been disposed!");
}
yield return await sources[i].ReadAsync(token);
tasks[i] = sources[i].WaitToReadAsync(token).AsTask();
}
}
}
public IAsyncEnumerator<IMessage> GetAsyncEnumerator(CancellationToken token = default)
{
return ReadAllAsync(token).GetAsyncEnumerator(token);
}
}
}