/// <summary> /// SocketReceive /// </summary> /// <param name="ar"></param> private void SocketReceive(IAsyncResult ar) { SocketState state = (SocketState)ar.AsyncState; try { int count = state.Socket.EndReceive(ar); if (count > 0) { state.Data.AddRange(state.Buffer, count); if (MllpPacket.IsValidMllpPacket(state.Data)) { try { if (AckNackReceived != null) { MllpPacket mllp = new MllpPacket(state.Data.ToArray()); RaiseAckNackReceived(state.HL7Message, mllp.HL7Message); } } catch { } state.Socket.Close(); return; } } state.Socket.BeginReceive(state.Buffer, 0, SocketState.BufferSize, SocketFlags.None, SocketReceive, state); } catch (Exception ex) { RaiseErrorEvent(ex, state.HL7Message); state.Socket.Close(); } }
/// <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> /// SocketConnect /// </summary> /// <param name="ar"></param> private void SocketConnect(IAsyncResult ar) { SocketState state = (SocketState)ar.AsyncState; try { state.Socket.EndConnect(ar); if (state.Socket.Connected) { MllpPacket mllpPacket = new MllpPacket(state.HL7Message); state.Socket.BeginSend(mllpPacket, 0, mllpPacket.Size, SocketFlags.None, SocketSend, state); } else throw new SocketException(); } catch (Exception ex) { RaiseErrorEvent(ex, state.HL7Message); } }