private static void sendAck(NetworkStream stream) { byte[] ackMsg = System.Text.Encoding.UTF8.GetBytes( "\x0BMSH|^~\\&|||EPIC|EPICADT|" + DateTime.Now.Ticks + "||ACK||D|2.5\r" + "MSA|AA|\x1C\x0D"); stream.Write(ackMsg, 0, ackMsg.Length); MLog.MsgCapture("Sent : " + Tool.ByteArrayToString(ackMsg)); }
// // ______ __ ____ ____ ___ __ ___ ____ __ __ ___ ____ // | T / ]| \ | \ / _] / ] / _]l j| T | / _]| \ // | | / / | o ) | D ) / [_ / / / [_ | T | | | / [_ | D ) // l_j l_j/ / | _/ | / Y _] / / Y _] | | | | |Y _]| / // | | / \_ | | | \ | [_ / \_ | [_ | | l : !| [_ | \ // | | \ || | | . Y| T\ || T j l \ / | T| . Y // l__j \____jl__j l__j\_jl_____j \____jl_____j|____j \_/ l_____jl__j\_j // // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region TCP Receiver private static void TCP_Listen_Thread_Function() { TcpListener server = null; ADTMessage adt_message; try { initDatabase(); Int32 port = Convert.ToInt32(Global.TCP_PORT); if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { MLog.Error("No network available... Press any key to exit."); Console.ReadKey(); return; } IPAddress localAddr = IPAddress.Parse(LocalIPAddress()); // TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); // Buffer for reading data byte[] raw; string rawString = ""; // Enter the listening loop. while (true) { MLog.Info("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); MLog.Info("Connected!"); NetworkStream stream = client.GetStream(); int i = 0; while (client.Connected) { // If this truly loops around 1 time per message // then let the info about the message be known // MLog.Info("Yo, whatup!" + i++); try { // this reads exactly 1 message raw = blockingEOMRead(stream, 1000, 500, 1000, Global.EOM); //MLog.Info("Test: " + i); if (raw.Length == 0 || raw.Length == 1) { continue; } rawString = Tool.ByteArrayToString(raw); MLog.MsgCapture("Received: " + rawString); // This extracts data from the message string adt_message = new ADTMessage(rawString, DateTime.Now); // Read from/Write to Patient and Location tables in database if (!adt_message.skip) { adt_message.parse(); } if (!adt_message.skip || Global.logIfSkipped) { adt_message.Log(); } // Send an acknowledge sendAck(stream); } catch (Exception ex) { MLog.Error("Exception Detected in msg: |" + rawString + "| Closing Connection.\n" + ex.Message); client.Close(); break; } } } } catch (SocketException ex) { MLog.Error("FATAL: SocketException: " + ex); } catch (Exception ex) { MLog.Error("FATAL: " + ex); } finally { MLog.Info("Exiting"); MLog.saveLog(null, null); // Stop listening for new clients. server.Stop(); } }