Dicom.HL7.MLLP.Receive C# (CSharp) Method

Receive() public method

public Receive ( ) : string
return string
        public string Receive()
        {
            int ib = 0x00;
            MemoryStream ms = new MemoryStream();
            for (; _stream.ReadByte() != 0x0B; ) ;
            while (true) {
                if (ib == 0x1C) {
                    ib = _stream.ReadByte();
                    if (ib == 0x0D)
                        break;
                    ms.WriteByte(0x1C);
                    ms.WriteByte((byte)ib);
                }
                else {
                    ib = _stream.ReadByte();
                    if (ib != 0x1C)
                        ms.WriteByte((byte)ib);
                }
            }
            if (_version3) {
                _stream.Write(ACK, 0, ACK.Length);
                _stream.Flush();
            }
            #if SILVERLIGHT
            return Encoding.UTF8.GetString(ms.ToArray(), 0, (int)ms.Length);
            #else
            return Encoding.ASCII.GetString(ms.ToArray());
            #endif
        }

Usage Example

Example #1
0
        private void HL7ClientProc(object state)
        {
            Socket socket = (Socket)state;

            try {
                Interlocked.Increment(ref _clients);
                Debug.Log.Info("HL7 client connected: " + socket.RemoteEndPoint);

                NetworkStream stream = new NetworkStream(socket);
                MLLP          mllp   = new MLLP(stream, false, _encoding);

                while (socket.Connected && !_stop)
                {
                    if (!socket.Poll(100000, SelectMode.SelectRead))
                    {
                        continue;
                    }

                    try {
                        if (!stream.DataAvailable)
                        {
                            break;
                        }
                    } catch {
                        // http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.dataavailable.aspx
                        // "If the remote host shuts down or closes the connection, DataAvailable may throw a SocketException."
                        break;
                    }

                    string hl7 = mllp.Receive();

                    if (OnReceiveMessage != null)
                    {
                        try {
                            HL7v2 req = HL7v2.Parse(hl7);
                            HL7v2 rsp = OnReceiveMessage(mllp, req, socket);
                            if (rsp != null)
                            {
                                mllp.Send(rsp.ToString());
                            }
                        } catch (Exception ex) {
                            Debug.Log.Error("Error processing HL7 message: " + ex.ToString());
                        }
                    }
                }

                Debug.Log.Info("HL7 client closed: " + socket.RemoteEndPoint);

                try {
                    socket.Close();
                } catch {
                }
            } catch {
                Debug.Log.Info("HL7 client closed on error");
            } finally {
                Interlocked.Decrement(ref _clients);
            }
        }
All Usage Examples Of Dicom.HL7.MLLP::Receive