-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionPool.cs
More file actions
executable file
·76 lines (65 loc) · 1.84 KB
/
Copy pathConnectionPool.cs
File metadata and controls
executable file
·76 lines (65 loc) · 1.84 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
76
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace BlopsAutoKicker
{
internal class ConnectionPool
{
static private Queue<Socket> pool;
const int PoolSize = 2;
static private string address;
static private int port;
static internal void InitializePool(string addr, int prt)
{
address = addr;
port = prt;
pool = new Queue<Socket>(PoolSize);
for (int i = 0; i < PoolSize; ++i)
{
pool.Enqueue(CreateConnectSocket());
}
}
static internal void DrainPool()
{
foreach (var sock in pool)
{
sock.Disconnect(false);
}
}
static private Socket CreateConnectSocket()
{
Logging.WriteLog("Creating pooled socket");
var sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.Connect(address, port);
return sock;
}
static internal Socket GetConnection()
{
lock (pool)
{
if (pool.Count != 0)
{
var sock = pool.Dequeue();
if (!sock.Connected)
{
sock = CreateConnectSocket();
}
return sock;
}
else
{
return CreateConnectSocket();
}
}
}
static internal void RecyleConnection(Socket sock)
{
lock(pool)
{
pool.Enqueue(sock);
}
}
}
}