* RemoteStateServer.cs: Update the session dictionary data and the
authorJackson Harper <jackson@novell.com>
Tue, 25 Nov 2003 21:43:29 +0000 (21:43 -0000)
committerJackson Harper <jackson@novell.com>
Tue, 25 Nov 2003 21:43:29 +0000 (21:43 -0000)
static objects data.
* SessionDictionary.cs: Remove type serialization methods, these
have been moved to a utility class. Add convenience methods for
converting to/from byte arrays.
* SessionSQLServerHandler.cs:
* SessionStateServerHandler.cs: Save/Restore static objects. Use new
to/from byte array methods.
* StateServerItem.cs: Hold static objects data.

svn path=/trunk/mcs/; revision=20439

mcs/class/System.Web/System.Web.SessionState/ChangeLog
mcs/class/System.Web/System.Web.SessionState/RemoteStateServer.cs
mcs/class/System.Web/System.Web.SessionState/SessionDictionary.cs
mcs/class/System.Web/System.Web.SessionState/SessionSQLServerHandler.cs
mcs/class/System.Web/System.Web.SessionState/SessionStateServerHandler.cs
mcs/class/System.Web/System.Web.SessionState/StateServerItem.cs

index 66a3e820db230b9f124b4f0345a0a5608adba3aa..45fae86a1d3c6cb7a2bddf6670f9420410aeac20 100644 (file)
@@ -1,3 +1,15 @@
+2003-11-25  Jackson Harper <jackson@ximian.com>
+
+       * RemoteStateServer.cs: Update the session dictionary data and the
+       static objects data.
+       * SessionDictionary.cs: Remove type serialization methods, these
+       have been moved to a utility class. Add convenience methods for
+       converting to/from byte arrays.
+       * SessionSQLServerHandler.cs: 
+       * SessionStateServerHandler.cs: Save/Restore static objects. Use new
+       to/from byte array methods.
+       * StateServerItem.cs: Hold static objects data.
+       
 2003-11-24  Jackson Harper <jackson@ximian.com>
 
        * SessionStateServerHandler.cs: Parse connection string. Need to
index 99ddd3dc061e0113595ab2f2802613ad6143b987..6ad50c6202fae83d0cd90b84420f673026e14f37 100644 (file)
@@ -27,14 +27,15 @@ namespace System.Web.SessionState {
                        table.Add (id, item);
                }
 
-               internal void Update (string id, byte [] data)
+               internal void Update (string id, byte [] dict_data, byte [] sobjs_data)
                {
                        StateServerItem item = table [id] as StateServerItem;
 
                        if (item == null)
                                return;
 
-                       item.Data = data;
+                       item.DictionaryData = dict_data;
+                       item.StaticObjectsData = sobjs_data;
                        item.Touch ();
                }
                
@@ -48,8 +49,6 @@ namespace System.Web.SessionState {
                        item.Touch ();
                        return item;
                }
-
        }
-
 }
 
index a0bf55195cfd0e31f5e50d842154f0a16e61d176..957b323128349447e8c8a44782eb7c0f8b691e3d 100644 (file)
@@ -11,34 +11,12 @@ using System;
 using System.IO;
 using System.Collections;
 using System.Collections.Specialized;
-using System.Runtime.Serialization.Formatters.Binary;
 
 namespace System.Web.SessionState {
 internal class SessionDictionary : NameObjectCollectionBase
 {
-       static ArrayList types;
        bool _dirty;
-
-       static SessionDictionary ()
-       {
-               types = new ArrayList ();
-               types.Add ("");
-               types.Add (typeof (string));
-               types.Add (typeof (int));
-               types.Add (typeof (bool));
-               types.Add (typeof (DateTime));
-               types.Add (typeof (Decimal));
-               types.Add (typeof (Byte));
-               types.Add (typeof (Char));
-               types.Add (typeof (Single));
-               types.Add (typeof (Double));
-               types.Add (typeof (short));
-               types.Add (typeof (long));
-               types.Add (typeof (ushort));
-               types.Add (typeof (uint));
-               types.Add (typeof (ulong));
-       }
-
+       
        public SessionDictionary ()
        {
        }
@@ -87,145 +65,30 @@ internal class SessionDictionary : NameObjectCollectionBase
                                w.Write (key);
                                object value = BaseGet (key);
                                if (value == null) {
-                                       w.Write (16); // types.Count + 1
+                                       w.Write (System.Web.Util.AltSerialization.NullIndex);
                                        continue;
                                }
 
-                               SerializeByType (w, value);
+                               System.Web.Util.AltSerialization.SerializeByType (w, value);
                        }
                }
        }
 
