SessionStateServerHandler.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 SessionStateServerHandler (SessionStateSection config)
  44. {
  45. this.config = config;
  46. }
  47. public override SessionStateStoreData CreateNewStoreData (HttpContext context, int timeout)
  48. {
  49. return new SessionStateStoreData (new SessionStateItemCollection (),
  50. SessionStateUtility.GetSessionStaticObjects(context),
  51. timeout);
  52. }
  53. public override void CreateUninitializedItem (HttpContext context, string id, int timeout)
  54. {
  55. EnsureGoodId (id, true);
  56. stateServer.CreateUninitializedItem (id, timeout);
  57. }
  58. public override void Dispose ()
  59. {
  60. }
  61. public override void EndRequest (HttpContext context)
  62. {
  63. }
  64. SessionStateStoreData GetItemInternal (HttpContext context,
  65. string id,
  66. out bool locked,
  67. out TimeSpan lockAge,
  68. out object lockId,
  69. out SessionStateActions actions,
  70. bool exclusive)
  71. {
  72. Console.WriteLine ("SessionStateServerHandler.GetItemInternal");
  73. Console.WriteLine ("\tid == {0}", id);
  74. Console.WriteLine ("\tpath == {0}", context.Request.FilePath);
  75. locked = false;
  76. lockAge = TimeSpan.MinValue;
  77. lockId = Int32.MinValue;
  78. actions = SessionStateActions.None;
  79. if (id == null)
  80. return null;
  81. StateServerItem item = stateServer.GetItem (id,
  82. out locked,
  83. out lockAge,
  84. out lockId,
  85. out actions,
  86. exclusive);
  87. if (item == null) {
  88. Console.WriteLine ("\titem is null (locked == {0}, actions == {1})", locked, actions);
  89. return null;
  90. }
  91. if (actions == SessionStateActions.InitializeItem) {
  92. Console.WriteLine ("\titem needs initialization");
  93. return CreateNewStoreData (context, item.Timeout);
  94. }
  95. SessionStateItemCollection items = null;
  96. HttpStaticObjectsCollection sobjs = null;
  97. MemoryStream stream = null;
  98. BinaryReader reader = null;
  99. try {
  100. if (item.CollectionData != null && item.CollectionData.Length > 0) {
  101. stream = new MemoryStream (item.CollectionData);
  102. reader = new BinaryReader (stream);
  103. items = SessionStateItemCollection.Deserialize (reader);
  104. } else
  105. items = new SessionStateItemCollection ();
  106. if (item.StaticObjectsData != null && item.StaticObjectsData.Length > 0)
  107. sobjs = HttpStaticObjectsCollection.FromByteArray (item.StaticObjectsData);
  108. else
  109. sobjs = new HttpStaticObjectsCollection ();
  110. } catch (Exception ex) {
  111. throw new HttpException ("Failed to retrieve session state.", ex);
  112. } finally {
  113. if (reader != null)
  114. reader.Close ();
  115. }
  116. return new SessionStateStoreData (items,
  117. sobjs,
  118. item.Timeout);
  119. }
  120. public override SessionStateStoreData GetItem (HttpContext context,
  121. string id,
  122. out bool locked,
  123. out TimeSpan lockAge,
  124. out object lockId,
  125. out SessionStateActions actions)
  126. {
  127. EnsureGoodId (id, false);
  128. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, false);
  129. }
  130. public override SessionStateStoreData GetItemExclusive (HttpContext context,
  131. string id,
  132. out bool locked,
  133. out TimeSpan lockAge,
  134. out object lockId,
  135. out SessionStateActions actions)
  136. {
  137. EnsureGoodId (id, false);
  138. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, true);
  139. }
  140. public override void Initialize (string name, NameValueCollection config)
  141. {
  142. Console.WriteLine ("SessionStateServerHandler.Initialize");
  143. if (String.IsNullOrEmpty (name))
  144. name = "Session Server handler";
  145. privateConfig = config;
  146. RemotingConfiguration.Configure (null);
  147. string cons = null, proto = null, server = null, port = null;
  148. GetConData (out proto, out server, out port);
  149. cons = String.Format ("{0}://{1}:{2}/StateServer", proto, server, port);
  150. stateServer = Activator.GetObject (typeof (RemoteStateServer), cons) as RemoteStateServer;
  151. base.Initialize (name, config);
  152. }
  153. public override void InitializeRequest (HttpContext context)
  154. {
  155. // nothing to do here
  156. }
  157. public override void ReleaseItemExclusive (HttpContext context,
  158. string id,
  159. object lockId)
  160. {
  161. EnsureGoodId (id, true);
  162. stateServer.ReleaseItemExclusive (id, lockId);
  163. }
  164. public override void RemoveItem (HttpContext context,
  165. string id,
  166. object lockId,
  167. SessionStateStoreData item)
  168. {
  169. EnsureGoodId (id, true);
  170. stateServer.Remove (id, lockId);
  171. }
  172. public override void ResetItemTimeout (HttpContext context, string id)
  173. {
  174. EnsureGoodId (id, true);
  175. stateServer.ResetItemTimeout (id);
  176. }
  177. public override void SetAndReleaseItemExclusive (HttpContext context,
  178. string id,
  179. SessionStateStoreData item,
  180. object lockId,
  181. bool newItem)
  182. {
  183. EnsureGoodId (id, true);
  184. byte[] collection_data = null;
  185. byte[] sobjs_data = null;
  186. MemoryStream stream = null;
  187. BinaryWriter writer = null;
  188. try {
  189. SessionStateItemCollection items = item.Items as SessionStateItemCollection;
  190. if (items != null && items.Count > 0) {
  191. stream = new MemoryStream ();
  192. writer = new BinaryWriter (stream);
  193. items.Serialize (writer);
  194. collection_data = stream.GetBuffer ();
  195. }
  196. HttpStaticObjectsCollection sobjs = item.StaticObjects;
  197. if (sobjs != null && sobjs.Count > 0)
  198. sobjs_data = sobjs.ToByteArray ();
  199. } catch (Exception ex) {
  200. throw new HttpException ("Failed to store session data.", ex);
  201. } finally {
  202. if (writer != null)
  203. writer.Close ();
  204. }
  205. stateServer.SetAndReleaseItemExclusive (id, collection_data, sobjs_data, lockId, item.Timeout, newItem);
  206. }
  207. public override bool SetItemExpireCallback (SessionStateItemExpireCallback expireCallback)
  208. {
  209. return false;
  210. }
  211. void EnsureGoodId (string id, bool throwOnNull)
  212. {
  213. if (id == null)
  214. if (throwOnNull)
  215. throw new HttpException ("Session ID is invalid");
  216. else
  217. return;
  218. if (id.Length > SessionIDManager.SessionIDMaxLength)
  219. throw new HttpException ("Session ID too long");
  220. }
  221. void GetConData (out string proto, out string server, out string port)
  222. {
  223. string cons = config.StateConnectionString;
  224. int ei = cons.IndexOf ('=');
  225. int ci = cons.IndexOf (':');
  226. if (ei < 0 || ci < 0)
  227. throw new HttpException ("Invalid StateConnectionString");
  228. proto = cons.Substring (0, ei);
  229. server = cons.Substring (ei+1, ci - ei - 1);
  230. port = cons.Substring (ci+1, cons.Length - ci - 1);
  231. if (proto == "tcpip")
  232. proto = "tcp";
  233. }
  234. }
  235. }
  236. #endif