コード例 #1
0
        public static T Deserialize <T>(Packet packet)
        {
            T result;

            SerializeReader.FromBinary <T>(packet, out result);
            return(result);
        }
コード例 #2
0
 public Type GetType(Packet packet)
 {
     if (packet.CategoryId == 0)
     {
         Type result;
         SerializeReader.FromBinary <Type>(packet, out result);
         return(result);
     }
     MessageHandlerFactory.Category category;
     if (!this.categoryDict.TryGetValue(packet.CategoryId, out category))
     {
         throw new InvalidOperationException(string.Format("CategoryId '{0}' does not have registered handler.", packet.CategoryId));
     }
     return(category.type);
 }
コード例 #3
0
 public MessageHandlerFactory(bool external)
 {
     MessageHandlerFactory.Category value = default(MessageHandlerFactory.Category);
     value.type          = typeof(object);
     value.packetHandler = delegate(Packet packet, object tag)
     {
         object obj;
         SerializeReader.FromBinary <object>(packet, out obj);
         if (this.Deserialized != null)
         {
             this.Deserialized(this, new EventArgs <object>(obj));
         }
         Type type = obj.GetType();
         while (type != null)
         {
             MessageHandlerFactory.Handler handler;
             if (this.handlerDict.TryGetValue(type, out handler))
             {
                 handler.objectHandler(obj, tag);
                 return;
             }
             Type[] interfaces = type.GetInterfaces();
             for (int i = 0; i < interfaces.Length; i++)
             {
                 if (this.handlerDict.TryGetValue(interfaces[i], out handler))
                 {
                     handler.objectHandler(obj, tag);
                     return;
                 }
             }
             type = type.BaseType;
         }
         throw new InvalidOperationException(string.Format("'{0}' does not have registered handler.", obj.GetType().AssemblyQualifiedName));
     };
     this.categoryDict.Add(0, value);
     if (!external)
     {
         this.handlerDict.Add(typeof(MessageHandlerFactory.TypeConverter), new MessageHandlerFactory.Handler
         {
             objectHandler = new Action <object, object>(this.ProcessTypeConverter)
         });
     }
 }
コード例 #4
0
        public static void FromBinary <T>(Packet packet, out T value)
        {
            int             categoryId      = ClassInfo <T> .CategoryId;
            SerializeReader serializeReader = new SerializeReader(packet);

            if (packet.CategoryId == 0)
            {
                object obj;
                SerializeReaderHelper <object, SerializeReader> .Deserialize(ref serializeReader, out obj);

                value = (T)((object)obj);
                return;
            }
            if (packet.CategoryId != categoryId && typeof(T).IsSealed)
            {
                throw new SerializationException(string.Format("Unexpected category {0:X8} for type {1}", packet.CategoryId, typeof(T).AssemblyQualifiedName));
            }
            SerializeReaderHelper <T, SerializeReader> .Deserialize(ref serializeReader, out value);
        }
