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 8b8e507..aadcdc8 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));
@@ -1247,19 +1259,19 @@ namespace OpenSim.Framework
1247 { 1259 {
1248 string os = String.Empty; 1260 string os = String.Empty;
1249 1261
1250 if (Environment.OSVersion.Platform != PlatformID.Unix) 1262// if (Environment.OSVersion.Platform != PlatformID.Unix)
1251 { 1263// {
1252 os = Environment.OSVersion.ToString(); 1264// os = Environment.OSVersion.ToString();
1253 } 1265// }
1254 else 1266// else
1255 { 1267// {
1256 os = ReadEtcIssue(); 1268// os = ReadEtcIssue();
1257 } 1269// }
1258 1270//
1259 if (os.Length > 45) 1271// if (os.Length > 45)
1260 { 1272// {
1261 os = os.Substring(0, 45); 1273// os = os.Substring(0, 45);
1262 } 1274// }
1263 1275
1264 return os; 1276 return os;
1265 } 1277 }
@@ -1391,7 +1403,7 @@ namespace OpenSim.Framework
1391 1403
1392 public static Guid GetHashGuid(string data, string salt) 1404 public static Guid GetHashGuid(string data, string salt)
1393 { 1405 {
1394 byte[] hash = ComputeMD5Hash(data + salt); 1406 byte[] hash = ComputeMD5Hash(data + salt, Encoding.Default);
1395 1407
1396 //string s = BitConverter.ToString(hash); 1408 //string s = BitConverter.ToString(hash);
1397 1409
@@ -2203,4 +2215,112 @@ namespace OpenSim.Framework
2203 return str.Replace("_", "\\_").Replace("%", "\\%"); 2215 return str.Replace("_", "\\_").Replace("%", "\\%");
2204 } 2216 }
2205 } 2217 }
2218
2219 public class DoubleQueue<T> where T:class
2220 {
2221 private Queue<T> m_lowQueue = new Queue<T>();
2222 private Queue<T> m_highQueue = new Queue<T>();
2223
2224 private object m_syncRoot = new object();
2225 private Semaphore m_s = new Semaphore(0, 1);
2226
2227 public DoubleQueue()
2228 {
2229 }
2230
2231 public virtual int Count
2232 {
2233 get { return m_highQueue.Count + m_lowQueue.Count; }
2234 }
2235
2236 public virtual void Enqueue(T data)
2237 {
2238 Enqueue(m_lowQueue, data);
2239 }
2240
2241 public virtual void EnqueueLow(T data)
2242 {
2243 Enqueue(m_lowQueue, data);
2244 }
2245
2246 public virtual void EnqueueHigh(T data)
2247 {
2248 Enqueue(m_highQueue, data);
2249 }
2250
2251 private void Enqueue(Queue<T> q, T data)
2252 {
2253 lock (m_syncRoot)
2254 {
2255 m_lowQueue.Enqueue(data);
2256 m_s.WaitOne(0);
2257 m_s.Release();
2258 }
2259 }
2260
2261 public virtual T Dequeue()
2262 {
2263 return Dequeue(Timeout.Infinite);
2264 }
2265
2266 public virtual T Dequeue(int tmo)
2267 {
2268 return Dequeue(TimeSpan.FromMilliseconds(tmo));
2269 }
2270
2271 public virtual T Dequeue(TimeSpan wait)
2272 {
2273 T res = null;
2274
2275 if (!Dequeue(wait, ref res))
2276 return null;
2277
2278 return res;
2279 }
2280
2281 public bool Dequeue(int timeout, ref T res)
2282 {
2283 return Dequeue(TimeSpan.FromMilliseconds(timeout), ref res);
2284 }
2285
2286 public bool Dequeue(TimeSpan wait, ref T res)
2287 {
2288 if (!m_s.WaitOne(wait))
2289 return false;
2290
2291 lock (m_syncRoot)
2292 {
2293 if (m_highQueue.Count > 0)
2294 res = m_highQueue.Dequeue();
2295 else
2296 res = m_lowQueue.Dequeue();
2297
2298 if (m_highQueue.Count == 0 && m_lowQueue.Count == 0)
2299 return true;
2300
2301 try
2302 {
2303 m_s.Release();
2304 }
2305 catch
2306 {
2307 }
2308
2309 return true;
2310 }
2311 }
2312
2313 public virtual void Clear()
2314 {
2315
2316 lock (m_syncRoot)
2317 {
2318 // Make sure sem count is 0
2319 m_s.WaitOne(0);
2320
2321 m_lowQueue.Clear();
2322 m_highQueue.Clear();
2323 }
2324 }
2325 }
2206} 2326}