Esempio n. 1
0
        private static void WriteCppCliTypes([NotNull] WriteContext context, [NotNull] TypeNode type)
        {
            var functionPointer = (FunctionPointer)type;

            context.TextBuilder.Append("=FUNC:");
            AppendTextForTypeNode(context, functionPointer.ReturnType);
            if (functionPointer.ParameterTypes.Count != 0)
            {
                bool comma = false;
                context.TextBuilder.Append('(');
                foreach (TypeNode parameterType in functionPointer.ParameterTypes)
                {
                    if (comma)
                    {
                        context.TextBuilder.Append(',');
                    }
                    AppendTextForTypeNode(context, parameterType);
                    comma = true;
                }
                context.TextBuilder.Append(')');
            }
            else
            {
                // Inconsistent with documentation: based on MS C++ compiler output.
                context.TextBuilder.Append("(System.Void)");
            }
        }
Esempio n. 2
0
        private static void WriteArray([NotNull] WriteContext context, [NotNull] TypeNode type)
        {
            var array = ((ArrayType)type);

            AppendTextForTypeNode(context, array.ElementType);
            if (array.IsSzArray())
            {
                context.TextBuilder.Append("[]");
            }
            else
            {
                // This case handles true multidimensional arrays.
                // For example, in C#: string[,] myArray
                context.TextBuilder.Append('[');
                for (int i = 0; i < array.Rank; i++)
                {
                    if (i != 0)
                    {
                        context.TextBuilder.Append(',');
                    }
                    // The following appears to be consistent with MS C# compiler output.
                    context.TextBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}:", array.GetLowerBound(i));
                    if (array.GetSize(i) != 0)
                    {
                        context.TextBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", array.GetSize(i));
                    }
                }
                context.TextBuilder.Append(']');
            }
        }
Esempio n. 3
0
 public void Write(WriteContext context)
 {
     if (WriteEnabled == null || WriteEnabled(context))
     {
         Writes.Add(context);
     }
 }
Esempio n. 4
0
        public static string GetDocumentationCommentId([NotNull] Member member)
        {
            Guard.NotNull(member, "member");

            var      textBuilder     = new StringBuilder();
            char     prefixCharacter = DeterminePrefixCharacter(member);
            TypeNode declaringType   = member.DeclaringType;

            // Determine all parent types of this potentially nested type.
            IEnumerable <TypeNode> parentTypes = DetermineParentTypesIncludingCurrent(declaringType).ToArray();

            List <TypeNode> typeTemplateParameters   = CollectTypeTemplateParameters(parentTypes);
            List <TypeNode> typeTemplateArguments    = CollectTypeTemplateArguments(parentTypes);
            List <TypeNode> memberTemplateParameters = CollectMethodTemplateParameters(member);

            var context = new WriteContext(typeTemplateParameters, typeTemplateArguments, memberTemplateParameters,
                                           textBuilder);

            OutputFullMethodName(context, member, prefixCharacter, declaringType);
            OutputMethodTemplateParameterCount(context);
            OutputParameters(context, member);
            OutputConversionOperatorReturnType(context, member);

            return(textBuilder.ToString());
        }
Esempio n. 5
0
        public void FirstTimeFieldIsAutomaticallyEventTime()
        {
            var ms  = new MemoryStream();
            var fio = new FileIO(ms);
            var fw  = new FormattedWriter(fio);
            var wc  = new WriteContext(fw);

            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis <C2>();
            wc.Description.Timescale       = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();

            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);

            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = ItemDescription.FromAnalysis <C2>();

            f.Read(rc);

            var fields = rc.Description.ItemDescription.Fields;

            fields[0].IsEventTime.Should().Be.True();
            fields[1].IsEventTime.Should().Be.False();
        }
Esempio n. 6
0
        public void TimeSectionRoundTrip()
        {
            var ms  = new MemoryStream();
            var fio = new FileIO(ms);
            var fw  = new FormattedWriter(fio);
            var wc  = new WriteContext(fw);

            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis <Event <C> >();
            wc.Description.Timescale       = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();

            f.Write(wc);

            ms.Position = 0;
            ms.Length.Should().Be(24); // epoch(8) ticksperday(8) fieldcount(4) + timefieldoffset(4) = 24

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);

            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = wc.Description.ItemDescription; // this makes the test a bit weaker, but we need some item description here
            f.Read(rc);
            rc.Description.Should().Not.Be.Null();
            rc.Description.Timescale.HasValue.Should().Be.True();
        }
