Exemplo n.º 1
0
        private static void OutputSingleQuery(PipeWriter pipeWriter, World row)
        {
            var writer = GetWriter(pipeWriter, sizeHint: 180); // in reality it's 150

            writer.Write(_dbPreamble);

            var lengthWriter = writer;

            writer.Write(_contentLengthGap);

            // Date header
            writer.Write(DateHeader.HeaderBytes);

            writer.Commit();

            Utf8JsonWriter utf8JsonWriter = t_writer ??= new Utf8JsonWriter(pipeWriter, new JsonWriterOptions {
                SkipValidation = true
            });

            utf8JsonWriter.Reset(pipeWriter);

            // Body
            JsonSerializer.Serialize <World>(utf8JsonWriter, row, SerializerOptions);

            // Content-Length
            lengthWriter.WriteNumeric((uint)utf8JsonWriter.BytesCommitted);
        }
Exemplo n.º 2
0
        [Benchmark(Description = "ArrayPool")] public void SerializeWithArrayPool()
        {
            JsonWriterOptions options = new JsonWriterOptions()
            {
                Indented = false,
                Encoder  = JavaScriptEncoder.Create(UnicodeRanges.All)
            };

            byte[] buffer = ArrayPool <byte> .Shared.Rent(10240);

            using (MemoryStream stream = new MemoryStream(buffer))
            {
                using (Utf8JsonWriter writer = new Utf8JsonWriter(stream, options))
                {
                    for (int i = 0; i < TestSize; i++)
                    {
                        Serialize2(writer);

                        ReadOnlySpan <byte> span = new ReadOnlySpan <byte>(buffer, 0, (int)writer.BytesCommitted);

                        writer.Reset();
                        stream.Position = 0;
                    }
                }
            }

            ArrayPool <byte> .Shared.Return(buffer);
        }
Exemplo n.º 3
0
        private static void GenericConverterTestHelper <T>(string converterName, object objectValue, string stringValue, JsonSerializerOptions options, bool nullOptionOkay = true)
        {
            JsonConverter <T> converter = (JsonConverter <T>)options.GetConverter(typeof(T));

            Assert.True(converter.CanConvert(typeof(T)));
            Assert.Equal(converterName, converter.GetType().Name);

            ReadOnlySpan <byte> data   = Encoding.UTF8.GetBytes(stringValue);
            Utf8JsonReader      reader = new Utf8JsonReader(data);

            reader.Read();

            T valueRead = converter.Read(ref reader, typeof(T), nullOptionOkay ? null: options);

            Assert.Equal(objectValue, valueRead);

            if (reader.TokenType != JsonTokenType.EndObject)
            {
                valueRead = converter.Read(ref reader, typeof(T), options);  // Test with given option if reader position haven't advanced.
                Assert.Equal(objectValue, valueRead);
            }

            using (var stream = new MemoryStream())
                using (var writer = new Utf8JsonWriter(stream))
                {
                    converter.Write(writer, (T)objectValue, options);
                    writer.Flush();
                    Assert.Equal(stringValue, Encoding.UTF8.GetString(stream.ToArray()));

                    writer.Reset(stream);
                    converter.Write(writer, (T)objectValue, nullOptionOkay ? null : options);
                    writer.Flush();
                    Assert.Equal(stringValue + stringValue, Encoding.UTF8.GetString(stream.ToArray()));
                }
        }
            /// <summary>
            /// Serializes an indexing operation in a bulk request
            /// </summary>
            /// <param name="message">Message to serialize.</param>
            private void SerializeIndexingRequest(LocalLogMessage message)
            {
                // serialize bulk request item object for indexing
                mRequestContentWriter.Reset();
                mStage.WriteBulkRequestIndexAction_ECS110(mRequestContentWriter);

                // finalize line with a newline character
                mContentStream.WriteByte((byte)'\n');

                // serialize the actual log message
                mRequestContentWriter.Reset();
                mStage.WriteJsonMessage_ECS110(mRequestContentWriter, message);

                // finalize line with a newline character
                mContentStream.WriteByte((byte)'\n');
            }
            protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
            {
                if (writer == null)
                {
                    writer = new Utf8JsonWriter(stream);
                }
                else
                {
                    writer.Reset(stream);
                }

                writer.WriteStartArray();

                foreach (var span in this.spans)
                {
                    var zipkinSpan = span.ToZipkinSpan(this.exporter.LocalEndpoint, this.exporter.options.UseShortTraceIds);

                    zipkinSpan.Write(writer);

                    zipkinSpan.Return();
                }

                writer.WriteEndArray();

                return(writer.FlushAsync());
            }