コード例 #5
0
        private void Register <H, T>(MethodInfo methodInfo, int categoryId)
        {
            if (!typeof(T).IsSerializable)
            {
                throw new InvalidOperationException(string.Format("'{0}' is not serializable.", typeof(T).AssemblyQualifiedName));
            }
            if (methodInfo == null)
            {
                throw new ArgumentNullException("methodInfo");
            }
            DynamicMethod dynamicMethod = new DynamicMethod(string.Format("{0}.{1}.ProcessMessage[{2}.{3}]", new object[]
            {
                typeof(MessageHandlerFactory).Namespace,
                typeof(MessageHandlerFactory).Name,
                typeof(T).Namespace,
                typeof(T).Name
            }), null, new Type[]
            {
                typeof(object),
                typeof(object)
            }, typeof(MessageHandlerFactory), true);

            dynamicMethod.DefineParameter(1, ParameterAttributes.In, "message");
            dynamicMethod.DefineParameter(2, ParameterAttributes.In, "tag");
            ILGenerator ilgenerator = dynamicMethod.GetILGenerator();

            if (methodInfo.IsStatic)
            {
                ilgenerator.Emit(OpCodes.Ldarg_0);
                ilgenerator.Emit(OpCodes.Ldarg_1);
            }
            else
            {
                ilgenerator.Emit(OpCodes.Ldarg_1);
                ilgenerator.Emit(OpCodes.Unbox_Any, typeof(H));
                ilgenerator.Emit(OpCodes.Ldarg_0);
            }
            ilgenerator.EmitCall(OpCodes.Call, methodInfo, null);
            ilgenerator.Emit(OpCodes.Ret);
            MessageHandlerFactory.Handler handlers = new MessageHandlerFactory.Handler
            {
                objectHandler = (dynamicMethod.CreateDelegate(typeof(Action <object, object>)) as Action <object, object>)
            };
            if (typeof(T).IsSealed)
            {
                handlers.packetHandler = delegate(Packet packet, object tag)
                {
                    T t;
                    SerializeReader.FromBinary <T>(packet, out t);
                    if (this.Deserialized != null)
                    {
                        this.Deserialized(this, new EventArgs <object>(t));
                    }
                    handlers.objectHandler(t, tag);
                };
            }
            this.handlerDict.Add(typeof(T), handlers);
            if (categoryId != 0)
            {
                if (!typeof(T).IsSealed || handlers.packetHandler == null)
                {
                    throw new InvalidOperationException(string.Format("'{0}' is not sealed.", typeof(T).AssemblyQualifiedName));
                }
                this.categoryDict.Add(categoryId, new MessageHandlerFactory.Category
                {
                    type          = typeof(T),
                    packetHandler = handlers.packetHandler
                });
                ClassInfo <T> .CategoryId = categoryId;
            }
        }
コード例 #6
0
        public static void Test()
        {
            SelfTest.SimpleStructure1 stest = default(SelfTest.SimpleStructure1);
            stest.Val1       = 1;
            stest.Test1      = new SelfTest.SimpleStructure2();
            stest.Test1.Val2 = 2;
            stest.Test1.Val3 = 3;
            stest.Test1.Val4 = 4;
            stest.Test1.Val5 = "5";
            stest.Test1.Val6 = new string[]
            {
                "6",
                "7",
                "8"
            };
            stest.Test1.Val7 = new byte[][]
            {
                new byte[]
                {
                    9,
                    10,
                    11,
                    12,
                    13
                },
                new byte[]
                {
                    14,
                    15,
                    16
                },
                new byte[]
                {
                    17,
                    18,
                    19,
                    20
                },
                new byte[0]
            };
            stest.Val8 = 14;
            Packet serialized = SerializeWriter.ToBinary <SelfTest.SimpleStructure1>(stest);

            for (int i = 0; i < serialized.Count; i++)
            {
                if (i != 0)
                {
                    Console.Write("-");
                }
                Console.Write("{0:x2}", serialized.Array[i + serialized.Offset]);
            }
            Console.WriteLine();
            SelfTest.SimpleStructure1 simpleStructure;
            SerializeReader.FromBinary <SelfTest.SimpleStructure1>(serialized, out simpleStructure);
            SelfTest.ComplexStructure1 stestd = new SelfTest.ComplexStructure1();
            SerializeWriter.ToBinary <SelfTest.ComplexStructure1>(stestd);
            stestd.d.h3.i[1].v6 = "Hello, world!";
            Console.WriteLine(SerializeWriter.ToBinary <SelfTest.ComplexStructure1>(stestd).Count.ToString());
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int watch = 0; watch < 1000000; watch++)
            {
                serialized = SerializeWriter.ToBinary <SelfTest.SimpleStructure1>(stest);
            }
            stopwatch.Stop();
            Console.WriteLine("Elapsed: {0}ms", stopwatch.ElapsedMilliseconds);
        }