public async Task <Command> ProcessCommandAsync(Command command, CancellationToken cancellationToken)
        {
            using (var content = GetContent(command))
                using (var request = new HttpRequestMessage(HttpMethod.Post, $"{_baseUrl}/commands"))
                {
                    request.Content = content;
                    using (var response = await _client.SendAsync(request, cancellationToken).ConfigureAwait(false))
                    {
                        response.EnsureSuccessStatusCode();
                        var jsonResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                        return((Command)_serializer.Deserialize(jsonResponse));
                    }
                }
        }
        private async Task ListenAsync(CancellationToken cancellationToken)
        {
            try
            {
                while (!cancellationToken.IsCancellationRequested && WebSocket.State == WebSocketState.Open)
                {
                    try
                    {
                        var buffer = new ArraySegment <byte>(_jsonBuffer.Buffer);
                        while (true)
                        {
                            var receiveResult =
                                await WebSocket.ReceiveAsync(buffer, cancellationToken).ConfigureAwait(false);

                            if (receiveResult.MessageType == WebSocketMessageType.Close)
                            {
                                await HandleCloseMessageAsync(receiveResult);

                                break;
                            }

                            if (receiveResult.MessageType != WebSocketMessageType.Text)
                            {
                                CloseStatus            = WebSocketCloseStatus.InvalidMessageType;
                                CloseStatusDescription = "An unsupported message type was received";
                                throw new InvalidOperationException(CloseStatusDescription);
                            }

                            _jsonBuffer.BufferCurPos += receiveResult.Count;
                            if (receiveResult.EndOfMessage)
                            {
                                break;
                            }
                        }

                        byte[] jsonBytes;
                        if (_jsonBuffer.TryExtractJsonFromBuffer(out jsonBytes))
                        {
                            var envelopeJson = Encoding.UTF8.GetString(jsonBytes);

                            if (_traceWriter != null &&
                                _traceWriter.IsEnabled)
                            {
                                await _traceWriter.TraceAsync(envelopeJson, DataOperation.Receive).ConfigureAwait(false);
                            }
                            var envelope = _envelopeSerializer.Deserialize(envelopeJson);
                            await _receivedEnvelopeBufferBlock.SendAsync(envelope, cancellationToken);
                        }
                    }
                    catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }
            finally
            {
                StopListenerTask();
            }
        }
        public override async Task <Envelope> ReceiveAsync(CancellationToken cancellationToken)
        {
            EnsureIsConnected();

            await _receiveSemaphore.WaitAsync(cancellationToken);

            try
            {
                using (var stream = await _webSocket.ReadMessageAsync(cancellationToken).ConfigureAwait(false))
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    if (stream == null)
                    {
                        return(null);
                    }
                    if (stream.MessageType != WebSocketMessageType.Text)
                    {
                        throw new NotSupportedException("An unsupported message type was received");
                    }

                    using (var reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        var envelopeJson = await reader.ReadToEndAsync().ConfigureAwait(false);
                        await TraceDataIfEnabledAsync(envelopeJson, DataOperation.Receive).ConfigureAwait(false);

                        return(_envelopeSerializer.Deserialize(envelopeJson));
                    }
                }
            }
            finally
            {
                _receiveSemaphore.Release();
            }
        }
        public async Task Invoke(HttpContext context)
        {
            if (context.Request.ContentType != null &&
                context.Request.ContentType.Equals("application/json") &&
                (context.Request.Path.StartsWithSegments("/messages") ||
                 context.Request.Path.StartsWithSegments("/notifications")))
            {
                try
                {
                    using (var reader = new StreamReader(context.Request.Body))
                    {
                        var json = await reader.ReadToEndAsync();

                        var envelope = _envelopeSerializer.Deserialize(json);
                        await _envelopeBuffer.SendAsync(envelope, context.RequestAborted);

                        context.Response.StatusCode = (int)HttpStatusCode.Accepted;
                    }
                }
                catch (Exception ex)
                {
                    context.Response.StatusCode  = (int)HttpStatusCode.BadRequest;
                    context.Response.ContentType = "text/plain";
                    using (var writer = new StreamWriter(context.Response.Body))
                    {
                        await writer.WriteAsync(ex.ToString());
                    }
                }
                return;
            }

            // Call the next delegate/middleware in the pipeline
            await _next(context);
        }