Exemplo n.º 6
0
        private static void OutputUpdates(PipeWriter pipeWriter, World[] rows)
        {
            var writer = GetWriter(pipeWriter, sizeHint: 120 * rows.Length); // in reality it's 112 for one

            writer.Write(_dbPreamble);

            var lengthWriter = writer;

            writer.Write(_contentLengthGap);

            // Date header
            writer.Write(DateHeader.HeaderBytes);

            writer.Commit();

            Utf8JsonWriter utf8JsonWriter = t_writer ??= new Utf8JsonWriter(pipeWriter, new JsonWriterOptions {
                SkipValidation = true
            });

            utf8JsonWriter.Reset(pipeWriter);

            // Body
            JsonSerializer.Serialize(utf8JsonWriter, rows, SerializerContext.WorldArray);

            // Content-Length
            lengthWriter.WriteNumeric((uint)utf8JsonWriter.BytesCommitted);
        }
Exemplo n.º 7
0
        private static void OutputMultipleQueries <TWorld>(PipeWriter pipeWriter, TWorld[] rows, JsonTypeInfo <TWorld[]> jsonTypeInfo)
        {
            var writer = GetWriter(pipeWriter, sizeHint: 160 * rows.Length); // in reality it's 152 for one

            writer.Write(_dbPreamble);

            var lengthWriter = writer;

            writer.Write(_contentLengthGap);

            // Date header
            writer.Write(DateHeader.HeaderBytes);

            writer.Commit();

            Utf8JsonWriter utf8JsonWriter = t_writer ??= new Utf8JsonWriter(pipeWriter, new JsonWriterOptions {
                SkipValidation = true
            });

            utf8JsonWriter.Reset(pipeWriter);

            // Body
            JsonSerializer.Serialize <TWorld[]>(utf8JsonWriter, rows, jsonTypeInfo);

            // Content-Length
            lengthWriter.WriteNumeric((uint)utf8JsonWriter.BytesCommitted);
        }
Exemplo n.º 8
0
 public long SystemTextJson()
 {
     SystemTextJsonOutput.Position = 0;
     System.Text.Json.JsonSerializer.Serialize <IntClass>(SystemTextJsonWriter, Input);
     SystemTextJsonWriter.Reset();
     return(SystemTextJsonOutput.Length);
 }
Exemplo n.º 9
0
 public void EndJsonObjectLine()
 {
     _jsonWriter.WriteEndObject();
     _jsonWriter.Flush();
     _stream.WriteByte((byte)'\n');
     _stream.Flush();
     _jsonWriter.Reset();
 }
Exemplo n.º 10
0
        private void Init()
        {
            _utf8jsonMemory.Seek(0, SeekOrigin.Begin);

            _writer.Reset();
            _writer.WriteStartObject();
            _writer.WriteStartArray("funcs");
        }
Exemplo n.º 11
0
 public void Utf8JsonWriterPooledWriter()
 {
     _writerWBufferWriter.WriteStartObject();
     _writerWBufferWriter.WriteString("message", _result.message);
     _writerWBufferWriter.WriteEndObject();
     _writerBufferWriter.Clear();
     _writerWBufferWriter.Reset();
 }
Exemplo n.º 12
0
 public void Utf8JsonWriterMemoryStreamWriter()
 {
     _writerWMemoryStream.WriteStartObject();
     _writerWMemoryStream.WriteString("message", _result.message);
     _writerWMemoryStream.WriteEndObject();
     _writerMemoryStream.Position = 0;
     _writerWMemoryStream.Reset();
 }
Exemplo n.º 13
0
        public void ParseThenWrite()
        {
            _writer.Reset();

            using (JsonDocument document = JsonDocument.Parse(_dataUtf8))
            {
                document.WriteTo(_writer);
            }
        }
Exemplo n.º 14
0
            public T Clone <T>(T item)
            {
                _bufferWriter.Clear();
                _jsonWriter.Reset();

                JsonSerializer.Serialize(_jsonWriter, item, _options);

                return(JsonSerializer.Deserialize <T>(_bufferWriter.WrittenSpan, _options));
            }
