private void run() { int receiveNumber; String receiveStr; while (!socketCancel.IsCancellationRequested) { try { receiveNumber = clientSocket.Receive(recyBytes); receiveStr = Encoding.UTF8.GetString(recyBytes, 0, receiveNumber); //接收到数据 if (receiveStr.Length > 10 && receiveStr.Substring(0, 3) == "MSH") { //传回来为标准信息 hl7Manager.AddHL7ApplySample(receiveStr); //扔给队列交给线程处理 hl7Manager.HL7ApplySampleSignal.Set(); //唤醒线程 } } catch { isSocketRun = false; //连接出错,尝试重新连接 for (int i = 0; i < 3; i++) { Thread.Sleep(1000); try { clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); clientSocket.Connect(new IPEndPoint(ip, SERVER_PORT)); clientSocket.ReceiveTimeout = -1; clientSocket.SendTimeout = 500; } catch { continue; } if (clientSocket.Connected) { Console.WriteLine("重新连接成功"); //发送client名称 clientSocket.Send(Encoding.ASCII.GetBytes(GlobalVariable.DSDeviceID)); isSocketRun = true; break; } } if (!isSocketRun) { socketCancel.Cancel(); break; } } } }
private void sendSocket() { string HL7Message = string.Empty; string receiveString; PipeParser parser; IMessage m; int SendNum = 0; log.Info("HL7 send socket start."); while (!ProcessHL7Cancel.IsCancellationRequested && GlobalVariable.IsSocketRun) { if (hl7Manager.IsHL7Available) { #region 向HL7发送样本测试结果 if (SendNum == GlobalVariable.ReSendNum) { //连续发送失败超过规定次数,不再发送 hl7Manager.RemoveHL7();//移除队列中开始处的HL7 SendNum = 0; continue; } Statusbar.SBar.SoftStatus = GlobalVariable.miniBusy;// mini mode try { HL7Message = hl7Manager.GetHL7Message(); Connect.sendSocket(HL7Message); log.Info("HL7 send" + hl7Manager.GetHL7Sample_ID()); if (SendNum == 0) { ++Statusbar.SBar.SendNum; } ++SendNum; } catch { Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器发送失败\r\n请重新打开软件\r\n", "LIS"); break; } if (!GlobalVariable.IsOneWay)//双向 { receiveString = Connect.receiveSocket(); if (receiveString.Substring(0, 3) == "MSH")//相当于一个判断 { parser = new PipeParser(); m = parser.Parse(receiveString); ACK ack = m as ACK; if (ack.MSA.AcknowledgementCode.Value == "AA") { ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器接收成功\r\n", "LIS"); log.Info("LIS receive success" + hl7Manager.GetHL7Sample_ID()); Statusbar.SBar.SoftStatus = GlobalVariable.miniWaiting; // mini mode Statusbar.SBar.SampleId = hl7Manager.GetHL7Sample_ID(); //mini mode ++Statusbar.SBar.ReplyNum; SendNum = 0; UpdateDB.Invoke(hl7Manager.GetHL7Sample_ID(), hl7Manager.GetHL7Item(), hl7Manager.GetHL7Device(), hl7Manager.GetHL7Sample_SendTime()); hl7Manager.RemoveHL7();//移除队列中开始处的HL7 } else { Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器第" + SendNum.ToString() + "次接收失败\r\n", "LIS"); log.Info("LIS " + SendNum + " receive fail"); } } else { //接收异常 Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器第" + SendNum.ToString() + "次接收失败\r\n", "LIS"); log.Info("LIS " + SendNum + " receive fail"); } } else//单向 { Statusbar.SBar.SoftStatus = GlobalVariable.miniWaiting;// mini mode Statusbar.SBar.SampleId = hl7Manager.GetHL7Sample_ID();//mini mode ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器发送成功\r\n", "LIS"); log.Info("LIS receive success" + hl7Manager.GetHL7Sample_ID()); UpdateDB.Invoke(hl7Manager.GetHL7Sample_ID(), hl7Manager.GetHL7Item(), hl7Manager.GetHL7Device(), hl7Manager.GetHL7Sample_SendTime()); //直接回调 hl7Manager.RemoveHL7(); //移除队列中开始处的HL7 } #endregion } else if (hl7Manager.IsHL7RequestSampleDataAvailable) { #region 向LIS请求样本信息 Statusbar.SBar.SoftStatus = GlobalVariable.miniBusy;// mini mode string HL7Apply; //申请信息发送 try { HL7Apply = hl7Manager.GetHL7RequestSampleDataMessage(); Connect.sendSocket(HL7Apply); ++Statusbar.SBar.SendNum; } catch { Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器发送失败\r\n请重新打开软件\r\n", "LIS"); break; } receiveString = Connect.receiveSocket(); if (receiveString.Substring(0, 3) == "MSH") { //传回为标准消息 parser = new PipeParser(); m = parser.Parse(receiveString); QCK_Q02 qck = m as QCK_Q02; if (qck.QAK.QueryResponseStatus.Value == "OK")//代表有样本数据 { string receiveStringDSR; int DSRIndex = receiveString.IndexOf("DSR"); if (DSRIndex == -1) //如果刚才接收过来的没有DSR数据 { receiveStringDSR = Connect.receiveSocket(); //重新来接受DSR数据 } else { receiveStringDSR = receiveString.Substring(DSRIndex);//截取后面一部分得为DSR数据 } //解析DSR //解析完DSR数据就要回复ACKQ03消息,此时设备信息传出去 PipeParser parserDSR = new PipeParser(); IMessage mDSR = parserDSR.Parse(receiveStringDSR); DSR_Q03 dsr = mDSR as DSR_Q03; if (dsr.QAK.QueryResponseStatus.Value == "OK") //相当于再做了一层判断 { HL7Manager.HL7SampleInfo hl7info = HL7_ParserSampleInfo(receiveStringDSR); //接收成功后就要发送应答信号 RequestSampleData.BeginInvoke(hl7info, null, null); Connect.sendSocket(CreatACKQ03(dsr.MSH.ReceivingFacility.NamespaceID.Value)); Statusbar.SBar.SoftStatus = GlobalVariable.miniWaiting; // mini mode Statusbar.SBar.SampleId = hl7Manager.GetHL7RequestSampleDataSample_ID(); //mini mode ProcessHL7Message.Invoke(hl7Manager.GetHL7RequestSampleDataSample_ID() + "LIS服务器申请样本成功\r\n", "LIS"); log.Info("Lis request sample success " + hl7Manager.GetHL7RequestSampleDataSample_ID()); } else { Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode ProcessHL7Message.Invoke(hl7Manager.GetHL7RequestSampleDataSample_ID() + "LIS服务器申请样本异常\r\n", "LIS"); log.Info("Lis request sample fail " + hl7Manager.GetHL7RequestSampleDataSample_ID()); } } else if (qck.QAK.QueryResponseStatus.Value == "NF")//代表没有样本数据 { //此时不再回复消息 //结束 Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode ProcessHL7Message.Invoke(hl7Manager.GetHL7RequestSampleDataSample_ID() + "LIS服务器无相关样本信息\r\n", "LIS"); log.Info("Lis no request sample " + hl7Manager.GetHL7RequestSampleDataSample_ID()); } } #endregion hl7Manager.RemoveHL7RequestSampleData();//移除掉队列中的申请信息 } else { #region LIS主动发送样本信息 //hl7Manager.HL7Signal.Reset(); //持续监听LIS服务器 receiveString = Connect.receiveSocket(); if (receiveString.Length > 10 && receiveString.Substring(0, 3) == "MSH") { //传回来为标准信息 hl7Manager.AddHL7ApplySample(receiveString); //扔给队列交给线程处理 hl7Manager.HL7ApplySampleSignal.Set(); //唤醒线程 } #endregion } } }