private readonly object deviceListLockObject = new object(); // Lock object to synchronise access to the Alpaca device list collection, which is not a thread safe collection #region New and IDisposable Support /// <summary> /// Initialiser that takes a trace logger /// </summary> /// <param name="tl">Trace logger instance to use for activity logging</param> internal AlpacaDiscovery(ITraceLoggerUtility tl) { try { // Save the supplied trace logger object TL = tl; // Initialise variables tryDnsNameResolution = false; // Initialise so that there is no host name resolution by default DiscoveryComplete = true; // Initialise so that discoveries can be run // Initialise utility objects discoveryCompleteTimer = new System.Threading.Timer(OnDiscoveryCompleteTimer); if (finder != null) { finder.Dispose(); finder = null; } finder = new Finder(BroadcastResponseEventHandler, TL); // Get a new broadcast response finder LogMessage("AlpacaDiscoveryInitialise", $"Complete - Running on thread {Thread.CurrentThread.ManagedThreadId}"); } catch (Exception ex) { LogMessage("AlpacaDiscoveryInitialise", $"Exception{ex.ToString()}"); } }
/// <summary> /// Creates a Alpaca Finder object that sends out a search request for Alpaca devices /// The results will be sent to the callback and stored in the cache /// Calling search and concatenating the results reduces the chance that a UDP packet is lost /// This may require firewall access /// </summary> /// <param name="callback">A callback function to receive the endpoint result</param> internal Finder(Action <IPEndPoint, AlpacaDiscoveryResponse> callback, ITraceLoggerUtility traceLogger) { TL = traceLogger; // Save the trace logger object LogMessage("Finder", "Starting Initialisation..."); callbackFunctionDelegate = callback; udpClient = new UdpClient(); udpClient.EnableBroadcast = true; udpClient.MulticastLoopback = false; //0 tells OS to give us a free ethereal port udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, 0)); udpClient.BeginReceive(FinderDiscoveryCallback, udpClient); LogMessage("Finder", "Initialised"); }