Exemplo n.º 15
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            lock (_jsonWriter)
            {
                _jsonWriter.WriteStartObject();
                if (!string.IsNullOrWhiteSpace(_options.TimestampFieldName))
                {
                    _jsonWriter.WriteString(_options.TimestampFieldName, DateTime.UtcNow);
                }

                if (!string.IsNullOrWhiteSpace(_options.MessageFieldName))
                {
                    _jsonWriter.WriteString(_options.MessageFieldName, formatter(state, exception));
                }

                if (!string.IsNullOrWhiteSpace(_options.LevelFieldName))
                {
                    _jsonWriter.WriteString(_options.LevelFieldName, GetLogLevelString(logLevel));
                }

                if (!string.IsNullOrWhiteSpace(_options.CategoryFieldName))
                {
                    _jsonWriter.WriteString(_options.CategoryFieldName, _categoryName);
                }

                if (!string.IsNullOrWhiteSpace(_options.EventIdFieldName))
                {
                    // This could really be just eventId.ToString() but this is to save on allocation
                    if (eventId.Name != default)
                    {
                        _jsonWriter.WriteString(_options.EventIdFieldName, eventId.Name);
                    }
                    else if (eventId.Id != default)
                    {
                        _jsonWriter.WriteNumber(_options.EventIdFieldName, eventId.Id);
                    }
                }

                if (!string.IsNullOrWhiteSpace(_options.ExceptionFieldName) && exception != default)
                {
                    _jsonWriter.WriteString(_options.ExceptionFieldName, exception.ToString());
                }

                WriteFormattedLogValues(state, _jsonWriter);

                _scopeProvider?.ForEachScope(WriteFormattedLogValues, _jsonWriter);

                _jsonWriter.WriteEndObject();
                _jsonWriter.Flush();
                _jsonWriter.Reset();

                _options.Stream.Write(NewLine, 0, NewLine.Length);
            }
        }
Exemplo n.º 16
0
        public static void RenderOne <T>(IHeaderDictionary headerDictionary, PipeWriter pipeWriter, T t, IJsonSerializer <T> jsonSerializer)
        {
            headerDictionary.Add(_headerServer);
            headerDictionary.Add(_headerContentType);

            Utf8JsonWriter utf8JsonWriter = _utf8JsonWriter ??= new Utf8JsonWriter(pipeWriter, new JsonWriterOptions {
                SkipValidation = true
            });

            utf8JsonWriter.Reset(pipeWriter);

            jsonSerializer.Serialize(utf8JsonWriter, t);
            utf8JsonWriter.Flush();
            headerDictionary.Add(new KeyValuePair <string, StringValues>("Content-Length", utf8JsonWriter.BytesCommitted.ToString()));
        }
Exemplo n.º 17
0
        private static void GenericObjectOrJsonElementConverterTestHelper <T>(string converterName, object objectValue, string stringValue, bool throws)
        {
            var options = new JsonSerializerOptions();

            JsonConverter <T> converter = (JsonConverter <T>)options.GetConverter(typeof(T));

            Assert.Equal(converterName, converter.GetType().Name);

            ReadOnlySpan <byte> data   = Encoding.UTF8.GetBytes(stringValue);
            Utf8JsonReader      reader = new Utf8JsonReader(data);

            reader.Read();
            T readValue = converter.Read(ref reader, typeof(T), null);

            if (readValue is JsonElement element)
            {
                Assert.Equal(JsonValueKind.Array, element.ValueKind);
                JsonElement.ArrayEnumerator iterator = element.EnumerateArray();
                Assert.True(iterator.MoveNext());
                Assert.Equal(3, iterator.Current.GetInt32());
            }
            else
            {
                Assert.True(false, "Must be JsonElement");
            }

            using (var stream = new MemoryStream())
                using (var writer = new Utf8JsonWriter(stream))
                {
                    if (throws)
                    {
                        Assert.Throws <InvalidOperationException>(() => converter.Write(writer, (T)objectValue, options));
                        Assert.Throws <InvalidOperationException>(() => converter.Write(writer, (T)objectValue, null));
                    }
                    else
                    {
                        converter.Write(writer, (T)objectValue, options);
                        writer.Flush();
                        Assert.Equal(stringValue, Encoding.UTF8.GetString(stream.ToArray()));

                        writer.Reset(stream);
                        converter.Write(writer, (T)objectValue, null); // Test with null option
                        writer.Flush();
                        Assert.Equal(stringValue + stringValue, Encoding.UTF8.GetString(stream.ToArray()));
                    }
                }
        }
        private static void Json(ref BufferWriter <WriterAdapter> writer)
        {
            writer.Write(_jsonPreamble);

            // Date header
            writer.Write(DateHeader.HeaderBytes);

            writer.Commit();

            Utf8JsonWriter utf8JsonWriter = t_writer ??= new Utf8JsonWriter(writer.Output);

            utf8JsonWriter.Reset(writer.Output);

            // Body
            JsonSerializer.Serialize <JsonMessage>(utf8JsonWriter, new JsonMessage {
                message = "Hello, World!"
            }, SerializerOptions);
        }
        private void Return(Utf8JsonWriter writer, StringBuilder output)
        {
            writer.Flush();

            if (_cachedMemoryStream.Length > 0)
            {
                if (!_cachedMemoryStream.TryGetBuffer(out var byteArray))
                {
                    byteArray = new ArraySegment <byte>(_cachedMemoryStream.GetBuffer(), 0, (int)_cachedMemoryStream.Length);
                }

                CopyToStringBuilder(byteArray, _cachedEncodingBuffer, output);
            }

            writer.Reset();
            _cachedMemoryStream.Position = 0;
            _cachedMemoryStream.SetLength(0);
            System.Threading.Interlocked.Exchange(ref _cachedJsonWriter, writer);
        }
