aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/PacketPool.cs
diff options
context:
space:
mode:
authorAdam Frisby2008-04-29 14:04:55 +0000
committerAdam Frisby2008-04-29 14:04:55 +0000
commit375163a6fece8b3a57c7555246abe8338223a599 (patch)
tree163001ca96a4b4d08589e9772f78510677d5d0dc /OpenSim/Framework/PacketPool.cs
parentPatch from Melanie: 0001087: Crash to bash de-linking objects. Thanks Melanie! (diff)
downloadopensim-SC_OLD-375163a6fece8b3a57c7555246abe8338223a599.zip
opensim-SC_OLD-375163a6fece8b3a57c7555246abe8338223a599.tar.gz
opensim-SC_OLD-375163a6fece8b3a57c7555246abe8338223a599.tar.bz2
opensim-SC_OLD-375163a6fece8b3a57c7555246abe8338223a599.tar.xz
* Spring cleaning.
* Added new generic "Location" class to handle 2D integer locations. Going to use it to replace all RegionHandle and X,Y coordinate references throughout the entire project. You have been warned.
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/PacketPool.cs76
1 files changed, 37 insertions, 39 deletions
diff --git a/OpenSim/Framework/PacketPool.cs b/OpenSim/Framework/PacketPool.cs
index 22f952a..be56cdc 100644
--- a/OpenSim/Framework/PacketPool.cs
+++ b/OpenSim/Framework/PacketPool.cs
@@ -35,18 +35,31 @@ namespace OpenSim.Framework
35{ 35{
36 public sealed class PacketPool 36 public sealed class PacketPool
37 { 37 {
38 static public void EncodeProxyMessage(byte[] bytes, ref int numBytes, EndPoint trueEP) 38 private static readonly PacketPool instance = new PacketPool();
39
40 private Hashtable pool = new Hashtable();
41
42 static PacketPool()
43 {
44 }
45
46 public static PacketPool Instance
47 {
48 get { return instance; }
49 }
50
51 public static void EncodeProxyMessage(byte[] bytes, ref int numBytes, EndPoint trueEP)
39 { 52 {
40 if( numBytes > 4090 ) // max UPD size = 4096 53 if (numBytes > 4090) // max UPD size = 4096
41 { 54 {
42 throw new Exception("ERROR: No space to encode the proxy EP"); 55 throw new Exception("ERROR: No space to encode the proxy EP");
43 } 56 }
44 57
45 ushort port = (ushort) ((IPEndPoint) trueEP).Port; 58 ushort port = (ushort) ((IPEndPoint) trueEP).Port;
46 bytes[numBytes++] = (byte)(port % 256); 59 bytes[numBytes++] = (byte) (port % 256);
47 bytes[numBytes++] = (byte)(port / 256); 60 bytes[numBytes++] = (byte) (port / 256);
48 61
49 foreach (byte b in ((IPEndPoint)trueEP).Address.GetAddressBytes()) 62 foreach (byte b in ((IPEndPoint) trueEP).Address.GetAddressBytes())
50 { 63 {
51 bytes[numBytes++] = b; 64 bytes[numBytes++] = b;
52 } 65 }
@@ -57,8 +70,8 @@ namespace OpenSim.Framework
57 70
58 numBytes = x; 71 numBytes = x;
59 } 72 }
60 73
61 static public EndPoint DecodeProxyMessage(byte[] bytes, ref int numBytes) 74 public static EndPoint DecodeProxyMessage(byte[] bytes, ref int numBytes)
62 { 75 {
63 // IPv4 Only 76 // IPv4 Only
64 byte[] addr = new byte[4]; 77 byte[] addr = new byte[4];
@@ -68,35 +81,19 @@ namespace OpenSim.Framework
68 addr[1] = bytes[--numBytes]; 81 addr[1] = bytes[--numBytes];
69 addr[0] = bytes[--numBytes]; 82 addr[0] = bytes[--numBytes];
70 83
71 ushort port = (ushort)(bytes[--numBytes] * 256); 84 ushort port = (ushort) (bytes[--numBytes] * 256);
72 port += (ushort)bytes[--numBytes]; 85 port += (ushort) bytes[--numBytes];
73
74 return (EndPoint) new IPEndPoint(new IPAddress(addr), (int)port);
75 }
76 86
77 // Set up a thread-safe singleton pattern 87 return (EndPoint) new IPEndPoint(new IPAddress(addr), (int) port);
78 static PacketPool()
79 {
80 } 88 }
81 89
82 static readonly PacketPool instance = new PacketPool(); 90 public Packet GetPacket(PacketType type)
83
84 public static PacketPool Instance
85 { 91 {
86 get
87 {
88 return instance;
89 }
90 }
91
92 private Hashtable pool = new Hashtable();
93
94 public Packet GetPacket(PacketType type) {
95 Packet packet = null; 92 Packet packet = null;
96 93
97 lock(pool) 94 lock (pool)
98 { 95 {
99 if(pool[type] == null || ((Stack) pool[type]).Count == 0) 96 if (pool[type] == null || ((Stack) pool[type]).Count == 0)
100 { 97 {
101 // Creating a new packet if we cannot reuse an old package 98 // Creating a new packet if we cannot reuse an old package
102 packet = Packet.BuildPacket(type); 99 packet = Packet.BuildPacket(type);
@@ -104,14 +101,14 @@ namespace OpenSim.Framework
104 else 101 else
105 { 102 {
106 // Recycle old packages 103 // Recycle old packages
107 packet=(Packet) ((Stack) pool[type]).Pop(); 104 packet = (Packet) ((Stack) pool[type]).Pop();
108 } 105 }
109 } 106 }
110 107
111 return packet; 108 return packet;
112 } 109 }
113 110
114 // private byte[] decoded_header = new byte[10]; 111 // private byte[] decoded_header = new byte[10];
115 private PacketType GetType(byte[] bytes) 112 private PacketType GetType(byte[] bytes)
116 { 113 {
117 byte[] decoded_header = new byte[10 + 8]; 114 byte[] decoded_header = new byte[10 + 8];
@@ -120,7 +117,7 @@ namespace OpenSim.Framework
120 117
121 Buffer.BlockCopy(bytes, 0, decoded_header, 0, 10); 118 Buffer.BlockCopy(bytes, 0, decoded_header, 0, 10);
122 119
123 if((bytes[0] & Helpers.MSG_ZEROCODED)!=0) 120 if ((bytes[0] & Helpers.MSG_ZEROCODED) != 0)
124 { 121 {
125 Helpers.ZeroDecodeCommand(bytes, decoded_header); 122 Helpers.ZeroDecodeCommand(bytes, decoded_header);
126 } 123 }
@@ -129,21 +126,21 @@ namespace OpenSim.Framework
129 { 126 {
130 if (decoded_header[7] == 0xFF) 127 if (decoded_header[7] == 0xFF)
131 { 128 {
132 id = (ushort)((decoded_header[8] << 8) + decoded_header[9]); 129 id = (ushort) ((decoded_header[8] << 8) + decoded_header[9]);
133 freq = PacketFrequency.Low; 130 freq = PacketFrequency.Low;
134 } 131 }
135 else 132 else
136 { 133 {
137 id = (ushort)decoded_header[7]; 134 id = (ushort) decoded_header[7];
138 freq = PacketFrequency.Medium; 135 freq = PacketFrequency.Medium;
139 } 136 }
140 } 137 }
141 else 138 else
142 { 139 {
143 id = (ushort)decoded_header[6]; 140 id = (ushort) decoded_header[6];
144 freq = PacketFrequency.High; 141 freq = PacketFrequency.High;
145 } 142 }
146 143
147 return Packet.GetType(id, freq); 144 return Packet.GetType(id, freq);
148 } 145 }
149 146
@@ -157,7 +154,8 @@ namespace OpenSim.Framework
157 return packet; 154 return packet;
158 } 155 }
159 156
160 public void ReturnPacket(Packet packet) { 157 public void ReturnPacket(Packet packet)
158 {
161 return; // packet pool disabled 159 return; // packet pool disabled
162 160
163 /* // Commented out to remove a compiler warning. :) 161 /* // Commented out to remove a compiler warning. :)
@@ -177,4 +175,4 @@ namespace OpenSim.Framework
177 */ 175 */
178 } 176 }
179 } 177 }
180} 178} \ No newline at end of file