Esempio n. 7
0
        public void TimeSectionValuesRoundTrip()
        {
            var ms  = new MemoryStream();
            var fio = new FileIO(ms);
            var fw  = new FormattedWriter(fio);
            var wc  = new WriteContext(fw);

            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis <Event <C> >();
            Time.Scale = Timescale.FromEpoch(33, 77);
            wc.Description.Timescale = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();

            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);

            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = wc.Description.ItemDescription;

            f.Read(rc);
            rc.Description.Timescale.Value.Epoch.Should().Be(33);
            rc.Description.Timescale.Value.TicksPerDay.Should().Be(77);

            Executing.This(() => f.Read(rc)).Should().Throw <EndOfStreamException>();
        }
Esempio n. 8
0
        public void EventTimeAttributeRoundTrip()
        {
            Time.Scale = Timescale.Net;

            var ms  = new MemoryStream();
            var fio = new FileIO(ms);
            var fw  = new FormattedWriter(fio);
            var wc  = new WriteContext(fw);

            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis <Event <C> >();
            wc.Description.Timescale       = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();

            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);

            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = wc.Description.ItemDescription;
            rc.Description.ItemDescription.Fields.ForEach(ff => ff.IsEventTime = ff.IsTime = false); // reset flags

            f.Read(rc);

            rc.Description.ItemDescription.Fields.Count(ff => ff.IsTime).Should().Be(1);
            rc.Description.ItemDescription.Fields.Count(ff => ff.IsEventTime).Should().Be(1);
            rc.Description.Timescale.Value.Epoch.Should().Be(0);
            rc.Description.Timescale.Value.TicksPerDay.Should().Be(TimeSpan.TicksPerDay);
        }
Esempio n. 9
0
        public void NameValueSectionRoundTrip1EntryTest()
        {
            var ms  = new MemoryStream();
            var fio = new FileIO(ms);
            var fw  = new FormattedWriter(fio);
            var wc  = new WriteContext(fw);

            ISectionFormatter f = new NameValueSectionFormatter();

            wc.Description            = new TeaFileDescription();
            wc.Description.NameValues = new NameValueCollection();
            wc.Description.NameValues.Add(new NameValue("someName", 1.23));
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);

            f.Read(rc);

            rc.Description.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Have.Count.EqualTo(1);
            rc.Description.NameValues.First().Name.Should().Be("someName");
            rc.Description.NameValues.First().GetValue <double>().Should().Be(1.23);
        }
Esempio n. 10
0
        private void TestSink_MessageLogged(WriteContext context)
        {
            var log = new LogMessage(context.LogLevel, context.EventId, context.Message, context.Exception);

            Logs.Enqueue(log);
            Output.WriteLine(log.ToString());
        }
Esempio n. 11
0
        public void Write(IEnumerable <TItem> items)
        {
            if (items == null)
            {
                return;
            }

            lock (streamLock)
            {
                if (!isOpen)
                {
                    throw new InvalidOperationException("The observable stream is closed.");
                }

                WriteContext context = new WriteContext()
                {
                    StartIndex = freeIndex,
                    Observers  = observers
                };

                foreach (TItem item in items)
                {
                    context.Item = item;
                    WriteItem(context);
                }

                NotifyObservers(context);
            }
        }
Esempio n. 12
0
        public void NameValueSectionRoundTrip3EntriesTest()
        {
            var ms  = new MemoryStream();
            var fio = new FileIO(ms);
            var fw  = new FormattedWriter(fio);
            var wc  = new WriteContext(fw);

            ISectionFormatter f = new NameValueSectionFormatter();

            wc.Description            = new TeaFileDescription();
            wc.Description.NameValues = new NameValueCollection();
            wc.Description.NameValues.Add(new NameValue("someName", 1.23));
            wc.Description.NameValues.Add(new NameValue("someName2", "second value"));
            wc.Description.NameValues.Add(new NameValue("someName3", 333));
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);

            f.Read(rc);

            rc.Description.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Have.Count.EqualTo(3);
            rc.Description.NameValues.Select(nv => nv.Name).Should().Have.SameSequenceAs("someName", "someName2", "someName3");
            rc.Description.NameValues.Select(nv => nv.GetValue <object>()).Should().Have.SameSequenceAs(1.23, "second value", 333);
        }