Exemplo n.º 20
0
    private static void Json(ref BufferWriter <WriterAdapter> writer, IBufferWriter <byte> bodyWriter)
    {
        writer.Write(_jsonPreamble);

        // Date header
        writer.Write(DateHeader.HeaderBytes);

        writer.Commit();

        Utf8JsonWriter utf8JsonWriter = t_writer ??= new Utf8JsonWriter(bodyWriter, new JsonWriterOptions {
            SkipValidation = true
        });

        utf8JsonWriter.Reset(bodyWriter);

        // Body
        JsonSerializer.Serialize(utf8JsonWriter, new JsonMessage {
            message = "Hello, World!"
        }, SerializerContext.JsonMessage);
    }
Exemplo n.º 21
0
            public void SerializeMessageToJson(LoggerJsonMessage message, Stream stream)
            {
                try
                {
                    _Writer.Reset();

                    try
                    {
                        JsonSerializer.Serialize(_Writer, message, _Options);
                    }
                    catch (JsonException JsonException)
                    {
                        JsonSerializer.Serialize(
                            _Writer,
                            new LoggerJsonMessage
                        {
                            LogLevel     = message.LogLevel,
                            TimestampUtc = message.TimestampUtc,
                            ThreadId     = message.ThreadId,
                            EventId      = message.EventId,
                            GroupName    = message.GroupName,
                            CategoryName = message.CategoryName,
                            Content      = $"Message with Content [{message.Content}] contained data that could not be serialized into Json.",
                            Exception    = LoggerJsonMessageException.FromException(JsonException)
                        },
                            _Options);
                    }
                    finally
                    {
                        LoggerJsonMessage.Return(message);
                    }

                    _Buffer.WriteToStream(stream);
                    stream.Write(s_NewLine, 0, s_NewLine.Length);
                    stream.Flush();
                }
                finally
                {
                    _Buffer.Clear();
                }
            }
Exemplo n.º 22
0
        public static void RenderMany <T>(IHeaderDictionary headerDictionary, PipeWriter pipeWriter, T[] tArray, IJsonSerializer <T> jsonSerializer)
        {
            headerDictionary.Add(_headerServer);
            headerDictionary.Add(_headerContentType);

            Utf8JsonWriter utf8JsonWriter = _utf8JsonWriter ??= new Utf8JsonWriter(pipeWriter, new JsonWriterOptions {
                SkipValidation = true
            });

            utf8JsonWriter.Reset(pipeWriter);

            utf8JsonWriter.WriteStartArray();

            foreach (var t in tArray)
            {
                jsonSerializer.Serialize(utf8JsonWriter, t);
            }

            utf8JsonWriter.WriteEndArray();
            utf8JsonWriter.Flush();
            headerDictionary.Add(new KeyValuePair <string, StringValues>("Content-Length", ((uint)utf8JsonWriter.BytesCommitted).ToString()));
        }
