dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Runtime/Serialization


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Runtime/Serialization/Formatters/Binary BinaryFormatter.cs, 1.6, 1.7 BinaryValueWriter.cs, 1.1, 1.2
Date: Mon, 04 Aug 2003 01:21:23 -0400

Update of 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Runtime/Serialization/Formatters/Binary
In directory 
subversions:/tmp/cvs-serv2574/runtime/System/Runtime/Serialization/Formatters/Binary

Modified Files:
        BinaryFormatter.cs BinaryValueWriter.cs 
Log Message:


Continue the implementation of binary formatting.


Index: BinaryFormatter.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.cs,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** BinaryFormatter.cs  4 Aug 2003 02:11:21 -0000       1.6
--- BinaryFormatter.cs  4 Aug 2003 05:21:21 -0000       1.7
***************
*** 40,44 ****
        private FormatterTypeStyle typeFormat;
        private TypeFilterLevel filterLevel;
!       private FormatterConverter converter;
  
        // Constructor.
--- 40,44 ----
        private FormatterTypeStyle typeFormat;
        private TypeFilterLevel filterLevel;
!       internal FormatterConverter converter;
  
        // Constructor.
***************
*** 75,82 ****
                        {
                                writer.Write((byte)(BinaryElementType.Header));
!                               writer.Write(1);
!                               writer.Write((headersPresent ? 2 : -1));
!                               writer.Write(1);
!                               writer.Write(0);
                        }
  
--- 75,82 ----
                        {
                                writer.Write((byte)(BinaryElementType.Header));
!                               writer.Write(1);                                
                        // Main object.
!                               writer.Write((headersPresent ? 2 : -1));        
// Header object.
!                               writer.Write(1);                                
                        // Major version.
!                               writer.Write(0);                                
                        // Minor version
                        }
  
***************
*** 88,92 ****
  
        // Write an object to a stream.