Esempio n. 13
0
            static void AssertScope(WriteContext log)
            {
                var scope = (IReadOnlyList <KeyValuePair <string, object> >)log.Scope;

                Assert.AreEqual(MethodType.Unary, scope[0].Value);
                Assert.AreEqual(new Uri("/ServiceName/MethodName", UriKind.Relative), scope[1].Value);
            }
        // This may called on the libuv event loop
        private void OnWriteCompleted(WriteContext writeContext)
        {
            // Called inside _contextLock
            var bytesWritten = writeContext.ByteCount;
            var status       = writeContext.WriteStatus;
            var error        = writeContext.WriteError;

            if (error != null)
            {
                // Abort the connection for any failed write
                // Queued on threadpool so get it in as first op.
                _connection.Abort();
                _cancelled      = true;
                _lastWriteError = error;
            }

            PoolWriteContext(writeContext);

            // _numBytesPreCompleted can temporarily go negative in the event there are
            // completed writes that we haven't triggered callbacks for yet.
            _numBytesPreCompleted -= bytesWritten;

            if (error == null)
            {
                CompleteFinishedWrites(status);
                _log.ConnectionWriteCallback(_connectionId, status);
            }
            else
            {
                CompleteAllWrites();
                _log.ConnectionError(_connectionId, error);
            }
        }
        public void ItemSectionRoundTrip()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            var writeID =
                wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<OHLCV>>();
            ISectionFormatter f = new ItemSectionFormatter();
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            f.Read(rc);
            rc.Description.Should().Not.Be.Null();
            var id = rc.Description.ItemDescription;

            id.ItemTypeName.Should().Be(typeof (Event<OHLCV>).GetLanguageName());
            id.ItemSize.Should().Be(wc.Description.ItemDescription.ItemSize);
            id.Fields.Select(ff => ff.Name).Should().Have.SameValuesAs("Time", "Open", "High", "Low", "Close", "Volume");
            id.Fields.Select(ff => ff.Index).Should().Have.SameValuesAs(0, 1, 2, 3, 4, 5);
            id.Fields.Select(ff => ff.FieldType).Should().Have.SameValuesAs(FieldType.Int64, FieldType.Double, FieldType.Double, FieldType.Double, FieldType.Double);
            id.Fields.Select(ff => ff.Offset).Should().Have.SameValuesAs(writeID.Fields.Select(ff => ff.Offset));

            ms.Position.Should().Be(ms.Length); // very important, all bytes must have been read
        }
Esempio n. 16
0
        private void LogMessages(WriteContext context)
        {
            var log = new LogMessage(context.LogLevel, context.Message, context.Exception);

            Logs.Enqueue(log);
            Output.WriteLine(log.ToString());
        }
        /// <summary>
        /// Create a new query instance
        /// </summary>
        /// <param name="domainName">
        /// The domain in which the query is to run
        /// </param>
        /// <param name="queryName">
        /// The name of the type of query to be run
        /// </param>
        /// <param name="queryUniqueIdentifier">
        /// The unique identifier of the query instance
        /// </param>
        /// <param name="context">
        /// Additional context information to use when writing events for the query
        /// </param>
        public Query(string domainName,
                     string queryName,
                     string queryUniqueIdentifier,
                     WriteContext context = null)
        {
            _domainName       = domainName;
            _queryName        = queryName;
            _uniqueIdentifier = queryUniqueIdentifier;
            // Make a query
            if (context == null)
            {
                // Use a default write context for this query
                _queryContext = new WriteContext()
                {
                    Source = _queryName,
                    CausationIdentifier = _uniqueIdentifier,
                    Commentary          = _queryName
                };
            }
            else
            {
                _queryContext = context;
            }

            StartListener();
        }
 /// <inheritdoc/>
 protected override void Write(ref WriteContext writer)
 {
     if (SourceValue != null)
     {
         writer.WriteString(SourceValue);
     }
 }
Esempio n. 19
0
        // This is called on the libuv event loop
        private void WriteAllPending()
        {
            WriteContext writingContext = null;

            if (Monitor.TryEnter(_contextLock))
            {
                _postingWrite = false;

                if (_nextWriteContext != null)
                {
                    writingContext    = _nextWriteContext;
                    _nextWriteContext = null;
                }
                else
                {
                    _ongoingWrites--;
                }

                Monitor.Exit(_contextLock);
            }
            else
            {
                ScheduleWrite();
            }

            if (writingContext != null)
            {
                writingContext.DoWriteIfNeeded();
            }
        }
Esempio n. 20
0
 /// <inheritdoc/>
 protected override void WriteValue(ref WriteContext writer, T?value)
 {
     if (value.HasValue)
     {
         _codeGenerator.WriteValue(ref writer, value.Value);
     }
 }
