-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathPacketPool.java
More file actions
63 lines (51 loc) · 1.17 KB
/
Copy pathPacketPool.java
File metadata and controls
63 lines (51 loc) · 1.17 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
package javaforce.net;
import java.util.*;
import javaforce.net.*;
/** Packet Pool
*
* @author pquiring
*/
public class PacketPool {
private int mtu;
private static final int max_packets = 64;
private Object availLock = new Object();
private ArrayList<Packet> avail = new ArrayList<>();
private Object inuseLock = new Object();
private ArrayList<Packet> inuse = new ArrayList<>();
public PacketPool(int mtu) {
this.mtu = mtu;
}
public Packet alloc() {
Packet packet;
synchronized (availLock) {
if (avail.size() > 0) {
packet = avail.remove(0);
} else {
if (inuse.size() > max_packets) {
return null;
}
packet = new Packet();
packet.data = new byte[mtu];
}
synchronized (inuseLock) {
inuse.add(packet);
}
}
packet.offset = 0;
packet.length = 0;
packet.host = null;
packet.port = 0;
return packet;
}
public void free(Packet packet) {
synchronized (inuseLock) {
inuse.remove(packet);
}
synchronized (availLock) {
avail.add(packet);
}
}
public int count() {
return avail.size() + inuse.size();
}
}