Exemplo n.º 23
0
        private void Serialize_Документ_Чеки()
        {
            JsonWriterOptions options = new JsonWriterOptions
            {
                Indented = false,
                Encoder  = JavaScriptEncoder.Create(UnicodeRanges.All)
            };

            MemoryStream   stream = new MemoryStream(2048);
            Utf8JsonWriter writer = new Utf8JsonWriter(stream, options);

            writer.WriteStartObject();
            writer.WriteString("#type", "jcfg:CatalogObject.Партии");

            writer.WritePropertyName("#value");
            writer.WriteStartObject();

            writer.Flush();

            ReadOnlySpan <byte> span = new ReadOnlySpan <byte>(stream.GetBuffer(), 0, (int)writer.BytesCommitted);

            writer.Reset();
            stream.Position = 0;
        }
Exemplo n.º 24
0
        [Benchmark(Description = "RecyclableStream")] public void SerializeWithRecyclableStream()
        {
            JsonWriterOptions options = new JsonWriterOptions()
            {
                Indented = false,
                Encoder  = JavaScriptEncoder.Create(UnicodeRanges.All)
            };

            using (MemoryStream stream = StreamManager.GetStream())
            {
                using (Utf8JsonWriter writer = new Utf8JsonWriter(stream, options))
                {
                    for (int i = 0; i < TestSize; i++)
                    {
                        Serialize2(writer);

                        ReadOnlySpan <byte> span = new ReadOnlySpan <byte>(stream.GetBuffer(), 0, (int)writer.BytesCommitted);

                        writer.Reset();
                        stream.Position = 0;
                    }
                }
            }
        }
Exemplo n.º 25
0
        private static void GenericObjectOrJsonElementConverterTestHelper <T>(string converterName, object objectValue, string stringValue)
        {
            var options = new JsonSerializerOptions();

            JsonConverter <T> converter = (JsonConverter <T>)options.GetConverter(typeof(T));

            Assert.Equal(converterName, converter.GetType().Name);

            ReadOnlySpan <byte> data   = Encoding.UTF8.GetBytes(stringValue);
            Utf8JsonReader      reader = new Utf8JsonReader(data);

            reader.Read();
            T readValue = converter.Read(ref reader, typeof(T), options);

            if (readValue is JsonElement element)
            {
                JsonTestHelper.AssertJsonEqual(stringValue, element.ToString());
            }
            else
            {
                Assert.True(false, "Must be JsonElement");
            }

            using (var stream = new MemoryStream())
                using (var writer = new Utf8JsonWriter(stream))
                {
                    converter.Write(writer, (T)objectValue, options);
                    writer.Flush();
                    Assert.Equal(stringValue, Encoding.UTF8.GetString(stream.ToArray()));

                    writer.Reset(stream);
                    converter.Write(writer, (T)objectValue, null); // Test with null option
                    writer.Flush();
                    Assert.Equal(stringValue + stringValue, Encoding.UTF8.GetString(stream.ToArray()));
                }
        }
 public void RunSystemTextJson()
 {
     _utf8JsonWriter.Reset();
     JsonSerializer.Serialize(_utf8JsonWriter, _instance);
 }
 public void RunSystemTextJson()
 {
     _memoryStream.Position = 0;
     _utf8JsonWriter.Reset();
     JsonSerializer.Serialize(_utf8JsonWriter, _instance);
 }
