Table of Contents

Class TrafficMeter

Namespace
VpnHood.Core.Tunneling
Assembly
VpnHood.Core.Tunneling.dll

Tracks tunnel traffic as two independent directions, and holds the session's speed limit: one bucket per direction, which every path of the session charges, so the limit covers all of its traffic. A sender waits on it once per turn (the tunnel's queue for its packets, each forwarding stream for its reads), so its senders share it equally; a receiver only enforces it, slowing a stream or dropping a datagram the peer sent past the limit. For session-facing limits, server download maps to send and server upload maps to receive.

public class TrafficMeter : IDisposable
Inheritance
TrafficMeter
Implements
Inherited Members

Constructors

TrafficMeter()

Initializes a new instance of the TrafficMeter class, with no limit.

public TrafficMeter()

Properties

LastActivityTime

Gets the last activity time.

public DateTime LastActivityTime { get; }

Property Value

DateTime

MaxSpeed

The maximum allowed speed (bytes per second) for throttling; 0 means unlimited.

public Traffic MaxSpeed { get; init; }

Property Value

Traffic

MaxSpeedBurst

What may pass at full speed after a pause (bytes), before the limit holds.

public Traffic MaxSpeedBurst { get; init; }

Property Value

Traffic

Speed

Gets the current transfer speed.

public Traffic Speed { get; }

Property Value

Traffic

Traffic

Gets the total traffic transferred.

public Traffic Traffic { get; }

Property Value

Traffic

Methods

Dispose()

Releases the meter, which then takes no more traffic.

public void Dispose()

OnReceived(long)

Reports received bytes to the traffic meter. This method is thread-safe.

public void OnReceived(long bytes)

Parameters

bytes long

The bytes.

Exceptions

ObjectDisposedException

The meter is disposed.

OnSent(long)

Reports sent bytes to the traffic meter. This method is thread-safe.

public void OnSent(long bytes)

Parameters

bytes long

The bytes.

Exceptions

ObjectDisposedException

The meter is disposed.

ShouldThrottleReceive(long)

For a receiver that cannot wait: charges bytes to the receive limit, or returns true, charging nothing, when they are past it and should be dropped.

public bool ShouldThrottleReceive(long bytes)

Parameters

bytes long

The bytes.

Returns

bool

true where the bytes are past the limit and should be dropped; otherwise, false, and they are charged.

ThrottleReceiveAsync(long, CancellationToken)

Charges bytes to the receive limit, and waits while the session is past it.

public ValueTask ThrottleReceiveAsync(long bytes, CancellationToken cancellationToken)

Parameters

bytes long

The bytes.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

ValueTask

A task that completes after the delay this charge calls for.

Remarks

It completes at once where the debt is below 20 milliseconds, otherwise after the debt's time, at most a day; other charges meanwhile may leave the session past its limit.

Exceptions

OperationCanceledException

cancellationToken was canceled.

ThrottleSendAsync(long, CancellationToken)

Charges bytes to the send limit, and waits while the session is past it.

public ValueTask ThrottleSendAsync(long bytes, CancellationToken cancellationToken)

Parameters

bytes long

The bytes.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

ValueTask

A task that completes after the delay this charge calls for.

Remarks

It completes at once where the debt is below 20 milliseconds, otherwise after the debt's time, at most a day; other charges meanwhile may leave the session past its limit.

Exceptions

OperationCanceledException

cancellationToken was canceled.