RemoteStateServer.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // System.Web.SessionState.RemoteStateServer
  3. //
  4. // Author(s):
  5. // Jackson Harper ([email protected])
  6. // Gonzalo Paniagua ([email protected])
  7. // Marek Habersack ([email protected])
  8. //
  9. // (C) 2003-2006 Novell, Inc (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System;
  33. using System.Threading;
  34. using System.Web.Caching;
  35. namespace System.Web.SessionState {
  36. class LockableStateServerItem
  37. {
  38. public StateServerItem item;
  39. public ReaderWriterLock rwlock;
  40. public LockableStateServerItem (StateServerItem item)
  41. {
  42. this.item = item;
  43. this.rwlock = new ReaderWriterLock ();
  44. }
  45. }
  46. internal class RemoteStateServer : MarshalByRefObject {
  47. const Int32 lockAcquireTimeout = 30000;
  48. Cache cache;
  49. internal RemoteStateServer ()
  50. {
  51. cache = new Cache ();
  52. }
  53. void Insert (string id, LockableStateServerItem item)
  54. {
  55. cache.Insert (id, item, null, Cache.NoAbsoluteExpiration, new TimeSpan (0, item.item.Timeout, 0));
  56. }
  57. LockableStateServerItem Retrieve (string id)
  58. {
  59. return cache [id] as LockableStateServerItem;
  60. }
  61. internal void CreateUninitializedItem (string id, int timeout)
  62. {
  63. StateServerItem item = new StateServerItem (timeout);
  64. item.Action = SessionStateActions.InitializeItem;
  65. LockableStateServerItem cacheItem = new LockableStateServerItem (item);
  66. Insert (id, cacheItem);
  67. }
  68. internal StateServerItem GetItem (string id,
  69. out bool locked,
  70. out TimeSpan lockAge,
  71. out object lockId,
  72. out SessionStateActions actions,
  73. bool exclusive)
  74. {
  75. locked = false;
  76. lockAge = TimeSpan.MinValue;
  77. lockId = Int32.MinValue;
  78. actions = SessionStateActions.None;
  79. LockableStateServerItem item = Retrieve (id);
  80. if (item == null || item.item.IsAbandoned ())
  81. return null;
  82. try {
  83. item.rwlock.AcquireReaderLock (lockAcquireTimeout);
  84. if (item.item.Locked) {
  85. locked = true;
  86. lockAge = DateTime.UtcNow.Subtract (item.item.LockedTime);
  87. lockId = item.item.LockId;
  88. return null;
  89. }
  90. item.rwlock.ReleaseReaderLock ();
  91. if (exclusive) {
  92. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  93. item.item.Locked = true;
  94. item.item.LockedTime = DateTime.UtcNow;
  95. item.item.LockId++;
  96. lockId = item.item.LockId;
  97. }
  98. } catch {
  99. throw;
  100. } finally {
  101. if (item.rwlock.IsReaderLockHeld)
  102. item.rwlock.ReleaseReaderLock ();
  103. if (item.rwlock.IsWriterLockHeld)
  104. item.rwlock.ReleaseWriterLock ();
  105. }
  106. actions = item.item.Action;
  107. return item.item;
  108. }
  109. internal void Remove (string id, object lockid)
  110. {
  111. cache.Remove (id);
  112. }
  113. internal void ResetItemTimeout (string id)
  114. {
  115. LockableStateServerItem item = Retrieve (id);
  116. if (item == null)
  117. return;
  118. item.item.Touch ();
  119. }
  120. internal void ReleaseItemExclusive (string id, object lockId)
  121. {
  122. LockableStateServerItem item = Retrieve (id);
  123. if (item == null || item.item.LockId != (Int32)lockId)
  124. return;
  125. try {
  126. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  127. item.item.Locked = false;
  128. } catch {
  129. throw;
  130. } finally {
  131. if (item.rwlock.IsWriterLockHeld)
  132. item.rwlock.ReleaseWriterLock ();
  133. }
  134. }
  135. internal void SetAndReleaseItemExclusive (string id, byte [] collection_data, byte [] sobjs_data,
  136. object lockId, int timeout, bool newItem)
  137. {
  138. LockableStateServerItem item = Retrieve (id);
  139. bool fresh = false;
  140. if (newItem || item == null) {
  141. item = new LockableStateServerItem (new StateServerItem (collection_data, sobjs_data, timeout));
  142. item.item.LockId = (Int32)lockId;
  143. fresh = true;
  144. } else {
  145. if (item.item.LockId != (Int32)lockId)
  146. return;
  147. Remove (id, lockId);
  148. }
  149. try {
  150. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  151. item.item.Locked = false;
  152. if (!fresh) {
  153. item.item.CollectionData = collection_data;
  154. item.item.StaticObjectsData = sobjs_data;
  155. }
  156. Insert (id, item);
  157. } catch {
  158. throw;
  159. } finally {
  160. if (item.rwlock.IsWriterLockHeld)
  161. item.rwlock.ReleaseWriterLock ();
  162. }
  163. }
  164. public override object InitializeLifetimeService ()
  165. {
  166. return null; // just in case...
  167. }
  168. }
  169. }
  170. #endif