Beispiel #5
0
        private async Task SendCommandAsync(Command command, CancellationToken cancellationToken)
        {
            // Give fake responses for these specific commands that are not supported by the HTTP interface
            if (command.IsPingRequest() ||
                (command.Method == CommandMethod.Set &&
                 (command.Uri.ToString() == "/presence" ||
                  command.Uri.ToString() == "/receipt")))
            {
                await _envelopeBuffer.SendAsync(command.CreateSuccessResponse(), cancellationToken);

                return;
            }

            using (var content = GetContent(command))
                using (var response = await _client.PostAsync($"{_baseUri}/commands", content, cancellationToken).ConfigureAwait(false))
                {
                    response.EnsureSuccessStatusCode();

                    if (response.Content != null &&
                        response.Headers.Contains("Content-Type"))
                    {
                        await _envelopeBuffer.SendAsync(
                            _serializer.Deserialize((await response.Content.ReadAsStringAsync().ConfigureAwait(false))),
                            cancellationToken);
                    }
                }
        }
Beispiel #6
0
        private async Task <Envelope> GetEnvelopeFromBufferAsync()
        {
            Envelope envelope = null;

            byte[] jsonBytes;

            if (_jsonBuffer.TryExtractJsonFromBuffer(out jsonBytes))
            {
                var envelopeJson = Encoding.UTF8.GetString(jsonBytes);

                await TraceAsync(envelopeJson, DataOperation.Receive).ConfigureAwait(false);

                try
                {
                    envelope = _envelopeSerializer.Deserialize(envelopeJson);
                }
                catch (Exception ex)
                {
                    if (!_ignoreDeserializationErrors)
                    {
                        throw;
                    }

                    var deserializationErrorHandler = DeserializationErrorHandler;
                    if (deserializationErrorHandler != null)
                    {
                        await deserializationErrorHandler(envelopeJson, ex);
                    }
                }
            }
            return(envelope);
        }
Beispiel #7
0
        private void HandleReceivedData(RedisChannel channel, RedisValue value)
        {
            var envelopeJson = (string)value;

            _traceWriter.TraceIfEnabledAsync(envelopeJson, DataOperation.Receive).Wait();

            var envelope = _envelopeSerializer.Deserialize(envelopeJson);
            var session  = envelope as Session;

            if (session == null ||
                session.State != SessionState.New)
            {
                _traceWriter.TraceAsync("RedisTransportListener: An unexpected envelope was received", DataOperation.Error).Wait();
            }
            else
            {
                var transport = new RedisTransport(
                    _connectionMultiplexer,
                    _envelopeSerializer,
                    _traceWriter,
                    _channelNamespace,
                    RedisTransport.ClientChannelPrefix,
                    RedisTransport.ServerChannelPrefix);

                _transportBufferBlock.SendAsync(transport).Wait();
                transport.ReceivedEnvelopesBufferBlock.SendAsync(envelope).Wait();
            }
        }
Beispiel #8
0
 private void deserialize(Envelope envelope, ChannelNode receiver)
 {
     // TODO -- Not super duper wild about this one.
     if (envelope.Message == null)
     {
         envelope.Message = _serializer.Deserialize(envelope, receiver);
     }
 }
        private async Task <Envelope?> ReadEnvelopeAsync(HttpContext context)
        {
            using var reader = new StreamReader(context.Request.Body);
            var json = await reader.ReadToEndAsync();

            var envelope = _envelopeSerializer.Deserialize(json);

            return(envelope);
        }
Beispiel #10
0
        private void HandleReceivedData(RedisChannel channel, RedisValue value)
        {
            var envelopeJson = (string)value;

            _traceWriter.TraceIfEnabledAsync(envelopeJson, DataOperation.Receive).Wait();
            var envelope = _envelopeSerializer.Deserialize(envelopeJson);

            if (!ReceivedEnvelopesBufferBlock.Post(envelope))
            {
                _traceWriter.TraceIfEnabledAsync("RedisTransport: The input buffer has not accepted the envelope", DataOperation.Error).Wait();
            }
        }
