aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ClientStack/UDPServer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ClientStack/UDPServer.cs')
-rw-r--r--OpenSim/Region/ClientStack/UDPServer.cs65
1 files changed, 64 insertions, 1 deletions
diff --git a/OpenSim/Region/ClientStack/UDPServer.cs b/OpenSim/Region/ClientStack/UDPServer.cs
index f91e5e2..5cad041 100644
--- a/OpenSim/Region/ClientStack/UDPServer.cs
+++ b/OpenSim/Region/ClientStack/UDPServer.cs
@@ -26,6 +26,7 @@
26* 26*
27*/ 27*/
28using System; 28using System;
29using System.Collections;
29using System.Collections.Generic; 30using System.Collections.Generic;
30using System.Net; 31using System.Net;
31using System.Net.Sockets; 32using System.Net.Sockets;
@@ -36,6 +37,68 @@ using OpenSim.Framework.Console;
36 37
37namespace OpenSim.Region.ClientStack 38namespace OpenSim.Region.ClientStack
38{ 39{
40 public sealed class PacketPool
41 {
42 // Set up a thread-safe singleton pattern
43 static PacketPool()
44 {
45 }
46
47 static readonly PacketPool instance = new PacketPool();
48
49 public static PacketPool Instance
50 {
51 get
52 {
53 return instance;
54 }
55 }
56
57 private Hashtable pool = new Hashtable();
58
59 public Packet GetPacket(PacketType type) {
60 Packet packet = null;
61
62 lock(pool)
63 {
64 if(pool[type] == null || ((Stack) pool[type]).Count == 0)
65 {
66 // Creating a new packet if we cannot reuse an old package
67 packet = Packet.BuildPacket(type);
68 }
69 else
70 {
71 // Recycle old packages
72 packet=(Packet) ((Stack) pool[type]).Pop();
73 }
74 }
75
76 return packet;
77 }
78
79 public Packet GetPacket(byte[] bytes, ref int packetEnd, byte[] zeroBuffer) {
80 Packet packet = GetPacket(Packet.GetType(bytes, packetEnd, zeroBuffer));
81
82 int i = 0;
83 packet.FromBytes(bytes, ref i, ref packetEnd, zeroBuffer);
84 return packet;
85 }
86
87 public void ReturnPacket(Packet packet) {
88 lock(pool)
89 {
90 PacketType type=packet.Type;
91
92 if(pool[type] == null)
93 {
94 pool[type] = new Stack();
95 }
96
97 ((Stack) pool[type]).Push(packet);
98 }
99 }
100 }
101
39 public class UDPServer : ClientStackNetworkHandler 102 public class UDPServer : ClientStackNetworkHandler
40 { 103 {
41 protected Dictionary<EndPoint, uint> clientCircuits = new Dictionary<EndPoint, uint>(); 104 protected Dictionary<EndPoint, uint> clientCircuits = new Dictionary<EndPoint, uint>();
@@ -194,7 +257,7 @@ namespace OpenSim.Region.ClientStack
194 257
195 try 258 try
196 { 259 {
197 packet = Packet.BuildPacket(RecvBuffer, ref packetEnd, ZeroBuffer); 260 packet = PacketPool.Instance.GetPacket(RecvBuffer, ref packetEnd, ZeroBuffer);
198 } 261 }
199 catch(Exception) 262 catch(Exception)
200 { 263 {