public virtual void TightUnmarshal( OpenWireFormat wireFormat, Object o, BinaryReader dataIn, BooleanStream bs) { }
protected virtual DataStructure TightUnmarshalNestedObject( OpenWireFormat wireFormat, BinaryReader dataIn, BooleanStream bs) { return wireFormat.TightUnmarshalNestedObject(dataIn, bs); }
public virtual void TightMarshal2( OpenWireFormat wireFormat, Object o, BinaryWriter dataOut, BooleanStream bs) { }
protected virtual int TightMarshalNestedObject1( OpenWireFormat wireFormat, DataStructure o, BooleanStream bs) { return wireFormat.TightMarshalNestedObject1(o, bs); }
protected void AssertMarshalBooleans(int count, GetBooleanValueDelegate valueDelegate) { BooleanStream bs = new BooleanStream(); for(int i = 0; i < count; i++) { bs.WriteBoolean(valueDelegate(i, count)); } MemoryStream buffer = new MemoryStream(); BinaryWriter ds = new EndianBinaryWriter(buffer); bs.Marshal(ds); ds.Write(endOfStreamMarker); // now lets read from the stream MemoryStream ins = new MemoryStream(buffer.ToArray()); BinaryReader dis = new EndianBinaryReader(ins); bs = new BooleanStream(); bs.Unmarshal(dis); for(int i = 0; i < count; i++) { bool expected = valueDelegate(i, count); try { bool actual = bs.ReadBoolean(); Assert.AreEqual(expected, actual); } catch(Exception e) { Assert.Fail("Failed to parse bool: " + i + " out of: " + count + " due to: " + e); } } int marker = dis.ReadInt32(); Assert.AreEqual(endOfStreamMarker, marker, "did not match: " + endOfStreamMarker + " and " + marker); // lets try read and we should get an exception try { dis.ReadByte(); Assert.Fail("Should have reached the end of the stream"); } catch(EndOfStreamException) { } }
public static void TightMarshalString2(String value, BinaryWriter dataOut, BooleanStream bs) { if (bs.ReadBoolean()) { // If we verified it only holds ascii values if (bs.ReadBoolean()) { dataOut.Write((short) value.Length); // now lets write the bytes char[] chars = value.ToCharArray(); for (int i = 0; i < chars.Length; i++) { dataOut.Write((byte)(chars[i]&0xFF00>>8)); } } else { dataOut.Write(value); } } }
public virtual int TightMarshal1(OpenWireFormat wireFormat, Object o, BooleanStream bs) { return 0; }
public DataStructure TightUnmarshalNestedObject(BinaryReader dis, BooleanStream bs) { if(bs.ReadBoolean()) { byte dataType = dis.ReadByte(); BaseDataStreamMarshaller dsm = GetDataStreamMarshallerForType(dataType); DataStructure data = dsm.CreateObject(); if(data.IsMarshallAware() && bs.ReadBoolean()) { dis.ReadInt32(); dis.ReadByte(); BooleanStream bs2 = new BooleanStream(); bs2.Unmarshal(dis); dsm.TightUnmarshal(this, data, dis, bs2); } else { dsm.TightUnmarshal(this, data, dis, bs); } return data; } return null; }
public int TightMarshalNestedObject1(DataStructure o, BooleanStream bs) { bs.WriteBoolean(o != null); if(null == o) { return 0; } if(o.IsMarshallAware()) { MarshallAware ma = (MarshallAware) o; byte[] sequence = ma.GetMarshalledForm(this); bs.WriteBoolean(sequence != null); if(sequence != null) { return 1 + sequence.Length; } } byte type = o.GetDataStructureType(); if(type == 0) { throw new IOException("No valid data structure type for: " + o + " of type: " + o.GetType()); } BaseDataStreamMarshaller dsm = GetDataStreamMarshallerForType(type); Tracer.Debug("Marshalling type: " + type + " with structure: " + o); return 1 + dsm.TightMarshal1(this, o, bs); }
protected virtual void TightMarshalNestedObject2( OpenWireFormat wireFormat, DataStructure o, BinaryWriter dataOut, BooleanStream bs) { wireFormat.TightMarshalNestedObject2(o, dataOut, bs); }
protected virtual int TightMarshalObjectArray1( OpenWireFormat wireFormat, DataStructure[] objects, BooleanStream bs) { if (objects != null) { int rc = 0; bs.WriteBoolean(true); rc += 2; for (int i = 0; i < objects.Length; i++) { rc += TightMarshalNestedObject1(wireFormat, objects[i], bs); } return rc; } else { bs.WriteBoolean(false); return 0; } }
public virtual long TightUnmarshalLong(OpenWireFormat wireFormat, BinaryReader dataIn, BooleanStream bs) { if (bs.ReadBoolean()) { if (bs.ReadBoolean()) { return dataIn.ReadInt64(); // dataIn.ReadInt64(); } else { return dataIn.ReadInt32(); } } else { if (bs.ReadBoolean()) { return dataIn.ReadInt16(); } else { return 0; } } }
public virtual void TightMarshalLong2( OpenWireFormat wireFormat, long o, BinaryWriter dataOut, BooleanStream bs) { if (bs.ReadBoolean()) { if (bs.ReadBoolean()) { dataOut.Write(o); } else { dataOut.Write((int)o); } } else { if (bs.ReadBoolean()) { dataOut.Write((short)o); } } }
public virtual int TightMarshalLong1(OpenWireFormat wireFormat, long o, BooleanStream bs) { if (o == 0L) { bs.WriteBoolean(false); bs.WriteBoolean(false); return 0; } else { ulong ul = (ulong) o; if ((ul & 0xFFFFFFFFFFFF0000ul) == 0L) { bs.WriteBoolean(false); bs.WriteBoolean(true); return 2; } else if ((ul & 0xFFFFFFFF00000000ul) == 0L) { bs.WriteBoolean(true); bs.WriteBoolean(false); return 4; } else { bs.WriteBoolean(true); bs.WriteBoolean(true); return 8; } } }
protected virtual int TightMarshalString1(String value, BooleanStream bs) { bs.WriteBoolean(value != null); if (value != null) { int strlen = value.Length; int utflen = 0; int c = 0; bool isOnlyAscii = true; char[] charr = value.ToCharArray(); for (int i = 0; i < strlen; i++) { c = charr[i]; if ((c >= 0x0001) && (c <= 0x007F)) { utflen++; } else if (c > 0x07FF) { utflen += 3; isOnlyAscii = false; } else { isOnlyAscii = false; utflen += 2; } } if (utflen >= Int16.MaxValue) throw new IOException("Encountered a String value that is too long to encode."); bs.WriteBoolean(isOnlyAscii); return utflen + 2; } else { return 0; } }
protected virtual String TightUnmarshalString(BinaryReader dataIn, BooleanStream bs) { if(bs.ReadBoolean()) { if(bs.ReadBoolean()) { return ReadAsciiString(dataIn); } else { return dataIn.ReadString(); } } else { return null; } }
protected virtual void TightMarshalObjectArray2( OpenWireFormat wireFormat, DataStructure[] objects, BinaryWriter dataOut, BooleanStream bs) { if (bs.ReadBoolean()) { dataOut.Write((short) objects.Length); for (int i = 0; i < objects.Length; i++) { TightMarshalNestedObject2(wireFormat, objects[i], dataOut, bs); } } }
protected virtual BrokerError TightUnmarshalBrokerError( OpenWireFormat wireFormat, BinaryReader dataIn, BooleanStream bs) { if (bs.ReadBoolean()) { BrokerError answer = new BrokerError(); answer.ExceptionClass = TightUnmarshalString(dataIn, bs); answer.Message = TightUnmarshalString(dataIn, bs); if (wireFormat.StackTraceEnabled) { short length = dataIn.ReadInt16(); StackTraceElement[] stackTrace = new StackTraceElement[length]; for (int i = 0; i < stackTrace.Length; i++) { StackTraceElement element = new StackTraceElement(); element.ClassName = TightUnmarshalString(dataIn, bs); element.MethodName = TightUnmarshalString(dataIn, bs); element.FileName = TightUnmarshalString(dataIn, bs); element.LineNumber = dataIn.ReadInt32(); stackTrace[i] = element; } answer.StackTraceElements = stackTrace; answer.Cause = TightUnmarshalBrokerError(wireFormat, dataIn, bs); } return answer; } else { return null; } }
public void Marshal(Object o, BinaryWriter ds) { int size = 1; if(o != null) { DataStructure c = (DataStructure) o; byte type = c.GetDataStructureType(); BaseDataStreamMarshaller dsm = GetDataStreamMarshallerForType(type); if(tightEncodingEnabled) { BooleanStream bs = new BooleanStream(); size += dsm.TightMarshal1(this, c, bs); size += bs.MarshalledSize(); if(!sizePrefixDisabled) { ds.Write(size); } ds.Write(type); bs.Marshal(ds); dsm.TightMarshal2(this, c, ds, bs); } else { BinaryWriter looseOut = ds; MemoryStream ms = null; // If we are prefixing then we need to first write it to memory, // otherwise we can write direct to the stream. if(!sizePrefixDisabled) { ms = new MemoryStream(); looseOut = new EndianBinaryWriter(ms); looseOut.Write(size); } looseOut.Write(type); dsm.LooseMarshal(this, c, looseOut); if(!sizePrefixDisabled) { ms.Position = 0; looseOut.Write((int) ms.Length - 4); ds.Write(ms.GetBuffer(), 0, (int) ms.Length); } } } else { ds.Write(size); ds.Write(NULL_TYPE); } }
protected int TightMarshalBrokerError1(OpenWireFormat wireFormat, BrokerError o, BooleanStream bs) { if (o == null) { bs.WriteBoolean(false); return 0; } else { int rc = 0; bs.WriteBoolean(true); rc += TightMarshalString1(o.ExceptionClass, bs); rc += TightMarshalString1(o.Message, bs); if (wireFormat.StackTraceEnabled) { rc += 2; StackTraceElement[] stackTrace = o.StackTraceElements; for (int i = 0; i < stackTrace.Length; i++) { StackTraceElement element = stackTrace[i]; rc += TightMarshalString1(element.ClassName, bs); rc += TightMarshalString1(element.MethodName, bs); rc += TightMarshalString1(element.FileName, bs); rc += 4; } rc += TightMarshalBrokerError1(wireFormat, o.Cause, bs); } return rc; } }
public void TightMarshalNestedObject2(DataStructure o, BinaryWriter ds, BooleanStream bs) { if(!bs.ReadBoolean()) { return; } byte type = o.GetDataStructureType(); ds.Write(type); if(o.IsMarshallAware() && bs.ReadBoolean()) { MarshallAware ma = (MarshallAware) o; byte[] sequence = ma.GetMarshalledForm(this); ds.Write(sequence, 0, sequence.Length); } else { BaseDataStreamMarshaller dsm = GetDataStreamMarshallerForType(type); dsm.TightMarshal2(this, o, ds, bs); } }
protected void TightMarshalBrokerError2( OpenWireFormat wireFormat, BrokerError o, BinaryWriter dataOut, BooleanStream bs) { if (bs.ReadBoolean()) { TightMarshalString2(o.ExceptionClass, dataOut, bs); TightMarshalString2(o.Message, dataOut, bs); if (wireFormat.StackTraceEnabled) { StackTraceElement[] stackTrace = o.StackTraceElements; dataOut.Write((short) stackTrace.Length); for (int i = 0; i < stackTrace.Length; i++) { StackTraceElement element = stackTrace[i]; TightMarshalString2(element.ClassName, dataOut, bs); TightMarshalString2(element.MethodName, dataOut, bs); TightMarshalString2(element.FileName, dataOut, bs); dataOut.Write(element.LineNumber); } TightMarshalBrokerError2(wireFormat, o.Cause, dataOut, bs); } } }
public Object Unmarshal(BinaryReader dis) { // lets ignore the size of the packet if(!sizePrefixDisabled) { dis.ReadInt32(); } // first byte is the type of the packet byte dataType = dis.ReadByte(); if(dataType != NULL_TYPE) { BaseDataStreamMarshaller dsm = GetDataStreamMarshallerForType(dataType); Object data = dsm.CreateObject(); if(tightEncodingEnabled) { BooleanStream bs = new BooleanStream(); bs.Unmarshal(dis); dsm.TightUnmarshal(this, data, dis, bs); return data; } else { dsm.LooseUnmarshal(this, data, dis); return data; } } return null; }
public void Marshal(Object o, BinaryWriter ds) { int size = 1; if (o != null) { DataStructure c = (DataStructure)o; byte type = c.GetDataStructureType(); BaseDataStreamMarshaller dsm; bool _tightEncodingEnabled; bool _sizePrefixDisabled; lock (this.marshalLock) { dsm = GetDataStreamMarshallerForType(type); _tightEncodingEnabled = this.tightEncodingEnabled; _sizePrefixDisabled = this.sizePrefixDisabled; } if (_tightEncodingEnabled) { BooleanStream bs = new BooleanStream(); size += dsm.TightMarshal1(this, c, bs); size += bs.MarshalledSize(); if (!_sizePrefixDisabled) { ds.Write(size); } ds.Write(type); bs.Marshal(ds); dsm.TightMarshal2(this, c, ds, bs); } else { BinaryWriter looseOut = ds; MemoryStream ms = null; // If we are prefixing then we need to first write it to memory, // otherwise we can write direct to the stream. if (!_sizePrefixDisabled) { ms = new MemoryStream(); looseOut = new OpenWireBinaryWriter(ms); looseOut.Write(size); } looseOut.Write(type); dsm.LooseMarshal(this, c, looseOut); if (!_sizePrefixDisabled) { ms.Position = 0; looseOut.Write((int)ms.Length - 4); ds.Write(ms.GetBuffer(), 0, (int)ms.Length); } } } else { ds.Write(size); ds.Write(NULL_TYPE); } }