Beispiel #1
0
        private bool SerializeCustom(StreamBuffer dout, object serObject)
        {
            CustomType customType = default(CustomType);

            if (Protocol.TypeDict.TryGetValue(serObject.GetType(), out customType))
            {
                if (customType.SerializeStreamFunction == null)
                {
                    byte[] array = customType.SerializeFunction(serObject);
                    dout.WriteByte(99);
                    dout.WriteByte(customType.Code);
                    this.SerializeShort(dout, (short)array.Length, false);
                    dout.Write(array, 0, array.Length);
                    return(true);
                }
                dout.WriteByte(99);
                dout.WriteByte(customType.Code);
                int position = dout.Position;
                dout.Position += 2;
                short num  = customType.SerializeStreamFunction(dout, serObject);
                long  num2 = dout.Position;
                dout.Position = position;
                this.SerializeShort(dout, num, false);
                dout.Position += num;
                if (dout.Position != num2)
                {
                    throw new Exception("Serialization failed. Stream position corrupted. Should be " + num2 + " is now: " + dout.Position + " serializedLength: " + num);
                }
                return(true);
            }
            return(false);
        }
Beispiel #2
0
        private void SerializeArray(StreamBuffer dout, Array serObject, bool setType)
        {
            if (setType)
            {
                dout.WriteByte(121);
            }
            if (serObject.Length > 32767)
            {
                throw new NotSupportedException("String[] that exceed 32767 (short.MaxValue) entries are not supported. Yours is: " + serObject.Length);
            }
            this.SerializeShort(dout, (short)serObject.Length, false);
            Type   elementType = serObject.GetType().GetElementType();
            GpType codeOfType  = this.GetCodeOfType(elementType);

            if (codeOfType != 0)
            {
                dout.WriteByte((byte)codeOfType);
                if (codeOfType == GpType.Dictionary)
                {
                    bool setKeyType   = default(bool);
                    bool setValueType = default(bool);
                    this.SerializeDictionaryHeader(dout, (object)serObject, out setKeyType, out setValueType);
                    for (int i = 0; i < serObject.Length; i++)
                    {
                        object value = serObject.GetValue(i);
                        this.SerializeDictionaryElements(dout, value, setKeyType, setValueType);
                    }
                }
                else
                {
                    for (int j = 0; j < serObject.Length; j++)
                    {
                        object value2 = serObject.GetValue(j);
                        this.Serialize(dout, value2, false);
                    }
                }
                return;
            }
            CustomType customType = default(CustomType);

            if (Protocol.TypeDict.TryGetValue(elementType, out customType))
            {
                dout.WriteByte(99);
                dout.WriteByte(customType.Code);
                int   num = 0;
                short num2;
                long  num3;
                while (true)
                {
                    if (num < serObject.Length)
                    {
                        object value3 = serObject.GetValue(num);
                        if (customType.SerializeStreamFunction == null)
                        {
                            byte[] array = customType.SerializeFunction(value3);
                            this.SerializeShort(dout, (short)array.Length, false);
                            dout.Write(array, 0, array.Length);
                        }
                        else
                        {
                            int position = dout.Position;
                            dout.Position += 2;
                            num2           = customType.SerializeStreamFunction(dout, value3);
                            num3           = dout.Position;
                            dout.Position  = position;
                            this.SerializeShort(dout, num2, false);
                            dout.Position += num2;
                            if (dout.Position != num3)
                            {
                                break;
                            }
                        }
                        num++;
                        continue;
                    }
                    return;
                }
                throw new Exception("Serialization failed. Stream position corrupted. Should be " + num3 + " is now: " + dout.Position + " serializedLength: " + num2);
            }
            throw new NotSupportedException("cannot serialize array of type " + elementType);
        }