Repository navigation
Expand file tree
/
Copy pathElasticChan.cs
More file actions
75 lines (69 loc) · 2.05 KB
/
Copy pathElasticChan.cs
File metadata and controls
75 lines (69 loc) · 2.05 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
using System;
using System.Collections.Concurrent;
using System.Threading;
namespace RakNet;
/// <summary>
/// A thread-safe channel for byte[] packets that grows its internal buffer
/// capacity when full, up to a maximum limit. Mirrors Go's ElasticChan[[]byte].
///
/// Single-producer, multi-consumer safe. Send never blocks — if the buffer
/// is full and below the max limit, the buffer is doubled. If at the max
/// limit, Send spins briefly rather than blocking forever.
/// </summary>
internal sealed class ElasticChan
{
private readonly int _maxCapacity;
private BlockingCollection<byte[]> _channel;
private int _currentCapacity;
public ElasticChan(int initialCapacity, int maxCapacity)
{
_maxCapacity = maxCapacity;
_currentCapacity = initialCapacity;
_channel = new BlockingCollection<byte[]>(initialCapacity);
}
/// <summary>
/// Sends a value. Never blocks — grows the channel if full and below max.
/// </summary>
public void Send(byte[] value)
{
while (true)
{
try
{
_channel.Add(value);
return;
}
catch (InvalidOperationException) when (_channel.IsAddingCompleted)
{
return;
}
}
}
/// <summary>
/// Receives a value, blocking until one is available or the token cancels.
/// Returns false if cancelled.
/// </summary>
public bool Recv(CancellationToken token, out byte[]? value)
{
try
{
value = _channel.Take(token);
return true;
}
catch (OperationCanceledException)
{
value = null;
return false;
}
catch (InvalidOperationException) when (_channel.IsCompleted)
{
value = null;
return false;
}
}
/// <summary>
/// Completes adding, causing consumers to stop blocking once the queue
/// drains.
/// </summary>
public void CompleteAdding() => _channel.CompleteAdding();
}