AutoDiscoveryEngine.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using PRSServices;
  9. namespace PRSServer;
  10. internal class AutoDiscoveryEngine : Engine<AutoDiscoveryServerProperties>
  11. {
  12. public override void Run()
  13. {
  14. var settings = new AutoDiscoverySettings();
  15. settings.Name = Properties.DisplayName;
  16. var urls = Properties.ServerURLs;
  17. if (String.IsNullOrWhiteSpace(urls))
  18. {
  19. var url = CoreUtils.GetPropertyValue(Properties, "ServerURL") as String;
  20. var port = CoreUtils.GetPropertyValue(Properties, "ServerPort");
  21. urls = !String.IsNullOrWhiteSpace(url) ? $"{url}:{port}" : "";
  22. }
  23. settings.URLs = urls.Split(';');
  24. settings.Protocol = Properties.Protocol;
  25. settings.LibraryLocation = Properties.LibraryLocation;
  26. settings.GoogleAPIKey = Properties.GoogleAPIKey;
  27. var responseData = Encoding.ASCII.GetBytes(Serialization.Serialize(settings));
  28. while (true)
  29. {
  30. var server = new UdpClient(8888);
  31. var clientEp = new IPEndPoint(IPAddress.Any, 0);
  32. var clientRequestData = server.Receive(ref clientEp);
  33. Logger.Send(LogType.Information, "", string.Format("Processing Request from {0}", clientEp));
  34. server.Send(responseData, responseData.Length, clientEp);
  35. Logger.Send(LogType.Information, "", string.Format("- Sending: {0} bytes", responseData.Length));
  36. server.Close();
  37. Logger.Send(LogType.Information, "", "- Closed");
  38. }
  39. }
  40. public override void Stop()
  41. {
  42. }
  43. }