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 BinaryElementType.cs, NONE, 1.1 BinaryMethodFlags.cs, NONE, 1.1 BinaryPrimitiveTypeCode.cs, NONE, 1.1 BinaryReturnTypeTag.cs, NONE, 1.1 BinaryTypeTag.cs, NONE, 1.1 BinaryFormatter.cs, 1.4, 1.5
Date: Sun, 03 Aug 2003 05:03:37 -0400

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

Modified Files:
        BinaryFormatter.cs 
Added Files:
        BinaryElementType.cs BinaryMethodFlags.cs 
        BinaryPrimitiveTypeCode.cs BinaryReturnTypeTag.cs 
        BinaryTypeTag.cs 
Log Message:


Begin implementing the outgoing side of the binary formatter.


--- NEW FILE ---
/*
 * BinaryElementType.cs - Implementation of the
 *      "System.Runtime.Serialization.Formatters.Binary.BinaryElementType" 
class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Runtime.Serialization.Formatters.Binary
{

#if CONFIG_REMOTING

// Type codes for individual elements within a binary stream.

internal enum BinaryElementType
{
        Header                                          = 0,
        RefTypeObject                           = 1,
        RuntimeObject                           = 4,
        ExternalObject                          = 5,
        String                                          = 6,
        GenericArray                            = 7,
        BoxedPrimitiveTypeValue         = 8,
        ObjectReference                         = 9,
        NullValue                                       = 10,
        End                                                     = 11,
        Assembly                                        = 12,
        ArrayFiller8b                           = 13,
        ArrayFiller32b                          = 14,
        ArrayOfPrimitiveType            = 15,
        ArrayOfObject                           = 16,
        ArrayOfString                           = 17,
        MethodCall                                      = 21,
        MethodResponse                          = 22,

}; // enum BinaryElementType

#endif // CONFIG_REMOTING

}; // namespace System.Runtime.Serialization.Formatters.Binary

--- NEW FILE ---
/*
 * BinaryMethodFlags.cs - Implementation of
 *      "System.Runtime.Serialization.Formatters.Binary.BinaryMethodCallFlags".
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Runtime.Serialization.Formatters.Binary
{

#if CONFIG_REMOTING

// Flags that are used on method calls and responses.

[Flags]
internal enum BinaryMethodFlags
{
        NoArguments                                     = 0x0001,
        PrimitiveArguments                      = 0x0002,
        ArgumentsInSimpleArray          = 0x0004,
        ArgumentsInMultiArray           = 0x0008,
        ExcludeLogicalCallContext       = 0x0010,
        IncludesLogicalCallContext      = 0x0040,
        IncludesSignature                       = 0x0080,

}; // enum BinaryMethodFlags

#endif // CONFIG_REMOTING

}; // namespace System.Runtime.Serialization.Formatters.Binary

--- NEW FILE ---
/*
 * BinaryPrimitiveTypeCode.cs - Implementation of
 *      
"System.Runtime.Serialization.Formatters.Binary.BinaryPrimitiveTypeCode".
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Runtime.Serialization.Formatters.Binary
{

#if CONFIG_REMOTING

// Type codes for primitive types.

internal enum BinaryPrimitiveTypeCode
{
        Boolean         = 1,
        Byte            = 2,
        Char            = 3,
        Decimal         = 5,
        Double          = 6,
        Int16           = 7,
        Int32           = 8,
        Int64           = 9,
        SByte           = 10,
        Single          = 11,
        DateTime        = 13,
        UInt16          = 14,
        UInt32          = 15,
        UInt64          = 16,
        String          = 18,

}; // enum BinaryPrimitiveTypeCode

#endif // CONFIG_REMOTING

}; // namespace System.Runtime.Serialization.Formatters.Binary

--- NEW FILE ---
/*
 * BinaryReturnTypeTag.cs - Implementation of the
 *      "System.Runtime.Serialization.Formatters.Binary.BinaryReturnTypeTag" 
class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Runtime.Serialization.Formatters.Binary
{

#if CONFIG_REMOTING

// Type tags for describing the return value for a method call.

internal enum BinaryReturnTypeTag
{
        Null                    = 2,
        PrimitiveType   = 8,
        ObjectType              = 16,
        Exception               = 32,

}; // enum BinaryReturnTypeTag

#endif // CONFIG_REMOTING

}; // namespace System.Runtime.Serialization.Formatters.Binary

--- NEW FILE ---
/*
 * BinaryTypeTag.cs - Implementation of the
 *      "System.Runtime.Serialization.Formatters.Binary.BinaryTypeTag" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Runtime.Serialization.Formatters.Binary
{

#if CONFIG_REMOTING

// Type tags for describing the type of a serialized value.

internal enum BinaryTypeTag
{
        PrimitiveType                   = 0,
        String                                  = 1,
        ObjectType                              = 2,
        RuntimeType                             = 3,
        GenericType                             = 4,
        ArrayOfObject                   = 5,
        ArrayOfString                   = 6,
        ArrayOfPrimitiveType    = 7,

}; // enum BinaryTypeTag

#endif // CONFIG_REMOTING

}; // namespace System.Runtime.Serialization.Formatters.Binary

Index: BinaryFormatter.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.cs,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** BinaryFormatter.cs  4 Jun 2003 06:38:13 -0000       1.4
--- BinaryFormatter.cs  3 Aug 2003 09:03:35 -0000       1.5
***************
*** 26,29 ****
--- 26,30 ----
  
  using System.IO;
+ using System.Reflection;
  using System.Runtime.Serialization;
  using System.Runtime.InteropServices;
***************
*** 39,47 ****
        private FormatterTypeStyle typeFormat;
        private TypeFilterLevel filterLevel;
  
        // Constructor.
        public BinaryFormatter()
                        {
!                               context = new 
StreamingContext(StreamingContextStates.All);
                        }
        public BinaryFormatter(ISurrogateSelector selector,
--- 40,53 ----
        private FormatterTypeStyle typeFormat;
        private TypeFilterLevel filterLevel;
+       private FormatterConverter converter;
  
        // Constructor.
        public BinaryFormatter()
                        {
!                               this.context = new 
StreamingContext(StreamingContextStates.All);
!                               this.assemblyFormat = 
FormatterAssemblyStyle.Full;
!                               this.typeFormat = 
FormatterTypeStyle.TypesAlways;
!                               this.filterLevel = TypeFilterLevel.Full;
!                               this.converter = new FormatterConverter();
                        }
        public BinaryFormatter(ISurrogateSelector selector,
***************
*** 50,56 ****
                                this.surrogateSelector = selector;
                                this.context = context;
                        }
  
!       // Implement the IRemotingFormatter interface.
        [TODO]
        public Object Deserialize(Stream serializationStream,
--- 56,66 ----
                                this.surrogateSelector = selector;
                                this.context = context;
+                               this.assemblyFormat = 
FormatterAssemblyStyle.Full;
+                               this.typeFormat = 
FormatterTypeStyle.TypesAlways;
+                               this.filterLevel = TypeFilterLevel.Full;
+                               this.converter = new FormatterConverter();
                        }
  
!       // Deserialize an object from a stream.
        [TODO]
        public Object Deserialize(Stream serializationStream,
***************
*** 60,68 ****
                                return null;
                        }
        [TODO]
        public void Serialize(Stream serializationStream,
                                                  Object graph, Header[] 
headers)
                        {
!                               // TODO
                        }
  
--- 70,328 ----
                                return null;
                        }
+ 
+       // Write a serialization header to a stream.
+       private static void WriteHeader(BinaryWriter writer, bool 
headersPresent)
+                       {
+                               writer.Write((byte)(BinaryElementType.Header));
+                               writer.Write(1);
+                               writer.Write((headersPresent ? 2 : -1));
+                               writer.Write(1);
+                               writer.Write(0);
+                       }
+ 
+       // Write a serialization footer to a stream.
+       private static void WriteFooter(BinaryWriter writer)
+                       {
+                               writer.Write((byte)(BinaryElementType.End));
+                       }
+ 
+       // Write an assembly reference to a stream and return its object ID.
+       private long WriteAssembly(BinaryWriter writer, ObjectIDGenerator gen,
+                                                          Assembly assembly, 
String name)
+                       {
+                               bool firstTime;
+                               long objectID = gen.GetId(assembly, out 
firstTime);
+                               if(firstTime)
+                               {
+                                       
writer.Write((byte)(BinaryElementType.Assembly));
+                                       writer.Write(objectID);
+                                       if(assemblyFormat == 
FormatterAssemblyStyle.Full)
+                                       {
+                                               writer.Write(name);
+                                       }
+                                       else
+                                       {
+                                               int index = name.IndexOf(',');
+                                               if(index != -1)
+                                               {
+                                                       
writer.Write(name.Substring(0, index));
+                                               }
+                                               else
+                                               {
+                                                       writer.Write(name);
+                                               }
+                                       }
+                               }
+                               return objectID;
+                       }
+ 
+       // Get the serialization data for an object.
+       private SerializationInfo GetObjectData(Object obj, Type type)
+                       {
+                               SerializationInfo info;
+                               info = new SerializationInfo(type, converter);
+                               if(obj is ISerializable)
+                               {
+                                       // Use the type-supplied object data 
method.
+                                       
((ISerializable)obj).GetObjectData(info, context);
+                                       return info;
+                               }
+                               if(surrogateSelector != null)
+                               {
+                                       // Look for a serialization surrogate.
+                                       ISerializationSurrogate surrogate;
+                                       ISurrogateSelector selector;
+                                       surrogate = 
surrogateSelector.GetSurrogate
+                                               (type, context, out selector);
+                                       if(surrogate != null)
+                                       {
+                                               surrogate.GetObjectData(obj, 
info, context);
+                                               return info;
+                                       }
+                               }
+                               if(type.IsSerializable)
+                               {
+                                       // Collect up fields using the default 
algorithm.
+                                       // TODO: serialize the properties also
+                                       FieldInfo[] fields = type.GetFields
+                                               (BindingFlags.Public |
+                                                BindingFlags.NonPublic |
+                                                BindingFlags.Instance);
+                                       foreach(FieldInfo field in fields)
+                                       {
+                                               if(!(field.IsNotSerialized))
+                                               {
+                                                       
info.AddValue(field.Name, field.GetValue(obj),
+                                                                               
  field.FieldType);
+                                               }
+                                       }
+                                       return info;
+                               }
+                               throw new SerializationException
+                                       
(String.Format(_("Serialize_NotSerializable"), type));
+                       }
+ 
+       // Write a type tag.
+       private static void WriteTypeTag(BinaryWriter writer, Type type)
+                       {
+                               // TODO
+                       }
+ 
+       // Write a type specification.
+       private static void WriteTypeSpec(BinaryWriter writer, Type type)
+                       {
+                               // TODO
+                       }
+ 
+       // Write the type information to a stream.
+       private static void WriteTypeInfo(BinaryWriter writer,
+                                                                         
BinaryElementType elementType,
+                                                                         
SerializationInfo info,
+                                                                         long 
assemID, int objectID, long typeID)
+                       {
+                               if(typeID != -1)
+                               {
+                                       // Reference a previous object's type 
information.
+                                       
writer.Write((byte)(BinaryElementType.RefTypeObject));
+                                       writer.Write(objectID);
+                                       writer.Write((int)typeID);
+                               }
+                               else
+                               {
+                                       // Dump type information for the first 
time.
+                                       writer.Write((byte)elementType);
+                                       writer.Write(objectID);
+                                       writer.Write(info.FullTypeName);
+                                       writer.Write(info.MemberCount);
+                                       SerializationInfoEnumerator e = 
info.GetEnumerator();
+                                       while(e.MoveNext())
+                                       {
+                                               writer.Write(e.Name);
+                                       }
+                                       e.Reset();
+                                       while(e.MoveNext())
+                                       {
+                                               WriteTypeTag(writer, 
e.ObjectType);
+                                       }
+                                       e.Reset();
+                                       while(e.MoveNext())
+                                       {
+                                               WriteTypeSpec(writer, 
e.ObjectType);
+                                       }
+                                       if(assemID != -1)
+                                       {
+                                               writer.Write((int)assemID);
+                                       }
+                               }
+                       }
+ 
+       // Write the fields of an object.
+       private void WriteObjectInfo(BinaryWriter writer, ObjectIDGenerator gen,
+                                                                Object value, 
Type type,
+                                                                
SerializationInfo info)
+                       {
+                               SerializationInfoEnumerator e = 
info.GetEnumerator();
+                               while(e.MoveNext())
+                               {
+                                       // TODO: write inline field values
+                               }
+                       }
+ 
+       // Write an object to a stream.
+       private void WriteObject(BinaryWriter writer, ObjectIDGenerator gen,
+                                                        Object value)
+                       {
+                               // Handle the null case first.
+                               if(value == null)
+                               {
+                                       
writer.Write((byte)(BinaryElementType.NullValue));
+                                       return;
+                               }
+ 
+                               // Get the type of the object and see if we've
+                               // processed the type before.
+                               Type type = value.GetType();
+                               long typeID = gen.GetIDForType(type);
+ 
+                               // Get an object identifier and check to see if 
we
+                               // already saw this object previously.
+                               bool firstTime;
+                               int objectID = (int)(gen.GetId(value, out 
firstTime));
+                               if(!firstTime)
+                               {
+                                       
writer.Write((byte)(BinaryElementType.ObjectReference));
+                                       writer.Write(objectID);
+                                       return;
+                               }
+ 
+                               // Handle strings, which are relatively easy.
+                               if(value is String)
+                               {
+                                       
writer.Write((byte)(BinaryElementType.String));
+                                       writer.Write(objectID);
+                                       writer.Write((String)value);
+                                       return;
+                               }
+ 
+                               // Determine how to serialize the object.
+                               SerializationInfo info;
+                               if(type.IsArray)
+                               {
+                                       // This is an array object.
+                                       // TODO
+                               }
+                               else if(type.Assembly == 
Assembly.GetExecutingAssembly())
+                               {
+                                       // This is a runtime object.
+                                       info = GetObjectData(value, type);
+                                       WriteTypeInfo(writer, 
BinaryElementType.RuntimeObject,
+                                                                 info, -1, 
objectID, typeID);
+                                       WriteObjectInfo(writer, gen, value, 
type, info);
+                               }
+                               else
+                               {
+                                       // This is an object within another 
assembly.
+                                       info = GetObjectData(value, type);
+                                       long assemID = WriteAssembly
+                                               (writer, gen, type.Assembly, 
info.AssemblyName);
+                                       WriteTypeInfo(writer, 
BinaryElementType.ExternalObject,
+                                                                 info, 
assemID, objectID, typeID);
+                                       WriteObjectInfo(writer, gen, value, 
type, info);
+                               }
+                       }
+ 
+       // Serialize an object to a stream.
        [TODO]
        public void Serialize(Stream serializationStream,
                                                  Object graph, Header[] 
headers)
                        {
!                               // Validate the parameters.
!                               if(serializationStream == null)
!                               {
!                                       throw new 
ArgumentNullException("serializationStream");
!                               }
! 
!                               // We need an object ID generator for 
serialization.
!                               ObjectIDGenerator gen = new ObjectIDGenerator();
! 
!                               // Wrap the stream in a binary writer.
!                               using(BinaryWriter writer =
!                                               new 
BinaryWriter(serializationStream))
!                               {
!                                       WriteHeader(writer, (headers != null));
!                                       if(graph is IMethodCallMessage)
!                                       {
!                                               // TODO
!                                       }
!                                       else if(graph is IMethodReturnMessage)
!                                       {
!                                               // TODO
!                                       }
!                                       else
!                                       {
!                                               WriteObject(writer, gen, graph);
!                                       }
!                                       WriteFooter(writer);
!                               }
                        }
  





reply via email to

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