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 94a172c..e4d7e19 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));
@@ -1262,19 +1274,19 @@ namespace OpenSim.Framework
1262 { 1274 {
1263 string os = String.Empty; 1275 string os = String.Empty;
1264 1276
1265 if (Environment.OSVersion.Platform != PlatformID.Unix) 1277// if (Environment.OSVersion.Platform != PlatformID.Unix)
1266 { 1278// {
1267 os = Environment.OSVersion.ToString(); 1279// os = Environment.OSVersion.ToString();
1268 } 1280// }
1269 else 1281// else
1270 { 1282// {
1271 os = ReadEtcIssue(); 1283// os = ReadEtcIssue();
1272 } 1284// }
1273 1285//
1274 if (os.Length > 45) 1286// if (os.Length > 45)
1275 { 1287// {
1276 os = os.Substring(0, 45); 1288// os = os.Substring(0, 45);
1277 } 1289// }
1278 1290
1279 return os; 1291 return os;
1280 } 1292 }
@@ -1406,7 +1418,7 @@ namespace OpenSim.Framework
1406 1418
1407 public static Guid GetHashGuid(string data, string salt) 1419 public static Guid GetHashGuid(string data, string salt)
1408 { 1420 {
1409 byte[] hash = ComputeMD5Hash(data + salt); 1421 byte[] hash = ComputeMD5Hash(data + salt, Encoding.Default);
1410 1422
1411 //string s = BitConverter.ToString(hash); 1423 //string s = BitConverter.ToString(hash);
1412 1424
@@ -2218,4 +2230,112 @@ namespace OpenSim.Framework
2218 return str.Replace("_", "\\_").Replace("%", "\\%"); 2230 return str.Replace("_", "\\_").Replace("%", "\\%");
2219 } 2231 }
2220 } 2232 }
2233
2234 public class DoubleQueue<T> where T:class
2235 {
2236 private Queue<T> m_lowQueue = new Queue<T>();
2237 private Queue<T> m_highQueue = new Queue<T>();
2238
2239 private object m_syncRoot = new object();
2240 private Semaphore m_s = new Semaphore(0, 1);
2241
2242 public DoubleQueue()
2243 {
2244 }
2245
2246 public virtual int Count
2247 {
2248 get { return m_highQueue.Count + m_lowQueue.Count; }
2249 }
2250
2251 public virtual void Enqueue(T data)
2252 {
2253 Enqueue(m_lowQueue, data);
2254 }
2255
2256 public virtual void EnqueueLow(T data)
2257 {
2258 Enqueue(m_lowQueue, data);
2259 }
2260
2261 public virtual void EnqueueHigh(T data)
2262 {
2263 Enqueue(m_highQueue, data);
2264 }
2265
2266 private void Enqueue(Queue<T> q, T data)
2267 {
2268 lock (m_syncRoot)
2269 {
2270 m_lowQueue.Enqueue(data);
2271 m_s.WaitOne(0);
2272 m_s.Release();
2273 }
2274 }
2275
2276 public virtual T Dequeue()
2277 {
2278 return Dequeue(Timeout.Infinite);
2279 }
2280
2281 public virtual T Dequeue(int tmo)
2282 {
2283 return Dequeue(TimeSpan.FromMilliseconds(tmo));
2284 }
2285
2286 public virtual T Dequeue(TimeSpan wait)
2287 {
2288 T res = null;
2289
2290 if (!Dequeue(wait, ref res))
2291 return null;
2292
2293 return res;
2294 }
2295
2296 public bool Dequeue(int timeout, ref T res)
2297 {
2298 return Dequeue(TimeSpan.FromMilliseconds(timeout), ref res);
2299 }
2300
2301 public bool Dequeue(TimeSpan wait, ref T res)
2302 {
2303 if (!m_s.WaitOne(wait))
2304 return false;
2305
2306 lock (m_syncRoot)
2307 {
2308 if (m_highQueue.Count > 0)
2309 res = m_highQueue.Dequeue();
2310 else
2311 res = m_lowQueue.Dequeue();
2312
2313 if (m_highQueue.Count == 0 && m_lowQueue.Count == 0)
2314 return true;
2315
2316 try
2317 {
2318 m_s.Release();
2319 }
2320 catch
2321 {
2322 }
2323
2324 return true;
2325 }
2326 }
2327
2328 public virtual void Clear()
2329 {
2330
2331 lock (m_syncRoot)
2332 {
2333 // Make sure sem count is 0
2334 m_s.WaitOne(0);
2335
2336 m_lowQueue.Clear();
2337 m_highQueue.Clear();
2338 }
2339 }
2340 }
2221} 2341}