RTI Connext C# API  6.1.0
DataReaderAsyncExtensions Class Reference

Provides extensions to DataReader<T> More...

Static Public Member Functions

static IAsyncEnumerable< LoanedSample< T > > TakeAsync< T > (this DataReader< T > reader)
 Provides an infinite asynchronous enumerable that returns new samples from a reader as they become available. More...
 
static IAsyncEnumerable< LoanedSample< T > > TakeAsync< T > (this Selector< T > selector)
 Provides an infinite asynchronous enumerable that returns new samples from a reader selector as they become available. More...
 
static async IAsyncEnumerable< T > ValidData< T > (this IAsyncEnumerable< LoanedSample< T >> samples, [EnumeratorCancellation] CancellationToken cancellationToken=default)
 Filters a sequence of samples, returning valid data only. More...
 

Detailed Description

Provides extensions to DataReader<T>

Note
These extensions are included in the Rti.ConnextDds.Extra package.

Member Function Documentation

◆ TakeAsync< T >() [1/2]

static IAsyncEnumerable<LoanedSample<T> > TakeAsync< T > ( this DataReader< T >  reader)
static

Provides an infinite asynchronous enumerable that returns new samples from a reader as they become available.

Parameters
readerThe reader where the samples come from
Template Parameters
TThe reader's topic-type

When an element of the returned IAsyncEnumerable is requested, if the reader has any data available, it returns it immediately. Otherwise it awaits until new data is available.

The simplest way to use this function is an infinite loop that prints the data as the reader receives it:

await foreach (var sample in reader.TakeAsync())
{
if (sample.Info.ValidData)
{
Console.WriteLine($"Received data: {sample.Data}");
}
}
Warning
Each element (LoanedSample<T>) is only valid until the enumerable advances to the next element. The sample or any reference to the sample's fields can no longer be accessed after the enumerable moves to the next sample.

The enumerable can receive a cancellation token, for example:

var cancel = new CancellationTokenSource();
// A:
await foreach (var sample in reader.TakeAsync().WithCancellation(cancel.Token))
{
...
}
// B:
cancel.Cancel();
Warning
The disposal of the DataReader or the Subscriber and DomainParticipant that contains it may fail while the iterator is active. Use a cancellation token to ensure TakeAsync() finishes before disposing said entities.
See also
Reading data asynchronously for more code examples.

◆ TakeAsync< T >() [2/2]

static IAsyncEnumerable<LoanedSample<T> > TakeAsync< T > ( this Selector< T >  selector)
static

Provides an infinite asynchronous enumerable that returns new samples from a reader selector as they become available.

Parameters
selectorA selector created with DataReader<T>.Select
Template Parameters
TThe reader's topic-type
See also
TakeAsync<T>(DataReader<T>)

◆ ValidData< T >()

static async IAsyncEnumerable<T> ValidData< T > ( this IAsyncEnumerable< LoanedSample< T >>  samples,
[EnumeratorCancellation] CancellationToken  cancellationToken = default 
)
static

Filters a sequence of samples, returning valid data only.

Parameters
samplesA IAsyncEnumerable of loaned samples: the result of TakeAsync()
cancellationTokenA cancellation token, automatically set by the compiler when .WithCancellation() is used.
Returns
A new IAsyncEnumerable such that, for each LoanedSample<T> s in samples, it returns s.Data only if s.Info.ValidData is true.

Just like TakeAsync, only the current T object in the iteration is valid at a given moment; as soon as the iterator advances, that object becomes invalid and can no longer be accessed (unless its copied)