-       static void SerializeByType (BinaryWriter w, object value)
-       {
-               Type type = value.GetType ();
-               int i = types.IndexOf (type);
-               if (i == -1) {
-                       w.Write (15); // types.Count
-                       BinaryFormatter bf = new BinaryFormatter ();
-                       bf.Serialize (w.BaseStream, value);
-                       return;
-               }
-
-               w.Write (i);
-               switch (i) {
-               case 1:
-                       w.Write ((string) value);
-                       break;
-               case 2:
-                       w.Write ((int) value);
-                       break;
-               case 3:
-                       w.Write ((bool) value);
-                       break;
-               case 4:
-                       w.Write (((DateTime) value).Ticks);
-                       break;
-               case 5:
-                       w.Write ((decimal) value);
-                       break;
-               case 6:
-                       w.Write ((byte) value);
-                       break;
-               case 7:
-                       w.Write ((char) value);
-                       break;
-               case 8:
-                       w.Write ((float) value);
-                       break;
-               case 9:
-                       w.Write ((double) value);
-                       break;
-               case 10:
-                       w.Write ((short) value);
-                       break;
-               case 11:
-                       w.Write ((long) value);
-                       break;
-               case 12:
-                       w.Write ((ushort) value);
-                       break;
-               case 13:
-                       w.Write ((uint) value);
-                       break;
-               case 14:
-                       w.Write ((ulong) value);
-                       break;
-               }
-       }
-
        internal static SessionDictionary Deserialize (BinaryReader r)
        {
                SessionDictionary result = new SessionDictionary ();
                for (int i = r.ReadInt32 (); i > 0; i--) {
                        string key = r.ReadString ();
                        int index = r.ReadInt32 ();
-                       if (index == 16)
+                       if (index == System.Web.Util.AltSerialization.NullIndex)
                                result [key] = null;
                        else
-                               result [key] = DeserializeFromIndex (index, r);
+                               result [key] = System.Web.Util.AltSerialization.DeserializeFromIndex (index, r);
                }
 
                return result;
        }
 
-       static object DeserializeFromIndex (int index, BinaryReader r)
-       {
-               if (index == 15){
-                       BinaryFormatter bf = new BinaryFormatter ();
-                       return bf.Deserialize (r.BaseStream);
-               }
-
-               object value = null;
-               switch (index) {
-               case 1:
-                       value = r.ReadString ();
-                       break;
-               case 2:
-                       value = r.ReadInt32 ();
-                       break;
-               case 3:
-                       value = r.ReadBoolean ();
-                       break;
-               case 4:
-                       value = new DateTime (r.ReadInt64 ());
-                       break;
-               case 5:
-                       value = r.ReadDecimal ();
-                       break;
-               case 6:
-                       value = r.ReadByte ();
-                       break;
-               case 7:
-                       value = r.ReadChar ();
-                       break;
-               case 8:
-                       value = r.ReadSingle ();
-                       break;
-               case 9:
-                       value = r.ReadDouble ();
-                       break;
-               case 10:
-                       value = r.ReadInt16 ();
-                       break;
-               case 11:
-                       value = r.ReadInt64 ();
-                       break;
-               case 12:
-                       value = r.ReadUInt16 ();
-                       break;
-               case 13:
-                       value = r.ReadUInt32 ();
-                       break;
-               case 14:
-                       value = r.ReadUInt64 ();
-                       break;
-               }
-
-               return value;
-       }
-
-
        internal bool Dirty
        {
                get { return _dirty; }
@@ -266,6 +129,37 @@ internal class SessionDictionary : NameObjectCollectionBase
                        _dirty = true;
                }
        }