Beispiel #11
0
        /// <summary>
        /// Reads one envelope from the stream
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override async Task <Envelope> ReceiveAsync(CancellationToken cancellationToken)
        {
            if (_stream == null)
            {
                throw new InvalidOperationException("The stream was not initialized. Call StartAsync first.");
            }

            await _receiveSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                Envelope envelope = null;

                while (envelope == null && _stream.CanRead)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    byte[] json;

                    if (this.TryExtractJsonFromBuffer(out json))
                    {
                        var jsonString = Encoding.UTF8.GetString(json);

                        if (_traceWriter != null &&
                            _traceWriter.IsEnabled)
                        {
                            await _traceWriter.TraceAsync(jsonString, DataOperation.Receive).ConfigureAwait(false);
                        }

                        envelope = _envelopeSerializer.Deserialize(jsonString);
                    }

                    if (envelope == null &&
                        _stream.CanRead)
                    {
                        _bufferCurPos += await _stream.ReadAsync(_buffer, _bufferCurPos, _buffer.Length - _bufferCurPos, cancellationToken).ConfigureAwait(false);

                        if (_bufferCurPos >= _buffer.Length)
                        {
                            await base.CloseAsync(CancellationToken.None).ConfigureAwait(false);

                            throw new InvalidOperationException("Maximum buffer size reached");
                        }
                    }
                }

                return(envelope);
            }
            finally
            {
                _receiveSemaphore.Release();
            }
        }
Beispiel #12
0
        /// <summary>
        /// Receives an envelope from the receive pipe.
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public async Task <Envelope> ReceiveAsync(CancellationToken cancellationToken)
        {
            if (_receiveTask == null)
            {
                throw new InvalidOperationException($"The receive task was not initialized. Call {nameof(StartAsync)} first.");
            }
            if (_receiveTask.IsCompleted)
            {
                throw new InvalidOperationException("The receive task is completed");
            }

            Envelope envelope = null;

            while (envelope == null &&
                   !cancellationToken.IsCancellationRequested)
            {
                var readResult = await _receivePipe.Reader.ReadAsync(cancellationToken).ConfigureAwait(false);

                var buffer = readResult.Buffer;
                if (readResult.IsCompleted || buffer.IsEmpty)
                {
                    // The receiveTask is completing, no need to thrown an exception.
                    break;
                }

                var consumed = buffer.Start;
                if (JsonBuffer.TryExtractJsonFromBuffer(buffer, out var json))
                {
                    var envelopeJson = Encoding.UTF8.GetString(json.ToArray());
                    await TraceAsync(envelopeJson, DataOperation.Receive).ConfigureAwait(false);

                    envelope = _envelopeSerializer.Deserialize(envelopeJson);
                    consumed = json.End;
                }

                if (envelope != null)
                {
                    // A envelope was found and the buffer may contain another one
                    _receivePipe.Reader.AdvanceTo(consumed);
                }
                else
                {
                    // No envelope found after examining the whole buffer, more data is needed
                    _receivePipe.Reader.AdvanceTo(consumed, buffer.End);
                }
            }

            return(envelope);
        }
Beispiel #13
0
        private async Task <Envelope> GetEnvelopeFromBufferAsync()
        {
            Envelope envelope = null;

            if (_jsonBuffer.TryExtractJsonFromBuffer(out var jsonBytes))
            {
                var envelopeJson = Encoding.UTF8.GetString(jsonBytes);

                await TraceAsync(envelopeJson, DataOperation.Receive).ConfigureAwait(false);

                envelope = _envelopeSerializer.Deserialize(envelopeJson);
            }

            return(envelope);
        }
Beispiel #14
0
        public static Envelope ExecuteDeserialization(IEnvelopeSerializer serializer, string json, int count)
        {
            var sw = System.Diagnostics.Stopwatch.StartNew();

            Envelope envelope = null;

            for (int i = 0; i < count; i++)
            {
                envelope = serializer.Deserialize(json);
            }
            sw.Stop();

            System.Console.WriteLine("{0}: {1} ms", serializer.GetType().Name, sw.ElapsedMilliseconds);

            return(envelope);
        }
        public async Task SendHugeEnvelope()
        {
            var sw = Stopwatch.StartNew();

            var path    = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
            var content = File.ReadAllLines(Path.Combine(path, "huge-json.txt"));

            var envelope = _envelopeSerializer.Deserialize(string.Join("", content));

            await _clientTransport.SendAsync(envelope, _cancellationToken);

            await _serverTransport.ReceiveAsync(_cancellationToken);

            sw.Stop();

            // Assert
            sw.ElapsedMilliseconds.ShouldBeLessThan(300);
        }