Esempio n. 21
0
        private static void WriteOrdinaryType([NotNull] WriteContext context, [NotNull] TypeNode type, bool includeArity)
        {
            if (type.DeclaringType == null)
            {
                if (type.Namespace.Name.Length != 0)
                {
                    context.TextBuilder.Append(type.Namespace.Name);
                    context.TextBuilder.Append('.');
                }
            }
            else
            {
                AppendTextForTypeNode(context, type.DeclaringType);
                context.TextBuilder.Append('.');
            }

            if (type.IsGeneric && type.Template != null)
            {
                string templateName = type.Template.Name.Name.Replace('+', '.');
                int    pos          = includeArity ? -1 : templateName.LastIndexOf('`');
                context.TextBuilder.Append(pos != -1 ? templateName.Substring(0, pos) : templateName);
            }
            else
            {
                context.TextBuilder.Append(type.Name.Name.Replace('+', '.'));
            }
        }
        public void NameValueSectionRoundTrip1EntryTest()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);

            ISectionFormatter f = new NameValueSectionFormatter();
            wc.Description = new TeaFileDescription();
            wc.Description.NameValues = new NameValueCollection();
            wc.Description.NameValues.Add(new NameValue("someName", 1.23));
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            f.Read(rc);

            rc.Description.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Have.Count.EqualTo(1);
            rc.Description.NameValues.First().Name.Should().Be("someName");
            rc.Description.NameValues.First().GetValue<double>().Should().Be(1.23);
        }
Esempio n. 23
0
        public void WriteTo(ref WriteContext ctx, FieldCodec <T> codec)
        {
            if (count == 0)
            {
                return;
            }
            var writer = codec.ValueWriter;
            var tag    = codec.Tag;

            if (codec.PackedRepeatedField)
            {
                // Packed primitive type
                int size = CalculatePackedDataSize(codec);
                ctx.WriteTag(tag);
                ctx.WriteLength(size);
                for (int i = 0; i < count; i++)
                {
                    writer(ref ctx, array[i]);
                }
            }
            else
            {
                // Not packed: a simple tag/value pair for each value.
                // Can't use codec.WriteTagAndValue, as that omits default values.
                for (int i = 0; i < count; i++)
                {
                    ctx.WriteTag(tag);
                    writer(ref ctx, array[i]);
                    if (codec.EndTag != 0)
                    {
                        ctx.WriteTag(codec.EndTag);
                    }
                }
            }
        }
        public static string GetDocumentationCommentId([NotNull] Member member)
        {
            Guard.NotNull(member, "member");

            var textBuilder = new StringBuilder();
            char prefixCharacter = DeterminePrefixCharacter(member);
            TypeNode declaringType = member.DeclaringType;

            // Determine all parent types of this potentially nested type.
            IEnumerable<TypeNode> parentTypes = DetermineParentTypesIncludingCurrent(declaringType).ToArray();

            List<TypeNode> typeTemplateParameters = CollectTypeTemplateParameters(parentTypes);
            List<TypeNode> typeTemplateArguments = CollectTypeTemplateArguments(parentTypes);
            List<TypeNode> memberTemplateParameters = CollectMethodTemplateParameters(member);

            var context = new WriteContext(typeTemplateParameters, typeTemplateArguments, memberTemplateParameters,
                textBuilder);

            OutputFullMethodName(context, member, prefixCharacter, declaringType);
            OutputMethodTemplateParameterCount(context);
            OutputParameters(context, member);
            OutputConversionOperatorReturnType(context, member);

            return textBuilder.ToString();
        }
