protected override void PrimitiveCollectionSerializeOverride(StreamLimiter stream, int? itemLength, int? itemCount) { var typeNode = (ListTypeNode)TypeNode; var childSerializer = (ValueValueNode)typeNode.Child.CreateSerializer(this); var writer = new EndianAwareBinaryWriter(stream, Endianness); var childSerializedType = childSerializer.TypeNode.GetSerializedType(); var list = BoundValue as IList; if (list == null) return; // Handle const-sized mismatched collections if (itemCount != null && list.Count != itemCount) { var tempList = list; list = (IList)CreateCollection(itemCount.Value); for (int i = 0; i < Math.Min(tempList.Count, list.Count); i++) list[i] = tempList[i]; } foreach (var value in list) { if (stream.IsAtLimit) break; childSerializer.Serialize(writer, value, childSerializedType, itemLength); } }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var serializationContext = CreateSerializationContext(); _binarySerializable = (IBinarySerializable)Activator.CreateInstance(TypeNode.Type); _binarySerializable.Deserialize(stream.BaseStream, Endianness, serializationContext); }
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var typeNode = (EnumTypeNode) TypeNode; var enumInfo = typeNode.EnumInfo; var value = enumInfo.EnumValues != null ? enumInfo.EnumValues[(Enum)BoundValue] : BoundValue; Serialize(stream, value, enumInfo.SerializedType, enumInfo.EnumValueLength); }
protected override void PrimitiveCollectionSerializeOverride(StreamLimiter stream, int? itemLength, int? itemCount) { var typeNode = (ArrayTypeNode)TypeNode; var childSerializer = (ValueValueNode)typeNode.Child.CreateSerializer(this); var writer = new EndianAwareBinaryWriter(stream, Endianness); var childSerializedType = childSerializer.TypeNode.GetSerializedType(); var array = BoundValue as Array; if (array == null) return; // Handle const-sized mismatched collections if (itemCount != null && array.Length != itemCount) { var tempArray = array; array = (Array)CreateCollection(itemCount.Value); Array.Copy(tempArray, array, Math.Min(tempArray.Length, array.Length)); } for (int i = 0; i < array.Length; i++) { if (stream.IsAtLimit) break; var value = array.GetValue(i); childSerializer.Serialize(writer, value, childSerializedType, itemLength); } }
protected static bool ShouldTerminate(StreamLimiter stream) { if (stream.IsAtLimit) { return(true); } return(stream.CanSeek && stream.Position >= stream.Length); }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { // try to get bounded length from limiter _remainder = stream.Remainder; object value = Deserialize(stream, TypeNode.GetSerializedType()); Value = ConvertToFieldType(value); }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var typeNode = (CollectionTypeNode)TypeNode; /* Create temporary list */ Type collectionType = typeof(List<>).MakeGenericType(typeNode.ChildType); var collection = (IList)Activator.CreateInstance(collectionType); /* Create single serializer to do all the work */ var childSerializer = (ValueValueNode)typeNode.Child.CreateSerializer(this); var reader = new EndianAwareBinaryReader(stream, Endianness); var childSerializedType = childSerializer.TypeNode.GetSerializedType(); var count = TypeNode.FieldCountBinding != null ? Convert.ToInt32(TypeNode.FieldCountBinding.GetValue(this)) : int.MaxValue; int? itemLength = null; if (TypeNode.ItemLengthBinding != null) itemLength = Convert.ToInt32(TypeNode.ItemLengthBinding.GetValue(this)); var terminationValue = typeNode.TerminationValue; var terminationChild = typeNode.TerminationChild == null ? null : typeNode.TerminationChild.CreateSerializer(this); int itemCount = 0; for (int i = 0; i < count; i++) { if (ShouldTerminate(stream)) break; /* Check termination case */ if (terminationChild != null) { using (var streamResetter = new StreamResetter(stream)) { terminationChild.Deserialize(stream, eventShuttle); if (terminationChild.Value.Equals(terminationValue)) { streamResetter.CancelReset(); break; } } } var value = childSerializer.Deserialize(reader, childSerializedType, itemLength); collection.Add(value); itemCount++; } /* Create final collection */ Value = CreateCollection(itemCount); /* Copy temp list into final collection */ for (int i = 0; i < itemCount; i++) SetCollectionValue(collection[i], i); }
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var valueStream = (Stream)Value; var valueStreamlet = TypeNode.FieldLengthBinding.IsConst ? new Streamlet(valueStream, valueStream.Position, Convert.ToInt64(TypeNode.FieldLengthBinding.ConstValue)) : new Streamlet(valueStream); valueStreamlet.CopyTo(stream); }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var typeNode = (CollectionTypeNode)TypeNode; var count = TypeNode.FieldCountBinding != null?Convert.ToInt32(TypeNode.FieldCountBinding.GetValue(this)) : int.MaxValue; var terminationValue = typeNode.TerminationValue; var terminationChild = typeNode.TerminationChild == null ? null : typeNode.TerminationChild.CreateSerializer(this); for (int i = 0; i < count; i++) { if (ShouldTerminate(stream)) { break; } /* Check termination case */ if (terminationChild != null) { using (var streamResetter = new StreamResetter(stream)) { terminationChild.Deserialize(stream, eventShuttle); if (terminationChild.Value.Equals(terminationValue)) { streamResetter.CancelReset(); break; } } } var child = typeNode.Child.CreateSerializer(this); child.Deserialize(stream, eventShuttle); /* Check child termination case */ if (TypeNode.ItemSerializeUntilBinding != null) { var itemTerminationValue = TypeNode.ItemSerializeUntilBinding.GetValue(this); var itemTerminationChild = child.GetChild(TypeNode.ItemSerializeUntilAttribute.ItemValuePath); if (itemTerminationChild.Value.Equals(itemTerminationValue)) { if (!TypeNode.ItemSerializeUntilAttribute.ExcludeLastItem) { Children.Add(child); } break; } } Children.Add(child); } }
public async Task HandleClientAsnyc(Boolean startSsl) { try { this.networkStream = this.client.GetStream(); if (startSsl) { await this.UpgradeConnectionAsync(); } else { this.streamLimiter = new StreamLimiter(networkStream, 150 * 1024 * 1024); this.streamReader = new StreamReader(this.streamLimiter, Encoding.UTF8, false, 4069, true); } this.state = new BeginState(this); do { try { this.state = await this.state.HandleStateAsync(); } catch (TimeoutException) { this.state = new CloseState(this); } catch (SecurityException) { this.state = new CloseState(this); } catch (AggregateException) { this.state = null; } catch (Exception ex) { this.Logger.LogError(ex.GetType() + ": " + ex.Message); this.state = null; } } while (this.state != null); } finally { #if NET45 this.client.Close(); #else this.client.Dispose(); #endif this.Server.ConnectionClosed(this); } }
public void Deserialize(StreamLimiter stream, EventShuttle eventShuttle) { try { var serializeWhenBindings = TypeNode.SerializeWhenBindings; if (serializeWhenBindings != null && !serializeWhenBindings.Any(binding => binding.ConditionalValue.Equals(binding.GetValue(this)))) { return; } if (TypeNode.FieldLengthBinding != null) { stream = new StreamLimiter(stream, Convert.ToInt64(TypeNode.FieldLengthBinding.GetValue(this))); } Binding fieldOffsetBinding = TypeNode.FieldOffsetBinding; using (new StreamResetter(stream, fieldOffsetBinding != null)) { if (fieldOffsetBinding != null) { stream.Position = Convert.ToInt64(fieldOffsetBinding.GetValue(this)); } DeserializeOverride(stream, eventShuttle); } } catch (EndOfStreamException e) { string reference = Name == null ? string.Format("type '{0}'", TypeNode.Type) : string.Format("member '{0}'", Name); string message = string.Format("Error deserializing '{0}'. See inner exception for detail.", reference); throw new InvalidOperationException(message, e); } catch (IOException) { throw; } catch (Exception e) { string message = string.Format("Error deserializing {0}.", Name); throw new InvalidOperationException(message, e); } }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { if (TypeNode.SubtypeBinding == null) { _valueType = TypeNode.Type; if(_valueType.IsAbstract) throw new InvalidOperationException("Abstract types must have at least one subtype binding to be deserialized."); } else { var subTypeValue = TypeNode.SubtypeBinding.GetValue(this); var matchingAttribute = TypeNode.SubtypeAttributes.SingleOrDefault( attribute => subTypeValue.Equals(Convert.ChangeType(attribute.Value, subTypeValue.GetType(), null))); _valueType = matchingAttribute == null ? null : matchingAttribute.Subtype; } // skip over if null (this may happen if subtypes are unknown during deserialization) if (_valueType != null) { var typeNode = (ObjectTypeNode) TypeNode; var subType = typeNode.GetSubType(_valueType); Children = new List<ValueNode>(subType.Children.Select(child => child.CreateSerializer(this))); ObjectDeserializeOverride(stream, eventShuttle); } /* Check if we need to read past padding */ if (TypeNode.FieldLengthBinding != null) { var length = Convert.ToInt64(TypeNode.FieldLengthBinding.GetValue(this)); if (length > stream.RelativePosition) { var padLength = length - stream.RelativePosition; var pad = new byte[padLength]; stream.Read(pad, 0, pad.Length); } } }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { /* This is weird but we need to find the base stream so we can reference it directly */ Stream baseStream = stream; while (baseStream is StreamLimiter) baseStream = (baseStream as StreamLimiter).Source; var length = TypeNode.FieldLengthBinding == null ? (long?)null : Convert.ToInt64(TypeNode.FieldLengthBinding.GetValue(this)); Value = length != null ? new Streamlet(baseStream, baseStream.Position, Convert.ToInt64(TypeNode.FieldLengthBinding.GetValue(this))) : new Streamlet(baseStream, baseStream.Position); if (length != null) stream.Seek(length.Value, SeekOrigin.Current); else stream.Seek(0, SeekOrigin.End); }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var typeNode = (EnumTypeNode)TypeNode; var enumInfo = typeNode.EnumInfo; var value = Deserialize(stream, enumInfo.SerializedType, enumInfo.EnumValueLength); if (enumInfo.ValueEnums != null) { value = enumInfo.ValueEnums[(string)value]; } Func<object, object> converter; var underlyingValue = TypeConverters.TryGetValue(enumInfo.UnderlyingType, out converter) ? converter(value) : value; Value = Enum.ToObject(TypeNode.BaseSerializedType, underlyingValue); }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var typeNode = (EnumTypeNode)TypeNode; var enumInfo = typeNode.EnumInfo; var value = Deserialize(stream, enumInfo.SerializedType, enumInfo.EnumValueLength); if (enumInfo.ValueEnums != null) { value = enumInfo.ValueEnums[(string)value]; } Func <object, object> converter; var underlyingValue = TypeConverters.TryGetValue(enumInfo.UnderlyingType, out converter) ? converter(value) : value; Value = Enum.ToObject(TypeNode.Type, underlyingValue); }
public async Task <String> UpgradeConnectionAsync() { if (this.Server.X509Certificate == null) { return("454 TLS not available due to temporary reason"); } this.sslStream = new SslStream(this.networkStream, true, null, null, EncryptionPolicy.RequireEncryption); await this.sslStream.AuthenticateAsServerAsync(this.Server.X509Certificate); if (this.streamReader != null) { this.streamReader.Dispose(); } this.streamLimiter = new StreamLimiter(this.streamLimiter, 150 * 1024 * 1024); this.streamReader = new StreamReader(this.sslStream, Encoding.UTF8, false, 4069, true); this.IsSecureSocket = true; return(null); }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { /* This is weird but we need to find the base stream so we can reference it directly */ Stream baseStream = stream.BaseStream; var length = TypeNode.FieldLengthBinding == null ? (long?)null : Convert.ToInt64(TypeNode.FieldLengthBinding.GetValue(this)); Value = length != null ? new Streamlet(baseStream, baseStream.Position, Convert.ToInt64(TypeNode.FieldLengthBinding.GetValue(this))) : new Streamlet(baseStream, baseStream.Position); if (length != null) { stream.Seek(length.Value, SeekOrigin.Current); } else { stream.Seek(0, SeekOrigin.End); } }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { throw new InvalidOperationException("Deserializing object fields not supported."); }
protected abstract void PrimitiveCollectionSerializeOverride(StreamLimiter stream, int? length, int? itemCount);
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { int? itemLength = null; if (TypeNode.ItemLengthBinding != null && TypeNode.ItemLengthBinding.IsConst) itemLength = Convert.ToInt32(TypeNode.ItemLengthBinding.ConstValue); int? itemCount = null; if (TypeNode.FieldCountBinding != null && TypeNode.FieldCountBinding.IsConst) itemCount = Convert.ToInt32(TypeNode.FieldCountBinding.ConstValue); PrimitiveCollectionSerializeOverride(stream, itemLength, itemCount); var typeNode = (CollectionTypeNode)TypeNode; /* Add termination */ if (typeNode.TerminationChild != null) { var terminationChild = typeNode.TerminationChild.CreateSerializer(this); terminationChild.Value = typeNode.TerminationValue; terminationChild.Serialize(stream, eventShuttle); } }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var typeNode = (CollectionTypeNode)TypeNode; var count = TypeNode.FieldCountBinding != null ? Convert.ToInt32(TypeNode.FieldCountBinding.GetValue(this)) : int.MaxValue; var terminationValue = typeNode.TerminationValue; var terminationChild = typeNode.TerminationChild == null ? null : typeNode.TerminationChild.CreateSerializer(this); IEnumerable<int> itemLengths = null; if (TypeNode.ItemLengthBinding != null) { var itemLengthValue = TypeNode.ItemLengthBinding.GetValue(this); var enumerableItemLengthValue = itemLengthValue as IEnumerable; itemLengths = enumerableItemLengthValue != null ? enumerableItemLengthValue.Cast<object>().Select(Convert.ToInt32) : GetInfiniteSequence(Convert.ToInt32(itemLengthValue)); } IEnumerator<int> itemLengthEnumerator = null; try { if (itemLengths != null) itemLengthEnumerator = itemLengths.GetEnumerator(); for (int i = 0; i < count; i++) { if (ShouldTerminate(stream)) break; /* Check termination case */ if (terminationChild != null) { using (var streamResetter = new StreamResetter(stream)) { terminationChild.Deserialize(stream, eventShuttle); if (terminationChild.Value.Equals(terminationValue)) { streamResetter.CancelReset(); break; } } } var child = typeNode.Child.CreateSerializer(this); if (itemLengthEnumerator != null) itemLengthEnumerator.MoveNext(); var childStream = itemLengthEnumerator == null ? stream : new StreamLimiter(stream, itemLengthEnumerator.Current); child.Deserialize(childStream, eventShuttle); /* Check child termination case */ if (TypeNode.ItemSerializeUntilBinding != null) { var itemTerminationValue = TypeNode.ItemSerializeUntilBinding.GetValue(this); var itemTerminationChild = child.GetChild(TypeNode.ItemSerializeUntilAttribute.ItemValuePath); if (itemTerminationChild.Value.Equals(itemTerminationValue)) { if (!TypeNode.ItemSerializeUntilAttribute.ExcludeLastItem) { Children.Add(child); } break; } } Children.Add(child); } } finally { if (itemLengthEnumerator != null) itemLengthEnumerator.Dispose(); } }
public abstract void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle);
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { Serialize(stream, BoundValue, TypeNode.GetSerializedType()); }
protected override IEnumerable<long> MeasureItemsOverride() { var nullStream = new NullStream(); var streamLimiter = new StreamLimiter(nullStream); var serializableChildren = GetSerializableChildren(); return serializableChildren.Select(child => { streamLimiter.RelativePosition = 0; child.Serialize(streamLimiter, null); return streamLimiter.RelativePosition; }); }
protected abstract void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle);
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { Child = ((RootTypeNode)TypeNode).Child.CreateSerializer(this); Child.Deserialize(stream, eventShuttle); }
protected static bool ShouldTerminate(StreamLimiter stream) { if (stream.IsAtLimit) return true; return stream.CanSeek && stream.Position >= stream.Length; }
protected virtual long MeasureOverride() { var nullStream = new NullStream(); var streamLimiter = new StreamLimiter(nullStream); Serialize(streamLimiter, null); return streamLimiter.RelativePosition; }
public void Serialize(StreamLimiter stream, EventShuttle eventShuttle) { try { var serializeWhenBindings = TypeNode.SerializeWhenBindings; if (serializeWhenBindings != null && !serializeWhenBindings.Any(binding => binding.ConditionalValue.Equals(binding.GetBoundValue(this)))) return; if (TypeNode.FieldLengthBinding != null && TypeNode.FieldLengthBinding.IsConst) stream = new StreamLimiter(stream, Convert.ToInt64(TypeNode.FieldLengthBinding.ConstValue)); Binding fieldOffsetBinding = TypeNode.FieldOffsetBinding; using (new StreamResetter(stream, fieldOffsetBinding != null)) { if (fieldOffsetBinding != null) stream.Position = Convert.ToInt64(fieldOffsetBinding.GetValue(this)); SerializeOverride(stream, eventShuttle); } } catch (IOException) { throw; } catch (Exception e) { string reference = Name == null ? string.Format("type '{0}'", TypeNode.Type) : string.Format("member '{0}'", Name); string message = string.Format("Error serializing {0}. See inner exception for detail.", reference); throw new InvalidOperationException(message, e); } }
protected virtual void ObjectSerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var serializableChildren = GetSerializableChildren(); var serializationContextLazy = new Lazy<BinarySerializationContext>(CreateSerializationContext); foreach (var child in serializableChildren) { if (eventShuttle != null && eventShuttle.HasSerializationSubscribers) eventShuttle.OnMemberSerializing(this, child.Name, serializationContextLazy.Value, stream.GlobalRelativePosition); child.Serialize(stream, eventShuttle); if (eventShuttle != null && eventShuttle.HasSerializationSubscribers) eventShuttle.OnMemberSerialized(this, child.Name, child.BoundValue, serializationContextLazy.Value, stream.GlobalRelativePosition); } }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { if (TypeNode.SubtypeBinding == null) { _valueType = TypeNode.Type; } else { var subTypeValue = TypeNode.SubtypeBinding.GetValue(this); var matchingAttribute = TypeNode.SubtypeAttributes.SingleOrDefault( attribute => subTypeValue.Equals(Convert.ChangeType(attribute.Value, subTypeValue.GetType(), null))); _valueType = matchingAttribute == null ? null : matchingAttribute.Subtype; } if (_valueType == null) { return; } var typeNode = (ObjectTypeNode)TypeNode; var typeChildren = typeNode.GetTypeChildren(_valueType); Children = new List <ValueNode>(typeChildren.Select(child => child.CreateSerializer(this))); var context = CreateSerializationContext(); var serializableChildren = GetSerializableChildren().ToArray(); var fixedSizeRemaining = serializableChildren.Sum(node => node.GetFixedSize()); foreach (var child in serializableChildren) { if (eventShuttle != null) { eventShuttle.OnMemberDeserializing(this, child.Name, context); } fixedSizeRemaining -= child.GetFixedSize(); var limitedStream = stream; if (child.TypeNode.FieldOffsetBinding == null && fixedSizeRemaining > 0) { limitedStream = new StreamLimiter(stream, stream.Remainder - fixedSizeRemaining); } if (ShouldTerminate(limitedStream)) { break; } child.Deserialize(limitedStream, eventShuttle); if (eventShuttle != null) { eventShuttle.OnMemberDeserialized(this, child.Name, child.Value, context); } } }
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { ObjectSerializeOverride(stream, eventShuttle); /* Check if we need to pad out object */ if (TypeNode.FieldLengthBinding != null) { var length = Convert.ToInt64(TypeNode.FieldLengthBinding.GetValue(this)); if (length > stream.RelativePosition) { var padLength = length - stream.RelativePosition; var pad = new byte[padLength]; stream.Write(pad, 0, pad.Length); } } }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { object value = Deserialize(stream, TypeNode.GetSerializedType()); Value = ConvertToFieldType(value); }
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { Child.Serialize(stream, eventShuttle); }
public object Deserialize(StreamLimiter stream, SerializedType serializedType, int? length = null) { var reader = new EndianAwareBinaryReader(stream, Endianness); return Deserialize(reader, serializedType, length); }
//public override Node Parent //{ // get // { // var parent = base.Parent; // return parent.Parent; // } //} protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { foreach(var child in Children) child.Serialize(stream, eventShuttle); }
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var serializableChildren = GetSerializableChildren(); int? itemLength = null; if (TypeNode.ItemLengthBinding != null && TypeNode.ItemLengthBinding.IsConst) itemLength = Convert.ToInt32(TypeNode.ItemLengthBinding.ConstValue); foreach (var child in serializableChildren) { if (stream.IsAtLimit) break; var childStream = itemLength == null ? stream : new StreamLimiter(stream, itemLength.Value); child.Serialize(childStream, eventShuttle); } var typeNode = (CollectionTypeNode)TypeNode; if (typeNode.TerminationChild != null) { var terminationChild = typeNode.TerminationChild.CreateSerializer(this); terminationChild.Value = typeNode.TerminationValue; terminationChild.Serialize(stream, eventShuttle); } }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var typeNode = (CollectionTypeNode)TypeNode; /* Create temporary list */ Type collectionType = typeof(List <>).MakeGenericType(typeNode.ChildType); var collection = (IList)Activator.CreateInstance(collectionType); /* Create single serializer to do all the work */ var dummyChild = (ValueValueNode)typeNode.Child.CreateSerializer(this); var reader = new EndianAwareBinaryReader(stream, Endianness); var childSerializedType = dummyChild.TypeNode.GetSerializedType(); var count = TypeNode.FieldCountBinding != null?Convert.ToInt32(TypeNode.FieldCountBinding.GetValue(this)) : int.MaxValue; int?length = null; if (TypeNode.ItemLengthBinding != null) { length = Convert.ToInt32(TypeNode.ItemLengthBinding.GetValue(this)); } var terminationValue = typeNode.TerminationValue; var terminationChild = typeNode.TerminationChild == null ? null : typeNode.TerminationChild.CreateSerializer(this); int itemCount = 0; for (int i = 0; i < count; i++) { if (ShouldTerminate(stream)) { break; } /* Check termination case */ if (terminationChild != null) { using (var streamResetter = new StreamResetter(stream)) { terminationChild.Deserialize(stream, eventShuttle); if (terminationChild.Value.Equals(terminationValue)) { streamResetter.CancelReset(); break; } } } var value = dummyChild.Deserialize(reader, childSerializedType, length); collection.Add(value); itemCount++; } /* Create final collection */ Value = CreateCollection(itemCount); /* Copy temp list into final collection */ for (int i = 0; i < itemCount; i++) { SetCollectionValue(collection[i], i); } }