aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Util.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Util.cs')
-rw-r--r--OpenSim/Framework/Util.cs154
1 files changed, 137 insertions, 17 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs
index 1700d3e..724e38b 100644
--- a/OpenSim/Framework/Util.cs
+++ b/OpenSim/Framework/Util.cs
@@ -501,19 +501,25 @@ namespace OpenSim.Framework
501 /// </summary> 501 /// </summary>
502 /// <param name="data"></param> 502 /// <param name="data"></param>
503 /// <returns></returns> 503 /// <returns></returns>
504
504 public static string Md5Hash(string data) 505 public static string Md5Hash(string data)
505 { 506 {
506 byte[] dataMd5 = ComputeMD5Hash(data); 507 return Md5Hash(data, Encoding.Default);
508 }
509
510 public static string Md5Hash(string data, Encoding encoding)
511 {
512 byte[] dataMd5 = ComputeMD5Hash(data, encoding);
507 StringBuilder sb = new StringBuilder(); 513 StringBuilder sb = new StringBuilder();
508 for (int i = 0; i < dataMd5.Length; i++) 514 for (int i = 0; i < dataMd5.Length; i++)
509 sb.AppendFormat("{0:x2}", dataMd5[i]); 515 sb.AppendFormat("{0:x2}", dataMd5[i]);
510 return sb.ToString(); 516 return sb.ToString();
511 } 517 }
512 518
513 private static byte[] ComputeMD5Hash(string data) 519 private static byte[] ComputeMD5Hash(string data, Encoding encoding)
514 { 520 {
515 MD5 md5 = MD5.Create(); 521 MD5 md5 = MD5.Create();
516 return md5.ComputeHash(Encoding.Default.GetBytes(data)); 522 return md5.ComputeHash(encoding.GetBytes(data));
517 } 523 }
518 524
519 /// <summary> 525 /// <summary>
@@ -521,6 +527,12 @@ namespace OpenSim.Framework
521 /// </summary> 527 /// </summary>
522 /// <param name="data"></param> 528 /// <param name="data"></param>
523 /// <returns></returns> 529 /// <returns></returns>
530
531 public static string SHA1Hash(string data, Encoding enc)
532 {
533 return SHA1Hash(enc.GetBytes(data));
534 }
535
524 public static string SHA1Hash(string data) 536 public static string SHA1Hash(string data)
525 { 537 {
526 return SHA1Hash(Encoding.Default.GetBytes(data)); 538 return SHA1Hash(Encoding.Default.GetBytes(data));
@@ -1209,19 +1221,19 @@ namespace OpenSim.Framework
1209 { 1221 {
1210 string os = String.Empty; 1222 string os = String.Empty;
1211 1223
1212 if (Environment.OSVersion.Platform != PlatformID.Unix) 1224// if (Environment.OSVersion.Platform != PlatformID.Unix)
1213 { 1225// {
1214 os = Environment.OSVersion.ToString(); 1226// os = Environment.OSVersion.ToString();
1215 } 1227// }
1216 else 1228// else
1217 { 1229// {
1218 os = ReadEtcIssue(); 1230// os = ReadEtcIssue();
1219 } 1231// }
1220 1232//
1221 if (os.Length > 45) 1233// if (os.Length > 45)
1222 { 1234// {
1223 os = os.Substring(0, 45); 1235// os = os.Substring(0, 45);
1224 } 1236// }
1225 1237
1226 return os; 1238 return os;
1227 } 1239 }
@@ -1353,7 +1365,7 @@ namespace OpenSim.Framework
1353 1365
1354 public static Guid GetHashGuid(string data, string salt) 1366 public static Guid GetHashGuid(string data, string salt)
1355 { 1367 {
1356 byte[] hash = ComputeMD5Hash(data + salt); 1368 byte[] hash = ComputeMD5Hash(data + salt, Encoding.Default);
1357 1369
1358 //string s = BitConverter.ToString(hash); 1370 //string s = BitConverter.ToString(hash);
1359 1371
@@ -2165,4 +2177,112 @@ namespace OpenSim.Framework
2165 return str.Replace("_", "\\_").Replace("%", "\\%"); 2177 return str.Replace("_", "\\_").Replace("%", "\\%");
2166 } 2178 }
2167 } 2179 }
2180
2181 public class DoubleQueue<T> where T:class
2182 {
2183 private Queue<T> m_lowQueue = new Queue<T>();
2184 private Queue<T> m_highQueue = new Queue<T>();
2185
2186 private object m_syncRoot = new object();
2187 private Semaphore m_s = new Semaphore(0, 1);
2188
2189 public DoubleQueue()
2190 {
2191 }
2192
2193 public virtual int Count
2194 {
2195 get { return m_highQueue.Count + m_lowQueue.Count; }
2196 }
2197
2198 public virtual void Enqueue(T data)
2199 {
2200 Enqueue(m_lowQueue, data);
2201 }
2202
2203 public virtual void EnqueueLow(T data)
2204 {
2205 Enqueue(m_lowQueue, data);
2206 }
2207
2208 public virtual void EnqueueHigh(T data)
2209 {
2210 Enqueue(m_highQueue, data);
2211 }
2212
2213 private void Enqueue(Queue<T> q, T data)
2214 {
2215 lock (m_syncRoot)
2216 {
2217 m_lowQueue.Enqueue(data);
2218 m_s.WaitOne(0);
2219 m_s.Release();
2220 }
2221 }
2222
2223 public virtual T Dequeue()
2224 {
2225 return Dequeue(Timeout.Infinite);
2226 }
2227
2228 public virtual T Dequeue(int tmo)
2229 {
2230 return Dequeue(TimeSpan.FromMilliseconds(tmo));
2231 }
2232
2233 public virtual T Dequeue(TimeSpan wait)
2234 {
2235 T res = null;
2236
2237 if (!Dequeue(wait, ref res))
2238 return null;
2239
2240 return res;
2241 }
2242
2243 public bool Dequeue(int timeout, ref T res)
2244 {
2245 return Dequeue(TimeSpan.FromMilliseconds(timeout), ref res);
2246 }
2247
2248 public bool Dequeue(TimeSpan wait, ref T res)
2249 {
2250 if (!m_s.WaitOne(wait))
2251 return false;
2252
2253 lock (m_syncRoot)
2254 {
2255 if (m_highQueue.Count > 0)
2256 res = m_highQueue.Dequeue();
2257 else
2258 res = m_lowQueue.Dequeue();
2259
2260 if (m_highQueue.Count == 0 && m_lowQueue.Count == 0)
2261 return true;
2262
2263 try
2264 {
2265 m_s.Release();
2266 }
2267 catch
2268 {
2269 }
2270
2271 return true;
2272 }
2273 }
2274
2275 public virtual void Clear()
2276 {
2277
2278 lock (m_syncRoot)
2279 {
2280 // Make sure sem count is 0
2281 m_s.WaitOne(0);
2282
2283 m_lowQueue.Clear();
2284 m_highQueue.Clear();
2285 }
2286 }
2287 }
2168} 2288}