!       private void WriteObject
                                (BinaryValueWriter.BinaryValueContext context, 
Object value)
                        {
--- 88,92 ----
  
        // Write an object to a stream.
!       internal void WriteObject
                                (BinaryValueWriter.BinaryValueContext context, 
Object value)
                        {
***************
*** 106,109 ****
--- 106,113 ----
                                bool firstTime;
                                long objectID = context.gen.GetId(value, out 
firstTime);
+                               if(typeID == -1)
+                               {
+                                       context.gen.RegisterType(type, 
objectID);
+                               }
  
                                // Get a value writer for the type.
***************
*** 112,116 ****
  
                                // Write the object header.
!                               writer.WriteObjectHeader(context, type, 
objectID, typeID);
  
                                // Write the object internals.
--- 116,121 ----
  
                                // Write the object header.
!                               writer.WriteObjectHeader
!                                       (context, value, type, objectID, 
typeID);
  
                                // Write the object internals.
***************
*** 124,127 ****
--- 129,136 ----
                        {
                                // Validate the parameters.
+                               if(graph == null)
+                               {
+                                       throw new 
ArgumentNullException("graph");
+                               }
                                if(serializationStream == null)
                                {
***************
*** 137,140 ****
--- 146,158 ----
                                                new 
BinaryValueWriter.BinaryValueContext(this, writer);
  
+                                       // Allocate object ID's 1 and 2 to the 
top-most
+                                       // object graph and the header block, 
respectively.
+                                       bool firstTime;
+                                       context.gen.GetId(graph, out firstTime);
+                                       if(headers != null)
+                                       {
+                                               context.gen.GetId(headers, out 
firstTime);
+                                       }
+ 
                                        // Write the header information.
                                        WriteHeader(writer, (headers != null));
***************
*** 151,156 ****
                                        else
                                        {
!                                               WriteObject(context, graph);
                                        }
  
                                        // Write the footer information.
--- 169,181 ----
                                        else
                                        {
!                                               if(headers != null)
!                                               {
!                                                       
context.queue.Enqueue(headers);
!                                               }
!                                               context.queue.Enqueue(graph);
                                        }
+ 
+                                       // Process outstanding queued objects.
+                                       context.ProcessQueue();
  
                                        // Write the footer information.

Index: BinaryValueWriter.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Runtime/Serialization/Formatters/Binary/BinaryValueWriter.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** BinaryValueWriter.cs        4 Aug 2003 02:11:21 -0000       1.1
--- BinaryValueWriter.cs        4 Aug 2003 05:21:21 -0000       1.2
***************
*** 49,52 ****
--- 49,53 ----
        private static BinaryValueWriter stringWriter = new StringWriter();
        private static BinaryValueWriter objectWriter = new ObjectWriter();
+       private static BinaryValueWriter infoWriter = new SurrogateWriter(null);
  #if false
        // TODO
***************
*** 77,80 ****
--- 78,111 ----
                                }
  
+               // Process queued objects.
+               public void ProcessQueue()
+                               {
+                                       Assembly assembly;
+                                       Object obj;
+                                       bool firstTime;
+                                       long objectID;
+                                       for(;;)
+                                       {
+                                               if(assemblyQueue.Count > 0)
+                                               {
+                                                       // Output a pending 
assembly reference.
+                                                       assembly = 
(assemblyQueue.Dequeue() as Assembly);
+                                                       objectID = 
gen.GetId(assembly, out firstTime);
+                                                       
writer.Write((byte)(BinaryElementType.Assembly));
+                                                       
writer.Write((int)objectID);
+                                                       WriteAssemblyName(this, 
assembly);
+                                               }
+                                               else if(queue.Count > 0)
+                                               {
+                                                       // Output a pending 
object.
+                                                       
formatter.WriteObject(this, queue.Dequeue());
+                                               }
+                                               else
+                                               {
+                                                       break;
+                                               }
+                                       }
+                               }
+ 
        }; // class BinaryValueContext
  
***************
*** 95,100 ****
        // Write the object header information for a type.
        public abstract void WriteObjectHeader(BinaryValueContext context,
!                                                                               
   Type type, long objectID,
!                                                                               
   long prevObject);
  
        // Write the object form of values for a type.
--- 126,131 ----
        // Write the object header information for a type.
        public abstract void WriteObjectHeader(BinaryValueContext context,
!                                                                               
   Object value, Type type,
!                                                                               
   long objectID, long prevObject);
  
        // Write the object form of values for a type.
***************
*** 245,248 ****
--- 276,308 ----
  #endif
  
+                               // Check for surrogates.
+                               ISurrogateSelector selector;
+                               ISerializationSurrogate surrogate;
+                               selector = context.formatter.SurrogateSelector;
+                               if(selector != null)
+                               {
+                                       surrogate = selector.GetSurrogate
+                                               (type, 
context.formatter.Context, out selector);
+                                       if(surrogate != null)
+                                       {
+                                               return new 
SurrogateWriter(surrogate);
+                                       }
+                               }
+ 
+                               // Check for types that implement ISerializable.
+                               if(typeof(ISerializable).IsAssignableFrom(type))
+                               {
+                                       return infoWriter;
+                               }
+ 
+                               // Bail out if the type is not marked with the
+                               // "serializable" flag.
+                               if(!type.IsSerializable)
+                               {
+                                       throw new SerializationException
+                                               (String.Format
+                                                       
(_("Serialize_CannotSerialize"), type));
+                               }
+ 
                                // Everything else is handled as an object.
                                return objectWriter;
***************
*** 311,316 ****
                                        else
                                        {
                                                
context.writer.Write(type.FullName);
!                                               // TODO: assembly ID
                                        }
                                }
--- 371,385 ----
                                        else
                                        {
+                                               bool firstTime;
+                                               long assemblyId;
+                                               assemblyId = context.gen.GetId
+                                                       (type.Assembly, out 
firstTime);
                                                
context.writer.Write(type.FullName);
!                                               
context.writer.Write((int)assemblyId);
!                                               if(firstTime)
!                                               {
!                                                       // We need to output 
the assembly later.
!                                                       
context.assemblyQueue.Enqueue(type.Assembly);
!                                               }
                                        }
                                }
***************
*** 342,346 ****
                                                        typeID = 
context.gen.GetIDForType(type);
                                                        objectID = 
context.gen.GetId(value, out firstTime);
!                                                       
vw.WriteObjectHeader(context, type,
                                                                                
                 objectID, typeID);
                                                        vw.WriteObject(context, 
value, type);
--- 411,419 ----
                                                        typeID = 
context.gen.GetIDForType(type);
                                                        objectID = 