+
+       internal byte [] ToByteArray ()
+       {
+               MemoryStream stream = null;
+               try {
+                       stream = new MemoryStream ();
+                       Serialize (new BinaryWriter (stream));
+                       return stream.GetBuffer ();
+               } catch {
+                       throw;
+               } finally {
+                       if (stream != null)
+                               stream.Close ();
+               }
+       }
+
+       internal static SessionDictionary FromByteArray (byte [] data)
+       {
+               SessionDictionary result = null;
+               MemoryStream stream = null;
+               try {
+                       stream = new MemoryStream (data);
+                       result = Deserialize (new BinaryReader (stream));
+               } catch {
+                       throw;
+               } finally {
+                       if (stream != null)
+                               stream.Close ();
+               }
+               return result;
+       }
 }
 
 }
index 38f2f5ad5ec9610025be5784a8d746bd5c2058b5..ea18814a2ed59533a8439c06be4d83b6eddfbc47 100644 (file)
@@ -146,30 +146,18 @@ namespace System.Web.SessionState {
                                if (!reader.Read ())
                                        return null;
 
-                               SessionDictionary dict = null;
-                               MemoryStream stream = null;
-                               int len = (int) reader.GetBytes (reader.FieldCount-1, 0, null, 0, 0);
-                               byte[] data = new byte[len];
-                               reader.GetBytes (reader.FieldCount-1, 0, data, 0, len);
-                               try {
-                                       stream = new MemoryStream (data);
-                                       dict = SessionDictionary.Deserialize (new BinaryReader (stream));
-                               } catch {
-                                       throw;
-                               } finally {
-                                       if (stream != null)
-                                               stream.Close ();
-                               }
-
-                               session = new HttpSessionState (id, dict,
-                                               new HttpStaticObjectsCollection (),
-                                               100, true, false,
+                               SessionDictionary dict; 
+                               HttpStaticObjectsCollection sobjs;
+                               
+                               dict = SessionDictionary.FromByteArray (ReadBytes (reader, reader.FieldCount-1));
+                               sobjs = HttpStaticObjectsCollection.FromByteArray (ReadBytes (reader, reader.FieldCount-2));
+                               
+                               session = new HttpSessionState (id, dict, sobjs, 100, true, false,
                                                SessionStateMode.SQLServer, false);
                                return session;
                        } catch {
                                throw;
                        }
-
                }
 
                private void InsertSession (HttpSessionState session, int timeout)
@@ -178,7 +166,7 @@ namespace System.Web.SessionState {
                        IDataParameterCollection param;
 
                        string insert = "INSERT INTO ASPStateTempSessions VALUES " +
-                       "(:SessionID, :Created, :Expires, :Timeout, :SessionData)";
+                       "(:SessionID, :Created, :Expires, :Timeout, :StaticObjectsData, :SessionData)";
 
                        command.CommandText = insert;
 
@@ -187,8 +175,10 @@ namespace System.Web.SessionState {
                        param.Add (CreateParam (command, DbType.DateTime, ":Created", DateTime.Now));
                        param.Add (CreateParam (command, DbType.DateTime, ":Expires", Tommorow ()));
                        param.Add (CreateParam (command, DbType.Int32, ":Timeout", timeout));
+                       param.Add (CreateParam (command, DbType.Binary, ":StaticObjectData",
+                                                  session.StaticObjects.ToByteArray ()));
                        param.Add (CreateParam (command, DbType.Binary, ":SessionData",
-                                                  GetDictData (session.SessionDictionary)));
+                                                  session.SessionDictionary.ToByteArray ()));
 
                        command.ExecuteNonQuery ();
                }
@@ -206,7 +196,7 @@ namespace System.Web.SessionState {
                        param = command.Parameters;
                        param.Add (CreateParam (command, DbType.String, ":SessionID", id));
                        param.Add (CreateParam (command, DbType.Binary, ":SessionData",
-                                                               GetDictData (dict)));
+                                                               dict.ToByteArray ()));
 
                        command.ExecuteNonQuery ();
                }
@@ -221,21 +211,6 @@ namespace System.Web.SessionState {
                        return result;
                }
 