Esempio n. 25
0
        // This is called on the libuv event loop
        private void WriteAllPending()
        {
            WriteContext writingContext;

            lock (_lockObj)
            {
                if (_nextWriteContext != null)
                {
                    writingContext    = _nextWriteContext;
                    _nextWriteContext = null;
                }
                else
                {
                    _writesPending--;
                    return;
                }
            }

            try
            {
                writingContext.DoWriteIfNeeded();
            }
            catch
            {
                lock (_lockObj)
                {
                    // Lock instead of using Interlocked.Decrement so _writesSending
                    // doesn't change in the middle of executing other synchronized code.
                    _writesPending--;
                }

                throw;
            }
        }
        public void ItemSectionRoundTrip()
        {
            var ms  = new MemoryStream();
            var fio = new FileIO(ms);
            var fw  = new FormattedWriter(fio);
            var wc  = new WriteContext(fw);

            wc.Description = new TeaFileDescription();
            var writeID =
                wc.Description.ItemDescription = ItemDescription.FromAnalysis <Event <OHLCV> >();
            ISectionFormatter f = new ItemSectionFormatter();

            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);

            rc.Description.Should().Not.Be.Null();
            f.Read(rc);
            rc.Description.Should().Not.Be.Null();
            var id = rc.Description.ItemDescription;

            id.ItemTypeName.Should().Be(typeof(Event <OHLCV>).GetLanguageName());
            id.ItemSize.Should().Be(wc.Description.ItemDescription.ItemSize);
            id.Fields.Select(ff => ff.Name).Should().Have.SameValuesAs("Time", "Open", "High", "Low", "Close", "Volume");
            id.Fields.Select(ff => ff.Index).Should().Have.SameValuesAs(0, 1, 2, 3, 4, 5);
            id.Fields.Select(ff => ff.FieldType).Should().Have.SameValuesAs(FieldType.Int64, FieldType.Double, FieldType.Double, FieldType.Double, FieldType.Double);
            id.Fields.Select(ff => ff.Offset).Should().Have.SameValuesAs(writeID.Fields.Select(ff => ff.Offset));

            ms.Position.Should().Be(ms.Length); // very important, all bytes must have been read
        }
Esempio n. 27
0
        public void MessageWrittenEventInvoked()
        {
            WriteContext context = null;

            TestSink.MessageLogged += ctx => context = ctx;
            Logger.LogInformation("Information");
            Assert.Equal(TestSink.Writes.Single(), context);
        }
Esempio n. 28
0
        private static void WriteOptionalModifier([NotNull] WriteContext context, [NotNull] TypeNode type)
        {
            var optionalModifier = (OptionalModifier)type;

            AppendTextForTypeNode(context, optionalModifier.ModifiedType);
            context.TextBuilder.Append("!");
            AppendTextForTypeNode(context, optionalModifier.Modifier);
        }
Esempio n. 29
0
 /// <inheritdoc/>
 protected override void Write(ref WriteContext writer)
 {
     if (SourceValue.HasValue)
     {
         IBufferMessage message = (Message <T>)SourceValue.Value;
         message.InternalWriteTo(ref writer);
     }
 }
 /// <inheritdoc/>
 protected override void WriteValue(ref WriteContext context, DateTime value)
 {
     if (value.Kind != DateTimeKind.Utc)
     {
         value = value.ToUniversalTime();
     }
     context.WriteMessage(Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(value));
 }
Esempio n. 31
0
        private static void WriteRequiredModifier([NotNull] WriteContext context, [NotNull] TypeNode type)
        {
            var requiredModifier = (RequiredModifier)type;

            AppendTextForTypeNode(context, requiredModifier.ModifiedType);
            context.TextBuilder.Append("|");
            AppendTextForTypeNode(context, requiredModifier.Modifier);
        }
 public void ContentSectionFormatterEmptyDescription()
 {
     var fw = new Mock<IFormattedWriter>(MockBehavior.Strict);
     var wc = new WriteContext(fw.Object);
     ISectionFormatter f = new ContentSectionFormatter();
     f.Write(wc);
     // due to the strict behavior of the fw mock, this test succeeds only if no method of fw was called.
 }
Esempio n. 33
0
 private void PoolWriteContext(WriteContext writeContext)
 {
     // called inside _contextLock
     if (_writeContextPool.Count < _maxPooledWriteContexts)
     {
         writeContext.Reset();
         _writeContextPool.Enqueue(writeContext);
     }
 }
Esempio n. 34
0
        public void TimeSectionFormatterEmptyDescriptionTest()
        {
            var fw = new Mock <IFormattedWriter>(MockBehavior.Strict);
            var wc = new WriteContext(fw.Object);
            ISectionFormatter f = new TimeSectionFormatter();

            f.Write(wc);
            // due to the strict behavior of the fw mock, this test succeeds only if no method of fw was called.
        }
 public void TimeSectionFormatterDescriptionItemDescriptionNoTimeFields()
 {
     var fw = new Mock<IFormattedWriter>(MockBehavior.Strict);
     var wc = new WriteContext(fw.Object);
     wc.Description = new TeaFileDescription();
     wc.Description.ItemDescription = new ItemDescription(DescriptionSource.File);
     ISectionFormatter f = new TimeSectionFormatter();
     f.Write(wc);
     // due to the strict behavior of the fw mock, this test succeeds only if no method of fw was called.
 }