context.gen.GetId(value, out firstTime);
!                                                       if(typeID == -1)
!                                                       {
!                                                               
context.gen.RegisterType(type, objectID);
!                                                       }
!                                                       
vw.WriteObjectHeader(context, value, type,
                                                                                
                 objectID, typeID);
                                                        vw.WriteObject(context, 
value, type);
***************
*** 373,378 ****
                // Write the object header information for a type.
                public override void WriteObjectHeader(BinaryValueContext 
context,
!                                                                               
           Type type, long objectID,
!                                                                               
           long prevObject)
                                {
                                        if(prevObject == -1)
--- 446,451 ----
                // Write the object header information for a type.
                public override void WriteObjectHeader(BinaryValueContext 
context,
!                                                                               
           Object value, Type type,
!                                                                               
           long objectID, long prevObject)
                                {
                                        if(prevObject == -1)
***************
*** 462,470 ****
                                                                                
 Object value, Type type)
                                {
!                                       // TODO: output the field value 
information
                                }
  
        }; // class ObjectWriter
  
        // Write primitive values.
        private abstract class PrimitiveWriter : BinaryValueWriter
--- 535,703 ----
                                                                                
 Object value, Type type)
                                {
!                                       MemberInfo[] members =
!                                               
FormatterServices.GetSerializableMembers
!                                                       (type, 
context.formatter.Context);
!                                       Object[] values =
!                                               
FormatterServices.GetObjectData(value, members);
!                                       int index;
!                                       Type fieldType;
!                                       Type valueType;
!                                       for(index = 0; index < members.Length; 
++index)
!                                       {
!                                               if(members[index] is FieldInfo)
!                                               {
!                                                       fieldType = 
((FieldInfo)(members[index]))
!                                                                               
        .FieldType;
!                                               }
!                                               else
!                                               {
!                                                       fieldType = 
((PropertyInfo)(members[index]))
!                                                                               
        .PropertyType;
!                                               }
!                                               if(values[index] != null)
!                                               {
!                                                       valueType = 
values[index].GetType();
!                                               }
!                                               else
!                                               {
!                                                       valueType = fieldType;
!                                               }
!                                               GetWriter(context, 
fieldType).WriteInline
!                                                       (context, 
values[index], valueType, fieldType);
!                                       }
                                }
  
        }; // class ObjectWriter
  
