using System; using System.Net; using System.Net.Sockets; using SafeMobileLib; using System.Threading; namespace MotoRepeater { class TelemetryReceiveThread { private static UdpClient udpClient; private ushort telemetryPort = 0; public TelemetryReceiveThread(ushort port) { telemetryPort = port; } public void handleConnection() { Utils.WriteLine("♥♥♥ Telemetry Receive Thread Started on port " + telemetryPort); udpClient = null; try { udpClient = new UdpClient(); udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, telemetryPort)); } catch (Exception ex) { Utils.WriteLine("TelemetryReceiveThread handleConnection exception: " + ex.ToString(), ConsoleColor.Red); } IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); while (MotoRepeater_GW.isRunning) { try { // Blocks until a message returns on this socket from a remote host. Byte[] receivedBytes = udpClient.Receive(ref remoteIpEndPoint); char[] separator = { '.' }; string[] su = remoteIpEndPoint.Address.ToString().Split(separator); uint radioID = (Convert.ToUInt32(su[1])) * 256 * 256 + (Convert.ToUInt32(su[2])) * 256 + Convert.ToUInt32(su[3]); string suid = radioID.ToString(); int seqNo = receivedBytes[0] * 256 + receivedBytes[1]; String seqID = TelemetryConfirm.getFromConfirmationQueue(seqNo); Utils.WriteLine("TelemetryReceiveThread received data from radio " + radioID + "[" + seqID + "]", ConsoleColor.Red); //Utils.printBytesArray(receivedBytes); // raise the event OnTelemetryBytesReceived(radioID, receivedBytes); Thread.Sleep(100); } catch (Exception e) { if(MotoRepeater_GW.isRunning) Utils.WriteLine("##### TelemetryThread Exception #########\n" + e.ToString(), ConsoleColor.Red); } } } public delegate void TelemetryReceivedBytes(Int64 radioID, byte[] received); public event TelemetryReceivedBytes OnTelemetryBytesReceived; } }