예제 #1
0
        public void WriteAndParseDateTimeConvertsToUTC(DateTimeKind dateTimeKind)
        {
            // The messagepack Timestamp format always converts input DateTime to Utc if they are passed as "DateTimeKind.Local" :
            // https://github.com/neuecc/MessagePack-CSharp/pull/520/files#diff-ed970b3daebc708ce49f55d418075979
            var originalDateTime = new DateTime(2018, 4, 9, 0, 0, 0, dateTimeKind);
            var writer           = MemoryBufferWriter.Get();

            try
            {
                HubProtocol.WriteMessage(CompletionMessage.WithResult("xyz", originalDateTime), writer);
                var bytes = new ReadOnlySequence <byte>(writer.ToArray());
                HubProtocol.TryParseMessage(ref bytes, new TestBinder(typeof(DateTime)), out var hubMessage);

                var completionMessage = Assert.IsType <CompletionMessage>(hubMessage);

                var resultDateTime = (DateTime)completionMessage.Result;
                // The messagepack Timestamp format specifies that time is stored as seconds since 1970-01-01 UTC
                // so the library has no choice but to store the time as UTC
                // https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
                // So If the original DateTiem was a "Local" one, we create a new DateTime equivalent to the original one but converted to Utc
                var expectedUtcDateTime = (originalDateTime.Kind == DateTimeKind.Local) ? originalDateTime.ToUniversalTime() : originalDateTime;

                Assert.Equal(expectedUtcDateTime, resultDateTime);
            }
            finally
            {
                MemoryBufferWriter.Return(writer);
            }
        }
예제 #2
0
 public byte[] WriteMessage(HubMessage hubMessage)
 {
     using (var ms = new MemoryStream())
     {
         _hubProtocol.WriteMessage(hubMessage, ms);
         return(_dataEncoder.Encode(ms.ToArray()));
     }
 }
예제 #3
0
 public static byte[] WriteToArray(this IHubProtocol hubProtocol, HubMessage message)
 {
     using (var ms = new MemoryStream())
     {
         hubProtocol.WriteMessage(message, ms);
         return(ms.ToArray());
     }
 }
예제 #4
0
 public static byte[] WriteToArray(this IHubProtocol hubProtocol, HubMessage message)
 {
     using (var writer = new MemoryBufferWriter())
     {
         hubProtocol.WriteMessage(message, writer);
         return(writer.ToArray());
     }
 }
예제 #5
0
 private async Task WriteLoopAsync()
 {
     while (await _outgoing.Reader.WaitToReadAsync())
     {
         if (_outgoing.Reader.TryRead(out var hubMessage))
         {
             _hubProtocol.WriteMessage(hubMessage, _connection.Transport.Output);
             await _connection.Transport.Output.FlushAsync();
         }
     }
 }
예제 #6
0
        /// <summary>
        /// Converts the specified <see cref="HubMessage"/> to its serialized representation.
        /// </summary>
        /// <param name="hubProtocol">The hub protocol.</param>
        /// <param name="message">The message to convert to bytes.</param>
        /// <returns>The serialized representation of the specified message.</returns>
        public static byte[] GetMessageBytes(this IHubProtocol hubProtocol, HubMessage message)
        {
            var writer = MemoryBufferWriter.Get();

            try
            {
                hubProtocol.WriteMessage(message, writer);
                return(writer.ToArray());
            }
            finally
            {
                MemoryBufferWriter.Return(writer);
            }
        }
예제 #7
0
        public void WriteAndParseDateTimeConvertsToUTC()
        {
            var dateTime = new DateTime(2018, 4, 9);
            var writer   = MemoryBufferWriter.Get();

            try
            {
                HubProtocol.WriteMessage(CompletionMessage.WithResult("xyz", dateTime), writer);
                var bytes = new ReadOnlySequence <byte>(writer.ToArray());
                HubProtocol.TryParseMessage(ref bytes, new TestBinder(typeof(DateTime)), out var hubMessage);

                var completionMessage = Assert.IsType <CompletionMessage>(hubMessage);

                var resultDateTime = (DateTime)completionMessage.Result;
                // The messagepack Timestamp format specifies that time is stored as seconds since 1970-01-01 UTC
                // so the library has no choice but to store the time as UTC
                // https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
                Assert.Equal(dateTime.ToUniversalTime(), resultDateTime);
            }
            finally
            {
                MemoryBufferWriter.Return(writer);
            }
        }
예제 #8
0
        public void CustomWriteMessage(string protocolTestDataName)
        {
            var testData = CustomProtocolTestData[protocolTestDataName];

            var expectedOutput = Frame(testData.Json);

            var writer = MemoryBufferWriter.Get();

            try
            {
                JsonHubProtocol.WriteMessage(testData.Message, writer);
                var json = Encoding.UTF8.GetString(writer.ToArray());

                Assert.Equal(expectedOutput, json);
            }
            finally
            {
                MemoryBufferWriter.Return(writer);
            }
        }
예제 #9
0
 public void WriteMessage(HubMessage message, IBufferWriter <byte> output)
 {
     _innerProtocol.WriteMessage(message, output);
 }