Exemplo n.º 1
0
        public override Object readObj(bool isUnique, BSerializer ser)
        {
            Object obj = null;

            if (ser != null && ser.inlineInstance)
            {
                currentId = 0;
                obj       = ser.read(null, this, header.version);
                return(obj);
            }

            Dictionary <int, Object> idMap = isUnique ? null : this.idMap;

            int id = bbuf.getPointer();

            if (id > 0)
            {
                // Read type and size from stream
                int typeId = bbuf.getTypeId();
                if (typeId < 0)
                {
                    throw new BException(BExceptionC.CORRUPT, "Invalid type ID at stream position " + bbuf.position());
                }

                // If the serializer is not supplied, lookup
                // the serializer from the registry
                if (ser == null)
                {
                    ser = registry.getSerializer(typeId);
                }

                // Create and read
                currentId = id;
                obj       = ser.read(null, this, header.version);
            }
            else if (id < 0)
            {
                if (idMap == null)
                {
                    throw new BException(BExceptionC.INTERNAL, "Reference map must not be null.");
                }

                if (!idMap.TryGetValue(-id, out obj))
                {
                    string errorMessage    = "Corrupt read buffer at position=" + (bbuf.position() - 4) + ", expected reference to existing object, reference-id=" + id;
                    string messageAsString = bbuf.toDetailString();
                    string fileName        = System.IO.Path.GetTempPath() + "eloixclientcs-error.txt";
                    System.IO.File.WriteAllText(fileName, errorMessage + "\r\n" + messageAsString);
                    Console.WriteLine();
                    Console.WriteLine(messageAsString);
                    throw new BException(BExceptionC.INTERNAL, "Corrupt read buffer at position " + (bbuf.position() - 4) + ".");
                }
            }
            else
            {
                // NULL reference
            }

            return(obj);
        }
Exemplo n.º 2
0
        public void setException(Exception ex)
        {
            if (ex.GetType() == typeof(BException))
            {
                header.error = ((BException)ex).Code;
            }
            else
            {
                header.error = BExceptionC.REMOTE_ERROR;
                BSerializer ser = registry.getSerializer(ex, false);
                if (ser == null)
                {
                    ex = new BException(BExceptionC.REMOTE_ERROR, "", ex);
                }
            }
            store(ex);

            // Alle Streams schließen. Sie werden nicht gesendet.
            if (streams != null)
            {
                foreach (BContentStream bstream in streams)
                {
                    try
                    {
                        bstream.Close();
                    }
                    catch (Exception) { }
                }
                streams = null;
            }
        }
Exemplo n.º 3
0
        protected override Object loadObj(BSerializer ser)
        {
            long strmVersion = header.version;

            if (strmVersion <= 0)
            {
                throw new BException(BExceptionC.CORRUPT, "Invalid stream version " + strmVersion);
            }
            return(readObj(false, ser));
        }
Exemplo n.º 4
0
        public override Object readObj(bool isUnique, BSerializer ser)
        {
            Object obj = null;

            if (ser != null && ser.inlineInstance)
            {
                currentId = 0;
                obj       = ser.read(null, this, header.version);
                return(obj);
            }

            Dictionary <int, Object> idMap = isUnique ? null : this.idMap;

            int id = bbuf.getPointer();

            if (id > 0)
            {
                // Read type and size from stream
                int typeId = bbuf.getTypeId();
                if (typeId < 0)
                {
                    throw new BException(BExceptionC.CORRUPT, "Invalid type ID at stream position " + bbuf.position());
                }

                // If the serializer is not supplied, lookup
                // the serializer from the registry
                if (ser == null)
                {
                    ser = registry.getSerializer(typeId);
                }

                // Create and read
                currentId = id;
                obj       = ser.read(null, this, header.version);
            }
            else if (id < 0)
            {
                if (idMap == null)
                {
                    throw new BException(BExceptionC.INTERNAL, "Reference map must not be null.");
                }
                if (!idMap.TryGetValue(-id, out obj))
                {
                    throw new BException(BExceptionC.INTERNAL, "Null values must not be mapped.");
                }
            }
            else
            {
                // NULL reference
            }

            return(obj);
        }