Esempio n. 36
0
        public void Write(ArraySegment<byte> buffer, Action<Exception, object> callback, object state, bool immediate = true)
        {
            //TODO: need buffering that works
            var copy = new byte[buffer.Count];
            Array.Copy(buffer.Array, buffer.Offset, copy, 0, buffer.Count);
            buffer = new ArraySegment<byte>(copy);

            KestrelTrace.Log.ConnectionWrite(0, buffer.Count);

            bool triggerCallbackNow = false;

            lock (_lockObj)
            {
                if (_nextWriteContext == null)
                {
                    _nextWriteContext = new WriteContext(this);
                }

                _nextWriteContext.Buffers.Enqueue(buffer);

                // Complete the write task immediately if all previous write tasks have been completed,
                // the buffers haven't grown too large, and the last write to the socket succeeded.
                triggerCallbackNow = _lastWriteError == null &&
                                     _callbacksPending.Count == 0 &&
                                     _numBytesPreCompleted + buffer.Count <= _maxBytesPreCompleted;
                if (triggerCallbackNow)
                {
                    _numBytesPreCompleted += buffer.Count;
                }
                else
                {
                    _callbacksPending.Enqueue(new CallbackContext
                    {
                        Callback = callback,
                        State = state,
                        BytesToWrite = buffer.Count
                    });
                }

                if (_writesPending < _maxPendingWrites && immediate)
                {
                    ScheduleWrite();
                    _writesPending++;
                }
            }

            // Make sure we call user code outside of the lock.
            if (triggerCallbackNow)
            {
                callback(null, state);
            }
        }
        public void NameValueSectionIOTest()
        {
            var fw = new Mock<IFormattedWriter>(MockBehavior.Strict);
            fw.Setup(w => w.WriteInt32(1));
            fw.Setup(w => w.WriteNameValue(It.Is<NameValue>(nv => nv.Name == "someName")));
            var wc = new WriteContext(fw.Object);

            ISectionFormatter f = new NameValueSectionFormatter();
            wc.Description = new TeaFileDescription();
            wc.Description.NameValues = new NameValueCollection();
            wc.Description.NameValues.Add(new NameValue("someName", 1.23));
            f.Write(wc);

            fw.Verify();
        }
        public void NoNameValueSectionTest()
        {
            var fw = new Mock<IFormattedWriter>(MockBehavior.Strict);
            var wc = new WriteContext(fw.Object);

            ISectionFormatter f = new NameValueSectionFormatter();
            f.Write(wc);

            wc.Description = new TeaFileDescription();
            f.Write(wc);

            wc.Description.NameValues = new NameValueCollection();
            f.Write(wc);

            // due to the strict behavior of the fw mock, this test succeeds only if no method of fw was called.
        }
 public void Write(WriteContext c)
 {
     if (c.Description == null) return;
     var id = c.Description.ItemDescription;
     if (id == null) return;
     var w = c.Writer;
     w.WriteInt32(id.ItemSize);
     w.WriteText(id.ItemTypeName);
     w.WriteInt32(id.Fields.Count);
     foreach (Field f in id.Fields)
     {
         w.WriteInt32((int)f.FieldType);
         w.WriteInt32(f.Offset);
         w.WriteText(f.Name);
     }
 }
        public void ContentSectionFormatterRoundTrip()
        {
            const string testValue = "Météo pour Paris, France. @€";

            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ContentDescription = testValue;
            ISectionFormatter f = new ContentSectionFormatter();
            f.Write(wc);
            ms.Position = 0;
            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            f.Read(rc);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ContentDescription.Should().Be(testValue);
        }
