-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketAsyncEventArgsPool.cs
40 lines (34 loc) · 1.18 KB
/
SocketAsyncEventArgsPool.cs
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
using System.Net.Sockets;
namespace SocketHelper;
// from: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socketasynceventargs.-ctor?view=net-8.0
class SocketAsyncEventArgsPool {
Stack<SocketAsyncEventArgs> m_pool;
// Initializes the object pool to the specified size
//
// The "capacity" parameter is the maximum number of
// SocketAsyncEventArgs objects the pool can hold
public SocketAsyncEventArgsPool(int capacity) {
m_pool = new Stack<SocketAsyncEventArgs>(capacity);
}
// Add a SocketAsyncEventArg instance to the pool
//
//The "item" parameter is the SocketAsyncEventArgs instance
// to add to the pool
public void Push(SocketAsyncEventArgs item) {
if (item == null) { throw new ArgumentNullException("Items added to a SocketAsyncEventArgsPool cannot be null"); }
lock (m_pool) {
m_pool.Push(item);
}
}
// Removes a SocketAsyncEventArgs instance from the pool
// and returns the object removed from the pool
public SocketAsyncEventArgs Pop() {
lock (m_pool) {
return m_pool.Pop();
}
}
// The number of SocketAsyncEventArgs instances in the pool
public int Count {
get { return m_pool.Count; }
}
}