Table of Contents

Class EventBus

Namespace
Pamoja.Bus
Assembly
Pamoja.Bus.dll

One endpoint on an event bus.

public sealed class EventBus : IDisposable
Inheritance
EventBus
Implements
Inherited Members

Remarks

One publisher, many subscribers, inside a single process. It is how the parts of a gateway talk to each other without knowing about each other, so a sampler can announce a reading and whatever cares about readings picks it up.

An endpoint both publishes and receives, and it receives what it publishes itself. It only sees events published after it existed, so subscribe before publishing anything it needs to see. Only NextAsync() and NextTextAsync() wait: publishing and subscribing return at once, even while a wait on the same endpoint is open.

Constructors

EventBus(int)

Creates an event bus.

public EventBus(int capacity = 64)

Parameters

capacity int

How many events a slow subscriber may fall behind before it starts missing them, rounded up to the next power of two and at most 1048576.

Exceptions

ArgumentOutOfRangeException

The capacity is negative.

PamojaException

The native bus could not be created.

Properties

Missed

How many events this endpoint lost by falling behind, as of its last completed wait.

public long Missed { get; }

Property Value

long

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

NextAsync()

Waits for the next event on this endpoint.

public Task<byte[]> NextAsync()

Returns

Task<byte[]>

The event.

Remarks

The wait lasts until an event arrives, and it holds a thread-pool thread while it does. NextAsync(TimeSpan) gives up sooner.

Exceptions

PamojaException

The native call failed.

NextAsync(TimeSpan)

Waits a limited time for the next event on this endpoint.

public Task<byte[]> NextAsync(TimeSpan timeout)

Parameters

timeout TimeSpan

How long to wait, rounded up to the next millisecond.

Returns

Task<byte[]>

The event.

Remarks

A wait that runs out takes no event, so the next one is left for the next call.

Exceptions

ArgumentOutOfRangeException

The time limit is negative.

TimeoutException

No event arrived in time.

PamojaException

The native call failed.

NextTextAsync()

Waits for the next event on this endpoint, as text.

public Task<string> NextTextAsync()

Returns

Task<string>

The event, decoded as UTF-8.

Exceptions

PamojaException

The native call failed.

NextTextAsync(TimeSpan)

Waits a limited time for the next event on this endpoint, as text.

public Task<string> NextTextAsync(TimeSpan timeout)

Parameters

timeout TimeSpan

How long to wait, rounded up to the next millisecond.

Returns

Task<string>

The event, decoded as UTF-8.

Exceptions

ArgumentOutOfRangeException

The time limit is negative.

TimeoutException

No event arrived in time.

PamojaException

The native call failed.

Publish(ReadOnlyMemory<byte>)

Publishes an event to every subscriber, this endpoint included.

public void Publish(ReadOnlyMemory<byte> payload)

Parameters

payload ReadOnlyMemory<byte>

The event bytes.

Remarks

It never waits: a subscriber that has fallen behind loses its oldest event rather than holding up the publisher.

Exceptions

PamojaException

The native call failed.

Publish(string)

Publishes text to every subscriber, this endpoint included: an event name, or a reading written out.

public void Publish(string text)

Parameters

text string

The event text, as UTF-8.

Remarks

It never waits: a subscriber that has fallen behind loses its oldest event rather than holding up the publisher.

Exceptions

PamojaException

The native call failed.

Publisher()

Takes a publish-only handle on the same bus, for a part that announces and never reads.

public EventPublisher Publisher()

Returns

EventPublisher

The publisher.

Exceptions

PamojaException

The native call failed.

Subscribe()

Takes another endpoint on the same bus.

public EventBus Subscribe()

Returns

EventBus

An endpoint that sees events published from now on.

Exceptions

PamojaException

The native call failed.