-               private byte[] GetDictData (SessionDictionary dict)
-               {
-                       MemoryStream stream = null;
-                       try {
-                               stream = new MemoryStream ();
-                               dict.Serialize (new BinaryWriter (stream));
-                               return stream.GetBuffer ();
-                       } catch {
-                               throw;
-                       } finally {
-                               if (stream != null)
-                                       stream.Close ();
-                       }
-               }
-
                private DateTime Tommorow ()
                {
                        return DateTime.Now.AddDays (1);
@@ -249,6 +224,14 @@ namespace System.Web.SessionState {
 
                        return null;
                }
+
+               private byte [] ReadBytes (IDataReader reader, int index)
+               {
+                       int len = (int) reader.GetBytes (reader.FieldCount-1, 0, null, 0, 0);
+                       byte [] data = new byte [len];
+                       reader.GetBytes (index, 0, data, 0, len);
+                       return data;
+               }
        }
 }
 
index 8270d40f17c7041c87b3cf10e282056d587e0f3c..cffc3cf786485a3c2c3296a0e2d6c4d7f319a104 100644 (file)
@@ -45,8 +45,8 @@ namespace System.Web.SessionState {
                        
                        string id = context.Session.SessionID;
                        SessionDictionary dict = context.Session.SessionDictionary;
-
-                       state_server.Update (id, GetDictData (dict));
+                       HttpStaticObjectsCollection sobjs = context.Session.StaticObjects;
+                       state_server.Update (id, dict.ToByteArray (), sobjs.ToByteArray ());
                }
 
                public bool UpdateContext (HttpContext context, SessionStateModule module)
@@ -60,17 +60,9 @@ namespace System.Web.SessionState {
                        if (id != null) {
                                item = state_server.Get (id);
                                if (item != null) {
-                                       MemoryStream stream = null;
-                                       try {
-                                               stream = new MemoryStream (item.Data);
-                                               dict = SessionDictionary.Deserialize (new BinaryReader (stream));
-                                       } catch {
-                                               throw;
-                                       } finally {
-                                               if (stream != null)
-                                                       stream.Close ();
-                                       }
-                                       session = new HttpSessionState (id, dict, new HttpStaticObjectsCollection (),
+                                       dict = SessionDictionary.FromByteArray (item.DictionaryData);
+                                       sobjs = HttpStaticObjectsCollection.FromByteArray (item.StaticObjectsData);
+                                       session = new HttpSessionState (id, dict, sobjs, 
                                                        config.Timeout, false, config.CookieLess,
                                                        SessionStateMode.StateServer, false);
                                        context.SetSession (session);
@@ -78,12 +70,10 @@ namespace System.Web.SessionState {
                                }
                        }
                        
-                       
                        id = SessionId.Create (module.Rng);
-
                        dict = new SessionDictionary ();
                        sobjs = new HttpStaticObjectsCollection ();
-                       item = new StateServerItem (GetDictData (dict), config.Timeout);
+                       item = new StateServerItem (dict.ToByteArray (), sobjs.ToByteArray (), config.Timeout);
                        
                        state_server.Insert (id, item);
 
index 6f7783a60982cf97bceb28dc0a13ca32cae405d4..14ca097a3274dc3bb96c1c7728a55fbe7dcef53e 100644 (file)
@@ -7,7 +7,6 @@
 // (C) 2003 Novell, Inc (http://www.novell.com)
 //
 
-
 using System;
 
 namespace System.Web.SessionState {
@@ -15,28 +14,29 @@ namespace System.Web.SessionState {
        [Serializable]
        public class StateServerItem {
 
-               private byte [] data;
-               //        private HttpStaticObjectsCollection static_objects;
+               private byte [] dict_data;
+               private byte [] sobjs_data;
                private DateTime last_access;
                private int timeout;
 
-               public StateServerItem (byte[] data, int timeout)
+               public StateServerItem (byte [] dict_data, byte [] sobjs_data, int timeout)
                {
-                       this.data = data;
+                       this.dict_data = dict_data;
+                       this.sobjs_data = sobjs_data;
                        this.timeout = timeout;
                        this.last_access = DateTime.Now;
                }
 
-               public byte [] Data {
-                       get { return data; }
-                       set { data = value; }
+               public byte [] DictionaryData {
+                       get { return dict_data; }
+                       set { dict_data = value; }
                }
-               
-/*
-               internal HttpStaticObjectsCollection StaticObjects {
-                       get { return static_objects; }
+
+               public byte [] StaticObjectsData {
+                       get { return sobjs_data; }
+                       set { sobjs_data = value; }
                }
-*/               
+               
                public void Touch ()
                {
                        last_access = DateTime.Now;