Пример #1
0
        private async Task SendResponse(byte[] messageBuilder, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var s       = _encoding.GetString(messageBuilder);
            var message = new Hl7Message(s, _client.Client.RemoteEndPoint.ToString());
            var result  = await _middleware.Handle(message).ConfigureAwait(false);

            await WriteToStream(result, cancellationToken).ConfigureAwait(false);
        }
        /// <summary>
        /// Handles the passed <see cref="Hl7Message"/> message.
        /// </summary>
        /// <param name="message">The <see cref="Hl7Message"/> to handle.</param>
        /// <param name="cancellation"></param>
        /// <returns>An HL7 response as a <see cref="string"/>.</returns>
        public async Task <IMessage> Handle(
            Hl7Message message,
            CancellationToken cancellation = default)
        {
            cancellation.ThrowIfCancellationRequested();
            var structureName = message.Message.GetStructureName();
            var handler       = _handlers[message.Message.Version + structureName];
            var response      = await handler.Handle(message.Message, cancellation).ConfigureAwait(false);

            return(response);
        }
Пример #3
0
 /// <summary>
 /// Reads the stream and returns the received message.
 /// </summary>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <exception cref="Exception">When none expected characters are received.</exception>
 private void ReadStream(CancellationToken cancellationToken)
 {
     try
     {
         byte previous = 0;
         this.readingStream = true;
         var messageBuilder = new List <byte>();
         while (this.IsConnectionEstablished())
         {
             cancellationToken.ThrowIfCancellationRequested();
             var current = (byte)this.stream.ReadByte();
             if (Constants.EndBlock.SequenceEqual(new[] { previous, current }))
             {
                 messageBuilder.RemoveAt(messageBuilder.Count - 1);
                 var responseAsString = this.connectionDetails.Encoding.GetString(messageBuilder.ToArray());
                 var message          = new Hl7Message(responseAsString, this.tcpClient.Client.RemoteEndPoint.ToString());
                 this.completionSource.SetResult(message);
                 this.readingStream = false;
                 break;
             }
             if (previous == 0 && current == 11)
             {
                 if (messageBuilder.Count > 0)
                 {
                     throw new Exception($"Unexpected character: {current:x2}");
                 }
             }
             else
             {
                 messageBuilder.Add(current);
                 previous = current;
             }
         }
     }
     catch (IOException io)
     {
         this.completionSource.SetException(io);
         this.readingStream = false;
     }
 }
Пример #4
0
        private async Task SendResponse(
            byte[] messageBuilder,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var s        = _encoding.GetString(messageBuilder);
            var received = _parser.Parse(s);
            var message  = new Hl7Message(
                received,
                _client.Client.RemoteEndPoint.ToString());
            await _messageLog.Write(s).ConfigureAwait(false);

            var result = await _middleware.Handle(message, cancellationToken)
                         .ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();
            string resultMsg = _parser.Encode(result);

            await WriteToStream(resultMsg, cancellationToken)
            .ConfigureAwait(false);

            await _messageLog.Write(resultMsg);
        }
Пример #5
0
        private async Task ReadStream(CancellationToken cancellationToken)
        {
            await Task.Yield();

            try
            {
                byte previous       = 0;
                var  messageBuilder = new List <byte>();
                while (!cancellationToken.IsCancellationRequested)
                {
                    var current = (byte)_stream.ReadByte();

                    cancellationToken.ThrowIfCancellationRequested();
                    if (Constants.EndBlock.SequenceEqual(
                            new[] { previous, current }))
                    {
                        messageBuilder.RemoveAt(messageBuilder.Count - 1);
                        var s = _encoding.GetString(messageBuilder.ToArray());
                        messageBuilder.Clear();
                        previous = 0;
                        var msg              = _parser.Parse(s);
                        var message          = new Hl7Message(msg, _remoteAddress);
                        var controlId        = msg.GetMessageControlId();
                        var completionSource = _messages[controlId];
                        _messages.Remove(controlId);
                        completionSource.SetResult(message);
                        continue;
                    }

                    if (previous == 0 && current == 11)
                    {
                        if (messageBuilder.Count > 0)
                        {
                            foreach (var completionSource in _messages.Values)
                            {
                                completionSource.SetException(
                                    new Exception(
                                        $"Unexpected character: {current:x2}"));
                            }

                            _messages.Clear();
                        }
                    }
                    else
                    {
                        messageBuilder.Add(current);
                        previous = current;
                    }
                }
            }
            catch (IOException io)
            {
                Trace.TraceInformation(io.Message);
                if (!cancellationToken.IsCancellationRequested)
                {
                    foreach (var completionSource in _messages.Values)
                    {
                        completionSource.SetException(io);
                    }

                    _messages.Clear();
                }
            }
        }