SessionStateServerHandler.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // System.Web.Compilation.SessionStateItemCollection
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2006 Marek Habersack
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Collections.Specialized;
  31. using System.IO;
  32. using System.Web;
  33. using System.Web.Configuration;
  34. using System.Runtime.Remoting;
  35. namespace System.Web.SessionState
  36. {
  37. internal class SessionStateServerHandler : SessionStateStoreProviderBase
  38. {
  39. private const Int32 lockAcquireTimeout = 30000;
  40. SessionStateSection config;
  41. NameValueCollection privateConfig;
  42. RemoteStateServer stateServer;
  43. public override SessionStateStoreData CreateNewStoreData (HttpContext context, int timeout)
  44. {
  45. return new SessionStateStoreData (new SessionStateItemCollection (),
  46. SessionStateUtility.GetSessionStaticObjects(context),
  47. timeout);
  48. }
  49. public override void CreateUninitializedItem (HttpContext context, string id, int timeout)
  50. {
  51. EnsureGoodId (id, true);
  52. stateServer.CreateUninitializedItem (id, timeout);
  53. }
  54. public override void Dispose ()
  55. {
  56. }
  57. public override void EndRequest (HttpContext context)
  58. {
  59. }
  60. SessionStateStoreData GetItemInternal (HttpContext context,
  61. string id,
  62. out bool locked,
  63. out TimeSpan lockAge,
  64. out object lockId,
  65. out SessionStateActions actions,
  66. bool exclusive)
  67. {
  68. Console.WriteLine ("SessionStateServerHandler.GetItemInternal");
  69. Console.WriteLine ("\tid == {0}", id);
  70. Console.WriteLine ("\tpath == {0}", context.Request.FilePath);
  71. locked = false;
  72. lockAge = TimeSpan.MinValue;
  73. lockId = Int32.MinValue;
  74. actions = SessionStateActions.None;
  75. if (id == null)
  76. return null;
  77. StateServerItem item = stateServer.GetItem (id,
  78. out locked,
  79. out lockAge,
  80. out lockId,
  81. out actions,
  82. exclusive);
  83. if (item == null) {
  84. Console.WriteLine ("\titem is null (locked == {0}, actions == {1})", locked, actions);
  85. return null;
  86. }
  87. if (actions == SessionStateActions.InitializeItem) {
  88. Console.WriteLine ("\titem needs initialization");
  89. return CreateNewStoreData (context, item.Timeout);
  90. }
  91. SessionStateItemCollection items = null;
  92. HttpStaticObjectsCollection sobjs = null;
  93. MemoryStream stream = null;
  94. BinaryReader reader = null;
  95. try {
  96. if (item.CollectionData != null && item.CollectionData.Length > 0) {
  97. stream = new MemoryStream (item.CollectionData);
  98. reader = new BinaryReader (stream);
  99. items = SessionStateItemCollection.Deserialize (reader);
  100. } else
  101. items = new SessionStateItemCollection ();
  102. if (item.StaticObjectsData != null && item.StaticObjectsData.Length > 0)
  103. sobjs = HttpStaticObjectsCollection.FromByteArray (item.StaticObjectsData);
  104. else
  105. sobjs = new HttpStaticObjectsCollection ();
  106. } catch (Exception ex) {
  107. throw new HttpException ("Failed to retrieve session state.", ex);
  108. } finally {
  109. if (reader != null)
  110. reader.Close ();
  111. }
  112. return new SessionStateStoreData (items,
  113. sobjs,
  114. item.Timeout);
  115. }
  116. public override SessionStateStoreData GetItem (HttpContext context,
  117. string id,
  118. out bool locked,
  119. out TimeSpan lockAge,
  120. out object lockId,
  121. out SessionStateActions actions)
  122. {
  123. EnsureGoodId (id, false);
  124. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, false);
  125. }
  126. public override SessionStateStoreData GetItemExclusive (HttpContext context,
  127. string id,
  128. out bool locked,
  129. out TimeSpan lockAge,
  130. out object lockId,
  131. out SessionStateActions actions)
  132. {
  133. EnsureGoodId (id, false);
  134. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, true);
  135. }
  136. public override void Initialize (string name, NameValueCollection config)
  137. {
  138. Console.WriteLine ("SessionStateServerHandler.Initialize");
  139. if (String.IsNullOrEmpty (name))
  140. name = "Session Server handler";
  141. privateConfig = config;
  142. RemotingConfiguration.Configure (null);
  143. string cons = null, proto = null, server = null, port = null;
  144. GetConData (out proto, out server, out port);
  145. cons = String.Format ("{0}://{1}:{2}/StateServer", proto, server, port);
  146. stateServer = Activator.GetObject (typeof (RemoteStateServer), cons) as RemoteStateServer;
  147. this.config = (SessionStateSection) WebConfigurationManager.GetSection ("system.web/sessionState");
  148. base.Initialize (name, config);
  149. }
  150. public override void InitializeRequest (HttpContext context)
  151. {
  152. // nothing to do here
  153. }
  154. public override void ReleaseItemExclusive (HttpContext context,
  155. string id,
  156. object lockId)
  157. {
  158. EnsureGoodId (id, true);
  159. stateServer.ReleaseItemExclusive (id, lockId);
  160. }
  161. public override void RemoveItem (HttpContext context,
  162. string id,
  163. object lockId,
  164. SessionStateStoreData item)
  165. {
  166. EnsureGoodId (id, true);
  167. stateServer.Remove (id, lockId);
  168. }
  169. public override void ResetItemTimeout (HttpContext context, string id)
  170. {
  171. EnsureGoodId (id, true);
  172. stateServer.ResetItemTimeout (id);
  173. }
  174. public override void SetAndReleaseItemExclusive (HttpContext context,
  175. string id,
  176. SessionStateStoreData item,
  177. object lockId,
  178. bool newItem)
  179. {
  180. EnsureGoodId (id, true);
  181. byte[] collection_data = null;
  182. byte[] sobjs_data = null;
  183. MemoryStream stream = null;
  184. BinaryWriter writer = null;
  185. try {
  186. SessionStateItemCollection items = item.Items as SessionStateItemCollection;
  187. if (items != null && items.Count > 0) {
  188. stream = new MemoryStream ();
  189. writer = new BinaryWriter (stream);
  190. items.Serialize (writer);
  191. collection_data = stream.GetBuffer ();
  192. }
  193. HttpStaticObjectsCollection sobjs = item.StaticObjects;
  194. if (sobjs != null && sobjs.Count > 0)
  195. sobjs_data = sobjs.ToByteArray ();
  196. } catch (Exception ex) {
  197. throw new HttpException ("Failed to store session data.", ex);
  198. } finally {
  199. if (writer != null)
  200. writer.Close ();
  201. }
  202. stateServer.SetAndReleaseItemExclusive (id, collection_data, sobjs_data, lockId, item.Timeout, newItem);
  203. }
  204. public override bool SetItemExpireCallback (SessionStateItemExpireCallback expireCallback)
  205. {
  206. return false;
  207. }
  208. void EnsureGoodId (string id, bool throwOnNull)
  209. {
  210. if (id == null)
  211. if (throwOnNull)
  212. throw new HttpException ("Session ID is invalid");
  213. else
  214. return;
  215. if (id.Length > SessionIDManager.SessionIDMaxLength)
  216. throw new HttpException ("Session ID too long");
  217. }
  218. void GetConData (out string proto, out string server, out string port)
  219. {
  220. string cons = config.StateConnectionString;
  221. int ei = cons.IndexOf ('=');
  222. int ci = cons.IndexOf (':');
  223. if (ei < 0 || ci < 0)
  224. throw new HttpException ("Invalid StateConnectionString");
  225. proto = cons.Substring (0, ei);
  226. server = cons.Substring (ei+1, ci - ei - 1);
  227. port = cons.Substring (ci+1, cons.Length - ci - 1);
  228. if (proto == "tcpip")
  229. proto = "tcp";
  230. }
  231. }
  232. }
  233. #endif