SessionStateServerHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 || context.Session.IsReadOnly)
  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 HttpSessionState UpdateContext (HttpContext context, SessionStateModule module,
  43. bool required, bool read_only, ref bool isNew)
  44. {
  45. if (!required)
  46. return null;
  47. StateServerItem item = null;
  48. HttpSessionState session = null;
  49. SessionDictionary dict = null;
  50. HttpStaticObjectsCollection sobjs = null;
  51. string id = GetId (context);
  52. if (id != null) {
  53. item = state_server.Get (id);
  54. if (item != null) {
  55. dict = SessionDictionary.FromByteArray (item.DictionaryData);
  56. sobjs = HttpStaticObjectsCollection.FromByteArray (item.StaticObjectsData);
  57. session = new HttpSessionState (id, dict,
  58. HttpApplicationFactory.ApplicationState.SessionObjects,
  59. config.Timeout, false, config.CookieLess,
  60. SessionStateMode.StateServer, read_only);
  61. return session;
  62. }
  63. }
  64. id = SessionId.Create (module.Rng);
  65. dict = new SessionDictionary ();
  66. sobjs = HttpApplicationFactory.ApplicationState.SessionObjects;
  67. item = new StateServerItem (dict.ToByteArray (), sobjs.ToByteArray (), config.Timeout);
  68. state_server.Insert (id, item);
  69. session = new HttpSessionState (id, dict, sobjs, config.Timeout, true,
  70. config.CookieLess, SessionStateMode.StateServer,
  71. read_only);
  72. isNew = true;
  73. return session;
  74. }
  75. private string GetId (HttpContext context)
  76. {
  77. if (!config.CookieLess &&
  78. context.Request.Cookies [CookieName] != null)
  79. return context.Request.Cookies [CookieName].Value;
  80. return null;
  81. }
  82. private byte[] GetDictData (SessionDictionary dict)
  83. {
  84. MemoryStream stream = null;
  85. try {
  86. stream = new MemoryStream ();
  87. dict.Serialize (new BinaryWriter (stream));
  88. return stream.GetBuffer ();
  89. } catch {
  90. throw;
  91. } finally {
  92. if (stream != null)
  93. stream.Close ();
  94. }
  95. }
  96. private void GetConData (string cons, out string proto, out string server, out string port)
  97. {
  98. int ei = cons.IndexOf ('=');
  99. int ci = cons.IndexOf (':');
  100. if (ei < 0 || ci < 0)
  101. throw new Exception ("Invalid StateConnectionString");
  102. proto = cons.Substring (0, ei);
  103. server = cons.Substring (ei+1, ci - ei - 1);
  104. port = cons.Substring (ci+1, cons.Length - ci - 1);
  105. if (proto == "tcpip")
  106. proto = "tcp";
  107. }
  108. }
  109. }