Esempio n. 41
0
        // This is called on the libuv event loop
        private void OnWriteCompleted(WriteContext writeContext)
        {
            var bytesWritten = writeContext.ByteCount;
            var status = writeContext.WriteStatus;
            var error = writeContext.WriteError;


            if (error != null)
            {
                _lastWriteError = new IOException(error.Message, error);

                // Abort the connection for any failed write.
                _connection.Abort();
            }

            lock (_contextLock)
            {
                PoolWriteContext(writeContext);

                // _numBytesPreCompleted can temporarily go negative in the event there are
                // completed writes that we haven't triggered callbacks for yet.
                _numBytesPreCompleted -= bytesWritten;

                // bytesLeftToBuffer can be greater than _maxBytesPreCompleted
                // This allows large writes to complete once they've actually finished.
                var bytesLeftToBuffer = _maxBytesPreCompleted - _numBytesPreCompleted;
                while (_tasksPending.Count > 0 &&
                       (int)(_tasksPending.Peek().Task.AsyncState) <= bytesLeftToBuffer)
                {
                    var tcs = _tasksPending.Dequeue();
                    var bytesToWrite = (int)tcs.Task.AsyncState;

                    _numBytesPreCompleted += bytesToWrite;
                    bytesLeftToBuffer -= bytesToWrite;

                    _tasksCompleted.Enqueue(tcs);
                }
            }

            while (_tasksCompleted.Count > 0)
            {
                var tcs = _tasksCompleted.Dequeue();
                if (_lastWriteError == null)
                {
                    _threadPool.Complete(tcs);
                }
                else
                {
                    _threadPool.Error(tcs, _lastWriteError);
                }
            }

            _log.ConnectionWriteCallback(_connectionId, status);
            _tasksCompleted.Clear();
        }
Esempio n. 42
0
 private void PoolWriteContext(WriteContext writeContext)
 {
     // called inside _contextLock
     if (_writeContextPool.Count < _maxPooledWriteContexts)
     {
         writeContext.Reset();
         _writeContextPool.Enqueue(writeContext);
     }
 }
Esempio n. 43
0
        public Task WriteAsync(
            ArraySegment<byte> buffer,
            bool immediate = true,
            bool socketShutdownSend = false,
            bool socketDisconnect = false)
        {
            if (buffer.Count > 0)
            {
                var tail = ProducingStart();
                tail.CopyFrom(buffer);
                // We do our own accounting below
                ProducingCompleteNoPreComplete(tail);
            }
            TaskCompletionSource<object> tcs = null;

            var scheduleWrite = false;

            lock (_contextLock)
            {
                if (_nextWriteContext == null)
                {
                    if (_writeContextPool.Count > 0)
                    {
                        _nextWriteContext = _writeContextPool.Dequeue();
                    }
                    else
                    {
                        _nextWriteContext = new WriteContext(this);
                    }
                }

                if (socketShutdownSend)
                {
                    _nextWriteContext.SocketShutdownSend = true;
                }
                if (socketDisconnect)
                {
                    _nextWriteContext.SocketDisconnect = true;
                }

                if (!immediate)
                {
                    // immediate==false calls always return complete tasks, because there is guaranteed
                    // to be a subsequent immediate==true call which will go down one of the previous code-paths
                    _numBytesPreCompleted += buffer.Count;
                }
                else if (_lastWriteError == null &&
                        _tasksPending.Count == 0 &&
                        _numBytesPreCompleted + buffer.Count <= _maxBytesPreCompleted)
                {
                    // Complete the write task immediately if all previous write tasks have been completed,
                    // the buffers haven't grown too large, and the last write to the socket succeeded.
                    _numBytesPreCompleted += buffer.Count;
                }
                else
                {
                    // immediate write, which is not eligable for instant completion above
                    tcs = new TaskCompletionSource<object>(buffer.Count);
                    _tasksPending.Enqueue(tcs);
                }

                if (!_writePending && immediate)
                {
                    _writePending = true;
                    scheduleWrite = true;
                }
            }

            if (scheduleWrite)
            {
                ScheduleWrite();
            }

            // Return TaskCompletionSource's Task if set, otherwise completed Task 
            return tcs?.Task ?? TaskUtilities.CompletedTask;
        }
        public void EventTimeAttributeRoundTrip()
        {
            Time.Scale = Timescale.Net;

            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>();
            wc.Description.Timescale = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = wc.Description.ItemDescription;
            rc.Description.ItemDescription.Fields.ForEach(ff => ff.IsEventTime = ff.IsTime = false); // reset flags

            f.Read(rc);

            rc.Description.ItemDescription.Fields.Count(ff => ff.IsTime).Should().Be(1);
            rc.Description.ItemDescription.Fields.Count(ff => ff.IsEventTime).Should().Be(1);
            rc.Description.Timescale.Value.Epoch.Should().Be(0);
            rc.Description.Timescale.Value.TicksPerDay.Should().Be(TimeSpan.TicksPerDay);
        }
        public void FirstTimeFieldIsAutomaticallyEventTime()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis<C2>();
            wc.Description.Timescale = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = ItemDescription.FromAnalysis<C2>();

            f.Read(rc);

            var fields = rc.Description.ItemDescription.Fields;
            fields[0].IsEventTime.Should().Be.True();
            fields[1].IsEventTime.Should().Be.False();
        }