Exemplo n.º 5
0
 public bool replaceSerializer(BSerializer bser)
 {
     BRegisteredSerializer[] ssers = getSortedSerializers();
     foreach (BRegisteredSerializer sser in ssers)
     {
         if (sser.typeId == bser.typeId)
         {
             sser.instance = bser;
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 6
0
        public override void writeObj(object obj, bool isUnique, BSerializer ser)
        {
            if (ser != null && ser.inlineInstance)
            {
                // Inline objects must not be null
                if (obj == null)
                {
                    obj = ser.read(null, null, 0);
                }

                ser.write(obj, this, header.version);
                return;
            }

            if (obj != null)
            {
                BObjMap objMap = isUnique ? null : this.objMap;

                int id_o = objMap != null?objMap.get(obj) : 0;

                if (id_o != 0)
                {
                    bbuf.putPointer(-id_o);
                }
                else
                {
                    id_o = ++nextObjId;

                    bbuf.putPointer(nextObjId);

                    if (objMap != null)
                    {
                        objMap.put(obj, id_o);
                    }

                    if (ser == null)
                    {
                        ser = registry.getSerializer(obj, true);
                    }

                    bbuf.putTypeId(ser.typeId);

                    ser.write(obj, this, header.version);
                }
            }
            else
            {
                // NULL-Referenz
                bbuf.putPointer(0);
            }
        }
Exemplo n.º 7
0
        public override void writeObj(object obj, bool isUnique, BSerializer ser)
        {
            if (ser != null && ser.inlineInstance)
            {

                // Inline objects must not be null
                if (obj == null)
                {
                    obj = ser.read(null, null, 0);
                }

                ser.write(obj, this, header.version);
                return;
            }

            if (obj != null)
            {

                BObjMap objMap = isUnique ? null : this.objMap;

                int id_o = objMap != null ? objMap.get(obj) : 0;
                if (id_o != 0)
                {
                    bbuf.putPointer(-id_o);
                }
                else
                {
                    id_o = ++nextObjId;

                    bbuf.putPointer(nextObjId);

                    if (objMap != null) objMap.put(obj, id_o);

                    if (ser == null)
                    {
                        ser = registry.getSerializer(obj, true);
                    }

                    bbuf.putTypeId(ser.typeId);

                    ser.write(obj, this, header.version);
                }
            }
            else
            {
                // NULL-Referenz
                bbuf.putPointer(0);
            }
        }
Exemplo n.º 8
0
        public BSerializer getSerializer(object obj, bool throwEx)
        {
            BSerializer ret  = null;
            Type        type = obj.GetType();

            try
            {
                int  typeId     = 0;
                long longTypeId = 0;

                while (type != typeof(Object))
                {
                    if (type == typeof(Stream))
                    {
                        typeId = BRegistry.TYPEID_STREAM;
                        break;
                    }

                    FieldInfo field = type.GetField("serialVersionUID",
                                                    System.Reflection.BindingFlags.NonPublic |
                                                    System.Reflection.BindingFlags.Public |
                                                    System.Reflection.BindingFlags.Static);
                    if (field != null)
                    {
                        longTypeId = (long)field.GetValue(null);
                        typeId     = (int)longTypeId;
                        break;
                    }

                    type = type.BaseType;
                }

                ret = getSerializer(typeId);
            }
            catch (Exception x)
            {
                if (throwEx)
                {
                    throw new BException(BExceptionC.CORRUPT,
                                         "No serializer for className=" + type + ". " +
                                         "Only classes marked with BSerializable can be serialized as \"Object\" types. " +
                                         "This error occurs e. g. if a List<Object> contains String values. String is not a BSerializable. " + x);
                }
            }

            return(ret);
        }
Exemplo n.º 9
0
        public BSerializer getSerializer(int typeId)
        {
            BRegisteredSerializer[] ssers = getSortedSerializers();
            int left = 0, right = ssers.Length;

            while (left <= right)
            {
                int idx = (right + left) / 2;

                BRegisteredSerializer rser = ssers[idx];
                if (rser.typeId == typeId)
                {
                    // rser.name must be the qualified assembly name.
                    // I don't know the assembly name that contains the serializers
                    //if (rser.instance == null)
                    //{
                    //    Type type = Type.GetType(rser.name, true);
                    //    rser.instance = (BSerializer)Activator.CreateInstance(type);
                    //}
                    return(rser.instance);
                }

                if (rser.typeId < typeId)
                {
                    left = idx + 1;
                }
                if (rser.typeId > typeId)
                {
                    right = idx - 1;
                }
            }

            BSerializer ser = getBuiltInSerializer(typeId);

            if (ser != null)
            {
                return(ser);
            }

            throw new BException(BExceptionC.CORRUPT, "No serializer for typeId=" + typeId);
        }
Exemplo n.º 10
0
	    public override Object readObj(bool isUnique, BSerializer ser) {
		    Object obj = null;
		
		    if (ser != null && ser.inlineInstance) {
                currentId = 0;
			    obj = ser.read(null, this, header.version);
			    return obj;
		    }

		    Dictionary<int, Object> idMap = isUnique ? null : this.idMap;
		
		    int id = bbuf.getPointer();
		    if (id > 0) {
			
			    // Read type and size from stream
			    int typeId = bbuf.getTypeId();
			    if (typeId < 0) throw new BException(BExceptionC.CORRUPT, "Invalid type ID at stream position " + bbuf.position());
			
			    // If the serializer is not supplied, lookup
			    // the serializer from the registry
			    if (ser == null) {
				    ser = registry.getSerializer(typeId);
			    }
			
			    // Create and read
                currentId = id;
			    obj = ser.read(null, this, header.version);
			
		    }
		    else if (id < 0) {
			    if (idMap == null) throw new BException(BExceptionC.INTERNAL, "Reference map must not be null.");
			    if (!idMap.TryGetValue(-id, out obj)) throw new BException(BExceptionC.INTERNAL, "Null values must not be mapped.");
		    }
		    else {
			    // NULL reference
		    }
		
		    return obj;

	    }
Exemplo n.º 11
0
 public BRegisteredSerializer(int typeId, String name, BSerializer instance)
 {
     this.typeId   = typeId;
     this.name     = name;
     this.instance = instance;
 }
Exemplo n.º 12
0
	    protected override Object loadObj(BSerializer ser) {
		    long strmVersion = header.version;
		    if (strmVersion <= 0) throw new BException(BExceptionC.CORRUPT, "Invalid stream version " + strmVersion);
		    return readObj(false, ser);
	    }
Exemplo n.º 13
0
 public BRegisteredSerializer(int typeId, String name, BSerializer instance)
 {
     this.typeId = typeId;
     this.name = name;
     this.instance = instance;
 }
Exemplo n.º 14
0
 public abstract Object readObj(bool isUnique, BSerializer ser);
Exemplo n.º 15
0
	    public abstract Object readObj(bool isUnique, BSerializer ser);
Exemplo n.º 16
0
 public abstract void writeObj(Object obj, bool isUnique, BSerializer ser);
Exemplo n.º 17
0
	    public abstract void writeObj(Object obj, bool isUnique, BSerializer ser);	
Exemplo n.º 18
0
 protected abstract Object loadObj(BSerializer ser);
Exemplo n.º 19
0
	    protected abstract Object loadObj(BSerializer ser);