+       // Write object values using serialization surrogates.
+       private class SurrogateWriter : ObjectWriter
+       {
+               // Internal state.
+               private ISerializationSurrogate surrogate;
+ 
+               // Constructor.
+               public SurrogateWriter(ISerializationSurrogate surrogate)
+                               {
+                                       this.surrogate = surrogate;
+                               }
+ 
+               // Get object data using the prevailing surrogate.
+               private SerializationInfo GetObjectData
+                                       (BinaryValueContext context, Object 
value, Type type)
+                               {
+                                       SerializationInfo info = new 
SerializationInfo
+                                               (type, 
context.formatter.converter);
+                                       if(surrogate == null)
+                                       {
+                                               
((ISerializable)value).GetObjectData
+                                                       (info, 
context.formatter.Context);
+                                       }
+                                       else
+                                       {
+                                               surrogate.GetObjectData
+                                                       (value, info, 
context.formatter.Context);
+                                       }
+                                       return info;
+                               }
+ 
+               // Write the object header information for a type.
+               public override void WriteObjectHeader(BinaryValueContext 
context,
+                                                                               
           Object value, Type type,
+                                                                               
           long objectID, long prevObject)
+                               {
+                                       if(prevObject == -1)
+                                       {
+                                               // Write the full type 
information.
+                                               long assemblyID;
+                                               if(type.Assembly == 
Assembly.GetExecutingAssembly())
+                                               {
+                                                       context.writer.Write
+                                                               
((byte)(BinaryElementType.RuntimeObject));
+                                                       assemblyID = -1;
+                                               }
+                                               else
+                                               {
+                                                       bool firstTime;
+                                                       assemblyID = 
context.gen.GetId
+                                                               (type.Assembly, 
out firstTime);
+                                                       if(firstTime)
+                                                       {
+                                                               
context.writer.Write
+                                                                       
((byte)(BinaryElementType.Assembly));
+                                                               
context.writer.Write((int)assemblyID);
+                                                               
WriteAssemblyName(context, type.Assembly);
+                                                       }
+                                                       context.writer.Write
+                                                               
((byte)(BinaryElementType.ExternalObject));
+                                               }
+                                               
context.writer.Write((int)objectID);
+                                               
context.writer.Write(type.FullName);
+                                               SerializationInfo info = 
GetObjectData
+                                                       (context, value, type);
+                                               SerializationInfoEnumerator e = 
info.GetEnumerator();
+                                               Type objectType;
+                                               while(e.MoveNext())
+                                               {
+                                                       
context.writer.Write(e.Name);
+                                               }
+                                               e.Reset();
+                                               while(e.MoveNext())
+                                               {
+                                                       objectType = 
e.ObjectType;
+                                                       GetWriter(context, 
objectType)
+                                                               
.WriteTypeTag(context, objectType);
+                                               }
+                                               e.Reset();
+                                               while(e.MoveNext())
+                                               {
+                                                       objectType = 
e.ObjectType;
+                                                       GetWriter(context, 
objectType)
+                                                               
.WriteTypeSpec(context, objectType);
+                                               }
+                                               if(assemblyID != -1)
+                                               {
+                                                       
context.writer.Write((int)assemblyID);
+                                               }
+                                       }
+                                       else
+                                       {
+                                               // Write a short header, 
referring to a previous
+                                               // object's type information.
+                                               context.writer.Write
+                                                       
((byte)(BinaryElementType.RefTypeObject));
+                                               
context.writer.Write((int)objectID);
+                                               
context.writer.Write((int)prevObject);
+                                       }
+                               }
+ 
+               // Write the object form of values for a type.
+               public override void WriteObject(BinaryValueContext context,
+                                                                               
 Object value, Type type)
+                               {
+                                       SerializationInfo info = GetObjectData
+                                               (context, value, type);
+                                       SerializationInfoEnumerator e = 
info.GetEnumerator();
+                                       Type objectType;
+                                       Type valueType;
+                                       Object fieldValue;
+                                       while(e.MoveNext())
+                                       {
+                                               objectType = e.ObjectType;
+                                               fieldValue = e.Value;
+                                               if(value == null)
+                                               {
+                                                       valueType = objectType;
+                                               }
+                                               else
+                                               {
+                                                       valueType = 
fieldValue.GetType();
+                                               }
+                                               GetWriter(context, 
objectType).WriteInline
+                                                       (context, fieldValue, 
valueType, objectType);
+                                       }
+                               }
+ 
+       }; // class SurrogateWriter
+ 
        // Write primitive values.
        private abstract class PrimitiveWriter : BinaryValueWriter
***************
*** 502,507 ****
                // Write the object header information for a type.
                public override void WriteObjectHeader(BinaryValueContext 
context,
!                                                                               
           Type type, long objectID,
!                                                                               
           long prevObject)
                                {
                                        if(prevObject == -1)
--- 735,740 ----
                // Write the object header information for a type.
                public override void WriteObjectHeader(BinaryValueContext 
context,
!                                                                               
           Object value, Type type,
!                                                                               
           long objectID, long prevObject)
                                {
                                        if(prevObject == -1)
***************
*** 738,743 ****
                // Write the object header information for a type.
                public override void WriteObjectHeader(BinaryValueContext 
context,
!                                                                               
           Type type, long objectID,
!                                                                               
           long prevObject)
                                {
                                        if(prevObject == -1)
--- 971,976 ----
                // Write the object header information for a type.
                public override void WriteObjectHeader(BinaryValueContext 
context,
!                                                                               
           Object value, Type type,
!                                                                               
           long objectID, long prevObject)
                                {
                                        if(prevObject == -1)
***************
*** 851,856 ****
                // Write the object header information for a type.
                public override void WriteObjectHeader(BinaryValueContext 
context,
!                                                                               
           Type type, long objectID,
!                                                                               
           long prevObject)
                                {
                                        
context.writer.Write((byte)(BinaryElementType.String));
--- 1084,1089 ----
                // Write the object header information for a type.
                public override void WriteObjectHeader(BinaryValueContext 
context,
!                                                                               
           Object value, Type type,
!                                                                               
           long objectID, long prevObject)
                                {
                                        
context.writer.Write((byte)(BinaryElementType.String));





reply via email to

[Prev in Thread] Current Thread [Next in Thread]