Esempio n. 46
0
        // This is called on the libuv event loop
        private void WriteAllPending()
        {
            WriteContext writingContext;

            lock (_contextLock)
            {
                _writePending = false;

                if (_nextWriteContext != null)
                {
                    writingContext = _nextWriteContext;
                    _nextWriteContext = null;
                }
                else
                {
                    return;
                }
            }

            writingContext.DoWriteIfNeeded();
        }
Esempio n. 47
0
        // This is called on the libuv event loop
        private void WriteAllPending()
        {
            WriteContext writingContext;

            lock (_lockObj)
            {
                if (_nextWriteContext != null)
                {
                    writingContext = _nextWriteContext;
                    _nextWriteContext = null;
                }
                else
                {
                    _writesPending--;
                    return;
                }
            }

            try
            {
                var buffers = new ArraySegment<byte>[writingContext.Buffers.Count];

                var i = 0;
                foreach (var buffer in writingContext.Buffers)
                {
                    buffers[i++] = buffer;
                }

                var writeReq = new UvWriteReq();
                writeReq.Init(_thread.Loop);

                writeReq.Write(_socket, new ArraySegment<ArraySegment<byte>>(buffers), (r, status, error, state) =>
                {
                    var writtenContext = (WriteContext)state;
                    writtenContext.Self.OnWriteCompleted(writtenContext.Buffers, r, status, error);
                }, writingContext);
            }
            catch
            {
                lock (_lockObj)
                {
                    // Lock instead of using Interlocked.Decrement so _writesSending
                    // doesn't change in the middle of executing other synchronized code.
                    _writesPending--;
                }

                throw;
            }
        }
        public void TimeSectionValuesRoundTrip()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>();
            Time.Scale = Timescale.FromEpoch(33, 77);
            wc.Description.Timescale = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = wc.Description.ItemDescription;

            f.Read(rc);
            rc.Description.Timescale.Value.Epoch.Should().Be(33);
            rc.Description.Timescale.Value.TicksPerDay.Should().Be(77);

            Executing.This(() => f.Read(rc)).Should().Throw<EndOfStreamException>();
        }
        // This is called on the libuv event loop
        private void WriteAllPending()
        {
            WriteContext writingContext;

            lock (_lockObj)
            {
                if (_nextWriteContext != null)
                {
                    writingContext = _nextWriteContext;
                    _nextWriteContext = null;
                }
                else
                {
                    _writesPending--;
                    return;
                }
            }

            try
            {
                writingContext.DoWriteIfNeeded();
            }
            catch
            {
                lock (_lockObj)
                {
                    // Lock instead of using Interlocked.Decrement so _writesSending
                    // doesn't change in the middle of executing other synchronized code.
                    _writesPending--;
                }

                throw;
            }
        }
        public void TimeSectionRoundTrip()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>();
            wc.Description.Timescale = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();
            f.Write(wc);

            ms.Position = 0;
            ms.Length.Should().Be(24); // epoch(8) ticksperday(8) fieldcount(4) + timefieldoffset(4) = 24

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = wc.Description.ItemDescription; // this makes the test a bit weaker, but we need some item description here
            f.Read(rc);
            rc.Description.Should().Not.Be.Null();
            rc.Description.Timescale.HasValue.Should().Be.True();
        }
        public void NameValueSectionRoundTrip3EntriesTest()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);

            ISectionFormatter f = new NameValueSectionFormatter();
            wc.Description = new TeaFileDescription();
            wc.Description.NameValues = new NameValueCollection();
            wc.Description.NameValues.Add(new NameValue("someName", 1.23));
            wc.Description.NameValues.Add(new NameValue("someName2", "second value"));
            wc.Description.NameValues.Add(new NameValue("someName3", 333));
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            f.Read(rc);

            rc.Description.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Have.Count.EqualTo(3);
            rc.Description.NameValues.Select(nv => nv.Name).Should().Have.SameSequenceAs("someName", "someName2", "someName3");
            rc.Description.NameValues.Select(nv => nv.GetValue<object>()).Should().Have.SameSequenceAs(1.23, "second value", 333);
        }
 public void Setup()
 {
     _context = new WriteContext();
 }
        public void TimeSectionRoundTripFieldIsNotInItemDescriptionError()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>();
            wc.Description.Timescale = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = ItemDescription.FromAnalysis<C2>();

            Executing.This(() => f.Read(rc)).Should().Throw<FileFormatException>();
        }