RemoteStateServer.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. using System;
  32. using System.Threading;
  33. using System.Web.Caching;
  34. namespace System.Web.SessionState {
  35. class LockableStateServerItem
  36. {
  37. public StateServerItem item;
  38. public ReaderWriterLock rwlock;
  39. public LockableStateServerItem (StateServerItem item)
  40. {
  41. this.item = item;
  42. this.rwlock = new ReaderWriterLock ();
  43. }
  44. }
  45. internal class RemoteStateServer : MarshalByRefObject {
  46. const Int32 lockAcquireTimeout = 30000;
  47. Cache cache;
  48. internal RemoteStateServer ()
  49. {
  50. cache = new Cache ();
  51. }
  52. void Insert (string id, LockableStateServerItem item)
  53. {
  54. cache.Insert (id, item, null, Cache.NoAbsoluteExpiration, new TimeSpan (0, item.item.Timeout, 0));
  55. }
  56. LockableStateServerItem Retrieve (string id)
  57. {
  58. return cache [id] as LockableStateServerItem;
  59. }
  60. internal void CreateUninitializedItem (string id, int timeout)
  61. {
  62. StateServerItem item = new StateServerItem (timeout);
  63. item.Action = SessionStateActions.InitializeItem;
  64. LockableStateServerItem cacheItem = new LockableStateServerItem (item);
  65. Insert (id, cacheItem);
  66. }
  67. internal StateServerItem GetItem (string id,
  68. out bool locked,
  69. out TimeSpan lockAge,
  70. out object lockId,
  71. out SessionStateActions actions,
  72. bool exclusive)
  73. {
  74. locked = false;
  75. lockAge = TimeSpan.MinValue;
  76. lockId = Int32.MinValue;
  77. actions = SessionStateActions.None;
  78. LockableStateServerItem item = Retrieve (id);
  79. if (item == null || item.item.IsAbandoned ())
  80. return null;
  81. try {
  82. item.rwlock.AcquireReaderLock (lockAcquireTimeout);
  83. if (item.item.Locked) {
  84. locked = true;
  85. lockAge = DateTime.UtcNow.Subtract (item.item.LockedTime);
  86. lockId = item.item.LockId;
  87. return null;
  88. }
  89. item.rwlock.ReleaseReaderLock ();
  90. if (exclusive) {
  91. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  92. item.item.Locked = true;
  93. item.item.LockedTime = DateTime.UtcNow;
  94. item.item.LockId++;
  95. lockId = item.item.LockId;
  96. }
  97. } catch {
  98. throw;
  99. } finally {
  100. if (item.rwlock.IsReaderLockHeld)
  101. item.rwlock.ReleaseReaderLock ();
  102. if (item.rwlock.IsWriterLockHeld)
  103. item.rwlock.ReleaseWriterLock ();
  104. }
  105. actions = item.item.Action;
  106. return item.item;
  107. }
  108. internal void Remove (string id, object lockid)
  109. {
  110. cache.Remove (id);
  111. }
  112. internal void ResetItemTimeout (string id)
  113. {
  114. LockableStateServerItem item = Retrieve (id);
  115. if (item == null)
  116. return;
  117. item.item.Touch ();
  118. }
  119. internal void ReleaseItemExclusive (string id, object lockId)
  120. {
  121. LockableStateServerItem item = Retrieve (id);
  122. if (item == null || item.item.LockId != (Int32)lockId)
  123. return;
  124. try {
  125. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  126. item.item.Locked = false;
  127. } catch {
  128. throw;
  129. } finally {
  130. if (item.rwlock.IsWriterLockHeld)
  131. item.rwlock.ReleaseWriterLock ();
  132. }
  133. }
  134. internal void SetAndReleaseItemExclusive (string id, byte [] collection_data, byte [] sobjs_data,
  135. object lockId, int timeout, bool newItem)
  136. {
  137. LockableStateServerItem item = Retrieve (id);
  138. bool fresh = false;
  139. if (newItem || item == null) {
  140. item = new LockableStateServerItem (new StateServerItem (collection_data, sobjs_data, timeout));
  141. item.item.LockId = (Int32)lockId;
  142. fresh = true;
  143. } else {
  144. if (item.item.LockId != (Int32)lockId)
  145. return;
  146. Remove (id, lockId);
  147. }
  148. try {
  149. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  150. item.item.Locked = false;
  151. if (!fresh) {
  152. item.item.CollectionData = collection_data;
  153. item.item.StaticObjectsData = sobjs_data;
  154. }
  155. Insert (id, item);
  156. } catch {
  157. throw;
  158. } finally {
  159. if (item.rwlock.IsWriterLockHeld)
  160. item.rwlock.ReleaseWriterLock ();
  161. }
  162. }
  163. public override object InitializeLifetimeService ()
  164. {
  165. return null; // just in case...
  166. }
  167. }
  168. }