Exemplo n.º 28
0
        public override void ThreadFunction()
        {
            ErrorCallback(this, "Network processing startup.", "", false);

            bool should_exit = false;

            byte[]         remote_bytes = Array.Empty <byte>();
            NetworkHeader  read_header  = null;
            Utf8JsonWriter jsonwriter;

            lock (_thread_interlock)
            {
                jsonwriter = new Utf8JsonWriter(DeviceStream);
                CommandQueue.Clear();
            }

            byte[] read_buffer = new byte[block_size];
            void DataReader()
            {
                try
                {
                    lock (_thread_interlock) DeviceStream.BeginRead(read_buffer, 0, read_buffer.Length, delegate(IAsyncResult ar)
                        {
                            try
                            {
                                lock (_thread_interlock)
                                {
                                    if (DeviceStream == null)
                                    {
                                        return;
                                    }
                                    int len = DeviceStream.EndRead(ar);
                                    int pos = remote_bytes.Length;
                                    Array.Resize(ref remote_bytes, pos + len);
                                    Buffer.BlockCopy(read_buffer, 0, remote_bytes, pos, len);
                                }
                            }
                            catch (Exception e)
                            {
                                bool exiting;
                                lock (_thread_interlock) exiting = _thread_end;
                                if (!exiting)
                                {
                                    ErrorCallback(this, "Network error.", e.Message, false);
                                }
                            }
                            DataReader();
                        }, null);
                }
                catch (Exception e)
                {
                    bool exiting;
                    lock (_thread_interlock) exiting = _thread_end;
                    if (!exiting)
                    {
                        ErrorCallback(this, "Network error.", e.Message, false);
                    }
                }
            }

            DataReader();

            while (!should_exit)
            {
                Thread.Yield();

                long consumed = 0;
                if (read_header == null)
                {
                    try
                    {
                        lock (_thread_interlock)
                        {
                            if (remote_bytes.Length > 0)
                            {
                                Utf8JsonReader jsonreader = new Utf8JsonReader(new ReadOnlySpan <byte>(remote_bytes));
                                read_header = JsonSerializer.Deserialize <NetworkHeader>(ref jsonreader);
                                consumed    = jsonreader.BytesConsumed;
                            }
                        }
                    }
                    catch (JsonException) { }
                }
                else
                {
                    CommandBase received_command = null;
                    try
                    {
                        lock (_thread_interlock)
                        {
                            if (remote_bytes.Length > 0)
                            {
                                Utf8JsonReader jsonreader = new Utf8JsonReader(new ReadOnlySpan <byte>(remote_bytes));
                                switch (read_header.ObjectType)
                                {
                                case TransmissionType.Nothing:
                                    break;

                                case TransmissionType.Ready:
                                    received_command = JsonSerializer.Deserialize <ReadyCommand>(ref jsonreader);
                                    break;

                                case TransmissionType.Panic:
                                    received_command = JsonSerializer.Deserialize <PanicCommand>(ref jsonreader);
                                    break;

                                case TransmissionType.Levels:
                                    received_command = JsonSerializer.Deserialize <LevelsCommand>(ref jsonreader);
                                    break;

                                case TransmissionType.Raw:
                                    received_command = JsonSerializer.Deserialize <RawCommand>(ref jsonreader);
                                    break;

                                case TransmissionType.Mode:
                                    received_command = JsonSerializer.Deserialize <ModeCommand>(ref jsonreader);
                                    break;

                                case TransmissionType.Response:
                                    received_command = JsonSerializer.Deserialize <DeviceResponse>(ref jsonreader);
                                    break;

                                case TransmissionType.Request:
                                    break;

                                case TransmissionType.Error:
                                    received_command = JsonSerializer.Deserialize <ErrorCommand>(ref jsonreader);
                                    break;
                                }
                                consumed = jsonreader.BytesConsumed;
                            }
                        }
                    }
                    catch (JsonException) { }

                    if (received_command != null)
                    {
                        switch (read_header.ObjectType)
                        {
                        case TransmissionType.Nothing:
                            break;

                        case TransmissionType.Ready:
                            QueueStateCallback(this, 0);
                            break;

                        case TransmissionType.Panic:
                            MassLevelSetCallback(this, 0, 0, 0, true);
                            ErrorCallback(this, "PANIC!", "Panic received from remote!", true);
                            break;

                        case TransmissionType.Levels:
                            LevelsCommand levels = (LevelsCommand)received_command;
                            LastA = levels.A;
                            LastB = levels.B;
                            MassLevelSetCallback(this, levels.A, levels.B, levels.MA, levels.Mode == ControlMode.absolute);
                            break;

                        case TransmissionType.Raw:
                            RawCommand raw = (RawCommand)received_command;
                            if (raw.Address == (int)AddressByte.Pot_A)
                            {
                                LastA = raw.Data;
                            }
                            if (raw.Address == (int)AddressByte.Pot_B)
                            {
                                LastB = raw.Data;
                            }
                            StateUpdatedCallback(this, raw.Address, raw.Data);
                            break;

                        case TransmissionType.Mode:
                            ModeCommand mode = (ModeCommand)received_command;
                            StateUpdatedCallback(this, (int)AddressByte.ModeOverride, mode.Mode);
                            StateUpdatedCallback(this, (int)AddressByte.Pot_MA, mode.MA);
                            break;

                        case TransmissionType.Request:
                            break;

                        case TransmissionType.Response:
                            DeviceResponse response = (DeviceResponse)received_command;
                            DataReturnedCallback(this, response.Address, response.Data);
                            double     current_unixtime = UnixTime.Current();
                            LevelEvent level            = new LevelEvent()
                            {
                                unixtime = current_unixtime
                            };
                            lock (_thread_interlock)
                            {
                                if (response.Address == (int)AddressByte.PulseAmp_A)
                                {
                                    level.level = (int)Math.Sqrt(LastA * response.Data);
                                    AmpHistoryA.Add(level);
                                }
                                if (response.Address == (int)AddressByte.PulseAmp_B)
                                {
                                    level.level = (int)Math.Sqrt(LastB * response.Data);
                                    AmpHistoryB.Add(level);
                                }
                            }
                            break;

                        case TransmissionType.Error:
                            ErrorCommand error = (ErrorCommand)received_command;
                            ErrorCallback(this, error.Error, error.Details, false);
                            break;
                        }
                        read_header = null;
                    }
                }

                if (consumed > 0)
                {
                    lock (_thread_interlock)
                    {
                        byte[] remaining = new byte[remote_bytes.Length - consumed];
                        Buffer.BlockCopy(remote_bytes, (int)consumed, remaining, 0, remaining.Length);
                        remote_bytes = remaining;
                    }
                }

                lock (_thread_interlock)
                {
                    if (CommandQueue.Count > 0)
                    {
                        CommandBase command = CommandQueue.First();
                        CommandQueue.RemoveAt(0);
                        NetworkHeader write_header = new NetworkHeader()
                        {
                            ObjectType = command.ObjectType
                        };
                        try
                        {
                            JsonSerializer.Serialize(jsonwriter, write_header, typeof(NetworkHeader));
                            jsonwriter.Flush();
                            jsonwriter.Reset();
                            switch (command.ObjectType)
                            {
                            case TransmissionType.Nothing:
                                break;

                            case TransmissionType.Ready:
                                JsonSerializer.Serialize(jsonwriter, (ReadyCommand)command, typeof(ReadyCommand));
                                break;

                            case TransmissionType.Panic:
                                JsonSerializer.Serialize(jsonwriter, (PanicCommand)command, typeof(PanicCommand));
                                break;

                            case TransmissionType.Levels:
                                JsonSerializer.Serialize(jsonwriter, (LevelsCommand)command, typeof(LevelsCommand));
                                break;

                            case TransmissionType.Raw:
                                JsonSerializer.Serialize(jsonwriter, (RawCommand)command, typeof(RawCommand));
                                break;

                            case TransmissionType.Mode:
                                JsonSerializer.Serialize(jsonwriter, (ModeCommand)command, typeof(ModeCommand));
                                break;

                            case TransmissionType.Request:
                                JsonSerializer.Serialize(jsonwriter, (RequestCommand)command, typeof(RequestCommand));
                                break;

                            case TransmissionType.Response:
                                JsonSerializer.Serialize(jsonwriter, (ResponseCommand)command, typeof(ResponseCommand));
                                break;

                            case TransmissionType.Error:
                                JsonSerializer.Serialize(jsonwriter, (ErrorCommand)command, typeof(ErrorCommand));
                                break;
                            }
                            jsonwriter.Flush();
                            jsonwriter.Reset();
                        }
                        catch (JsonException) { }
                        catch (IOException e) when(e.InnerException is SocketException)
                        {
                            _thread_end = true;
                            ErrorCallback(this, "Network error.", e.InnerException.Message, false);
                        }
                        catch (IOException e)
                        {
                            _thread_end = true;
                            ErrorCallback(this, "Network error.", e.Message, false);
                        }
                        catch (Exception e)
                        {
                            _thread_end = true;
                            ErrorCallback(this, "Network error.", e.Message, false);
                        }
                        if (CommandQueue.Count == 0 && command.ObjectType != TransmissionType.Ready)
                        {
                            CommandQueue.Add(new ReadyCommand());
                        }
                    }
                    should_exit = _thread_end;
                }
            }

            try
            {
                lock (_thread_interlock)
                {
                    if (DeviceStream != null)
                    {
                        DeviceStream.Close();
                        DeviceStream.Dispose();
                    }
                    DeviceStream = null;
                }
            }
            catch (Exception e)
            {
                ErrorCallback(this, "Network error.", e.Message, false);
            }

            ErrorCallback(this, "Network processing shutdown.", "", false);
        }
