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
+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
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 ();
}
item.Touch ();
return item;
}
-
}
-
}
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 ()
{
}
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; }
_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;
+ }
}
}
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)
IDataParameterCollection param;
string insert = "INSERT INTO ASPStateTempSessions VALUES " +
- "(:SessionID, :Created, :Expires, :Timeout, :SessionData)";
+ "(:SessionID, :Created, :Expires, :Timeout, :StaticObjectsData, :SessionData)";
command.CommandText = insert;
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 ();
}
param = command.Parameters;
param.Add (CreateParam (command, DbType.String, ":SessionID", id));
param.Add (CreateParam (command, DbType.Binary, ":SessionData",
- GetDictData (dict)));
+ dict.ToByteArray ()));
command.ExecuteNonQuery ();
}
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);
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;
+ }
}
}
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)
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);
}
}
-
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);
// (C) 2003 Novell, Inc (http://www.novell.com)
//
-
using System;
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;