SessionStateServerHandler.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // System.Web.SessionState.SessionStateServerHandler
  3. //
  4. // Author(s):
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2003 Novell, Inc, (http://www.novell.com)
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Reflection;
  12. using System.Configuration;
  13. using System.Runtime.Remoting;
  14. namespace System.Web.SessionState {
  15. internal class SessionStateServerHandler : ISessionHandler
  16. {
  17. const string CookieName = "ASPSESSION";
  18. const int DefTimeout = 600;
  19. private RemoteStateServer state_server;
  20. private SessionConfig config;
  21. public void Dispose ()
  22. {
  23. }
  24. public void Init (HttpApplication context, SessionConfig config)
  25. {
  26. this.config = config;
  27. RemotingConfiguration.Configure (null);
  28. string cons, proto, server, port;
  29. GetConData (config.StateConnectionString, out proto, out server, out port);
  30. cons = String.Format ("{0}://{1}:{2}/StateServer", proto, server, port);
  31. state_server = (RemoteStateServer) Activator.GetObject (typeof (RemoteStateServer), cons);
  32. }
  33. public void UpdateHandler (HttpContext context, SessionStateModule module)
  34. {
  35. if (context.Session == null)
  36. return;
  37. string id = context.Session.SessionID;
  38. SessionDictionary dict = context.Session.SessionDictionary;
  39. HttpStaticObjectsCollection sobjs = context.Session.StaticObjects;
  40. state_server.Update (id, dict.ToByteArray (), sobjs.ToByteArray ());
  41. }
  42. public bool UpdateContext (HttpContext context, SessionStateModule module)
  43. {
  44. StateServerItem item = null;
  45. HttpSessionState session = null;
  46. SessionDictionary dict = null;
  47. HttpStaticObjectsCollection sobjs = null;
  48. string id = GetId (context);
  49. if (id != null) {
  50. item = state_server.Get (id);
  51. if (item != null) {
  52. dict = SessionDictionary.FromByteArray (item.DictionaryData);
  53. sobjs = HttpStaticObjectsCollection.FromByteArray (item.StaticObjectsData);
  54. session = new HttpSessionState (id, dict, HttpApplicationFactory.ApplicationState.SessionObjects,
  55. config.Timeout, false, config.CookieLess,
  56. SessionStateMode.StateServer, module.IsReadOnly);
  57. context.SetSession (session);
  58. return false;
  59. }
  60. }
  61. id = SessionId.Create (module.Rng);
  62. dict = new SessionDictionary ();
  63. sobjs = HttpApplicationFactory.ApplicationState.SessionObjects;
  64. item = new StateServerItem (dict.ToByteArray (), sobjs.ToByteArray (), config.Timeout);
  65. state_server.Insert (id, item);
  66. session = new HttpSessionState (id, dict, sobjs, config.Timeout,
  67. true, config.CookieLess, SessionStateMode.StateServer, module.IsReadOnly);
  68. context.SetSession (session);
  69. return true;
  70. }
  71. private string GetId (HttpContext context)
  72. {
  73. if (!config.CookieLess &&
  74. context.Request.Cookies [CookieName] != null)
  75. return context.Request.Cookies [CookieName].Value;
  76. return null;
  77. }
  78. private byte[] GetDictData (SessionDictionary dict)
  79. {
  80. MemoryStream stream = null;
  81. try {
  82. stream = new MemoryStream ();
  83. dict.Serialize (new BinaryWriter (stream));
  84. return stream.GetBuffer ();
  85. } catch {
  86. throw;
  87. } finally {
  88. if (stream != null)
  89. stream.Close ();
  90. }
  91. }
  92. private void GetConData (string cons, out string proto, out string server, out string port)
  93. {
  94. int ei = cons.IndexOf ('=');
  95. int ci = cons.IndexOf (':');
  96. if (ei < 0 || ci < 0)
  97. throw new Exception ("Invalid StateConnectionString");
  98. proto = cons.Substring (0, ei);
  99. server = cons.Substring (ei+1, ci - ei - 1);
  100. port = cons.Substring (ci+1, cons.Length - ci - 1);
  101. if (proto == "tcpip")
  102. proto = "tcp";
  103. }
  104. }
  105. }