Exemple #1
0
        /// <summary>
        /// Send a HL7 message
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <returns>Reply message</returns>
        public string SendHL7Message(string message)
        {
            message = MLLP.CreateMLLPMessage(message);

            // Send the message
            StreamWriter sw = new StreamWriter(streamToUse);

            sw.Write(message);
            sw.Flush();

            // Read the reply
            StringBuilder sb = new StringBuilder();
            bool          messageComplete = false;

            while (!messageComplete)
            {
                int b = streamToUse.ReadByte();
                if (b != -1)
                {
                    sb.Append((char)b);
                }

                messageComplete = MLLP.ValidateMLLPMessage(sb);
            }
            MLLP.StripMLLPContainer(sb);

            return(sb.ToString());
        }
        /// <summary>
        /// Send a HL7 message
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <returns>Reply message</returns>
        public string SendHL7Message(string message)
        {
            message = MLLP.CreateMLLPMessage(message);

            // Send the message
            StreamWriter sw = new StreamWriter(streamToUse);

            sw.Write(message);
            sw.Flush();

            var stopwatch = new Stopwatch();

            if (ReceiveTimeoutInSeconds > 0)
            {
                stopwatch.Start();
            }

            // Read the reply
            StringBuilder sb = new StringBuilder();
            bool          messageComplete = false;

            while (!messageComplete)
            {
                int b = streamToUse.ReadByte();
                if (b != -1)
                {
                    sb.Append((char)b);
                }

                messageComplete = MLLP.ValidateMLLPMessage(sb);

                if (ReceiveTimeoutInSeconds > 0)
                {
                    if (stopwatch.ElapsedMilliseconds / 1000 > ReceiveTimeoutInSeconds)
                    {
                        messageComplete = true;
                        stopwatch.Stop();
                        return(string.Format("Timed out waiting for response after {0} seconds", ReceiveTimeoutInSeconds));
                    }
                }
            }
            MLLP.StripMLLPContainer(sb);

            return(sb.ToString());
        }
Exemple #3
0
        /// <summary>
        /// Send a HL7 message
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <param name="timeout">Read timeout, throws exception when receiving no data until timeout</param>
        /// <returns>Reply message</returns>
        /// <exception cref="TimeoutException">throws exception on read timeout</exception>
        public string SendHL7Message(string message, double timeout = 30000)
        {
            message = MLLP.CreateMLLPMessage(message);

            // Send the message
            StreamWriter sw = new StreamWriter(streamToUse, encodingForStream);

            sw.Write(message);
            sw.Flush();

            // Read the reply
            StringBuilder sb = new StringBuilder();
            bool          messageComplete  = false;
            DateTime      startReadingTime = DateTime.Now;

            while (!messageComplete)
            {
                int b = streamToUse.ReadByte();
                if (b != -1)
                {
                    sb.Append((char)b);
                }

                messageComplete = MLLP.ValidateMLLPMessage(sb);

                if (b > 0) // reset start reading time for timout check
                {
                    startReadingTime = DateTime.Now;
                }

                if (!messageComplete && DateTime.Now.Subtract(startReadingTime).TotalMilliseconds > timeout)
                {
                    throw new TimeoutException($"Reading the HL7 reply timed out after {timeout} milliseconds.");
                }
            }
            MLLP.StripMLLPContainer(sb);

            return(sb.ToString());
        }