/// <summary> /// Send: sends an HL7 message to a host and returns the ACK/NACK response (if required). /// </summary> /// <param name="data"></param> /// <returns>byte[]</returns> public HL7Message Send(HL7Message hl7) { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { s.Connect(ep); if (s.Connected) { MllpPacket mllp = new MllpPacket(hl7); int count = s.Send((byte[])mllp); if (count > 0 && ackNackRequired) { SocketState state = new SocketState(); s.Receive(state.Buffer, 0, SocketState.BufferSize, SocketFlags.None); if (MllpPacket.IsValidMllpPacket(state.Buffer.ToList())) return new MllpPacket(state.Buffer).HL7Message; } } } finally { s.Close(); } return null; }
/// <summary> /// AcceptConnection /// </summary> /// <param name="ar"></param> private void AcceptConnection(IAsyncResult ar) { try { Socket s1 = (Socket)ar.AsyncState; Socket s2 = s1.EndAccept(ar); SocketState state = new SocketState() { Socket = s2 }; s2.BeginReceive(state.Buffer, 0, SocketState.BufferSize, SocketFlags.None, SocketReceive, state); s1.BeginAccept(AcceptConnection, s1); } catch (ObjectDisposedException) { // eat it } catch (Exception ex) { RaiseErrorEvent(ex); } }