Beispiel #16
0
        public override async Task <Envelope> ReceiveAsync(CancellationToken cancellationToken)
        {
            if (_clientWebSocket.State != WebSocketState.Open)
            {
                throw new InvalidOperationException("The connection was not initialized. Call OpenAsync first.");
            }

            await _receiveSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                var buffer = new ArraySegment <byte>(_jsonBuffer.Buffer);
                while (true)
                {
                    var receiveResult = await _clientWebSocket.ReceiveAsync(buffer, cancellationToken).ConfigureAwait(false);

                    _jsonBuffer.BufferCurPos += receiveResult.Count;
                    if (receiveResult.EndOfMessage)
                    {
                        break;
                    }
                }

                byte[] jsonBytes;
                if (_jsonBuffer.TryExtractJsonFromBuffer(out jsonBytes))
                {
                    var envelopeJson = Encoding.UTF8.GetString(jsonBytes);

                    if (_traceWriter != null &&
                        _traceWriter.IsEnabled)
                    {
                        await _traceWriter.TraceAsync(envelopeJson, DataOperation.Receive).ConfigureAwait(false);
                    }

                    return(_envelopeSerializer.Deserialize(envelopeJson));
                }
            }
            finally
            {
                _receiveSemaphore.Release();
            }

            return(null);
        }
        private async Task <Command> RunCommandAsync(Command command)
        {
            var commandString = _envelopeSerializer.Serialize(command);

            using (var httpContent = new StringContent(commandString, Encoding.UTF8, "application/json"))
            {
                try
                {
                    var response = await _client.PostAsync("/commands", httpContent);

                    response.EnsureSuccessStatusCode();
                    var responseBody = await response.Content.ReadAsStringAsync();

                    return((Command)_envelopeSerializer.Deserialize(responseBody));
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Failed to run command");
                    throw ex;
                }
            }
        }
 public void UseSerializer(IEnvelopeSerializer serializer)
 {
     _message = new Lazy<object>(() => serializer.Deserialize(this));
 }
Beispiel #19
0
        public QueueMessageVisualization get_message_Port_QueueName_SourceInstanceId_MessageId(MessageInputModel input)
        {
            var messageId = MessageId.Parse($"{input.SourceInstanceId}/{input.MessageId}");

            var message = retrieveMessage(messageId, input.Port, input.QueueName);

            if (message == null)
            {
                return(new QueueMessageVisualization {
                    NotFound = true
                });
            }

            var model = new QueueMessageVisualization
            {
                MessageId = messageId,
                QueueName = message.Queue,
                SentAt    = message.SentAt,
                Headers   = message.Headers,
                Port      = input.Port
            };

            try
            {
                object payload;
                var    envelope = message.ToEnvelope();
                envelope.UseSerializer(_serializer);
                if (input.QueueName == "errors")
                {
                    var errorReport = ErrorReport.Deserialize(message.Data);
                    message = new Message
                    {
                        Data    = errorReport.RawData,
                        Headers = errorReport.Headers,
                        Id      = messageId,
                        Queue   = input.QueueName,
                    };
                    envelope = message.ToEnvelope();
                    var originalMessage = _serializer.Deserialize(envelope);
                    var errorSummary    = new ErrorSummary
                    {
                        exceptiontype    = errorReport.ExceptionType,
                        exceptionmessage = errorReport.ExceptionMessage,
                        exceptiontext    = errorReport.ExceptionText,
                        originalmessage  = originalMessage
                    };
                    payload = errorSummary;
                }
                else
                {
                    payload = envelope.Message;
                }


                model.Payload = JsonSerialization.ToJson(payload, true);
            }
            catch (Exception)
            {
                model.Payload = "Could not render as JSON";
            }

            return(model);
        }
Beispiel #20
0
        private async Task <Command> CommandRequestAsync(string endpoint, Envelope request, CancellationToken cancellationToken)
        {
            var response = await RequestAsync(endpoint, request, cancellationToken);

            return(string.IsNullOrEmpty(response) ? default : _envelopeSerializer.Deserialize(response) as Command);
        }
Beispiel #21
0
 public void UseSerializer(IEnvelopeSerializer serializer)
 {
     _message = new Lazy <object>(() => serializer.Deserialize(this));
 }
Beispiel #22
0
        public static Envelope ExecuteDeserialization(IEnvelopeSerializer serializer, string json, int count)
        {
            var sw = System.Diagnostics.Stopwatch.StartNew();

            Envelope envelope = null;

            for (int i = 0; i < count; i++)
            {
                envelope = serializer.Deserialize(json);
            }
            sw.Stop();

            System.Console.WriteLine("{0}: {1} ms", serializer.GetType().Name, sw.ElapsedMilliseconds);

            return envelope;

        }