Exemplo n.º 29
0
 public void Write_String()
 {
     writer.Reset();
     writer.WriteString(propertyNameString, propertyValueString);
 }
Exemplo n.º 30
0
        // Currently supports https://github.com/grafana/loki/blob/master/docs/api.md#post-lokiapiv1push
        public void Format(IEnumerable <LogEvent> logEvents, ITextFormatter formatter, TextWriter output)
        {
            if (logEvents == null)
            {
                throw new ArgumentNullException(nameof(logEvents));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            if (!logEvents.Any())
            {
                return;
            }

            var timestampOverride = SystemClock.Instance.GetCurrentInstant();

            // process labels for grouping/sorting
            var sortedStreams = logEvents
                                .GroupBy(x => x.Properties
                                         .Where(prop => _labelNames.Contains(prop.Key))
                                         .Select(prop => new KeyValuePair <string, string>(prop.Key, cleanseString(prop.Value.ToString())))
                                         .Concat(_labelNames.Contains("level") ? new[] { new KeyValuePair <string, string>("level", GetLevel(x.Level)) } : new KeyValuePair <string, string>[] { })
                                         .Concat(_globalLabels).ToHashSet(), new HashSetComparer())
                                .Select(stream => new KeyValuePair <HashSet <KeyValuePair <string, string> >, IOrderedEnumerable <LogEvent> >(stream.Key, stream.OrderBy(log => log.Timestamp)));

            var logLineBuffer = new ArrayBufferWriter <byte>();

            using var logLineJsonWriter = new Utf8JsonWriter(logLineBuffer);
            var outputBuffer = new ArrayBufferWriter <byte>();

            using var jsonWriter = new Utf8JsonWriter(outputBuffer);

            jsonWriter.WriteStartObject();
            jsonWriter.WriteStartArray("streams");

            foreach (var stream in sortedStreams)
            {
                jsonWriter.WriteStartObject();
                jsonWriter.WriteStartObject("stream");

                foreach (var label in stream.Key)
                {
                    jsonWriter.WriteString(label.Key, label.Value);
                }

                jsonWriter.WriteEndObject();

                jsonWriter.WriteStartArray("values");

                foreach (var logEvent in stream.Value)
                {
                    jsonWriter.WriteStartArray();

                    var timestamp = this._preserveTimestamps ? Instant.FromDateTimeOffset(logEvent.Timestamp) : timestampOverride;
                    jsonWriter.WriteStringValue((timestamp.ToUnixTimeTicks() * 100).ToString());

                    // Construct a json object for the log line
                    logLineJsonWriter.WriteStartObject();
                    logLineJsonWriter.WriteString("message", logEvent.RenderMessage());

                    foreach (var property in logEvent.Properties)
                    {
                        logLineJsonWriter.WriteString(property.Key, cleanseString(property.Value.ToString()));
                    }

                    if (this._preserveTimestamps == false)
                    {
                        logLineJsonWriter.WriteString("timestamp", Instant.FromDateTimeOffset(logEvent.Timestamp).ToString());
                    }

                    if (logEvent.Exception != null)
                    {
                        var sb = new StringBuilder();
                        var e  = logEvent.Exception;
                        while (e != null)
                        {
                            sb.AppendLine(e.Message);
                            sb.AppendLine(e.StackTrace);
                            e = e.InnerException;
                        }
                        logLineJsonWriter.WriteString("exception", sb.ToString());
                    }

                    logLineJsonWriter.WriteEndObject();
                    logLineJsonWriter.Flush();
                    jsonWriter.WriteStringValue(Encoding.UTF8.GetString(logLineBuffer.WrittenSpan));
                    jsonWriter.WriteEndArray();
                    logLineJsonWriter.Reset();
                    logLineBuffer.Clear();
                }

                jsonWriter.WriteEndArray();
                jsonWriter.WriteEndObject();
            }

            jsonWriter.WriteEndArray();
            jsonWriter.WriteEndObject();
            jsonWriter.Flush();

            output.Write(Encoding.UTF8.GetString(outputBuffer.WrittenSpan));
        }