예제 #1
0
파일: HL7v2.cs 프로젝트: gogorrr/mdcm
        public static HL7v2 Parse(string hl7)
        {
            HL7v2 msg = new HL7v2();
            msg._field_delim = '|';
            msg._component_delim = '^';
            msg._subcomponent_delim = '&';

            string line = null;
            StringReader reader = new StringReader(hl7);
            while ((line = reader.ReadLine()) != null) {
                line = line.Trim();
                if (line == String.Empty)
                    continue;

                string id = line.Substring(0, 3);
                if (IsHeaderSegment(id)) {
                    msg._field_delim = line[3];
                    msg._component_delim = line[4];
                    msg._subcomponent_delim = line[7];

                    List<string> segment = new List<string>();
                    segment.Add(id);
                    segment.Add(new string(new char[] { msg._field_delim }));
                    segment.AddRange(line.Substring(4).Split(msg._field_delim));

                    msg._segments.Add(segment);
                } else {
                    msg._segments.Add(new List<string>(line.Split(msg._field_delim)));
                }
            }

            return msg;
        }
예제 #2
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);
            }
        }
예제 #3
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);

                while (socket.Connected && !_stop)
                {
                    if (!stream.DataAvailable)
                    {
                        Thread.Sleep(50);
                        continue;
                    }
                    string message = mllp.Receive();

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

                try {
                    socket.Close();
                } catch {
                }

                Debug.Log.Info("HL7 client closed: " + socket.RemoteEndPoint);
            } catch {
                Debug.Log.Info("HL7 client closed on error: " + socket.RemoteEndPoint);
            } finally {
                Interlocked.Decrement(ref _clients);
            }
        }
예제 #4
0
        public static HL7v2 Parse(string hl7)
        {
            HL7v2 msg = new HL7v2();

            msg._field_delim        = '|';
            msg._component_delim    = '^';
            msg._subcomponent_delim = '&';

            string       line   = null;
            StringReader reader = new StringReader(hl7);

            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();
                if (line == String.Empty)
                {
                    continue;
                }

                string id = line.Substring(0, 3);
                if (IsHeaderSegment(id))
                {
                    msg._field_delim        = line[3];
                    msg._component_delim    = line[4];
                    msg._subcomponent_delim = line[7];

                    List <string> segment = new List <string>();
                    segment.Add(id);
                    segment.Add(new string(new char[] { msg._field_delim }));
                    segment.AddRange(line.Substring(4).Split(msg._field_delim));

                    msg._segments.Add(segment);
                }
                else
                {
                    msg._segments.Add(new List <string>(line.Split(msg._field_delim)));
                }
            }

            return(msg);
        }