// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
-// (C) 2002 Ximian, Inc (http://www.ximian.com)
+// (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
//
using System;
using System.Collections;
namespace System.Web.SessionState {
public sealed class HttpSessionState : ICollection, IEnumerable
{
- private NameValueCollection _state; //FIXME: it should be a ManagementNamedValueCollection
private string _id;
private SessionDictionary _dict;
private HttpStaticObjectsCollection _staticObjects;
public int Count
{
- get { return _state.Count; }
+ get { return _dict.Count; }
}
internal bool IsAbandoned
public object this [string key]
{
- get { return _state [key]; }
- set { _state [key] = (string) value; }
+ get { return _dict [key]; }
+ set { _dict [key] = value; }
}
public object this [int index]
{
- get { return _state [index]; }
+ get { return _dict [index]; }
set {
- string key = _state.Keys [index];
- _state [key] = (string) value;
+ string key = _dict.Keys [index];
+ _dict [key] = value;
}
}
public NameObjectCollectionBase.KeysCollection Keys
{
- get { return _state.Keys; }
+ get { return _dict.Keys; }
}
public int LCID
public void Add (string name, object value)
{
- if (_state == null)
- _state = new NameValueCollection ();
-
- _state.Add (name, (string) value);
+ _dict [name] = value;
}
public void Clear ()
{
- if (_state != null)
- _state.Clear ();
+ if (_dict != null)
+ _dict.Clear ();
}
public void CopyTo (Array array, int index)
{
- if (_state == null)
- _state = new NameValueCollection ();
-
- _state.CopyTo (array, index);
+ NameObjectCollectionBase.KeysCollection all = Keys;
+ for (int i = 0; i < all.Count; i++)
+ array.SetValue (_dict [all [i]], i + index);
}
public IEnumerator GetEnumerator ()
{
- if (_state == null)
- _state = new NameValueCollection ();
-
- return _state.GetEnumerator ();
+ return _dict.GetEnumerator ();
}
public void Remove (string name)
{
- if (_state != null)
- _state.Remove (name);
+ _dict.Remove (name);
}
public void RemoveAll ()
{
- if (_state != null)
- foreach (string key in _state.AllKeys)
- _state.Remove (key);
+ _dict.Clear ();
}
- [MonoTODO("Implement ManagementNameValueCollection")]
public void RemoveAt (int index)
{
- throw new NotImplementedException ();
- //if (_state != null)
- // _state.RemoveAt (index);
+ _dict.RemoveAt (index);
}
}
}
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
-// (C) 2002 Ximian, Inc (http://www.ximian.com)
+// (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
//
+
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
{
- private bool _dirty;
+ 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 ()
{
}
- void Clear ()
+ internal void Clear ()
{
_dirty = true;
BaseClear ();
}
- [MonoTODO]
- static SessionDictionary Deserialize (BinaryReader r)
- {
- throw new NotImplementedException ();
- }
-
- public string GetKey (int index)
+ internal string GetKey (int index)
{
return BaseGetKey (index);
}
- [MonoTODO]
- static bool IsInmutable (object o)
+ internal static bool IsInmutable (object o)
{
- throw new NotImplementedException ();
+ Type t = o.GetType ();
+ return (t == typeof (string) || t.IsPrimitive);
}
- void Remove (string s)
+ internal void Remove (string s)
{
BaseRemove (s);
_dirty = true;
}
- void RemoveAt (int index)
+ internal void RemoveAt (int index)
{
BaseRemoveAt (index);
_dirty = true;
}
- [MonoTODO]
- void Serialize(BinaryWriter w)
+ internal void Serialize (BinaryWriter w)
{
- throw new NotImplementedException ();
+ w.Write (Count);
+ foreach (string key in Keys) {
+ w.Write (key);
+ object value = BaseGet (key);
+ if (value == null) {
+ w.Write (16); // types.Count + 1
+ continue;
+ }
+
+ SerializeByType (w, value);
+ }
}
- bool Dirty
+ 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)
+ result [key] = null;
+ else
+ result [key] = 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; }
set { _dirty = value; }