diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Util.cs | 154 |
1 files changed, 137 insertions, 17 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index d9148fb..0545365 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)); |
@@ -1189,19 +1201,19 @@ namespace OpenSim.Framework | |||
1189 | { | 1201 | { |
1190 | string os = String.Empty; | 1202 | string os = String.Empty; |
1191 | 1203 | ||
1192 | if (Environment.OSVersion.Platform != PlatformID.Unix) | 1204 | // if (Environment.OSVersion.Platform != PlatformID.Unix) |
1193 | { | 1205 | // { |
1194 | os = Environment.OSVersion.ToString(); | 1206 | // os = Environment.OSVersion.ToString(); |
1195 | } | 1207 | // } |
1196 | else | 1208 | // else |
1197 | { | 1209 | // { |
1198 | os = ReadEtcIssue(); | 1210 | // os = ReadEtcIssue(); |
1199 | } | 1211 | // } |
1200 | 1212 | // | |
1201 | if (os.Length > 45) | 1213 | // if (os.Length > 45) |
1202 | { | 1214 | // { |
1203 | os = os.Substring(0, 45); | 1215 | // os = os.Substring(0, 45); |
1204 | } | 1216 | // } |
1205 | 1217 | ||
1206 | return os; | 1218 | return os; |
1207 | } | 1219 | } |
@@ -1333,7 +1345,7 @@ namespace OpenSim.Framework | |||
1333 | 1345 | ||
1334 | public static Guid GetHashGuid(string data, string salt) | 1346 | public static Guid GetHashGuid(string data, string salt) |
1335 | { | 1347 | { |
1336 | byte[] hash = ComputeMD5Hash(data + salt); | 1348 | byte[] hash = ComputeMD5Hash(data + salt, Encoding.Default); |
1337 | 1349 | ||
1338 | //string s = BitConverter.ToString(hash); | 1350 | //string s = BitConverter.ToString(hash); |
1339 | 1351 | ||
@@ -2145,4 +2157,112 @@ namespace OpenSim.Framework | |||
2145 | return str.Replace("_", "\\_").Replace("%", "\\%"); | 2157 | return str.Replace("_", "\\_").Replace("%", "\\%"); |
2146 | } | 2158 | } |
2147 | } | 2159 | } |
2160 | |||
2161 | public class DoubleQueue<T> where T:class | ||
2162 | { | ||
2163 | private Queue<T> m_lowQueue = new Queue<T>(); | ||
2164 | private Queue<T> m_highQueue = new Queue<T>(); | ||
2165 | |||
2166 | private object m_syncRoot = new object(); | ||
2167 | private Semaphore m_s = new Semaphore(0, 1); | ||
2168 | |||
2169 | public DoubleQueue() | ||
2170 | { | ||
2171 | } | ||
2172 | |||
2173 | public virtual int Count | ||
2174 | { | ||
2175 | get { return m_highQueue.Count + m_lowQueue.Count; } | ||
2176 | } | ||
2177 | |||
2178 | public virtual void Enqueue(T data) | ||
2179 | { | ||
2180 | Enqueue(m_lowQueue, data); | ||
2181 | } | ||
2182 | |||
2183 | public virtual void EnqueueLow(T data) | ||
2184 | { | ||
2185 | Enqueue(m_lowQueue, data); | ||
2186 | } | ||
2187 | |||
2188 | public virtual void EnqueueHigh(T data) | ||
2189 | { | ||
2190 | Enqueue(m_highQueue, data); | ||
2191 | } | ||
2192 | |||
2193 | private void Enqueue(Queue<T> q, T data) | ||
2194 | { | ||
2195 | lock (m_syncRoot) | ||
2196 | { | ||
2197 | m_lowQueue.Enqueue(data); | ||
2198 | m_s.WaitOne(0); | ||
2199 | m_s.Release(); | ||
2200 | } | ||
2201 | } | ||
2202 | |||
2203 | public virtual T Dequeue() | ||
2204 | { | ||
2205 | return Dequeue(Timeout.Infinite); | ||
2206 | } | ||
2207 | |||
2208 | public virtual T Dequeue(int tmo) | ||
2209 | { | ||
2210 | return Dequeue(TimeSpan.FromMilliseconds(tmo)); | ||
2211 | } | ||
2212 | |||
2213 | public virtual T Dequeue(TimeSpan wait) | ||
2214 | { | ||
2215 | T res = null; | ||
2216 | |||
2217 | if (!Dequeue(wait, ref res)) | ||
2218 | return null; | ||
2219 | |||
2220 | return res; | ||
2221 | } | ||
2222 | |||
2223 | public bool Dequeue(int timeout, ref T res) | ||
2224 | { | ||
2225 | return Dequeue(TimeSpan.FromMilliseconds(timeout), ref res); | ||
2226 | } | ||
2227 | |||
2228 | public bool Dequeue(TimeSpan wait, ref T res) | ||
2229 | { | ||
2230 | if (!m_s.WaitOne(wait)) | ||
2231 | return false; | ||
2232 | |||
2233 | lock (m_syncRoot) | ||
2234 | { | ||
2235 | if (m_highQueue.Count > 0) | ||
2236 | res = m_highQueue.Dequeue(); | ||
2237 | else | ||
2238 | res = m_lowQueue.Dequeue(); | ||
2239 | |||
2240 | if (m_highQueue.Count == 0 && m_lowQueue.Count == 0) | ||
2241 | return true; | ||
2242 | |||
2243 | try | ||
2244 | { | ||
2245 | m_s.Release(); | ||
2246 | } | ||
2247 | catch | ||
2248 | { | ||
2249 | } | ||
2250 | |||
2251 | return true; | ||
2252 | } | ||
2253 | } | ||
2254 | |||
2255 | public virtual void Clear() | ||
2256 | { | ||
2257 | |||
2258 | lock (m_syncRoot) | ||
2259 | { | ||
2260 | // Make sure sem count is 0 | ||
2261 | m_s.WaitOne(0); | ||
2262 | |||
2263 | m_lowQueue.Clear(); | ||
2264 | m_highQueue.Clear(); | ||
2265 | } | ||
2266 | } | ||
2267 | } | ||
2148 | } | 2268 | } |